| Left: | ||
| Right: |
| 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 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 | 50 |
| 51 /** | 51 /** |
| 52 * Blacklist/whitelist filter matching | 52 * Blacklist/whitelist filter matching |
| 53 */ | 53 */ |
| 54 class Matcher | 54 class Matcher |
| 55 { | 55 { |
| 56 constructor() | 56 constructor() |
| 57 { | 57 { |
| 58 /** | 58 /** |
| 59 * Lookup table for filters by their associated keyword | 59 * Lookup table for filters by their associated keyword |
| 60 * @private | |
|
Manish Jethani
2018/10/21 12:12:19
I've already started using @private/@protected els
Jon Sonesen
2018/10/23 01:28:01
Acknowledged.
| |
| 60 * @type {Map.<string,(Filter|Set.<Filter>)>} | 61 * @type {Map.<string,(Filter|Set.<Filter>)>} |
| 61 */ | 62 */ |
| 62 this.filterByKeyword = new Map(); | 63 this._filterByKeyword = new Map(); |
| 63 } | 64 } |
| 64 | 65 |
| 65 /** | 66 /** |
| 66 * Removes all known filters | 67 * Removes all known filters |
| 67 */ | 68 */ |
| 68 clear() | 69 clear() |
| 69 { | 70 { |
| 70 this.filterByKeyword.clear(); | 71 this._filterByKeyword.clear(); |
| 71 } | 72 } |
| 72 | 73 |
| 73 /** | 74 /** |
| 74 * Adds a filter to the matcher | 75 * Adds a filter to the matcher |
| 75 * @param {RegExpFilter} filter | 76 * @param {RegExpFilter} filter |
| 76 */ | 77 */ |
| 77 add(filter) | 78 add(filter) |
| 78 { | 79 { |
| 79 // Look for a suitable keyword | 80 // Look for a suitable keyword |
| 80 let keyword = this.findKeyword(filter); | 81 let keyword = this.findKeyword(filter); |
| 81 let set = this.filterByKeyword.get(keyword); | 82 let set = this._filterByKeyword.get(keyword); |
| 82 if (typeof set == "undefined") | 83 if (typeof set == "undefined") |
| 83 { | 84 { |
| 84 this.filterByKeyword.set(keyword, filter); | 85 this._filterByKeyword.set(keyword, filter); |
| 85 } | 86 } |
| 86 else if (set.size == 1) | 87 else if (set.size == 1) |
| 87 { | 88 { |
| 88 if (filter != set) | 89 if (filter != set) |
| 89 this.filterByKeyword.set(keyword, new Set([set, filter])); | 90 this._filterByKeyword.set(keyword, new Set([set, filter])); |
| 90 } | 91 } |
| 91 else | 92 else |
| 92 { | 93 { |
| 93 set.add(filter); | 94 set.add(filter); |
| 94 } | 95 } |
| 95 } | 96 } |
| 96 | 97 |
| 97 /** | 98 /** |
| 98 * Removes a filter from the matcher | 99 * Removes a filter from the matcher |
| 99 * @param {RegExpFilter} filter | 100 * @param {RegExpFilter} filter |
| 100 */ | 101 */ |
| 101 remove(filter) | 102 remove(filter) |
| 102 { | 103 { |
| 103 let keyword = this.findKeyword(filter); | 104 let keyword = this.findKeyword(filter); |
| 104 let set = this.filterByKeyword.get(keyword); | 105 let set = this._filterByKeyword.get(keyword); |
| 105 if (typeof set == "undefined") | 106 if (typeof set == "undefined") |
| 106 return; | 107 return; |
| 107 | 108 |
| 108 if (set.size == 1) | 109 if (set.size == 1) |
| 109 { | 110 { |
| 110 if (filter == set) | 111 if (filter == set) |
| 111 this.filterByKeyword.delete(keyword); | 112 this._filterByKeyword.delete(keyword); |
| 112 } | 113 } |
| 113 else | 114 else |
| 114 { | 115 { |
| 115 set.delete(filter); | 116 set.delete(filter); |
| 116 | 117 |
| 117 if (set.size == 1) | 118 if (set.size == 1) |
| 118 this.filterByKeyword.set(keyword, [...set][0]); | 119 this._filterByKeyword.set(keyword, [...set][0]); |
| 119 } | 120 } |
| 120 } | 121 } |
| 121 | 122 |
| 122 /** | 123 /** |
| 123 * Chooses a keyword to be associated with the filter | 124 * Chooses a keyword to be associated with the filter |
| 125 * @protected | |
|
Manish Jethani
2018/10/21 12:12:19
Same here.
Jon Sonesen
2018/10/23 01:28:04
Acknowledged.
| |
| 124 * @param {Filter} filter | 126 * @param {Filter} filter |
| 125 * @returns {string} keyword or an empty string if no keyword could be found | 127 * @returns {string} keyword or an empty string if no keyword could be found |
| 126 */ | 128 */ |
| 127 findKeyword(filter) | 129 findKeyword(filter) |
| 128 { | 130 { |
| 129 let result = ""; | 131 let result = ""; |
| 130 let {pattern} = filter; | 132 let {pattern} = filter; |
| 131 if (pattern == null) | 133 if (pattern == null) |
| 132 return result; | 134 return result; |
| 133 | 135 |
| 134 let candidates = pattern.toLowerCase().match(allKeywordsRegExp); | 136 let candidates = pattern.toLowerCase().match(allKeywordsRegExp); |
| 135 if (!candidates) | 137 if (!candidates) |
| 136 return result; | 138 return result; |
| 137 | 139 |
| 138 let hash = this.filterByKeyword; | 140 let hash = this._filterByKeyword; |
| 139 let resultCount = 0xFFFFFF; | 141 let resultCount = 0xFFFFFF; |
| 140 let resultLength = 0; | 142 let resultLength = 0; |
| 141 for (let i = 0, l = candidates.length; i < l; i++) | 143 for (let i = 0, l = candidates.length; i < l; i++) |
| 142 { | 144 { |
| 143 let candidate = candidates[i].substr(1); | 145 let candidate = candidates[i].substr(1); |
| 144 let filters = hash.get(candidate); | 146 let filters = hash.get(candidate); |
| 145 let count = typeof filters != "undefined" ? filters.size : 0; | 147 let count = typeof filters != "undefined" ? filters.size : 0; |
| 146 if (count < resultCount || | 148 if (count < resultCount || |
| 147 (count == resultCount && candidate.length > resultLength)) | 149 (count == resultCount && candidate.length > resultLength)) |
| 148 { | 150 { |
| 149 result = candidate; | 151 result = candidate; |
| 150 resultCount = count; | 152 resultCount = count; |
| 151 resultLength = candidate.length; | 153 resultLength = candidate.length; |
| 152 } | 154 } |
| 153 } | 155 } |
| 154 return result; | 156 return result; |
| 155 } | 157 } |
| 156 | 158 |
| 157 /** | 159 /** |
| 158 * Checks whether the entries for a particular keyword match a URL | 160 * Checks whether the entries for a particular keyword match a URL |
| 159 * @param {string} keyword | 161 * @param {string} keyword |
| 160 * @param {string} location | 162 * @param {string} location |
| 161 * @param {number} typeMask | 163 * @param {number} typeMask |
| 162 * @param {string} [docDomain] | 164 * @param {string} [docDomain] |
| 163 * @param {boolean} [thirdParty] | 165 * @param {boolean} [thirdParty] |
| 164 * @param {string} [sitekey] | 166 * @param {string} [sitekey] |
| 165 * @param {boolean} [specificOnly] | 167 * @param {boolean} [specificOnly] |
| 166 * @returns {?Filter} | 168 * @returns {?Filter} |
| 167 */ | 169 */ |
| 168 _checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey, | 170 _checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey, |
|
Manish Jethani
2018/10/21 12:52:04
About this function, I think we can make it "prote
Jon Sonesen
2018/10/23 01:28:02
Acknowledged.
| |
| 169 specificOnly) | 171 specificOnly) |
| 170 { | 172 { |
| 171 let set = this.filterByKeyword.get(keyword); | 173 let set = this._filterByKeyword.get(keyword); |
| 172 if (typeof set == "undefined") | 174 if (typeof set == "undefined") |
| 173 return null; | 175 return null; |
| 174 | 176 |
| 175 for (let filter of set) | 177 for (let filter of set) |
| 176 { | 178 { |
| 177 if (specificOnly && filter.isGeneric() && | 179 if (specificOnly && filter.isGeneric() && |
| 178 !(filter instanceof WhitelistFilter)) | 180 !(filter instanceof WhitelistFilter)) |
| 179 continue; | 181 continue; |
| 180 | 182 |
| 181 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey)) | 183 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey)) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 /** | 247 /** |
| 246 * Matcher for exception rules. | 248 * Matcher for exception rules. |
| 247 * @type {Matcher} | 249 * @type {Matcher} |
| 248 */ | 250 */ |
| 249 this.whitelist = new Matcher(); | 251 this.whitelist = new Matcher(); |
| 250 | 252 |
| 251 /** | 253 /** |
| 252 * Lookup table of previous {@link Matcher#matchesAny} results | 254 * Lookup table of previous {@link Matcher#matchesAny} results |
| 253 * @type {Map.<string,Filter>} | 255 * @type {Map.<string,Filter>} |
| 254 */ | 256 */ |
| 255 this.resultCache = new Map(); | 257 this.resultCache = new Map(); |
|
Manish Jethani
2018/10/21 12:52:04
Let's do this entire file while we're at it. I don
Manish Jethani
2018/10/22 22:57:50
FYI we're having a discussion [1] about making bla
Jon Sonesen
2018/10/23 01:28:02
Acknowledged. In the new patch set, I made them pr
Manish Jethani
2018/10/23 03:10:14
I don't think that has anything to do with the cha
Jon Sonesen
2018/10/23 03:16:42
It does, have you tested making them private? I ca
Manish Jethani
2018/10/23 03:23:17
You'll have to modify the tests of course, so they
Manish Jethani
2018/10/23 03:28:42
Please try just changing this line:
https://hg.ad
Jon Sonesen
2018/10/23 04:08:03
Yeah I am the worst, thanks for your help and pati
Manish Jethani
2018/10/23 10:38:26
Not at all, it happens to me too :)
| |
| 256 } | 258 } |
| 257 | 259 |
| 258 /** | 260 /** |
| 259 * @see Matcher#clear | 261 * @see Matcher#clear |
| 260 */ | 262 */ |
| 261 clear() | 263 clear() |
| 262 { | 264 { |
| 263 this.blacklist.clear(); | 265 this.blacklist.clear(); |
| 264 this.whitelist.clear(); | 266 this.whitelist.clear(); |
| 265 this.resultCache.clear(); | 267 this.resultCache.clear(); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 291 this.blacklist.remove(filter); | 293 this.blacklist.remove(filter); |
| 292 | 294 |
| 293 this.resultCache.clear(); | 295 this.resultCache.clear(); |
| 294 } | 296 } |
| 295 | 297 |
| 296 /** | 298 /** |
| 297 * @see Matcher#findKeyword | 299 * @see Matcher#findKeyword |
| 298 * @param {Filter} filter | 300 * @param {Filter} filter |
| 299 * @returns {string} keyword | 301 * @returns {string} keyword |
| 300 */ | 302 */ |
| 301 findKeyword(filter) | 303 findKeyword(filter) |
|
Manish Jethani
2018/10/21 12:52:03
If we're marking fineKeyword @protected in Matcher
Jon Sonesen
2018/10/23 01:28:04
Done.
| |
| 302 { | 304 { |
| 303 if (filter instanceof WhitelistFilter) | 305 if (filter instanceof WhitelistFilter) |
| 304 return this.whitelist.findKeyword(filter); | 306 return this.whitelist.findKeyword(filter); |
| 305 return this.blacklist.findKeyword(filter); | 307 return this.blacklist.findKeyword(filter); |
| 306 } | 308 } |
| 307 | 309 |
| 308 /** | 310 /** |
| 309 * Optimized filter matching testing both whitelist and blacklist matchers | 311 * Optimized filter matching testing both whitelist and blacklist matchers |
| 310 * simultaneously. For parameters see | 312 * simultaneously. For parameters see |
| 311 {@link Matcher#matchesAny Matcher.matchesAny()}. | 313 {@link Matcher#matchesAny Matcher.matchesAny()}. |
| 312 * @see Matcher#matchesAny | 314 * @see Matcher#matchesAny |
| 313 * @inheritdoc | 315 * @inheritdoc |
| 314 */ | 316 */ |
| 315 matchesAnyInternal(location, typeMask, docDomain, thirdParty, sitekey, | 317 matchesAnyInternal(location, typeMask, docDomain, thirdParty, sitekey, |
|
Manish Jethani
2018/10/21 12:52:03
Let's make this private (underscore and @private).
Jon Sonesen
2018/10/23 01:28:02
Acknowledged.
| |
| 316 specificOnly) | 318 specificOnly) |
| 317 { | 319 { |
| 318 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); | 320 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); |
| 319 if (candidates === null) | 321 if (candidates === null) |
| 320 candidates = []; | 322 candidates = []; |
| 321 candidates.push(""); | 323 candidates.push(""); |
| 322 | 324 |
| 323 let blacklistHit = null; | 325 let blacklistHit = null; |
| 324 for (let i = 0, l = candidates.length; i < l; i++) | 326 for (let i = 0, l = candidates.length; i < l; i++) |
| 325 { | 327 { |
| 326 let substr = candidates[i]; | 328 let substr = candidates[i]; |
| 327 let result = this.whitelist._checkEntryMatch( | 329 let result = this.whitelist._checkEntryMatch( |
| 328 substr, location, typeMask, docDomain, thirdParty, sitekey | 330 substr, location, typeMask, docDomain, thirdParty, sitekey |
| 329 ); | 331 ); |
| 330 if (result) | 332 if (result) |
| 331 return result; | 333 return result; |
| 332 if (blacklistHit === null) | 334 if (blacklistHit === null) |
| 333 { | 335 { |
| 334 blacklistHit = this.blacklist._checkEntryMatch( | 336 blacklistHit = this.blacklist._checkEntryMatch( |
| 335 substr, location, typeMask, docDomain, thirdParty, sitekey, | 337 substr, location, typeMask, docDomain, thirdParty, sitekey, |
| 336 specificOnly | 338 specificOnly |
| 337 ); | 339 ); |
| 338 } | 340 } |
| 339 } | 341 } |
| 340 return blacklistHit; | 342 return blacklistHit; |
| 341 } | 343 } |
| 342 | 344 |
| 343 /** | 345 /** |
| 344 * @see Matcher#matchesAny | 346 * @see Matcher#matchesAny |
| 345 * @inheritdoc | 347 * @inheritdoc |
|
Manish Jethani
2018/10/21 12:52:04
There are two instances of @inheritdoc in this fil
Jon Sonesen
2018/10/23 01:28:01
Doing this makes eslint throw errors for the valid
Manish Jethani
2018/10/23 03:10:14
Oh yeah, I think you had mentioned this earlier as
| |
| 346 */ | 348 */ |
| 347 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) | 349 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) |
| 348 { | 350 { |
| 349 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + | 351 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + |
| 350 " " + sitekey + " " + specificOnly; | 352 " " + sitekey + " " + specificOnly; |
| 351 | 353 |
| 352 let result = this.resultCache.get(key); | 354 let result = this.resultCache.get(key); |
| 353 if (typeof result != "undefined") | 355 if (typeof result != "undefined") |
| 354 return result; | 356 return result; |
| 355 | 357 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 367 | 369 |
| 368 exports.CombinedMatcher = CombinedMatcher; | 370 exports.CombinedMatcher = CombinedMatcher; |
| 369 | 371 |
| 370 /** | 372 /** |
| 371 * Shared {@link CombinedMatcher} instance that should usually be used. | 373 * Shared {@link CombinedMatcher} instance that should usually be used. |
| 372 * @type {CombinedMatcher} | 374 * @type {CombinedMatcher} |
| 373 */ | 375 */ |
| 374 let defaultMatcher = new CombinedMatcher(); | 376 let defaultMatcher = new CombinedMatcher(); |
| 375 | 377 |
| 376 exports.defaultMatcher = defaultMatcher; | 378 exports.defaultMatcher = defaultMatcher; |
| OLD | NEW |