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

#881@patch: Adds support for query selectors for attributes with empt… #882

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
2 changes: 1 addition & 1 deletion packages/happy-dom/src/query-selector/SelectorItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export default class SelectorItem {
priorityWeight += 10;

if (attribute.value !== null) {
if (!elementAttribute.value) {
if (elementAttribute.value === null) {
return null;
}

Expand Down
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 @@ -96,7 +96,7 @@ export default class SelectorParser {
operator: null,
value: null
});
} else if (match[6] && match[8]) {
} else if (match[6] && match[8] !== undefined) {
currentSelectorItem.attributes = currentSelectorItem.attributes || [];
currentSelectorItem.attributes.push({
name: match[6].toLowerCase(),
Expand Down
10 changes: 10 additions & 0 deletions packages/happy-dom/test/query-selector/QuerySelector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,16 @@ describe('QuerySelector', () => {
expect(elements[1] === container.children[0].children[1].children[1]).toBe(true);
});

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

expect(elements.length).toBe(2);
expect(elements[0] === container.children[0].children[1].children[0]).toBe(true);
expect(elements[1] === container.children[0].children[1].children[1]).toBe(true);
});

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