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

Unified Diff: lib/content/snippets.js

Issue 30024560: Issue 7450 - Implement hide-if-contains-visible-text snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: added docs, more test. Created April 6, 2019, 4:38 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 | test/browser/_utils.js » ('j') | 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
@@ -900,8 +900,98 @@
}
return fetch_.apply(this, args);
};
}
exports["strip-fetch-query-parameter"] = makeInjector(stripFetchQueryParameter,
toRegExp, regexEscape);
+
Manish Jethani 2019/04/17 13:54:26 Nit: Maybe a good idea to group all the `hide-if-c
hub 2019/04/17 17:25:06 yes
+/**
+ * Hide an element matching a selector if it contains a specified visible text.
Manish Jethani 2019/04/17 13:54:25 Nits or consistency: s/Hide/Hides/ s/an element
hub 2019/04/17 17:25:06 Done.
+ *
+ * @param {string} search the text to match to the visible text. Is considered
+ * visible text that isn't hidden by CSS properties or other means.
+ * If the string begins and ends with a slash (<code>/</code>), the
+ * text in between is treated as a regular expression.
+ * @param {string} hideSelector the selector for the element that must be
+ * hidden.
+ * @param {?string} [innerSelector] the selector relative to the element that
+ * will select the text content to match.
+ */
+function hideIfContainsVisibleText(search, hideSelector, innerSelector = null)
Manish Jethani 2019/04/09 12:31:23 I know the use case here is to target a node based
hub 2019/04/10 16:16:21 I think this defeat the design of that snippet tha
Manish Jethani 2019/04/10 16:39:23 Instead of hard-coding the styles that would ident
Manish Jethani 2019/04/10 17:15:17 By the way, if you think we should add a `hide-if-
hub 2019/04/10 19:40:05 I'd be ok, despite being more complicated, to allo
hub 2019/04/10 19:40:05 I don't think it make sense to merge the code of t
Manish Jethani 2019/04/10 19:48:20 We don't have to invent anything new for the synta
Manish Jethani 2019/04/10 19:48:20 What I'm saying is that the entire functionality t
Manish Jethani 2019/04/10 19:58:52 To give an example of what I'm saying, let's say t
Manish Jethani 2019/04/17 13:54:25 Nit: parameter names for consistency with other sn
hub 2019/04/17 17:25:06 They have a different meaning, but.... (see below)
+{
+ /**
+ * Determine if the text inside the element is visible.
Manish Jethani 2019/04/17 13:54:25 Nit: s/Determine/Determines/
hub 2019/04/17 17:25:06 Done.
+ * @param {Element} element the leaf element we are checking.
Manish Jethani 2019/04/17 13:54:26 Nit: So far in this file the doc string starts wit
hub 2019/04/17 17:25:06 Done.
+ * @param {?CSSStyleDeclaration} [style] the computed style of element. If
+ * falsey it will be queried.
+ * @returns {bool} whether the text is visible.
+ */
+ function isTextVisible(element, style)
+ {
+ if (!style)
+ style = window.getComputedStyle(element);
Manish Jethani 2019/04/17 13:54:26 There's a local `getComputedStyle()` which covers
hub 2019/04/17 17:25:06 I only see `getComputedCSSText()` which calls`getC
Manish Jethani 2019/04/17 18:33:28 Sorry, my bad. Ack.
+
+ if (style.getPropertyValue("opacity") == "0")
+ return false;
+ if (style.getPropertyValue("font-size") == "0px")
+ return false;
+ if (style.getPropertyValue("color") ==
+ style.getPropertyValue("background-color"))
+ return false;
+
+ return true;
+ }
+
+ /**
+ * Returns the visible text content from an element and its children.
+ * @param {Element} element the element whose visible text we want.
+ * @returns {string} the text that is visible.
+ */
+ function getVisibleContent(element)
+ {
+ let style = window.getComputedStyle(element);
+ if (style.getPropertyValue("display") == "none")
+ return "";
+ let visibility = style.getPropertyValue("visibility");
+ if (visibility == "hidden" || visibility == "collapse")
+ return "";
+
+ let text = "";
+ for (let node of element.childNodes)
+ {
+ switch (node.nodeType)
+ {
+ case Node.ELEMENT_NODE:
+ text += getVisibleContent(node);
+ break;
+ case Node.TEXT_NODE:
+ if (isTextVisible(element, style))
+ text += node.nodeValue;
+ break;
+ }
+ }
+ return text;
+ }
+
+ let re = toRegExp(search);
+
+ new MutationObserver(() =>
+ {
+ for (let element of document.querySelectorAll(hideSelector))
+ {
+ let inners =
+ innerSelector ? element.querySelectorAll(innerSelector) : [element];
Manish Jethani 2019/04/17 13:54:26 Here there's a second call to `querySelectorAll()`
hub 2019/04/17 17:25:06 Let's rework this in the next patch. (see comment
Manish Jethani 2019/04/17 18:33:28 Just so I get this right, we'll have a follow-up p
hub 2019/04/17 18:40:34 The current patch already refactor :-)
+ for (let inner of inners)
+ {
+ let content = getVisibleContent(inner);
+ if (re.test(content))
+ hideElement(element);
+ }
+ }
+ })
+ .observe(document, {childList: true, characterData: true, subtree: true});
+}
+
+exports["hide-if-contains-visible-text"] =
+ makeInjector(hideIfContainsVisibleText, hideElement);
« no previous file with comments | « no previous file | test/browser/_utils.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld