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

Delta Between Two Patch Sets: lib/matcher.js

Issue 5991150368325632: Issue 616 - Add specificOnly parameter to matchesAny (Closed)
Left Patch Set: Make specificOnly parameter for matchesAny actually work... Created March 11, 2015, 5:04 p.m.
Right Patch Set: Make use of new isGeneric filter method Created Aug. 25, 2015, 5:43 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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, specificOnly) 168 _checkEntryMatch: function(keyword, location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
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
Sebastian Noack 2015/03/12 11:36:21 Since the filter match is certainly the most expen
Sebastian Noack 2015/03/12 11:36:21 This is a very long chain of conditions. How about
kzar 2015/03/12 17:08:09 Done.
kzar 2015/03/12 17:08:09 Done.
175 (!specificOnly || filter instanceof WhitelistFilter || 175 if (specificOnly && filter.isGeneric() &&
176 !(!filter.domains || filter.domains[""]))) 176 !(filter instanceof WhitelistFilter))
Sebastian Noack 2015/03/12 11:36:21 Isn't this the same as (filter.domains && !filter.
kzar 2015/03/12 17:08:09 Done.
177 continue;
178
179 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey))
177 return filter; 180 return filter;
178 } 181 }
179 return null; 182 return null;
180 }, 183 },
181 184
182 /** 185 /**
183 * Tests whether the URL matches any of the known filters 186 * Tests whether the URL matches any of the known filters
184 * @param {String} location URL to be tested 187 * @param {String} location URL to be tested
185 * @param {String} contentType content type identifier of the URL 188 * @param {String} typeMask bitmask of content / request types to match
186 * @param {String} docDomain domain name of the document that loads the URL 189 * @param {String} docDomain domain name of the document that loads the URL
187 * @param {Boolean} thirdParty should be true if the URL is a third-party requ est 190 * @param {Boolean} thirdParty should be true if the URL is a third-party requ est
188 * @param {String} sitekey public key provided by the document 191 * @param {String} sitekey public key provided by the document
189 * @param {Boolean} specificOnly should be true if generic matches should be i gnored 192 * @param {Boolean} specificOnly should be true if generic matches should be i gnored
190 * @return {RegExpFilter} matching filter or null 193 * @return {RegExpFilter} matching filter or null
191 */ 194 */
192 matchesAny: function(location, contentType, docDomain, thirdParty, sitekey, sp ecificOnly) 195 matchesAny: function(location, typeMask, docDomain, thirdParty, sitekey, speci ficOnly)
193 { 196 {
194 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); 197 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g);
195 if (candidates === null) 198 if (candidates === null)
196 candidates = []; 199 candidates = [];
197 candidates.push(""); 200 candidates.push("");
198 for (let i = 0, l = candidates.length; i < l; i++) 201 for (let i = 0, l = candidates.length; i < l; i++)
199 { 202 {
200 let substr = candidates[i]; 203 let substr = candidates[i];
201 if (substr in this.filterByKeyword) 204 if (substr in this.filterByKeyword)
202 { 205 {
203 let result = this._checkEntryMatch(substr, location, contentType, docDom ain, thirdParty, sitekey, specificOnly); 206 let result = this._checkEntryMatch(substr, location, typeMask, docDomain , thirdParty, sitekey, specificOnly);
204 if (result) 207 if (result)
205 return result; 208 return result;
206 } 209 }
207 } 210 }
208 211
209 return null; 212 return null;
210 } 213 }
211 }; 214 };
212 215
213 /** 216 /**
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 return !matcher.getKeywordForFilter(filter); 346 return !matcher.getKeywordForFilter(filter);
344 else 347 else
345 return !matcher.findKeyword(filter); 348 return !matcher.findKeyword(filter);
346 }, 349 },
347 350
348 /** 351 /**
349 * Optimized filter matching testing both whitelist and blacklist matchers 352 * Optimized filter matching testing both whitelist and blacklist matchers
350 * simultaneously. For parameters see Matcher.matchesAny(). 353 * simultaneously. For parameters see Matcher.matchesAny().
351 * @see Matcher#matchesAny 354 * @see Matcher#matchesAny
352 */ 355 */
353 matchesAnyInternal: function(location, contentType, docDomain, thirdParty, sit ekey, specificOnly) 356 matchesAnyInternal: function(location, typeMask, docDomain, thirdParty, siteke y, specificOnly)
354 { 357 {
355 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); 358 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g);
356 if (candidates === null) 359 if (candidates === null)
357 candidates = []; 360 candidates = [];
358 candidates.push(""); 361 candidates.push("");
359 362
360 let blacklistHit = null; 363 let blacklistHit = null;
361 for (let i = 0, l = candidates.length; i < l; i++) 364 for (let i = 0, l = candidates.length; i < l; i++)
362 { 365 {
363 let substr = candidates[i]; 366 let substr = candidates[i];
364 if (substr in this.whitelist.filterByKeyword) 367 if (substr in this.whitelist.filterByKeyword)
365 { 368 {
366 let result = this.whitelist._checkEntryMatch(substr, location, contentTy pe, docDomain, thirdParty, sitekey); 369 let result = this.whitelist._checkEntryMatch(substr, location, typeMask, docDomain, thirdParty, sitekey);
367 if (result) 370 if (result)
368 return result; 371 return result;
369 } 372 }
370 if (substr in this.blacklist.filterByKeyword && blacklistHit === null) 373 if (substr in this.blacklist.filterByKeyword && blacklistHit === null)
371 blacklistHit = this.blacklist._checkEntryMatch(substr, location, content Type, docDomain, thirdParty, sitekey, specificOnly); 374 blacklistHit = this.blacklist._checkEntryMatch(substr, location, typeMas k, docDomain, thirdParty, sitekey, specificOnly);
372 } 375 }
373 return blacklistHit; 376 return blacklistHit;
374 }, 377 },
375 378
376 /** 379 /**
377 * @see Matcher#matchesAny 380 * @see Matcher#matchesAny
378 */ 381 */
379 matchesAny: function(location, contentType, docDomain, thirdParty, sitekey, sp ecificOnly) 382 matchesAny: function(location, typeMask, docDomain, thirdParty, sitekey, speci ficOnly)
380 { 383 {
381 let key = location + " " + contentType + " " + docDomain + " " + thirdParty + " " + sitekey + " " + specificOnly; 384 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + " " + sitekey + " " + specificOnly;
382 if (key in this.resultCache) 385 if (key in this.resultCache)
383 return this.resultCache[key]; 386 return this.resultCache[key];
384 387
385 let result = this.matchesAnyInternal(location, contentType, docDomain, third Party, sitekey, specificOnly); 388 let result = this.matchesAnyInternal(location, typeMask, docDomain, thirdPar ty, sitekey, specificOnly);
386 389
387 if (this.cacheEntries >= CombinedMatcher.maxCacheEntries) 390 if (this.cacheEntries >= CombinedMatcher.maxCacheEntries)
388 { 391 {
389 this.resultCache = Object.create(null); 392 this.resultCache = Object.create(null);
390 this.cacheEntries = 0; 393 this.cacheEntries = 0;
391 } 394 }
392 395
393 this.resultCache[key] = result; 396 this.resultCache[key] = result;
394 this.cacheEntries++; 397 this.cacheEntries++;
395 398
396 return result; 399 return result;
397 } 400 }
398 } 401 }
399 402
400 /** 403 /**
401 * Shared CombinedMatcher instance that should usually be used. 404 * Shared CombinedMatcher instance that should usually be used.
402 * @type CombinedMatcher 405 * @type CombinedMatcher
403 */ 406 */
404 let defaultMatcher = exports.defaultMatcher = new CombinedMatcher(); 407 let defaultMatcher = exports.defaultMatcher = new CombinedMatcher();
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld