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 |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 * @param {string} location | 172 * @param {string} location |
173 * @param {number} typeMask | 173 * @param {number} typeMask |
174 * @param {string} [docDomain] | 174 * @param {string} [docDomain] |
175 * @param {boolean} [thirdParty] | 175 * @param {boolean} [thirdParty] |
176 * @param {string} [sitekey] | 176 * @param {string} [sitekey] |
177 * @param {boolean} [specificOnly] | 177 * @param {boolean} [specificOnly] |
178 * @returns {?Filter} | 178 * @returns {?Filter} |
179 * @protected | 179 * @protected |
180 */ | 180 */ |
181 checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey, | 181 checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey, |
182 specificOnly) | 182 specificOnly) |
183 { | 183 { |
184 let set = this._filterByKeyword.get(keyword); | 184 let set = this._filterByKeyword.get(keyword); |
185 if (typeof set == "undefined") | 185 if (typeof set == "undefined") |
186 return null; | 186 return null; |
187 | 187 |
188 for (let filter of set) | 188 for (let filter of set) |
189 { | 189 { |
190 if (specificOnly && filter.isGeneric() && | 190 if (specificOnly && filter.isGeneric() && |
191 !(filter instanceof WhitelistFilter)) | 191 !(filter instanceof WhitelistFilter)) |
192 continue; | 192 continue; |
(...skipping 23 matching lines...) Expand all Loading... |
216 */ | 216 */ |
217 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) | 217 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) |
218 { | 218 { |
219 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); | 219 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); |
220 if (candidates === null) | 220 if (candidates === null) |
221 candidates = []; | 221 candidates = []; |
222 candidates.push(""); | 222 candidates.push(""); |
223 for (let i = 0, l = candidates.length; i < l; i++) | 223 for (let i = 0, l = candidates.length; i < l; i++) |
224 { | 224 { |
225 let result = this.checkEntryMatch(candidates[i], location, typeMask, | 225 let result = this.checkEntryMatch(candidates[i], location, typeMask, |
226 docDomain, thirdParty, sitekey, | 226 docDomain, thirdParty, sitekey, |
227 specificOnly); | 227 specificOnly); |
228 if (result) | 228 if (result) |
229 return result; | 229 return result; |
230 } | 230 } |
231 | 231 |
232 return null; | 232 return null; |
233 } | 233 } |
234 } | 234 } |
235 | 235 |
236 exports.Matcher = Matcher; | 236 exports.Matcher = Matcher; |
237 | 237 |
238 /** | 238 /** |
239 * Combines a matcher for blocking and exception rules, automatically sorts | 239 * Combines a matcher for blocking and exception rules, automatically sorts |
240 * rules into two {@link Matcher} instances. | 240 * rules into two {@link Matcher} instances. |
241 */ | 241 */ |
242 class CombinedMatcher | 242 class CombinedMatcher |
243 { | 243 { |
244 constructor() | 244 constructor() |
245 { | 245 { |
246 /** | 246 /** |
247 * Maximal number of matching cache entries to be kept | 247 * Maximal number of matching cache entries to be kept |
248 * @type {number} | 248 * @type {number} |
249 */ | 249 */ |
250 this.maxCacheEntries = 1000; | 250 this.maxCacheEntries = 1000; |
251 | 251 |
252 /** | 252 /** |
253 * Matcher for blocking rules. | 253 * Matcher for blocking rules. |
254 * @type {Matcher} | 254 * @type {Matcher} |
| 255 * @private |
255 */ | 256 */ |
256 this._blacklist = new Matcher(); | 257 this._blacklist = new Matcher(); |
257 | 258 |
258 /** | 259 /** |
259 * Matcher for exception rules. | 260 * Matcher for exception rules. |
260 * @type {Matcher} | 261 * @type {Matcher} |
| 262 * @private |
261 */ | 263 */ |
262 this._whitelist = new Matcher(); | 264 this._whitelist = new Matcher(); |
263 | 265 |
264 /** | 266 /** |
265 * Lookup table of previous {@link Matcher#matchesAny} results | 267 * Lookup table of previous {@link Matcher#matchesAny} results |
266 * @type {Map.<string,Filter>} | 268 * @type {Map.<string,Filter>} |
| 269 * @private |
267 */ | 270 */ |
268 this._resultCache = new Map(); | 271 this._resultCache = new Map(); |
269 } | 272 } |
270 | 273 |
271 /** | 274 /** |
272 * @see Matcher#clear | 275 * @see Matcher#clear |
273 */ | 276 */ |
274 clear() | 277 clear() |
275 { | 278 { |
276 this._blacklist.clear(); | 279 this._blacklist.clear(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 324 |
322 /** | 325 /** |
323 * Optimized filter matching testing both whitelist and blacklist matchers | 326 * Optimized filter matching testing both whitelist and blacklist matchers |
324 * simultaneously. For parameters see | 327 * simultaneously. For parameters see |
325 {@link Matcher#matchesAny Matcher.matchesAny()}. | 328 {@link Matcher#matchesAny Matcher.matchesAny()}. |
326 * @see Matcher#matchesAny | 329 * @see Matcher#matchesAny |
327 * @inheritdoc | 330 * @inheritdoc |
328 * @private | 331 * @private |
329 */ | 332 */ |
330 _matchesAnyInternal(location, typeMask, docDomain, thirdParty, sitekey, | 333 _matchesAnyInternal(location, typeMask, docDomain, thirdParty, sitekey, |
331 specificOnly) | 334 specificOnly) |
332 { | 335 { |
333 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); | 336 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); |
334 if (candidates === null) | 337 if (candidates === null) |
335 candidates = []; | 338 candidates = []; |
336 candidates.push(""); | 339 candidates.push(""); |
337 | 340 |
338 let whitelistHit = null; | 341 let whitelistHit = null; |
339 let blacklistHit = null; | 342 let blacklistHit = null; |
340 | 343 |
341 // If the type mask includes no types other than whitelist-only types, we | 344 // If the type mask includes no types other than whitelist-only types, we |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) | 376 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) |
374 { | 377 { |
375 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + | 378 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + |
376 " " + sitekey + " " + specificOnly; | 379 " " + sitekey + " " + specificOnly; |
377 | 380 |
378 let result = this._resultCache.get(key); | 381 let result = this._resultCache.get(key); |
379 if (typeof result != "undefined") | 382 if (typeof result != "undefined") |
380 return result; | 383 return result; |
381 | 384 |
382 result = this._matchesAnyInternal(location, typeMask, docDomain, | 385 result = this._matchesAnyInternal(location, typeMask, docDomain, |
383 thirdParty, sitekey, specificOnly); | 386 thirdParty, sitekey, specificOnly); |
384 | 387 |
385 if (this._resultCache.size >= this.maxCacheEntries) | 388 if (this._resultCache.size >= this.maxCacheEntries) |
386 this._resultCache.clear(); | 389 this._resultCache.clear(); |
387 | 390 |
388 this._resultCache.set(key, result); | 391 this._resultCache.set(key, result); |
389 | 392 |
390 return result; | 393 return result; |
391 } | 394 } |
392 } | 395 } |
393 | 396 |
394 exports.CombinedMatcher = CombinedMatcher; | 397 exports.CombinedMatcher = CombinedMatcher; |
395 | 398 |
396 /** | 399 /** |
397 * Shared {@link CombinedMatcher} instance that should usually be used. | 400 * Shared {@link CombinedMatcher} instance that should usually be used. |
398 * @type {CombinedMatcher} | 401 * @type {CombinedMatcher} |
399 */ | 402 */ |
400 let defaultMatcher = new CombinedMatcher(); | 403 let defaultMatcher = new CombinedMatcher(); |
401 | 404 |
402 exports.defaultMatcher = defaultMatcher; | 405 exports.defaultMatcher = defaultMatcher; |
LEFT | RIGHT |