Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 18 matching lines...) Expand all Loading... | |
29 */ | 29 */ |
30 function positionInParent(node) | 30 function positionInParent(node) |
31 { | 31 { |
32 let {children} = node.parentNode; | 32 let {children} = node.parentNode; |
33 for (let i = 0; i < children.length; i++) | 33 for (let i = 0; i < children.length; i++) |
34 if (children[i] == node) | 34 if (children[i] == node) |
35 return i + 1; | 35 return i + 1; |
36 return 0; | 36 return 0; |
37 } | 37 } |
38 | 38 |
39 function isShadowRoot(node) | |
40 { | |
41 return typeof ShadowRoot != "undefined" && node instanceof ShadowRoot; | |
42 } | |
43 | |
39 function makeSelector(node, selector) | 44 function makeSelector(node, selector) |
40 { | 45 { |
41 if (node == null) | 46 if (node == null) |
42 return null; | 47 return null; |
43 | 48 |
44 // If this is the topmost element in a shadow DOM, climb up one more level | 49 // If this is the topmost element in a shadow DOM, climb up one more level |
45 // and then use a ":host" prefix. | 50 // and then use a ":host" prefix. |
46 if (!node.parentElement && !(node.parentNode instanceof ShadowRoot)) | 51 if (!node.parentElement && !isShadowRoot(node.parentNode)) |
47 { | 52 { |
48 let newSelector = node instanceof ShadowRoot ? ":host" : ":root"; | 53 let newSelector = isShadowRoot(node) ? ":host" : ":root"; |
49 if (selector) | 54 if (selector) |
50 newSelector += " > " + selector; | 55 newSelector += " > " + selector; |
51 return newSelector; | 56 return newSelector; |
52 } | 57 } |
53 let idx = positionInParent(node); | 58 let idx = positionInParent(node); |
54 if (idx > 0) | 59 if (idx > 0) |
55 { | 60 { |
56 let newSelector = `${node.tagName}:nth-child(${idx})`; | 61 let newSelector = `${node.tagName}:nth-child(${idx})`; |
57 if (selector) | 62 if (selector) |
58 newSelector += " > " + selector; | 63 newSelector += " > " + selector; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 yield null; | 144 yield null; |
140 else | 145 else |
141 yield* evaluate(chain, index + 1, selector, element, styles); | 146 yield* evaluate(chain, index + 1, selector, element, styles); |
142 } | 147 } |
143 // Just in case the getSelectors() generator above had to run some heavy | 148 // Just in case the getSelectors() generator above had to run some heavy |
144 // document.querySelectorAll() call which didn't produce any results, make | 149 // document.querySelectorAll() call which didn't produce any results, make |
145 // sure there is at least one point where execution can pause. | 150 // sure there is at least one point where execution can pause. |
146 yield null; | 151 yield null; |
147 } | 152 } |
148 | 153 |
149 function removeRedundantNodes(nodes) | 154 function isDescendantOf(node, subtrees) |
150 { | 155 { |
151 return nodes.filter( | 156 return subtrees.some(subtree => subtree.contains(node)); |
152 node => !nodes.some( | 157 } |
153 otherNode => otherNode != node && otherNode.contains(node) | 158 |
154 ) | 159 function* extractAddedSubtrees(mutations) |
155 ); | 160 { |
161 let knownSubtrees = []; | |
162 | |
163 for (let mutation of mutations) | |
164 { | |
165 for (let node of mutation.addedNodes) | |
166 { | |
167 if (node instanceof Element && !isDescendantOf(node, knownSubtrees)) | |
168 { | |
169 knownSubtrees.push(node); | |
170 yield node; | |
171 } | |
172 } | |
173 } | |
156 } | 174 } |
157 | 175 |
158 function* traverse(nodes) | 176 function* traverse(nodes) |
159 { | 177 { |
160 for (let node of nodes) | 178 for (let node of nodes) |
161 { | 179 { |
162 yield* traverse(node.children); | 180 yield* traverse(node.children); |
163 yield node; | 181 yield node; |
164 } | 182 } |
165 } | 183 } |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
338 } | 356 } |
339 }; | 357 }; |
340 | 358 |
341 function isSelectorHidingOnlyPattern(pattern) | 359 function isSelectorHidingOnlyPattern(pattern) |
342 { | 360 { |
343 return pattern.selectors.some(s => s.preferHideWithSelector) && | 361 return pattern.selectors.some(s => s.preferHideWithSelector) && |
344 !pattern.selectors.some(s => s.requiresHiding); | 362 !pattern.selectors.some(s => s.requiresHiding); |
345 } | 363 } |
346 | 364 |
347 function ElemHideEmulation(document, root, addSelectorsFunc, hideElemsFunc, | 365 function ElemHideEmulation(document, root, addSelectorsFunc, hideElemsFunc, |
348 shadowAttachedEventType) | 366 shadowAttachedEventType) |
Manish Jethani
2017/10/19 23:44:02
This is the event type, it's a parameter now, incl
| |
349 { | 367 { |
350 this.document = document; | 368 this.document = document; |
351 this.root = root || document; | 369 this.root = root || document; |
352 this.addSelectorsFunc = addSelectorsFunc; | 370 this.addSelectorsFunc = addSelectorsFunc; |
353 this.hideElemsFunc = hideElemsFunc; | 371 this.hideElemsFunc = hideElemsFunc; |
354 this.shadowAttachedEventType = shadowAttachedEventType; | 372 this.shadowAttachedEventType = shadowAttachedEventType; |
355 this.patterns = []; | 373 this.patterns = []; |
356 this.observer = new MutationObserver(this.observe.bind(this)); | 374 this.observer = new MutationObserver(this.observe.bind(this)); |
357 this.shadowEmulations = new WeakMap(); | 375 this.shadowEmulations = new WeakMap(); |
358 | 376 |
359 if (shadowAttachedEventType) | 377 if (shadowAttachedEventType) |
360 { | 378 { |
361 this.root.addEventListener(shadowAttachedEventType, | 379 this.root.addEventListener(shadowAttachedEventType, |
362 this.onShadowAttached.bind(this), true); | 380 this.onShadowAttached.bind(this), true); |
363 } | 381 } |
364 | 382 |
365 if (this.root == this.document) | 383 if (this.root == this.document) |
366 this.document.addEventListener("load", this.onLoad.bind(this), true); | 384 this.document.addEventListener("load", this.onLoad.bind(this), true); |
367 else | 385 else |
368 this.findShadowRoots(this.root.children); | 386 this.findShadowRoots(this.root.children); |
Manish Jethani
2017/10/19 23:44:02
If this root is not the document (it's a shadow ro
| |
369 } | 387 } |
370 | 388 |
371 ElemHideEmulation.prototype = { | 389 ElemHideEmulation.prototype = { |
372 isSameOrigin(stylesheet) | 390 isSameOrigin(stylesheet) |
373 { | 391 { |
374 try | 392 try |
375 { | 393 { |
376 return new URL(stylesheet.href).origin == this.document.location.origin; | 394 return new URL(stylesheet.href).origin == this.document.location.origin; |
377 } | 395 } |
378 catch (e) | 396 catch (e) |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
611 } | 629 } |
612 else | 630 else |
613 { | 631 { |
614 this._filteringInProgress = true; | 632 this._filteringInProgress = true; |
615 this._addSelectors(stylesheets, completion); | 633 this._addSelectors(stylesheets, completion); |
616 } | 634 } |
617 }, | 635 }, |
618 | 636 |
619 onLoad(event) | 637 onLoad(event) |
620 { | 638 { |
621 this.findShadowRoots(this.root.children); | 639 this.findShadowRoots(this.root.children); |
Manish Jethani
2017/10/19 23:44:03
Find shadow roots on the load event.
This is not
| |
622 | 640 |
623 if (this.patterns.length == 0) | 641 if (this.patterns.length == 0) |
624 return; | 642 return; |
625 | 643 |
626 let stylesheet = event.target.sheet; | 644 let stylesheet = event.target.sheet; |
627 if (stylesheet) | 645 if (stylesheet) |
628 this.queueFiltering([stylesheet]); | 646 this.queueFiltering([stylesheet]); |
629 }, | 647 }, |
630 | 648 |
631 onShadowAttached(event) | 649 onShadowAttached(event) |
(...skipping 12 matching lines...) Expand all Loading... | |
644 }, | 662 }, |
645 | 663 |
646 addShadowRoot(shadowRoot) | 664 addShadowRoot(shadowRoot) |
647 { | 665 { |
648 if (!this.shadowEmulations.has(shadowRoot)) | 666 if (!this.shadowEmulations.has(shadowRoot)) |
649 { | 667 { |
650 let emulation = new ElemHideEmulation(this.document, | 668 let emulation = new ElemHideEmulation(this.document, |
651 shadowRoot, | 669 shadowRoot, |
652 this.addSelectorsFunc, | 670 this.addSelectorsFunc, |
653 this.hideElemsFunc, | 671 this.hideElemsFunc, |
654 this.shadowAttachedEventType); | 672 this.shadowAttachedEventType); |
Manish Jethani
2017/10/19 23:44:03
Pass this on.
| |
655 this.shadowEmulations.set(shadowRoot, emulation); | 673 this.shadowEmulations.set(shadowRoot, emulation); |
656 emulation.apply(this.patterns, true); | 674 emulation.apply(this.patterns, true); |
657 } | 675 } |
658 }, | 676 }, |
659 | 677 |
660 findShadowRoots(nodes) | 678 findShadowRoots(nodes) |
661 { | 679 { |
662 niceLoop(traverse(nodes), node => | 680 niceLoop(traverse(nodes), node => |
663 { | 681 { |
664 let shadowRoot = node.shadowRoot; | 682 let shadowRoot = node.shadowRoot; |
665 if (shadowRoot) | 683 if (shadowRoot) |
666 this.addShadowRoot(shadowRoot); | 684 this.addShadowRoot(shadowRoot); |
667 }); | 685 }); |
668 }, | 686 }, |
669 | 687 |
670 observe(mutations) | 688 observe(mutations) |
671 { | 689 { |
672 let allAddedElements = []; | 690 if (typeof ShadowRoot != "undefined") |
673 for (let mutation of mutations) | 691 { |
674 { | 692 // Find any preattached shadows. |
675 for (let node of mutation.addedNodes) | 693 this.findShadowRoots(extractAddedSubtrees(mutations)); |
676 { | 694 } |
677 if (node instanceof Element) | |
678 allAddedElements.push(node); | |
679 } | |
680 } | |
681 | |
682 // Find any preattached shadows. | |
683 this.findShadowRoots(removeRedundantNodes(allAddedElements)); | |
684 | 695 |
685 this.queueFiltering(); | 696 this.queueFiltering(); |
686 }, | 697 }, |
687 | 698 |
688 apply(patterns, parsed) | 699 apply(patterns, parsed) |
689 { | 700 { |
690 if (parsed) | 701 if (parsed) |
691 { | 702 { |
692 this.patterns = patterns; | 703 this.patterns = patterns; |
693 } | 704 } |
(...skipping 18 matching lines...) Expand all Loading... | |
712 attributes: true, | 723 attributes: true, |
713 characterData: true, | 724 characterData: true, |
714 subtree: true | 725 subtree: true |
715 } | 726 } |
716 ); | 727 ); |
717 } | 728 } |
718 } | 729 } |
719 }; | 730 }; |
720 | 731 |
721 exports.ElemHideEmulation = ElemHideEmulation; | 732 exports.ElemHideEmulation = ElemHideEmulation; |
LEFT | RIGHT |