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

Side by Side Diff: test/browser/snippets.js

Issue 30024560: Issue 7450 - Implement hide-if-contains-visible-text snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Fixed a few nits. Unfactored test code. Created April 18, 2019, 5:48 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
« lib/content/snippets.js ('K') | « lib/content/snippets.js ('k') | no next file » | 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 const library = require("../../lib/content/snippets.js"); 20 const library = require("../../lib/content/snippets.js");
21 const {timeout} = require("./_utils"); 21 const {timeout} = require("./_utils");
22 22
23 // We need this stub for the injector. 23 // We need this stub for the injector.
24 window.browser = { 24 window.browser = {
25 runtime: { 25 runtime: {
26 getURL: () => "" 26 getURL: () => ""
27 } 27 }
28 }; 28 };
29 29
30 function expectHidden(test, element, id)
31 {
32 let withId = "";
33 if (typeof id != "undefined")
34 withId = ` with ID '${id}'`;
35
36 test.equal(
37 window.getComputedStyle(element).display, "none",
38 `The element${withId}'s display property should be set to 'none'`);
39 }
40
41 function expectVisible(test, element, id)
42 {
43 let withId = "";
44 if (typeof id != "undefined")
45 withId = ` with ID '${id}'`;
46
47 test.notEqual(
48 window.getComputedStyle(element).display, "none",
49 `The element${withId}'s display property should not be set to 'none'`);
50 }
51
30 async function runSnippet(test, snippetName, ...args) 52 async function runSnippet(test, snippetName, ...args)
31 { 53 {
32 let snippet = library[snippetName]; 54 let snippet = library[snippetName];
33 55
34 test.ok(snippet); 56 test.ok(snippet);
35 57
36 snippet(...args); 58 snippet(...args);
37 59
38 // For snippets that run in the context of the document via a <script> 60 // For snippets that run in the context of the document via a <script>
39 // element (i.e. snippets that use makeInjector()), we need to wait for 61 // element (i.e. snippets that use makeInjector()), we need to wait for
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 253
232 if (element && msg) 254 if (element && msg)
233 { 255 {
234 test.equals(element.textContent, "", "Result element should be empty"); 256 test.equals(element.textContent, "", "Result element should be empty");
235 test.equals(msg.textContent, "ReferenceError", 257 test.equals(msg.textContent, "ReferenceError",
236 "There should have been an error"); 258 "There should have been an error");
237 } 259 }
238 260
239 test.done(); 261 test.done();
240 }; 262 };
263
264 exports.testHideIfContainsVisibleText = async function(test)
265 {
266 document.body.innerHTML = `
267 <style type="text/css">
268 body {
269 margin: 0;
270 padding: 0;
271 }
272 .transparent {
273 opacity: 0;
274 position: absolute;
275 display: block;
276 }
277 .zerosize {
278 font-size: 0;
279 }
280 div {
281 display: block;
282 }
283 .a {
284 display: inline-block;
285 white-space: pre-wrap;
286 }
287 .disp_none {
288 display: none;
289 }
290 .vis_hid {
291 visibility: hidden;
292 }
293 .vis_collapse {
294 visibility: collapse;
295 }
296 .same_colour {
297 color: rgb(255,255,255);
298 background-color: rgb(255,255,255);
299 }
300 #label {
301 overflow-wrap: break-word;
302 }
303 </style>
304 <div id="parent">
305 <div id="middle">
306 <div id="middle1"><div id="inside" class="inside"></div></div>
307 </div>
308 <div id="sibling">
309 <div id="tohide">to hide \ud83d\ude42!</div>
310 </div>
311 <div id="sibling2">
312 <div id="sibling21"><div id="sibling211" class="inside">Ad*</div></div>
313 </div>
314 <div id="label"><div id="content"><div class="a transparent">Sp</div><div class="a">Sp</div><div class="a zerosize">S</div><div class="a transparent">on</ div><div class="a">on</div><div class="a zerosize">S</div></div></div>
315 <div id="label2"><div class="a vis_hid">Visibility: hidden</div><div class ="a">S</div><div class="a vis_collapse">Visibility: collapse</div><div class="a" >p</div><div class="a disp_none">Display: none</div><div class="a">o</div><div c lass="a same_colour">Same colour</div><div class="a">n</div></div>
316 <article id="article"><div style="display: none"><a href="foo"><div>Spon</ div></a>Visit us</div></article>
317 <article id="article2"><div><a href="foo"><div>Spon</div></a>By this</div> </article>
318 <article id="article3"><div><a href="foo"><div>by Writer</div></a> about t he Sponsorship.</div></article>
319 </div>`;
a.giammarchi 2019/04/26 13:33:58 nit: I wonder if for test maintainability sake we
hub 2019/04/27 17:32:05 A '\n' in the markup generate a blank space in the
320
321 await runSnippet(
322 test, "hide-if-contains-visible-text", "Spon", "#parent > div"
323 );
324
325 let element = document.getElementById("label");
326 expectHidden(test, element, "label");
327 element = document.getElementById("label2");
328 expectHidden(test, element, "label2");
329
330 element = document.getElementById("article");
331 expectVisible(test, element, "article");
332 element = document.getElementById("article2");
333 expectVisible(test, element, "article2");
334
335 await runSnippet(
336 test, "hide-if-contains-visible-text", "Spon", "#parent > article", "#parent > article a"
337 );
338
339 element = document.getElementById("article");
340 expectHidden(test, element, "article");
341 element = document.getElementById("article2");
342 expectHidden(test, element, "article2");
343 element = document.getElementById("article3");
344 expectVisible(test, element, "article3");
345
346 test.done();
a.giammarchi 2019/04/26 13:33:59 Happy to discuss this offline/elsewhere though, I
hub 2019/04/27 17:32:06 This is because we still use nodeunit. There is is
347 };
OLDNEW
« lib/content/snippets.js ('K') | « lib/content/snippets.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld