@@ -75,7 +75,6 @@ var filterIndex = {};
7575var reIgnoreFilter = / ^ \[ | ^ ! | # # | @ # | @ @ | ^ \| h t t p / ;
7676var reConditionalRule = / \$ / ;
7777var reHostnameRule = / ^ \| \| [ a - z 0 - 9 . - ] + \^ ? $ / ;
78- var reWildcardRule = / [ ^ * ] [ * ] + [ ^ * ] / ;
7978var reToken = / [ % 0 - 9 A - Z a - z ] { 2 , } / g;
8079
8180// My favorite regex tester: http://regexpal.com/
@@ -107,7 +106,7 @@ FilterPlain.prototype.match = function(s, tokenBeg) {
107106
108107/******************************************************************************/
109108
110- FilterPlainPrefix0 = function ( s , tokenBeg ) {
109+ var FilterPlainPrefix0 = function ( s , tokenBeg ) {
111110 Filter . apply ( this , arguments ) ;
112111} ;
113112
@@ -117,7 +116,7 @@ FilterPlainPrefix0.prototype.match = function(s, tokenBeg) {
117116
118117/******************************************************************************/
119118
120- FilterPlainPrefix1 = function ( s , tokenBeg ) {
119+ var FilterPlainPrefix1 = function ( s , tokenBeg ) {
121120 Filter . apply ( this , arguments ) ;
122121} ;
123122
@@ -132,15 +131,15 @@ FilterPlainPrefix1.prototype.match = function(s, tokenBeg) {
132131// http://jsperf.com/regexp-vs-indexof-abp-miss
133132// http://jsperf.com/regexp-vs-indexof-abp-hit
134133
135- FilterSingleWildcard = function ( s , tokenBeg , tokenLen ) {
134+ var FilterSingleWildcard = function ( s , tokenBeg , tokenLen ) {
136135 Filter . apply ( this , arguments ) ;
137136 this . wcOffset = s . indexOf ( '*' ) ;
138137 this . lSegment = s . slice ( 0 , this . wcOffset ) ;
139138 this . rSegment = s . slice ( this . wcOffset + 1 ) ;
140139} ;
141140
142141FilterSingleWildcard . prototype . match = function ( s , tokenBeg ) {
143- tokenBeg - this . tokenBeg ;
142+ tokenBeg -= this . tokenBeg ;
144143 return s . indexOf ( this . lSegment , tokenBeg ) === tokenBeg &&
145144 s . indexOf ( this . rSegment , tokenBeg + this . wcOffset ) > 0 ;
146145} ;
@@ -149,7 +148,7 @@ FilterSingleWildcard.prototype.match = function(s, tokenBeg) {
149148
150149// With many wildcards, a regex is best.
151150
152- FilterManyWildcards = function ( s , tokenBeg , tokenLen ) {
151+ var FilterManyWildcards = function ( s , tokenBeg , tokenLen ) {
153152 Filter . apply ( this , arguments ) ;
154153 // Ref: escaper taken from:
155154 // https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
@@ -163,15 +162,15 @@ FilterManyWildcards.prototype.match = function(s, tokenBeg) {
163162
164163/******************************************************************************/
165164
166- FilterFactory = function ( s , tokenBeg , tokenLen ) {
165+ var FilterFactory = function ( s , tokenBeg , tokenLen ) {
167166 var wcOffset = s . indexOf ( '*' ) ;
168167 if ( wcOffset > 0 ) {
169168 return FilterWildcardFactory ( s , tokenBeg , tokenLen ) ;
170169 }
171170 return FilterPlainFactory ( s , tokenBeg , tokenLen ) ;
172171} ;
173172
174- FilterPlainFactory = function ( s , tokenBeg , tokenLen ) {
173+ var FilterPlainFactory = function ( s , tokenBeg , tokenLen ) {
175174 if ( tokenBeg === 0 ) {
176175 return new FilterPlainPrefix0 ( s , 0 , tokenLen ) ;
177176 }
@@ -181,7 +180,7 @@ FilterPlainFactory = function(s, tokenBeg, tokenLen) {
181180 return new FilterPlain ( s , tokenBeg , tokenLen ) ;
182181} ;
183182
184- FilterWildcardFactory = function ( s , tokenBeg , tokenLen ) {
183+ var FilterWildcardFactory = function ( s , tokenBeg , tokenLen ) {
185184 if ( ( / \* [ ^ * ] \* / ) . test ( s ) ) {
186185 return FilterManyWildcards ( s , tokenBeg , tokenLen ) ;
187186 }
@@ -201,7 +200,7 @@ var reset = function() {
201200/******************************************************************************/
202201
203202// Given a string, find a good token. Tokens which are too generic, i.e. very
204- // common with a high probability of ending up as a false positive , are not
203+ // common with a high probability of ending up as a miss , are not
205204// good. Avoid if possible. This has a *significant* positive impact on
206205// performance.
207206// These "bad tokens" are collated manually.
@@ -210,6 +209,8 @@ var badTokens = {
210209 'com' : true ,
211210 'http' : true ,
212211 'https' : true ,
212+ 'images' : true ,
213+ 'img' : true ,
213214 'js' : true ,
214215 'news' : true ,
215216 'www' : true
@@ -230,6 +231,26 @@ var findGoodToken = function(s) {
230231
231232/******************************************************************************/
232233
234+ // Trim leading/trailing char "c"
235+
236+ var trimChar = function ( s , c ) {
237+ // Remove leading and trailing wildcards
238+ var pos = 0 ;
239+ while ( s . charAt ( pos ) === c ) {
240+ pos += 1 ;
241+ }
242+ s = s . slice ( pos ) ;
243+ if ( pos = s . length ) {
244+ while ( s . charAt ( pos - 1 ) === c ) {
245+ pos -= 1 ;
246+ }
247+ s = s . slice ( 0 , pos ) ;
248+ }
249+ return s ;
250+ } ;
251+
252+ /******************************************************************************/
253+
233254var add = function ( s ) {
234255 // Ignore unsupported filters
235256 if ( reIgnoreFilter . test ( s ) ) {
@@ -245,11 +266,6 @@ var add = function(s) {
245266 s = s . replace ( / \^ / g, '*' ) ;
246267 s = s . replace ( / \* \* + / g, '*' ) ;
247268
248- // Ignore rules with a wildcard in the middle
249- // if ( reWildcardRule.test(s) ) {
250- // return false;
251- // }
252-
253269 // Ignore hostname rules, these will be taken care of by HTTPSB.
254270 if ( reHostnameRule . test ( s ) ) {
255271 return false ;
@@ -259,16 +275,7 @@ var add = function(s) {
259275 s = s . replace ( / ^ \| + | \| + $ / , '' ) ;
260276
261277 // Remove leading and trailing wildcards
262- var pos = 0 ;
263- while ( s . charAt ( pos ) === '*' ) {
264- pos += 1 ;
265- }
266- s = s . slice ( pos ) ;
267- pos = s . length ;
268- while ( s . charAt ( pos - 1 ) === '*' ) {
269- pos -= 1 ;
270- }
271- s = s . slice ( 0 , pos ) ;
278+ s = trimChar ( s , '*' ) ;
272279
273280 // Already in dictionary?
274281 var filter = filterDict [ s ] ;
@@ -291,8 +298,8 @@ var add = function(s) {
291298 }
292299 filterDict [ s ] = filter ;
293300
294- var prefixKey = s . substring ( tokenBeg - 1 , tokenBeg ) ;
295- var suffixKey = s . substring ( tokenEnd , tokenEnd + 2 ) ;
301+ var prefixKey = trimChar ( s . substring ( tokenBeg - 1 , tokenBeg ) , '*' ) ;
302+ var suffixKey = trimChar ( s . substring ( tokenEnd , tokenEnd + 2 ) , '*' ) ;
296303 var fidx = filterIndex ;
297304 var tokenKey = prefixKey + token + suffixKey ;
298305 filter . next = fidx [ tokenKey ] ;
0 commit comments