Skip to content

Commit

Permalink
fix: [#1072] Handle nested square brackets and parentheses inside pse…
Browse files Browse the repository at this point in the history
…udo-class arguments (#1752)

Co-authored-by: David Ortner <david@ortner.se>
  • Loading branch information
karpiuMG and capricorn86 authored Mar 4, 2025
1 parent 6f7057d commit 0452d7c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 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
15 changes: 15 additions & 0 deletions packages/happy-dom/test/query-selector/QuerySelector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,21 @@ describe('QuerySelector', () => {
expect(subsequentSiblings3[0].textContent).toBe('a2');
expect(subsequentSiblings3[1].textContent).toBe('a3');
});

it('Returns all elements for attribute selector with round brackets within', () => {
const div = document.createElement('div');

div.innerHTML = `
<span>loremipsum</span>
<a href="/123">normal link</a>
<a href="javascript:void(0)">void</a>
`;

const voidLinks = div.querySelectorAll('a[href="javascript:void(0)"]');
expect(voidLinks.length).toBe(1);
const normalLinks = div.querySelectorAll('a[href]:not([href="javascript:void(0)"])');
expect(normalLinks.length).toBe(1);
});
});

describe('querySelector()', () => {
Expand Down

0 comments on commit 0452d7c

Please sign in to comment.