Skip to content

Commit aca7674

Browse files
committed
Add :matches-prop() pseudo CSS operator
`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)
1 parent 7be7e0b commit aca7674

2 files changed

Lines changed: 276 additions & 254 deletions

File tree

src/js/contentscript-extra.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ class PSelectorMatchesPathTask extends PSelectorTask {
173173
}
174174
}
175175

176+
class PSelectorMatchesPropTask extends PSelectorTask {
177+
constructor(task) {
178+
super();
179+
this.props = task[1].attr.split('.');
180+
this.reValue = task[1].value !== ''
181+
? regexFromString(task[1].value, true)
182+
: null;
183+
}
184+
transpose(node, output) {
185+
let value = node;
186+
for ( const prop of this.props ) {
187+
if ( value === undefined ) { return; }
188+
if ( value === null ) { return; }
189+
value = value[prop];
190+
}
191+
if ( this.reValue !== null && this.reValue.test(value) === false ) { return; }
192+
output.push(node);
193+
}
194+
}
195+
176196
class PSelectorMinTextLengthTask extends PSelectorTask {
177197
constructor(task) {
178198
super();
@@ -461,6 +481,7 @@ PSelector.prototype.operatorToTaskMap = new Map([
461481
[ 'matches-css-before', PSelectorMatchesCSSBeforeTask ],
462482
[ 'matches-media', PSelectorMatchesMediaTask ],
463483
[ 'matches-path', PSelectorMatchesPathTask ],
484+
[ 'matches-prop', PSelectorMatchesPropTask ],
464485
[ 'min-text-length', PSelectorMinTextLengthTask ],
465486
[ 'not', PSelectorIfNotTask ],
466487
[ 'others', PSelectorOthersTask ],

0 commit comments

Comments
 (0)