8000 New extended selectors: matches-attr, matches-path, upward by chrmod · Pull Request #4931 · ghostery/adblocker · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

New extended selectors: matches-attr, matches-path, upward #4931

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

Merged
merged 27 commits into from
Jun 23, 2025

Conversation

chrmod
Copy link
Member
@chrmod chrmod commented Jun 9, 2025

No description provided.

@chrmod chrmod changed the title WIP: additional extended selectors New extended selectors: matches-attr, matches-path, upward Jun 9, 2025
@chrmod chrmod marked this pull request as ready for review June 9, 2025 09:27
@chrmod chrmod requested a review from remusao as a code owner June 9, 2025 09:27
@chrmod chrmod added the PR: New Feature 🚀 Increment minor version when merged label Jun 9, 2025
// Convert the pattern to a RegExp
// Escape special characters except for regex patterns
const escapedPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const regex = new RegExp(escapedPattern);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same comment about caching compiled regular expressions)

@chrmod chrmod force-pushed the extended-selectors branch from d2892e2 to 975714b Compare June 11, 2025 11:00
: findAncestorBySelector(c, argument);

if (ancestor === null) {
return [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep processing other candidates instead of doing an early return here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was a great catch! test to cover that case added


const path = globalThis.window.location.pathname;

const pattern = argument.replace(/^\/|\/$/g, '');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we handle cases where patterns might end with /i for case-insensitive matching for example?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was replaced with a simpler approach to detect a pair of wrapping slashes

return false;
}

const escapedPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have a utility for this already in network filters parsing code, maybe it's worth sharing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by looking at existing filters:

href=/[a-zA-Z0-9]{100,}/
/^on/=/event/
/-h?ref/
href=/https:\/\/thehackernews\.uk\/[a-zA-Z0-9]{4,}/
href=/^\/[-a-z]+\?[a-z]{2,}=/
href=/(^|audiobookbay\.lu)\/[-a-z0-9]+$/
class=/^[a-zA-Z]{2}$/
href="/__cft__\[0\]=[-\w]{265,}/"
class=/^[a-zA-Z]{2}$/
alt="/.*\shttps:\/\/t\.co\/[\w]{10}$/"
id=/[a-zA-Z]{40,}/
href="/__cft__\[0\]=[-\w]{290,}/"
href=/[a-zA-Z0-9]{100,}/

all of them should be handled as regexp without a need for escaping. New implementation does not have the escaping.

for (const attr of element.attributes) {
if (regex.test(attr.name)) {
attrName = attr.name;
break;
Copy link
Member
@philipp-classen philipp-classen Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, there is a match. But if there is no match, then arguably returning false would be clearer. As I understand, now it will lookup the regular expression (e.g. /foo/) as the attribute. I think, it is guaranteed to find nothing (getAttribute will return null), since it is no valid attribute key, so it is technically correct.

But the straightforward way would be to look for a matching attribute. And if there is none, immediately exit.

< 8000 div class="">
chrmod and others added 3 commits June 12, 2025 13:00
Co-authored-by: Philipp Claßen <philipp.classen@posteo.de>
Co-authored-by: Philipp Claßen <philipp.classen@posteo.de>
@@ -35,7 +35,7 @@ export type PseudoClass = Base & {
type: 'pseudo-class';
name: string;
argument: string | undefined;
subtree: AST | undefined;
subtree?: AST | undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
subtree?: AST | undefined;
subtree?: AST;

Also, argument?: string; above (for consistency)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted to old code - this one should never have been committed

return false;
}

const path = globalThis.window.location.pathname;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. Wondering if we're fine accessing globals from this function or if we want to provide everything through arguments. Not sure if relied on globals previously.

return false;
}

const path = globalThis.window.location.pathname;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation from uBO specifies that this should match on path + query but here we only consider path. See: https://github.com/gorhill/uBlock/wiki/Procedural-cosmetic-filters#subjectmatches-patharg

const path = globalThis.window.location.pathname;

let pattern = argument;
if (pattern.startsWith('/') && pattern.endsWith('/')) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mentioned it wasn't used in any filters out there but in case there is any modifier like i (e.g. /foo/i) do we want to handle or maybe detect such cases in filters builder (or in unit tests)? Otherwise they would silently fail.

}

const indexOfEqual = argument.indexOf('=');
let namePattern, valuePattern;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to handle cases where the name is quoted as well here.

A declaration in the form name="value" or "name"="value",

From: https://github.com/gorhill/uBlock/wiki/Procedural-cosmetic-filters#subjectmatches-attrarg

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the parsing algorithm of uBO, it looks like quotes wrapping both name and value are optional. It basically shares algorithm with other scriptlet functions, so we need to support the following forms: "literal", 'literal', /regex/, "/regexLiteral/", '/regexLiteral/'.

valuePattern = argument.slice(indexOfEqual + 1);
}

namePattern = stripsWrappingQuotes(namePattern);
Copy link
Member
@philipp-classen philipp-classen Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we handle quoted keys, do we have to handle arbitrary strings like that?

"x=y"=z

(If so, we cannot split by "=" in the first step, but have to strip the quotes in the same step)

}

const distance = parseInt(argument, 10);
const ancestor = !Number.isNaN(distance)
Copy link
Member
@philipp-classen philipp-classen Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best to guard the parsing with Number.isInteger(argument) before.

Currently, "x3" will run in the NaN path, while "3x" will succeed (parsed as "3").

@philipp-classen philipp-classen self-requested a review June 16, 2025 17:38
Comment on lines 243 to 246
if (ancestor === null) {
continue;
}
ancestors.add(ancestor);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (ancestor === null) {
continue;
}
ancestors.add(ancestor);
if (ancestor !== null) {
ancestors.add(ancestor);
}

chrmod and others added 2 commits June 16, 2025 21:02
Co-authored-by: Philipp Claßen <philipp.classen@posteo.de>
Co-authored-by: Philipp Claßen <philipp.classen@posteo.de>

if (after.length > 0) {
if (after[0].type === 'pseudo-class' && after[0].name === 'upward') {
return Array.from(ancestors).flatMap((a) =>
Copy link
Member
@philipp-classen philipp-classen Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably, we should also filter out duplicates here.

Comment on lines 168 to 170
F438
} else if (selector.name === 'upward') {
// :upward is handled in querySelectorAll
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this section, we can just fall to line 77

Comment on lines 193 to 196
let ancestor: Element | null = element.parentElement;
while (ancestor !== null) {
if (ancestor.matches(selector)) {
return ancestor;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if transforming this into selector: e.g. documentElement.querySelectorAll would make sense. This way, we don't need to loop.

@chrmod chrmod requested a review from seia-soto June 23, 2025 09:46
@chrmod chrmod merged commit abd2894 into master Jun 23, 2025
4 checks passed
@chrmod chrmod deleted the extended-selectors branch June 23, 2025 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: New Feature 🚀 Increment minor version when merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0