Skip to content

Commit

Permalink
Merge pull request #879 from capricorn86/task/877-id-attributes-with-…
Browse files Browse the repository at this point in the history
…colons-break-dom-selectors

#877@patch: Adds support for using escaped characters to ID:s in quer…
  • Loading branch information
capricorn86 committed Apr 29, 2023
2 parents f94ed89 + de0db65 commit e3d2611
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/happy-dom/src/query-selector/SelectorParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import DOMException from '../exception/DOMException';
* Group 11: Combinator.
*/
const SELECTOR_REGEXP =
/(\*)|([a-zA-Z0-9-]+)|#([a-zA-Z0-9-_]+)|\.((?:[a-zA-Z0-9-_]|\\.)+)|\[([a-zA-Z0-9-_]+)\]|\[([a-zA-Z0-9-_]+)([~|^$*]{0,1}) *= *["']{0,1}([^"']+)["']{0,1}\]|:([a-zA-Z-:]+)|\(([^)]+)\)|([ ,+>]*)/g;
/(\*)|([a-zA-Z0-9-]+)|#((?:[a-zA-Z0-9-_]|\\.)+)|\.((?:[a-zA-Z0-9-_]|\\.)+)|\[([a-zA-Z0-9-_]+)\]|\[([a-zA-Z0-9-_]+)([~|^$*]{0,1}) *= *["']{0,1}([^"']+)["']{0,1}\]|:([a-zA-Z-:]+)|\(([^)]+)\)|([ ,+>]*)/g;

/**
* Escaped Character RegExp.
Expand Down Expand Up @@ -85,7 +85,7 @@ export default class SelectorParser {
} else if (match[2]) {
currentSelectorItem.tagName = match[2].toUpperCase();
} else if (match[3]) {
currentSelectorItem.id = match[3];
currentSelectorItem.id = match[3].replace(CLASS_ESCAPED_CHARACTER_REGEXP, '');
} else if (match[4]) {
currentSelectorItem.classNames = currentSelectorItem.classNames || [];
currentSelectorItem.classNames.push(match[4].replace(CLASS_ESCAPED_CHARACTER_REGEXP, ''));
Expand Down
12 changes: 11 additions & 1 deletion packages/happy-dom/test/query-selector/QuerySelector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,17 @@ describe('QuerySelector', () => {
div2.id = 'id';
div.appendChild(div2);

expect(div.querySelector('#id')).toEqual(div2);
expect(div.querySelector('#id') === div2).toBe(true);
});

it('Returns an element by id matching "#:id:".', () => {
const div = document.createElement('div');
const div2 = document.createElement('div');

div2.id = ':id:';
div.appendChild(div2);

expect(div.querySelector('#\\:id\\:') === div2).toBe(true);
});

it('Does not find input with selector of input:not([list])[type="search"]', () => {
Expand Down

0 comments on commit e3d2611

Please sign in to comment.