Skip to content

Commit 726f51b

Browse files
authored
fix: Don't add siblings multiple times in appendNextSiblings (#656)
1 parent 0e526af commit 726f51b

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ function appendNextSiblings<Node, ElementNode extends Node>(
115115
): Node[] {
116116
// Order matters because jQuery seems to check the children before the siblings
117117
const elems = Array.isArray(elem) ? elem.slice(0) : [elem];
118+
const elemsLength = elems.length;
118119

119-
for (let i = 0; i < elems.length; i++) {
120+
for (let i = 0; i < elemsLength; i++) {
120121
const nextSiblings = getNextSiblings(elems[i], adapter);
121122
elems.push(...nextSiblings);
122123
}

test/api.ts

+8
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ describe("API", () => {
255255
).toStrictEqual(p);
256256
});
257257

258+
it("should not crash when siblings repeat", () => {
259+
const dom = parseDOM(`<div></div>`.repeat(51)) as Element[];
260+
261+
expect(
262+
CSSselect.selectAll("+div", dom, { context: dom })
263+
).toHaveLength(50);
264+
});
265+
258266
it("should cache results by default", () => {
259267
const [dom] = parseDOM(
260268
'<div id="foo"><p>bar</p></div>'

0 commit comments

Comments
 (0)