mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-11 09:31:01 +01:00
#2781: code review
This commit is contained in:
parent
061bee8509
commit
f0ea2b6f9a
1 changed files with 22 additions and 16 deletions
|
@ -1801,7 +1801,9 @@ FilterParser.prototype.parse = function(raw) {
|
||||||
// Hostname-anchored with no wildcard always have a token index of 0.
|
// Hostname-anchored with no wildcard always have a token index of 0.
|
||||||
var reHostnameToken = /^[0-9a-z]+/;
|
var reHostnameToken = /^[0-9a-z]+/;
|
||||||
var reGoodToken = /[%0-9a-z]{2,}/g;
|
var reGoodToken = /[%0-9a-z]{2,}/g;
|
||||||
var reRegexToken = /^[^([{?]*?([%0-9a-z]{2,})/;
|
var reRegexToken = /^[^([{?]*?[^([{?%0-9A-Za-z]([%0-9A-Za-z]{2,})/;
|
||||||
|
var reRegexBadPrefix = /(^|\\|[^\\]\.)$/;
|
||||||
|
var reRegexBadSuffix = /^([^\\]\.|\\[dw]|[([{?*]|$)/;
|
||||||
|
|
||||||
var badTokens = new Set([
|
var badTokens = new Set([
|
||||||
'com',
|
'com',
|
||||||
|
@ -1816,9 +1818,10 @@ var badTokens = new Set([
|
||||||
'www'
|
'www'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
var findFirstGoodToken = function(s) {
|
FilterParser.prototype.findFirstGoodToken = function() {
|
||||||
reGoodToken.lastIndex = 0;
|
reGoodToken.lastIndex = 0;
|
||||||
var matches, lpos,
|
var s = this.f,
|
||||||
|
matches, lpos,
|
||||||
badTokenMatch = null;
|
badTokenMatch = null;
|
||||||
while ( (matches = reGoodToken.exec(s)) !== null ) {
|
while ( (matches = reGoodToken.exec(s)) !== null ) {
|
||||||
// https://github.com/gorhill/uBlock/issues/997
|
// https://github.com/gorhill/uBlock/issues/997
|
||||||
|
@ -1841,6 +1844,19 @@ var findFirstGoodToken = function(s) {
|
||||||
return badTokenMatch;
|
return badTokenMatch;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
FilterParser.prototype.extractTokenFromRegex = function() {
|
||||||
|
var s = this.f,
|
||||||
|
matches = reRegexToken.exec(s);
|
||||||
|
if ( matches === null ) { return; }
|
||||||
|
var tokenEnd = matches[0].length,
|
||||||
|
tokenBeg = tokenEnd - matches[1].length;
|
||||||
|
if ( reRegexBadPrefix.test(s.slice(0, tokenBeg)) ) { return; }
|
||||||
|
if ( reRegexBadSuffix.test(s.slice(tokenEnd)) ) { return; }
|
||||||
|
this.token = matches[1].toLowerCase();
|
||||||
|
this.tokenHash = µb.urlTokenizer.tokenHashFromString(this.token);
|
||||||
|
this.tokenBeg = tokenBeg;
|
||||||
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
// https://github.com/chrisaljoudi/uBlock/issues/1038
|
// https://github.com/chrisaljoudi/uBlock/issues/1038
|
||||||
|
@ -1850,29 +1866,19 @@ var findFirstGoodToken = function(s) {
|
||||||
// For efficiency purpose, try to extract a token from a regex-based filter.
|
// For efficiency purpose, try to extract a token from a regex-based filter.
|
||||||
|
|
||||||
FilterParser.prototype.makeToken = function() {
|
FilterParser.prototype.makeToken = function() {
|
||||||
var matches;
|
|
||||||
|
|
||||||
if ( this.isRegex ) {
|
if ( this.isRegex ) {
|
||||||
matches = reRegexToken.exec(this.f);
|
this.extractTokenFromRegex();
|
||||||
if (
|
|
||||||
matches !== null &&
|
|
||||||
this.f.charAt(matches[0].length - matches[1].length - 1) !== '\\'
|
|
||||||
) {
|
|
||||||
this.token = matches[1];
|
|
||||||
this.tokenHash = µb.urlTokenizer.tokenHashFromString(this.token);
|
|
||||||
this.tokenBeg = matches[0].length - matches[1].length;
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( this.f === '*' ) { return; }
|
if ( this.f === '*' ) { return; }
|
||||||
|
|
||||||
matches = null;
|
var matches = null;
|
||||||
if ( (this.anchor & 0x4) !== 0 && this.f.indexOf('*') === -1 ) {
|
if ( (this.anchor & 0x4) !== 0 && this.f.indexOf('*') === -1 ) {
|
||||||
matches = reHostnameToken.exec(this.f);
|
matches = reHostnameToken.exec(this.f);
|
||||||
}
|
}
|
||||||
if ( matches === null ) {
|
if ( matches === null ) {
|
||||||
matches = findFirstGoodToken(this.f);
|
matches = this.findFirstGoodToken();
|
||||||
}
|
}
|
||||||
if ( matches !== null ) {
|
if ( matches !== null ) {
|
||||||
this.token = matches[0];
|
this.token = matches[0];
|
||||||
|
|
Loading…
Reference in a new issue