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 let {blacklist, whitelist} = this; |
| 337 |
| 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 = 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 (blacklistHit || (typeMask & WHITELIST_ONLY_TYPES) != 0) |
| 350 { |
| 351 for (let i = 0, l = candidates.length; !whitelistHit && i < l; i++) |
| 352 { |
| 353 whitelistHit = whitelist._checkEntryMatch(candidates[i], location, |
| 354 typeMask, docDomain, |
| 355 thirdParty, sitekey); |
| 356 } |
| 357 } |
| 358 |
| 359 return whitelistHit || blacklistHit; |
341 } | 360 } |
342 | 361 |
343 /** | 362 /** |
344 * @see Matcher#matchesAny | 363 * @see Matcher#matchesAny |
345 * @inheritdoc | 364 * @inheritdoc |
346 */ | 365 */ |
347 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) | 366 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) |
348 { | 367 { |
349 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + | 368 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + |
350 " " + sitekey + " " + specificOnly; | 369 " " + sitekey + " " + specificOnly; |
(...skipping 16 matching lines...) Expand all Loading... |
367 | 386 |
368 exports.CombinedMatcher = CombinedMatcher; | 387 exports.CombinedMatcher = CombinedMatcher; |
369 | 388 |
370 /** | 389 /** |
371 * Shared {@link CombinedMatcher} instance that should usually be used. | 390 * Shared {@link CombinedMatcher} instance that should usually be used. |
372 * @type {CombinedMatcher} | 391 * @type {CombinedMatcher} |
373 */ | 392 */ |
374 let defaultMatcher = new CombinedMatcher(); | 393 let defaultMatcher = new CombinedMatcher(); |
375 | 394 |
376 exports.defaultMatcher = defaultMatcher; | 395 exports.defaultMatcher = defaultMatcher; |
OLD | NEW |