| OLD | NEW |
| 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 |
| 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 /* globals ElemHideEmulation, splitSelector, | 20 /* globals ElemHideEmulation */ |
| 21 parseSelectorContent, | |
| 22 parseSelector, positionInParent, makeSelector, | |
| 23 PlainSelector, HasSelector, PropsSelector */ | |
| 24 | 21 |
| 25 let myUrl = document.currentScript.src; | 22 let myUrl = document.currentScript.src; |
| 26 | 23 |
| 27 exports.tearDown = function(callback) | 24 exports.tearDown = function(callback) |
| 28 { | 25 { |
| 29 let styleElements = document.head.getElementsByTagName("style"); | 26 let styleElements = document.head.getElementsByTagName("style"); |
| 30 while (styleElements.length) | 27 while (styleElements.length) |
| 31 styleElements[0].parentNode.removeChild(styleElements[0]); | 28 styleElements[0].parentNode.removeChild(styleElements[0]); |
| 32 | 29 |
| 33 let child; | 30 let child; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 for (let elem of elems) | 134 for (let elem of elems) |
| 138 elem.style.display = "none"; | 135 elem.style.display = "none"; |
| 139 } | 136 } |
| 140 ); | 137 ); |
| 141 | 138 |
| 142 elemHideEmulation.apply(); | 139 elemHideEmulation.apply(); |
| 143 return Promise.resolve(elemHideEmulation); | 140 return Promise.resolve(elemHideEmulation); |
| 144 }); | 141 }); |
| 145 } | 142 } |
| 146 | 143 |
| 147 // internals testing | |
| 148 | |
| 149 exports.testParseSelectorContent = function(test) | |
| 150 { | |
| 151 loadElemHideEmulation().then(() => | |
| 152 { | |
| 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); | |
| 158 test.equal(parsed.text, "> div"); | |
| 159 test.equal(parsed.end, 5); | |
| 160 | |
| 161 // parens not closed. | |
| 162 parsed = parseSelectorContent("> div > div", 0); | |
| 163 test.equal(parsed, null); | |
| 164 }).catch(unexpectedError.bind(test)).then(() => test.done()); | |
| 165 }; | |
| 166 | |
| 167 exports.testParseSelector = function(test) | |
| 168 { | |
| 169 loadElemHideEmulation().then(() => | |
| 170 { | |
| 171 let selectors = parseSelector(""); | |
| 172 test.equal(selectors.length, 0); | |
| 173 | |
| 174 let selector = "div > :-abp-properties('background-color: rgb(0, 0, 0)')"; | |
| 175 selectors = parseSelector(selector); | |
| 176 test.equal(selectors.length, 2); | |
| 177 test.ok(selectors[0] instanceof PlainSelector); | |
| 178 test.ok(selectors[1] instanceof PropsSelector); | |
| 179 | |
| 180 selector = "div > :-abp-has(> div.inside) > div"; | |
| 181 selectors = parseSelector(selector); | |
| 182 test.equal(selectors.length, 3); | |
| 183 test.ok(selectors[0] instanceof PlainSelector); | |
| 184 test.ok(selectors[1] instanceof HasSelector); | |
| 185 test.ok(selectors[2] instanceof PlainSelector); | |
| 186 | |
| 187 selector = "div > div:-abp-has(> div.inside) > div"; | |
| 188 selectors = parseSelector(selector); | |
| 189 | |
| 190 test.equal(selectors.length, 3); | |
| 191 test.ok(selectors[0] instanceof PlainSelector); | |
| 192 test.ok(selectors[1] instanceof HasSelector); | |
| 193 test.ok(selectors[2] instanceof PlainSelector); | |
| 194 | |
| 195 selector = "div > :-abp-has(> div.inside) > :-abp-properties(background-colo
r: rgb(0, 0, 0))"; | |
| 196 selectors = parseSelector(selector); | |
| 197 | |
| 198 test.equal(selectors.length, 4); | |
| 199 test.ok(selectors[0] instanceof PlainSelector); | |
| 200 test.ok(selectors[1] instanceof HasSelector); | |
| 201 test.ok(selectors[2] instanceof PlainSelector); | |
| 202 test.ok(selectors[3] instanceof PropsSelector); | |
| 203 | |
| 204 selector = "div > :-abp-has(> div.inside > :-abp-properties(background-color
: rgb(0, 0, 0))"; | |
| 205 selectors = parseSelector(selector); | |
| 206 test.equal(selectors, null); | |
| 207 | |
| 208 // -abp-has-unsupported() is unknown. Ensure we fail parsing. | |
| 209 selector = 'div[arial-label="Story"]:-abp-has(> div > div > span > span:-abp
-unsupported("Suggested Post"))'; | |
| 210 selectors = parseSelector(selector); | |
| 211 test.equal(selectors, null); | |
| 212 }).catch(unexpectedError.bind(test)).then(() => test.done()); | |
| 213 }; | |
| 214 | |
| 215 function buildDom(doc) | |
| 216 { | |
| 217 doc.body.innerHTML = `<div id="parent"> | |
| 218 <div id="middle"> | |
| 219 <div id="middle1"><div id="inside" class="inside"></div></div> | |
| 220 </div> | |
| 221 <div id="sibling"> | |
| 222 <div id="tohide">to hide</div> | |
| 223 </div> | |
| 224 <div id="sibling2"> | |
| 225 <div id="sibling21"><div id="sibling211" class="inside"></div></div> | |
| 226 </div> | |
| 227 </div>`; | |
| 228 let parent = document.getElementById("parent"); | |
| 229 let middle = document.getElementById("middle"); | |
| 230 let middle1 = document.getElementById("middle1"); | |
| 231 let inside = document.getElementById("inside"); | |
| 232 let sibling = document.getElementById("sibling"); | |
| 233 let sibling2 = document.getElementById("sibling2"); | |
| 234 let toHide = document.getElementById("tohide"); | |
| 235 return {parent, middle, middle1, inside, sibling, sibling2, toHide}; | |
| 236 } | |
| 237 | |
| 238 exports.testPositionInParent = function(test) | |
| 239 { | |
| 240 let nodes = buildDom(document); | |
| 241 | |
| 242 loadElemHideEmulation().then(() => | |
| 243 { | |
| 244 test.equal(positionInParent(nodes.middle1), 1); | |
| 245 test.equal(positionInParent(nodes.inside), 1); | |
| 246 test.equal(positionInParent(nodes.sibling2), 3); | |
| 247 }).catch(unexpectedError.bind(test)).then(() => test.done()); | |
| 248 }; | |
| 249 | |
| 250 exports.testMakeSelector = function(test) | |
| 251 { | |
| 252 let nodes = buildDom(document); | |
| 253 | |
| 254 loadElemHideEmulation().then(() => | |
| 255 { | |
| 256 test.equal(makeSelector(nodes.middle, ""), | |
| 257 ":root > BODY:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(1)"
); | |
| 258 test.equal(makeSelector(nodes.toHide, ""), | |
| 259 ":root > BODY:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(2)
> DIV:nth-child(1)"); | |
| 260 }).catch(unexpectedError.bind(test)).then(() => test.done()); | |
| 261 }; | |
| 262 | |
| 263 exports.testPlainSelector = function(test) | |
| 264 { | |
| 265 buildDom(document); | |
| 266 | |
| 267 loadElemHideEmulation().then(() => | |
| 268 { | |
| 269 let selector = new PlainSelector("div > div"); | |
| 270 | |
| 271 let iter = selector.getSelectors("foo > "); | |
| 272 let value = iter.next(); | |
| 273 test.equal(value.value[0], "foo > div > div"); | |
| 274 test.ok(iter.next().done); | |
| 275 }).catch(unexpectedError.bind(test)).then(() => test.done()); | |
| 276 }; | |
| 277 | |
| 278 exports.testHasSelector = function(test) | |
| 279 { | |
| 280 buildDom(document); | |
| 281 | |
| 282 loadElemHideEmulation().then(() => | |
| 283 { | |
| 284 let selector = new HasSelector("> div.inside"); | |
| 285 | |
| 286 let iter = selector.getSelectors("", document, document.sheet); | |
| 287 let value = iter.next(); | |
| 288 test.ok(!value.done); | |
| 289 test.equal(value.value[0], | |
| 290 ":root > BODY:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(1)
> DIV:nth-child(1)"); | |
| 291 | |
| 292 iter = selector.getElements("", document, document.sheet); | |
| 293 value = iter.next(); | |
| 294 test.ok(!value.done); | |
| 295 test.equal(value.value.id, "middle1"); | |
| 296 value = iter.next(); | |
| 297 test.ok(!value.done); | |
| 298 test.equal(value.value.id, "sibling21"); | |
| 299 value = iter.next(); | |
| 300 test.ok(value.done); | |
| 301 | |
| 302 selector = new HasSelector(":-abp-has(> div.inside)"); | |
| 303 | |
| 304 test.ok(selector._innerSelectors); | |
| 305 }).catch(unexpectedError.bind(test)).then(() => test.done()); | |
| 306 }; | |
| 307 | |
| 308 exports.testSplitStyleRule = function(test) | |
| 309 { | |
| 310 loadElemHideEmulation().then(() => | |
| 311 { | |
| 312 let selectors = splitSelector("div:-abp-has(div) > [-abp-properties='backgro
und-color: rgb(0, 0, 0)'] > span"); | |
| 313 test.ok(selectors); | |
| 314 test.equal(selectors.length, 1, "There is only one selector"); | |
| 315 | |
| 316 selectors = splitSelector("div:-abp-has(div), [-abp-properties='background-c
olor: rgb(0, 0, 0)']"); | |
| 317 test.ok(selectors); | |
| 318 test.equal(selectors.length, 2, "There are two selectors"); | |
| 319 }).catch(unexpectedError.bind(test)).then(() => test.done()); | |
| 320 }; | |
| 321 | |
| 322 // API testing | |
| 323 | |
| 324 exports.testVerbatimPropertySelector = function(test) | 144 exports.testVerbatimPropertySelector = function(test) |
| 325 { | 145 { |
| 326 let toHide = createElementWithStyle("{background-color: #000}"); | 146 let toHide = createElementWithStyle("{background-color: #000}"); |
| 327 applyElemHideEmulation( | 147 applyElemHideEmulation( |
| 328 [":-abp-properties(background-color: rgb(0, 0, 0))"] | 148 [":-abp-properties(background-color: rgb(0, 0, 0))"] |
| 329 ).then(() => | 149 ).then(() => |
| 330 { | 150 { |
| 331 expectHidden(test, toHide); | 151 expectHidden(test, toHide); |
| 332 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 152 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| 333 }; | 153 }; |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 let parent = createElementWithStyle("{}"); | 397 let parent = createElementWithStyle("{}"); |
| 578 let child = createElementWithStyle("{background-color: #000}", parent); | 398 let child = createElementWithStyle("{background-color: #000}", parent); |
| 579 applyElemHideEmulation( | 399 applyElemHideEmulation( |
| 580 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"] | 400 ["div:-abp-has(:-abp-properties(background-color: rgb(0, 0, 0)))"] |
| 581 ).then(() => | 401 ).then(() => |
| 582 { | 402 { |
| 583 expectVisible(test, child); | 403 expectVisible(test, child); |
| 584 expectHidden(test, parent); | 404 expectHidden(test, parent); |
| 585 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 405 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| 586 }; | 406 }; |
| OLD | NEW |