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-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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 /** | 20 /** |
21 * @fileOverview Element hiding implementation. | 21 * @fileOverview Element hiding implementation. |
22 */ | 22 */ |
23 | 23 |
| 24 const {Filter} = require("./filterClasses"); |
24 const {ElemHideExceptions} = require("./elemHideExceptions"); | 25 const {ElemHideExceptions} = require("./elemHideExceptions"); |
25 const {filterNotifier} = require("./filterNotifier"); | 26 const {filterNotifier} = require("./filterNotifier"); |
26 const {normalizeHostname, domainSuffixes} = require("./url"); | 27 const {normalizeHostname, domainSuffixes} = require("./url"); |
27 const {Cache} = require("./caching"); | 28 const {Cache} = require("./caching"); |
28 | 29 |
29 /** | 30 /** |
30 * The maximum number of selectors in a CSS rule. This is used by | 31 * The maximum number of selectors in a CSS rule. This is used by |
31 * <code>{@link createStyleSheet}</code> to split up a long list of selectors | 32 * <code>{@link createStyleSheet}</code> to split up a long list of selectors |
32 * into multiple rules. | 33 * into multiple rules. |
33 * @const {number} | 34 * @const {number} |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 { | 128 { |
128 for (let [domain, isIncluded] of domains || defaultDomains) | 129 for (let [domain, isIncluded] of domains || defaultDomains) |
129 { | 130 { |
130 // There's no need to note that a filter is generically disabled. | 131 // There's no need to note that a filter is generically disabled. |
131 if (!isIncluded && domain == "") | 132 if (!isIncluded && domain == "") |
132 continue; | 133 continue; |
133 | 134 |
134 let filters = filtersByDomain.get(domain); | 135 let filters = filtersByDomain.get(domain); |
135 if (!filters) | 136 if (!filters) |
136 filtersByDomain.set(domain, filters = new Map()); | 137 filtersByDomain.set(domain, filters = new Map()); |
137 filters.set(filter, isIncluded); | 138 filters.set(filter.text, isIncluded ? filter.selector : null); |
138 } | 139 } |
139 } | 140 } |
140 | 141 |
141 /** | 142 /** |
142 * Returns a list of selectors that apply on each website unconditionally. | 143 * Returns a list of selectors that apply on each website unconditionally. |
143 * @returns {string[]} | 144 * @returns {string[]} |
144 */ | 145 */ |
145 function getUnconditionalSelectors() | 146 function getUnconditionalSelectors() |
146 { | 147 { |
147 if (!unconditionalSelectors) | 148 if (!unconditionalSelectors) |
(...skipping 16 matching lines...) Expand all Loading... |
164 { | 165 { |
165 let selectors = []; | 166 let selectors = []; |
166 | 167 |
167 let excluded = new Set(); | 168 let excluded = new Set(); |
168 | 169 |
169 for (let currentDomain of domainSuffixes(domain, !specificOnly)) | 170 for (let currentDomain of domainSuffixes(domain, !specificOnly)) |
170 { | 171 { |
171 let filters = filtersByDomain.get(currentDomain); | 172 let filters = filtersByDomain.get(currentDomain); |
172 if (filters) | 173 if (filters) |
173 { | 174 { |
174 for (let [filter, isIncluded] of filters) | 175 for (let [text, selector] of filters) |
175 { | 176 { |
176 if (!isIncluded) | 177 if (!selector) |
177 { | 178 { |
178 excluded.add(filter); | 179 excluded.add(text); |
179 } | 180 } |
180 else | 181 else if ((excluded.size == 0 || !excluded.has(text)) && |
| 182 !ElemHideExceptions.getException(selector, domain)) |
181 { | 183 { |
182 let {selector} = filter; | 184 selectors.push(selector); |
183 if ((excluded.size == 0 || !excluded.has(filter)) && | |
184 !ElemHideExceptions.getException(selector, domain)) | |
185 { | |
186 selectors.push(selector); | |
187 } | |
188 } | 185 } |
189 } | 186 } |
190 } | 187 } |
191 } | 188 } |
192 | 189 |
193 return selectors; | 190 return selectors; |
194 } | 191 } |
195 | 192 |
196 /** | 193 /** |
197 * Returns the list of selectors and the list of exceptions that apply on a | 194 * Returns the list of selectors and the list of exceptions that apply on a |
(...skipping 12 matching lines...) Expand all Loading... |
210 let selectors = []; | 207 let selectors = []; |
211 let exceptions = []; | 208 let exceptions = []; |
212 | 209 |
213 let excluded = new Set(); | 210 let excluded = new Set(); |
214 | 211 |
215 for (let currentDomain of domainSuffixes(domain, !specificOnly)) | 212 for (let currentDomain of domainSuffixes(domain, !specificOnly)) |
216 { | 213 { |
217 let filters = filtersByDomain.get(currentDomain); | 214 let filters = filtersByDomain.get(currentDomain); |
218 if (filters) | 215 if (filters) |
219 { | 216 { |
220 for (let [filter, isIncluded] of filters) | 217 for (let [text, selector] of filters) |
221 { | 218 { |
222 if (!isIncluded) | 219 if (!selector) |
223 { | 220 { |
224 excluded.add(filter); | 221 excluded.add(text); |
225 } | 222 } |
226 else if (excluded.size == 0 || !excluded.has(filter)) | 223 else if (excluded.size == 0 || !excluded.has(text)) |
227 { | 224 { |
228 let {selector} = filter; | |
229 let exception = ElemHideExceptions.getException(selector, domain); | 225 let exception = ElemHideExceptions.getException(selector, domain); |
230 | 226 |
231 if (exception) | 227 if (exception) |
232 exceptions.push(exception); | 228 exceptions.push(exception); |
233 else | 229 else |
234 selectors.push(selector); | 230 selectors.push(selector); |
235 } | 231 } |
236 } | 232 } |
237 } | 233 } |
238 } | 234 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 if (domain != "") | 296 if (domain != "") |
301 knownExceptionDomains.add(domain); | 297 knownExceptionDomains.add(domain); |
302 } | 298 } |
303 } | 299 } |
304 | 300 |
305 // If this is the first exception for a previously unconditionally applied | 301 // If this is the first exception for a previously unconditionally applied |
306 // element hiding selector we need to take care to update the lookups. | 302 // element hiding selector we need to take care to update the lookups. |
307 let unconditionalFilterForSelector = filterBySelector.get(selector); | 303 let unconditionalFilterForSelector = filterBySelector.get(selector); |
308 if (unconditionalFilterForSelector) | 304 if (unconditionalFilterForSelector) |
309 { | 305 { |
310 addToFiltersByDomain(unconditionalFilterForSelector); | 306 addToFiltersByDomain(Filter.fromText(unconditionalFilterForSelector, |
| 307 false)); |
311 filterBySelector.delete(selector); | 308 filterBySelector.delete(selector); |
312 unconditionalSelectors = null; | 309 unconditionalSelectors = null; |
313 defaultStyleSheet = null; | 310 defaultStyleSheet = null; |
314 } | 311 } |
315 }); | 312 }); |
316 | 313 |
317 /** | 314 /** |
318 * Container for element hiding filters | 315 * Container for element hiding filters |
319 * @class | 316 * @class |
320 */ | 317 */ |
(...skipping 16 matching lines...) Expand all Loading... |
337 | 334 |
338 filterNotifier.emit("elemhideupdate"); | 335 filterNotifier.emit("elemhideupdate"); |
339 }, | 336 }, |
340 | 337 |
341 /** | 338 /** |
342 * Add a new element hiding filter | 339 * Add a new element hiding filter |
343 * @param {ElemHideFilter} filter | 340 * @param {ElemHideFilter} filter |
344 */ | 341 */ |
345 add(filter) | 342 add(filter) |
346 { | 343 { |
347 if (knownFilters.has(filter)) | 344 let {text} = filter; |
| 345 |
| 346 if (knownFilters.has(text)) |
348 return; | 347 return; |
349 | 348 |
350 styleSheetCache.clear(); | 349 styleSheetCache.clear(); |
351 commonStyleSheet = null; | 350 commonStyleSheet = null; |
352 | 351 |
353 let {domains, selector} = filter; | 352 let {domains, selector} = filter; |
354 | 353 |
355 if (!(domains || ElemHideExceptions.hasExceptions(selector))) | 354 if (!(domains || ElemHideExceptions.hasExceptions(selector))) |
356 { | 355 { |
357 // The new filter's selector is unconditionally applied to all domains | 356 // The new filter's selector is unconditionally applied to all domains |
358 filterBySelector.set(selector, filter); | 357 filterBySelector.set(selector, text); |
359 unconditionalSelectors = null; | 358 unconditionalSelectors = null; |
360 defaultStyleSheet = null; | 359 defaultStyleSheet = null; |
361 } | 360 } |
362 else | 361 else |
363 { | 362 { |
364 // The new filter's selector only applies to some domains | 363 // The new filter's selector only applies to some domains |
365 addToFiltersByDomain(filter, domains); | 364 addToFiltersByDomain(filter, domains); |
366 } | 365 } |
367 | 366 |
368 knownFilters.add(filter); | 367 knownFilters.add(text); |
369 filterNotifier.emit("elemhideupdate"); | 368 filterNotifier.emit("elemhideupdate"); |
370 }, | 369 }, |
371 | 370 |
372 /** | 371 /** |
373 * Removes an element hiding filter | 372 * Removes an element hiding filter |
374 * @param {ElemHideFilter} filter | 373 * @param {ElemHideFilter} filter |
375 */ | 374 */ |
376 remove(filter) | 375 remove(filter) |
377 { | 376 { |
378 if (!knownFilters.has(filter)) | 377 let {text} = filter; |
| 378 |
| 379 if (!knownFilters.has(text)) |
379 return; | 380 return; |
380 | 381 |
381 styleSheetCache.clear(); | 382 styleSheetCache.clear(); |
382 commonStyleSheet = null; | 383 commonStyleSheet = null; |
383 | 384 |
384 let {selector} = filter; | 385 let {selector} = filter; |
385 | 386 |
386 // Unconditially applied element hiding filters | 387 // Unconditially applied element hiding filters |
387 if (filterBySelector.get(selector) == filter) | 388 if (filterBySelector.get(selector) == text) |
388 { | 389 { |
389 filterBySelector.delete(selector); | 390 filterBySelector.delete(selector); |
390 unconditionalSelectors = null; | 391 unconditionalSelectors = null; |
391 defaultStyleSheet = null; | 392 defaultStyleSheet = null; |
392 } | 393 } |
393 // Conditionally applied element hiding filters | 394 // Conditionally applied element hiding filters |
394 else | 395 else |
395 { | 396 { |
396 let domains = filter.domains || defaultDomains; | 397 let domains = filter.domains || defaultDomains; |
397 for (let domain of domains.keys()) | 398 for (let domain of domains.keys()) |
398 { | 399 { |
399 let filters = filtersByDomain.get(domain); | 400 let filters = filtersByDomain.get(domain); |
400 if (filters) | 401 if (filters) |
401 { | 402 { |
402 filters.delete(filter); | 403 filters.delete(text); |
403 | 404 |
404 if (filters.size == 0) | 405 if (filters.size == 0) |
405 filtersByDomain.delete(domain); | 406 filtersByDomain.delete(domain); |
406 } | 407 } |
407 } | 408 } |
408 } | 409 } |
409 | 410 |
410 knownFilters.delete(filter); | 411 knownFilters.delete(text); |
411 filterNotifier.emit("elemhideupdate"); | 412 filterNotifier.emit("elemhideupdate"); |
412 }, | 413 }, |
413 | 414 |
414 /** | 415 /** |
415 * @typedef {object} ElemHideStyleSheet | 416 * @typedef {object} ElemHideStyleSheet |
416 * @property {string} code CSS code. | 417 * @property {string} code CSS code. |
417 * @property {Array.<string>} selectors List of selectors. | 418 * @property {Array.<string>} selectors List of selectors. |
418 */ | 419 */ |
419 | 420 |
420 /** | 421 /** |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 { | 578 { |
578 let styleSheet = ""; | 579 let styleSheet = ""; |
579 | 580 |
580 for (let selectorGroup of splitSelectors(selectors)) | 581 for (let selectorGroup of splitSelectors(selectors)) |
581 styleSheet += createRule(selectorGroup); | 582 styleSheet += createRule(selectorGroup); |
582 | 583 |
583 return styleSheet; | 584 return styleSheet; |
584 } | 585 } |
585 | 586 |
586 exports.createStyleSheet = createStyleSheet; | 587 exports.createStyleSheet = createStyleSheet; |
OLD | NEW |