1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 03:12:33 +01:00
The previous fix led to a regression. Related ffedback:
- 8d136ec2d5 (commitcomment-34460854)
This commit is contained in:
Raymond Hill 2019-07-30 12:27:09 -04:00
parent 3d66bdc8e9
commit 104b222275
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -36,8 +36,6 @@ if (
} }
const reHasCSSCombinators = /[ >+~]/; const reHasCSSCombinators = /[ >+~]/;
const reHasPseudoClass = /:+(?:after|before)$/;
const sanitizedSelectors = new Map();
const simpleDeclarativeSet = new Set(); const simpleDeclarativeSet = new Set();
let simpleDeclarativeStr; let simpleDeclarativeStr;
const complexDeclarativeSet = new Set(); const complexDeclarativeSet = new Set();
@ -52,10 +50,39 @@ const loggedSelectors = new Set();
/******************************************************************************/ /******************************************************************************/
const rePseudoElements = /::?(?:after|before)$/;
const safeMatchSelector = function(selector, context) {
const safeSelector = rePseudoElements.test(selector)
? selector.replace(rePseudoElements, '')
: selector;
return context.matches(safeSelector);
};
const safeQuerySelector = function(selector, context = document) {
const safeSelector = rePseudoElements.test(selector)
? selector.replace(rePseudoElements, '')
: selector;
return context.querySelector(safeSelector);
};
const safeGroupSelectors = function(selectors) {
const arr = Array.isArray(selectors)
? selectors
: Array.from(selectors);
return arr.map(s => {
return rePseudoElements.test(s)
? s.replace(rePseudoElements, '')
: s;
}).join(',\n');
};
/******************************************************************************/
const processDeclarativeSimple = function(node, out) { const processDeclarativeSimple = function(node, out) {
if ( simpleDeclarativeSet.size === 0 ) { return; } if ( simpleDeclarativeSet.size === 0 ) { return; }
if ( simpleDeclarativeStr === undefined ) { if ( simpleDeclarativeStr === undefined ) {
simpleDeclarativeStr = Array.from(simpleDeclarativeSet).join(',\n'); simpleDeclarativeStr = safeGroupSelectors(simpleDeclarativeSet);
} }
if ( if (
(node === document || node.matches(simpleDeclarativeStr) === false) && (node === document || node.matches(simpleDeclarativeStr) === false) &&
@ -65,16 +92,15 @@ const processDeclarativeSimple = function(node, out) {
} }
for ( const selector of simpleDeclarativeSet ) { for ( const selector of simpleDeclarativeSet ) {
if ( if (
(node === document || node.matches(selector) === false) && (node === document || safeMatchSelector(selector, node) === false) &&
(node.querySelector(selector) === null) (safeQuerySelector(selector, node) === null)
) { ) {
continue; continue;
} }
out.push(`##${sanitizedSelectors.get(selector) || selector}`); out.push(`##${selector}`);
simpleDeclarativeSet.delete(selector); simpleDeclarativeSet.delete(selector);
simpleDeclarativeStr = undefined; simpleDeclarativeStr = undefined;
loggedSelectors.add(selector); loggedSelectors.add(selector);
if ( simpleDeclarativeSet.size === 0 ) { return; }
} }
}; };
@ -83,16 +109,15 @@ const processDeclarativeSimple = function(node, out) {
const processDeclarativeComplex = function(out) { const processDeclarativeComplex = function(out) {
if ( complexDeclarativeSet.size === 0 ) { return; } if ( complexDeclarativeSet.size === 0 ) { return; }
if ( complexDeclarativeStr === undefined ) { if ( complexDeclarativeStr === undefined ) {
complexDeclarativeStr = Array.from(complexDeclarativeSet).join(',\n'); complexDeclarativeStr = safeGroupSelectors(complexDeclarativeSet);
} }
if ( document.querySelector(complexDeclarativeStr) === null ) { return; } if ( document.querySelector(complexDeclarativeStr) === null ) { return; }
for ( const selector of complexDeclarativeSet ) { for ( const selector of complexDeclarativeSet ) {
if ( document.querySelector(selector) === null ) { continue; } if ( safeQuerySelector(selector) === null ) { continue; }
out.push(`##${sanitizedSelectors.get(selector) || selector}`); out.push(`##${selector}`);
complexDeclarativeSet.delete(selector); complexDeclarativeSet.delete(selector);
complexDeclarativeStr = undefined; complexDeclarativeStr = undefined;
loggedSelectors.add(selector); loggedSelectors.add(selector);
if ( complexDeclarativeSet.size === 0 ) { return; }
} }
}; };
@ -101,18 +126,16 @@ const processDeclarativeComplex = function(out) {
const processDeclarativeStyle = function(out) { const processDeclarativeStyle = function(out) {
if ( declarativeStyleDict.size === 0 ) { return; } if ( declarativeStyleDict.size === 0 ) { return; }
if ( declarativeStyleStr === undefined ) { if ( declarativeStyleStr === undefined ) {
declarativeStyleStr = Array.from(declarativeStyleDict.keys()) declarativeStyleStr = safeGroupSelectors(declarativeStyleDict.keys());
.join(',\n');
} }
if ( document.querySelector(declarativeStyleStr) === null ) { return; } if ( document.querySelector(declarativeStyleStr) === null ) { return; }
for ( const [ selector, style ] of declarativeStyleDict ) { for ( const [ selector, style ] of declarativeStyleDict ) {
if ( document.querySelector(selector) === null ) { continue; } if ( safeQuerySelector(selector) === null ) { continue; }
const raw = `##${selector}:style(${style})`; const raw = `##${selector}:style(${style})`;
out.push(raw); out.push(raw);
declarativeStyleDict.delete(selector); declarativeStyleDict.delete(selector);
declarativeStyleStr = undefined; declarativeStyleStr = undefined;
loggedSelectors.add(raw); loggedSelectors.add(raw);
if ( declarativeStyleDict.size === 0 ) { return; }
} }
}; };
@ -124,7 +147,6 @@ const processProcedural = function(out) {
if ( entry[1].test() === false ) { continue; } if ( entry[1].test() === false ) { continue; }
out.push(`##${entry[1].raw}`); out.push(`##${entry[1].raw}`);
proceduralDict.delete(entry[0]); proceduralDict.delete(entry[0]);
if ( proceduralDict.size === 0 ) { break; }
} }
}; };
@ -133,11 +155,11 @@ const processProcedural = function(out) {
const processExceptions = function(out) { const processExceptions = function(out) {
if ( exceptionDict.size === 0 ) { return; } if ( exceptionDict.size === 0 ) { return; }
if ( exceptionStr === undefined ) { if ( exceptionStr === undefined ) {
exceptionStr = Array.from(exceptionDict.keys()).join(',\n'); exceptionStr = safeGroupSelectors(exceptionDict.keys());
} }
if ( document.querySelector(exceptionStr) === null ) { return; } if ( document.querySelector(exceptionStr) === null ) { return; }
for ( const [ selector, raw ] of exceptionDict ) { for ( const [ selector, raw ] of exceptionDict ) {
if ( document.querySelector(selector) === null ) { continue; } if ( safeQuerySelector(selector) === null ) { continue; }
out.push(`#@#${raw}`); out.push(`#@#${raw}`);
exceptionDict.delete(selector); exceptionDict.delete(selector);
exceptionStr = undefined; exceptionStr = undefined;
@ -211,11 +233,6 @@ const handlers = {
declarativeStyleStr = undefined; declarativeStyleStr = undefined;
continue; continue;
} }
if ( reHasPseudoClass.test(selector) ) {
const sanitized = selector.replace(reHasPseudoClass, '');
sanitizedSelectors.set(sanitized, selector);
selector = sanitized;
}
if ( loggedSelectors.has(selector) ) { continue; } if ( loggedSelectors.has(selector) ) { continue; }
if ( reHasCSSCombinators.test(selector) ) { if ( reHasCSSCombinators.test(selector) ) {
complexDeclarativeSet.add(selector); complexDeclarativeSet.add(selector);