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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 22 matching lines...) Expand all Loading... | |
33 */ | 33 */ |
34 var filterByKey = []; | 34 var filterByKey = []; |
35 | 35 |
36 /** | 36 /** |
37 * Lookup table, keys of the filters by filter text | 37 * Lookup table, keys of the filters by filter text |
38 * @type Object | 38 * @type Object |
39 */ | 39 */ |
40 var keyByFilter = Object.create(null); | 40 var keyByFilter = Object.create(null); |
41 | 41 |
42 /** | 42 /** |
43 * Nested lookup table, filter (or false if inactive) by filter text by domain | 43 * Indicates whether we are using the getSelectorsForDomain function and |
44 * therefore mainting the required filtersByDomain, filtersBySelector and | |
45 * unconditionalSelectors lookups. (Will be false for Firefox) | |
46 * @type Boolean | |
47 */ | |
48 var usingGetSelectorsForDomain = !("nsIStyleSheetService" in Ci); | |
49 | |
50 /** | |
51 * Nested lookup table, filter (or false if inactive) by filter key by domain. | |
52 * (Only contains filters that aren't unconditionally matched for all domains.) | |
44 * @type Object | 53 * @type Object |
45 */ | 54 */ |
46 var filtersByDomain = Object.create(null); | 55 var filtersByDomain = Object.create(null); |
47 | 56 |
48 /** | 57 /** |
49 * Indicates whether we are using (and maintaining) the filtersByDomain lookup. | 58 * Lookup table, filters by selector. (Only contains filters that have a |
50 * (Will be false for Firefox) | 59 * selector that is unconditionally matched for all domains.) |
51 * @type Boolean | |
52 */ | 60 */ |
53 var usingFiltersByDomain = !("nsIStyleSheetService" in Ci); | 61 var filtersBySelector = Object.create(null); |
62 | |
63 /** | |
64 * Array of selectors which unconditionally apply to all domains, or null if | |
65 * the array needs to be regenerated. | |
Wladimir Palant
2016/05/25 10:43:03
Maybe change the description to better indicate wh
kzar
2016/05/25 10:51:28
Done.
| |
66 */ | |
67 var unconditionalSelectors = []; | |
Wladimir Palant
2016/05/25 10:43:03
Nit: Please initialize with null here and in clear
kzar
2016/05/25 10:51:28
Done.
| |
54 | 68 |
55 /** | 69 /** |
56 * Object to be used instead when a filter has a blank domains property. | 70 * Object to be used instead when a filter has a blank domains property. |
57 */ | 71 */ |
58 var defaultDomains = Object.create(null); | 72 var defaultDomains = Object.create(null); |
59 defaultDomains[""] = true; | 73 defaultDomains[""] = true; |
60 | 74 |
61 /** | 75 /** |
62 * Lookup table, keys are known element hiding exceptions | 76 * Lookup table, keys are known element hiding exceptions |
63 * @type Object | 77 * @type Object |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 }, | 126 }, |
113 | 127 |
114 /** | 128 /** |
115 * Removes all known filters | 129 * Removes all known filters |
116 */ | 130 */ |
117 clear: function() | 131 clear: function() |
118 { | 132 { |
119 filterByKey = []; | 133 filterByKey = []; |
120 keyByFilter = Object.create(null); | 134 keyByFilter = Object.create(null); |
121 filtersByDomain = Object.create(null); | 135 filtersByDomain = Object.create(null); |
136 filtersBySelector = Object.create(null); | |
137 unconditionalSelectors = []; | |
122 knownExceptions = Object.create(null); | 138 knownExceptions = Object.create(null); |
123 exceptions = Object.create(null); | 139 exceptions = Object.create(null); |
124 ElemHide.isDirty = false; | 140 ElemHide.isDirty = false; |
125 ElemHide.unapply(); | 141 ElemHide.unapply(); |
126 }, | 142 }, |
127 | 143 |
144 _addToFiltersByDomain: function(filter) | |
145 { | |
146 let key = keyByFilter[filter.text]; | |
147 let domains = filter.domains || defaultDomains; | |
148 for (let domain in domains) | |
149 { | |
150 let filters = filtersByDomain[domain]; | |
151 if (!filters) | |
152 filters = filtersByDomain[domain] = Object.create(null); | |
153 | |
154 if (domains[domain]) | |
155 filters[key] = filter; | |
156 else | |
157 filters[key] = false; | |
158 } | |
159 }, | |
160 | |
128 /** | 161 /** |
129 * Add a new element hiding filter | 162 * Add a new element hiding filter |
130 * @param {ElemHideFilter} filter | 163 * @param {ElemHideFilter} filter |
131 */ | 164 */ |
132 add: function(filter) | 165 add: function(filter) |
133 { | 166 { |
134 if (filter instanceof ElemHideException) | 167 if (filter instanceof ElemHideException) |
135 { | 168 { |
136 if (filter.text in knownExceptions) | 169 if (filter.text in knownExceptions) |
137 return; | 170 return; |
138 | 171 |
139 let selector = filter.selector; | 172 let selector = filter.selector; |
140 if (!(selector in exceptions)) | 173 if (!(selector in exceptions)) |
141 exceptions[selector] = []; | 174 exceptions[selector] = []; |
142 exceptions[selector].push(filter); | 175 exceptions[selector].push(filter); |
176 | |
177 if (usingGetSelectorsForDomain) | |
178 { | |
179 // If this is the first exception for a previously unconditionally | |
180 // applied element hiding selector we need to take care to update the | |
181 // lookups. | |
182 let unconditionalFilters = filtersBySelector[selector]; | |
183 if (unconditionalFilters) | |
184 { | |
185 for (let f of unconditionalFilters) | |
186 this._addToFiltersByDomain(f); | |
187 delete filtersBySelector[selector]; | |
188 unconditionalSelectors = null; | |
189 } | |
190 } | |
191 | |
143 knownExceptions[filter.text] = true; | 192 knownExceptions[filter.text] = true; |
144 } | 193 } |
145 else | 194 else |
146 { | 195 { |
147 if (filter.text in keyByFilter) | 196 if (filter.text in keyByFilter) |
148 return; | 197 return; |
149 | 198 |
150 let key = filterByKey.push(filter) - 1; | 199 let key = filterByKey.push(filter) - 1; |
151 keyByFilter[filter.text] = key; | 200 keyByFilter[filter.text] = key; |
152 | 201 |
153 if (usingFiltersByDomain) | 202 if (usingGetSelectorsForDomain) |
154 { | 203 { |
155 let domains = filter.domains || defaultDomains; | 204 if (!(filter.domains || filter.selector in exceptions)) |
156 for (let domain in domains) | |
157 { | 205 { |
158 let filters = filtersByDomain[domain]; | 206 // The new filter's selector is unconditionally applied to all domains |
159 if (!filters) | 207 let filters = filtersBySelector[filter.selector]; |
160 filters = filtersByDomain[domain] = Object.create(null); | 208 if (filters) |
161 | 209 { |
162 if (domains[domain]) | 210 filters.push(filter); |
163 filters[filter.text] = filter; | 211 } |
164 else | 212 else |
165 filters[filter.text] = false; | 213 { |
214 filtersBySelector[filter.selector] = [filter]; | |
215 unconditionalSelectors = null; | |
216 } | |
217 } | |
218 else | |
219 { | |
220 // The new filter's selector only applies to some domains | |
221 this._addToFiltersByDomain(filter); | |
166 } | 222 } |
167 } | 223 } |
168 | 224 |
169 ElemHide.isDirty = true; | 225 ElemHide.isDirty = true; |
170 } | 226 } |
171 }, | 227 }, |
172 | 228 |
173 /** | 229 /** |
174 * Removes an element hiding filter | 230 * Removes an element hiding filter |
175 * @param {ElemHideFilter} filter | 231 * @param {ElemHideFilter} filter |
(...skipping 14 matching lines...) Expand all Loading... | |
190 else | 246 else |
191 { | 247 { |
192 if (!(filter.text in keyByFilter)) | 248 if (!(filter.text in keyByFilter)) |
193 return; | 249 return; |
194 | 250 |
195 let key = keyByFilter[filter.text]; | 251 let key = keyByFilter[filter.text]; |
196 delete filterByKey[key]; | 252 delete filterByKey[key]; |
197 delete keyByFilter[filter.text]; | 253 delete keyByFilter[filter.text]; |
198 ElemHide.isDirty = true; | 254 ElemHide.isDirty = true; |
199 | 255 |
200 if (usingFiltersByDomain) | 256 if (usingGetSelectorsForDomain) |
201 { | 257 { |
202 let domains = filter.domains || defaultDomains; | 258 let filters = filtersBySelector[filter.selector]; |
203 for (let domain in domains) | 259 if (filters) |
204 { | 260 { |
205 let filters = filtersByDomain[domain]; | 261 if (filters.length > 1) |
206 if (filters) | 262 { |
207 delete filters[filter.text]; | 263 let index = filters.indexOf(filter); |
264 filters.splice(index, 1); | |
265 } | |
266 else | |
267 { | |
268 delete filtersBySelector[filter.selector]; | |
269 unconditionalSelectors = null; | |
270 } | |
271 } | |
272 else | |
273 { | |
274 let domains = filter.domains || defaultDomains; | |
275 for (let domain in domains) | |
276 { | |
277 let filters = filtersByDomain[domain]; | |
278 if (filters) | |
279 delete filters[key]; | |
280 } | |
208 } | 281 } |
209 } | 282 } |
210 } | 283 } |
211 }, | 284 }, |
212 | 285 |
213 /** | 286 /** |
214 * Checks whether an exception rule is registered for a filter on a particular | 287 * Checks whether an exception rule is registered for a filter on a particular |
215 * domain. | 288 * domain. |
216 */ | 289 */ |
217 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/ | 290 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/ |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
409 /** | 482 /** |
410 * Retrieves an element hiding filter by the corresponding protocol key | 483 * Retrieves an element hiding filter by the corresponding protocol key |
411 */ | 484 */ |
412 getFilterByKey: function(/**String*/ key) /**Filter*/ | 485 getFilterByKey: function(/**String*/ key) /**Filter*/ |
413 { | 486 { |
414 return (key in filterByKey ? filterByKey[key] : null); | 487 return (key in filterByKey ? filterByKey[key] : null); |
415 }, | 488 }, |
416 | 489 |
417 /** | 490 /** |
418 * Returns a list of all selectors active on a particular domain, must not be | 491 * Returns a list of all selectors active on a particular domain, must not be |
419 * used in Firefox (when usingFiltersByDomain is false). | 492 * used in Firefox (when usingGetSelectorsForDomain is false). |
420 */ | 493 */ |
421 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) | 494 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) |
422 { | 495 { |
423 if (!usingFiltersByDomain) | 496 if (!usingGetSelectorsForDomain) |
424 throw new Error("getSelectorsForDomain can not be used in Firefox!"); | 497 throw new Error("getSelectorsForDomain can not be used in Firefox!"); |
425 | 498 |
426 let selectors = []; | 499 if (!unconditionalSelectors) |
500 unconditionalSelectors = Object.keys(filtersBySelector); | |
501 let selectors = specificOnly ? [] : unconditionalSelectors.slice(); | |
427 | 502 |
428 let seenFilters = Object.create(null); | 503 let seenFilters = Object.create(null); |
429 let currentDomain = domain ? domain.toUpperCase() : ""; | 504 let currentDomain = domain ? domain.toUpperCase() : ""; |
430 while (true) | 505 while (true) |
431 { | 506 { |
432 if (specificOnly && currentDomain == "") | 507 if (specificOnly && currentDomain == "") |
433 break; | 508 break; |
434 | 509 |
435 let filters = filtersByDomain[currentDomain]; | 510 let filters = filtersByDomain[currentDomain]; |
436 if (filters) | 511 if (filters) |
437 { | 512 { |
438 for (let filterText in filters) | 513 for (let filterKey in filters) |
439 { | 514 { |
440 if (filterText in seenFilters) | 515 if (filterKey in seenFilters) |
441 continue; | 516 continue; |
442 seenFilters[filterText] = true; | 517 seenFilters[filterKey] = true; |
443 | 518 |
444 let filter = filters[filterText]; | 519 let filter = filters[filterKey]; |
445 if (filter && !this.getException(filter, domain)) | 520 if (filter && !this.getException(filter, domain)) |
446 selectors.push(filter.selector); | 521 selectors.push(filter.selector); |
447 } | 522 } |
448 } | 523 } |
449 | 524 |
450 if (currentDomain == "") | 525 if (currentDomain == "") |
451 break; | 526 break; |
452 | 527 |
453 let nextDot = currentDomain.indexOf("."); | 528 let nextDot = currentDomain.indexOf("."); |
454 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 529 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
455 } | 530 } |
456 | 531 |
457 return selectors; | 532 return selectors; |
458 } | 533 } |
459 }; | 534 }; |
OLD | NEW |