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

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

Issue 29728579: Issue 6437 - Apply :-abp-contains() to matching roots only (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created March 20, 2018, 10:41 a.m.
Right Patch Set: Update tests Created April 19, 2018, 4:28 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 const {textToRegExp, filterToRegExp, splitSelector} = require("../common"); 20 const {textToRegExp, filterToRegExp, splitSelector} = require("../common");
21 const {indexOf} = require("../coreUtils");
21 22
22 let MIN_INVOCATION_INTERVAL = 3000; 23 let MIN_INVOCATION_INTERVAL = 3000;
23 const MAX_SYNCHRONOUS_PROCESSING_TIME = 50; 24 const MAX_SYNCHRONOUS_PROCESSING_TIME = 50;
24 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i; 25 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i;
25 26
26 function getCachedPropertyValue(object, name, defaultValueFunc = () => {}) 27 function getCachedPropertyValue(object, name, defaultValueFunc = () => {})
27 { 28 {
28 let value = object[name]; 29 let value = object[name];
29 if (typeof value == "undefined") 30 if (typeof value == "undefined")
30 Object.defineProperty(object, name, {value: value = defaultValueFunc()}); 31 Object.defineProperty(object, name, {value: value = defaultValueFunc()});
31 return value; 32 return value;
32 } 33 }
33 34
34 /** Return position of node from parent. 35 /** Return position of node from parent.
35 * @param {Node} node the node to find the position of. 36 * @param {Node} node the node to find the position of.
36 * @return {number} One-based index like for :nth-child(), or 0 on error. 37 * @return {number} One-based index like for :nth-child(), or 0 on error.
37 */ 38 */
38 function positionInParent(node) 39 function positionInParent(node)
39 { 40 {
40 let {children} = node.parentNode; 41 return indexOf(node.parentNode.children, node) + 1;
41 for (let i = 0; i < children.length; i++) 42 }
42 if (children[i] == node) 43
43 return i + 1; 44 function makeSelector(node, selector = "")
44 return 0;
45 }
46
47 function makeSelector(node, selector)
48 { 45 {
49 if (node == null) 46 if (node == null)
50 return null; 47 return null;
51 if (!node.parentElement) 48 if (!node.parentElement)
52 { 49 {
53 let newSelector = ":root"; 50 let newSelector = ":root";
54 if (selector) 51 if (selector)
55 newSelector += " > " + selector; 52 newSelector += " > " + selector;
56 return newSelector; 53 return newSelector;
57 } 54 }
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 get maybeDependsOnAttributes() 266 get maybeDependsOnAttributes()
270 { 267 {
271 return this._innerSelectors.some( 268 return this._innerSelectors.some(
272 selector => selector.maybeDependsOnAttributes 269 selector => selector.maybeDependsOnAttributes
273 ); 270 );
274 }, 271 },
275 272
276 *getSelectors(prefix, subtree, styles) 273 *getSelectors(prefix, subtree, styles)
277 { 274 {
278 for (let element of this.getElements(prefix, subtree, styles)) 275 for (let element of this.getElements(prefix, subtree, styles))
279 yield [makeSelector(element, ""), element]; 276 yield [makeSelector(element), element];
280 }, 277 },
281 278
282 /** 279 /**
283 * Generator function returning selected elements. 280 * Generator function returning selected elements.
284 * @param {string} prefix the prefix for the selector. 281 * @param {string} prefix the prefix for the selector.
285 * @param {Node} subtree the subtree we work on. 282 * @param {Node} subtree the subtree we work on.
286 * @param {StringifiedStyle[]} styles the stringified style objects. 283 * @param {StringifiedStyle[]} styles the stringified style objects.
287 */ 284 */
288 *getElements(prefix, subtree, styles) 285 *getElements(prefix, subtree, styles)
289 { 286 {
(...skipping 24 matching lines...) Expand all
314 } 311 }
315 312
316 ContainsSelector.prototype = { 313 ContainsSelector.prototype = {
317 requiresHiding: true, 314 requiresHiding: true,
318 dependsOnDOM: true, 315 dependsOnDOM: true,
319 dependsOnCharacterData: true, 316 dependsOnCharacterData: true,
320 317
321 *getSelectors(prefix, subtree, styles) 318 *getSelectors(prefix, subtree, styles)
322 { 319 {
323 for (let element of this.getElements(prefix, subtree, styles)) 320 for (let element of this.getElements(prefix, subtree, styles))
324 yield [makeSelector(element, ""), subtree]; 321 yield [makeSelector(element), subtree];
325 }, 322 },
326 323
327 *getElements(prefix, subtree, styles) 324 *getElements(prefix, subtree, styles)
328 { 325 {
329 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 326 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
330 prefix + "*" : prefix; 327 prefix + "*" : prefix;
331 328
332 let elements = scopedQuerySelectorAll(subtree, actualPrefix); 329 let elements = scopedQuerySelectorAll(subtree, actualPrefix);
333 330
334 if (elements) 331 if (elements)
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 { 486 {
490 return patterns.some(pattern => pattern.dependsOnCharacterData); 487 return patterns.some(pattern => pattern.dependsOnCharacterData);
491 } 488 }
492 489
493 function ElemHideEmulation(addSelectorsFunc, hideElemsFunc) 490 function ElemHideEmulation(addSelectorsFunc, hideElemsFunc)
494 { 491 {
495 this.document = document; 492 this.document = document;
496 this.addSelectorsFunc = addSelectorsFunc; 493 this.addSelectorsFunc = addSelectorsFunc;
497 this.hideElemsFunc = hideElemsFunc; 494 this.hideElemsFunc = hideElemsFunc;
498 this.observer = new MutationObserver(this.observe.bind(this)); 495 this.observer = new MutationObserver(this.observe.bind(this));
496 this.useInlineStyles = true;
499 } 497 }
500 498
501 ElemHideEmulation.prototype = { 499 ElemHideEmulation.prototype = {
502 isSameOrigin(stylesheet) 500 isSameOrigin(stylesheet)
503 { 501 {
504 try 502 try
505 { 503 {
506 return new URL(stylesheet.href).origin == this.document.location.origin; 504 return new URL(stylesheet.href).origin == this.document.location.origin;
507 } 505 }
508 catch (e) 506 catch (e)
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 if (mutations && patterns.some(pattern => pattern.dependsOnStylesAndDOM)) 614 if (mutations && patterns.some(pattern => pattern.dependsOnStylesAndDOM))
617 stylesheets = this.document.styleSheets; 615 stylesheets = this.document.styleSheets;
618 616
619 for (let stylesheet of stylesheets || []) 617 for (let stylesheet of stylesheets || [])
620 { 618 {
621 // Explicitly ignore third-party stylesheets to ensure consistent behavior 619 // Explicitly ignore third-party stylesheets to ensure consistent behavior
622 // between Firefox and Chrome. 620 // between Firefox and Chrome.
623 if (!this.isSameOrigin(stylesheet)) 621 if (!this.isSameOrigin(stylesheet))
624 continue; 622 continue;
625 623
626 let rules = stylesheet.cssRules; 624 let rules;
625 try
626 {
627 rules = stylesheet.cssRules;
628 }
629 catch (e)
630 {
631 // On Firefox, there is a chance that an InvalidAccessError
632 // get thrown when accessing cssRules. Just skip the stylesheet
633 // in that case.
634 // See https://searchfox.org/mozilla-central/rev/f65d7528e34ef1a7665b4a1 a7b7cdb1388fcd3aa/layout/style/StyleSheet.cpp#699
635 continue;
636 }
637
627 if (!rules) 638 if (!rules)
628 continue; 639 continue;
629 640
630 for (let rule of rules) 641 for (let rule of rules)
631 { 642 {
632 if (rule.type != rule.STYLE_RULE) 643 if (rule.type != rule.STYLE_RULE)
633 continue; 644 continue;
634 645
635 cssStyles.push(stringifyStyle(rule)); 646 cssStyles.push(stringifyStyle(rule));
636 } 647 }
(...skipping 21 matching lines...) Expand all
658 669
659 pattern = patterns.shift(); 670 pattern = patterns.shift();
660 671
661 generator = evaluate(pattern.selectors, 0, "", 672 generator = evaluate(pattern.selectors, 0, "",
662 this.document, cssStyles); 673 this.document, cssStyles);
663 } 674 }
664 for (let selector of generator) 675 for (let selector of generator)
665 { 676 {
666 if (selector != null) 677 if (selector != null)
667 { 678 {
668 if (pattern.isSelectorHidingOnlyPattern()) 679 if (!this.useInlineStyles ||
680 pattern.isSelectorHidingOnlyPattern())
669 { 681 {
670 selectors.push(selector); 682 selectors.push(selector);
671 selectorFilters.push(pattern.text); 683 selectorFilters.push(pattern.text);
672 } 684 }
673 else 685 else
674 { 686 {
675 for (let element of this.document.querySelectorAll(selector)) 687 for (let element of this.document.querySelectorAll(selector))
676 { 688 {
677 elements.push(element); 689 elements.push(element);
678 elementFilters.push(pattern.text); 690 elementFilters.push(pattern.text);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
730 this.queueFiltering(params.stylesheets, params.mutations); 742 this.queueFiltering(params.stylesheets, params.mutations);
731 } 743 }
732 }; 744 };
733 745
734 if (this._scheduledProcessing) 746 if (this._scheduledProcessing)
735 { 747 {
736 if (!stylesheets && !mutations) 748 if (!stylesheets && !mutations)
737 { 749 {
738 this._scheduledProcessing = {}; 750 this._scheduledProcessing = {};
739 } 751 }
740 else 752 else if (this._scheduledProcessing.stylesheets ||
753 this._scheduledProcessing.mutations)
741 { 754 {
742 if (stylesheets) 755 if (stylesheets)
743 { 756 {
744 if (!this._scheduledProcessing.stylesheets) 757 if (!this._scheduledProcessing.stylesheets)
745 this._scheduledProcessing.stylesheets = []; 758 this._scheduledProcessing.stylesheets = [];
746 this._scheduledProcessing.stylesheets.push(...stylesheets); 759 this._scheduledProcessing.stylesheets.push(...stylesheets);
747 } 760 }
748 if (mutations) 761 if (mutations)
749 { 762 {
750 if (!this._scheduledProcessing.mutations) 763 if (!this._scheduledProcessing.mutations)
(...skipping 16 matching lines...) Expand all
767 this._scheduledProcessing = null; 780 this._scheduledProcessing = null;
768 this._addSelectors(params.stylesheets, params.mutations, completion); 781 this._addSelectors(params.stylesheets, params.mutations, completion);
769 }, 782 },
770 MIN_INVOCATION_INTERVAL - (performance.now() - this._lastInvocation)); 783 MIN_INVOCATION_INTERVAL - (performance.now() - this._lastInvocation));
771 } 784 }
772 else if (this.document.readyState == "loading") 785 else if (this.document.readyState == "loading")
773 { 786 {
774 this._scheduledProcessing = {stylesheets, mutations}; 787 this._scheduledProcessing = {stylesheets, mutations};
775 let handler = () => 788 let handler = () =>
776 { 789 {
777 document.removeEventListener("DOMContentLoaded", handler); 790 this.document.removeEventListener("DOMContentLoaded", handler);
778 let params = Object.assign({}, this._scheduledProcessing); 791 let params = Object.assign({}, this._scheduledProcessing);
779 this._filteringInProgress = true; 792 this._filteringInProgress = true;
780 this._scheduledProcessing = null; 793 this._scheduledProcessing = null;
781 this._addSelectors(params.stylesheets, params.mutations, completion); 794 this._addSelectors(params.stylesheets, params.mutations, completion);
782 }; 795 };
783 document.addEventListener("DOMContentLoaded", handler); 796 this.document.addEventListener("DOMContentLoaded", handler);
784 } 797 }
785 else 798 else
786 { 799 {
787 this._filteringInProgress = true; 800 this._filteringInProgress = true;
788 this._addSelectors(stylesheets, mutations, completion); 801 this._addSelectors(stylesheets, mutations, completion);
789 } 802 }
790 }, 803 },
791 804
792 onLoad(event) 805 onLoad(event)
793 { 806 {
(...skipping 28 matching lines...) Expand all
822 characterData: shouldObserveCharacterData(this.patterns), 835 characterData: shouldObserveCharacterData(this.patterns),
823 subtree: true 836 subtree: true
824 } 837 }
825 ); 838 );
826 this.document.addEventListener("load", this.onLoad.bind(this), true); 839 this.document.addEventListener("load", this.onLoad.bind(this), true);
827 } 840 }
828 } 841 }
829 }; 842 };
830 843
831 exports.ElemHideEmulation = ElemHideEmulation; 844 exports.ElemHideEmulation = ElemHideEmulation;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld