Skip to content

Commit 70eb289

Browse files
authored
fix: [1720] Fix bug where nested query selector is not returning the correct result when there are multiple matching selectorGroups (#1721)
1 parent 9cb1d32 commit 70eb289

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

packages/happy-dom/src/query-selector/QuerySelector.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@ export default class QuerySelector {
601601
rootElement,
602602
[nextElementSibling],
603603
selectorItems.slice(1),
604-
cachedItem
604+
cachedItem,
605+
position
605606
);
606607
if (match) {
607608
return match;
@@ -614,7 +615,8 @@ export default class QuerySelector {
614615
rootElement,
615616
childrenOfChild,
616617
selectorItems.slice(1),
617-
cachedItem
618+
cachedItem,
619+
position
618620
);
619621
if (match) {
620622
return match;
@@ -628,7 +630,8 @@ export default class QuerySelector {
628630
rootElement,
629631
[sibling],
630632
selectorItems.slice(1),
631-
cachedItem
633+
cachedItem,
634+
position
632635
);
633636
if (match) {
634637
return match;
@@ -640,7 +643,13 @@ export default class QuerySelector {
640643
}
641644

642645
if (selectorItem.combinator === SelectorCombinatorEnum.descendant && childrenOfChild.length) {
643-
const match = this.findFirst(rootElement, childrenOfChild, selectorItems, cachedItem);
646+
const match = this.findFirst(
647+
rootElement,
648+
childrenOfChild,
649+
selectorItems,
650+
cachedItem,
651+
position
652+
);
644653

645654
if (match) {
646655
return match;

packages/happy-dom/test/query-selector/QuerySelector.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,5 +1946,24 @@ describe('QuerySelector', () => {
19461946

19471947
expect(div.querySelector('.a,h1')).toBe(div.children[0].children[0]);
19481948
});
1949+
1950+
it('Matches grouped selectors with nesting', () => {
1951+
const div = document.createElement('div');
1952+
1953+
div.innerHTML = `
1954+
<div>
1955+
<blockquote>
1956+
<div class="a">
1957+
<ul>
1958+
<li><span>Item 1</span></li>
1959+
<li><span>Item 2</span></li>
1960+
</ul>
1961+
</div>
1962+
</blockquote>
1963+
</div>
1964+
`;
1965+
1966+
expect(div.querySelector('.a,BLOCKQUOTE')).toBe(div.children[0].children[0]);
1967+
});
19491968
});
19501969
});

0 commit comments

Comments
 (0)