1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-16 15:33:38 +01:00

Compare commits

..

2 Commits

2 changed files with 43 additions and 16 deletions

View File

@ -276,7 +276,7 @@ function registerProcedural(context) {
allFrames: true, allFrames: true,
matches, matches,
excludeMatches, excludeMatches,
runAt: 'document_end', runAt: 'document_start',
}; };
// register // register

View File

@ -21,8 +21,6 @@
/* jshint esversion:11 */ /* jshint esversion:11 */
'use strict';
/******************************************************************************/ /******************************************************************************/
// Important! // Important!
@ -112,18 +110,21 @@ const uBOL_injectCSS = (css, count = 10) => {
}; };
const nonVisualElements = { const nonVisualElements = {
head: true,
link: true,
meta: true,
script: true, script: true,
style: true, style: true,
}; };
const regexFromString = (s, exact = false) => { const regexFromString = (s, exact = false) => {
if ( s === '' ) { return /^/; } if ( s === '' ) { return /^/; }
const match = /^\/(.+)\/([i]?)$/.exec(s); const match = /^\/(.+)\/([imu]*)$/.exec(s);
if ( match !== null ) { if ( match !== null ) {
return new RegExp(match[1], match[2] || undefined); return new RegExp(match[1], match[2] || undefined);
} }
const reStr = s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const reStr = s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
return new RegExp(exact ? `^${reStr}$` : reStr, 'i'); return new RegExp(exact ? `^${reStr}$` : reStr);
}; };
/******************************************************************************/ /******************************************************************************/
@ -269,6 +270,32 @@ 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 ) {
if ( value === undefined ) { return; }
} else if ( this.reValue.test(value) === false ) {
return;
}
output.push(node);
}
}
/******************************************************************************/
class PSelectorMinTextLengthTask extends PSelectorTask { class PSelectorMinTextLengthTask extends PSelectorTask {
constructor(task) { constructor(task) {
super(); super();
@ -295,29 +322,28 @@ class PSelectorOthersTask extends PSelectorTask {
const toKeep = new Set(this.targets); const toKeep = new Set(this.targets);
const toDiscard = new Set(); const toDiscard = new Set();
const body = document.body; const body = document.body;
const head = document.head;
let discard = null; let discard = null;
for ( let keep of this.targets ) { for ( let keep of this.targets ) {
while ( keep !== null && keep !== body ) { while ( keep !== null && keep !== body && keep !== head ) {
toKeep.add(keep); toKeep.add(keep);
toDiscard.delete(keep); toDiscard.delete(keep);
discard = keep.previousElementSibling; discard = keep.previousElementSibling;
while ( discard !== null ) { while ( discard !== null ) {
if ( if ( nonVisualElements[discard.localName] !== true ) {
nonVisualElements[discard.localName] !== true && if ( toKeep.has(discard) === false ) {
toKeep.has(discard) === false
) {
toDiscard.add(discard); toDiscard.add(discard);
} }
}
discard = discard.previousElementSibling; discard = discard.previousElementSibling;
} }
discard = keep.nextElementSibling; discard = keep.nextElementSibling;
while ( discard !== null ) { while ( discard !== null ) {
if ( if ( nonVisualElements[discard.localName] !== true ) {
nonVisualElements[discard.localName] !== true && if ( toKeep.has(discard) === false ) {
toKeep.has(discard) === false
) {
toDiscard.add(discard); toDiscard.add(discard);
} }
}
discard = discard.nextElementSibling; discard = discard.nextElementSibling;
} }
keep = keep.parentElement; keep = keep.parentElement;
@ -570,6 +596,7 @@ PSelector.prototype.operatorToTaskMap = new Map([
[ 'matches-css-before', PSelectorMatchesCSSBeforeTask ], [ 'matches-css-before', PSelectorMatchesCSSBeforeTask ],
[ 'matches-media', PSelectorMatchesMediaTask ], [ 'matches-media', PSelectorMatchesMediaTask ],
[ 'matches-path', PSelectorMatchesPathTask ], [ 'matches-path', PSelectorMatchesPathTask ],
[ 'matches-prop', PSelectorMatchesPropTask ],
[ 'min-text-length', PSelectorMinTextLengthTask ], [ 'min-text-length', PSelectorMinTextLengthTask ],
[ 'not', PSelectorIfNotTask ], [ 'not', PSelectorIfNotTask ],
[ 'others', PSelectorOthersTask ], [ 'others', PSelectorOthersTask ],