| LEFT | RIGHT | 
|    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, expectHidden, expectVisible} = 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  | 
 |   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 } | 
|   29  |   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> | 
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  188       element.textContent = atob("dGhpcyBpcyBhIGJ1Zw=="); |  210       element.textContent = atob("dGhpcyBpcyBhIGJ1Zw=="); | 
|  189     } |  211     } | 
|  190     catch (e) |  212     catch (e) | 
|  191     { |  213     { | 
|  192       let msg = document.getElementById("message1"); |  214       let msg = document.getElementById("message1"); | 
|  193       msg.textContent = e.name; |  215       msg.textContent = e.name; | 
|  194     }`; |  216     }`; | 
|  195  |  217  | 
|  196   injectInlineScript(document, script); |  218   injectInlineScript(document, script); | 
|  197  |  219  | 
|  198   let e = document.getElementById("result1"); |  220   let element = document.getElementById("result1"); | 
|  199   test.ok(e, "Element 'result1' was not found"); |  221   test.ok(element, "Element 'result1' was not found"); | 
|  200  |  222  | 
|  201   let msg = document.getElementById("message1"); |  223   let msg = document.getElementById("message1"); | 
|  202   test.ok(msg, "Element 'message1' was not found"); |  224   test.ok(msg, "Element 'message1' was not found"); | 
|  203  |  225  | 
|  204   if (e && msg) |  226   if (element && msg) | 
|  205   { |  227   { | 
|  206     test.equals(e.textContent, "", "Result element should be empty"); |  228     test.equals(element.textContent, "", "Result element should be empty"); | 
|  207     test.equals(msg.textContent, "ReferenceError", |  229     test.equals(msg.textContent, "ReferenceError", | 
|  208                 "There should have been an error"); |  230                 "There should have been an error"); | 
|  209   } |  231   } | 
|  210  |  232  | 
|  211   script = ` |  233   script = ` | 
|  212     try |  234     try | 
|  213     { |  235     { | 
|  214       let element = document.getElementById("result2"); |  236       let element = document.getElementById("result2"); | 
|  215       document.write("<p>btoa: " + btoa("this is a bug") + "</p>"); |  237       document.write("<p>btoa: " + btoa("this is a bug") + "</p>"); | 
|  216       element.textContent = btoa("this is a bug"); |  238       element.textContent = btoa("this is a bug"); | 
|  217     } |  239     } | 
|  218     catch (e) |  240     catch (e) | 
|  219     { |  241     { | 
|  220       let msg = document.getElementById("message2"); |  242       let msg = document.getElementById("message2"); | 
|  221       msg.textContent = e.name; |  243       msg.textContent = e.name; | 
|  222     }`; |  244     }`; | 
|  223  |  245  | 
|  224   injectInlineScript(document, script); |  246   injectInlineScript(document, script); | 
|  225  |  247  | 
|  226   e = document.getElementById("result2"); |  248   element = document.getElementById("result2"); | 
|  227   test.ok(e, "Element 'result2' was not found"); |  249   test.ok(element, "Element 'result2' was not found"); | 
|  228  |  250  | 
|  229   msg = document.getElementById("message2"); |  251   msg = document.getElementById("message2"); | 
|  230   test.ok(msg, "Element 'message2' was not found"); |  252   test.ok(msg, "Element 'message2' was not found"); | 
|  231  |  253  | 
|  232   if (e && msg) |  254   if (element && msg) | 
|  233   { |  255   { | 
|  234     test.equals(e.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 }; | 
|  241  |  263  | 
|  242 exports.testHideIfContainsVisibleText = async function(test) |  264 exports.testHideIfContainsVisibleText = async function(test) | 
|  243 { |  265 { | 
|  244   document.body.innerHTML = `  <style type="text/css"> |  266   document.body.innerHTML = ` | 
|  245     body { |  267     <style type="text/css"> | 
|  246       margin: 0; |  268       body { | 
|  247       padding: 0; |  269         margin: 0; | 
|  248     } |  270         padding: 0; | 
|  249     .transparent { |  271       } | 
|  250       opacity: 0; |  272       .transparent { | 
|  251       position: absolute; |  273         opacity: 0; | 
|  252       display: block; |  274         position: absolute; | 
|  253     } |  275         display: block; | 
|  254     .zerosize { |  276       } | 
|  255       font-size: 0; |  277       .zerosize { | 
|  256     } |  278         font-size: 0; | 
|  257     div { |  279       } | 
|  258       display: block; |  280       div { | 
|  259     } |  281         display: block; | 
|  260     .a { |  282       } | 
|  261       display: inline-block; |  283       .a { | 
|  262       white-space: pre-wrap; |  284         display: inline-block; | 
|  263     } |  285         white-space: pre-wrap; | 
|  264     .disp_none { |  286       } | 
|  265       display: none; |  287       .disp_none { | 
|  266     } |  288         display: none; | 
|  267     .vis_hid { |  289       } | 
|  268       visibility: hidden; |  290       .vis_hid { | 
|  269     } |  291         visibility: hidden; | 
|  270     .vis_collapse { |  292       } | 
|  271       visibility: collapse; |  293       .vis_collapse { | 
|  272     } |  294         visibility: collapse; | 
|  273     .same_colour { |  295       } | 
|  274       color: rgb(255,255,255); |  296       .same_colour { | 
|  275       background-color: rgb(255,255,255); |  297         color: rgb(255,255,255); | 
|  276     } |  298         background-color: rgb(255,255,255); | 
|  277     #label { |  299       } | 
|  278       overflow-wrap: break-word; |  300       .transparent { | 
|  279     } |  301         color: transparent; | 
|  280   </style> |  302       } | 
 |  303       #label { | 
 |  304         overflow-wrap: break-word; | 
 |  305       } | 
 |  306     </style> | 
|  281     <div id="parent"> |  307     <div id="parent"> | 
|  282        <div id="middle"> |  308       <div id="middle"> | 
|  283          <div id="middle1"><div id="inside" class="inside"></div></div> |  309         <div id="middle1"><div id="inside" class="inside"></div></div> | 
|  284        </div> |  310       </div> | 
|  285        <div id="sibling"> |  311       <div id="sibling"> | 
|  286          <div id="tohide">to hide \ud83d\ude42!</div> |  312         <div id="tohide">to hide \ud83d\ude42!</div> | 
|  287        </div> |  313       </div> | 
|  288        <div id="sibling2"> |  314       <div id="sibling2"> | 
|  289          <div id="sibling21"><div id="sibling211" class="inside">Ad*</div></div> |  315         <div id="sibling21"><div id="sibling211" class="inside">Ad*</div></div> | 
|  290        </div> |  316       </div> | 
|  291       <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> |  317       <div id="label"> | 
|  292       <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> |  318         <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> | 
|  293       <article id="article"><div style="display: none"><div>Spon</div></div></ar
     ticle> |  319       </div> | 
|  294       <article id="article2"><div><div>Spon</div></div></article> |  320       <div id="label2"> | 
 |  321         <div class="a vis_hid">Visibility: hidden</div><div class="a">S</div><di
     v class="a vis_collapse">Visibility: collapse</div><div class="a">p</div><div cl
     ass="a disp_none">Display: none</div><div class="a">o</div><div class="a same_co
     lour">Same colour</div><div class="a transparent">Transparent</div><div class="a
     ">n</div> | 
 |  322       </div> | 
 |  323       <article id="article"> | 
 |  324         <div style="display: none"><a href="foo"><div>Spon</div></a>Visit us</di
     v> | 
 |  325       </article> | 
 |  326       <article id="article2"> | 
 |  327         <div><a href="foo"><div>Spon</div></a>By this</div> | 
 |  328       </article> | 
 |  329       <article id="article3"> | 
 |  330         <div><a href="foo"><div>by Writer</div></a> about the Sponsorship.</div> | 
 |  331       </article> | 
|  295     </div>`; |  332     </div>`; | 
|  296  |  333  | 
|  297   await runSnippet( |  334   await runSnippet( | 
|  298     test, "hide-if-contains-visible-text", "Spon", "#parent > div" |  335     test, "hide-if-contains-visible-text", "Spon", "#parent > div" | 
|  299   ); |  336   ); | 
|  300  |  337  | 
|  301   let element = document.getElementById("label"); |  338   let element = document.getElementById("label"); | 
|  302   expectHidden(test, element, "label"); |  339   expectHidden(test, element, "label"); | 
|  303   element = document.getElementById("label2"); |  340   element = document.getElementById("label2"); | 
|  304   expectHidden(test, element, "label2"); |  341   expectHidden(test, element, "label2"); | 
|  305  |  342  | 
|  306   element = document.getElementById("article"); |  343   element = document.getElementById("article"); | 
|  307   expectVisible(test, element, "article"); |  344   expectVisible(test, element, "article"); | 
|  308   element = document.getElementById("article2"); |  345   element = document.getElementById("article2"); | 
|  309   expectVisible(test, element, "article2"); |  346   expectVisible(test, element, "article2"); | 
|  310  |  347  | 
 |  348   await runSnippet( | 
 |  349     test, "hide-if-contains-visible-text", "Spon", "#parent > article", "#parent
      > article a" | 
 |  350   ); | 
 |  351  | 
 |  352   element = document.getElementById("article"); | 
 |  353   expectVisible(test, element, "article"); | 
 |  354   element = document.getElementById("article2"); | 
 |  355   expectHidden(test, element, "article2"); | 
 |  356   element = document.getElementById("article3"); | 
 |  357   expectVisible(test, element, "article3"); | 
 |  358  | 
|  311   test.done(); |  359   test.done(); | 
|  312 }; |  360 }; | 
| LEFT | RIGHT |