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