1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-19 19:52:51 +02:00

Handle potentially invalid CSS selector

Older browser versions may not support valid CSS selector syntax
in newer browser versions.

Related feedback:
- https://www.reddit.com/r/uBlockOrigin/comments/yzw5pt/
This commit is contained in:
Raymond Hill 2022-11-21 08:57:55 -05:00
parent eb709335f8
commit bdc7a4c539
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -51,18 +51,37 @@ const loggedSelectors = new Set();
const rePseudoElements = /:(?::?after|:?before|:[a-z-]+)$/;
const hasSelector = function(selector, context = document) {
try {
return context.querySelector(selector) !== null;
}
catch(ex) {
}
return false;
};
const safeMatchSelector = function(selector, context) {
const safeSelector = rePseudoElements.test(selector)
? selector.replace(rePseudoElements, '')
: selector;
return context.matches(safeSelector);
try {
return context.matches(safeSelector);
}
catch(ex) {
}
return false;
};
const safeQuerySelector = function(selector, context = document) {
const safeSelector = rePseudoElements.test(selector)
? selector.replace(rePseudoElements, '')
: selector;
return context.querySelector(safeSelector);
try {
return context.querySelector(safeSelector);
}
catch(ex) {
}
return null;
};
const safeGroupSelectors = function(selectors) {
@ -85,7 +104,7 @@ const processDeclarativeSimple = function(node, out) {
}
if (
(node === document || node.matches(simpleDeclarativeStr) === false) &&
(node.querySelector(simpleDeclarativeStr) === null)
(hasSelector(simpleDeclarativeStr, node) === false)
) {
return;
}
@ -110,7 +129,7 @@ const processDeclarativeComplex = function(out) {
if ( complexDeclarativeStr === undefined ) {
complexDeclarativeStr = safeGroupSelectors(complexDeclarativeSet);
}
if ( document.querySelector(complexDeclarativeStr) === null ) { return; }
if ( hasSelector(complexDeclarativeStr) === false ) { return; }
for ( const selector of complexDeclarativeSet ) {
if ( safeQuerySelector(selector) === null ) { continue; }
out.push(`##${selector}`);
@ -140,7 +159,7 @@ const processExceptions = function(out) {
if ( exceptionStr === undefined ) {
exceptionStr = safeGroupSelectors(exceptionDict.keys());
}
if ( document.querySelector(exceptionStr) === null ) { return; }
if ( hasSelector(exceptionStr) === false ) { return; }
for ( const [ selector, raw ] of exceptionDict ) {
if ( safeQuerySelector(selector) === null ) { continue; }
out.push(`#@#${raw}`);