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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
48 var usingGetSelectorsForDomain = !("nsIStyleSheetService" in Ci); | 48 var usingGetSelectorsForDomain = !("nsIStyleSheetService" in Ci); |
49 | 49 |
50 /** | 50 /** |
51 * Nested lookup table, filter (or false if inactive) by filter key by domain. | 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.) | 52 * (Only contains filters that aren't unconditionally matched for all domains.) |
53 * @type Object | 53 * @type Object |
54 */ | 54 */ |
55 var filtersByDomain = Object.create(null); | 55 var filtersByDomain = Object.create(null); |
56 | 56 |
57 /** | 57 /** |
58 * Lookup table, count of filter exceptions by domain. | |
59 * @type Object | |
60 */ | |
61 var exceptionCountByDomain = Object.create(null); | |
62 | |
63 /** | |
58 * Lookup table, filters by selector. (Only contains filters that have a | 64 * Lookup table, filters by selector. (Only contains filters that have a |
59 * selector that is unconditionally matched for all domains.) | 65 * selector that is unconditionally matched for all domains.) |
60 */ | 66 */ |
61 var filtersBySelector = Object.create(null); | 67 var filtersBySelector = Object.create(null); |
62 | 68 |
63 /** | 69 /** |
64 * This array caches the keys of filtersBySelector table (selectors which | 70 * 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 | 71 * unconditionally apply on all domains). It will be null if the cache needs to |
66 * be rebuilt. | 72 * be rebuilt. |
67 */ | 73 */ |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 | 134 |
129 /** | 135 /** |
130 * Removes all known filters | 136 * Removes all known filters |
131 */ | 137 */ |
132 clear: function() | 138 clear: function() |
133 { | 139 { |
134 filterByKey = []; | 140 filterByKey = []; |
135 keyByFilter = Object.create(null); | 141 keyByFilter = Object.create(null); |
136 filtersByDomain = Object.create(null); | 142 filtersByDomain = Object.create(null); |
137 filtersBySelector = Object.create(null); | 143 filtersBySelector = Object.create(null); |
144 exceptionCountByDomain = Object.create(null); | |
138 unconditionalSelectors = null; | 145 unconditionalSelectors = null; |
139 knownExceptions = Object.create(null); | 146 knownExceptions = Object.create(null); |
140 exceptions = Object.create(null); | 147 exceptions = Object.create(null); |
141 ElemHide.isDirty = false; | 148 ElemHide.isDirty = false; |
142 ElemHide.unapply(); | 149 ElemHide.unapply(); |
143 }, | 150 }, |
144 | 151 |
145 _addToFiltersByDomain: function(filter) | 152 _addToFiltersByDomain: function(filter) |
146 { | 153 { |
147 let key = keyByFilter[filter.text]; | 154 let key = keyByFilter[filter.text]; |
148 let domains = filter.domains || defaultDomains; | 155 let domains = filter.domains || defaultDomains; |
149 for (let domain in domains) | 156 for (let domain in domains) |
150 { | 157 { |
151 let filters = filtersByDomain[domain]; | 158 let filters = filtersByDomain[domain]; |
152 if (!filters) | 159 if (!filters) |
153 filters = filtersByDomain[domain] = Object.create(null); | 160 filters = filtersByDomain[domain] = Object.create(null); |
154 | 161 |
155 if (domains[domain]) | 162 if (domains[domain]) |
163 { | |
156 filters[key] = filter; | 164 filters[key] = filter; |
165 } | |
157 else | 166 else |
167 { | |
158 filters[key] = false; | 168 filters[key] = false; |
169 if (domain in exceptionCountByDomain) | |
170 exceptionCountByDomain[domain] += 1; | |
171 else | |
172 exceptionCountByDomain[domain] = 1; | |
173 } | |
159 } | 174 } |
160 }, | 175 }, |
161 | 176 |
162 /** | 177 /** |
163 * Add a new element hiding filter | 178 * Add a new element hiding filter |
164 * @param {ElemHideFilter} filter | 179 * @param {ElemHideFilter} filter |
165 */ | 180 */ |
166 add: function(filter) | 181 add: function(filter) |
167 { | 182 { |
168 if (filter instanceof ElemHideException) | 183 if (filter instanceof ElemHideException) |
169 { | 184 { |
170 if (filter.text in knownExceptions) | 185 if (filter.text in knownExceptions) |
171 return; | 186 return; |
172 | 187 |
173 let selector = filter.selector; | 188 let selector = filter.selector; |
174 if (!(selector in exceptions)) | 189 if (!(selector in exceptions)) |
175 exceptions[selector] = []; | 190 exceptions[selector] = []; |
176 exceptions[selector].push(filter); | 191 exceptions[selector].push(filter); |
177 | 192 |
178 if (usingGetSelectorsForDomain) | 193 if (usingGetSelectorsForDomain) |
179 { | 194 { |
195 let domains = filter.domains || defaultDomains; | |
196 for (let domain in domains) | |
197 { | |
198 if (domain in exceptionCountByDomain) | |
199 exceptionCountByDomain[domain] += 1; | |
200 else | |
201 exceptionCountByDomain[domain] = 1; | |
202 } | |
203 | |
180 // If this is the first exception for a previously unconditionally | 204 // If this is the first exception for a previously unconditionally |
181 // applied element hiding selector we need to take care to update the | 205 // applied element hiding selector we need to take care to update the |
182 // lookups. | 206 // lookups. |
183 let unconditionalFilters = filtersBySelector[selector]; | 207 let unconditionalFilters = filtersBySelector[selector]; |
184 if (unconditionalFilters) | 208 if (unconditionalFilters) |
185 { | 209 { |
186 for (let f of unconditionalFilters) | 210 for (let f of unconditionalFilters) |
187 this._addToFiltersByDomain(f); | 211 this._addToFiltersByDomain(f); |
188 delete filtersBySelector[selector]; | 212 delete filtersBySelector[selector]; |
189 unconditionalSelectors = null; | 213 unconditionalSelectors = null; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 if (!(filter.text in keyByFilter)) | 273 if (!(filter.text in keyByFilter)) |
250 return; | 274 return; |
251 | 275 |
252 let key = keyByFilter[filter.text]; | 276 let key = keyByFilter[filter.text]; |
253 delete filterByKey[key]; | 277 delete filterByKey[key]; |
254 delete keyByFilter[filter.text]; | 278 delete keyByFilter[filter.text]; |
255 ElemHide.isDirty = true; | 279 ElemHide.isDirty = true; |
256 | 280 |
257 if (usingGetSelectorsForDomain) | 281 if (usingGetSelectorsForDomain) |
258 { | 282 { |
283 let domains = filter.domains || defaultDomains; | |
284 for (let domain in domains) | |
285 { | |
286 let count = exceptionCountByDomain[domain]; | |
287 if (count > 1) | |
288 exceptionCountByDomain[domain] -= 1; | |
289 else if (count) | |
Sebastian Noack
2016/05/26 11:46:43
It seems the condition to check for non-zero count
| |
290 delete exceptionCountByDomain[domain]; | |
291 } | |
292 | |
259 let filters = filtersBySelector[filter.selector]; | 293 let filters = filtersBySelector[filter.selector]; |
260 if (filters) | 294 if (filters) |
261 { | 295 { |
262 if (filters.length > 1) | 296 if (filters.length > 1) |
263 { | 297 { |
264 let index = filters.indexOf(filter); | 298 let index = filters.indexOf(filter); |
265 filters.splice(index, 1); | 299 filters.splice(index, 1); |
266 } | 300 } |
267 else | 301 else |
268 { | 302 { |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
494 */ | 528 */ |
495 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) | 529 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) |
496 { | 530 { |
497 if (!usingGetSelectorsForDomain) | 531 if (!usingGetSelectorsForDomain) |
498 throw new Error("getSelectorsForDomain can not be used in Firefox!"); | 532 throw new Error("getSelectorsForDomain can not be used in Firefox!"); |
499 | 533 |
500 if (!unconditionalSelectors) | 534 if (!unconditionalSelectors) |
501 unconditionalSelectors = Object.keys(filtersBySelector); | 535 unconditionalSelectors = Object.keys(filtersBySelector); |
502 let selectors = specificOnly ? [] : unconditionalSelectors.slice(); | 536 let selectors = specificOnly ? [] : unconditionalSelectors.slice(); |
503 | 537 |
538 let anyExceptions = false; | |
539 | |
504 let seenFilters = Object.create(null); | 540 let seenFilters = Object.create(null); |
505 let currentDomain = domain ? domain.toUpperCase() : ""; | 541 let currentDomain = domain ? domain.toUpperCase() : ""; |
506 while (true) | 542 while (true) |
507 { | 543 { |
508 if (specificOnly && currentDomain == "") | 544 if (currentDomain == "") |
509 break; | 545 { |
546 if (specificOnly) | |
547 break; | |
548 | |
549 // For domains with no exceptions we can add all generic rules | |
550 if (!anyExceptions) | |
551 { | |
552 let filters = filtersByDomain[""]; | |
553 for (let filterKey in filters) | |
554 { | |
555 let filter = filters[filterKey]; | |
556 if (filter) | |
Sebastian Noack
2016/05/26 11:46:43
Just for my understanding, what is the scenario wh
| |
557 selectors.push(filters[filterKey].selector); | |
Sebastian Noack
2016/05/26 11:46:43
Use the "filter" variable here?
| |
558 } | |
559 break; | |
560 } | |
561 } | |
562 anyExceptions = anyExceptions || exceptionCountByDomain[currentDomain]; | |
Sebastian Noack
2016/05/26 11:46:43
I think it's preferable to write it like this:
i
| |
510 | 563 |
511 let filters = filtersByDomain[currentDomain]; | 564 let filters = filtersByDomain[currentDomain]; |
512 if (filters) | 565 if (filters) |
513 { | 566 { |
514 for (let filterKey in filters) | 567 for (let filterKey in filters) |
515 { | 568 { |
516 if (filterKey in seenFilters) | 569 if (filterKey in seenFilters) |
517 continue; | 570 continue; |
518 seenFilters[filterKey] = true; | 571 seenFilters[filterKey] = true; |
519 | 572 |
520 let filter = filters[filterKey]; | 573 let filter = filters[filterKey]; |
521 if (filter && !this.getException(filter, domain)) | 574 if (filter && !this.getException(filter, domain)) |
522 selectors.push(filter.selector); | 575 selectors.push(filter.selector); |
523 } | 576 } |
524 } | 577 } |
525 | 578 |
526 if (currentDomain == "") | 579 if (currentDomain == "") |
527 break; | 580 break; |
528 | 581 |
529 let nextDot = currentDomain.indexOf("."); | 582 let nextDot = currentDomain.indexOf("."); |
530 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); | 583 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1); |
531 } | 584 } |
532 | 585 |
533 return selectors; | 586 return selectors; |
534 } | 587 } |
535 }; | 588 }; |
OLD | NEW |