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 |
(...skipping 24 matching lines...) Expand all Loading... | |
35 * Lookup table, filter by selector. (Only used for selectors that are | 35 * Lookup table, filter by selector. (Only used for selectors that are |
36 * unconditionally matched for all domains.) | 36 * unconditionally matched for all domains.) |
37 * @type {Map.<string,Filter>} | 37 * @type {Map.<string,Filter>} |
38 */ | 38 */ |
39 let filterBySelector = new Map(); | 39 let filterBySelector = new Map(); |
40 | 40 |
41 /** | 41 /** |
42 * This array caches the keys of filterBySelector table (selectors | 42 * This array caches the keys of filterBySelector table (selectors |
43 * which unconditionally apply on all domains). It will be null if the | 43 * which unconditionally apply on all domains). It will be null if the |
44 * cache needs to be rebuilt. | 44 * cache needs to be rebuilt. |
45 * @type {?string[]} | |
45 */ | 46 */ |
46 let unconditionalSelectors = null; | 47 let unconditionalSelectors = null; |
47 | 48 |
48 /** | 49 /** |
49 * Map to be used instead when a filter has a blank domains property. | 50 * Map to be used instead when a filter has a blank domains property. |
51 * @type {Map.<string,boolean>} | |
52 * @const | |
50 */ | 53 */ |
51 let defaultDomains = new Map([["", true]]); | 54 let defaultDomains = new Map([["", true]]); |
52 | 55 |
53 /** | 56 /** |
54 * Set containing known element hiding and exception filters | 57 * Set containing known element hiding and exception filters |
55 * @type {Set.<ElemHideBase>} | 58 * @type {Set.<ElemHideBase>} |
56 */ | 59 */ |
57 let knownFilters = new Set(); | 60 let knownFilters = new Set(); |
58 | 61 |
59 /** | 62 /** |
60 * Lookup table, lists of element hiding exceptions by selector | 63 * Lookup table, lists of element hiding exceptions by selector |
61 * @type {Map.<string,Filter>} | 64 * @type {Map.<string,Filter>} |
62 */ | 65 */ |
63 let exceptions = new Map(); | 66 let exceptions = new Map(); |
67 | |
68 /** | |
69 * Adds a filter to the lookup table of filters by domain. | |
70 * @param {Filter} | |
71 */ | |
72 function addToFiltersByDomain(filter) | |
73 { | |
74 let domains = filter.domains || defaultDomains; | |
75 for (let [domain, isIncluded] of domains) | |
76 { | |
77 // There's no need to note that a filter is generically disabled. | |
78 if (!isIncluded && domain == "") | |
79 continue; | |
80 | |
81 let filters = filtersByDomain.get(domain); | |
82 if (!filters) | |
83 filtersByDomain.set(domain, filters = new Map()); | |
84 filters.set(filter, isIncluded); | |
85 } | |
86 } | |
64 | 87 |
65 /** | 88 /** |
66 * Returns a list of selectors that apply on each website unconditionally. | 89 * Returns a list of selectors that apply on each website unconditionally. |
kzar
2018/05/10 10:59:49
Please could you also remove this unrelated change
Manish Jethani
2018/05/11 04:07:10
Well if we're going to keep the function available
kzar
2018/05/11 11:23:43
Gotya, OK that makes sense.
This LGTM then, I'd p
| |
67 * @returns {string[]} | 90 * @returns {string[]} |
68 */ | 91 */ |
69 function getUnconditionalSelectors() | 92 function getUnconditionalSelectors() |
70 { | 93 { |
71 if (!unconditionalSelectors) | 94 if (!unconditionalSelectors) |
72 unconditionalSelectors = [...filterBySelector.keys()]; | 95 unconditionalSelectors = [...filterBySelector.keys()]; |
73 | 96 |
74 return unconditionalSelectors; | 97 return unconditionalSelectors; |
75 } | 98 } |
76 | 99 |
77 /** | 100 /** |
78 * Container for element hiding filters | 101 * Container for element hiding filters |
79 * @class | 102 * @class |
80 */ | 103 */ |
81 let ElemHide = exports.ElemHide = { | 104 let ElemHide = exports.ElemHide = { |
82 /** | 105 /** |
83 * Removes all known filters | 106 * Removes all known filters |
84 */ | 107 */ |
85 clear() | 108 clear() |
86 { | 109 { |
87 for (let collection of [filtersByDomain, filterBySelector, | 110 for (let collection of [filtersByDomain, filterBySelector, |
88 knownFilters, exceptions]) | 111 knownFilters, exceptions]) |
89 { | 112 { |
90 collection.clear(); | 113 collection.clear(); |
91 } | 114 } |
92 unconditionalSelectors = null; | 115 unconditionalSelectors = null; |
93 FilterNotifier.emit("elemhideupdate"); | 116 FilterNotifier.emit("elemhideupdate"); |
94 }, | |
95 | |
96 _addToFiltersByDomain(filter) | |
97 { | |
98 let domains = filter.domains || defaultDomains; | |
99 for (let [domain, isIncluded] of domains) | |
100 { | |
101 // There's no need to note that a filter is generically disabled. | |
102 if (!isIncluded && domain == "") | |
103 continue; | |
104 | |
105 let filters = filtersByDomain.get(domain); | |
106 if (!filters) | |
107 filtersByDomain.set(domain, filters = new Map()); | |
108 filters.set(filter, isIncluded); | |
109 } | |
110 }, | 117 }, |
111 | 118 |
112 /** | 119 /** |
113 * Add a new element hiding filter | 120 * Add a new element hiding filter |
114 * @param {ElemHideBase} filter | 121 * @param {ElemHideBase} filter |
115 */ | 122 */ |
116 add(filter) | 123 add(filter) |
117 { | 124 { |
118 if (knownFilters.has(filter)) | 125 if (knownFilters.has(filter)) |
119 return; | 126 return; |
120 | 127 |
121 if (filter instanceof ElemHideException) | 128 if (filter instanceof ElemHideException) |
122 { | 129 { |
123 let {selector} = filter; | 130 let {selector} = filter; |
124 let list = exceptions.get(selector); | 131 let list = exceptions.get(selector); |
125 if (list) | 132 if (list) |
126 list.push(filter); | 133 list.push(filter); |
127 else | 134 else |
128 exceptions.set(selector, [filter]); | 135 exceptions.set(selector, [filter]); |
129 | 136 |
130 // If this is the first exception for a previously unconditionally | 137 // If this is the first exception for a previously unconditionally |
131 // applied element hiding selector we need to take care to update the | 138 // applied element hiding selector we need to take care to update the |
132 // lookups. | 139 // lookups. |
133 let unconditionalFilterForSelector = filterBySelector.get(selector); | 140 let unconditionalFilterForSelector = filterBySelector.get(selector); |
134 if (unconditionalFilterForSelector) | 141 if (unconditionalFilterForSelector) |
135 { | 142 { |
136 this._addToFiltersByDomain(unconditionalFilterForSelector); | 143 addToFiltersByDomain(unconditionalFilterForSelector); |
137 filterBySelector.delete(selector); | 144 filterBySelector.delete(selector); |
138 unconditionalSelectors = null; | 145 unconditionalSelectors = null; |
139 } | 146 } |
140 } | 147 } |
141 else if (!(filter.domains || exceptions.has(filter.selector))) | 148 else if (!(filter.domains || exceptions.has(filter.selector))) |
142 { | 149 { |
143 // The new filter's selector is unconditionally applied to all domains | 150 // The new filter's selector is unconditionally applied to all domains |
144 filterBySelector.set(filter.selector, filter); | 151 filterBySelector.set(filter.selector, filter); |
145 unconditionalSelectors = null; | 152 unconditionalSelectors = null; |
146 } | 153 } |
147 else | 154 else |
148 { | 155 { |
149 // The new filter's selector only applies to some domains | 156 // The new filter's selector only applies to some domains |
150 this._addToFiltersByDomain(filter); | 157 addToFiltersByDomain(filter); |
151 } | 158 } |
152 | 159 |
153 knownFilters.add(filter); | 160 knownFilters.add(filter); |
154 FilterNotifier.emit("elemhideupdate"); | 161 FilterNotifier.emit("elemhideupdate"); |
155 }, | 162 }, |
156 | 163 |
157 /** | 164 /** |
158 * Removes an element hiding filter | 165 * Removes an element hiding filter |
159 * @param {ElemHideBase} filter | 166 * @param {ElemHideBase} filter |
160 */ | 167 */ |
(...skipping 29 matching lines...) Expand all Loading... | |
190 } | 197 } |
191 | 198 |
192 knownFilters.delete(filter); | 199 knownFilters.delete(filter); |
193 FilterNotifier.emit("elemhideupdate"); | 200 FilterNotifier.emit("elemhideupdate"); |
194 }, | 201 }, |
195 | 202 |
196 /** | 203 /** |
197 * Checks whether an exception rule is registered for a filter on a particular | 204 * Checks whether an exception rule is registered for a filter on a particular |
198 * domain. | 205 * domain. |
199 * @param {Filter} filter | 206 * @param {Filter} filter |
200 * @param {string} docDomain | 207 * @param {?string} docDomain |
201 * @return {ElemHideException} | 208 * @return {?ElemHideException} |
202 */ | 209 */ |
203 getException(filter, docDomain) | 210 getException(filter, docDomain) |
204 { | 211 { |
205 let list = exceptions.get(filter.selector); | 212 let list = exceptions.get(filter.selector); |
206 if (!list) | 213 if (!list) |
207 return null; | 214 return null; |
208 | 215 |
209 for (let i = list.length - 1; i >= 0; i--) | 216 for (let i = list.length - 1; i >= 0; i--) |
210 { | 217 { |
211 if (list[i].isActiveOnDomain(docDomain)) | 218 if (list[i].isActiveOnDomain(docDomain)) |
212 return list[i]; | 219 return list[i]; |
213 } | 220 } |
214 | 221 |
215 return null; | 222 return null; |
216 }, | 223 }, |
217 | 224 |
218 /** | 225 /** |
219 * Constant used by getSelectorsForDomain to return all selectors applying to | 226 * Constant used by getSelectorsForDomain to return all selectors applying to |
220 * a particular hostname. | 227 * a particular hostname. |
228 * @type {number} | |
229 * @const | |
221 */ | 230 */ |
222 ALL_MATCHING: 0, | 231 ALL_MATCHING: 0, |
223 | 232 |
224 /** | 233 /** |
225 * Constant used by getSelectorsForDomain to exclude selectors which apply to | 234 * Constant used by getSelectorsForDomain to exclude selectors which apply to |
226 * all websites without exception. | 235 * all websites without exception. |
236 * @type {number} | |
237 * @const | |
227 */ | 238 */ |
228 NO_UNCONDITIONAL: 1, | 239 NO_UNCONDITIONAL: 1, |
229 | 240 |
230 /** | 241 /** |
231 * Constant used by getSelectorsForDomain to return only selectors for filters | 242 * Constant used by getSelectorsForDomain to return only selectors for filters |
232 * which specifically match the given host name. | 243 * which specifically match the given host name. |
244 * @type {number} | |
245 * @const | |
233 */ | 246 */ |
234 SPECIFIC_ONLY: 2, | 247 SPECIFIC_ONLY: 2, |
235 | 248 |
236 /** | 249 /** |
237 * Determines from the current filter list which selectors should be applied | 250 * Determines from the current filter list which selectors should be applied |
238 * on a particular host name. | 251 * on a particular host name. |
239 * @param {string} domain | 252 * @param {string} domain |
240 * @param {number} [criteria] | 253 * @param {number} [criteria] |
241 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or | 254 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or |
242 * ElemHide.SPECIFIC_ONLY. | 255 * ElemHide.SPECIFIC_ONLY. |
243 * @returns {string[]} | 256 * @returns {string[]} |
244 * List of selectors. | 257 * List of selectors. |
245 */ | 258 */ |
246 getSelectorsForDomain(domain, criteria) | 259 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) |
247 { | 260 { |
248 let selectors = []; | 261 let selectors = []; |
249 | |
250 if (typeof criteria == "undefined") | |
251 criteria = ElemHide.ALL_MATCHING; | |
252 | 262 |
253 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); | 263 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); |
254 let excluded = new Set(); | 264 let excluded = new Set(); |
255 let currentDomain = domain ? domain.toUpperCase() : ""; | 265 let currentDomain = domain ? domain.toUpperCase() : ""; |
256 | 266 |
257 // This code is a performance hot-spot, which is why we've made certain | 267 // This code is a performance hot-spot, which is why we've made certain |
258 // micro-optimisations. Please be careful before making changes. | 268 // micro-optimisations. Please be careful before making changes. |
259 while (true) | 269 while (true) |
260 { | 270 { |
261 if (specificOnly && currentDomain == "") | 271 if (specificOnly && currentDomain == "") |
(...skipping 22 matching lines...) Expand all Loading... | |
284 let nextDot = currentDomain.indexOf("."); | 294 let nextDot = currentDomain.indexOf("."); |
285 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 295 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
286 } | 296 } |
287 | 297 |
288 if (criteria < ElemHide.NO_UNCONDITIONAL) | 298 if (criteria < ElemHide.NO_UNCONDITIONAL) |
289 selectors = getUnconditionalSelectors().concat(selectors); | 299 selectors = getUnconditionalSelectors().concat(selectors); |
290 | 300 |
291 return selectors; | 301 return selectors; |
292 } | 302 } |
293 }; | 303 }; |
LEFT | RIGHT |