LEFT | RIGHT |
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 |
| 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; |
38 | 48 |
39 /** | 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 } |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
323 let whitelistHit = null; | 333 let whitelistHit = null; |
324 let blacklistHit = null; | 334 let blacklistHit = null; |
325 | 335 |
326 for (let i = 0, l = candidates.length; i < l; i++) | 336 // If the type mask includes no types other than whitelist-only types, we |
327 { | 337 // can skip the blacklist. |
328 let substr = candidates[i]; | 338 if ((typeMask & ~WHITELIST_ONLY_TYPES) != 0) |
329 blacklistHit = this.blacklist._checkEntryMatch( | 339 { |
330 substr, location, typeMask, docDomain, thirdParty, sitekey, | 340 for (let i = 0, l = candidates.length; !blacklistHit && i < l; i++) |
331 specificOnly | |
332 ); | |
333 if (blacklistHit) | |
334 break; | |
335 } | |
336 | |
337 if (blacklistHit) | |
338 { | |
339 for (let i = 0, l = candidates.length; i < l; i++) | |
340 { | 341 { |
341 let substr = candidates[i]; | 342 blacklistHit = this.blacklist._checkEntryMatch(candidates[i], location, |
342 whitelistHit = this.whitelist._checkEntryMatch( | 343 typeMask, docDomain, |
343 substr, location, typeMask, docDomain, thirdParty, sitekey | 344 thirdParty, sitekey, |
344 ); | 345 specificOnly); |
345 if (whitelistHit) | 346 } |
346 break; | 347 } |
| 348 |
| 349 // If the type mask includes any whitelist-only types, we need to check the |
| 350 // whitelist. |
| 351 if (blacklistHit || (typeMask & WHITELIST_ONLY_TYPES) != 0) |
| 352 { |
| 353 for (let i = 0, l = candidates.length; !whitelistHit && i < l; i++) |
| 354 { |
| 355 whitelistHit = this.whitelist._checkEntryMatch(candidates[i], location, |
| 356 typeMask, docDomain, |
| 357 thirdParty, sitekey); |
347 } | 358 } |
348 } | 359 } |
349 | 360 |
350 return whitelistHit || blacklistHit; | 361 return whitelistHit || blacklistHit; |
351 } | 362 } |
352 | 363 |
353 /** | 364 /** |
354 * @see Matcher#matchesAny | 365 * @see Matcher#matchesAny |
355 * @inheritdoc | 366 * @inheritdoc |
356 */ | 367 */ |
(...skipping 20 matching lines...) Expand all Loading... |
377 | 388 |
378 exports.CombinedMatcher = CombinedMatcher; | 389 exports.CombinedMatcher = CombinedMatcher; |
379 | 390 |
380 /** | 391 /** |
381 * Shared {@link CombinedMatcher} instance that should usually be used. | 392 * Shared {@link CombinedMatcher} instance that should usually be used. |
382 * @type {CombinedMatcher} | 393 * @type {CombinedMatcher} |
383 */ | 394 */ |
384 let defaultMatcher = new CombinedMatcher(); | 395 let defaultMatcher = new CombinedMatcher(); |
385 | 396 |
386 exports.defaultMatcher = defaultMatcher; | 397 exports.defaultMatcher = defaultMatcher; |
LEFT | RIGHT |