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

Side by Side Diff: lib/matcher.js

Issue 5991150368325632: Issue 616 - Add specificOnly parameter to matchesAny (Closed)
Patch Set: Created March 6, 2015, 9:48 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
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} contentType content type identifier of the URL
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 * @param {Boolean} specificOnly should be true if generic matches should be i gnored
187 * @return {RegExpFilter} matching filter or null 188 * @return {RegExpFilter} matching filter or null
188 */ 189 */
189 matchesAny: function(location, contentType, docDomain, thirdParty, sitekey) 190 matchesAny: function(location, contentType, docDomain, thirdParty, sitekey, sp ecificOnly)
190 { 191 {
191 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); 192 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g);
192 if (candidates === null) 193 if (candidates === null)
193 candidates = []; 194 candidates = [];
194 candidates.push(""); 195
196 if (!specificOnly)
197 candidates.push("");
198
195 for (let i = 0, l = candidates.length; i < l; i++) 199 for (let i = 0, l = candidates.length; i < l; i++)
196 { 200 {
197 let substr = candidates[i]; 201 let substr = candidates[i];
198 if (substr in this.filterByKeyword) 202 if (substr in this.filterByKeyword)
199 { 203 {
200 let result = this._checkEntryMatch(substr, location, contentType, docDom ain, thirdParty, sitekey); 204 let result = this._checkEntryMatch(substr, location, contentType, docDom ain, thirdParty, sitekey);
201 if (result) 205 if (result)
202 return result; 206 return result;
203 } 207 }
204 } 208 }
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 return !matcher.getKeywordForFilter(filter); 344 return !matcher.getKeywordForFilter(filter);
341 else 345 else
342 return !matcher.findKeyword(filter); 346 return !matcher.findKeyword(filter);
343 }, 347 },
344 348
345 /** 349 /**
346 * Optimized filter matching testing both whitelist and blacklist matchers 350 * Optimized filter matching testing both whitelist and blacklist matchers
347 * simultaneously. For parameters see Matcher.matchesAny(). 351 * simultaneously. For parameters see Matcher.matchesAny().
348 * @see Matcher#matchesAny 352 * @see Matcher#matchesAny
349 */ 353 */
350 matchesAnyInternal: function(location, contentType, docDomain, thirdParty, sit ekey) 354 matchesAnyInternal: function(location, contentType, docDomain, thirdParty, sit ekey, specificOnly)
351 { 355 {
352 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); 356 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g);
353 if (candidates === null) 357 if (candidates === null)
354 candidates = []; 358 candidates = [];
355 candidates.push(""); 359
360 if (!specificOnly)
361 candidates.push("");
356 362
357 let blacklistHit = null; 363 let blacklistHit = null;
358 for (let i = 0, l = candidates.length; i < l; i++) 364 for (let i = 0, l = candidates.length; i < l; i++)
359 { 365 {
360 let substr = candidates[i]; 366 let substr = candidates[i];
361 if (substr in this.whitelist.filterByKeyword) 367 if (substr in this.whitelist.filterByKeyword)
362 { 368 {
363 let result = this.whitelist._checkEntryMatch(substr, location, contentTy pe, docDomain, thirdParty, sitekey); 369 let result = this.whitelist._checkEntryMatch(substr, location, contentTy pe, docDomain, thirdParty, sitekey);
364 if (result) 370 if (result)
365 return result; 371 return result;
366 } 372 }
367 if (substr in this.blacklist.filterByKeyword && blacklistHit === null) 373 if (substr in this.blacklist.filterByKeyword && blacklistHit === null)
368 blacklistHit = this.blacklist._checkEntryMatch(substr, location, content Type, docDomain, thirdParty, sitekey); 374 blacklistHit = this.blacklist._checkEntryMatch(substr, location, content Type, docDomain, thirdParty, sitekey);
369 } 375 }
370 return blacklistHit; 376 return blacklistHit;
371 }, 377 },
372 378
373 /** 379 /**
374 * @see Matcher#matchesAny 380 * @see Matcher#matchesAny
375 */ 381 */
376 matchesAny: function(location, contentType, docDomain, thirdParty, sitekey) 382 matchesAny: function(location, contentType, docDomain, thirdParty, sitekey, sp ecificOnly)
377 { 383 {
378 let key = location + " " + contentType + " " + docDomain + " " + thirdParty + " " + sitekey; 384 let key = location + " " + contentType + " " + docDomain + " " + thirdParty + " " + sitekey + " " + specificOnly;
379 if (key in this.resultCache) 385 if (key in this.resultCache)
380 return this.resultCache[key]; 386 return this.resultCache[key];
381 387
382 let result = this.matchesAnyInternal(location, contentType, docDomain, third Party, sitekey); 388 let result = this.matchesAnyInternal(location, contentType, docDomain, third Party, sitekey, specificOnly);
383 389
384 if (this.cacheEntries >= CombinedMatcher.maxCacheEntries) 390 if (this.cacheEntries >= CombinedMatcher.maxCacheEntries)
385 { 391 {
386 this.resultCache = Object.create(null); 392 this.resultCache = Object.create(null);
387 this.cacheEntries = 0; 393 this.cacheEntries = 0;
388 } 394 }
389 395
390 this.resultCache[key] = result; 396 this.resultCache[key] = result;
391 this.cacheEntries++; 397 this.cacheEntries++;
392 398
393 return result; 399 return result;
394 } 400 }
395 } 401 }
396 402
397 /** 403 /**
398 * Shared CombinedMatcher instance that should usually be used. 404 * Shared CombinedMatcher instance that should usually be used.
399 * @type CombinedMatcher 405 * @type CombinedMatcher
400 */ 406 */
401 let defaultMatcher = exports.defaultMatcher = new CombinedMatcher(); 407 let defaultMatcher = exports.defaultMatcher = new CombinedMatcher();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld