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 |
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 Loading... | |
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 Loading... | |
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 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
668 * @param {MutationRecord[]} [mutations] | 692 * @param {MutationRecord[]} [mutations] |
669 * 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 |
670 * made reprocessing necessary. This parameter shouldn't be passed in for | 694 * made reprocessing necessary. This parameter shouldn't be passed in for |
671 * the initial processing, the entire document will be considered | 695 * the initial processing, the entire document will be considered |
672 * 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. |
673 * @param {function} [done] | 697 * @param {function} [done] |
674 * Callback to call when done. | 698 * Callback to call when done. |
675 */ | 699 */ |
676 _addSelectors(stylesheets, mutations, done) | 700 _addSelectors(stylesheets, mutations, done) |
677 { | 701 { |
702 if (testInfo) | |
703 testInfo.lastProcessedElements.clear(); | |
704 | |
678 let patterns = filterPatterns(this.patterns, {stylesheets, mutations}); | 705 let patterns = filterPatterns(this.patterns, {stylesheets, mutations}); |
679 | 706 |
680 let selectors = []; | 707 let selectors = []; |
681 let selectorFilters = []; | 708 let selectorFilters = []; |
682 | 709 |
683 let elements = []; | 710 let elements = []; |
684 let elementFilters = []; | 711 let elementFilters = []; |
685 | 712 |
686 let cssStyles = []; | 713 let cssStyles = []; |
687 | 714 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
756 | 783 |
757 pattern = patterns.shift(); | 784 pattern = patterns.shift(); |
758 | 785 |
759 let evaluationTargets = targets; | 786 let evaluationTargets = targets; |
760 | 787 |
761 // 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 |
762 // easily optimize based on the mutation targets. Since this is a | 789 // easily optimize based on the mutation targets. Since this is a |
763 // 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 |
764 // make sure we process the entire DOM. | 791 // make sure we process the entire DOM. |
765 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) | |
766 evaluationTargets = null; | 798 evaluationTargets = null; |
767 | 799 |
768 generator = evaluate(pattern.selectors, 0, "", | 800 generator = evaluate(pattern.selectors, 0, "", |
769 this.document, cssStyles, evaluationTargets); | 801 this.document, cssStyles, evaluationTargets); |
770 } | 802 } |
771 for (let selector of generator) | 803 for (let selector of generator) |
772 { | 804 { |
773 if (selector != null) | 805 if (selector != null) |
774 { | 806 { |
775 if (!this.useInlineStyles) | 807 if (!this.useInlineStyles) |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
899 | 931 |
900 onLoad(event) | 932 onLoad(event) |
901 { | 933 { |
902 let stylesheet = event.target.sheet; | 934 let stylesheet = event.target.sheet; |
903 if (stylesheet) | 935 if (stylesheet) |
904 this.queueFiltering([stylesheet]); | 936 this.queueFiltering([stylesheet]); |
905 }, | 937 }, |
906 | 938 |
907 observe(mutations) | 939 observe(mutations) |
908 { | 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 | |
909 this.queueFiltering(null, mutations); | 956 this.queueFiltering(null, mutations); |
910 }, | 957 }, |
911 | 958 |
912 apply(patterns) | 959 apply(patterns) |
913 { | 960 { |
914 this.patterns = []; | 961 this.patterns = []; |
915 for (let pattern of patterns) | 962 for (let pattern of patterns) |
916 { | 963 { |
917 let selectors = this.parseSelector(pattern.selector); | 964 let selectors = this.parseSelector(pattern.selector); |
918 if (selectors != null && selectors.length > 0) | 965 if (selectors != null && selectors.length > 0) |
(...skipping 11 matching lines...) Expand all Loading... | |
930 characterData: shouldObserveCharacterData(this.patterns), | 977 characterData: shouldObserveCharacterData(this.patterns), |
931 subtree: true | 978 subtree: true |
932 } | 979 } |
933 ); | 980 ); |
934 this.document.addEventListener("load", this.onLoad.bind(this), true); | 981 this.document.addEventListener("load", this.onLoad.bind(this), true); |
935 } | 982 } |
936 } | 983 } |
937 }; | 984 }; |
938 | 985 |
939 exports.ElemHideEmulation = ElemHideEmulation; | 986 exports.ElemHideEmulation = ElemHideEmulation; |
LEFT | RIGHT |