1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-06 09:37:12 +02:00

Report specific filter before generic one

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/2092

Regression from:
- 72bb89495b
This commit is contained in:
Raymond Hill 2022-04-25 09:49:31 -04:00
parent 3af9cd2b71
commit 83d028ac7d
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 26 additions and 31 deletions

View File

@ -118,7 +118,7 @@ const onMessage = function(request, sender, callback) {
return; return;
case 'listsFromCosmeticFilter': case 'listsFromCosmeticFilter':
staticFilteringReverseLookup.fromCosmeticFilter( staticFilteringReverseLookup.fromExtendedFilter(
request request
).then(response => { ).then(response => {
callback(response); callback(response);

View File

@ -23,31 +23,28 @@
/******************************************************************************/ /******************************************************************************/
{ let listEntries = Object.create(null);
// >>>>> start of local scope
/******************************************************************************/ /******************************************************************************/
const reBlockStart = /^#block-start-([\w:]+)\n/gm; // https://github.com/uBlockOrigin/uBlock-issues/issues/2092
let listEntries = Object.create(null); // Order of ids matters
const extractBlocks = function(content, ...ids) { const extractBlocks = function(content, ...ids) {
reBlockStart.lastIndex = 0;
const out = []; const out = [];
let match = reBlockStart.exec(content); for ( const id of ids ) {
while ( match !== null ) { const pattern = `#block-start-${id}\n`;
const beg = match.index + match[0].length; let beg = content.indexOf(pattern);
const id = match[1]; if ( beg === -1 ) { continue; }
if ( ids.includes(id) ) { beg += pattern.length;
const end = content.indexOf(`#block-end-${id}`, beg); const end = content.indexOf(`#block-end-${id}`, beg);
out.push(content.slice(beg, end)); out.push(content.slice(beg, end));
reBlockStart.lastIndex = end;
}
match = reBlockStart.exec(content);
} }
return out.join('\n'); return out.join('\n');
}; };
/******************************************************************************/
// https://github.com/MajkiIT/polish-ads-filter/issues/14768#issuecomment-536006312 // https://github.com/MajkiIT/polish-ads-filter/issues/14768#issuecomment-536006312
// Avoid reporting badfilter-ed filters. // Avoid reporting badfilter-ed filters.
@ -66,8 +63,7 @@ const fromNetFilter = function(details) {
// We need an exact match. // We need an exact match.
// https://github.com/gorhill/uBlock/issues/1392 // https://github.com/gorhill/uBlock/issues/1392
// https://github.com/gorhill/uBlock/issues/835 // https://github.com/gorhill/uBlock/issues/835
const notFound = pos !== 0 && const notFound = pos !== 0 && content.charCodeAt(pos - 1) !== 0x0A;
content.charCodeAt(pos - 1) !== 0x0A;
pos += compiledFilter.length; pos += compiledFilter.length;
if ( if (
notFound || notFound ||
@ -90,6 +86,8 @@ const fromNetFilter = function(details) {
self.postMessage({ id: details.id, response }); self.postMessage({ id: details.id, response });
}; };
/******************************************************************************/
// Looking up filter lists from a cosmetic filter is a bit more complicated // Looking up filter lists from a cosmetic filter is a bit more complicated
// than with network filters: // than with network filters:
// //
@ -110,7 +108,7 @@ const fromNetFilter = function(details) {
// FilterContainer.fromCompiledContent() is our reference code to create // FilterContainer.fromCompiledContent() is our reference code to create
// the various compiled versions. // the various compiled versions.
const fromCosmeticFilter = function(details) { const fromExtendedFilter = function(details) {
const match = /^#@?#\^?/.exec(details.rawFilter); const match = /^#@?#\^?/.exec(details.rawFilter);
const prefix = match[0]; const prefix = match[0];
const exception = prefix.charAt(1) === '@'; const exception = prefix.charAt(1) === '@';
@ -161,8 +159,8 @@ const fromCosmeticFilter = function(details) {
if ( entry === undefined ) { continue; } if ( entry === undefined ) { continue; }
const content = extractBlocks( const content = extractBlocks(
entry.content, entry.content,
'COSMETIC_FILTERS:GENERIC',
'COSMETIC_FILTERS:SPECIFIC', 'COSMETIC_FILTERS:SPECIFIC',
'COSMETIC_FILTERS:GENERIC',
'SCRIPTLET_FILTERS', 'SCRIPTLET_FILTERS',
'HTML_FILTERS', 'HTML_FILTERS',
'HTTPHEADER_FILTERS' 'HTTPHEADER_FILTERS'
@ -270,7 +268,9 @@ const fromCosmeticFilter = function(details) {
self.postMessage({ id: details.id, response }); self.postMessage({ id: details.id, response });
}; };
self.onmessage = function(e) { // jshint ignore:line /******************************************************************************/
self.onmessage = function(e) {
const msg = e.data; const msg = e.data;
switch ( msg.what ) { switch ( msg.what ) {
@ -286,15 +286,10 @@ self.onmessage = function(e) { // jshint ignore:line
fromNetFilter(msg); fromNetFilter(msg);
break; break;
case 'fromCosmeticFilter': case 'fromExtendedFilter':
fromCosmeticFilter(msg); fromExtendedFilter(msg);
break; break;
} }
}; };
/******************************************************************************/ /******************************************************************************/
// <<<<< end of local scope
}
/******************************************************************************/

View File

@ -155,7 +155,7 @@ const fromNetFilter = async function(rawFilter) {
}); });
}; };
const fromCosmeticFilter = async function(details) { const fromExtendedFilter = async function(details) {
if ( if (
typeof details.rawFilter !== 'string' || typeof details.rawFilter !== 'string' ||
details.rawFilter === '' details.rawFilter === ''
@ -169,7 +169,7 @@ const fromCosmeticFilter = async function(details) {
const hostname = hostnameFromURI(details.url); const hostname = hostnameFromURI(details.url);
worker.postMessage({ worker.postMessage({
what: 'fromCosmeticFilter', what: 'fromExtendedFilter',
id: id, id: id,
domain: domainFromHostname(hostname), domain: domainFromHostname(hostname),
hostname: hostname, hostname: hostname,
@ -203,7 +203,7 @@ const resetLists = function() {
const staticFilteringReverseLookup = { const staticFilteringReverseLookup = {
fromNetFilter, fromNetFilter,
fromCosmeticFilter, fromExtendedFilter,
resetLists, resetLists,
shutdown: stopWorker shutdown: stopWorker
}; };