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

Side by Side Diff: lib/content/elemHideEmulation.js

Issue 29559743: Issue 5650 - Apply emulation filters to shadow DOMs Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Prevent author code from intercepting shadowAttached event Created Sept. 30, 2017, 12:20 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/browser/elemHideEmulation.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 22 matching lines...) Expand all
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 makeSelector(node, selector) 39 function makeSelector(node, selector)
40 { 40 {
41 if (node == null) 41 if (node == null)
42 return null; 42 return null;
43 if (!node.parentElement) 43
44 // If this is the topmost element in a shadow DOM, climb up one more level
45 // and then use a ":host" prefix.
46 if (!node.parentElement && !(node.parentNode instanceof ShadowRoot))
44 { 47 {
45 let newSelector = ":root"; 48 let newSelector = node instanceof ShadowRoot ? ":host" : ":root";
46 if (selector) 49 if (selector)
47 newSelector += " > " + selector; 50 newSelector += " > " + selector;
48 return newSelector; 51 return newSelector;
49 } 52 }
50 let idx = positionInParent(node); 53 let idx = positionInParent(node);
51 if (idx > 0) 54 if (idx > 0)
52 { 55 {
53 let newSelector = `${node.tagName}:nth-child(${idx})`; 56 let newSelector = `${node.tagName}:nth-child(${idx})`;
54 if (selector) 57 if (selector)
55 newSelector += " > " + selector; 58 newSelector += " > " + selector;
56 return makeSelector(node.parentElement, newSelector); 59 return makeSelector(node.parentElement || node.parentNode, newSelector);
57 } 60 }
58 61
59 return selector; 62 return selector;
60 } 63 }
61 64
62 function parseSelectorContent(content, startIndex) 65 function parseSelectorContent(content, startIndex)
63 { 66 {
64 let parens = 1; 67 let parens = 1;
65 let quote = null; 68 let quote = null;
66 let i = startIndex; 69 let i = startIndex;
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 yield [selector, subtree]; 298 yield [selector, subtree];
296 } 299 }
297 }; 300 };
298 301
299 function isSelectorHidingOnlyPattern(pattern) 302 function isSelectorHidingOnlyPattern(pattern)
300 { 303 {
301 return pattern.selectors.some(s => s.preferHideWithSelector) && 304 return pattern.selectors.some(s => s.preferHideWithSelector) &&
302 !pattern.selectors.some(s => s.requiresHiding); 305 !pattern.selectors.some(s => s.requiresHiding);
303 } 306 }
304 307
305 function ElemHideEmulation(addSelectorsFunc, hideElemsFunc) 308 function ElemHideEmulation(document, root, addSelectorsFunc, hideElemsFunc)
Manish Jethani 2017/09/30 12:29:01 It's best to pass these in the constructor as we n
306 { 309 {
307 this.document = document; 310 this.document = document;
311 this.root = root || document;
308 this.addSelectorsFunc = addSelectorsFunc; 312 this.addSelectorsFunc = addSelectorsFunc;
309 this.hideElemsFunc = hideElemsFunc; 313 this.hideElemsFunc = hideElemsFunc;
310 this.observer = new MutationObserver(this.observe.bind(this)); 314 this.observer = new MutationObserver(this.observe.bind(this));
315 this.shadowEmulations = new WeakMap();
316
317 if (this.root == this.document)
Manish Jethani 2017/09/30 12:29:01 We have to listen for these events as early as pos
318 {
319 this.document.addEventListener("load", this.onLoad.bind(this), true);
320 this.document.addEventListener("shadowAttached",
321 this.onShadowAttached.bind(this), true);
322 }
311 } 323 }
312 324
313 ElemHideEmulation.prototype = { 325 ElemHideEmulation.prototype = {
314 isSameOrigin(stylesheet) 326 isSameOrigin(stylesheet)
315 { 327 {
316 try 328 try
317 { 329 {
318 return new URL(stylesheet.href).origin == this.document.location.origin; 330 return new URL(stylesheet.href).origin == this.document.location.origin;
319 } 331 }
320 catch (e) 332 catch (e)
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 let selectors = []; 414 let selectors = [];
403 let selectorFilters = []; 415 let selectorFilters = [];
404 416
405 let elements = []; 417 let elements = [];
406 let elementFilters = []; 418 let elementFilters = [];
407 419
408 let cssStyles = []; 420 let cssStyles = [];
409 421
410 let stylesheetOnlyChange = !!stylesheets; 422 let stylesheetOnlyChange = !!stylesheets;
411 if (!stylesheets) 423 if (!stylesheets)
412 stylesheets = this.document.styleSheets; 424 stylesheets = this.root.styleSheets;
413 425
414 for (let stylesheet of stylesheets) 426 for (let stylesheet of stylesheets)
415 { 427 {
416 // Explicitly ignore third-party stylesheets to ensure consistent behavior 428 // Explicitly ignore third-party stylesheets to ensure consistent behavior
417 // between Firefox and Chrome. 429 // between Firefox and Chrome.
418 if (!this.isSameOrigin(stylesheet)) 430 if (!this.isSameOrigin(stylesheet))
419 continue; 431 continue;
420 432
421 let rules = stylesheet.cssRules; 433 let rules = stylesheet.cssRules;
422 if (!rules) 434 if (!rules)
(...skipping 29 matching lines...) Expand all
452 464
453 pattern = patterns.shift(); 465 pattern = patterns.shift();
454 466
455 if (stylesheetOnlyChange && 467 if (stylesheetOnlyChange &&
456 !pattern.selectors.some(selector => selector.dependsOnStyles)) 468 !pattern.selectors.some(selector => selector.dependsOnStyles))
457 { 469 {
458 pattern = null; 470 pattern = null;
459 return processPatterns(); 471 return processPatterns();
460 } 472 }
461 generator = evaluate(pattern.selectors, 0, "", 473 generator = evaluate(pattern.selectors, 0, "",
462 this.document, cssStyles); 474 this.root, cssStyles);
463 } 475 }
464 for (let selector of generator) 476 for (let selector of generator)
465 { 477 {
466 if (selector != null) 478 if (selector != null)
467 { 479 {
468 if (isSelectorHidingOnlyPattern(pattern)) 480 if (isSelectorHidingOnlyPattern(pattern))
469 { 481 {
470 selectors.push(selector); 482 selectors.push(selector);
471 selectorFilters.push(pattern.text); 483 selectorFilters.push(pattern.text);
472 } 484 }
473 else 485 else
474 { 486 {
475 for (let element of this.document.querySelectorAll(selector)) 487 for (let element of this.root.querySelectorAll(selector))
476 { 488 {
477 elements.push(element); 489 elements.push(element);
478 elementFilters.push(pattern.text); 490 elementFilters.push(pattern.text);
479 } 491 }
480 } 492 }
481 } 493 }
482 if (performance.now() - cycleStart > MAX_SYNCHRONOUS_PROCESSING_TIME) 494 if (performance.now() - cycleStart > MAX_SYNCHRONOUS_PROCESSING_TIME)
483 { 495 {
484 setTimeout(processPatterns, 0); 496 setTimeout(processPatterns, 0);
485 return; 497 return;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 } 565 }
554 else 566 else
555 { 567 {
556 this._filteringInProgress = true; 568 this._filteringInProgress = true;
557 this._addSelectors(stylesheets, completion); 569 this._addSelectors(stylesheets, completion);
558 } 570 }
559 }, 571 },
560 572
561 onLoad(event) 573 onLoad(event)
562 { 574 {
575 if (this.patterns.length == 0)
576 return;
577
563 let stylesheet = event.target.sheet; 578 let stylesheet = event.target.sheet;
564 if (stylesheet) 579 if (stylesheet)
565 this.queueFiltering([stylesheet]); 580 this.queueFiltering([stylesheet]);
566 }, 581 },
567 582
583 onShadowAttached(event)
584 {
585 event.stopImmediatePropagation();
Manish Jethani 2017/09/30 12:29:01 Prevent any author code from detecting even the pr
lainverse 2017/10/02 14:11:55 What will stop page from sending own events with t
Manish Jethani 2017/10/02 14:28:11 That's a good point, but the event would have to b
lainverse 2017/10/02 16:05:39 What about a proper DOM element with Proxy with ge
Manish Jethani 2017/10/02 16:12:31 A content script has its own copy of these APIs, a
586
587 if (this.patterns.length == 0)
588 return;
589
590 // The shadow root won't be available if it's a closed shadow root. Even
591 // though we override Element.attachShadow to create an open shadow root,
592 // it is possible that some other extension has overridden it before us to
593 // create a closed shadow root.
594 let shadowRoot = event.target.shadowRoot;
595 if (!shadowRoot)
596 return;
597
598 this.addShadowRoot(shadowRoot);
599 },
600
601 addShadowRoot(shadowRoot)
602 {
603 if (!this.shadowEmulations.has(shadowRoot))
604 {
605 let emulation = new ElemHideEmulation(this.document,
606 shadowRoot,
607 this.addSelectorsFunc,
608 this.hideElemsFunc);
609 this.shadowEmulations.set(shadowRoot, emulation);
610 emulation.apply(this.patterns, true);
611 }
612 },
613
614 findShadowRoots(node)
615 {
616 let shadowRoot = node.shadowRoot;
617 if (shadowRoot)
618 this.addShadowRoot(shadowRoot);
619
620 for (let child of node.children)
621 this.findShadowRoots(child);
622 },
623
568 observe(mutations) 624 observe(mutations)
569 { 625 {
626 for (let mutation of mutations)
627 {
628 // Find any preattached shadows.
629 for (let node of mutation.addedNodes)
630 this.findShadowRoots(node);
631 }
632
570 this.queueFiltering(); 633 this.queueFiltering();
571 }, 634 },
572 635
573 apply(patterns) 636 apply(patterns, parsed)
574 { 637 {
575 this.patterns = []; 638 if (parsed)
576 for (let pattern of patterns)
577 { 639 {
578 let selectors = this.parseSelector(pattern.selector); 640 this.patterns = patterns;
579 if (selectors != null && selectors.length > 0) 641 }
580 this.patterns.push({selectors, text: pattern.text}); 642 else
643 {
644 this.patterns = [];
645 for (let pattern of patterns)
646 {
647 let selectors = this.parseSelector(pattern.selector);
648 if (selectors != null && selectors.length > 0)
649 this.patterns.push({selectors, text: pattern.text});
650 }
581 } 651 }
582 652
583 if (this.patterns.length > 0) 653 if (this.patterns.length > 0)
584 { 654 {
585 this.queueFiltering(); 655 this.queueFiltering();
586 this.observer.observe( 656 this.observer.observe(
587 this.document, 657 this.root,
588 { 658 {
589 childList: true, 659 childList: true,
590 attributes: true, 660 attributes: true,
591 characterData: true, 661 characterData: true,
592 subtree: true 662 subtree: true
593 } 663 }
594 ); 664 );
595 this.document.addEventListener("load", this.onLoad.bind(this), true);
596 } 665 }
597 } 666 }
598 }; 667 };
599 668
600 exports.ElemHideEmulation = ElemHideEmulation; 669 exports.ElemHideEmulation = ElemHideEmulation;
OLDNEW
« no previous file with comments | « no previous file | test/browser/elemHideEmulation.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld