Skip to content

Commit

Permalink
fix: [#1683] Fix not recognizing subsequent sibling combinator in Que…
Browse files Browse the repository at this point in the history
…rySelector
  • Loading branch information
karpiuMG committed Jan 13, 2025
1 parent ecbf335 commit 123a765
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
24 changes: 24 additions & 0 deletions packages/happy-dom/src/query-selector/QuerySelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,15 @@ export default class QuerySelector {
)
);
break;
case SelectorCombinatorEnum.subsequentSibling:
let sibling = child.nextElementSibling;
while (sibling) {
matched = matched.concat(
this.findAll(rootElement, [sibling], selectorItems.slice(1), cachedItem, position)
);
sibling = sibling.nextElementSibling;
}
break;
}
}
}
Expand Down Expand Up @@ -570,6 +579,21 @@ export default class QuerySelector {
return match;
}
break;
case SelectorCombinatorEnum.subsequentSibling:
let sibling = child.nextElementSibling;
while (sibling) {
const match = this.findFirst(
rootElement,
[sibling],
selectorItems.slice(1),
cachedItem
);
if (match) {
return match;
}
sibling = sibling.nextElementSibling;
}
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
enum SelectorCombinatorEnum {
descendant = 'descendant',
child = 'child',
adjacentSibling = 'adjacentSibling'
adjacentSibling = 'adjacentSibling',
subsequentSibling = 'subsequentSibling'
}

export default SelectorCombinatorEnum;
9 changes: 8 additions & 1 deletion packages/happy-dom/src/query-selector/SelectorParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import ISelectorPseudo from './ISelectorPseudo.js';
* Group 17: Combinator.
*/
const SELECTOR_REGEXP =
/(\*)|([a-zA-Z0-9-]+)|#((?:[a-zA-Z0-9-_]|\\.)+)|\.((?:[a-zA-Z0-9-_]|\\.)+)|\[([a-zA-Z0-9-_\\:]+)\]|\[([a-zA-Z0-9-_\\:]+)\s*([~|^$*]{0,1})\s*=\s*["']{1}([^"']*)["']{1}\s*(s|i){0,1}\]|\[([a-zA-Z0-9-_]+)\s*([~|^$*]{0,1})\s*=\s*([^\]]*)\]|:([a-zA-Z-]+)\s*\(([^)]+)\){0,1}|:([a-zA-Z-]+)|::([a-zA-Z-]+)|([\s,+>]*)/gm;
/(\*)|([a-zA-Z0-9-]+)|#((?:[a-zA-Z0-9-_]|\\.)+)|\.((?:[a-zA-Z0-9-_]|\\.)+)|\[([a-zA-Z0-9-_\\:]+)\]|\[([a-zA-Z0-9-_\\:]+)\s*([~|^$*]{0,1})\s*=\s*["']{1}([^"']*)["']{1}\s*(s|i){0,1}\]|\[([a-zA-Z0-9-_]+)\s*([~|^$*]{0,1})\s*=\s*([^\]]*)\]|:([a-zA-Z-]+)\s*\(([^)]+)\){0,1}|:([a-zA-Z-]+)|::([a-zA-Z-]+)|([\s,+>~]*)/gm;

/**
* Escaped Character RegExp.
Expand Down Expand Up @@ -193,6 +193,13 @@ export default class SelectorParser {
});
currentGroup.push(currentSelectorItem);
break;
case '~':
currentSelectorItem = new SelectorItem({
combinator: SelectorCombinatorEnum.subsequentSibling,
ignoreErrors
});
currentGroup.push(currentSelectorItem);
break;
case '':
currentSelectorItem = new SelectorItem({
combinator: SelectorCombinatorEnum.descendant,
Expand Down
23 changes: 23 additions & 0 deletions packages/happy-dom/test/query-selector/QuerySelector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,29 @@ describe('QuerySelector', () => {
expect(firstDivC === null).toBe(true);
});

it('subsequentSibling combinator should select subsequent siblings', () => {
const div = document.createElement('div');

div.innerHTML = `
<div class="a">a1</div>
<div class="b">b1</div>
<div class="c">c1</div>
<div class="a">a2</div>
<div class="b">b2</div>
<div class="a">a3</div>
`;
const firstDivB = div.querySelector('.a ~ .b');
const secondChildren = div.children[1];

expect(secondChildren === firstDivB).toBe(true);
expect(firstDivB?.textContent).toBe('b1');

const allSubsequentSiblingsA = QuerySelector.querySelectorAll(div, '.a ~ .a');
expect(allSubsequentSiblingsA.length).toBe(2);
expect(allSubsequentSiblingsA[0].textContent).toBe('a2');
expect(allSubsequentSiblingsA[1].textContent).toBe('a3');
});

it('Returns all elements with matching attributes using "[attr1="value1"]".', () => {
const container = document.createElement('div');
container.innerHTML = QuerySelectorHTML;
Expand Down

0 comments on commit 123a765

Please sign in to comment.