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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 |
(...skipping 14 matching lines...) Expand all Loading... |
25 let myUrl = document.currentScript.src; | 25 let myUrl = document.currentScript.src; |
26 | 26 |
27 exports.tearDown = function(callback) | 27 exports.tearDown = function(callback) |
28 { | 28 { |
29 let styleElements = document.head.getElementsByTagName("style"); | 29 let styleElements = document.head.getElementsByTagName("style"); |
30 while (styleElements.length) | 30 while (styleElements.length) |
31 styleElements[0].parentNode.removeChild(styleElements[0]); | 31 styleElements[0].parentNode.removeChild(styleElements[0]); |
32 | 32 |
33 let child; | 33 let child; |
34 while (child = document.body.firstChild) | 34 while (child = document.body.firstChild) |
35 document.body.removeChild(child); | 35 child.parentNode.removeChild(child); |
36 | 36 |
37 callback(); | 37 callback(); |
38 }; | 38 }; |
39 | 39 |
40 function unexpectedError(error) | 40 function unexpectedError(error) |
41 { | 41 { |
42 console.error(error); | 42 console.error(error); |
43 this.ok(false, "Unexpected error: " + error); | 43 this.ok(false, "Unexpected error: " + error); |
44 } | 44 } |
45 | 45 |
(...skipping 24 matching lines...) Expand all Loading... |
70 if (styleElements.length) | 70 if (styleElements.length) |
71 styleElement = styleElements[0]; | 71 styleElement = styleElements[0]; |
72 else | 72 else |
73 { | 73 { |
74 styleElement = document.createElement("style"); | 74 styleElement = document.createElement("style"); |
75 document.head.appendChild(styleElement); | 75 document.head.appendChild(styleElement); |
76 } | 76 } |
77 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); | 77 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); |
78 } | 78 } |
79 | 79 |
80 // insert a <div> with a unique id and and empty CSS rule | 80 // insert a <div> with a unique id and a CSS rule |
81 // for the the selector matching the id. | 81 // for the the selector matching the id. |
82 function createElementWithStyle(styleBlock, parent) | 82 function createElementWithStyle(styleBlock, parent) |
83 { | 83 { |
84 let element = document.createElement("div"); | 84 let element = document.createElement("div"); |
85 element.id = findUniqueId(); | 85 element.id = findUniqueId(); |
86 if (!parent) | 86 if (!parent) |
87 document.body.appendChild(element); | 87 document.body.appendChild(element); |
88 else | 88 else |
89 parent.appendChild(element); | 89 parent.appendChild(element); |
90 insertStyleRule("#" + element.id + " " + styleBlock); | 90 insertStyleRule("#" + element.id + " " + styleBlock); |
91 return element; | 91 return element; |
92 } | 92 } |
93 | 93 |
94 // Will ensure the class ElemHideEmulation is loaded | 94 // Will ensure the class ElemHideEmulation is loaded. |
95 // and then will call the callback. | 95 // NOTE: if it never loads, this will probably hang. |
96 // NOTE: if it never loads, this will probably hang in an infinite | |
97 // loop | |
98 function loadElemHideEmulation() | 96 function loadElemHideEmulation() |
99 { | 97 { |
100 if (typeof ElemHideEmulation == "undefined") | 98 if (typeof ElemHideEmulation == "undefined") |
101 { | 99 { |
102 return loadScript(myUrl + "/../../../lib/common.js").then(() => | 100 return loadScript(myUrl + "/../../../lib/common.js").then(() => |
103 { | 101 { |
104 return loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js")
; | 102 return loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js")
; |
105 }).then(() => | 103 }).then(() => |
106 { | 104 { |
107 return loadElemHideEmulation(); | 105 return loadElemHideEmulation(); |
108 }); | 106 }); |
109 } | 107 } |
110 | 108 |
111 return Promise.resolve(); | 109 return Promise.resolve(); |
112 } | 110 } |
113 | 111 |
114 // instantiate a ElemHideEmulation with @selectors. | 112 // Create a new ElemHideEmulation instance with @selectors. |
115 function applyElemHideEmulation(selectors) | 113 function applyElemHideEmulation(selectors) |
116 { | 114 { |
117 return loadElemHideEmulation().then(() => | 115 return loadElemHideEmulation().then(() => |
118 { | 116 { |
119 let elemHideEmulation = new ElemHideEmulation( | 117 let elemHideEmulation = new ElemHideEmulation( |
120 window, | 118 window, |
121 callback => | 119 callback => |
122 { | 120 { |
123 let patterns = []; | 121 let patterns = []; |
124 selectors.forEach(selector => | 122 selectors.forEach(selector => |
125 { | 123 { |
126 patterns.push({selector}); | 124 patterns.push({selector}); |
127 }); | 125 }); |
128 callback(patterns); | 126 callback(patterns); |
129 }, | 127 }, |
130 newSelectors => | 128 newSelectors => |
131 { | 129 { |
132 if (!newSelectors.length) | 130 if (!newSelectors.length) |
133 return; | 131 return; |
134 let selector = newSelectors.join(", "); | 132 let selector = newSelectors.join(", "); |
135 insertStyleRule(selector + "{display: none !important;}"); | 133 insertStyleRule(selector + "{display: none !important;}"); |
136 }, | 134 }, |
137 elems => | 135 elems => |
138 { | 136 { |
139 if (!elems.length) | |
140 return; | |
141 for (let elem of elems) | 137 for (let elem of elems) |
142 elem.style.display = "none"; | 138 elem.style.display = "none"; |
143 } | 139 } |
144 ); | 140 ); |
145 | 141 |
146 elemHideEmulation.apply(); | 142 elemHideEmulation.apply(); |
147 return Promise.resolve(); | 143 return Promise.resolve(elemHideEmulation); |
148 }); | 144 }); |
149 } | 145 } |
150 | 146 |
| 147 // internals testing |
| 148 |
151 exports.testParseSelectorContent = function(test) | 149 exports.testParseSelectorContent = function(test) |
152 { | 150 { |
153 loadElemHideEmulation().then(() => | 151 loadElemHideEmulation().then(() => |
154 { | 152 { |
155 let parsed = parseSelectorContent("> div) > div", 0); | 153 let parsed = parseSelectorContent(":-abp-has(> div) > div", 10); |
| 154 test.equal(parsed.text, "> div"); |
| 155 test.equal(parsed.end, 15); |
| 156 |
| 157 parsed = parseSelectorContent("> div) > div", 0); |
156 test.equal(parsed.text, "> div"); | 158 test.equal(parsed.text, "> div"); |
157 test.equal(parsed.end, 5); | 159 test.equal(parsed.end, 5); |
158 | |
159 parsed = parseSelectorContent("\"> div\") > div", 0); | |
160 test.equal(parsed.text, "\"> div\""); | |
161 test.equal(parsed.end, 7); | |
162 parsed = parseSelectorContent(" \"> div\" ) > div", 0); | |
163 test.equal(parsed.text, " \"> div\" "); | |
164 test.equal(parsed.end, 9); | |
165 | 160 |
166 // parens not closed. | 161 // parens not closed. |
167 parsed = parseSelectorContent("> div > div", 0); | 162 parsed = parseSelectorContent("> div > div", 0); |
168 test.equal(parsed, null); | 163 test.equal(parsed, null); |
169 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 164 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
170 }; | 165 }; |
171 | 166 |
172 exports.testParseSelector = function(test) | 167 exports.testParseSelector = function(test) |
173 { | 168 { |
174 loadElemHideEmulation().then(() => | 169 loadElemHideEmulation().then(() => |
175 { | 170 { |
176 let {selectors, hide} = parseSelector(""); | 171 let selectors = parseSelector(""); |
177 test.equal(selectors.length, 0); | 172 test.equal(selectors.length, 0); |
178 test.ok(!hide); | |
179 | 173 |
180 let selector = "div > :-abp-properties('background-color: rgb(0, 0, 0)')"; | 174 let selector = "div > :-abp-properties('background-color: rgb(0, 0, 0)')"; |
181 let parsed = parseSelector(selector); | 175 selectors = parseSelector(selector); |
182 test.equal(parsed.selectors.length, 2); | 176 test.equal(selectors.length, 2); |
183 test.ok(!parsed.hide); | 177 test.ok(selectors[0] instanceof PlainSelector); |
184 test.ok(parsed.selectors[0] instanceof PlainSelector); | 178 test.ok(selectors[1] instanceof PropsSelector); |
185 test.ok(parsed.selectors[1] instanceof PropsSelector); | |
186 | 179 |
187 selector = "div > :-abp-has(> div.inside) > div"; | 180 selector = "div > :-abp-has(> div.inside) > div"; |
188 parsed = parseSelector(selector); | 181 selectors = parseSelector(selector); |
189 test.equal(parsed.selectors.length, 3); | 182 test.equal(selectors.length, 3); |
190 test.ok(parsed.hide); | 183 test.ok(selectors[0] instanceof PlainSelector); |
191 test.ok(parsed.selectors[0] instanceof PlainSelector); | 184 test.ok(selectors[1] instanceof HasSelector); |
192 test.ok(parsed.selectors[1] instanceof HasSelector); | 185 test.ok(selectors[2] instanceof PlainSelector); |
193 test.ok(parsed.selectors[2] instanceof PlainSelector); | |
194 | 186 |
195 selector = "div > div:-abp-has(> div.inside) > div"; | 187 selector = "div > div:-abp-has(> div.inside) > div"; |
196 parsed = parseSelector(selector); | 188 selectors = parseSelector(selector); |
197 | 189 |
198 test.equal(parsed.selectors.length, 3); | 190 test.equal(selectors.length, 3); |
199 test.ok(parsed.hide); | 191 test.ok(selectors[0] instanceof PlainSelector); |
200 test.ok(parsed.selectors[0] instanceof PlainSelector); | 192 test.ok(selectors[1] instanceof HasSelector); |
201 test.ok(parsed.selectors[1] instanceof HasSelector); | 193 test.ok(selectors[2] instanceof PlainSelector); |
202 test.ok(parsed.selectors[2] instanceof PlainSelector); | |
203 | 194 |
204 selector = "div > :-abp-has(> div.inside) > :-abp-properties(background-colo
r: rgb(0, 0, 0))"; | 195 selector = "div > :-abp-has(> div.inside) > :-abp-properties(background-colo
r: rgb(0, 0, 0))"; |
205 parsed = parseSelector(selector); | 196 selectors = parseSelector(selector); |
206 | 197 |
207 test.equal(parsed.selectors.length, 4); | 198 test.equal(selectors.length, 4); |
208 test.ok(parsed.hide); | 199 test.ok(selectors[0] instanceof PlainSelector); |
209 test.ok(parsed.selectors[0] instanceof PlainSelector); | 200 test.ok(selectors[1] instanceof HasSelector); |
210 test.ok(parsed.selectors[1] instanceof HasSelector); | 201 test.ok(selectors[2] instanceof PlainSelector); |
211 test.ok(parsed.selectors[2] instanceof PlainSelector); | 202 test.ok(selectors[3] instanceof PropsSelector); |
212 test.ok(parsed.selectors[3] instanceof PropsSelector); | |
213 | 203 |
214 selector = "div > :-abp-has(> div.inside > :-abp-properties(background-color
: rgb(0, 0, 0))"; | 204 selector = "div > :-abp-has(> div.inside > :-abp-properties(background-color
: rgb(0, 0, 0))"; |
215 parsed = parseSelector(selector); | 205 selectors = parseSelector(selector); |
216 test.equal(parsed.selectors, null); | 206 test.equal(selectors, null); |
217 | 207 |
218 // -abp-has-unsupported() is unknown. Ensure we fail parsing. | 208 // -abp-has-unsupported() is unknown. Ensure we fail parsing. |
219 selector = 'div[arial-label="Story"]:-abp-has(> div > div > span > span:-abp
-unsupported("Suggested Post"))'; | 209 selector = 'div[arial-label="Story"]:-abp-has(> div > div > span > span:-abp
-unsupported("Suggested Post"))'; |
220 parsed = parseSelector(selector); | 210 selectors = parseSelector(selector); |
221 test.equal(parsed.selectors, null); | 211 test.equal(selectors, null); |
222 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 212 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
223 }; | 213 }; |
224 | 214 |
225 function buildDom(doc) | 215 function buildDom(doc) |
226 { | 216 { |
227 doc.body.innerHTML = `<div id="parent"> | 217 doc.body.innerHTML = `<div id="parent"> |
228 <div id="middle"> | 218 <div id="middle"> |
229 <div id="middle1"><div id="inside" class="inside"></div></div> | 219 <div id="middle1"><div id="inside" class="inside"></div></div> |
230 </div> | 220 </div> |
231 <div id="sibling"> | 221 <div id="sibling"> |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 let selectors = splitSelector("div:-abp-has(div) > [-abp-properties='backgro
und-color: rgb(0, 0, 0)'] > span"); | 312 let selectors = splitSelector("div:-abp-has(div) > [-abp-properties='backgro
und-color: rgb(0, 0, 0)'] > span"); |
323 test.ok(selectors); | 313 test.ok(selectors); |
324 test.equal(selectors.length, 1, "There is only one selector"); | 314 test.equal(selectors.length, 1, "There is only one selector"); |
325 | 315 |
326 selectors = splitSelector("div:-abp-has(div), [-abp-properties='background-c
olor: rgb(0, 0, 0)']"); | 316 selectors = splitSelector("div:-abp-has(div), [-abp-properties='background-c
olor: rgb(0, 0, 0)']"); |
327 test.ok(selectors); | 317 test.ok(selectors); |
328 test.equal(selectors.length, 2, "There are two selectors"); | 318 test.equal(selectors.length, 2, "There are two selectors"); |
329 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 319 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
330 }; | 320 }; |
331 | 321 |
| 322 // API testing |
| 323 |
332 exports.testVerbatimPropertySelector = function(test) | 324 exports.testVerbatimPropertySelector = function(test) |
333 { | 325 { |
334 let toHide = createElementWithStyle("{background-color: #000}"); | 326 let toHide = createElementWithStyle("{background-color: #000}"); |
335 applyElemHideEmulation( | 327 applyElemHideEmulation( |
336 [":-abp-properties(background-color: rgb(0, 0, 0))"] | 328 [":-abp-properties(background-color: rgb(0, 0, 0))"] |
337 ).then(() => | 329 ).then(() => |
338 { | 330 { |
339 expectHidden(test, toHide); | 331 expectHidden(test, toHide); |
340 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 332 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
341 }; | 333 }; |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 let parent = createElementWithStyle("{}"); | 577 let parent = createElementWithStyle("{}"); |
586 let child = createElementWithStyle("{background-color: #000}", parent); | 578 let child = createElementWithStyle("{background-color: #000}", parent); |
587 applyElemHideEmulation( | 579 applyElemHideEmulation( |
588 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"] | 580 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"] |
589 ).then(() => | 581 ).then(() => |
590 { | 582 { |
591 expectVisible(test, child); | 583 expectVisible(test, child); |
592 expectHidden(test, parent); | 584 expectHidden(test, parent); |
593 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 585 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
594 }; | 586 }; |
LEFT | RIGHT |