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

Side by Side Diff: lib/matcher.js

Issue 29896562: Issue 7003 - Look up whitelist filter only if URL is blocked (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Inline access to blacklist and whitelist Created Oct. 21, 2018, 3: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
« 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-present eyeo GmbH 3 * Copyright (C) 2006-present 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 /** 20 /**
21 * @fileOverview Matcher class implementing matching addresses against 21 * @fileOverview Matcher class implementing matching addresses against
22 * a list of filters. 22 * a list of filters.
23 */ 23 */
24 24
25 const {WhitelistFilter} = require("./filterClasses"); 25 const {RegExpFilter, WhitelistFilter} = require("./filterClasses");
26 26
27 /** 27 /**
28 * Regular expression for matching a keyword in a filter. 28 * Regular expression for matching a keyword in a filter.
29 * @type {RegExp} 29 * @type {RegExp}
30 */ 30 */
31 const keywordRegExp = /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/; 31 const keywordRegExp = /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/;
32 32
33 /** 33 /**
34 * Regular expression for matching all keywords in a filter. 34 * Regular expression for matching all keywords in a filter.
35 * @type {RegExp} 35 * @type {RegExp}
36 */ 36 */
37 const allKeywordsRegExp = new RegExp(keywordRegExp, "g"); 37 const allKeywordsRegExp = new RegExp(keywordRegExp, "g");
38 38
39 /** 39 /**
40 * Bitmask for "types" that are for exception rules only, like
41 * <code>$document</code>, <code>$elemhide</code>, and so on.
42 * @type {number}
43 */
44 const WHITELIST_ONLY_TYPES = RegExpFilter.typeMap.DOCUMENT |
45 RegExpFilter.typeMap.ELEMHIDE |
46 RegExpFilter.typeMap.GENERICHIDE |
47 RegExpFilter.typeMap.GENERICBLOCK;
48
49 /**
40 * Checks whether a particular filter is slow. 50 * Checks whether a particular filter is slow.
41 * @param {RegExpFilter} filter 51 * @param {RegExpFilter} filter
42 * @returns {boolean} 52 * @returns {boolean}
43 */ 53 */
44 function isSlowFilter(filter) 54 function isSlowFilter(filter)
45 { 55 {
46 return !filter.pattern || !keywordRegExp.test(filter.pattern); 56 return !filter.pattern || !keywordRegExp.test(filter.pattern);
47 } 57 }
48 58
49 exports.isSlowFilter = isSlowFilter; 59 exports.isSlowFilter = isSlowFilter;
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 * @inheritdoc 323 * @inheritdoc
314 */ 324 */
315 matchesAnyInternal(location, typeMask, docDomain, thirdParty, sitekey, 325 matchesAnyInternal(location, typeMask, docDomain, thirdParty, sitekey,
316 specificOnly) 326 specificOnly)
317 { 327 {
318 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); 328 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g);
319 if (candidates === null) 329 if (candidates === null)
320 candidates = []; 330 candidates = [];
321 candidates.push(""); 331 candidates.push("");
322 332
333 let whitelistHit = null;
323 let blacklistHit = null; 334 let blacklistHit = null;
324 for (let i = 0, l = candidates.length; i < l; i++) 335
336 if ((typeMask & ~WHITELIST_ONLY_TYPES) != 0)
Sebastian Noack 2018/10/21 22:41:39 I wonder if this logic is necessary. Shouldn't cal
Manish Jethani 2018/10/22 11:05:16 I like the idea, but we would lose out on the cach
Sebastian Noack 2018/10/22 15:04:31 No strong opinion either way, but in case we go wi
Manish Jethani 2018/10/22 20:39:30 We will be doing this as part of this: https://co
Manish Jethani 2018/10/22 21:16:19 I've added comments to make it clearer.
Sebastian Noack 2018/10/22 22:12:35 I don't see any changes in the current patch there
Manish Jethani 2018/10/22 22:57:56 Yes, I'll suggest that change there if we agree on
325 { 337 {
326 let substr = candidates[i]; 338 for (let i = 0, l = candidates.length; !blacklistHit && i < l; i++)
327 let result = this.whitelist._checkEntryMatch(
328 substr, location, typeMask, docDomain, thirdParty, sitekey
329 );
330 if (result)
331 return result;
332 if (blacklistHit === null)
333 { 339 {
334 blacklistHit = this.blacklist._checkEntryMatch( 340 blacklistHit = this.blacklist._checkEntryMatch(candidates[i], location,
335 substr, location, typeMask, docDomain, thirdParty, sitekey, 341 typeMask, docDomain,
336 specificOnly 342 thirdParty, sitekey,
337 ); 343 specificOnly);
338 } 344 }
339 } 345 }
340 return blacklistHit; 346
347 if (blacklistHit || (typeMask & WHITELIST_ONLY_TYPES) != 0)
348 {
349 for (let i = 0, l = candidates.length; !whitelistHit && i < l; i++)
350 {
351 whitelistHit = this.whitelist._checkEntryMatch(candidates[i], location,
352 typeMask, docDomain,
353 thirdParty, sitekey);
354 }
355 }
356
357 return whitelistHit || blacklistHit;
341 } 358 }
342 359
343 /** 360 /**
344 * @see Matcher#matchesAny 361 * @see Matcher#matchesAny
345 * @inheritdoc 362 * @inheritdoc
346 */ 363 */
347 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) 364 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
348 { 365 {
349 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + 366 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty +
350 " " + sitekey + " " + specificOnly; 367 " " + sitekey + " " + specificOnly;
(...skipping 16 matching lines...) Expand all
367 384
368 exports.CombinedMatcher = CombinedMatcher; 385 exports.CombinedMatcher = CombinedMatcher;
369 386
370 /** 387 /**
371 * Shared {@link CombinedMatcher} instance that should usually be used. 388 * Shared {@link CombinedMatcher} instance that should usually be used.
372 * @type {CombinedMatcher} 389 * @type {CombinedMatcher}
373 */ 390 */
374 let defaultMatcher = new CombinedMatcher(); 391 let defaultMatcher = new CombinedMatcher();
375 392
376 exports.defaultMatcher = defaultMatcher; 393 exports.defaultMatcher = defaultMatcher;
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