Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/matcher.js

Issue 29321478: Issue 2738 - Make RegExpFilter.matches() take a bit mask instead of content type string (Closed)
Patch Set: Created July 9, 2015, 1:45 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 { 158 {
159 if (filter.text in this.keywordByFilter) 159 if (filter.text in this.keywordByFilter)
160 return this.keywordByFilter[filter.text]; 160 return this.keywordByFilter[filter.text];
161 else 161 else
162 return null; 162 return null;
163 }, 163 },
164 164
165 /** 165 /**
166 * Checks whether the entries for a particular keyword match a URL 166 * Checks whether the entries for a particular keyword match a URL
167 */ 167 */
168 _checkEntryMatch: function(keyword, location, contentType, docDomain, thirdPar ty, sitekey) 168 _checkEntryMatch: function(keyword, location, typeMask, docDomain, thirdParty, sitekey)
169 { 169 {
170 let list = this.filterByKeyword[keyword]; 170 let list = this.filterByKeyword[keyword];
171 for (let i = 0; i < list.length; i++) 171 for (let i = 0; i < list.length; i++)
172 { 172 {
173 let filter = list[i]; 173 let filter = list[i];
174 if (filter.matches(location, contentType, docDomain, thirdParty, sitekey)) 174 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey))
175 return filter; 175 return filter;
176 } 176 }
177 return null; 177 return null;
178 }, 178 },
179 179
180 /** 180 /**
181 * Tests whether the URL matches any of the known filters 181 * Tests whether the URL matches any of the known filters
182 * @param {String} location URL to be tested 182 * @param {String} location URL to be tested
183 * @param {String} contentType content type identifier of the URL 183 * @param {String} typeMask bitmask of content / request types to match
184 * @param {String} docDomain domain name of the document that loads the URL 184 * @param {String} docDomain domain name of the document that loads the URL
185 * @param {Boolean} thirdParty should be true if the URL is a third-party requ est 185 * @param {Boolean} thirdParty should be true if the URL is a third-party requ est
186 * @param {String} sitekey public key provided by the document 186 * @param {String} sitekey public key provided by the document
187 * @return {RegExpFilter} matching filter or null 187 * @return {RegExpFilter} matching filter or null
188 */ 188 */
189 matchesAny: function(location, contentType, docDomain, thirdParty, sitekey) 189 matchesAny: function(location, typeMask, docDomain, thirdParty, sitekey)
190 { 190 {
191 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); 191 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g);
192 if (candidates === null) 192 if (candidates === null)
193 candidates = []; 193 candidates = [];
194 candidates.push(""); 194 candidates.push("");
195 for (let i = 0, l = candidates.length; i < l; i++) 195 for (let i = 0, l = candidates.length; i < l; i++)
196 { 196 {
197 let substr = candidates[i]; 197 let substr = candidates[i];
198 if (substr in this.filterByKeyword) 198 if (substr in this.filterByKeyword)
199 { 199 {
200 let result = this._checkEntryMatch(substr, location, contentType, docDom ain, thirdParty, sitekey); 200 let result = this._checkEntryMatch(substr, location, typeMask, docDomain , thirdParty, sitekey);
201 if (result) 201 if (result)
202 return result; 202 return result;
203 } 203 }
204 } 204 }
205 205
206 return null; 206 return null;
207 } 207 }
208 }; 208 };
209 209
210 /** 210 /**
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 return !matcher.getKeywordForFilter(filter); 340 return !matcher.getKeywordForFilter(filter);
341 else 341 else
342 return !matcher.findKeyword(filter); 342 return !matcher.findKeyword(filter);
343 }, 343 },
344 344
345 /** 345 /**
346 * Optimized filter matching testing both whitelist and blacklist matchers 346 * Optimized filter matching testing both whitelist and blacklist matchers
347 * simultaneously. For parameters see Matcher.matchesAny(). 347 * simultaneously. For parameters see Matcher.matchesAny().
348 * @see Matcher#matchesAny 348 * @see Matcher#matchesAny
349 */ 349 */
350 matchesAnyInternal: function(location, contentType, docDomain, thirdParty, sit ekey) 350 matchesAnyInternal: function(location, typeMask, docDomain, thirdParty, siteke y)
351 { 351 {
352 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); 352 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g);
353 if (candidates === null) 353 if (candidates === null)
354 candidates = []; 354 candidates = [];
355 candidates.push(""); 355 candidates.push("");
356 356
357 let blacklistHit = null; 357 let blacklistHit = null;
358 for (let i = 0, l = candidates.length; i < l; i++) 358 for (let i = 0, l = candidates.length; i < l; i++)
359 { 359 {
360 let substr = candidates[i]; 360 let substr = candidates[i];
361 if (substr in this.whitelist.filterByKeyword) 361 if (substr in this.whitelist.filterByKeyword)
362 { 362 {
363 let result = this.whitelist._checkEntryMatch(substr, location, contentTy pe, docDomain, thirdParty, sitekey); 363 let result = this.whitelist._checkEntryMatch(substr, location, typeMask, docDomain, thirdParty, sitekey);
364 if (result) 364 if (result)
365 return result; 365 return result;
366 } 366 }
367 if (substr in this.blacklist.filterByKeyword && blacklistHit === null) 367 if (substr in this.blacklist.filterByKeyword && blacklistHit === null)
368 blacklistHit = this.blacklist._checkEntryMatch(substr, location, content Type, docDomain, thirdParty, sitekey); 368 blacklistHit = this.blacklist._checkEntryMatch(substr, location, typeMas k, docDomain, thirdParty, sitekey);
369 } 369 }
370 return blacklistHit; 370 return blacklistHit;
371 }, 371 },
372 372
373 /** 373 /**
374 * @see Matcher#matchesAny 374 * @see Matcher#matchesAny
375 */ 375 */
376 matchesAny: function(location, contentType, docDomain, thirdParty, sitekey) 376 matchesAny: function(location, typeMask, docDomain, thirdParty, sitekey)
377 { 377 {
378 let key = location + " " + contentType + " " + docDomain + " " + thirdParty + " " + sitekey; 378 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + " " + sitekey;
379 if (key in this.resultCache) 379 if (key in this.resultCache)
380 return this.resultCache[key]; 380 return this.resultCache[key];
381 381
382 let result = this.matchesAnyInternal(location, contentType, docDomain, third Party, sitekey); 382 let result = this.matchesAnyInternal(location, typeMask, docDomain, thirdPar ty, sitekey);
383 383
384 if (this.cacheEntries >= CombinedMatcher.maxCacheEntries) 384 if (this.cacheEntries >= CombinedMatcher.maxCacheEntries)
385 { 385 {
386 this.resultCache = Object.create(null); 386 this.resultCache = Object.create(null);
387 this.cacheEntries = 0; 387 this.cacheEntries = 0;
388 } 388 }
389 389
390 this.resultCache[key] = result; 390 this.resultCache[key] = result;
391 this.cacheEntries++; 391 this.cacheEntries++;
392 392
393 return result; 393 return result;
394 } 394 }
395 } 395 }
396 396
397 /** 397 /**
398 * Shared CombinedMatcher instance that should usually be used. 398 * Shared CombinedMatcher instance that should usually be used.
399 * @type CombinedMatcher 399 * @type CombinedMatcher
400 */ 400 */
401 let defaultMatcher = exports.defaultMatcher = new CombinedMatcher(); 401 let defaultMatcher = exports.defaultMatcher = new CombinedMatcher();
OLDNEW
« lib/filterClasses.js ('K') | « lib/filterClasses.js ('k') | lib/notification.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld