From adcad2775b77fbc8d57dea0249a316659a1056eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20B=C3=B6hm?= <188768+fb55@users.noreply.github.com> Date: Sun, 26 Dec 2021 12:36:11 +0100 Subject: [PATCH] fix: Don't add siblings multiple times in `appendNextSiblings` This bug was hidden due to `removeSubsets` ordering elements. Fixes #655 --- src/index.ts | 3 ++- test/api.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index afd936a5..a4f7ccf5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -115,8 +115,9 @@ function appendNextSiblings( ): Node[] { // Order matters because jQuery seems to check the children before the siblings const elems = Array.isArray(elem) ? elem.slice(0) : [elem]; + const elemsLength = elems.length; - for (let i = 0; i < elems.length; i++) { + for (let i = 0; i < elemsLength; i++) { const nextSiblings = getNextSiblings(elems[i], adapter); elems.push(...nextSiblings); } diff --git a/test/api.ts b/test/api.ts index 1f2080a5..23f7603e 100644 --- a/test/api.ts +++ b/test/api.ts @@ -255,6 +255,14 @@ describe("API", () => { ).toStrictEqual(p); }); + it("should not crash when siblings repeat", () => { + const dom = parseDOM(`
`.repeat(51)) as Element[]; + + expect( + CSSselect.selectAll("+div", dom, { context: dom }) + ).toHaveLength(50); + }); + it("should cache results by default", () => { const [dom] = parseDOM( '

bar

'