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 {Filter, WhitelistFilter} = require("./filterClasses"); | 25 const {WhitelistFilter} = require("./filterClasses"); |
26 | 26 |
27 /** | 27 /** |
28 * Blacklist/whitelist filter matching | 28 * Blacklist/whitelist filter matching |
29 */ | 29 */ |
30 class Matcher | 30 class Matcher |
31 { | 31 { |
32 constructor() | 32 constructor() |
33 { | 33 { |
34 /** | 34 /** |
35 * Lookup table for filters by their associated keyword | 35 * Lookup table for filters by their associated keyword |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 } | 102 } |
103 | 103 |
104 /** | 104 /** |
105 * Chooses a keyword to be associated with the filter | 105 * Chooses a keyword to be associated with the filter |
106 * @param {Filter} filter | 106 * @param {Filter} filter |
107 * @returns {string} keyword or an empty string if no keyword could be found | 107 * @returns {string} keyword or an empty string if no keyword could be found |
108 */ | 108 */ |
109 findKeyword(filter) | 109 findKeyword(filter) |
110 { | 110 { |
111 let result = ""; | 111 let result = ""; |
112 let {text} = filter; | 112 let {pattern} = filter; |
113 if (Filter.regexpRegExp.test(text)) | 113 if (pattern == null) |
114 return result; | 114 return result; |
115 | 115 |
116 // Remove options | 116 let candidates = pattern.toLowerCase().match( |
117 let match = Filter.optionsRegExp.exec(text); | |
118 if (match) | |
119 text = match.input.substr(0, match.index); | |
120 | |
121 // Remove whitelist marker | |
122 if (text[0] == "@" && text[1] == "@") | |
123 text = text.substr(2); | |
124 | |
125 let candidates = text.toLowerCase().match( | |
126 /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/g | 117 /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/g |
127 ); | 118 ); |
128 if (!candidates) | 119 if (!candidates) |
129 return result; | 120 return result; |
130 | 121 |
131 let hash = this.filterByKeyword; | 122 let hash = this.filterByKeyword; |
132 let resultCount = 0xFFFFFF; | 123 let resultCount = 0xFFFFFF; |
133 let resultLength = 0; | 124 let resultLength = 0; |
134 for (let i = 0, l = candidates.length; i < l; i++) | 125 for (let i = 0, l = candidates.length; i < l; i++) |
135 { | 126 { |
(...skipping 15 matching lines...) Expand all Loading... |
151 * Checks whether a particular filter is being matched against. | 142 * Checks whether a particular filter is being matched against. |
152 * @param {RegExpFilter} filter | 143 * @param {RegExpFilter} filter |
153 * @returns {boolean} | 144 * @returns {boolean} |
154 */ | 145 */ |
155 hasFilter(filter) | 146 hasFilter(filter) |
156 { | 147 { |
157 return this.keywordByFilter.has(filter); | 148 return this.keywordByFilter.has(filter); |
158 } | 149 } |
159 | 150 |
160 /** | 151 /** |
161 * Returns the keyword used for a filter, null for unknown filters. | 152 * Returns the keyword used for a filter, <code>null</code> |
| 153 * for unknown filters. |
162 * @param {RegExpFilter} filter | 154 * @param {RegExpFilter} filter |
163 * @returns {?string} | 155 * @returns {?string} |
164 */ | 156 */ |
165 getKeywordForFilter(filter) | 157 getKeywordForFilter(filter) |
166 { | 158 { |
167 let keyword = this.keywordByFilter.get(filter); | 159 let keyword = this.keywordByFilter.get(filter); |
168 return typeof keyword != "undefined" ? keyword : null; | 160 return typeof keyword != "undefined" ? keyword : null; |
169 } | 161 } |
170 | 162 |
171 /** | 163 /** |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 return result; | 223 return result; |
232 } | 224 } |
233 | 225 |
234 return null; | 226 return null; |
235 } | 227 } |
236 } | 228 } |
237 | 229 |
238 exports.Matcher = Matcher; | 230 exports.Matcher = Matcher; |
239 | 231 |
240 | 232 |
241 | 233 /** |
| 234 * Combines a matcher for blocking and exception rules, automatically sorts |
| 235 * rules into two {@link Matcher} instances. |
| 236 */ |
242 class CombinedMatcher | 237 class CombinedMatcher |
243 { | 238 { |
244 /** | |
245 * Combines a matcher for blocking and exception rules, automatically sorts | |
246 * rules into two Matcher instances. | |
247 * @augments Matcher | |
248 */ | |
249 constructor() | 239 constructor() |
250 { | 240 { |
251 /** | 241 /** |
252 * Maximal number of matching cache entries to be kept | 242 * Maximal number of matching cache entries to be kept |
253 * @type {number} | 243 * @type {number} |
254 */ | 244 */ |
255 this.maxCacheEntries = 1000; | 245 this.maxCacheEntries = 1000; |
256 | 246 |
257 /** | 247 /** |
258 * Matcher for blocking rules. | 248 * Matcher for blocking rules. |
259 * @type {Matcher} | 249 * @type {Matcher} |
260 */ | 250 */ |
261 this.blacklist = new Matcher(); | 251 this.blacklist = new Matcher(); |
262 | 252 |
263 /** | 253 /** |
264 * Matcher for exception rules. | 254 * Matcher for exception rules. |
265 * @type {Matcher} | 255 * @type {Matcher} |
266 */ | 256 */ |
267 this.whitelist = new Matcher(); | 257 this.whitelist = new Matcher(); |
268 | 258 |
269 /** | 259 /** |
270 * Lookup table of previous matchesAny results | 260 * Lookup table of previous {@link Matcher#matchesAny} results |
271 * @type {Map.<string,Filter>} | 261 * @type {Map.<string,Filter>} |
272 */ | 262 */ |
273 this.resultCache = new Map(); | 263 this.resultCache = new Map(); |
274 } | 264 } |
275 | 265 |
276 /** | 266 /** |
277 * @see Matcher#clear | 267 * @see Matcher#clear |
278 */ | 268 */ |
279 clear() | 269 clear() |
280 { | 270 { |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 | 408 |
419 this.resultCache.set(key, result); | 409 this.resultCache.set(key, result); |
420 | 410 |
421 return result; | 411 return result; |
422 } | 412 } |
423 } | 413 } |
424 | 414 |
425 exports.CombinedMatcher = CombinedMatcher; | 415 exports.CombinedMatcher = CombinedMatcher; |
426 | 416 |
427 /** | 417 /** |
428 * Shared CombinedMatcher instance that should usually be used. | 418 * Shared {@link CombinedMatcher} instance that should usually be used. |
429 * @type {CombinedMatcher} | 419 * @type {CombinedMatcher} |
430 */ | 420 */ |
431 exports.defaultMatcher = new CombinedMatcher(); | 421 let defaultMatcher = new CombinedMatcher(); |
| 422 |
| 423 exports.defaultMatcher = defaultMatcher; |
LEFT | RIGHT |