mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-11 17:41:03 +01:00
Fix reverse lookup of entity-based cosmetic filters
Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/805
This commit is contained in:
parent
f7cc2ed7b1
commit
082201d24a
1 changed files with 18 additions and 28 deletions
|
@ -121,43 +121,33 @@ const fromCosmeticFilter = function(details) {
|
||||||
const exception = prefix.charAt(1) === '@';
|
const exception = prefix.charAt(1) === '@';
|
||||||
const selector = details.rawFilter.slice(prefix.length);
|
const selector = details.rawFilter.slice(prefix.length);
|
||||||
const isHtmlFilter = prefix.endsWith('^');
|
const isHtmlFilter = prefix.endsWith('^');
|
||||||
|
const hostname = details.hostname;
|
||||||
|
|
||||||
// The longer the needle, the lower the number of false positives.
|
// The longer the needle, the lower the number of false positives.
|
||||||
const needle = selector.match(/\w+/g).reduce(function(a, b) {
|
const needle = selector.match(/\w+/g).reduce(function(a, b) {
|
||||||
return a.length > b.length ? a : b;
|
return a.length > b.length ? a : b;
|
||||||
});
|
});
|
||||||
|
|
||||||
const reHostname = new RegExp(
|
const regexFromLabels = (hn, suffix) =>
|
||||||
'^' +
|
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(
|
|
||||||
'^' +
|
'^' +
|
||||||
domain.slice(0, pos).split('.').reduce(
|
hn.split('.').reduce((acc, item) => `(${acc}\\.)?${item}`) +
|
||||||
function(acc, item) {
|
suffix
|
||||||
return acc === ''
|
|
||||||
? item
|
|
||||||
: '(' + acc + '\\.)?' + item;
|
|
||||||
},
|
|
||||||
''
|
|
||||||
) +
|
|
||||||
'\\.\\*$'
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
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 => {
|
const hostnameMatches = hn => {
|
||||||
return hn === '' ||
|
return hn === '' ||
|
||||||
reHostname.test(hn) ||
|
reHostname.test(hn) ||
|
||||||
|
|
Loading…
Reference in a new issue