code review

This commit is contained in:
gorhill 2015-03-19 11:16:56 -04:00
parent 512cc48dee
commit aa9bea2e76
1 changed files with 15 additions and 19 deletions

View File

@ -237,19 +237,9 @@ var removeElements = function(elems) {
var incrementalNetFilter = (function() { var incrementalNetFilter = (function() {
var lastHostname = ''; var lastHostname = '';
var lastNetFilter = ''; var lastNetFilter = '';
var reTokenizer = /([^0-9a-z%*]+)([0-9a-z%]+|\*)/gi; var reTokenizer = /[^0-9a-z%*]+|[0-9a-z%]+|\*/gi;
var a = document.createElement('a'); var a = document.createElement('a');
var tokenize = function(s) {
var out = [];
var match;
reTokenizer.lastIndex = 0;
while ( match = reTokenizer.exec(s) ) {
out.push(match[1], match[2]);
}
return out;
};
var compute = function(to, out) { var compute = function(to, out) {
a.href= to; a.href= to;
to = a.pathname + a.search; to = a.pathname + a.search;
@ -270,24 +260,30 @@ var incrementalNetFilter = (function() {
// Related URLs // Related URLs
lastHostname = a.host; lastHostname = a.host;
var fromTokens = tokenize(from); var fromTokens = from.match(reTokenizer);
var toTokens = tokenize(to); var toTokens = to.match(reTokenizer);
var toIndex = 0, i; var toCount = toTokens.length, toIndex = 0;
var fromToken, pos;
for ( var fromIndex = 0; fromIndex < fromTokens.length; fromIndex += 1 ) { for ( var fromIndex = 0; fromIndex < fromTokens.length; fromIndex += 1 ) {
if ( fromTokens[fromIndex] === '*' ) { fromToken = fromTokens[fromIndex];
if ( fromToken === '*' ) {
continue; continue;
} }
i = toTokens.indexOf(fromTokens[fromIndex], toIndex); pos = toTokens.indexOf(fromToken, toIndex);
if ( i === -1 ) { if ( pos === -1 ) {
fromTokens[fromIndex] = '*'; fromTokens[fromIndex] = '*';
continue; continue;
} }
if ( i !== toIndex ) { if ( pos !== toIndex ) {
fromTokens.splice(fromIndex, 0, '*'); fromTokens.splice(fromIndex, 0, '*');
fromIndex += 1; fromIndex += 1;
} }
toIndex = i + 1; toIndex = pos + 1;
if ( toIndex === toCount ) {
fromTokens = fromTokens.slice(0, fromIndex + 1);
break;
}
} }
from = fromTokens.join('').replace(/\*\*+/g, '*'); from = fromTokens.join('').replace(/\*\*+/g, '*');
if ( from !== '/*' ) { if ( from !== '/*' ) {