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 function(elements) |
| 148 { |
| 149 console.log("hideElementsFunc", elements.length); |
| 150 if (!elements.length) |
| 151 return; |
| 152 for (var i = 0; i < elements.length; i++) |
| 153 elements[i].style.display = "none"; |
| 154 } |
| 155 ); |
| 156 |
| 157 elemHideEmulation.apply(); |
| 158 callback(); |
| 159 }.bind(this)); |
| 160 } |
| 161 |
| 162 exports.testPseudoHasRule = function(test) |
| 163 { |
| 164 loadElemHideEmulation(function() |
| 165 { |
| 166 var selectors = ["div:has(span)"]; |
| 167 // testing the regexp |
| 168 var match = pseudoClassHasSelectorRegExp.exec(selectors[0]); |
| 169 test.ok(match); |
| 170 test.equal(match[1], "span"); |
| 171 |
| 172 test.done(); |
| 173 }); |
| 174 }; |
| 175 |
| 176 exports.testSplitStyleRule = function(test) |
| 177 { |
| 178 loadElemHideEmulation(function() |
| 179 { |
| 180 var selectors = splitSelector("div:has(div) > [-abp-properties='background-c
olor: rgb(0, 0, 0)'] > span"); |
| 181 test.ok(selectors); |
| 182 test.equal(selectors.length, 1, "There is only one selector"); |
| 183 |
| 184 selectors = splitSelector("div:has(div), [-abp-properties='background-color:
rgb(0, 0, 0)']"); |
| 185 test.ok(selectors); |
| 186 test.equal(selectors.length, 2, "There are two selectors"); |
| 187 |
| 188 test.done(); |
| 189 }); |
| 190 }; |
| 191 |
136 exports.testVerbatimPropertySelector = function(test) | 192 exports.testVerbatimPropertySelector = function(test) |
137 { | 193 { |
138 var toHide = createElementWithStyle("{background-color: #000}"); | 194 var toHide = createElementWithStyle("{background-color: #000}"); |
139 applyElemHideEmulation( | 195 applyElemHideEmulation( |
140 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], | 196 ["[-abp-properties='background-color: rgb(0, 0, 0)']"], |
141 function() | 197 function() |
142 { | 198 { |
143 expectHidden(test, toHide); | 199 expectHidden(test, toHide); |
144 test.done(); | 200 test.done(); |
145 } | 201 } |
146 ); | 202 ); |
147 }; | 203 }; |
148 | 204 |
| 205 exports.testVerbatimPropertySelectorWithPrefix = function(test) |
| 206 { |
| 207 var parent = createElementWithStyle("{background-color: #000}"); |
| 208 var toHide = createElementWithStyle("{background-color: #000}", parent); |
| 209 applyElemHideEmulation( |
| 210 ["div > [-abp-properties='background-color: rgb(0, 0, 0)']"], |
| 211 function() |
| 212 { |
| 213 expectVisible(test, parent); |
| 214 expectHidden(test, toHide); |
| 215 test.done(); |
| 216 } |
| 217 ); |
| 218 }; |
| 219 |
| 220 exports.testVerbatimPropertySelectorWithPrefixNoMatch = function(test) |
| 221 { |
| 222 var parent = createElementWithStyle("{background-color: #000}"); |
| 223 var toHide = createElementWithStyle("{background-color: #fff}", parent); |
| 224 applyElemHideEmulation( |
| 225 ["div > [-abp-properties='background-color: rgb(0, 0, 0)']"], |
| 226 function() |
| 227 { |
| 228 expectVisible(test, parent); |
| 229 expectVisible(test, toHide); |
| 230 test.done(); |
| 231 } |
| 232 ); |
| 233 }; |
| 234 |
| 235 exports.testVerbatimPropertySelectorWithSuffix = function(test) |
| 236 { |
| 237 var parent = createElementWithStyle("{background-color: #000}"); |
| 238 var toHide = createElementWithStyle("{background-color: #000}", parent); |
| 239 applyElemHideEmulation( |
| 240 ["[-abp-properties='background-color: rgb(0, 0, 0)'] > div"], |
| 241 function() |
| 242 { |
| 243 expectVisible(test, parent); |
| 244 expectHidden(test, toHide); |
| 245 test.done(); |
| 246 } |
| 247 ); |
| 248 }; |
| 249 |
| 250 exports.testVerbatimPropertySelectorWithPrefixAndSuffix = function(test) |
| 251 { |
| 252 var parent = createElementWithStyle("{background-color: #000}"); |
| 253 var middle = createElementWithStyle("{background-color: #000}", parent); |
| 254 var toHide = createElementWithStyle("{background-color: #000}", middle); |
| 255 applyElemHideEmulation( |
| 256 ["div > [-abp-properties='background-color: rgb(0, 0, 0)'] > div"], |
| 257 function() |
| 258 { |
| 259 expectVisible(test, parent); |
| 260 expectVisible(test, middle); |
| 261 expectHidden(test, toHide); |
| 262 test.done(); |
| 263 } |
| 264 ); |
| 265 }; |
| 266 |
149 exports.testPropertySelectorWithWildcard = function(test) | 267 exports.testPropertySelectorWithWildcard = function(test) |
150 { | 268 { |
151 var toHide = createElementWithStyle("{background-color: #000}"); | 269 var toHide = createElementWithStyle("{background-color: #000}"); |
152 applyElemHideEmulation( | 270 applyElemHideEmulation( |
153 ["[-abp-properties='*color: rgb(0, 0, 0)']"], | 271 ["[-abp-properties='*color: rgb(0, 0, 0)']"], |
154 function() | 272 function() |
155 { | 273 { |
156 expectHidden(test, toHide); | 274 expectHidden(test, toHide); |
157 test.done(); | 275 test.done(); |
158 } | 276 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 expectVisible(test, toHide); | 326 expectVisible(test, toHide); |
209 insertStyleRule("#" + toHide.id + " {background-color: #000}"); | 327 insertStyleRule("#" + toHide.id + " {background-color: #000}"); |
210 window.setTimeout(function() | 328 window.setTimeout(function() |
211 { | 329 { |
212 expectHidden(test, toHide); | 330 expectHidden(test, toHide); |
213 test.done(); | 331 test.done(); |
214 }, 0); | 332 }, 0); |
215 } | 333 } |
216 ); | 334 ); |
217 }; | 335 }; |
| 336 |
| 337 exports.testPseudoClassHasMatcher = function(test) |
| 338 { |
| 339 var parent = createElementWithStyle("{}"); |
| 340 var child = createElementWithStyle("{}", parent); |
| 341 loadElemHideEmulation(function() |
| 342 { |
| 343 var matcher = new PseudoHasMatcher("div"); |
| 344 test.equal(matcher.match(parent).length, 1, "Parent should contain what is e
xpected"); |
| 345 test.equal(matcher.match(child).length, 0, "Child shouldn't match"); |
| 346 |
| 347 var matcher2 = new PseudoHasMatcher("span"); |
| 348 test.equal(matcher2.match(parent), 0, "Doesn't have a <span> child, shouldn'
t mactch"); |
| 349 test.equal(matcher2.match(child), 0, "Child shouldn't match"); |
| 350 |
| 351 test.done(); |
| 352 }); |
| 353 }; |
| 354 |
| 355 exports.testPseudoClassHasSelector = function(test) |
| 356 { |
| 357 var toHide = createElementWithStyle("{}"); |
| 358 applyElemHideEmulation( |
| 359 ["div:has(div)"], |
| 360 function() |
| 361 { |
| 362 expectVisible(test, toHide); |
| 363 test.done(); |
| 364 } |
| 365 ); |
| 366 }; |
| 367 |
| 368 exports.testPseudoClassHasSelectorWithPrefix = function(test) |
| 369 { |
| 370 var parent = createElementWithStyle("{}"); |
| 371 var child = createElementWithStyle("{}", parent); |
| 372 applyElemHideEmulation( |
| 373 ["div:has(div)"], |
| 374 function() |
| 375 { |
| 376 expectVisible(test, parent); |
| 377 expectHidden(test, child); |
| 378 test.done(); |
| 379 } |
| 380 ); |
| 381 }; |
| 382 |
| 383 exports.testPseudoClassHasSelectorWithSuffix = function(test) |
| 384 { |
| 385 var parent = createElementWithStyle("{}"); |
| 386 var middle = createElementWithStyle("{}", parent); |
| 387 var child = createElementWithStyle("{}", middle); |
| 388 applyElemHideEmulation( |
| 389 ["div:has(div) > div"], |
| 390 function() |
| 391 { |
| 392 expectVisible(test, parent); |
| 393 expectVisible(test, middle); |
| 394 expectHidden(test, child); |
| 395 test.done(); |
| 396 } |
| 397 ); |
| 398 }; |
| 399 |
OLD | NEW |