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.

The original rationale for such procedural cosmetic
operator[1] is to be able to remove inline script
elements according to a minimum text length using
HTML filtering.

[1] As a result of internal discussion with filter
    list maintainers @ uAssets.
  • Loading branch information
gorhill committed Jun 20, 2019
1 parent 793aca7 commit b428a25
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
16 changes: 16 additions & 0 deletions src/js/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,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 @@ -658,6 +673,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
18 changes: 17 additions & 1 deletion src/js/html-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,26 @@

const PSelectorIfNotTask = class extends PSelectorIfTask {
constructor(task) {
super.call(task);
super(task);
this.target = false;
}
};

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 @@ -191,6 +206,7 @@
[ ':has-text', PSelectorHasTextTask ],
[ ':if', PSelectorIfTask ],
[ ':if-not', PSelectorIfNotTask ],
[ ':min-text-length', PSelectorMinTextLengthTask ],
[ ':not', PSelectorIfNotTask ],
[ ':nth-ancestor', PSelectorNthAncestorTask ],
[ ':xpath', PSelectorXpathTask ]
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 @@ -231,6 +232,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 @@ -242,10 +251,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 @@ -289,6 +295,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 @@ -344,12 +351,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 b428a25

Please sign in to comment.