Skip to content

Commit

Permalink
Add new procedural operator: :min-text-length(x)
Browse files Browse the repository at this point in the history
Where `x` is the minimal text length of the subject
DOM element. DOM elements whose text length is
greater than or equal to `x` will be selected.
  • Loading branch information
gorhill authored and hawkeye116477 committed Jun 29, 2020
1 parent 4dccbfd commit 8123d49
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/js/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,21 @@ vAPI.DOMFilterer = (function() {
}
};

const PSelectorMinTextLengthTask = class {
constructor(task) {
this.min = task[1];
}
exec(input) {
const output = [];
for ( const node of input ) {
if ( node.textContent.length >= this.min ) {
output.push(node);
}
}
return output;
}
};

const PSelectorNthAncestorTask = class {
constructor(task) {
this.nth = task[1];
Expand Down Expand Up @@ -566,6 +581,7 @@ vAPI.DOMFilterer = (function() {
[ ':matches-css', PSelectorMatchesCSSTask ],
[ ':matches-css-after', PSelectorMatchesCSSAfterTask ],
[ ':matches-css-before', PSelectorMatchesCSSBeforeTask ],
[ ':min-text-length', PSelectorMinTextLengthTask ],
[ ':not', PSelectorIfNotTask ],
[ ':nth-ancestor', PSelectorNthAncestorTask ],
[ ':spath', PSelectorSpathTask ],
Expand Down
20 changes: 13 additions & 7 deletions src/js/static-ext-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
'matches-css',
'matches-css-after',
'matches-css-before',
'min-text-length',
'not',
'nth-ancestor',
'watch-attrs',
Expand Down Expand Up @@ -227,6 +228,14 @@
return compile(s);
};

const compileInteger = function(s, min = 0, max = 0x7FFFFFFF) {
if ( /^\d+$/.test(s) === false ) { return; }
const n = parseInt(s, 10);
if ( n >= min && n < max ) {
return n;
}
};

const compileNotSelector = function(s) {
// https://github.com/uBlockOrigin/uBlock-issues/issues/341#issuecomment-447603588
// Reject instances of :not() filters for which the argument is
Expand All @@ -238,10 +247,7 @@
};

const compileNthAncestorSelector = function(s) {
const n = parseInt(s, 10);
if ( isNaN(n) === false && n >= 1 && n < 256 ) {
return n;
}
return compileInteger(s, 1, 256);
};

const compileSpathExpression = function(s) {
Expand Down Expand Up @@ -285,6 +291,7 @@
[ ':matches-css', compileCSSDeclaration ],
[ ':matches-css-after', compileCSSDeclaration ],
[ ':matches-css-before', compileCSSDeclaration ],
[ ':min-text-length', compileInteger ],
[ ':not', compileNotSelector ],
[ ':nth-ancestor', compileNthAncestorSelector ],
[ ':spath', compileSpathExpression ],
Expand Down Expand Up @@ -340,12 +347,11 @@
case ':if-not':
raw.push(`:not(${decompile(task[1])})`);
break;
case ':nth-ancestor':
raw.push(`:nth-ancestor(${task[1]})`);
break;
case ':spath':
raw.push(task[1]);
break;
case ':min-text-length':
case ':nth-ancestor':
case ':watch-attrs':
case ':xpath':
raw.push(`${task[0]}(${task[1]})`);
Expand Down

0 comments on commit 8123d49

Please sign in to comment.