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

feat: [#1642] Adds support for child combinator to :has pseudo selector #1660

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: 10 additions & 0 deletions packages/happy-dom/src/query-selector/SelectorItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,16 @@ export default class SelectorItem {
priorityWeightForHas = match.priorityWeight;
}
}
} else if (pseudo.arguments[0] === '>') {
for (const selectorItem of pseudo.selectorItems) {
for (const child of element[PropertySymbol.elementArray]) {
const match = selectorItem.match(child);
if (match && priorityWeightForHas < match.priorityWeight) {
priorityWeightForHas = match.priorityWeight;
break;
}
}
}
} else {
for (const selectorItem of pseudo.selectorItems) {
const match = this.matchChildOfElement(selectorItem, element);
Expand Down
15 changes: 11 additions & 4 deletions packages/happy-dom/src/query-selector/SelectorParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@
): ISelectorPseudo {
const lowerName = name.toLowerCase();

if (args) {
args = args.trim();
}

if (!args) {
return { name: lowerName, arguments: null, selectorItems: null, nthFunction: null };
}
Expand Down Expand Up @@ -328,10 +332,13 @@

// The ":has()" pseudo selector doesn't allow for it to be nested inside another ":has()" pseudo selector, as it can lead to cyclic querying.
if (!args.includes(':has(')) {
for (const group of this.getSelectorGroups(
args[0] === '+' ? args.replace('+', '') : args,
options
)) {
let newArgs = args;
if (args[0] === '+') {
newArgs = args.replace('+', '');
} else if (args[0] === '>') {
newArgs = args.replace('>', '');

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This replaces only the first occurrence of '>'.

Copilot Autofix AI 14 days ago

To fix the problem, we need to ensure that all occurrences of the character > in the args string are replaced, not just the first one. This can be achieved by using a regular expression with the global flag (g). Specifically, we will replace args.replace('>', '') with args.replace(/>/g, '').

Suggested changeset 1
packages/happy-dom/src/query-selector/SelectorParser.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/happy-dom/src/query-selector/SelectorParser.ts b/packages/happy-dom/src/query-selector/SelectorParser.ts
--- a/packages/happy-dom/src/query-selector/SelectorParser.ts
+++ b/packages/happy-dom/src/query-selector/SelectorParser.ts
@@ -338,3 +338,3 @@
 					} else if (args[0] === '>') {
-						newArgs = args.replace('>', '');
+						newArgs = args.replace(/>/g, '');
 					}
EOF
@@ -338,3 +338,3 @@
} else if (args[0] === '>') {
newArgs = args.replace('>', '');
newArgs = args.replace(/>/g, '');
}
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
}
for (const group of this.getSelectorGroups(newArgs, options)) {
hasSelectorItems.push(group[0]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1205,9 +1205,12 @@ describe('QuerySelector', () => {
expect(Array.from(container.querySelectorAll('span:has(+video)'))).toEqual([
container.children[1]
]);
expect(Array.from(container.querySelectorAll('h1:has(+h2)'))).toEqual([
expect(Array.from(container.querySelectorAll('h1:has( +h2)'))).toEqual([
container.children[3]
]);
expect(Array.from(container.querySelectorAll('span:has(> video)'))).toEqual([
container.children[0]
]);
});
});

Expand Down
Loading