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