Skip to content

Commit

Permalink
Add :matches-prop() pseudo CSS operator
Browse files Browse the repository at this point in the history
`subject:matches-prop(arg)`

Description: Allows to select an element by a property name (or chain of
properties), and optionally the property value.

Chainable: Yes.

`subject`: Can be a plain CSS selector, or a procedural cosmetic filter.

`arg`: A declaration in the form `chain=value`, where `chain` is a dot-
  separated string for the target property, and `value` is the optional
  property value to match. `value` can be literal text or literal regular
  expression. When no `value` is declared, the operator only tests for
  the presence of the target property

Example:

  example.org##div:matches-prop(imanad)
  example.org##img:matches-prop(naturalWidth=160)
  • Loading branch information
gorhill committed Jun 19, 2024
1 parent 7be7e0b commit aca7674
Show file tree
Hide file tree
Showing 2 changed files with 276 additions and 254 deletions.
21 changes: 21 additions & 0 deletions src/js/contentscript-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,26 @@ class PSelectorMatchesPathTask extends PSelectorTask {
}
}

class PSelectorMatchesPropTask extends PSelectorTask {
constructor(task) {
super();
this.props = task[1].attr.split('.');
this.reValue = task[1].value !== ''
? regexFromString(task[1].value, true)
: null;
}
transpose(node, output) {
let value = node;
for ( const prop of this.props ) {
if ( value === undefined ) { return; }
if ( value === null ) { return; }
value = value[prop];
}
if ( this.reValue !== null && this.reValue.test(value) === false ) { return; }
output.push(node);
}
}

class PSelectorMinTextLengthTask extends PSelectorTask {
constructor(task) {
super();
Expand Down Expand Up @@ -461,6 +481,7 @@ PSelector.prototype.operatorToTaskMap = new Map([
[ 'matches-css-before', PSelectorMatchesCSSBeforeTask ],
[ 'matches-media', PSelectorMatchesMediaTask ],
[ 'matches-path', PSelectorMatchesPathTask ],
[ 'matches-prop', PSelectorMatchesPropTask ],
[ 'min-text-length', PSelectorMinTextLengthTask ],
[ 'not', PSelectorIfNotTask ],
[ 'others', PSelectorOthersTask ],
Expand Down
Loading

0 comments on commit aca7674

Please sign in to comment.