| 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 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 if (styleElements.length) | 78 if (styleElements.length) |
| 79 styleElement = styleElements[0]; | 79 styleElement = styleElements[0]; |
| 80 else | 80 else |
| 81 { | 81 { |
| 82 styleElement = document.createElement("style"); | 82 styleElement = document.createElement("style"); |
| 83 document.head.appendChild(styleElement); | 83 document.head.appendChild(styleElement); |
| 84 } | 84 } |
| 85 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); | 85 styleElement.sheet.insertRule(rule, styleElement.sheet.cssRules.length); |
| 86 } | 86 } |
| 87 | 87 |
| 88 function createElementWithStyle(styleBlock) | 88 // insert a <div> with a unique id and and empty CSS rule |
| 89 // for the the selector matching the id. |
| 90 function createElementWithStyle(styleBlock, parent) |
| 89 { | 91 { |
| 90 var element = document.createElement("div"); | 92 var element = document.createElement("div"); |
| 91 element.id = findUniqueId(); | 93 element.id = findUniqueId(); |
| 92 document.body.appendChild(element); | 94 if (!parent) |
| 95 document.body.appendChild(element); |
| 96 else |
| 97 parent.appendChild(element); |
| 93 insertStyleRule("#" + element.id + " " + styleBlock); | 98 insertStyleRule("#" + element.id + " " + styleBlock); |
| 94 return element; | 99 return element; |
| 95 } | 100 } |
| 96 | 101 |
| 97 function applyElemHideEmulation(selectors, callback) | 102 // Will ensure the class ElemHideEmulation is loaded |
| 103 // and then will call the callback. |
| 104 // NOTE: if it never loads, this will probably hang in an infinite |
| 105 // loop |
| 106 function loadElemHideEmulation(callback) |
| 98 { | 107 { |
| 99 if (typeof ElemHideEmulation == "undefined") | 108 if (typeof ElemHideEmulation == "undefined") |
| 100 { | 109 { |
| 101 loadScript(myUrl + "/../../../lib/common.js", function() | 110 loadScript(myUrl + "/../../../lib/common.js", function() |
| 102 { | 111 { |
| 103 loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js", | 112 loadScript(myUrl + "/../../../chrome/content/elemHideEmulation.js", |
| 104 function() | 113 function() |
| 105 { | 114 { |
| 106 applyElemHideEmulation(selectors, callback); | 115 loadElemHideEmulation(callback); |
| 107 }); | 116 }); |
| 108 }); | 117 }); |
| 109 return; | 118 return; |
| 110 } | 119 } |
| 111 | 120 |
| 112 var elemHideEmulation = new ElemHideEmulation( | |
| 113 window, | |
| 114 function(callback) | |
| 115 { | |
| 116 var patterns = []; | |
| 117 selectors.forEach(function(selector) | |
| 118 { | |
| 119 patterns.push({selector: selector}); | |
| 120 }); | |
| 121 callback(patterns); | |
| 122 }, | |
| 123 function(selectors) | |
| 124 { | |
| 125 if (!selectors.length) | |
| 126 return; | |
| 127 var selector = selectors.join(", "); | |
| 128 insertStyleRule(selector + "{display: none !important;}"); | |
| 129 } | |
| 130 ); | |
| 131 | |
| 132 elemHideEmulation.apply(); | |
| 133 callback(); | 121 callback(); |
| 134 } | 122 } |
| 135 | 123 |
| 124 // instantiate a ElemHideEmulation with @selectors. |
| 125 function applyElemHideEmulation(selectors, callback) |
| 126 { |
| 127 loadElemHideEmulation(function() |
| 128 { |
| 129 var elemHideEmulation = new ElemHideEmulation( |
| 130 window, |
| 131 function(callback) |
| 132 { |
| 133 var patterns = []; |
| 134 selectors.forEach(function(selector) |
| 135 { |
| 136 patterns.push({selector: selector}); |
| 137 }); |
| 138 callback(patterns); |
| 139 }, |
| 140 function(selectors) |
| 141 { |
| 142 if (!selectors.length) |
| 143 return; |
| 144 var selector = selectors.join(", "); |
| 145 insertStyleRule(selector + "{display: none !important;}"); |
| 146 } |
| 147 ); |
| 148 |
| 149 elemHideEmulation.apply(); |
| 150 callback(); |
| 151 }.bind(this)); |
| 152 } |
| 153 |
| 154 exports.testPseudoHasRule = function(test) |
| 155 { |
| 156 loadElemHideEmulation(function() |
| 157 { |
| 158 var selectors = ["div:has(span)"]; |
| 159 // testing the regexp |
| 160 var match = pseudoClassHasSelectorRegExp.exec(selectors[0]); |
| 161 test.ok(match); |
| 162 test.equal(match[1], "span"); |
| 163 |
| 164 test.done(); |
| 165 }); |
| 166 }; |
| 167 |
| 168 exports.testSplitStyleRule = function(test) |
| 169 { |
| 170 loadElemHideEmulation(function() |
| 171 { |
| 172 var selectors = splitSelector("div:has(div) > [-abp-properties='background-c
olor: rgb(0, 0, 0)'] > span"); |
| 173 test.ok(selectors); |
| 174 test.equal(selectors.length, 1, "There is only one selector"); |
| 175 |
| 176 selectors = splitSelector("div:has(div), [-abp-properties='background-color:
rgb(0, 0, 0)']"); |
| 177 test.ok(selectors); |
| 178 test.equal(selectors.length, 2, "There are two selectors"); |
| 179 |
| 180 test.done(); |
| 181 }); |
| 182 }; |
| 183 |
| 136 exports.testVerbatimPropertySelector = function(test) | 184 exports.testVerbatimPropertySelector = function(test) |
| 137 { | 185 { |
| 138 var toHide = createElementWithStyle("{background-color: #000}"); | 186 var toHide = createElementWithStyle("{background-color: #000}"); |
| 139 applyElemHideEmulation( | 187 applyElemHideEmulation( |
| 140 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], | 188 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], |
| 141 function() | 189 function() |
| 142 { | 190 { |
| 143 expectHidden(test, toHide); | 191 expectHidden(test, toHide); |
| 144 test.done(); | 192 test.done(); |
| 145 } | 193 } |
| 146 ); | 194 ); |
| 147 }; | 195 }; |
| 148 | 196 |
| 197 exports.testVerbatimPropertySelectorWithPrefix = function(test) |
| 198 { |
| 199 var parent = createElementWithStyle("{background-color: #000}"); |
| 200 var toHide = createElementWithStyle("{background-color: #000}", parent); |
| 201 applyElemHideEmulation( |
| 202 ["div > [-abp-properties='background-color: rgb(0, 0, 0)']"], |
| 203 function() |
| 204 { |
| 205 expectVisible(test, parent); |
| 206 expectHidden(test, toHide); |
| 207 test.done(); |
| 208 } |
| 209 ); |
| 210 }; |
| 211 |
| 212 exports.testVerbatimPropertySelectorWithPrefixNoMatch = function(test) |
| 213 { |
| 214 var parent = createElementWithStyle("{background-color: #000}"); |
| 215 var toHide = createElementWithStyle("{background-color: #fff}", parent); |
| 216 applyElemHideEmulation( |
| 217 ["div > [-abp-properties='background-color: rgb(0, 0, 0)']"], |
| 218 function() |
| 219 { |
| 220 expectVisible(test, parent); |
| 221 expectVisible(test, toHide); |
| 222 test.done(); |
| 223 } |
| 224 ); |
| 225 }; |
| 226 |
| 227 exports.testVerbatimPropertySelectorWithSuffix = function(test) |
| 228 { |
| 229 var parent = createElementWithStyle("{background-color: #000}"); |
| 230 var toHide = createElementWithStyle("{background-color: #000}", parent); |
| 231 applyElemHideEmulation( |
| 232 ["[-abp-properties='background-color: rgb(0, 0, 0)'] > div"], |
| 233 function() |
| 234 { |
| 235 expectVisible(test, parent); |
| 236 expectHidden(test, toHide); |
| 237 test.done(); |
| 238 } |
| 239 ); |
| 240 }; |
| 241 |
| 242 exports.testVerbatimPropertySelectorWithPrefixAndSuffix = function(test) |
| 243 { |
| 244 var parent = createElementWithStyle("{background-color: #000}"); |
| 245 var middle = createElementWithStyle("{background-color: #000}", parent); |
| 246 var toHide = createElementWithStyle("{background-color: #000}", middle); |
| 247 applyElemHideEmulation( |
| 248 ["div > [-abp-properties='background-color: rgb(0, 0, 0)'] > div"], |
| 249 function() |
| 250 { |
| 251 expectVisible(test, parent); |
| 252 expectVisible(test, middle); |
| 253 expectHidden(test, toHide); |
| 254 test.done(); |
| 255 } |
| 256 ); |
| 257 }; |
| 258 |
| 149 exports.testPropertySelectorWithWildcard = function(test) | 259 exports.testPropertySelectorWithWildcard = function(test) |
| 150 { | 260 { |
| 151 var toHide = createElementWithStyle("{background-color: #000}"); | 261 var toHide = createElementWithStyle("{background-color: #000}"); |
| 152 applyElemHideEmulation( | 262 applyElemHideEmulation( |
| 153 ["[-abp-properties='*color: rgb(0, 0, 0)']"], | 263 ["[-abp-properties='*color: rgb(0, 0, 0)']"], |
| 154 function() | 264 function() |
| 155 { | 265 { |
| 156 expectHidden(test, toHide); | 266 expectHidden(test, toHide); |
| 157 test.done(); | 267 test.done(); |
| 158 } | 268 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 expectVisible(test, toHide); | 318 expectVisible(test, toHide); |
| 209 insertStyleRule("#" + toHide.id + " {background-color: #000}"); | 319 insertStyleRule("#" + toHide.id + " {background-color: #000}"); |
| 210 window.setTimeout(function() | 320 window.setTimeout(function() |
| 211 { | 321 { |
| 212 expectHidden(test, toHide); | 322 expectHidden(test, toHide); |
| 213 test.done(); | 323 test.done(); |
| 214 }, 0); | 324 }, 0); |
| 215 } | 325 } |
| 216 ); | 326 ); |
| 217 }; | 327 }; |
| 328 |
| 329 exports.testPseudoClassHasMatcher = function(test) |
| 330 { |
| 331 var parent = createElementWithStyle("{}"); |
| 332 var toHide = createElementWithStyle("{}", parent); |
| 333 loadElemHideEmulation(function() |
| 334 { |
| 335 var matcher = new PseudoHasMatcher("div"); |
| 336 test.equal(matcher.match(parent).length, 1); |
| 337 test.equal(matcher.match(toHide).length, 0); |
| 338 |
| 339 var matcher2 = new PseudoHasMatcher("span"); |
| 340 test.equal(matcher2.match(parent), 0); |
| 341 test.equal(matcher2.match(toHide), 0); |
| 342 |
| 343 test.done(); |
| 344 }); |
| 345 }; |
| 346 |
| 347 exports.testPseudoClassHasSelector = function(test) |
| 348 { |
| 349 var toHide = createElementWithStyle("{}"); |
| 350 applyElemHideEmulation( |
| 351 ["div:has(div)"], |
| 352 function() |
| 353 { |
| 354 expectVisible(test, toHide); |
| 355 test.done(); |
| 356 } |
| 357 ); |
| 358 }; |
| 359 |
| 360 exports.testPseudoClassHasSelectorWithPrefix = function(test) |
| 361 { |
| 362 var parent = createElementWithStyle("{}"); |
| 363 var toHide = createElementWithStyle("{}", parent); |
| 364 applyElemHideEmulation( |
| 365 ["div:has(div)"], |
| 366 function() |
| 367 { |
| 368 expectVisible(test, parent); |
| 369 expectHidden(test, toHide); |
| 370 test.done(); |
| 371 } |
| 372 ); |
| 373 }; |
| OLD | NEW |