Skip to content

Commit

Permalink
Report specific filter before generic one
Browse files Browse the repository at this point in the history
Related issue:
- uBlockOrigin/uBlock-issues#2092

Regression from:
- gorhill@72bb894
  • Loading branch information
gorhill committed Apr 25, 2022
1 parent 3af9cd2 commit 83d028a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/js/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const onMessage = function(request, sender, callback) {
return;

case 'listsFromCosmeticFilter':
staticFilteringReverseLookup.fromCosmeticFilter(
staticFilteringReverseLookup.fromExtendedFilter(
request
).then(response => {
callback(response);
Expand Down
49 changes: 22 additions & 27 deletions src/js/reverselookup-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,28 @@

/******************************************************************************/

{
// >>>>> start of local scope
let listEntries = Object.create(null);

/******************************************************************************/

const reBlockStart = /^#block-start-([\w:]+)\n/gm;
let listEntries = Object.create(null);
// https://github.com/uBlockOrigin/uBlock-issues/issues/2092
// Order of ids matters

const extractBlocks = function(content, ...ids) {
reBlockStart.lastIndex = 0;
const out = [];
let match = reBlockStart.exec(content);
while ( match !== null ) {
const beg = match.index + match[0].length;
const id = match[1];
if ( ids.includes(id) ) {
const end = content.indexOf(`#block-end-${id}`, beg);
out.push(content.slice(beg, end));
reBlockStart.lastIndex = end;
}
match = reBlockStart.exec(content);
for ( const id of ids ) {
const pattern = `#block-start-${id}\n`;
let beg = content.indexOf(pattern);
if ( beg === -1 ) { continue; }
beg += pattern.length;
const end = content.indexOf(`#block-end-${id}`, beg);
out.push(content.slice(beg, end));
}
return out.join('\n');
};

/******************************************************************************/

// https://github.com/MajkiIT/polish-ads-filter/issues/14768#issuecomment-536006312
// Avoid reporting badfilter-ed filters.

Expand All @@ -66,8 +63,7 @@ const fromNetFilter = function(details) {
// We need an exact match.
// https://github.com/gorhill/uBlock/issues/1392
// https://github.com/gorhill/uBlock/issues/835
const notFound = pos !== 0 &&
content.charCodeAt(pos - 1) !== 0x0A;
const notFound = pos !== 0 && content.charCodeAt(pos - 1) !== 0x0A;
pos += compiledFilter.length;
if (
notFound ||
Expand All @@ -90,6 +86,8 @@ const fromNetFilter = function(details) {
self.postMessage({ id: details.id, response });
};

/******************************************************************************/

// Looking up filter lists from a cosmetic filter is a bit more complicated
// than with network filters:
//
Expand All @@ -110,7 +108,7 @@ const fromNetFilter = function(details) {
// FilterContainer.fromCompiledContent() is our reference code to create
// the various compiled versions.

const fromCosmeticFilter = function(details) {
const fromExtendedFilter = function(details) {
const match = /^#@?#\^?/.exec(details.rawFilter);
const prefix = match[0];
const exception = prefix.charAt(1) === '@';
Expand Down Expand Up @@ -161,8 +159,8 @@ const fromCosmeticFilter = function(details) {
if ( entry === undefined ) { continue; }
const content = extractBlocks(
entry.content,
'COSMETIC_FILTERS:GENERIC',
'COSMETIC_FILTERS:SPECIFIC',
'COSMETIC_FILTERS:GENERIC',
'SCRIPTLET_FILTERS',
'HTML_FILTERS',
'HTTPHEADER_FILTERS'
Expand Down Expand Up @@ -270,7 +268,9 @@ const fromCosmeticFilter = function(details) {
self.postMessage({ id: details.id, response });
};

self.onmessage = function(e) { // jshint ignore:line
/******************************************************************************/

self.onmessage = function(e) {
const msg = e.data;

switch ( msg.what ) {
Expand All @@ -286,15 +286,10 @@ self.onmessage = function(e) { // jshint ignore:line
fromNetFilter(msg);
break;

case 'fromCosmeticFilter':
fromCosmeticFilter(msg);
case 'fromExtendedFilter':
fromExtendedFilter(msg);
break;
}
};

/******************************************************************************/

// <<<<< end of local scope
}

/******************************************************************************/
6 changes: 3 additions & 3 deletions src/js/reverselookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ const fromNetFilter = async function(rawFilter) {
});
};

const fromCosmeticFilter = async function(details) {
const fromExtendedFilter = async function(details) {
if (
typeof details.rawFilter !== 'string' ||
details.rawFilter === ''
Expand All @@ -169,7 +169,7 @@ const fromCosmeticFilter = async function(details) {
const hostname = hostnameFromURI(details.url);

worker.postMessage({
what: 'fromCosmeticFilter',
what: 'fromExtendedFilter',
id: id,
domain: domainFromHostname(hostname),
hostname: hostname,
Expand Down Expand Up @@ -203,7 +203,7 @@ const resetLists = function() {

const staticFilteringReverseLookup = {
fromNetFilter,
fromCosmeticFilter,
fromExtendedFilter,
resetLists,
shutdown: stopWorker
};
Expand Down

0 comments on commit 83d028a

Please sign in to comment.