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

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

Issue 29713565: Issue 6437 - Filter out patterns that do not match DOM mutations (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Process plain selectors on DOM mutations Created April 24, 2018, 1:56 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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 // Just in case the getSelectors() generator above had to run some heavy 217 // Just in case the getSelectors() generator above had to run some heavy
218 // document.querySelectorAll() call which didn't produce any results, make 218 // document.querySelectorAll() call which didn't produce any results, make
219 // sure there is at least one point where execution can pause. 219 // sure there is at least one point where execution can pause.
220 yield null; 220 yield null;
221 } 221 }
222 222
223 function PlainSelector(selector) 223 function PlainSelector(selector)
224 { 224 {
225 this._selector = selector; 225 this._selector = selector;
226 this.maybeDependsOnAttributes = /[#.]|\[.+\]/.test(selector); 226 this.maybeDependsOnAttributes = /[#.]|\[.+\]/.test(selector);
227 this.dependsOnDOM = this.maybeDependsOnAttributes;
Manish Jethani 2018/04/24 13:58:32 OK, since Trac #6618 is rejected we have to proces
227 } 228 }
228 229
229 PlainSelector.prototype = { 230 PlainSelector.prototype = {
230 /** 231 /**
231 * Generator function returning a pair of selector 232 * Generator function returning a pair of selector
232 * string and subtree. 233 * string and subtree.
233 * @param {string} prefix the prefix for the selector. 234 * @param {string} prefix the prefix for the selector.
234 * @param {Node} subtree the subtree we work on. 235 * @param {Node} subtree the subtree we work on.
235 * @param {StringifiedStyle[]} styles the stringified style objects. 236 * @param {StringifiedStyle[]} styles the stringified style objects.
236 */ 237 */
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 }, 457 },
457 458
458 get dependsOnCharacterData() 459 get dependsOnCharacterData()
459 { 460 {
460 // Observe changes to character data only if there's a contains selector in 461 // Observe changes to character data only if there's a contains selector in
461 // one of the patterns. 462 // one of the patterns.
462 return getCachedPropertyValue( 463 return getCachedPropertyValue(
463 this, "_dependsOnCharacterData", 464 this, "_dependsOnCharacterData",
464 () => this.selectors.some(selector => selector.dependsOnCharacterData) 465 () => this.selectors.some(selector => selector.dependsOnCharacterData)
465 ); 466 );
467 },
468
469 matchesMutationTypes(mutationTypes)
470 {
471 let mutationTypeMatchMap = getCachedPropertyValue(
472 this, "_mutationTypeMatchMap",
473 () => new Map([
474 // All types of DOM-dependent patterns are affected by mutations of
475 // type "childList".
476 ["childList", true],
477 ["attributes", this.maybeDependsOnAttributes],
478 ["characterData", this.dependsOnCharacterData]
479 ])
480 );
481
482 for (let mutationType of mutationTypes)
483 {
484 if (mutationTypeMatchMap.get(mutationType))
485 return true;
486 }
487
488 return false;
466 } 489 }
467 }; 490 };
468 491
492 function extractMutationTypes(mutations)
493 {
494 let types = new Set();
495
496 for (let mutation of mutations)
497 {
498 types.add(mutation.type);
499
500 // There are only 3 types of mutations: "attributes", "characterData", and
501 // "childList".
502 if (types.size == 3)
503 break;
504 }
505
506 return types;
507 }
508
469 function filterPatterns(patterns, {stylesheets, mutations}) 509 function filterPatterns(patterns, {stylesheets, mutations})
470 { 510 {
471 if (!stylesheets && !mutations) 511 if (!stylesheets && !mutations)
472 return patterns.slice(); 512 return patterns.slice();
473 513
514 let mutationTypes = mutations ? extractMutationTypes(mutations) : null;
515
474 return patterns.filter( 516 return patterns.filter(
475 pattern => (stylesheets && pattern.dependsOnStyles) || 517 pattern => (stylesheets && pattern.dependsOnStyles) ||
476 (mutations && pattern.dependsOnDOM) 518 (mutations && pattern.dependsOnDOM &&
519 pattern.matchesMutationTypes(mutationTypes))
477 ); 520 );
478 } 521 }
479 522
480 function shouldObserveAttributes(patterns) 523 function shouldObserveAttributes(patterns)
481 { 524 {
482 return patterns.some(pattern => pattern.maybeDependsOnAttributes); 525 return patterns.some(pattern => pattern.maybeDependsOnAttributes);
483 } 526 }
484 527
485 function shouldObserveCharacterData(patterns) 528 function shouldObserveCharacterData(patterns)
486 { 529 {
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
835 characterData: shouldObserveCharacterData(this.patterns), 878 characterData: shouldObserveCharacterData(this.patterns),
836 subtree: true 879 subtree: true
837 } 880 }
838 ); 881 );
839 this.document.addEventListener("load", this.onLoad.bind(this), true); 882 this.document.addEventListener("load", this.onLoad.bind(this), true);
840 } 883 }
841 } 884 }
842 }; 885 };
843 886
844 exports.ElemHideEmulation = ElemHideEmulation; 887 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