Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/content/elemHideEmulation.js

Issue 29559743: Issue 5650 - Apply emulation filters to shadow DOMs Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Sept. 29, 2017, 11:21 p.m.
Right Patch Set: Use pure generator function to extract added subtrees Created Oct. 22, 2017, 7:51 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | test/browser/elemHideEmulation.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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))
Manish Jethani 2017/09/29 23:31:30 BODY has a parent element (HTML), but the topmost
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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 { 142 {
138 if (selector == null) 143 if (selector == null)
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;
152 }
153
154 function isDescendantOf(node, subtrees)
155 {
156 return subtrees.some(subtree => subtree.contains(node));
157 }
158
159 function* extractAddedSubtrees(mutations)
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 }
174 }
175
176 function* traverse(nodes)
177 {
178 for (let node of nodes)
179 {
180 yield* traverse(node.children);
181 yield node;
182 }
183 }
184
185 function niceLoop(iterator, callback)
186 {
187 let loop = () =>
188 {
189 let cycleStart = performance.now();
190
191 for (let next = iterator.next(); !next.done; next = iterator.next())
192 {
193 callback(next.value);
194
195 if (performance.now() - cycleStart > MAX_SYNCHRONOUS_PROCESSING_TIME)
196 {
197 setTimeout(loop, 0);
198 return;
199 }
200 }
201 };
202
203 loop();
147 } 204 }
148 205
149 function PlainSelector(selector) 206 function PlainSelector(selector)
150 { 207 {
151 this._selector = selector; 208 this._selector = selector;
152 } 209 }
153 210
154 PlainSelector.prototype = { 211 PlainSelector.prototype = {
155 /** 212 /**
156 * Generator function returning a pair of selector 213 * Generator function returning a pair of selector
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 yield [selector, subtree]; 355 yield [selector, subtree];
299 } 356 }
300 }; 357 };
301 358
302 function isSelectorHidingOnlyPattern(pattern) 359 function isSelectorHidingOnlyPattern(pattern)
303 { 360 {
304 return pattern.selectors.some(s => s.preferHideWithSelector) && 361 return pattern.selectors.some(s => s.preferHideWithSelector) &&
305 !pattern.selectors.some(s => s.requiresHiding); 362 !pattern.selectors.some(s => s.requiresHiding);
306 } 363 }
307 364
308 function ElemHideEmulation(addSelectorsFunc, hideElemsFunc) 365 function ElemHideEmulation(document, root, addSelectorsFunc, hideElemsFunc,
366 shadowAttachedEventType)
309 { 367 {
310 this.document = document; 368 this.document = document;
311 this.root = document; 369 this.root = root || document;
Manish Jethani 2017/09/29 23:31:30 Now we have a root property that can be a document
312 this.addSelectorsFunc = addSelectorsFunc; 370 this.addSelectorsFunc = addSelectorsFunc;
313 this.hideElemsFunc = hideElemsFunc; 371 this.hideElemsFunc = hideElemsFunc;
372 this.shadowAttachedEventType = shadowAttachedEventType;
373 this.patterns = [];
314 this.observer = new MutationObserver(this.observe.bind(this)); 374 this.observer = new MutationObserver(this.observe.bind(this));
315 this.shadowEmulations = new WeakMap(); 375 this.shadowEmulations = new WeakMap();
Manish Jethani 2017/09/29 23:31:30 Using WeakMap here, though I suspect the shadow ro
376
377 if (shadowAttachedEventType)
378 {
379 this.root.addEventListener(shadowAttachedEventType,
380 this.onShadowAttached.bind(this), true);
381 }
382
383 if (this.root == this.document)
384 this.document.addEventListener("load", this.onLoad.bind(this), true);
385 else
386 this.findShadowRoots(this.root.children);
316 } 387 }
317 388
318 ElemHideEmulation.prototype = { 389 ElemHideEmulation.prototype = {
319 isSameOrigin(stylesheet) 390 isSameOrigin(stylesheet)
320 { 391 {
321 try 392 try
322 { 393 {
323 return new URL(stylesheet.href).origin == this.document.location.origin; 394 return new URL(stylesheet.href).origin == this.document.location.origin;
324 } 395 }
325 catch (e) 396 catch (e)
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 } 629 }
559 else 630 else
560 { 631 {
561 this._filteringInProgress = true; 632 this._filteringInProgress = true;
562 this._addSelectors(stylesheets, completion); 633 this._addSelectors(stylesheets, completion);
563 } 634 }
564 }, 635 },
565 636
566 onLoad(event) 637 onLoad(event)
567 { 638 {
639 this.findShadowRoots(this.root.children);
640
641 if (this.patterns.length == 0)
642 return;
643
568 let stylesheet = event.target.sheet; 644 let stylesheet = event.target.sheet;
569 if (stylesheet) 645 if (stylesheet)
570 this.queueFiltering([stylesheet]); 646 this.queueFiltering([stylesheet]);
571 }, 647 },
572 648
573 onShadowAttached(event) 649 onShadowAttached(event)
574 { 650 {
575 // The shadow root won't be available if it's a closed shadow root. Even 651 event.stopImmediatePropagation();
576 // though we override Element.attachShadow to create an open shadow root, 652
577 // it is possible that some other extension has overridden it before us to 653 if (this.patterns.length == 0)
578 // create a closed shadow root. 654 return;
655
656 // The shadow root may not be available if it's a closed shadow root.
579 let shadowRoot = event.target.shadowRoot; 657 let shadowRoot = event.target.shadowRoot;
580 if (!shadowRoot) 658 if (!shadowRoot)
581 return; 659 return;
582 660
583 this.addShadowRoot(shadowRoot); 661 this.addShadowRoot(shadowRoot);
584 }, 662 },
585 663
586 addShadowRoot(shadowRoot) 664 addShadowRoot(shadowRoot)
587 { 665 {
588 if (!this.shadowEmulations.has(shadowRoot)) 666 if (!this.shadowEmulations.has(shadowRoot))
589 { 667 {
590 let emulation = new ElemHideEmulation(this.addSelectorsFunc, 668 let emulation = new ElemHideEmulation(this.document,
591 this.hideElemsFunc); 669 shadowRoot,
670 this.addSelectorsFunc,
671 this.hideElemsFunc,
672 this.shadowAttachedEventType);
592 this.shadowEmulations.set(shadowRoot, emulation); 673 this.shadowEmulations.set(shadowRoot, emulation);
593 emulation.root = shadowRoot;
594 emulation.apply(this.patterns, true); 674 emulation.apply(this.patterns, true);
595 } 675 }
596 }, 676 },
597 677
598 findShadowRoots(node) 678 findShadowRoots(nodes)
599 { 679 {
600 let shadowRoot = node.shadowRoot; 680 niceLoop(traverse(nodes), node =>
601 if (shadowRoot) 681 {
602 this.addShadowRoot(shadowRoot); 682 let shadowRoot = node.shadowRoot;
603 683 if (shadowRoot)
604 for (let child of node.children) 684 this.addShadowRoot(shadowRoot);
Manish Jethani 2017/09/29 23:43:56 We could use a generator and setTimeout here to ke
605 this.findShadowRoots(child); 685 });
606 }, 686 },
607 687
608 observe(mutations) 688 observe(mutations)
609 { 689 {
610 for (let mutation of mutations) 690 if (typeof ShadowRoot != "undefined")
Manish Jethani 2017/09/29 23:31:30 We listen for "shadowAttached" on the document, bu
611 { 691 {
612 // Find any preattached shadows. 692 // Find any preattached shadows.
613 for (let node of mutation.addedNodes) 693 this.findShadowRoots(extractAddedSubtrees(mutations));
614 this.findShadowRoots(node);
615 } 694 }
616 695
617 this.queueFiltering(); 696 this.queueFiltering();
618 }, 697 },
619 698
620 apply(patterns, parsed) 699 apply(patterns, parsed)
621 { 700 {
622 if (parsed) 701 if (parsed)
623 { 702 {
624 this.patterns = patterns; 703 this.patterns = patterns;
(...skipping 14 matching lines...) Expand all
639 this.queueFiltering(); 718 this.queueFiltering();
640 this.observer.observe( 719 this.observer.observe(
641 this.root, 720 this.root,
642 { 721 {
643 childList: true, 722 childList: true,
644 attributes: true, 723 attributes: true,
645 characterData: true, 724 characterData: true,
646 subtree: true 725 subtree: true
647 } 726 }
648 ); 727 );
649
650 if (this.root == this.document)
Manish Jethani 2017/09/29 23:31:30 Listen for these events only if this is the main i
651 {
652 this.document.addEventListener("load", this.onLoad.bind(this), true);
653 this.document.addEventListener("shadowAttached",
654 this.onShadowAttached.bind(this), true);
655 }
656 } 728 }
657 } 729 }
658 }; 730 };
659 731
660 exports.ElemHideEmulation = ElemHideEmulation; 732 exports.ElemHideEmulation = ElemHideEmulation;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld