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 {WhitelistFilter} = require("./filterClasses"); |
26 | 26 |
27 /** | 27 /** |
| 28 * Regular expression for matching a keyword in a filter. |
| 29 * @type {RegExp} |
| 30 */ |
| 31 const keywordRegExp = /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/; |
| 32 |
| 33 /** |
| 34 * Regular expression for matching all keywords in a filter. |
| 35 * @type {RegExp} |
| 36 */ |
| 37 const allKeywordsRegExp = new RegExp(keywordRegExp, "g"); |
| 38 |
| 39 /** |
| 40 * Checks whether a particular filter is slow. |
| 41 * @param {RegExpFilter} filter |
| 42 * @returns {boolean} |
| 43 */ |
| 44 function isSlowFilter(filter) |
| 45 { |
| 46 return !filter.pattern || !keywordRegExp.test(filter.pattern); |
| 47 } |
| 48 |
| 49 exports.isSlowFilter = isSlowFilter; |
| 50 |
| 51 /** |
28 * Blacklist/whitelist filter matching | 52 * Blacklist/whitelist filter matching |
29 */ | 53 */ |
30 class Matcher | 54 class Matcher |
31 { | 55 { |
32 constructor() | 56 constructor() |
33 { | 57 { |
34 /** | 58 /** |
35 * Lookup table for filters by their associated keyword | 59 * Lookup table for filters by their associated keyword |
36 * @type {Map.<string,(Filter|Set.<Filter>)>} | 60 * @type {Map.<string,(Filter|Set.<Filter>)>} |
37 */ | 61 */ |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 * @param {Filter} filter | 124 * @param {Filter} filter |
101 * @returns {string} keyword or an empty string if no keyword could be found | 125 * @returns {string} keyword or an empty string if no keyword could be found |
102 */ | 126 */ |
103 findKeyword(filter) | 127 findKeyword(filter) |
104 { | 128 { |
105 let result = ""; | 129 let result = ""; |
106 let {pattern} = filter; | 130 let {pattern} = filter; |
107 if (pattern == null) | 131 if (pattern == null) |
108 return result; | 132 return result; |
109 | 133 |
110 let candidates = pattern.toLowerCase().match( | 134 let candidates = pattern.toLowerCase().match(allKeywordsRegExp); |
111 /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/g | |
112 ); | |
113 if (!candidates) | 135 if (!candidates) |
114 return result; | 136 return result; |
115 | 137 |
116 let hash = this.filterByKeyword; | 138 let hash = this.filterByKeyword; |
117 let resultCount = 0xFFFFFF; | 139 let resultCount = 0xFFFFFF; |
118 let resultLength = 0; | 140 let resultLength = 0; |
119 for (let i = 0, l = candidates.length; i < l; i++) | 141 for (let i = 0, l = candidates.length; i < l; i++) |
120 { | 142 { |
121 let candidate = candidates[i].substr(1); | 143 let candidate = candidates[i].substr(1); |
122 let filters = hash.get(candidate); | 144 let filters = hash.get(candidate); |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 | 367 |
346 exports.CombinedMatcher = CombinedMatcher; | 368 exports.CombinedMatcher = CombinedMatcher; |
347 | 369 |
348 /** | 370 /** |
349 * Shared {@link CombinedMatcher} instance that should usually be used. | 371 * Shared {@link CombinedMatcher} instance that should usually be used. |
350 * @type {CombinedMatcher} | 372 * @type {CombinedMatcher} |
351 */ | 373 */ |
352 let defaultMatcher = new CombinedMatcher(); | 374 let defaultMatcher = new CombinedMatcher(); |
353 | 375 |
354 exports.defaultMatcher = defaultMatcher; | 376 exports.defaultMatcher = defaultMatcher; |
LEFT | RIGHT |