From 082201d24acc6850c9a4ef32c8b49fbda7698eb6 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 4 Dec 2019 10:10:07 -0500 Subject: [PATCH] Fix reverse lookup of entity-based cosmetic filters Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/805 --- src/js/reverselookup-worker.js | 46 +++++++++++++--------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/src/js/reverselookup-worker.js b/src/js/reverselookup-worker.js index eb12ccea9..37c81e252 100644 --- a/src/js/reverselookup-worker.js +++ b/src/js/reverselookup-worker.js @@ -121,43 +121,33 @@ const fromCosmeticFilter = function(details) { const exception = prefix.charAt(1) === '@'; const selector = details.rawFilter.slice(prefix.length); const isHtmlFilter = prefix.endsWith('^'); + const hostname = details.hostname; // The longer the needle, the lower the number of false positives. const needle = selector.match(/\w+/g).reduce(function(a, b) { return a.length > b.length ? a : b; }); - const reHostname = new RegExp( - '^' + - details.hostname.split('.').reduce( - function(acc, item) { - return acc === '' - ? item - : '(' + acc + '\\.)?' + item; - }, - '' - ) + - '$' - ); - - let reEntity, - domain = details.domain, - pos = domain.indexOf('.'); - if ( pos !== -1 ) { - reEntity = new RegExp( + const regexFromLabels = (hn, suffix) => + new RegExp( '^' + - domain.slice(0, pos).split('.').reduce( - function(acc, item) { - return acc === '' - ? item - : '(' + acc + '\\.)?' + item; - }, - '' - ) + - '\\.\\*$' + hn.split('.').reduce((acc, item) => `(${acc}\\.)?${item}`) + + suffix ); + + const reHostname = regexFromLabels(hostname, '$'); + let reEntity; + { + const domain = details.domain; + const pos = domain.indexOf('.'); + if ( pos !== -1 ) { + reEntity = regexFromLabels( + hostname.slice(0, pos + hostname.length - domain.length), + '\\.\\*$' + ); + } } - + const hostnameMatches = hn => { return hn === '' || reHostname.test(hn) ||