Skip to content

Commit

Permalink
Merge pull request #882 from capricorn86/task/881-domexception-is-thr…
Browse files Browse the repository at this point in the history
…own-for-selectors-with-an-explicit-empty-attribute-value

#881@patch: Adds support for query selectors for attributes with empt…
  • Loading branch information
capricorn86 committed May 2, 2023
2 parents e3d2611 + 6ac8168 commit dcd7e06
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
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

0 comments on commit dcd7e06

Please sign in to comment.