LEFT | RIGHT |
1 // We are currently limited to ECMAScript 5 in this file, because it is being | 1 /* |
2 // used in the browser tests. See https://issues.adblockplus.org/ticket/4796 | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 var propertySelectorRegExp = /\[\-abp\-properties=(["'])([^"']+)\1\]/; | 4 * |
5 var pseudoClassHasSelectorRegExp = /:has\((.*)\)/; | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 | 6 * it under the terms of the GNU General Public License version 3 as |
7 // polyfill as PhantomJS doesn't have matches(). | 7 * published by the Free Software Foundation. |
8 // Until https://issues.adblockplus.org/ticket/4796 | 8 * |
9 if (!Element.prototype.matches) { | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 Element.prototype.matches = | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 Element.prototype.webkitMatchesSelector | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 } | 12 * GNU General Public License for more details. |
13 | 13 * |
14 // return the index were the simple-selector ends | 14 * You should have received a copy of the GNU General Public License |
15 function findFirstSelector(selector) | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 { | 16 */ |
17 for (var i = 0; i < selector.length; i++) | 17 |
18 { | 18 /* globals filterToRegExp */ |
19 var chr = selector[i]; | 19 |
20 if (chr == ' ' || chr == '>' || chr == '+' || chr == '~') | 20 "use strict"; |
21 return i; | 21 |
22 } | 22 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i; |
23 return -1; | 23 |
24 } | 24 let reportError = () => {}; |
25 | |
26 function extractFirstSelector(selector) | |
27 { | |
28 var sepIndex = findFirstSelector(selector); | |
29 | |
30 if (sepIndex == -1) | |
31 return selector; | |
32 | |
33 return selector.substr(0, sepIndex); | |
34 } | |
35 | 25 |
36 function splitSelector(selector) | 26 function splitSelector(selector) |
37 { | 27 { |
38 if (selector.indexOf(",") == -1) | 28 if (selector.indexOf(",") == -1) |
39 return [selector]; | 29 return [selector]; |
40 | 30 |
41 var selectors = []; | 31 let selectors = []; |
42 var start = 0; | 32 let start = 0; |
43 var level = 0; | 33 let level = 0; |
44 var sep = ""; | 34 let sep = ""; |
45 | 35 |
46 for (var i = 0; i < selector.length; i++) | 36 for (let i = 0; i < selector.length; i++) |
47 { | 37 { |
48 var chr = selector[i]; | 38 let chr = selector[i]; |
49 | 39 |
50 if (chr == "\\") // ignore escaped characters | 40 if (chr == "\\") // ignore escaped characters |
51 i++; | 41 i++; |
52 else if (chr == sep) // don't split within quoted text | 42 else if (chr == sep) // don't split within quoted text |
53 sep = ""; // e.g. [attr=","] | 43 sep = ""; // e.g. [attr=","] |
54 else if (sep == "") | 44 else if (sep == "") |
55 { | 45 { |
56 if (chr == '"' || chr == "'") | 46 if (chr == '"' || chr == "'") |
57 sep = chr; | 47 sep = chr; |
58 else if (chr == "(") // don't split between parentheses | 48 else if (chr == "(") // don't split between parentheses |
59 level++; // e.g. :matches(div,span) | 49 level++; // e.g. :matches(div,span) |
60 else if (chr == ")") | 50 else if (chr == ")") |
61 level = Math.max(0, level - 1); | 51 level = Math.max(0, level - 1); |
62 else if (chr == "," && level == 0) | 52 else if (chr == "," && level == 0) |
63 { | 53 { |
64 selectors.push(selector.substring(start, i)); | 54 selectors.push(selector.substring(start, i)); |
65 start = i + 1; | 55 start = i + 1; |
66 } | 56 } |
67 } | 57 } |
68 } | 58 } |
69 | 59 |
70 selectors.push(selector.substring(start)); | 60 selectors.push(selector.substring(start)); |
71 return selectors; | 61 return selectors; |
72 } | 62 } |
73 | 63 |
74 function selectChildren(e, selector) | 64 /** Return position of node from parent. |
75 { | 65 * @param {Node} node the node to find the position of. |
76 var sel = selector; | 66 * @return {number} One-based index like for :nth-child(), or 0 on error. |
77 // XXX we should have a more elegant way | 67 */ |
78 // also startsWith isn't available in PhantomJS. | 68 function positionInParent(node) |
79 var combinator = sel.substr(0, 1); | 69 { |
80 var subElements; | 70 let {children} = node.parentNode; |
81 var nextEl = e; | 71 for (let i = 0; i < children.length; i++) |
82 sel = sel.substr(1).trim(); | 72 if (children[i] == node) |
83 switch (combinator) | 73 return i + 1; |
84 { | 74 return 0; |
85 case ">": | 75 } |
86 subElements = e.querySelectorAll(sel); | 76 |
87 break; | 77 function makeSelector(node, selector) |
88 | 78 { |
89 case "+": | 79 if (!node.parentElement) |
90 do | 80 { |
91 { | 81 let newSelector = ":root"; |
92 nextEl = nextEl.nextSibling; | 82 if (selector) |
93 } | 83 newSelector += " > " + selector; |
94 while (nextEl && nextEl.nodeType != 1); | 84 return newSelector; |
95 | 85 } |
96 var siblingSel = extractFirstSelector(sel); | 86 let idx = positionInParent(node); |
97 var idx = findFirstSelector(sel); | 87 if (idx > 0) |
98 var childSel = idx != -1 ? sel.substr(idx + 1).trim() : ""; | 88 { |
99 | 89 let newSelector = `${node.tagName}:nth-child(${idx})`; |
100 if (nextEl && nextEl.matches(siblingSel)) | 90 if (selector) |
101 { | 91 newSelector += " > " + selector; |
102 if (childSel != "") | 92 return makeSelector(node.parentElement, newSelector); |
103 subElements = selectChildren(nextEl, childSel); | 93 } |
104 else | 94 |
105 subElements = [ nextEl ]; | 95 return selector; |
106 } | 96 } |
107 break; | 97 |
108 | 98 function parseSelectorContent(content, startIndex) |
109 case "~": | 99 { |
110 do | 100 let parens = 1; |
111 { | 101 let quote = null; |
112 nextEl = nextEl.nextSibling; | 102 let i = startIndex; |
113 if (nextEl && nextEl.nodeType == 1 && nextEl.matches(sel)) | 103 for (; i < content.length; i++) |
114 { | 104 { |
115 subElements = nextEl.querySelectorAll(sel); | 105 let c = content[i]; |
| 106 if (c == "\\") |
| 107 { |
| 108 // Ignore escaped characters |
| 109 i++; |
| 110 } |
| 111 else if (quote) |
| 112 { |
| 113 if (c == quote) |
| 114 quote = null; |
| 115 } |
| 116 else if (c == "'" || c == '"') |
| 117 quote = c; |
| 118 else if (c == "(") |
| 119 parens++; |
| 120 else if (c == ")") |
| 121 { |
| 122 parens--; |
| 123 if (parens == 0) |
116 break; | 124 break; |
117 } | 125 } |
118 } | 126 } |
119 while (nextEl); | 127 |
120 | 128 if (parens > 0) |
121 break; | 129 return null; |
122 } | 130 return {text: content.substring(startIndex, i), end: i}; |
123 return subElements; | 131 } |
124 } | 132 |
125 | 133 /** Parse the selector |
126 function parsePattern(pattern) | 134 * @param {string} selector the selector to parse |
127 { | 135 * @return {Object} selectors is an array of objects, |
128 // we should catch the :has() pseudo class first. | 136 * or null in case of errors. hide is true if we'll hide |
129 var match = pseudoClassHasSelectorRegExp.exec(pattern.selector); | 137 * elements instead of styles.. |
130 if (match) | 138 */ |
131 { | 139 function parseSelector(selector) |
132 return { | 140 { |
133 type: "has", | 141 if (selector.length == 0) |
134 text: pattern.text, | 142 return []; |
135 elementMatcher: new PseudoHasMatcher(match[1]), | 143 |
136 prefix: pattern.selector.substr(0, match.index).trim(), | 144 let match = abpSelectorRegexp.exec(selector); |
137 suffix: pattern.selector.substr(match.index + match[0].length).trim() | 145 if (!match) |
138 }; | 146 return [new PlainSelector(selector)]; |
139 } | 147 |
140 | 148 let selectors = []; |
141 match = propertySelectorRegExp.exec(pattern.selector); | 149 if (match.index > 0) |
142 if (match) | 150 selectors.push(new PlainSelector(selector.substr(0, match.index))); |
143 { | 151 |
144 var regexpString; | 152 let startIndex = match.index + match[0].length; |
145 var propertyExpression = match[2]; | 153 let content = parseSelectorContent(selector, startIndex); |
146 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | 154 if (!content) |
147 propertyExpression[propertyExpression.length - 1] == "/") | 155 { |
148 regexpString = propertyExpression.slice(1, -1) | 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) |
149 .replace("\\x7B ", "{").replace("\\x7D ", "}"); | 300 .replace("\\x7B ", "{").replace("\\x7D ", "}"); |
150 else | 301 } |
151 regexpString = filterToRegExp(propertyExpression); | 302 else |
152 return { | 303 regexpString = filterToRegExp(propertyExpression); |
153 type: "props", | 304 |
154 text: pattern.text, | 305 this._regexp = new RegExp(regexpString, "i"); |
155 regexp: new RegExp(regexpString, "i"), | 306 } |
156 prefix: pattern.selector.substr(0, match.index), | 307 |
157 suffix: pattern.selector.substr(match.index + match[0].length) | 308 PropsSelector.prototype = { |
158 }; | 309 *findPropsSelectors(styles, prefix, regexp) |
159 } | 310 { |
160 } | 311 for (let style of styles) |
161 | 312 if (regexp.test(style.style)) |
162 function matchStyleProps(style, rule, pattern, selectors, filters) | 313 for (let subSelector of style.subSelectors) |
163 { | 314 yield prefix + subSelector; |
164 if (pattern.regexp.test(style)) | 315 }, |
165 { | 316 |
166 var subSelectors = splitSelector(rule.selectorText); | 317 *getSelectors(prefix, subtree, styles) |
167 for (var i = 0; i < subSelectors.length; i++) | 318 { |
168 { | 319 for (let selector of this.findPropsSelectors(styles, prefix, this._regexp)) |
169 var subSelector = subSelectors[i]; | 320 yield [selector, subtree]; |
170 selectors.push(pattern.prefix + subSelector + pattern.suffix); | |
171 filters.push(pattern.text); | |
172 } | |
173 } | |
174 } | |
175 | |
176 function findPropsSelectors(stylesheet, patterns, selectors, filters) | |
177 { | |
178 var rules = stylesheet.cssRules; | |
179 if (!rules) | |
180 return; | |
181 | |
182 for (var i = 0; i < rules.length; i++) | |
183 { | |
184 var rule = rules[i]; | |
185 if (rule.type != rule.STYLE_RULE) | |
186 continue; | |
187 | |
188 var style = stringifyStyle(rule.style); | |
189 for (var j = 0; j < patterns.length; j++) | |
190 { | |
191 matchStyleProps(style, rule, patterns[j], selectors, filters); | |
192 } | |
193 } | |
194 } | |
195 | |
196 function pseudoClassHasMatch(pattern, node, stylesheets, elements, filters) | |
197 { | |
198 var haveEl = pattern.prefix ? node.querySelectorAll(pattern.prefix) : [ node ]
; | |
199 for (var j = 0; j < haveEl.length; j++) | |
200 { | |
201 var matched = pattern.elementMatcher.match(haveEl[j], stylesheets, !pattern.
suffix); | |
202 if (matched.length == 0) | |
203 continue; | |
204 | |
205 if (pattern.suffix) | |
206 { | |
207 matched.forEach(function(e) | |
208 { | |
209 var subElements = selectChildren(e, pattern.suffix); | |
210 if (subElements) | |
211 { | |
212 for (var k = 0; k < subElements.length; k++) | |
213 { | |
214 elements.push(subElements[k]); | |
215 filters.push(pattern.text); | |
216 } | |
217 } | |
218 }); | |
219 } | |
220 else | |
221 { | |
222 elements.push(haveEl[j]); | |
223 filters.push(pattern.text); | |
224 } | |
225 } | |
226 } | |
227 | |
228 function stringifyStyle(style) | |
229 { | |
230 var styles = []; | |
231 for (var i = 0; i < style.length; i++) | |
232 { | |
233 var property = style.item(i); | |
234 var value = style.getPropertyValue(property); | |
235 var priority = style.getPropertyPriority(property); | |
236 styles.push(property + ": " + value + (priority ? " !" + priority : "") + ";
"); | |
237 } | |
238 styles.sort(); | |
239 return styles.join(" "); | |
240 } | |
241 | |
242 // matcher for the pseudo CSS4 class :has | |
243 // For those browser that don't have it yet. | |
244 function PseudoHasMatcher(selector) | |
245 { | |
246 this.hasSelector = selector; | |
247 this.parsed = parsePattern({ selector: this.hasSelector }); | |
248 } | |
249 | |
250 PseudoHasMatcher.prototype = { | |
251 match: function(elem, stylesheets, firstOnly) | |
252 { | |
253 var matches = []; | |
254 var selectors = []; | |
255 | |
256 if (this.parsed) | |
257 { | |
258 var filters = []; // don't need this | |
259 if (this.parsed.type == "has") | |
260 { | |
261 pseudoClassHasMatch(this.parsed, elem, stylesheets, matches, filters) | |
262 return matches; | |
263 } | |
264 if (this.parsed.type == "props") | |
265 { | |
266 for (var i = 0; i < stylesheets.length; i++) | |
267 findPropsSelectors(stylesheets[i], [this.parsed], selectors, filters); | |
268 } | |
269 } | |
270 else | |
271 { | |
272 selectors = [this.hasSelector]; | |
273 } | |
274 | |
275 // look up for all elements that match the :has(). | |
276 for (var k = 0; k < selectors.length; k++) | |
277 { | |
278 try | |
279 { | |
280 var hasElem = elem.querySelector(selectors[k]); | |
281 if (hasElem) | |
282 { | |
283 matches.push(hasElem); | |
284 if (firstOnly) | |
285 break; | |
286 } | |
287 } | |
288 catch(e) | |
289 { | |
290 console.log("Exception with querySelector()", selectors[k]); | |
291 } | |
292 } | |
293 return matches; | |
294 } | 321 } |
295 }; | 322 }; |
296 | 323 |
297 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc, hideElement
sFunc) | 324 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc, |
| 325 hideElemsFunc) |
298 { | 326 { |
299 this.window = window; | 327 this.window = window; |
300 this.getFiltersFunc = getFiltersFunc; | 328 this.getFiltersFunc = getFiltersFunc; |
301 this.addSelectorsFunc = addSelectorsFunc; | 329 this.addSelectorsFunc = addSelectorsFunc; |
302 this.hideElementsFunc = hideElementsFunc; | 330 this.hideElemsFunc = hideElemsFunc; |
303 } | 331 } |
304 | 332 |
305 ElemHideEmulation.prototype = { | 333 ElemHideEmulation.prototype = { |
306 | 334 isSameOrigin(stylesheet) |
307 isSameOrigin: function(stylesheet) | |
308 { | 335 { |
309 try | 336 try |
310 { | 337 { |
311 return new URL(stylesheet.href).origin == this.window.location.origin; | 338 return new URL(stylesheet.href).origin == this.window.location.origin; |
312 } | 339 } |
313 catch (e) | 340 catch (e) |
314 { | 341 { |
315 // Invalid URL, assume that it is first-party. | 342 // Invalid URL, assume that it is first-party. |
316 return true; | 343 return true; |
317 } | 344 } |
318 }, | 345 }, |
319 | 346 |
320 findPseudoClassHasElements: function(node, stylesheets, elements, filters) | 347 addSelectors(stylesheets) |
321 { | 348 { |
322 for (var i = 0; i < this.pseudoHasPatterns.length; i++) | 349 let selectors = []; |
323 { | 350 let selectorFilters = []; |
324 pseudoClassHasMatch(this.pseudoHasPatterns[i], node, stylesheets, elements
, filters); | 351 |
325 } | 352 let elements = []; |
326 }, | 353 let elementFilters = []; |
327 | 354 |
328 addSelectors: function(stylesheets) | 355 let cssStyles = []; |
329 { | 356 |
330 var selectors = []; | 357 for (let stylesheet of stylesheets) |
331 var filters = []; | |
332 for (var i = 0; i < stylesheets.length; i++) | |
333 { | 358 { |
334 // Explicitly ignore third-party stylesheets to ensure consistent behavior | 359 // Explicitly ignore third-party stylesheets to ensure consistent behavior |
335 // between Firefox and Chrome. | 360 // between Firefox and Chrome. |
336 if (!this.isSameOrigin(stylesheets[i])) | 361 if (!this.isSameOrigin(stylesheet)) |
337 continue; | 362 continue; |
338 findPropsSelectors(stylesheets[i], this.propSelPatterns, selectors, filter
s); | 363 |
339 } | 364 let rules = stylesheet.cssRules; |
340 this.addSelectorsFunc(selectors, filters); | 365 if (!rules) |
341 }, | 366 continue; |
342 | 367 |
343 hideElements: function(stylesheets) | 368 for (let rule of rules) |
344 { | 369 { |
345 var elements = []; | 370 if (rule.type != rule.STYLE_RULE) |
346 var filters = []; | 371 continue; |
347 this.findPseudoClassHasElements(document, stylesheets, elements, filters); | 372 |
348 this.hideElementsFunc(elements, filters); | 373 cssStyles.push(stringifyStyle(rule)); |
349 }, | 374 } |
350 | 375 } |
351 onLoad: function(event) | 376 |
352 { | 377 let {document} = this.window; |
353 var stylesheet = event.target.sheet; | 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); |
| 401 }, |
| 402 |
| 403 onLoad(event) |
| 404 { |
| 405 let stylesheet = event.target.sheet; |
354 if (stylesheet) | 406 if (stylesheet) |
355 this.addSelectors([stylesheet]); | 407 this.addSelectors([stylesheet]); |
356 this.hideElements([stylesheet]); | 408 }, |
357 }, | 409 |
358 | 410 apply() |
359 apply: function() | 411 { |
360 { | 412 this.getFiltersFunc(patterns => |
361 this.getFiltersFunc(function(patterns) | 413 { |
362 { | 414 let oldReportError = reportError; |
363 this.propSelPatterns = []; | 415 reportError = error => this.window.console.error(error); |
364 this.pseudoHasPatterns = []; | 416 |
365 for (var i = 0; i < patterns.length; i++) | 417 this.patterns = []; |
| 418 for (let pattern of patterns) |
366 { | 419 { |
367 var pattern = patterns[i]; | 420 let selectors = parseSelector(pattern.selector); |
368 var parsed = parsePattern(pattern); | 421 if (selectors != null && selectors.length > 0) |
369 if (parsed == undefined) | 422 this.patterns.push({selectors, text: pattern.text}); |
370 continue; | |
371 if (parsed.type == "props") | |
372 { | |
373 this.propSelPatterns.push(parsed); | |
374 } | |
375 else if (parsed.type == "has") | |
376 { | |
377 this.pseudoHasPatterns.push(parsed); | |
378 } | |
379 } | 423 } |
380 | 424 |
381 if (this.pseudoHasPatterns.length > 0 || this.propSelPatterns.length > 0) | 425 if (this.patterns.length > 0) |
382 { | 426 { |
383 var document = this.window.document; | 427 let {document} = this.window; |
384 this.addSelectors(document.styleSheets); | 428 this.addSelectors(document.styleSheets); |
385 this.hideElements(document.styleSheets); | |
386 document.addEventListener("load", this.onLoad.bind(this), true); | 429 document.addEventListener("load", this.onLoad.bind(this), true); |
387 } | 430 } |
388 }.bind(this)); | 431 reportError = oldReportError; |
| 432 }); |
389 } | 433 } |
390 }; | 434 }; |
LEFT | RIGHT |