Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#632@patch: Adds support for using an escape character in attribute q… #997

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/happy-dom/src/query-selector/SelectorParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const SELECTOR_REGEXP =
/**
* Escaped Character RegExp.
*/
const CLASS_ESCAPED_CHARACTER_REGEXP = /\\/g;
const ESCAPED_CHARACTER_REGEXP = /\\/g;

/**
* Nth Function.
Expand Down Expand Up @@ -110,10 +110,10 @@ export default class SelectorParser {
} else if (match[2]) {
currentSelectorItem.tagName = match[2].toUpperCase();
} else if (match[3]) {
currentSelectorItem.id = match[3].replace(CLASS_ESCAPED_CHARACTER_REGEXP, '');
currentSelectorItem.id = match[3].replace(ESCAPED_CHARACTER_REGEXP, '');
} else if (match[4]) {
currentSelectorItem.classNames = currentSelectorItem.classNames || [];
currentSelectorItem.classNames.push(match[4].replace(CLASS_ESCAPED_CHARACTER_REGEXP, ''));
currentSelectorItem.classNames.push(match[4].replace(ESCAPED_CHARACTER_REGEXP, ''));
} else if (match[5]) {
currentSelectorItem.attributes = currentSelectorItem.attributes || [];
currentSelectorItem.attributes.push({
Expand All @@ -128,7 +128,7 @@ export default class SelectorParser {
currentSelectorItem.attributes.push({
name: match[6].toLowerCase(),
operator: match[7] || null,
value: match[8],
value: match[8].replace(ESCAPED_CHARACTER_REGEXP, ''),
modifier: match[9] || null,
regExp: this.getAttributeRegExp({
operator: match[7],
Expand All @@ -141,7 +141,7 @@ export default class SelectorParser {
currentSelectorItem.attributes.push({
name: match[10].toLowerCase(),
operator: match[11] || null,
value: match[12],
value: match[12].replace(ESCAPED_CHARACTER_REGEXP, ''),
modifier: null,
regExp: this.getAttributeRegExp({ operator: match[11], value: match[12] })
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1117,5 +1117,14 @@ describe('QuerySelector', () => {
expect(div.querySelector('::-webkit-inner-spin-button')).toBeNull();
expect(document.querySelector('::-webkit-inner-spin-button')).toBeNull();
});

it('Has support for attributes containing colon', () => {
const div = document.createElement('div');
div.innerHTML = '<meta ab="a:b"></meta>';
const element = div.querySelector('[ab="a\\:b"]');
const element2 = div.querySelector('[ab="a:b"]');
expect(element === div.children[0]).toBe(true);
expect(element2 === div.children[0]).toBe(true);
});
});
});