Left: | ||
Right: |
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 // We are currently limited to ECMAScript 5 in this file, because it is being | |
19 // used in the browser tests. See https://issues.adblockplus.org/ticket/4796 | |
20 | |
21 QUnit.module("Element hiding emulation", | |
22 { | |
23 before: function() | |
24 { | |
25 // The URL object in PhantomJS 2.1.7 does not implement any properties, so | |
26 // we need a polyfill. | |
27 if (!URL || !("origin" in URL)) | |
28 { | |
29 var doc = document.implementation.createHTMLDocument(); | |
30 var anchor = doc.createElement("a"); | |
31 doc.body.appendChild(anchor); | |
32 URL = function(url) | |
33 { | |
34 if (!url) | |
35 throw "Invalid URL"; | |
36 anchor.href = url; | |
37 this.origin = anchor.origin; | |
38 }; | |
39 } | |
40 }, | |
41 afterEach: function() | |
42 { | |
43 var styleElements = document.head.getElementsByTagName("style"); | |
44 for (var i = 0; i < styleElements.length; i++) | |
45 document.head.removeChild(styleElements[0]); | |
46 } | |
47 }); | |
48 | |
49 QUnit.assert.hidden = function(element) | |
50 { | |
51 this.equal(getComputedStyle(element).display, "none", | |
52 "The element's display property should be set to 'none'"); | |
53 }; | |
54 | |
55 QUnit.assert.visible = function(element) | |
56 { | |
57 this.notEqual(getComputedStyle(element).display, "none", | |
58 "The element's display property should not be set to 'none'"); | |
59 }; | |
60 | |
61 function findUniqueId() | |
62 { | |
63 var id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000); | |
64 if (!document.getElementById(id)) | |
65 return id; | |
66 return findUniqueId(); | |
67 } | |
68 | |
69 function insertStyleRule(rule) | |
70 { | |
71 var styleElement; | |
72 var styleElements = document.head.getElementsByTagName("style"); | |
73 if (styleElements.length) | |
74 { | |
75 styleElement = styleElements[0]; | |
76 } | |
77 else | |
78 { | |
79 styleElement = document.createElement("style"); | |
80 document.head.appendChild(styleElement); | |
81 } | |
82 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); | |
83 } | |
84 | |
85 function createElementWithStyle(styleBlock) | |
86 { | |
87 var element = document.createElement("div"); | |
88 element.id = findUniqueId(); | |
89 document.body.appendChild(element); | |
90 insertStyleRule("#" + element.id + " " + styleBlock); | |
91 return element; | |
92 } | |
93 | |
94 function applyElemHideEmulation(selectors, callback) | |
95 { | |
96 var elemHideEmulation = new ElemHideEmulation( | |
97 window, | |
98 function(callback) | |
99 { | |
100 var patterns = []; | |
101 selectors.forEach(function(selector) | |
102 { | |
103 patterns.push({selector: selector}); | |
104 }); | |
105 callback(patterns); | |
106 }, | |
107 function(selectors) | |
108 { | |
109 if (!selectors.length) | |
110 return; | |
111 var selector = selectors.join(", "); | |
112 insertStyleRule(selector + "{display: none !important;}"); | |
113 } | |
114 ); | |
115 | |
116 elemHideEmulation.load(function() | |
117 { | |
118 elemHideEmulation.apply(); | |
119 callback(); | |
120 }); | |
121 } | |
122 | |
123 QUnit.test("Verbatim property selector", function(assert) | |
124 { | |
125 var done = assert.async(); | |
126 var toHide = createElementWithStyle("{background-color: #000}"); | |
127 applyElemHideEmulation(["[-abp-properties='background-color: rgb(0, 0, 0)']"], | |
kzar
2017/01/18 04:17:15
Mind also moving the first argument of the applyEl
Felix Dahlke
2017/01/19 09:56:51
I think there's a range of how we generally do thi
Sebastian Noack
2017/01/19 10:13:00
I agree with Dave. As I explained in my previous c
| |
128 function() | |
129 { | |
130 assert.hidden(toHide); | |
131 done(); | |
132 } | |
133 ); | |
134 }); | |
135 | |
136 QUnit.test("Property selector with wildcard", function(assert) | |
kzar
2017/01/18 04:17:15
Mind bringing the function call and test descripti
Felix Dahlke
2017/01/19 09:56:51
There's no need for the wrapping in these cases, a
Sebastian Noack
2017/01/19 10:13:00
FWIW, if all arguments fit on the same line as the
| |
137 { | |
138 var done = assert.async(); | |
139 var toHide = createElementWithStyle("{background-color: #000}"); | |
140 applyElemHideEmulation(["[-abp-properties='*color: rgb(0, 0, 0)']"], | |
141 function() | |
142 { | |
143 assert.hidden(toHide); | |
144 done(); | |
145 } | |
146 ); | |
147 }); | |
148 | |
149 QUnit.test("Property selector with regular expression", function(assert) | |
150 { | |
151 var done = assert.async(); | |
152 var toHide = createElementWithStyle("{background-color: #000}"); | |
153 applyElemHideEmulation(["[-abp-properties='/.*color: rgb\\(0, 0, 0\\)/']"], | |
154 function() | |
155 { | |
156 assert.hidden(toHide); | |
157 done(); | |
158 } | |
159 ); | |
160 }); | |
161 | |
162 QUnit.test("Property selector with regular expression containing escaped brace", | |
163 function(assert) | |
164 { | |
165 var done = assert.async(); | |
166 var toHide = createElementWithStyle("{background-color: #000}"); | |
167 applyElemHideEmulation( | |
168 ["[-abp-properties='/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/']"], | |
169 function() | |
170 { | |
171 assert.hidden(toHide); | |
172 done(); | |
173 } | |
174 ); | |
175 } | |
176 ); | |
177 | |
178 QUnit.test("Property selector with regular expression containing improperly \ | |
179 escaped brace", | |
180 function(assert) | |
181 { | |
182 var done = assert.async(); | |
183 var toHide = createElementWithStyle("{background-color: #000}"); | |
184 applyElemHideEmulation( | |
185 ["[-abp-properties='/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/']"], | |
186 function() | |
187 { | |
188 assert.visible(toHide); | |
189 done(); | |
190 } | |
191 ); | |
192 } | |
193 ); | |
194 | |
195 QUnit.test("Property selector works for dynamically changed property", | |
196 function(assert) | |
197 { | |
198 var done = assert.async(); | |
199 var toHide = createElementWithStyle("{}"); | |
200 applyElemHideEmulation( | |
201 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], | |
202 function() | |
203 { | |
204 assert.visible(toHide); | |
205 insertStyleRule("#" + toHide.id + " {background-color: #000}"); | |
206 setTimeout(function() | |
207 { | |
208 assert.hidden(toHide); | |
209 done(); | |
210 }); | |
211 } | |
212 ); | |
213 } | |
214 ); | |
OLD | NEW |