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

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

Issue 29714601: Issue 6437 - Skip elements not affected by DOM mutations (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Disable optimization for patterns containing sibling combinators Created May 11, 2018, 4:18 p.m.
Right Patch Set: Rename debug mode to test mode Created May 18, 2018, 3:41 a.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 const {indexOf} = require("../coreUtils");
22 22
23 let MIN_INVOCATION_INTERVAL = 3000; 23 let MIN_INVOCATION_INTERVAL = 3000;
24 const MAX_SYNCHRONOUS_PROCESSING_TIME = 50; 24 const MAX_SYNCHRONOUS_PROCESSING_TIME = 50;
25 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i; 25 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i;
26 26
27 let testInfo = null;
28
29 function setTestMode()
30 {
31 testInfo = {
32 lastProcessedElements: new Set()
33 };
34 }
35
36 exports.setTestMode = setTestMode;
37
38 function getTestInfo()
39 {
40 return testInfo;
41 }
42
43 exports.getTestInfo = getTestInfo;
hub 2018/05/24 19:19:09 I'm a bit skeptical with that way or test, includi
Manish Jethani 2018/05/25 07:21:25 We can work on improving our test framework, but f
44
27 function getCachedPropertyValue(object, name, defaultValueFunc = () => {}) 45 function getCachedPropertyValue(object, name, defaultValueFunc = () => {})
28 { 46 {
29 let value = object[name]; 47 let value = object[name];
30 if (typeof value == "undefined") 48 if (typeof value == "undefined")
31 Object.defineProperty(object, name, {value: value = defaultValueFunc()}); 49 Object.defineProperty(object, name, {value: value = defaultValueFunc()});
32 return value; 50 return value;
33 } 51 }
34 52
35 /** Return position of node from parent. 53 /** Return position of node from parent.
36 * @param {Node} node the node to find the position of. 54 * @param {Node} node the node to find the position of.
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 let iter = evaluate(this._innerSelectors, 0, "", element, styles, 324 let iter = evaluate(this._innerSelectors, 0, "", element, styles,
307 targets); 325 targets);
308 for (let selector of iter) 326 for (let selector of iter)
309 { 327 {
310 if (selector == null) 328 if (selector == null)
311 yield null; 329 yield null;
312 else if (scopedQuerySelector(element, selector)) 330 else if (scopedQuerySelector(element, selector))
313 yield element; 331 yield element;
314 } 332 }
315 yield null; 333 yield null;
334
335 if (testInfo)
336 testInfo.lastProcessedElements.add(element);
316 } 337 }
317 } 338 }
318 } 339 }
319 }; 340 };
320 341
321 function ContainsSelector(textContent) 342 function ContainsSelector(textContent)
322 { 343 {
323 this._regexp = makeRegExpParameter(textContent); 344 this._regexp = makeRegExpParameter(textContent);
324 } 345 }
325 346
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 target.contains(element))) 381 target.contains(element)))
361 { 382 {
362 yield null; 383 yield null;
363 continue; 384 continue;
364 } 385 }
365 386
366 if (this._regexp && this._regexp.test(element.textContent)) 387 if (this._regexp && this._regexp.test(element.textContent))
367 yield element; 388 yield element;
368 else 389 else
369 yield null; 390 yield null;
391
392 if (testInfo)
393 testInfo.lastProcessedElements.add(element);
370 } 394 }
371 } 395 }
372 } 396 }
373 }; 397 };
374 398
375 function PropsSelector(propertyExpression) 399 function PropsSelector(propertyExpression)
376 { 400 {
377 let regexpString; 401 let regexpString;
378 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && 402 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" &&
379 propertyExpression[propertyExpression.length - 1] == "/") 403 propertyExpression[propertyExpression.length - 1] == "/")
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 let targets = new Set(); 555 let targets = new Set();
532 556
533 for (let mutation of mutations) 557 for (let mutation of mutations)
534 { 558 {
535 if (mutation.type == "childList") 559 if (mutation.type == "childList")
536 { 560 {
537 // When new nodes are added, we're interested in the added nodes rather 561 // When new nodes are added, we're interested in the added nodes rather
538 // than the parent. 562 // than the parent.
539 for (let node of mutation.addedNodes) 563 for (let node of mutation.addedNodes)
540 targets.add(node); 564 targets.add(node);
541
542 // Ideally we would also be interested in removed nodes, but since we
Manish Jethani 2018/05/11 16:32:00 Well we're already using CSS selectors for element
543 // never unhide an element once hidden we can simply ignore any removed
544 // nodes. Note that this will change once we start using CSS selectors
545 // for -abp-has and -abp-contains, i.e. we'll have to remove the
546 // selectors for any removed nodes.
547 } 565 }
548 else 566 else
549 { 567 {
550 targets.add(mutation.target); 568 targets.add(mutation.target);
551 } 569 }
552 } 570 }
553 571
554 return [...targets]; 572 return [...targets];
555 } 573 }
556 574
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
674 * @param {MutationRecord[]} [mutations] 692 * @param {MutationRecord[]} [mutations]
675 * The list of DOM mutations that have been applied to the document and 693 * The list of DOM mutations that have been applied to the document and
676 * made reprocessing necessary. This parameter shouldn't be passed in for 694 * made reprocessing necessary. This parameter shouldn't be passed in for
677 * the initial processing, the entire document will be considered 695 * the initial processing, the entire document will be considered
678 * then and all rules, including the ones not dependent on the DOM. 696 * then and all rules, including the ones not dependent on the DOM.
679 * @param {function} [done] 697 * @param {function} [done]
680 * Callback to call when done. 698 * Callback to call when done.
681 */ 699 */
682 _addSelectors(stylesheets, mutations, done) 700 _addSelectors(stylesheets, mutations, done)
683 { 701 {
702 if (testInfo)
703 testInfo.lastProcessedElements.clear();
704
684 let patterns = filterPatterns(this.patterns, {stylesheets, mutations}); 705 let patterns = filterPatterns(this.patterns, {stylesheets, mutations});
685 706
686 let selectors = []; 707 let selectors = [];
687 let selectorFilters = []; 708 let selectorFilters = [];
688 709
689 let elements = []; 710 let elements = [];
690 let elementFilters = []; 711 let elementFilters = [];
691 712
692 let cssStyles = []; 713 let cssStyles = [];
693 714
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 783
763 pattern = patterns.shift(); 784 pattern = patterns.shift();
764 785
765 let evaluationTargets = targets; 786 let evaluationTargets = targets;
766 787
767 // If the pattern appears to contain any sibling combinators, we can't 788 // If the pattern appears to contain any sibling combinators, we can't
768 // easily optimize based on the mutation targets. Since this is a 789 // easily optimize based on the mutation targets. Since this is a
769 // special case, skip the optimization. By setting it to null here we 790 // special case, skip the optimization. By setting it to null here we
770 // make sure we process the entire DOM. 791 // make sure we process the entire DOM.
771 if (pattern.maybeContainsSiblingCombinators) 792 if (pattern.maybeContainsSiblingCombinators)
793 evaluationTargets = null;
794
795 // Ignore mutation targets when using style sheets, because we may have
796 // to update all the CSS selectors.
797 if (!this.useInlineStyles)
772 evaluationTargets = null; 798 evaluationTargets = null;
773 799
774 generator = evaluate(pattern.selectors, 0, "", 800 generator = evaluate(pattern.selectors, 0, "",
775 this.document, cssStyles, evaluationTargets); 801 this.document, cssStyles, evaluationTargets);
776 } 802 }
777 for (let selector of generator) 803 for (let selector of generator)
778 { 804 {
779 if (selector != null) 805 if (selector != null)
780 { 806 {
781 if (!this.useInlineStyles) 807 if (!this.useInlineStyles)
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
905 931
906 onLoad(event) 932 onLoad(event)
907 { 933 {
908 let stylesheet = event.target.sheet; 934 let stylesheet = event.target.sheet;
909 if (stylesheet) 935 if (stylesheet)
910 this.queueFiltering([stylesheet]); 936 this.queueFiltering([stylesheet]);
911 }, 937 },
912 938
913 observe(mutations) 939 observe(mutations)
914 { 940 {
941 if (testInfo)
942 {
943 // In test mode, filter out any mutations likely done by us
944 // (i.e. style="display: none !important"). This makes it easier to
945 // observe how the code responds to DOM mutations.
946 mutations = mutations.filter(
947 ({type, attributeName, target: {style: newValue}, oldValue}) =>
948 !(type == "attributes" && attributeName == "style" &&
949 newValue.display == "none" && oldValue.display != "none")
950 );
951
952 if (mutations.length == 0)
953 return;
954 }
hub 2018/05/24 19:19:09 ... and this :-/ There should be a better way to
Manish Jethani 2018/05/25 07:21:25 OK, do you have any suggestions? This is only done
955
915 this.queueFiltering(null, mutations); 956 this.queueFiltering(null, mutations);
916 }, 957 },
917 958
918 apply(patterns) 959 apply(patterns)
919 { 960 {
920 this.patterns = []; 961 this.patterns = [];
921 for (let pattern of patterns) 962 for (let pattern of patterns)
922 { 963 {
923 let selectors = this.parseSelector(pattern.selector); 964 let selectors = this.parseSelector(pattern.selector);
924 if (selectors != null && selectors.length > 0) 965 if (selectors != null && selectors.length > 0)
(...skipping 11 matching lines...) Expand all
936 characterData: shouldObserveCharacterData(this.patterns), 977 characterData: shouldObserveCharacterData(this.patterns),
937 subtree: true 978 subtree: true
938 } 979 }
939 ); 980 );
940 this.document.addEventListener("load", this.onLoad.bind(this), true); 981 this.document.addEventListener("load", this.onLoad.bind(this), true);
941 } 982 }
942 } 983 }
943 }; 984 };
944 985
945 exports.ElemHideEmulation = ElemHideEmulation; 986 exports.ElemHideEmulation = ElemHideEmulation;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld