| 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 |
| 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 /* globals filterToRegExp */ | 18 /* globals filterToRegExp */ |
| 19 | 19 |
| 20 "use strict"; | 20 "use strict"; |
| 21 | 21 |
| 22 let propertySelectorRegExp = /\[-abp-properties=(["'])([^"']+)\1\]/; | 22 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i; |
| 23 let pseudoClassHasSelectorRegExp = /:-abp-has\((.*)\)/; | 23 |
| 24 let pseudoClassPropsSelectorRegExp = /:-abp-properties\((["'])([^"']+)\1\)/; | 24 let reportError = () => {}; |
| 25 | 25 |
| 26 function splitSelector(selector) | 26 function splitSelector(selector) |
| 27 { | 27 { |
| 28 if (selector.indexOf(",") == -1) | 28 if (selector.indexOf(",") == -1) |
| 29 return [selector]; | 29 return [selector]; |
| 30 | 30 |
| 31 let selectors = []; | 31 let selectors = []; |
| 32 let start = 0; | 32 let start = 0; |
| 33 let level = 0; | 33 let level = 0; |
| 34 let sep = ""; | 34 let sep = ""; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 54 selectors.push(selector.substring(start, i)); | 54 selectors.push(selector.substring(start, i)); |
| 55 start = i + 1; | 55 start = i + 1; |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 | 59 |
| 60 selectors.push(selector.substring(start)); | 60 selectors.push(selector.substring(start)); |
| 61 return selectors; | 61 return selectors; |
| 62 } | 62 } |
| 63 | 63 |
| 64 // 1 base index like for :nth-child() | 64 /** Return position of node from parent. |
| 65 * @param {Node} node the node to find the position of. |
| 66 * @return {number} One-based index like for :nth-child(), or 0 on error. |
| 67 */ |
| 65 function positionInParent(node) | 68 function positionInParent(node) |
| 66 { | 69 { |
| 67 let parentNode = node ? node.parentNode : null; | 70 let {children} = node.parentNode; |
| 68 if (parentNode == null) | 71 for (let i = 0; i < children.length; i++) |
| 69 return 0; | |
| 70 | |
| 71 let {children} = parentNode; | |
| 72 if (!children) | |
| 73 return 0; | |
| 74 let i = 0; | |
| 75 for (i = 0; i < children.length; i++) | |
| 76 if (children[i] == node) | 72 if (children[i] == node) |
| 77 break; | 73 return i + 1; |
| 78 return i + 1; | 74 return 0; |
| 79 } | 75 } |
| 80 | 76 |
| 81 function makeSelector(node, selector) | 77 function makeSelector(node, selector) |
| 82 { | 78 { |
| 79 if (!node.parentElement) |
| 80 { |
| 81 let newSelector = ":root"; |
| 82 if (selector) |
| 83 newSelector += " > " + selector; |
| 84 return newSelector; |
| 85 } |
| 83 let idx = positionInParent(node); | 86 let idx = positionInParent(node); |
| 84 if (idx > 0) | 87 if (idx > 0) |
| 85 { | 88 { |
| 86 let newSelector = `:nth-child(${idx}) `; | 89 let newSelector = `${node.tagName}:nth-child(${idx})`; |
| 87 if (selector != "") | 90 if (selector) |
| 88 newSelector += "> "; | 91 newSelector += " > " + selector; |
| 89 return makeSelector(node.parentNode, newSelector + selector); | 92 return makeSelector(node.parentElement, newSelector); |
| 90 } | 93 } |
| 91 | 94 |
| 92 return selector; | 95 return selector; |
| 93 } | 96 } |
| 94 | 97 |
| 95 function matchChildren(e, selector) | 98 function parseSelectorContent(content, startIndex) |
| 96 { | 99 { |
| 97 let newSelector = makeSelector(e, ""); | 100 let parens = 1; |
| 98 newSelector += selector; | 101 let quote = null; |
| 99 | 102 let i = startIndex; |
| 100 return document.querySelector(newSelector) != null; | 103 for (; i < content.length; i++) |
| 101 } | 104 { |
| 102 | 105 let c = content[i]; |
| 103 function selectChildren(e, selector) | 106 if (c == "\\") |
| 104 { | 107 { |
| 105 let newSelector = makeSelector(e, ""); | 108 // Ignore escaped characters |
| 106 newSelector += selector; | 109 i++; |
| 107 | 110 } |
| 108 return document.querySelectorAll(newSelector); | 111 else if (quote) |
| 109 } | 112 { |
| 110 | 113 if (c == quote) |
| 111 function parsePattern(pattern) | 114 quote = null; |
| 112 { | 115 } |
| 113 let {selector} = pattern; | 116 else if (c == "'" || c == '"') |
| 114 let match = pseudoClassHasSelectorRegExp.exec(selector); | 117 quote = c; |
| 115 if (match) | 118 else if (c == "(") |
| 116 return { | 119 parens++; |
| 117 type: "has", | 120 else if (c == ")") |
| 118 text: pattern.text, | 121 { |
| 119 elementMatcher: new PseudoHasMatcher(match[1]), | 122 parens--; |
| 120 prefix: selector.substr(0, match.index).trim(), | 123 if (parens == 0) |
| 121 suffix: selector.substr(match.index + match[0].length).trim() | 124 break; |
| 122 }; | 125 } |
| 123 match = pseudoClassPropsSelectorRegExp.exec(selector); | 126 } |
| 124 | 127 |
| 128 if (parens > 0) |
| 129 return null; |
| 130 return {text: content.substring(startIndex, i), end: i}; |
| 131 } |
| 132 |
| 133 /** Parse the selector |
| 134 * @param {string} selector the selector to parse |
| 135 * @return {Object} selectors is an array of objects, |
| 136 * or null in case of errors. hide is true if we'll hide |
| 137 * elements instead of styles.. |
| 138 */ |
| 139 function parseSelector(selector) |
| 140 { |
| 141 if (selector.length == 0) |
| 142 return []; |
| 143 |
| 144 let match = abpSelectorRegexp.exec(selector); |
| 125 if (!match) | 145 if (!match) |
| 126 match = propertySelectorRegExp.exec(selector); | 146 return [new PlainSelector(selector)]; |
| 127 if (match) | 147 |
| 128 { | 148 let selectors = []; |
| 129 let regexpString; | 149 if (match.index > 0) |
| 130 let propertyExpression = match[2]; | 150 selectors.push(new PlainSelector(selector.substr(0, match.index))); |
| 131 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | 151 |
| 132 propertyExpression[propertyExpression.length - 1] == "/") | 152 let startIndex = match.index + match[0].length; |
| 133 regexpString = propertyExpression.slice(1, -1) | 153 let content = parseSelectorContent(selector, startIndex); |
| 154 if (!content) |
| 155 { |
| 156 reportError(new SyntaxError("Failed to parse Adblock Plus " + |
| 157 `selector ${selector}, ` + |
| 158 "due to unmatched parentheses.")); |
| 159 return null; |
| 160 } |
| 161 if (match[1] == "properties") |
| 162 selectors.push(new PropsSelector(content.text)); |
| 163 else if (match[1] == "has") |
| 164 { |
| 165 let hasSelector = new HasSelector(content.text); |
| 166 if (!hasSelector.valid()) |
| 167 return null; |
| 168 selectors.push(hasSelector); |
| 169 } |
| 170 else |
| 171 { |
| 172 // this is an error, can't parse selector. |
| 173 reportError(new SyntaxError("Failed to parse Adblock Plus " + |
| 174 `selector ${selector}, invalid ` + |
| 175 `pseudo-class :-abp-${match[1]}().`)); |
| 176 return null; |
| 177 } |
| 178 |
| 179 let suffix = parseSelector(selector.substr(content.end + 1)); |
| 180 if (suffix == null) |
| 181 return null; |
| 182 |
| 183 selectors.push(...suffix); |
| 184 |
| 185 return selectors; |
| 186 } |
| 187 |
| 188 /** Stringified style objects |
| 189 * @typedef {Object} StringifiedStyle |
| 190 * @property {string} style CSS style represented by a string. |
| 191 * @property {string[]} subSelectors selectors the CSS properties apply to. |
| 192 */ |
| 193 |
| 194 /** |
| 195 * Produce a string representation of the stylesheet entry. |
| 196 * @param {CSSStyleRule} rule the CSS style rule. |
| 197 * @return {StringifiedStyle} the stringified style. |
| 198 */ |
| 199 function stringifyStyle(rule) |
| 200 { |
| 201 let styles = []; |
| 202 for (let i = 0; i < rule.style.length; i++) |
| 203 { |
| 204 let property = rule.style.item(i); |
| 205 let value = rule.style.getPropertyValue(property); |
| 206 let priority = rule.style.getPropertyPriority(property); |
| 207 styles.push(`${property}: ${value}${priority ? " !" + priority : ""};`); |
| 208 } |
| 209 styles.sort(); |
| 210 return { |
| 211 style: styles.join(" "), |
| 212 subSelectors: splitSelector(rule.selectorText) |
| 213 }; |
| 214 } |
| 215 |
| 216 function* evaluate(chain, index, prefix, subtree, styles) |
| 217 { |
| 218 if (index >= chain.length) |
| 219 { |
| 220 yield prefix; |
| 221 return; |
| 222 } |
| 223 for (let [selector, element] of |
| 224 chain[index].getSelectors(prefix, subtree, styles)) |
| 225 yield* evaluate(chain, index + 1, selector, element, styles); |
| 226 } |
| 227 |
| 228 function PlainSelector(selector) |
| 229 { |
| 230 this._selector = selector; |
| 231 } |
| 232 |
| 233 PlainSelector.prototype = { |
| 234 /** |
| 235 * Generator function returning a pair of selector |
| 236 * string and subtree. |
| 237 * @param {string} prefix the prefix for the selector. |
| 238 * @param {Node} subtree the subtree we work on. |
| 239 * @param {StringifiedStyle[]} styles the stringified style objects. |
| 240 */ |
| 241 *getSelectors(prefix, subtree, styles) |
| 242 { |
| 243 yield [prefix + this._selector, subtree]; |
| 244 } |
| 245 }; |
| 246 |
| 247 const incompletePrefixRegexp = /[\s>+~]$/; |
| 248 |
| 249 function HasSelector(selector) |
| 250 { |
| 251 this._innerSelectors = parseSelector(selector); |
| 252 } |
| 253 |
| 254 HasSelector.prototype = { |
| 255 requiresHiding: true, |
| 256 |
| 257 valid() |
| 258 { |
| 259 return this._innerSelectors != null; |
| 260 }, |
| 261 |
| 262 *getSelectors(prefix, subtree, styles) |
| 263 { |
| 264 for (let element of this.getElements(prefix, subtree, styles)) |
| 265 yield [makeSelector(element, ""), element]; |
| 266 }, |
| 267 |
| 268 /** |
| 269 * Generator function returning selected elements. |
| 270 * @param {string} prefix the prefix for the selector. |
| 271 * @param {Node} subtree the subtree we work on. |
| 272 * @param {StringifiedStyle[]} styles the stringified style objects. |
| 273 */ |
| 274 *getElements(prefix, subtree, styles) |
| 275 { |
| 276 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? |
| 277 prefix + "*" : prefix; |
| 278 let elements = subtree.querySelectorAll(actualPrefix); |
| 279 for (let element of elements) |
| 280 { |
| 281 let newPrefix = makeSelector(element, ""); |
| 282 let iter = evaluate(this._innerSelectors, 0, newPrefix + " ", |
| 283 element, styles); |
| 284 for (let selector of iter) |
| 285 // we insert a space between the two. It becomes a no-op if selector |
| 286 // doesn't have a combinator |
| 287 if (subtree.querySelector(selector)) |
| 288 yield element; |
| 289 } |
| 290 } |
| 291 }; |
| 292 |
| 293 function PropsSelector(propertyExpression) |
| 294 { |
| 295 let regexpString; |
| 296 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && |
| 297 propertyExpression[propertyExpression.length - 1] == "/") |
| 298 { |
| 299 regexpString = propertyExpression.slice(1, -1) |
| 134 .replace("\\x7B ", "{").replace("\\x7D ", "}"); | 300 .replace("\\x7B ", "{").replace("\\x7D ", "}"); |
| 135 else | 301 } |
| 136 regexpString = filterToRegExp(propertyExpression); | 302 else |
| 137 return { | 303 regexpString = filterToRegExp(propertyExpression); |
| 138 type: "props", | 304 |
| 139 text: pattern.text, | 305 this._regexp = new RegExp(regexpString, "i"); |
| 140 regexp: new RegExp(regexpString, "i"), | 306 } |
| 141 prefix: selector.substr(0, match.index), | 307 |
| 142 suffix: selector.substr(match.index + match[0].length) | 308 PropsSelector.prototype = { |
| 143 }; | 309 *findPropsSelectors(styles, prefix, regexp) |
| 144 } | 310 { |
| 145 } | 311 for (let style of styles) |
| 146 | 312 if (regexp.test(style.style)) |
| 147 function matchStyleProps(style, rule, pattern, selectors, filters) | 313 for (let subSelector of style.subSelectors) |
| 148 { | 314 yield prefix + subSelector; |
| 149 if (pattern.regexp.test(style)) | 315 }, |
| 150 { | 316 |
| 151 let subSelectors = splitSelector(rule.selectorText); | 317 *getSelectors(prefix, subtree, styles) |
| 152 for (let i = 0; i < subSelectors.length; i++) | 318 { |
| 153 { | 319 for (let selector of this.findPropsSelectors(styles, prefix, this._regexp)) |
| 154 let subSelector = subSelectors[i]; | 320 yield [selector, subtree]; |
| 155 selectors.push(pattern.prefix + subSelector + pattern.suffix); | |
| 156 filters.push(pattern.text); | |
| 157 } | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 function findPropsSelectors(stylesheet, patterns, selectors, filters) | |
| 162 { | |
| 163 let rules = stylesheet.cssRules; | |
| 164 if (!rules) | |
| 165 return; | |
| 166 | |
| 167 for (let rule of rules) | |
| 168 { | |
| 169 if (rule.type != rule.STYLE_RULE) | |
| 170 continue; | |
| 171 | |
| 172 let style = stringifyStyle(rule.style); | |
| 173 for (let pattern of patterns) | |
| 174 { | |
| 175 matchStyleProps(style, rule, pattern, selectors, filters); | |
| 176 } | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 /** | |
| 181 * Match the selector @pattern containing :-abp-has() starting from @node. | |
| 182 * @param {string} pattern - the pattern to match | |
| 183 * @param {object} node - the top level node. | |
| 184 * @param {object} stylesheets - the stylesheets to check from. | |
| 185 * @param {array} elements - elements that match | |
| 186 * @param {array} filters - filters that match | |
| 187 */ | |
| 188 function pseudoClassHasMatch(pattern, node, stylesheets, elements, filters) | |
| 189 { | |
| 190 // select element for the prefix pattern. Or just use node. | |
| 191 let haveElems = pattern.prefix ? node.querySelectorAll(pattern.prefix) : | |
| 192 [node]; | |
| 193 for (let elem of haveElems) | |
| 194 { | |
| 195 let matched = pattern.elementMatcher.match(elem, stylesheets); | |
| 196 if (!matched) | |
| 197 continue; | |
| 198 | |
| 199 if (pattern.suffix) | |
| 200 { | |
| 201 let subElements = selectChildren(elem, pattern.suffix); | |
| 202 if (subElements) | |
| 203 { | |
| 204 for (let subElement of subElements) | |
| 205 { | |
| 206 elements.push(subElement); | |
| 207 filters.push(pattern.text); | |
| 208 } | |
| 209 } | |
| 210 } | |
| 211 else | |
| 212 { | |
| 213 elements.push(elem); | |
| 214 filters.push(pattern.text); | |
| 215 } | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 function stringifyStyle(style) | |
| 220 { | |
| 221 let styles = []; | |
| 222 for (let i = 0; i < style.length; i++) | |
| 223 { | |
| 224 let property = style.item(i); | |
| 225 let value = style.getPropertyValue(property); | |
| 226 let priority = style.getPropertyPriority(property); | |
| 227 styles.push(property + ": " + value + (priority ? " !" + priority : "") + | |
| 228 ";"); | |
| 229 } | |
| 230 styles.sort(); | |
| 231 return styles.join(" "); | |
| 232 } | |
| 233 | |
| 234 /** matcher for the pseudo class :-abp-has() | |
| 235 * For those browser that don't have it yet. | |
| 236 * @param {string} selector - the inner selector. | |
| 237 */ | |
| 238 function PseudoHasMatcher(selector) | |
| 239 { | |
| 240 this.hasSelector = selector; | |
| 241 this.parsed = parsePattern({selector}); | |
| 242 } | |
| 243 | |
| 244 PseudoHasMatcher.prototype = { | |
| 245 match(elem, stylesheets) | |
| 246 { | |
| 247 let selectors = []; | |
| 248 | |
| 249 if (this.parsed) | |
| 250 { | |
| 251 let filters = []; // don't need this, we have a partial filter. | |
| 252 if (this.parsed.type == "has") | |
| 253 { | |
| 254 let child = elem.firstChild; | |
| 255 while (child) | |
| 256 { | |
| 257 if (child.nodeType === Node.ELEMENT_NODE) | |
| 258 { | |
| 259 let matches = []; | |
| 260 pseudoClassHasMatch(this.parsed, child, stylesheets, matches, | |
| 261 filters); | |
| 262 if (matches.length > 0) | |
| 263 return true; | |
| 264 } | |
| 265 child = child.nextSibling; | |
| 266 } | |
| 267 return false; | |
| 268 } | |
| 269 if (this.parsed.type == "props") | |
| 270 for (let stylesheet of stylesheets) | |
| 271 findPropsSelectors(stylesheet, [this.parsed], selectors, filters); | |
| 272 } | |
| 273 else | |
| 274 selectors = [this.hasSelector]; | |
| 275 | |
| 276 let matched = false; | |
| 277 // look up for all elements that match the :-abp-has(). | |
| 278 for (let selector of selectors) | |
| 279 try | |
| 280 { | |
| 281 matched = matchChildren(elem, selector); | |
| 282 if (matched) | |
| 283 break; | |
| 284 } | |
| 285 catch (e) | |
| 286 { | |
| 287 console.error("Exception with querySelector()", selector, e); | |
| 288 } | |
| 289 | |
| 290 return matched; | |
| 291 } | 321 } |
| 292 }; | 322 }; |
| 293 | 323 |
| 294 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc, | 324 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc, |
| 295 hideElementsFunc) | 325 hideElemsFunc) |
| 296 { | 326 { |
| 297 this.window = window; | 327 this.window = window; |
| 298 this.getFiltersFunc = getFiltersFunc; | 328 this.getFiltersFunc = getFiltersFunc; |
| 299 this.addSelectorsFunc = addSelectorsFunc; | 329 this.addSelectorsFunc = addSelectorsFunc; |
| 300 this.hideElementsFunc = hideElementsFunc; | 330 this.hideElemsFunc = hideElemsFunc; |
| 301 } | 331 } |
| 302 | 332 |
| 303 ElemHideEmulation.prototype = { | 333 ElemHideEmulation.prototype = { |
| 304 | |
| 305 isSameOrigin(stylesheet) | 334 isSameOrigin(stylesheet) |
| 306 { | 335 { |
| 307 try | 336 try |
| 308 { | 337 { |
| 309 return new URL(stylesheet.href).origin == this.window.location.origin; | 338 return new URL(stylesheet.href).origin == this.window.location.origin; |
| 310 } | 339 } |
| 311 catch (e) | 340 catch (e) |
| 312 { | 341 { |
| 313 // Invalid URL, assume that it is first-party. | 342 // Invalid URL, assume that it is first-party. |
| 314 return true; | 343 return true; |
| 315 } | 344 } |
| 316 }, | 345 }, |
| 317 | 346 |
| 318 findPseudoClassHasElements(node, stylesheets, elements, filters) | |
| 319 { | |
| 320 for (let pattern of this.pseudoHasPatterns) | |
| 321 pseudoClassHasMatch(pattern, node, stylesheets, elements, filters); | |
| 322 }, | |
| 323 | |
| 324 addSelectors(stylesheets) | 347 addSelectors(stylesheets) |
| 325 { | 348 { |
| 326 let selectors = []; | 349 let selectors = []; |
| 327 let filters = []; | 350 let selectorFilters = []; |
| 351 |
| 352 let elements = []; |
| 353 let elementFilters = []; |
| 354 |
| 355 let cssStyles = []; |
| 356 |
| 328 for (let stylesheet of stylesheets) | 357 for (let stylesheet of stylesheets) |
| 329 { | 358 { |
| 330 // Explicitly ignore third-party stylesheets to ensure consistent behavior | 359 // Explicitly ignore third-party stylesheets to ensure consistent behavior |
| 331 // between Firefox and Chrome. | 360 // between Firefox and Chrome. |
| 332 if (!this.isSameOrigin(stylesheet)) | 361 if (!this.isSameOrigin(stylesheet)) |
| 333 continue; | 362 continue; |
| 334 findPropsSelectors(stylesheet, this.propSelPatterns, selectors, filters); | 363 |
| 335 } | 364 let rules = stylesheet.cssRules; |
| 336 this.addSelectorsFunc(selectors, filters); | 365 if (!rules) |
| 337 }, | 366 continue; |
| 338 | 367 |
| 339 hideElements(stylesheets) | 368 for (let rule of rules) |
| 340 { | 369 { |
| 341 let elements = []; | 370 if (rule.type != rule.STYLE_RULE) |
| 342 let filters = []; | 371 continue; |
| 343 this.findPseudoClassHasElements(document, stylesheets, elements, filters); | 372 |
| 344 this.hideElementsFunc(elements, filters); | 373 cssStyles.push(stringifyStyle(rule)); |
| 374 } |
| 375 } |
| 376 |
| 377 let {document} = this.window; |
| 378 for (let pattern of this.patterns) |
| 379 { |
| 380 for (let selector of evaluate(pattern.selectors, |
| 381 0, "", document, cssStyles)) |
| 382 { |
| 383 if (!pattern.selectors.some(s => s.requiresHiding)) |
| 384 { |
| 385 selectors.push(selector); |
| 386 selectorFilters.push(pattern.text); |
| 387 } |
| 388 else |
| 389 { |
| 390 for (let element of document.querySelectorAll(selector)) |
| 391 { |
| 392 elements.push(element); |
| 393 elementFilters.push(pattern.text); |
| 394 } |
| 395 } |
| 396 } |
| 397 } |
| 398 |
| 399 this.addSelectorsFunc(selectors, selectorFilters); |
| 400 this.hideElemsFunc(elements, elementFilters); |
| 345 }, | 401 }, |
| 346 | 402 |
| 347 onLoad(event) | 403 onLoad(event) |
| 348 { | 404 { |
| 349 let stylesheet = event.target.sheet; | 405 let stylesheet = event.target.sheet; |
| 350 if (stylesheet) | 406 if (stylesheet) |
| 351 this.addSelectors([stylesheet]); | 407 this.addSelectors([stylesheet]); |
| 352 this.hideElements([stylesheet]); | |
| 353 }, | 408 }, |
| 354 | 409 |
| 355 apply() | 410 apply() |
| 356 { | 411 { |
| 357 this.getFiltersFunc(patterns => | 412 this.getFiltersFunc(patterns => |
| 358 { | 413 { |
| 359 this.propSelPatterns = []; | 414 let oldReportError = reportError; |
| 360 this.pseudoHasPatterns = []; | 415 reportError = error => this.window.console.error(error); |
| 361 | 416 |
| 417 this.patterns = []; |
| 362 for (let pattern of patterns) | 418 for (let pattern of patterns) |
| 363 { | 419 { |
| 364 let parsed = parsePattern(pattern); | 420 let selectors = parseSelector(pattern.selector); |
| 365 if (parsed == undefined) | 421 if (selectors != null && selectors.length > 0) |
| 366 continue; | 422 this.patterns.push({selectors, text: pattern.text}); |
| 367 | |
| 368 if (parsed.type == "props") | |
| 369 { | |
| 370 this.propSelPatterns.push(parsed); | |
| 371 } | |
| 372 else if (parsed.type == "has") | |
| 373 { | |
| 374 this.pseudoHasPatterns.push(parsed); | |
| 375 } | |
| 376 } | 423 } |
| 377 | 424 |
| 378 if (this.pseudoHasPatterns.length > 0 || this.propSelPatterns.length > 0) | 425 if (this.patterns.length > 0) |
| 379 { | 426 { |
| 380 let {document} = this.window; | 427 let {document} = this.window; |
| 381 this.addSelectors(document.styleSheets); | 428 this.addSelectors(document.styleSheets); |
| 382 this.hideElements(document.styleSheets); | |
| 383 document.addEventListener("load", this.onLoad.bind(this), true); | 429 document.addEventListener("load", this.onLoad.bind(this), true); |
| 384 } | 430 } |
| 431 reportError = oldReportError; |
| 385 }); | 432 }); |
| 386 } | 433 } |
| 387 }; | 434 }; |
| LEFT | RIGHT |