| 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"; |
|
hub
2017/10/20 15:37:57
In Firefox Nightly
`document.querySelector("div"
Manish Jethani
2017/10/20 16:16:41
Done.
| |
| 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 extractSubtrees(nodes) | 154 function isDescendantOf(node, subtrees) |
| 150 { | 155 { |
| 151 return nodes.filter( | 156 return subtrees.some(subtree => subtree.contains(node)); |
| 152 (node, index) => !nodes.some( | 157 } |
| 153 // The index comparison is based on the fact that a node always appears | 158 |
| 154 // in the list before any of its descendants. | 159 function* extractAddedSubtrees(mutations) |
| 155 (otherNode, otherIndex) => otherIndex < index && otherNode.contains(node) | 160 { |
| 156 ) | 161 let knownSubtrees = []; |
| 157 ); | 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 } | |
| 158 } | 174 } |
| 159 | 175 |
| 160 function* traverse(nodes) | 176 function* traverse(nodes) |
| 161 { | 177 { |
| 162 for (let node of nodes) | 178 for (let node of nodes) |
| 163 { | 179 { |
| 164 yield* traverse(node.children); | 180 yield* traverse(node.children); |
| 165 yield node; | 181 yield node; |
| 166 } | 182 } |
| 167 } | 183 } |
| (...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 664 niceLoop(traverse(nodes), node => | 680 niceLoop(traverse(nodes), node => |
| 665 { | 681 { |
| 666 let shadowRoot = node.shadowRoot; | 682 let shadowRoot = node.shadowRoot; |
| 667 if (shadowRoot) | 683 if (shadowRoot) |
| 668 this.addShadowRoot(shadowRoot); | 684 this.addShadowRoot(shadowRoot); |
| 669 }); | 685 }); |
| 670 }, | 686 }, |
| 671 | 687 |
| 672 observe(mutations) | 688 observe(mutations) |
| 673 { | 689 { |
| 674 let allAddedElements = []; | 690 if (typeof ShadowRoot != "undefined") |
| 675 for (let mutation of mutations) | 691 { |
| 676 { | 692 // Find any preattached shadows. |
| 677 for (let node of mutation.addedNodes) | 693 this.findShadowRoots(extractAddedSubtrees(mutations)); |
| 678 { | 694 } |
| 679 if (node instanceof Element) | |
|
lainverse
2017/10/20 15:37:07
Actually if nodes are always added in parent -> ch
Manish Jethani
2017/10/20 16:16:41
That's a very good point!
While it is better in t
lainverse
2017/10/20 17:37:36
Most likely that's because arrow function were def
Manish Jethani
2017/10/22 20:01:34
Simply moving the array function out of the loop d
| |
| 680 allAddedElements.push(node); | |
| 681 } | |
| 682 } | |
| 683 | |
| 684 // Find any preattached shadows. | |
| 685 this.findShadowRoots(extractSubtrees(allAddedElements)); | |
| 686 | 695 |
| 687 this.queueFiltering(); | 696 this.queueFiltering(); |
| 688 }, | 697 }, |
| 689 | 698 |
| 690 apply(patterns, parsed) | 699 apply(patterns, parsed) |
| 691 { | 700 { |
| 692 if (parsed) | 701 if (parsed) |
| 693 { | 702 { |
| 694 this.patterns = patterns; | 703 this.patterns = patterns; |
| 695 } | 704 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 714 attributes: true, | 723 attributes: true, |
| 715 characterData: true, | 724 characterData: true, |
| 716 subtree: true | 725 subtree: true |
| 717 } | 726 } |
| 718 ); | 727 ); |
| 719 } | 728 } |
| 720 } | 729 } |
| 721 }; | 730 }; |
| 722 | 731 |
| 723 exports.ElemHideEmulation = ElemHideEmulation; | 732 exports.ElemHideEmulation = ElemHideEmulation; |
| LEFT | RIGHT |