Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 | |
1 QUnit.module("Element hiding emulation", | 21 QUnit.module("Element hiding emulation", |
2 { | 22 { |
3 before: function() | 23 before: function() |
4 { | 24 { |
5 // The URL object in PhantomJS 2.1.7 does not implement any properties, so | 25 // The URL object in PhantomJS 2.1.7 does not implement any properties, so |
6 // we need a polyfill. | 26 // we need a polyfill. |
7 if (!URL || !("origin" in URL)) | 27 if (!URL || !("origin" in URL)) |
8 { | 28 { |
9 var doc = document.implementation.createHTMLDocument(); | 29 var doc = document.implementation.createHTMLDocument(); |
10 var anchor = doc.createElement("a"); | 30 var anchor = doc.createElement("a"); |
11 doc.body.appendChild(anchor); | 31 doc.body.appendChild(anchor); |
12 URL = function(url) | 32 URL = function(url) |
13 { | 33 { |
14 if (!url) | 34 if (!url) |
15 throw "Invalid URL"; | 35 throw "Invalid URL"; |
16 anchor.href = url; | 36 anchor.href = url; |
17 this.origin = anchor.origin; | 37 this.origin = anchor.origin; |
18 }; | 38 }; |
19 } | 39 } |
20 }, | 40 }, |
21 afterEach: function() | 41 afterEach: function() |
22 { | 42 { |
23 var styleElements = document.head.getElementsByTagName("style"); | 43 var styleElements = document.head.getElementsByTagName("style"); |
24 for (var i = 0; i < styleElements.length; i++) | 44 for (var i = 0; i < styleElements.length; i++) |
kzar
2017/01/09 08:28:43
Can we not use more modern JavaScript features wit
Felix Dahlke
2017/01/10 09:16:49
Unfortunately not with version 2.1.7, that one sti
kzar
2017/01/10 11:05:33
Damn, this could be a problem in the near future s
kzar
2017/01/11 04:25:28
You missed this one? (Some other smaller ones in t
Felix Dahlke
2017/01/11 10:06:02
No, it rather sounded like you thought this could
kzar
2017/01/11 11:19:18
Please at least put a comment / note in relevant p
Felix Dahlke
2017/01/12 09:24:06
Sure! This affects chrome/content/elemHideEmulatio
kzar
2017/01/12 09:42:13
Cool, sounds good. (IMO an issue is overkill since
Felix Dahlke
2017/01/17 19:41:08
Done.
| |
25 document.head.removeChild(styleElements[0]); | 45 document.head.removeChild(styleElements[0]); |
26 } | 46 } |
27 }); | 47 }); |
28 | 48 |
29 QUnit.assert.hidden = function(element) | 49 QUnit.assert.hidden = function(element) |
kzar
2017/01/09 08:28:43
I wonder if the URL polyfill and these utility fun
Felix Dahlke
2017/01/10 09:16:49
I personally prefer to share code once it needs to
kzar
2017/01/10 11:05:33
Fair enough.
| |
30 { | 50 { |
31 this.equal(getComputedStyle(element).display, "none", | 51 this.equal(getComputedStyle(element).display, "none", |
32 "The element's display property should be set to 'none'"); | 52 "The element's display property should be set to 'none'"); |
33 }; | 53 }; |
34 | 54 |
35 QUnit.assert.visible = function(element) | 55 QUnit.assert.visible = function(element) |
36 { | 56 { |
37 this.notEqual(getComputedStyle(element).display, "none", | 57 this.notEqual(getComputedStyle(element).display, "none", |
38 "The element's display property should not be set to 'none'"); | 58 "The element's display property should not be set to 'none'"); |
39 }; | 59 }; |
40 | 60 |
41 function findUniqueId() | 61 function findUniqueId() |
42 { | 62 { |
43 var id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000); | 63 var id = "elemHideEmulationTest-" + Math.floor(Math.random() * 10000); |
44 if (!document.getElementById(id)) | 64 if (!document.getElementById(id)) |
45 return id; | 65 return id; |
46 return findUniqueId(); | 66 return findUniqueId(); |
47 } | 67 } |
48 | 68 |
49 function insertStyleRule(rule) | 69 function insertStyleRule(rule) |
50 { | 70 { |
51 var styleElement; | 71 var styleElement; |
kzar
2017/01/09 08:28:43
Nit: `let` rather than `var` throughout?
Felix Dahlke
2017/01/10 09:16:48
See above.
| |
52 var styleElements = document.head.getElementsByTagName("style"); | 72 var styleElements = document.head.getElementsByTagName("style"); |
53 if (styleElements.length) | 73 if (styleElements.length) |
74 { | |
54 styleElement = styleElements[0]; | 75 styleElement = styleElements[0]; |
kzar
2017/01/09 08:28:43
Nit: Since the else clause has braces the if claus
Felix Dahlke
2017/01/10 09:16:48
Why? I personally prefer it this way.
kzar
2017/01/10 11:05:33
I've been told to do that on codereviews before by
Felix Dahlke
2017/01/10 13:27:32
IIRC he actually prefers it that way, but since we
kzar
2017/01/11 04:25:29
OK well how's this? I have been asked to use consi
Felix Dahlke
2017/01/11 10:06:02
What others have asked in other places is not part
kzar
2017/01/11 11:19:19
I think it is relevant what Wladimir has told me i
Felix Dahlke
2017/01/17 19:41:07
You're not reviewing it for him, you're reviewing
| |
76 } | |
55 else | 77 else |
56 { | 78 { |
57 styleElement = document.createElement("style"); | 79 styleElement = document.createElement("style"); |
58 document.head.appendChild(styleElement); | 80 document.head.appendChild(styleElement); |
59 } | 81 } |
60 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); | 82 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); |
61 } | 83 } |
62 | 84 |
63 function createElementWithStyle(styleBlock) | 85 function createElementWithStyle(styleBlock) |
64 { | 86 { |
65 var element = document.createElement("div"); | 87 var element = document.createElement("div"); |
66 element.id = findUniqueId(); | 88 element.id = findUniqueId(); |
67 document.body.appendChild(element); | 89 document.body.appendChild(element); |
68 insertStyleRule("#" + element.id + " " + styleBlock); | 90 insertStyleRule("#" + element.id + " " + styleBlock); |
69 return element; | 91 return element; |
70 } | 92 } |
71 | 93 |
72 function applyElemHideEmulation(selectors, callback) | 94 function applyElemHideEmulation(selectors, callback) |
73 { | 95 { |
74 var elemHideEmulation = new ElemHideEmulation( | 96 var elemHideEmulation = new ElemHideEmulation( |
75 window, | 97 window, |
76 function(callback) | 98 function(callback) |
kzar
2017/01/09 08:28:43
Nit: Mind using arrow functions here and for the o
Felix Dahlke
2017/01/10 09:16:48
See above.
| |
77 { | 99 { |
78 var patterns = []; | 100 var patterns = []; |
79 selectors.forEach(function(selector) | 101 selectors.forEach(function(selector) |
80 { | 102 { |
81 patterns.push({selector: selector}); | 103 patterns.push({selector: selector}); |
82 }); | 104 }); |
83 callback(patterns); | 105 callback(patterns); |
84 }, | 106 }, |
85 function(selectors) | 107 function(selectors) |
86 { | 108 { |
87 if (!selectors.length) | 109 if (!selectors.length) |
88 return; | 110 return; |
89 var selector = selectors.join(", "); | 111 var selector = selectors.join(", "); |
90 insertStyleRule(selector + "{display: none !important;}"); | 112 insertStyleRule(selector + "{display: none !important;}"); |
91 } | 113 } |
92 ); | 114 ); |
93 | 115 |
94 elemHideEmulation.load(function() | 116 elemHideEmulation.load(function() |
95 { | 117 { |
96 elemHideEmulation.apply(); | 118 elemHideEmulation.apply(); |
97 callback(); | 119 callback(); |
98 }); | 120 }); |
99 } | 121 } |
100 | 122 |
101 QUnit.test("Verbatim property selector", function(assert) | 123 QUnit.test( |
102 { | 124 "Verbatim property selector", |
103 var done = assert.async(); | 125 function(assert) |
104 var toHide = createElementWithStyle("{background-color: #000}"); | 126 { |
105 applyElemHideEmulation(["[-abp-properties='background-color: rgb(0, 0, 0)']"], | 127 var done = assert.async(); |
kzar
2017/01/09 08:28:43
Nit: This indentation looks weird. How about somet
Felix Dahlke
2017/01/10 09:16:49
Yeah I know, I did some research in our code base
kzar
2017/01/10 11:05:33
Well I'm pretty sure we've done it the way I've su
Felix Dahlke
2017/01/10 13:27:32
Then I figure we should leave it as-is for the sak
kzar
2017/01/11 04:25:29
Or alternatively you could just improve the indent
Felix Dahlke
2017/01/11 10:06:02
I don't find it very beautiful, but it does look l
kzar
2017/01/11 11:19:18
Well we "indent the function body" all the time, f
Felix Dahlke
2017/01/12 09:24:06
No, I mean the opening brace of an anonymous funct
kzar
2017/01/12 09:42:13
I really think that you're overthinking this and s
Sebastian Noack
2017/01/12 23:12:58
I agree that putting the function header on a line
Felix Dahlke
2017/01/17 19:41:07
Thanks Sebastian, done.
(On a related note: I thi
| |
106 function() | 128 var toHide = createElementWithStyle("{background-color: #000}"); |
107 { | 129 applyElemHideEmulation( |
108 assert.hidden(toHide); | 130 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], |
109 done(); | 131 function() |
110 }); | 132 { |
111 }); | 133 assert.hidden(toHide); |
112 | 134 done(); |
113 QUnit.test("Property selector with wildcard", function(assert) | 135 } |
114 { | 136 ); |
115 var done = assert.async(); | 137 } |
116 var toHide = createElementWithStyle("{background-color: #000}"); | 138 ); |
117 applyElemHideEmulation(["[-abp-properties='*color: rgb(0, 0, 0)']"], | 139 |
118 function() | 140 QUnit.test( |
119 { | 141 "Property selector with wildcard", |
120 assert.hidden(toHide); | 142 function(assert) |
121 done(); | 143 { |
122 }); | 144 var done = assert.async(); |
123 }); | 145 var toHide = createElementWithStyle("{background-color: #000}"); |
124 | 146 applyElemHideEmulation( |
125 QUnit.test("Property selector with regular expression", function(assert) | 147 ["[-abp-properties='*color: rgb(0, 0, 0)']"], |
126 { | 148 function() |
127 var done = assert.async(); | 149 { |
128 var toHide = createElementWithStyle("{background-color: #000}"); | 150 assert.hidden(toHide); |
129 applyElemHideEmulation(["[-abp-properties='/.*color: rgb\\(0, 0, 0\\)/']"], | 151 done(); |
130 function() | 152 } |
131 { | 153 ); |
132 assert.hidden(toHide); | 154 } |
133 done(); | 155 ); |
134 }); | 156 |
135 }); | 157 QUnit.test( |
136 | 158 "Property selector with regular expression", |
137 QUnit.test("Property selector with regular expression containing escaped brace", | 159 function(assert) |
138 function(assert) | 160 { |
139 { | 161 var done = assert.async(); |
140 var done = assert.async(); | 162 var toHide = createElementWithStyle("{background-color: #000}"); |
141 var toHide = createElementWithStyle("{background-color: #000}"); | 163 applyElemHideEmulation( |
142 applyElemHideEmulation( | 164 ["[-abp-properties='/.*color: rgb\\(0, 0, 0\\)/']"], |
143 ["[-abp-properties='/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/']"], | 165 function() |
144 function() | 166 { |
145 { | 167 assert.hidden(toHide); |
146 assert.hidden(toHide); | 168 done(); |
147 done(); | 169 } |
148 }); | 170 ); |
149 }); | 171 } |
150 | 172 ); |
151 QUnit.test("Property selector with regular expression containing improperly \ | 173 |
152 escaped brace", | 174 QUnit.test( |
153 function(assert) | 175 "Property selector with regular expression containing escaped brace", |
154 { | 176 function(assert) |
155 var done = assert.async(); | 177 { |
156 var toHide = createElementWithStyle("{background-color: #000}"); | 178 var done = assert.async(); |
157 applyElemHideEmulation( | 179 var toHide = createElementWithStyle("{background-color: #000}"); |
158 ["[-abp-properties='/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/']"], | 180 applyElemHideEmulation( |
159 function() | 181 ["[-abp-properties='/background.\\x7B 0,6\\x7D : rgb\\(0, 0, 0\\)/']"], |
160 { | 182 function() |
161 assert.visible(toHide); | 183 { |
162 done(); | 184 assert.hidden(toHide); |
163 }); | 185 done(); |
164 }); | 186 } |
165 | 187 ); |
166 QUnit.test("Property selector works for dynamically changed property", | 188 } |
167 function(assert) | 189 ); |
168 { | 190 |
169 var done = assert.async(); | 191 QUnit.test( |
170 var toHide = createElementWithStyle("{}"); | 192 "Property selector with regular expression containing improperly escaped \ |
171 applyElemHideEmulation(["[-abp-properties='background-color: rgb(0, 0, 0)']"], | 193 brace", |
172 function() | 194 function(assert) |
173 { | 195 { |
174 assert.visible(toHide); | 196 var done = assert.async(); |
175 insertStyleRule("#" + toHide.id + " {background-color: #000}"); | 197 var toHide = createElementWithStyle("{background-color: #000}"); |
176 setTimeout(function() | 198 applyElemHideEmulation( |
177 { | 199 ["[-abp-properties='/background.\\x7B0,6\\x7D: rgb\\(0, 0, 0\\)/']"], |
178 assert.hidden(toHide); | 200 function() |
179 done(); | 201 { |
180 }); | 202 assert.visible(toHide); |
181 }); | 203 done(); |
182 }); | 204 } |
205 ); | |
206 } | |
207 ); | |
208 | |
209 QUnit.test( | |
210 "Property selector works for dynamically changed property", | |
211 function(assert) | |
212 { | |
213 var done = assert.async(); | |
214 var toHide = createElementWithStyle("{}"); | |
215 applyElemHideEmulation( | |
216 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], | |
217 function() | |
218 { | |
219 assert.visible(toHide); | |
220 insertStyleRule("#" + toHide.id + " {background-color: #000}"); | |
221 setTimeout(function() | |
222 { | |
223 assert.hidden(toHide); | |
224 done(); | |
225 }); | |
226 } | |
227 ); | |
228 } | |
229 ); | |
LEFT | RIGHT |