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

Unified Diff: lib/content/elemHideEmulation.js

Issue 29585589: Issue 6423 - Observe only relevant mutation types (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Oct. 22, 2017, 10:51 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/content/elemHideEmulation.js
===================================================================
--- a/lib/content/elemHideEmulation.js
+++ b/lib/content/elemHideEmulation.js
@@ -297,16 +297,44 @@
};
function isSelectorHidingOnlyPattern(pattern)
{
return pattern.selectors.some(s => s.preferHideWithSelector) &&
!pattern.selectors.some(s => s.requiresHiding);
}
+function shouldObserveAttributes(patterns)
+{
+ // Observe changes to attributes if either there's a plain selector that
+ // looks like an attribute selector in one of the patterns
+ // (e.g. "a[href='https://example.com/']")
+ // or there's a properties selector nested inside a has selector
+ // (e.g. "div:-abp-has(:-abp-properties(color: blue))")
+ return patterns.some(
+ pattern => pattern.selectors.some(
+ selector => (selector instanceof PlainSelector &&
+ /\[.+\]/.test(selector._selector))||
Manish Jethani 2017/10/22 22:58:13 This check for attribute selector syntax can give
+ (pattern.selectors[0] instanceof HasSelector &&
Manish Jethani 2017/10/22 22:58:13 I realize that the second check is essentially pat
Manish Jethani 2017/10/23 00:12:09 Done.
+ selector instanceof PropsSelector)
+ )
+ );
+}
+
+function shouldObserveCharacterData(patterns)
+{
+ // Observe changes to character data only if there's a contains selector in
+ // one of the patterns.
+ return patterns.some(
+ pattern => pattern.selectors.some(
+ selector => selector instanceof ContainsSelector
+ )
+ );
+}
+
function ElemHideEmulation(addSelectorsFunc, hideElemsFunc)
{
this.document = document;
this.addSelectorsFunc = addSelectorsFunc;
this.hideElemsFunc = hideElemsFunc;
this.observer = new MutationObserver(this.observe.bind(this));
}
@@ -582,18 +610,18 @@
if (this.patterns.length > 0)
{
this.queueFiltering();
this.observer.observe(
this.document,
{
childList: true,
- attributes: true,
- characterData: true,
+ attributes: shouldObserveAttributes(this.patterns),
+ characterData: shouldObserveCharacterData(this.patterns),
subtree: true
}
);
this.document.addEventListener("load", this.onLoad.bind(this), true);
}
}
};
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld