| Left: | ||
| Right: |
| 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> |
|
Manish Jethani
2018/09/03 18:46:49
Let's make this `<code>null</code>` as well while
Jon Sonesen
2018/09/05 14:05:27
Done.
| |
| 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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 */ | 268 */ |
| 277 clear() | 269 clear() |
| 278 { | 270 { |
| 279 this.blacklist.clear(); | 271 this.blacklist.clear(); |
| 280 this.whitelist.clear(); | 272 this.whitelist.clear(); |
| 281 this.resultCache.clear(); | 273 this.resultCache.clear(); |
| 282 } | 274 } |
| 283 | 275 |
| 284 /** | 276 /** |
| 285 * @see Matcher#add | 277 * @see Matcher#add |
| 286 * @param {Filter} filter | 278 * @param {Filter} filter |
|
Manish Jethani
2018/09/03 18:46:49
When we use @see, we're not being consistent in ad
Jon Sonesen
2018/09/05 14:05:27
The reason for this is that matchesAny does not ha
Manish Jethani
2018/09/05 19:33:57
Oh damn. OK, no problem then.
| |
| 287 */ | 279 */ |
| 288 add(filter) | 280 add(filter) |
| 289 { | 281 { |
| 290 if (filter instanceof WhitelistFilter) | 282 if (filter instanceof WhitelistFilter) |
| 291 this.whitelist.add(filter); | 283 this.whitelist.add(filter); |
| 292 else | 284 else |
| 293 this.blacklist.add(filter); | 285 this.blacklist.add(filter); |
| 294 | 286 |
| 295 this.resultCache.clear(); | 287 this.resultCache.clear(); |
| 296 } | 288 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 358 if (matcher.hasFilter(filter)) | 350 if (matcher.hasFilter(filter)) |
| 359 return !matcher.getKeywordForFilter(filter); | 351 return !matcher.getKeywordForFilter(filter); |
| 360 return !matcher.findKeyword(filter); | 352 return !matcher.findKeyword(filter); |
| 361 } | 353 } |
| 362 | 354 |
| 363 /** | 355 /** |
| 364 * Optimized filter matching testing both whitelist and blacklist matchers | 356 * Optimized filter matching testing both whitelist and blacklist matchers |
| 365 * simultaneously. For parameters see | 357 * simultaneously. For parameters see |
| 366 {@link Matcher#matchesAny Matcher.matchesAny()}. | 358 {@link Matcher#matchesAny Matcher.matchesAny()}. |
| 367 * @see Matcher#matchesAny | 359 * @see Matcher#matchesAny |
| 368 * @inheritdoc | 360 * @inheritdoc |
|
Manish Jethani
2018/09/03 18:46:49
I don't think @inheritdoc makes sense here (also b
Jon Sonesen
2018/09/05 14:05:27
Similarly to above, eslint complains here without
| |
| 369 */ | 361 */ |
| 370 matchesAnyInternal(location, typeMask, docDomain, thirdParty, sitekey, | 362 matchesAnyInternal(location, typeMask, docDomain, thirdParty, sitekey, |
| 371 specificOnly) | 363 specificOnly) |
| 372 { | 364 { |
| 373 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); | 365 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); |
| 374 if (candidates === null) | 366 if (candidates === null) |
| 375 candidates = []; | 367 candidates = []; |
| 376 candidates.push(""); | 368 candidates.push(""); |
| 377 | 369 |
| 378 let blacklistHit = null; | 370 let blacklistHit = null; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 419 return result; | 411 return result; |
| 420 } | 412 } |
| 421 } | 413 } |
| 422 | 414 |
| 423 exports.CombinedMatcher = CombinedMatcher; | 415 exports.CombinedMatcher = CombinedMatcher; |
| 424 | 416 |
| 425 /** | 417 /** |
| 426 * Shared {@link CombinedMatcher} instance that should usually be used. | 418 * Shared {@link CombinedMatcher} instance that should usually be used. |
| 427 * @type {CombinedMatcher} | 419 * @type {CombinedMatcher} |
| 428 */ | 420 */ |
| 429 exports.defaultMatcher = new CombinedMatcher(); | 421 let defaultMatcher = new CombinedMatcher(); |
|
Manish Jethani
2018/09/03 18:46:49
I think we should follow the convention of first d
Jon Sonesen
2018/09/05 14:05:27
Done.
| |
| 422 | |
| 423 exports.defaultMatcher = defaultMatcher; | |
| LEFT | RIGHT |