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 |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 yield null; | 220 yield null; |
221 } | 221 } |
222 | 222 |
223 function PlainSelector(selector) | 223 function PlainSelector(selector) |
224 { | 224 { |
225 this._selector = selector; | 225 this._selector = selector; |
226 this.maybeDependsOnAttributes = /[#.]|\[.+\]/.test(selector); | 226 this.maybeDependsOnAttributes = /[#.]|\[.+\]/.test(selector); |
227 } | 227 } |
228 | 228 |
229 PlainSelector.prototype = { | 229 PlainSelector.prototype = { |
| 230 preferHideWithSelector: true, |
| 231 |
230 /** | 232 /** |
231 * Generator function returning a pair of selector | 233 * Generator function returning a pair of selector |
232 * string and subtree. | 234 * string and subtree. |
233 * @param {string} prefix the prefix for the selector. | 235 * @param {string} prefix the prefix for the selector. |
234 * @param {Node} subtree the subtree we work on. | 236 * @param {Node} subtree the subtree we work on. |
235 * @param {StringifiedStyle[]} styles the stringified style objects. | 237 * @param {StringifiedStyle[]} styles the stringified style objects. |
236 */ | 238 */ |
237 *getSelectors(prefix, subtree, styles) | 239 *getSelectors(prefix, subtree, styles) |
238 { | 240 { |
239 yield [prefix + this._selector, subtree]; | 241 yield [prefix + this._selector, subtree]; |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 }, | 458 }, |
457 | 459 |
458 get dependsOnCharacterData() | 460 get dependsOnCharacterData() |
459 { | 461 { |
460 // Observe changes to character data only if there's a contains selector in | 462 // Observe changes to character data only if there's a contains selector in |
461 // one of the patterns. | 463 // one of the patterns. |
462 return getCachedPropertyValue( | 464 return getCachedPropertyValue( |
463 this, "_dependsOnCharacterData", | 465 this, "_dependsOnCharacterData", |
464 () => this.selectors.some(selector => selector.dependsOnCharacterData) | 466 () => this.selectors.some(selector => selector.dependsOnCharacterData) |
465 ); | 467 ); |
| 468 }, |
| 469 |
| 470 matchesMutationTypes(mutationTypes) |
| 471 { |
| 472 let mutationTypeMatchMap = getCachedPropertyValue( |
| 473 this, "_mutationTypeMatchMap", |
| 474 () => new Map([ |
| 475 // All types of DOM-dependent patterns are affected by mutations of |
| 476 // type "childList". |
| 477 ["childList", true], |
| 478 ["attributes", this.maybeDependsOnAttributes], |
| 479 ["characterData", this.dependsOnCharacterData] |
| 480 ]) |
| 481 ); |
| 482 |
| 483 for (let mutationType of mutationTypes) |
| 484 { |
| 485 if (mutationTypeMatchMap.get(mutationType)) |
| 486 return true; |
| 487 } |
| 488 |
| 489 return false; |
466 } | 490 } |
467 }; | 491 }; |
468 | 492 |
| 493 function extractMutationTypes(mutations) |
| 494 { |
| 495 let types = new Set(); |
| 496 |
| 497 for (let mutation of mutations) |
| 498 { |
| 499 types.add(mutation.type); |
| 500 |
| 501 // There are only 3 types of mutations: "attributes", "characterData", and |
| 502 // "childList". |
| 503 if (types.size == 3) |
| 504 break; |
| 505 } |
| 506 |
| 507 return types; |
| 508 } |
| 509 |
469 function filterPatterns(patterns, {stylesheets, mutations}) | 510 function filterPatterns(patterns, {stylesheets, mutations}) |
470 { | 511 { |
471 if (!stylesheets && !mutations) | 512 if (!stylesheets && !mutations) |
472 return patterns.slice(); | 513 return patterns.slice(); |
473 | 514 |
| 515 let mutationTypes = mutations ? extractMutationTypes(mutations) : null; |
| 516 |
474 return patterns.filter( | 517 return patterns.filter( |
475 pattern => (stylesheets && pattern.dependsOnStyles) || | 518 pattern => (stylesheets && pattern.dependsOnStyles) || |
476 (mutations && pattern.dependsOnDOM) | 519 (mutations && pattern.dependsOnDOM && |
| 520 pattern.matchesMutationTypes(mutationTypes)) |
477 ); | 521 ); |
478 } | 522 } |
479 | 523 |
480 function shouldObserveAttributes(patterns) | 524 function shouldObserveAttributes(patterns) |
481 { | 525 { |
482 return patterns.some(pattern => pattern.maybeDependsOnAttributes); | 526 return patterns.some(pattern => pattern.maybeDependsOnAttributes); |
483 } | 527 } |
484 | 528 |
485 function shouldObserveCharacterData(patterns) | 529 function shouldObserveCharacterData(patterns) |
486 { | 530 { |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
835 characterData: shouldObserveCharacterData(this.patterns), | 879 characterData: shouldObserveCharacterData(this.patterns), |
836 subtree: true | 880 subtree: true |
837 } | 881 } |
838 ); | 882 ); |
839 this.document.addEventListener("load", this.onLoad.bind(this), true); | 883 this.document.addEventListener("load", this.onLoad.bind(this), true); |
840 } | 884 } |
841 } | 885 } |
842 }; | 886 }; |
843 | 887 |
844 exports.ElemHideEmulation = ElemHideEmulation; | 888 exports.ElemHideEmulation = ElemHideEmulation; |
OLD | NEW |