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

Unified Diff: lib/content/snippets.js

Issue 29851644: Issue 6848 - Add searchSelector parameter to hide-if-contains snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Use Element.closest Created Aug. 10, 2018, 2:54 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/snippets.js
===================================================================
--- a/lib/content/snippets.js
+++ b/lib/content/snippets.js
@@ -92,17 +92,17 @@
*/
function makeInjector(injectable, ...dependencies)
{
return (...args) => injectCode(stringifyFunctionCall(injectable, ...args),
dependencies);
}
/**
- * Hides an HTML element by settings its <code>style</code> attribute to
+ * Hides an HTML element by setting its <code>style</code> attribute to
* <code>display: none !important</code>.
*
* @param {HTMLElement} element The HTML element to hide.
*/
function hideElement(element)
{
element.style.setProperty("display", "none", "important");
@@ -212,27 +212,19 @@
if (!host || !root)
return;
// If the shadow contains the given text, check if the host or one of its
// ancestors matches the selector; if a matching element is found, hide
// it.
if (root.textContent.includes(search))
{
- let element = host;
-
- do
- {
- if (element.matches(selector))
- {
- hideElement(element);
- break;
- }
- }
- while (element = element.parentElement);
+ let closest = host.closest(selector);
+ if (closest)
+ hideElement(closest);
}
}
Object.defineProperty(Element.prototype, "attachShadow", {
value(...args)
{
// Create the shadow root first. It doesn't matter if it's a closed
// shadow root, we keep the reference in a weak map.
@@ -256,31 +248,41 @@
}
});
}
exports["hide-if-shadow-contains"] = makeInjector(hideIfShadowContains,
hideElement);
/**
- * Hides any HTML element if the text content of the element contains a given
- * string.
+ * Hides any HTML element or one of its ancestors matching a CSS selector if
+ * the text content of the element contains a given string.
*
- * @param {string} search The string to look for in every HTML element.
+ * @param {string} search The string to look for in HTML elements.
* @param {string} selector The CSS selector that an HTML element must match
* for it to be hidden.
+ * @param {string?} [searchSelector] The CSS selector that an HTML element
+ * containing the given string must match. Defaults to the value of the
+ * <code>selector</code> argument.
*/
-function hideIfContains(search, selector = "*")
+function hideIfContains(search, selector = "*", searchSelector = null)
{
+ if (searchSelector == null)
+ searchSelector = selector;
+
new MutationObserver(() =>
{
- for (let element of document.querySelectorAll(selector))
+ for (let element of document.querySelectorAll(searchSelector))
{
if (element.textContent.includes(search))
- hideElement(element);
+ {
+ let closest = element.closest(selector);
+ if (closest)
+ hideElement(closest);
+ }
}
})
.observe(document, {childList: true, characterData: true, subtree: true});
}
exports["hide-if-contains"] = hideIfContains;
/**
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld