Code review of reworked generic cosmetic filtering code

Related commit:
- 26594fb902
This commit is contained in:
Raymond Hill 2022-12-07 13:48:44 -05:00
parent d7c18fd7db
commit 1bda7e0085
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 17 additions and 6 deletions

View File

@ -1041,7 +1041,7 @@ vAPI.DOMFilterer = class {
}
};
const getSurveyResults = hashes => {
const getSurveyResults = (hashes, safeOnly) => {
if ( self.vAPI.messaging instanceof Object === false ) {
stop(); return;
}
@ -1052,6 +1052,7 @@ vAPI.DOMFilterer = class {
hostname,
hashes,
exceptions: domFilterer.exceptions,
safeOnly,
});
promise.then(response => {
processSurveyResults(response);
@ -1152,6 +1153,7 @@ vAPI.DOMFilterer = class {
idFromNode(document.body, hashes);
classesFromNode(document.body, hashes);
}
getSurveyResults(hashes, true);
addPendingList(document.querySelectorAll(
'[id]:not(html):not(body),[class]:not(html):not(body)'
));

View File

@ -170,13 +170,15 @@ const hashFromStr = (type, s) => {
// It's an uncommon case, so it's best to unescape only when needed.
const keyFromSelector = selector => {
let key = '';
let matches = rePlainSelector.exec(selector);
if ( matches === null ) {
if ( matches !== null ) {
key = matches[0];
} else {
matches = rePlainSelectorEx.exec(selector);
if ( matches !== null ) { return matches[1] || matches[2]; }
return;
if ( matches === null ) { return; }
key = matches[1] || matches[2];
}
let key = matches[0];
if ( key.includes('\\') === false ) { return key; }
matches = rePlainSelectorEscaped.exec(selector);
if ( matches === null ) { return; }
@ -744,10 +746,17 @@ FilterContainer.prototype.retrieveGenericSelectors = function(request) {
hashes.push(hash);
}
if ( request.safeOnly ) {
for ( const selector of selectorsSet ) {
if ( selector !== keyFromSelector(selector) ) { continue; }
selectorsSet.delete(selector);
}
}
// Apply exceptions: it is the responsibility of the caller to provide
// the exceptions to be applied.
const excepted = [];
if ( Array.isArray(request.exceptions) ) {
if ( selectorsSet.size !== 0 && Array.isArray(request.exceptions) ) {
for ( const exception of request.exceptions ) {
if ( selectorsSet.delete(exception) ) {
excepted.push(exception);