Skip to content

Commit

Permalink
Merge pull request #919 from capricorn86/914-selector-attribute-modif…
Browse files Browse the repository at this point in the history
…iers-doesnt-match-valid-elements

#914@patch: Fixes issue where attribute query selectors using "~" or …
  • Loading branch information
capricorn86 committed May 13, 2023
2 parents 12a9f81 + a19301e commit 58e8c72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/happy-dom/src/query-selector/SelectorParser.ts
Expand Up @@ -212,10 +212,13 @@ export default class SelectorParser {
switch (attribute.operator) {
// [attribute~="value"] - Contains a specified word.
case '~':
return new RegExp(`[- ]${attribute.value}|${attribute.value}[- ]`, modifier);
return new RegExp(
`[- ]${attribute.value}|${attribute.value}[- ]|^${attribute.value}$`,
modifier
);
// [attribute|="value"] - Starts with the specified word.
case '|':
return new RegExp(`^${attribute.value}[- ]`, modifier);
return new RegExp(`^${attribute.value}[- ]|^${attribute.value}$`, modifier);
// [attribute^="value"] - Begins with a specified value.
case '^':
return new RegExp(`^${attribute.value}`, modifier);
Expand Down
20 changes: 20 additions & 0 deletions packages/happy-dom/test/query-selector/QuerySelector.test.ts
Expand Up @@ -427,6 +427,16 @@ describe('QuerySelector', () => {
expect(elements[4] === container.children[0].children[1].children[2]).toBe(true);
});

it('Returns all elements with an attribute value containing a specified word using "[attr1~="value1"]" (which doesn\'t include spaces).', () => {
const container = document.createElement('div');
container.innerHTML = QuerySelectorHTML;
const elements = container.querySelectorAll('[attr1~="value1"]');

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 an attribute value starting with the specified word using "[class|="class1"]".', () => {
const container = document.createElement('div');
container.innerHTML = QuerySelectorHTML;
Expand All @@ -440,6 +450,16 @@ describe('QuerySelector', () => {
expect(elements[4] === container.children[0].children[1].children[2]).toBe(true);
});

it('Returns all elements with an attribute value containing a specified word using "[attr1|="value1"]" (which doesn\'t include spaces).', () => {
const container = document.createElement('div');
container.innerHTML = QuerySelectorHTML;
const elements = container.querySelectorAll('[attr1|="value1"]');

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 an attribute value that begins with a specified value using "[class^="cl"]".', () => {
const container = document.createElement('div');
container.innerHTML = QuerySelectorHTML;
Expand Down

0 comments on commit 58e8c72

Please sign in to comment.