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 = ` | 266 document.body.innerHTML = ` |
(...skipping 23 matching lines...) Expand all Loading... |
268 .vis_hid { | 290 .vis_hid { |
269 visibility: hidden; | 291 visibility: hidden; |
270 } | 292 } |
271 .vis_collapse { | 293 .vis_collapse { |
272 visibility: collapse; | 294 visibility: collapse; |
273 } | 295 } |
274 .same_colour { | 296 .same_colour { |
275 color: rgb(255,255,255); | 297 color: rgb(255,255,255); |
276 background-color: rgb(255,255,255); | 298 background-color: rgb(255,255,255); |
277 } | 299 } |
| 300 .transparent { |
| 301 color: transparent; |
| 302 } |
278 #label { | 303 #label { |
279 overflow-wrap: break-word; | 304 overflow-wrap: break-word; |
280 } | 305 } |
281 </style> | 306 </style> |
282 <div id="parent"> | 307 <div id="parent"> |
283 <div id="middle"> | 308 <div id="middle"> |
284 <div id="middle1"><div id="inside" class="inside"></div></div> | 309 <div id="middle1"><div id="inside" class="inside"></div></div> |
285 </div> | 310 </div> |
286 <div id="sibling"> | 311 <div id="sibling"> |
287 <div id="tohide">to hide \ud83d\ude42!</div> | 312 <div id="tohide">to hide \ud83d\ude42!</div> |
288 </div> | 313 </div> |
289 <div id="sibling2"> | 314 <div id="sibling2"> |
290 <div id="sibling21"><div id="sibling211" class="inside">Ad*</div></div> | 315 <div id="sibling21"><div id="sibling211" class="inside">Ad*</div></div> |
291 </div> | 316 </div> |
292 <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"> |
293 <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> |
294 <article id="article"><div style="display: none"><a href="foo"><div>Spon</
div></a>Visit us</div></article> | 319 </div> |
295 <article id="article2"><div><a href="foo"><div>Spon</div></a>By this</div>
</article> | 320 <div id="label2"> |
296 <article id="article3"><div><a href="foo"><div>by Writer</div></a> about t
he Sponsorship.</div></article> | 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> |
297 </div>`; | 332 </div>`; |
298 | 333 |
299 await runSnippet( | 334 await runSnippet( |
300 test, "hide-if-contains-visible-text", "Spon", "#parent > div" | 335 test, "hide-if-contains-visible-text", "Spon", "#parent > div" |
301 ); | 336 ); |
302 | 337 |
303 let element = document.getElementById("label"); | 338 let element = document.getElementById("label"); |
304 expectHidden(test, element, "label"); | 339 expectHidden(test, element, "label"); |
305 element = document.getElementById("label2"); | 340 element = document.getElementById("label2"); |
306 expectHidden(test, element, "label2"); | 341 expectHidden(test, element, "label2"); |
307 | 342 |
308 element = document.getElementById("article"); | 343 element = document.getElementById("article"); |
309 expectVisible(test, element, "article"); | 344 expectVisible(test, element, "article"); |
310 element = document.getElementById("article2"); | 345 element = document.getElementById("article2"); |
311 expectVisible(test, element, "article2"); | 346 expectVisible(test, element, "article2"); |
312 | 347 |
313 await runSnippet( | 348 await runSnippet( |
314 test, "hide-if-contains-visible-text", "Spon", "#parent > article", "a" | 349 test, "hide-if-contains-visible-text", "Spon", "#parent > article", "#parent
> article a" |
315 ); | 350 ); |
316 | 351 |
317 element = document.getElementById("article"); | 352 element = document.getElementById("article"); |
318 expectHidden(test, element, "article"); | 353 expectVisible(test, element, "article"); |
319 element = document.getElementById("article2"); | 354 element = document.getElementById("article2"); |
320 expectHidden(test, element, "article2"); | 355 expectHidden(test, element, "article2"); |
321 element = document.getElementById("article3"); | 356 element = document.getElementById("article3"); |
322 expectVisible(test, element, "article3"); | 357 expectVisible(test, element, "article3"); |
323 | 358 |
324 test.done(); | 359 test.done(); |
325 }; | 360 }; |
LEFT | RIGHT |