OLD | NEW |
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 Loading... |
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 the type mask includes no types other than whitelist-only types, we |
| 337 // can skip the blacklist. |
| 338 if ((typeMask & ~WHITELIST_ONLY_TYPES) != 0) |
325 { | 339 { |
326 let substr = candidates[i]; | 340 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 { | 341 { |
334 blacklistHit = this.blacklist._checkEntryMatch( | 342 blacklistHit = this.blacklist._checkEntryMatch(candidates[i], location, |
335 substr, location, typeMask, docDomain, thirdParty, sitekey, | 343 typeMask, docDomain, |
336 specificOnly | 344 thirdParty, sitekey, |
337 ); | 345 specificOnly); |
338 } | 346 } |
339 } | 347 } |
340 return blacklistHit; | 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); |
| 358 } |
| 359 } |
| 360 |
| 361 return whitelistHit || blacklistHit; |
341 } | 362 } |
342 | 363 |
343 /** | 364 /** |
344 * @see Matcher#matchesAny | 365 * @see Matcher#matchesAny |
345 * @inheritdoc | 366 * @inheritdoc |
346 */ | 367 */ |
347 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) | 368 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) |
348 { | 369 { |
349 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + | 370 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + |
350 " " + sitekey + " " + specificOnly; | 371 " " + sitekey + " " + specificOnly; |
(...skipping 16 matching lines...) Expand all Loading... |
367 | 388 |
368 exports.CombinedMatcher = CombinedMatcher; | 389 exports.CombinedMatcher = CombinedMatcher; |
369 | 390 |
370 /** | 391 /** |
371 * Shared {@link CombinedMatcher} instance that should usually be used. | 392 * Shared {@link CombinedMatcher} instance that should usually be used. |
372 * @type {CombinedMatcher} | 393 * @type {CombinedMatcher} |
373 */ | 394 */ |
374 let defaultMatcher = new CombinedMatcher(); | 395 let defaultMatcher = new CombinedMatcher(); |
375 | 396 |
376 exports.defaultMatcher = defaultMatcher; | 397 exports.defaultMatcher = defaultMatcher; |
OLD | NEW |