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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 */ | 56 */ |
57 let knownFilters = new Set(); | 57 let knownFilters = new Set(); |
58 | 58 |
59 /** | 59 /** |
60 * Lookup table, lists of element hiding exceptions by selector | 60 * Lookup table, lists of element hiding exceptions by selector |
61 * @type {Map.<string,Filter>} | 61 * @type {Map.<string,Filter>} |
62 */ | 62 */ |
63 let exceptions = new Map(); | 63 let exceptions = new Map(); |
64 | 64 |
65 /** | 65 /** |
| 66 * Returns a list of selectors that apply on each website unconditionally. |
| 67 * @returns {string[]} |
| 68 */ |
| 69 function getUnconditionalSelectors() |
| 70 { |
| 71 if (!unconditionalSelectors) |
| 72 unconditionalSelectors = [...filterBySelector.keys()]; |
| 73 return unconditionalSelectors; |
| 74 } |
| 75 |
| 76 /** |
66 * Container for element hiding filters | 77 * Container for element hiding filters |
67 * @class | 78 * @class |
68 */ | 79 */ |
69 let ElemHide = exports.ElemHide = { | 80 let ElemHide = exports.ElemHide = { |
70 /** | 81 /** |
71 * Removes all known filters | 82 * Removes all known filters |
72 */ | 83 */ |
73 clear() | 84 clear() |
74 { | 85 { |
75 for (let collection of [filtersByDomain, filterBySelector, | 86 for (let collection of [filtersByDomain, filterBySelector, |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 for (let i = list.length - 1; i >= 0; i--) | 208 for (let i = list.length - 1; i >= 0; i--) |
198 { | 209 { |
199 if (list[i].isActiveOnDomain(docDomain)) | 210 if (list[i].isActiveOnDomain(docDomain)) |
200 return list[i]; | 211 return list[i]; |
201 } | 212 } |
202 | 213 |
203 return null; | 214 return null; |
204 }, | 215 }, |
205 | 216 |
206 /** | 217 /** |
207 * Returns a list of selectors that apply on each website unconditionally. | |
208 * @returns {string[]} | |
209 */ | |
210 getUnconditionalSelectors() | |
211 { | |
212 if (!unconditionalSelectors) | |
213 unconditionalSelectors = [...filterBySelector.keys()]; | |
214 return unconditionalSelectors.slice(); | |
215 }, | |
216 | |
217 /** | |
218 * Constant used by getSelectorsForDomain to return all selectors applying to | 218 * Constant used by getSelectorsForDomain to return all selectors applying to |
219 * a particular hostname. | 219 * a particular hostname. |
220 */ | 220 */ |
221 ALL_MATCHING: 0, | 221 ALL_MATCHING: 0, |
222 | 222 |
223 /** | 223 /** |
224 * Constant used by getSelectorsForDomain to exclude selectors which apply to | 224 * Constant used by getSelectorsForDomain to exclude selectors which apply to |
225 * all websites without exception. | 225 * all websites without exception. |
226 */ | 226 */ |
227 NO_UNCONDITIONAL: 1, | 227 NO_UNCONDITIONAL: 1, |
228 | 228 |
229 /** | 229 /** |
230 * Constant used by getSelectorsForDomain to return only selectors for filters | 230 * Constant used by getSelectorsForDomain to return only selectors for filters |
231 * which specifically match the given host name. | 231 * which specifically match the given host name. |
232 */ | 232 */ |
233 SPECIFIC_ONLY: 2, | 233 SPECIFIC_ONLY: 2, |
234 | 234 |
235 /** | 235 /** |
236 * Determines from the current filter list which selectors should be applied | 236 * Determines from the current filter list which selectors should be applied |
237 * on a particular host name. | 237 * on a particular host name. |
238 * @param {string} domain | 238 * @param {string} domain |
239 * @param {number} [criteria] | 239 * @param {number} [criteria] |
240 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or | 240 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or |
241 * ElemHide.SPECIFIC_ONLY. | 241 * ElemHide.SPECIFIC_ONLY. |
242 * @returns {string[]} | 242 * @returns {string[]} |
243 * List of selectors. | 243 * List of selectors. |
244 */ | 244 */ |
245 getSelectorsForDomain(domain, criteria) | 245 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) |
246 { | 246 { |
247 let selectors = []; | 247 let selectors = []; |
248 | 248 |
249 if (typeof criteria == "undefined") | 249 let specificOnly = criteria >= ElemHide.SPECIFIC_ONLY; |
250 criteria = ElemHide.ALL_MATCHING; | |
251 if (criteria < ElemHide.NO_UNCONDITIONAL) | |
252 selectors = this.getUnconditionalSelectors(); | |
253 | 250 |
254 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); | |
255 let excluded = new Set(); | 251 let excluded = new Set(); |
256 let currentDomain = domain ? domain.toUpperCase() : ""; | 252 let currentDomain = domain ? domain.toUpperCase() : ""; |
257 | 253 |
258 // This code is a performance hot-spot, which is why we've made certain | 254 // This code is a performance hot-spot, which is why we've made certain |
259 // micro-optimisations. Please be careful before making changes. | 255 // micro-optimisations. Please be careful before making changes. |
260 while (true) | 256 while (true) |
261 { | 257 { |
262 if (specificOnly && currentDomain == "") | 258 if (specificOnly && currentDomain == "") |
263 break; | 259 break; |
264 | 260 |
(...skipping 14 matching lines...) Expand all Loading... |
279 } | 275 } |
280 } | 276 } |
281 | 277 |
282 if (currentDomain == "") | 278 if (currentDomain == "") |
283 break; | 279 break; |
284 | 280 |
285 let nextDot = currentDomain.indexOf("."); | 281 let nextDot = currentDomain.indexOf("."); |
286 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 282 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
287 } | 283 } |
288 | 284 |
| 285 if (criteria < ElemHide.NO_UNCONDITIONAL) |
| 286 selectors = getUnconditionalSelectors().concat(selectors); |
| 287 |
289 return selectors; | 288 return selectors; |
290 } | 289 } |
291 }; | 290 }; |
OLD | NEW |