| 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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 exports.isSlowFilter = isSlowFilter; | 144 exports.isSlowFilter = isSlowFilter; |
| 145 | 145 |
| 146 /** | 146 /** |
| 147 * Blacklist/whitelist filter matching | 147 * Blacklist/whitelist filter matching |
| 148 */ | 148 */ |
| 149 class Matcher | 149 class Matcher |
| 150 { | 150 { |
| 151 constructor() | 151 constructor() |
| 152 { | 152 { |
| 153 /** | 153 /** |
| 154 * Lookup table for keywords by their associated filter |
| 155 * @type {Map.<RegExpFilter,string>} |
| 156 * @private |
| 157 */ |
| 158 this._keywordsByFilter = new Map(); |
| 159 |
| 160 /** |
| 154 * Lookup table for simple filters by their associated keyword | 161 * Lookup table for simple filters by their associated keyword |
| 155 * @type {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>} | 162 * @type {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>} |
| 156 * @private | 163 * @private |
| 157 */ | 164 */ |
| 158 this._simpleFiltersByKeyword = new Map(); | 165 this._simpleFiltersByKeyword = new Map(); |
| 159 | 166 |
| 160 /** | 167 /** |
| 161 * Lookup table for complex filters by their associated keyword | 168 * Lookup table for complex filters by their associated keyword |
| 162 * @type {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>} | 169 * @type {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>} |
| 163 * @private | 170 * @private |
| (...skipping 18 matching lines...) Expand all Loading... |
| 182 this._complexFiltersByKeyword.clear(); | 189 this._complexFiltersByKeyword.clear(); |
| 183 this._filterMapsByType.clear(); | 190 this._filterMapsByType.clear(); |
| 184 } | 191 } |
| 185 | 192 |
| 186 /** | 193 /** |
| 187 * Adds a filter to the matcher | 194 * Adds a filter to the matcher |
| 188 * @param {RegExpFilter} filter | 195 * @param {RegExpFilter} filter |
| 189 */ | 196 */ |
| 190 add(filter) | 197 add(filter) |
| 191 { | 198 { |
| 199 if (this._keywordsByFilter.has(filter)) |
| 200 return; |
| 201 |
| 192 // Look for a suitable keyword | 202 // Look for a suitable keyword |
| 193 let keyword = this.findKeyword(filter); | 203 let keyword = this.findKeyword(filter); |
| 194 let locationOnly = filter.isLocationOnly(); | 204 let locationOnly = filter.isLocationOnly(); |
| 195 | 205 |
| 196 addFilterByKeyword(filter, keyword, | 206 addFilterByKeyword(filter, keyword, |
| 197 locationOnly ? this._simpleFiltersByKeyword : | 207 locationOnly ? this._simpleFiltersByKeyword : |
| 198 this._complexFiltersByKeyword); | 208 this._complexFiltersByKeyword); |
| 199 | 209 |
| 210 this._keywordsByFilter.set(filter, keyword); |
| 211 |
| 200 if (locationOnly) | 212 if (locationOnly) |
| 201 return; | 213 return; |
| 202 | 214 |
| 203 for (let type of nonDefaultTypes(filter.contentType)) | 215 for (let type of nonDefaultTypes(filter.contentType)) |
| 204 { | 216 { |
| 205 let map = this._filterMapsByType.get(type); | 217 let map = this._filterMapsByType.get(type); |
| 206 if (!map) | 218 if (!map) |
| 207 this._filterMapsByType.set(type, map = new Map()); | 219 this._filterMapsByType.set(type, map = new Map()); |
| 208 | 220 |
| 209 addFilterByKeyword(filter, keyword, map); | 221 addFilterByKeyword(filter, keyword, map); |
| 210 } | 222 } |
| 211 } | 223 } |
| 212 | 224 |
| 213 /** | 225 /** |
| 214 * Removes a filter from the matcher | 226 * Removes a filter from the matcher |
| 215 * @param {RegExpFilter} filter | 227 * @param {RegExpFilter} filter |
| 216 */ | 228 */ |
| 217 remove(filter) | 229 remove(filter) |
| 218 { | 230 { |
| 219 let keyword = this.findKeyword(filter); | 231 let keyword = this._keywordsByFilter.get(filter); |
| 232 if (typeof keyword == "undefined") |
| 233 return; |
| 234 |
| 220 let locationOnly = filter.isLocationOnly(); | 235 let locationOnly = filter.isLocationOnly(); |
| 221 | 236 |
| 222 removeFilterByKeyword(filter, keyword, | 237 removeFilterByKeyword(filter, keyword, |
| 223 locationOnly ? this._simpleFiltersByKeyword : | 238 locationOnly ? this._simpleFiltersByKeyword : |
| 224 this._complexFiltersByKeyword); | 239 this._complexFiltersByKeyword); |
| 225 | 240 |
| 241 this._keywordsByFilter.delete(filter); |
| 242 |
| 226 if (locationOnly) | 243 if (locationOnly) |
| 227 return; | 244 return; |
| 228 | 245 |
| 229 for (let type of nonDefaultTypes(filter.contentType)) | 246 for (let type of nonDefaultTypes(filter.contentType)) |
| 230 { | 247 { |
| 231 let map = this._filterMapsByType.get(type); | 248 let map = this._filterMapsByType.get(type); |
| 232 if (map) | 249 if (map) |
| 233 removeFilterByKeyword(filter, keyword, map); | 250 removeFilterByKeyword(filter, keyword, map); |
| 234 } | 251 } |
| 235 } | 252 } |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 | 569 |
| 553 exports.CombinedMatcher = CombinedMatcher; | 570 exports.CombinedMatcher = CombinedMatcher; |
| 554 | 571 |
| 555 /** | 572 /** |
| 556 * Shared {@link CombinedMatcher} instance that should usually be used. | 573 * Shared {@link CombinedMatcher} instance that should usually be used. |
| 557 * @type {CombinedMatcher} | 574 * @type {CombinedMatcher} |
| 558 */ | 575 */ |
| 559 let defaultMatcher = new CombinedMatcher(); | 576 let defaultMatcher = new CombinedMatcher(); |
| 560 | 577 |
| 561 exports.defaultMatcher = defaultMatcher; | 578 exports.defaultMatcher = defaultMatcher; |
| OLD | NEW |