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