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: Check whitelist for whitelist-only types Created Oct. 21, 2018, 2:43 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)
Manish Jethani 2018/10/21 15:00:23 If the type mask includes no types other than DOCU
325 { 337 {
326 let substr = candidates[i]; 338 for (let i = 0, l = candidates.length; 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 {
340 let substr = candidates[i];
334 blacklistHit = this.blacklist._checkEntryMatch( 341 blacklistHit = this.blacklist._checkEntryMatch(
335 substr, location, typeMask, docDomain, thirdParty, sitekey, 342 substr, location, typeMask, docDomain, thirdParty, sitekey,
336 specificOnly 343 specificOnly
337 ); 344 );
345 if (blacklistHit)
346 break;
338 } 347 }
339 } 348 }
340 return blacklistHit; 349
350 if (blacklistHit || (typeMask & WHITELIST_ONLY_TYPES) != 0)
Manish Jethani 2018/10/21 15:00:22 If the type mask includes DOCUMENT, ELEMHIDE, GENE
351 {
352 for (let i = 0, l = candidates.length; i < l; i++)
353 {
354 let substr = candidates[i];
355 whitelistHit = this.whitelist._checkEntryMatch(
356 substr, location, typeMask, docDomain, thirdParty, sitekey
357 );
358 if (whitelistHit)
359 break;
360 }
361 }
362
363 return whitelistHit || blacklistHit;
341 } 364 }
342 365
343 /** 366 /**
344 * @see Matcher#matchesAny 367 * @see Matcher#matchesAny
345 * @inheritdoc 368 * @inheritdoc
346 */ 369 */
347 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) 370 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
348 { 371 {
349 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + 372 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty +
350 " " + sitekey + " " + specificOnly; 373 " " + sitekey + " " + specificOnly;
(...skipping 16 matching lines...) Expand all
367 390
368 exports.CombinedMatcher = CombinedMatcher; 391 exports.CombinedMatcher = CombinedMatcher;
369 392
370 /** 393 /**
371 * Shared {@link CombinedMatcher} instance that should usually be used. 394 * Shared {@link CombinedMatcher} instance that should usually be used.
372 * @type {CombinedMatcher} 395 * @type {CombinedMatcher}
373 */ 396 */
374 let defaultMatcher = new CombinedMatcher(); 397 let defaultMatcher = new CombinedMatcher();
375 398
376 exports.defaultMatcher = defaultMatcher; 399 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