Skip to content

Commit

Permalink
Merge pull request #687 from takaya1992/task/686-bugfix-query-selecto…
Browse files Browse the repository at this point in the history
…r-n-infinite-loop

#686@patch: Bugfix query selector n infinite loop.
  • Loading branch information
capricorn86 committed Jan 5, 2023
2 parents 3ef9a2a + 43a5017 commit 67d95f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/happy-dom/src/query-selector/SelectorItem.ts
Expand Up @@ -170,7 +170,7 @@ export default class SelectorItem {
} else if (place.includes('n')) {
const [a, b] = place.replace(/ /g, '').split('n');
const childIndex = children.indexOf(element);
const aNumber = Number(a);
const aNumber = a !== '' ? Number(a) : 1;
const bNumber = b !== undefined ? Number(b) : 0;
if (isNaN(aNumber) || isNaN(bNumber)) {
throw new DOMException(`The selector "${this.selector}" is not valid.`);
Expand Down
10 changes: 10 additions & 0 deletions packages/happy-dom/test/query-selector/QuerySelector.test.ts
Expand Up @@ -470,6 +470,16 @@ describe('QuerySelector', () => {
expect(elements[1]).toBe(container.children[0].children[1].children[1]);
});

it('Returns all elements matching ":nth-child(n+8)".', () => {
const container = document.createElement('div');
container.innerHTML = QuerySelectorNthChildHTML;
const elements = container.querySelectorAll(':nth-child(n+8)');

expect(
elements.map((element) => `${element.tagName.toLowerCase()}.${element.className}`)
).toEqual(['span.n8', 'div.n9', 'i.n10']);
});

it('Returns all elements matching :nth-child(2n).', () => {
const container = document.createElement('div');
container.innerHTML = QuerySelectorNthChildHTML;
Expand Down

0 comments on commit 67d95f0

Please sign in to comment.