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

Side by Side 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.
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/_utils.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 887 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 898
899 args[0] = url.href; 899 args[0] = url.href;
900 } 900 }
901 901
902 return fetch_.apply(this, args); 902 return fetch_.apply(this, args);
903 }; 903 };
904 } 904 }
905 905
906 exports["strip-fetch-query-parameter"] = makeInjector(stripFetchQueryParameter, 906 exports["strip-fetch-query-parameter"] = makeInjector(stripFetchQueryParameter,
907 toRegExp, regexEscape); 907 toRegExp, regexEscape);
908
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
909 /**
910 * 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.
911 *
912 * @param {string} search the text to match to the visible text. Is considered
913 * visible text that isn't hidden by CSS properties or other means.
914 * If the string begins and ends with a slash (<code>/</code>), the
915 * text in between is treated as a regular expression.
916 * @param {string} hideSelector the selector for the element that must be
917 * hidden.
918 * @param {?string} [innerSelector] the selector relative to the element that
919 * will select the text content to match.
920 */
921 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)
922 {
923 /**
924 * 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.
925 * @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.
926 * @param {?CSSStyleDeclaration} [style] the computed style of element. If
927 * falsey it will be queried.
928 * @returns {bool} whether the text is visible.
929 */
930 function isTextVisible(element, style)
931 {
932 if (!style)
933 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.
934
935 if (style.getPropertyValue("opacity") == "0")
936 return false;
937 if (style.getPropertyValue("font-size") == "0px")
938 return false;
939 if (style.getPropertyValue("color") ==
940 style.getPropertyValue("background-color"))
941 return false;
942
943 return true;
944 }
945
946 /**
947 * Returns the visible text content from an element and its children.
948 * @param {Element} element the element whose visible text we want.
949 * @returns {string} the text that is visible.
950 */
951 function getVisibleContent(element)
952 {
953 let style = window.getComputedStyle(element);
954 if (style.getPropertyValue("display") == "none")
955 return "";
956 let visibility = style.getPropertyValue("visibility");
957 if (visibility == "hidden" || visibility == "collapse")
958 return "";
959
960 let text = "";
961 for (let node of element.childNodes)
962 {
963 switch (node.nodeType)
964 {
965 case Node.ELEMENT_NODE:
966 text += getVisibleContent(node);
967 break;
968 case Node.TEXT_NODE:
969 if (isTextVisible(element, style))
970 text += node.nodeValue;
971 break;
972 }
973 }
974 return text;
975 }
976
977 let re = toRegExp(search);
978
979 new MutationObserver(() =>
980 {
981 for (let element of document.querySelectorAll(hideSelector))
982 {
983 let inners =
984 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 :-)
985 for (let inner of inners)
986 {
987 let content = getVisibleContent(inner);
988 if (re.test(content))
989 hideElement(element);
990 }
991 }
992 })
993 .observe(document, {childList: true, characterData: true, subtree: true});
994 }
995
996 exports["hide-if-contains-visible-text"] =
997 makeInjector(hideIfContainsVisibleText, hideElement);
OLDNEW
« 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