OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2017 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 QUnit.module("Element hiding emulation", |
| 19 { |
| 20 before: function() |
| 21 { |
| 22 // The URL object in PhantomJS 2.1.7 does not implement any properties, so |
| 23 // we need a polyfill. |
| 24 if (!URL || !("origin" in URL)) |
| 25 { |
| 26 var doc = document.implementation.createHTMLDocument(); |
| 27 var anchor = doc.createElement("a"); |
| 28 doc.body.appendChild(anchor); |
| 29 URL = function(url) |
| 30 { |
| 31 if (!url) |
| 32 throw "Invalid URL"; |
| 33 anchor.href = url; |
| 34 this.origin = anchor.origin; |
| 35 }; |
| 36 } |
| 37 }, |
| 38 afterEach: function() |
| 39 { |
| 40 var styleElements = document.head.getElementsByTagName("style"); |
| 41 for (var i = 0; i < styleElements.length; i++) |
| 42 document.head.removeChild(styleElements[0]); |
| 43 } |
| 44 }); |
| 45 |
| 46 QUnit.assert.hidden = function(element) |
| 47 { |
| 48 this.equal(getComputedStyle(element).display, "none", |
| 49 "The element's display property should be set to 'none'"); |
| 50 }; |
| 51 |
| 52 QUnit.assert.visible = function(element) |
| 53 { |
| 54 this.notEqual(getComputedStyle(element).display, "none", |
| 55 "The element's display property should not be set to 'none'"); |
| 56 }; |
| 57 |
| 58 function findUniqueId() |
| 59 { |
| 60 var id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000); |
| 61 if (!document.getElementById(id)) |
| 62 return id; |
| 63 return findUniqueId(); |
| 64 } |
| 65 |
| 66 function insertStyleRule(rule) |
| 67 { |
| 68 var styleElement; |
| 69 var styleElements = document.head.getElementsByTagName("style"); |
| 70 if (styleElements.length) |
| 71 styleElement = styleElements[0]; |
| 72 else |
| 73 { |
| 74 styleElement = document.createElement("style"); |
| 75 document.head.appendChild(styleElement); |
| 76 } |
| 77 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); |
| 78 } |
| 79 |
| 80 function createElementWithStyle(styleBlock) |
| 81 { |
| 82 var element = document.createElement("div"); |
| 83 element.id = findUniqueId(); |
| 84 document.body.appendChild(element); |
| 85 insertStyleRule("#" + element.id + " " + styleBlock); |
| 86 return element; |
| 87 } |
| 88 |
| 89 function applyElemHideEmulation(selectors, callback) |
| 90 { |
| 91 var elemHideEmulation = new ElemHideEmulation( |
| 92 window, |
| 93 function(callback) |
| 94 { |
| 95 var patterns = []; |
| 96 selectors.forEach(function(selector) |
| 97 { |
| 98 patterns.push({selector: selector}); |
| 99 }); |
| 100 callback(patterns); |
| 101 }, |
| 102 function(selectors) |
| 103 { |
| 104 if (!selectors.length) |
| 105 return; |
| 106 var selector = selectors.join(", "); |
| 107 insertStyleRule(selector + "{display: none !important;}"); |
| 108 } |
| 109 ); |
| 110 |
| 111 elemHideEmulation.load(function() |
| 112 { |
| 113 elemHideEmulation.apply(); |
| 114 callback(); |
| 115 }); |
| 116 } |
| 117 |
| 118 QUnit.test("Verbatim property selector", function(assert) |
| 119 { |
| 120 var done = assert.async(); |
| 121 var toHide = createElementWithStyle("{background-color: #000}"); |
| 122 applyElemHideEmulation(["[-abp-properties='background-color: rgb(0, 0, 0)']"], |
| 123 function() |
| 124 { |
| 125 assert.hidden(toHide); |
| 126 done(); |
| 127 }); |
| 128 }); |
| 129 |
| 130 QUnit.test("Property selector with wildcard", function(assert) |
| 131 { |
| 132 var done = assert.async(); |
| 133 var toHide = createElementWithStyle("{background-color: #000}"); |
| 134 applyElemHideEmulation(["[-abp-properties='*color: rgb(0, 0, 0)']"], |
| 135 function() |
| 136 { |
| 137 assert.hidden(toHide); |
| 138 done(); |
| 139 }); |
| 140 }); |
| 141 |
| 142 QUnit.test("Property selector with regular expression", function(assert) |
| 143 { |
| 144 var done = assert.async(); |
| 145 var toHide = createElementWithStyle("{background-color: #000}"); |
| 146 applyElemHideEmulation(["[-abp-properties='/.*color: rgb\\(0, 0, 0\\)/']"], |
| 147 function() |
| 148 { |
| 149 assert.hidden(toHide); |
| 150 done(); |
| 151 }); |
| 152 }); |
| 153 |
| 154 QUnit.test("Property selector with regular expression containing escaped brace", |
| 155 function(assert) |
| 156 { |
| 157 var done = assert.async(); |
| 158 var toHide = createElementWithStyle("{background-color: #000}"); |
| 159 applyElemHideEmulation( |
| 160 ["[-abp-properties='/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/']"], |
| 161 function() |
| 162 { |
| 163 assert.hidden(toHide); |
| 164 done(); |
| 165 }); |
| 166 }); |
| 167 |
| 168 QUnit.test("Property selector with regular expression containing improperly \ |
| 169 escaped brace", |
| 170 function(assert) |
| 171 { |
| 172 var done = assert.async(); |
| 173 var toHide = createElementWithStyle("{background-color: #000}"); |
| 174 applyElemHideEmulation( |
| 175 ["[-abp-properties='/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/']"], |
| 176 function() |
| 177 { |
| 178 assert.visible(toHide); |
| 179 done(); |
| 180 }); |
| 181 }); |
| 182 |
| 183 QUnit.test("Property selector works for dynamically changed property", |
| 184 function(assert) |
| 185 { |
| 186 var done = assert.async(); |
| 187 var toHide = createElementWithStyle("{}"); |
| 188 applyElemHideEmulation(["[-abp-properties='background-color: rgb(0, 0, 0)']"], |
| 189 function() |
| 190 { |
| 191 assert.visible(toHide); |
| 192 insertStyleRule("#" + toHide.id + " {background-color: #000}"); |
| 193 setTimeout(function() |
| 194 { |
| 195 assert.hidden(toHide); |
| 196 done(); |
| 197 }); |
| 198 }); |
| 199 }); |
OLD | NEW |