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/>, |
Wladimir Palant
2017/04/25 10:57:52
This is really a PITA. Given that https://issues.a
hub
2017/04/25 19:42:47
If issue 4796 lands before this, then I'll fix it.
Wladimir Palant
2017/04/26 00:03:09
As I suggested on IRC, it would probably be easies
hub
2017/04/27 17:43:04
Newer patch is now ES6 based on top of your patch.
| |
3 | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 var propertySelectorRegExp = /\[-abp-properties=(["'])([^"']+)\1\]/; | 4 * |
5 var abpSelectorRegExp = /\[-abp-selector=(["'])(.+)\1\]/; | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 var pseudoClassHasSelectorRegExp = /:has\((.*)\)/; | 6 * it under the terms of the GNU General Public License version 3 as |
7 var pseudoClassPropsSelectorRegExp = /:-abp-properties\((["'])([^"']+)\1\)/; | 7 * published by the Free Software Foundation. |
Wladimir Palant
2017/04/25 11:30:46
Having :has() but :-abp-properties() is inconsiste
hub
2017/04/25 19:42:47
OK.
| |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
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/>. | |
16 */ | |
17 | |
18 /* globals filterToRegExp */ | |
19 | |
20 "use strict"; | |
21 | |
22 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i; | |
23 | |
24 let reportError = () => {}; | |
8 | 25 |
9 function splitSelector(selector) | 26 function splitSelector(selector) |
10 { | 27 { |
11 if (selector.indexOf(",") == -1) | 28 if (selector.indexOf(",") == -1) |
12 return [selector]; | 29 return [selector]; |
13 | 30 |
14 var selectors = []; | 31 let selectors = []; |
15 var start = 0; | 32 let start = 0; |
16 var level = 0; | 33 let level = 0; |
17 var sep = ""; | 34 let sep = ""; |
18 | 35 |
19 for (var i = 0; i < selector.length; i++) | 36 for (let i = 0; i < selector.length; i++) |
20 { | 37 { |
21 var chr = selector[i]; | 38 let chr = selector[i]; |
22 | 39 |
23 if (chr == "\\") // ignore escaped characters | 40 if (chr == "\\") // ignore escaped characters |
24 i++; | 41 i++; |
25 else if (chr == sep) // don't split within quoted text | 42 else if (chr == sep) // don't split within quoted text |
26 sep = ""; // e.g. [attr=","] | 43 sep = ""; // e.g. [attr=","] |
27 else if (sep == "") | 44 else if (sep == "") |
28 { | 45 { |
29 if (chr == '"' || chr == "'") | 46 if (chr == '"' || chr == "'") |
30 sep = chr; | 47 sep = chr; |
31 else if (chr == "(") // don't split between parentheses | 48 else if (chr == "(") // don't split between parentheses |
32 level++; // e.g. :matches(div,span) | 49 level++; // e.g. :matches(div,span) |
33 else if (chr == ")") | 50 else if (chr == ")") |
34 level = Math.max(0, level - 1); | 51 level = Math.max(0, level - 1); |
35 else if (chr == "," && level == 0) | 52 else if (chr == "," && level == 0) |
36 { | 53 { |
37 selectors.push(selector.substring(start, i)); | 54 selectors.push(selector.substring(start, i)); |
38 start = i + 1; | 55 start = i + 1; |
39 } | 56 } |
40 } | 57 } |
41 } | 58 } |
42 | 59 |
43 selectors.push(selector.substring(start)); | 60 selectors.push(selector.substring(start)); |
44 return selectors; | 61 return selectors; |
45 } | 62 } |
46 | 63 |
47 const ABP_ATTR = "abp-0ac791f0-a03b-4f2c-a935-a285bc2e668e"; | 64 /** Return position of node from parent. |
48 const ABP_ATTR_SEL = "[" + ABP_ATTR + "] "; | 65 * @param {Node} node the node to find the position of. |
49 | 66 * @return {number} One-based index like for :nth-child(), or 0 on error. |
50 function matchChildren(e, selector) | |
51 { | |
52 var subElement; | |
53 var newSelector = ABP_ATTR_SEL + selector; | |
54 var parentNode = e.parentNode || document; | |
55 | |
56 e.setAttribute(ABP_ATTR, ""); | |
57 subElement = parentNode.querySelector(newSelector); | |
58 e.removeAttribute(ABP_ATTR); | |
Wladimir Palant
2017/04/25 10:57:52
This hack is problematic. Even if we make the attr
hub
2017/04/25 19:42:47
I was thinking we could generate an actually rando
hub
2017/04/27 17:43:04
On 2017/04/25 19:42:47, hub wrote:
Newer patch re
| |
59 | |
60 return subElement != null; | |
61 } | |
62 | |
63 function selectChildren(e, selector) | |
64 { | |
65 var subElements; | |
66 var newSelector = ABP_ATTR_SEL + selector; | |
67 var parentNode = e.parentNode || document; | |
68 | |
69 e.setAttribute(ABP_ATTR, ""); | |
70 subElements = parentNode.querySelectorAll(newSelector); | |
71 e.removeAttribute(ABP_ATTR); | |
72 | |
73 return subElements; | |
74 } | |
75 | |
76 /** Unwrap the pattern out of a [-abp-selector=''] if necessary | |
77 * @param {object} pattern - The pattern object to unwrap | |
78 * @return {object} the parsed pattern object or undefined if nothing parse. | |
79 */ | 67 */ |
80 function unwrapPattern(pattern) | 68 function positionInParent(node) |
Wladimir Palant
2017/04/25 10:57:53
This unwrapping belongs into lib/filterClasses.js
hub
2017/04/25 19:42:47
ok, moving it.
| |
81 { | 69 { |
82 var match = abpSelectorRegExp.exec(pattern.selector); | 70 let {children} = node.parentNode; |
83 if (match) | 71 for (let i = 0; i < children.length; i++) |
84 { | 72 if (children[i] == node) |
85 var prefix = pattern.selector.substr(0, match.index).trim(); | 73 return i + 1; |
86 var suffix = pattern.selector.substr(match.index + match[0].length).trim(); | 74 return 0; |
87 var selector = prefix + match[2] + suffix; | 75 } |
88 return parsePattern({selector: selector}, true); | 76 |
89 } | 77 function makeSelector(node, selector) |
90 | 78 { |
91 return parsePattern(pattern, false); | 79 if (!node.parentElement) |
92 } | 80 { |
93 | 81 let newSelector = ":root"; |
94 function parsePattern(pattern, unwrapped) | 82 if (selector) |
95 { | 83 newSelector += " > " + selector; |
96 var selector = pattern.selector; | 84 return newSelector; |
97 var match; | 85 } |
98 | 86 let idx = positionInParent(node); |
99 if (unwrapped) | 87 if (idx > 0) |
100 { | 88 { |
101 // Seems that the pattern should be unwrapped. | 89 let newSelector = `${node.tagName}:nth-child(${idx})`; |
102 // This is a fundamental error. | 90 if (selector) |
103 if (selector.indexOf("[-abp-selector=") != -1) | 91 newSelector += " > " + selector; |
104 return; | 92 return makeSelector(node.parentElement, newSelector); |
105 | 93 } |
106 match = pseudoClassHasSelectorRegExp.exec(selector); | 94 |
Wladimir Palant
2017/04/25 10:57:52
The syntax is getting too complex, parsing CSS wit
| |
107 if (match) | 95 return selector; |
108 return { | 96 } |
109 type: "has", | 97 |
110 text: pattern.text, | 98 function parseSelectorContent(content, startIndex) |
111 elementMatcher: new PseudoHasMatcher(match[1]), | 99 { |
112 prefix: selector.substr(0, match.index).trim(), | 100 let parens = 1; |
113 suffix: selector.substr(match.index + match[0].length).trim() | 101 let quote = null; |
114 }; | 102 let i = startIndex; |
115 match = pseudoClassPropsSelectorRegExp.exec(selector); | 103 for (; i < content.length; i++) |
116 } | 104 { |
117 | 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) | |
124 break; | |
125 } | |
126 } | |
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); | |
118 if (!match) | 145 if (!match) |
119 match = propertySelectorRegExp.exec(selector); | 146 return [new PlainSelector(selector)]; |
Wladimir Palant
2017/04/25 10:57:52
I don't think that we should have this special han
hub
2017/04/25 19:42:47
good idea. Like unwrap above.
| |
120 if (match) | 147 |
121 { | 148 let selectors = []; |
122 var regexpString; | 149 if (match.index > 0) |
123 var propertyExpression = match[2]; | 150 selectors.push(new PlainSelector(selector.substr(0, match.index))); |
124 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | 151 |
125 propertyExpression[propertyExpression.length - 1] == "/") | 152 let startIndex = match.index + match[0].length; |
126 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) | |
127 .replace("\\x7B ", "{").replace("\\x7D ", "}"); | 300 .replace("\\x7B ", "{").replace("\\x7D ", "}"); |
128 else | 301 } |
129 regexpString = filterToRegExp(propertyExpression); | 302 else |
130 return { | 303 regexpString = filterToRegExp(propertyExpression); |
131 type: "props", | 304 |
132 text: pattern.text, | 305 this._regexp = new RegExp(regexpString, "i"); |
133 regexp: new RegExp(regexpString, "i"), | 306 } |
134 prefix: selector.substr(0, match.index), | 307 |
135 suffix: selector.substr(match.index + match[0].length) | 308 PropsSelector.prototype = { |
136 }; | 309 *findPropsSelectors(styles, prefix, regexp) |
137 } | 310 { |
138 } | 311 for (let style of styles) |
139 | 312 if (regexp.test(style.style)) |
140 function matchStyleProps(style, rule, pattern, selectors, filters) | 313 for (let subSelector of style.subSelectors) |
141 { | 314 yield prefix + subSelector; |
142 if (pattern.regexp.test(style)) | 315 }, |
143 { | 316 |
144 var subSelectors = splitSelector(rule.selectorText); | 317 *getSelectors(prefix, subtree, styles) |
145 for (var i = 0; i < subSelectors.length; i++) | 318 { |
146 { | 319 for (let selector of this.findPropsSelectors(styles, prefix, this._regexp)) |
147 var subSelector = subSelectors[i]; | 320 yield [selector, subtree]; |
148 selectors.push(pattern.prefix + subSelector + pattern.suffix); | |
149 filters.push(pattern.text); | |
150 } | |
151 } | |
152 } | |
153 | |
154 function findPropsSelectors(stylesheet, patterns, selectors, filters) | |
155 { | |
156 var rules = stylesheet.cssRules; | |
157 if (!rules) | |
158 return; | |
159 | |
160 for (var i = 0; i < rules.length; i++) | |
161 { | |
162 var rule = rules[i]; | |
163 if (rule.type != rule.STYLE_RULE) | |
164 continue; | |
165 | |
166 var style = stringifyStyle(rule.style); | |
167 for (var j = 0; j < patterns.length; j++) | |
168 { | |
169 matchStyleProps(style, rule, patterns[j], selectors, filters); | |
170 } | |
171 } | |
172 } | |
173 | |
174 /** | |
175 * Match the selector @pattern containing :has() starting from @node. | |
176 * @param {string} pattern - the pattern to match | |
177 * @param {object} node - the top level node. | |
178 */ | |
179 function pseudoClassHasMatch(pattern, node, stylesheets, elements, filters) | |
180 { | |
181 // select element for the prefix pattern. Or just use node. | |
182 var haveEl = pattern.prefix ? node.querySelectorAll(pattern.prefix) : [ node ] ; | |
Wladimir Palant
2017/04/25 10:57:53
This will work correctly for selectors like "div >
hub
2017/04/25 19:42:47
I naively assumed that since these are filters, we
Wladimir Palant
2017/04/26 00:03:09
Feel free to suggest a meaningful subset of the sy
| |
183 for (var j = 0; j < haveEl.length; j++) | |
184 { | |
185 var matched = pattern.elementMatcher.match(haveEl[j], stylesheets); | |
186 if (!matched) | |
187 continue; | |
188 | |
189 if (pattern.suffix) | |
190 { | |
191 var subElements = selectChildren(haveEl[j], pattern.suffix); | |
192 if (subElements) | |
193 { | |
194 for (var k = 0; k < subElements.length; k++) | |
195 { | |
196 elements.push(subElements[k]); | |
197 filters.push(pattern.text); | |
198 } | |
199 } | |
200 } | |
201 else | |
202 { | |
203 elements.push(haveEl[j]); | |
204 filters.push(pattern.text); | |
205 } | |
206 } | |
207 } | |
208 | |
209 function stringifyStyle(style) | |
210 { | |
211 var styles = []; | |
212 for (var i = 0; i < style.length; i++) | |
213 { | |
214 var property = style.item(i); | |
215 var value = style.getPropertyValue(property); | |
216 var priority = style.getPropertyPriority(property); | |
217 styles.push(property + ": " + value + (priority ? " !" + priority : "") + "; "); | |
218 } | |
219 styles.sort(); | |
220 return styles.join(" "); | |
221 } | |
222 | |
223 /** matcher for the pseudo CSS4 class :has() | |
224 * For those browser that don't have it yet. | |
225 */ | |
226 function PseudoHasMatcher(selector) | |
227 { | |
228 this.hasSelector = selector; | |
229 this.parsed = parsePattern({selector: this.hasSelector}, true); | |
230 } | |
231 | |
232 PseudoHasMatcher.prototype = { | |
233 match: function(elem, stylesheets) | |
234 { | |
235 var selectors = []; | |
236 | |
237 if (this.parsed) | |
238 { | |
239 var filters = []; // don't need this, we have a partial filter. | |
240 if (this.parsed.type == "has") | |
241 { | |
242 var child = elem.firstChild; | |
243 while (child) | |
244 { | |
245 if (child.nodeType === Node.ELEMENT_NODE) | |
246 { | |
247 var matches = []; | |
248 pseudoClassHasMatch(this.parsed, child, stylesheets, matches, filter s); | |
249 if (matches.length > 0) | |
250 return true; | |
251 } | |
252 child = child.nextSibling; | |
253 } | |
254 return false; | |
255 } | |
256 if (this.parsed.type == "props") | |
257 { | |
258 for (var i = 0; i < stylesheets.length; i++) | |
259 findPropsSelectors(stylesheets[i], [this.parsed], selectors, filters); | |
Wladimir Palant
2017/04/25 10:57:52
We will be going through the stylesheets multiple
| |
260 } | |
261 } | |
262 else | |
263 { | |
264 selectors = [this.hasSelector]; | |
265 } | |
266 | |
267 var matched = false; | |
268 // look up for all elements that match the :has(). | |
269 for (var k = 0; k < selectors.length; k++) | |
270 { | |
271 try | |
272 { | |
273 matched = matchChildren(elem, selectors[k]); | |
274 if (matched) | |
275 break; | |
276 } | |
277 catch (e) | |
278 { | |
279 console.error("Exception with querySelector()", selectors[k], e); | |
280 } | |
281 } | |
282 return matched; | |
283 } | 321 } |
284 }; | 322 }; |
285 | 323 |
286 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc, hideElement sFunc) | 324 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc, |
325 hideElemsFunc) | |
287 { | 326 { |
288 this.window = window; | 327 this.window = window; |
289 this.getFiltersFunc = getFiltersFunc; | 328 this.getFiltersFunc = getFiltersFunc; |
290 this.addSelectorsFunc = addSelectorsFunc; | 329 this.addSelectorsFunc = addSelectorsFunc; |
291 this.hideElementsFunc = hideElementsFunc; | 330 this.hideElemsFunc = hideElemsFunc; |
Wladimir Palant
2017/04/25 10:57:53
I guess that hideElementsFunc should do some kind
| |
292 } | 331 } |
293 | 332 |
294 ElemHideEmulation.prototype = { | 333 ElemHideEmulation.prototype = { |
295 | 334 isSameOrigin(stylesheet) |
296 isSameOrigin: function(stylesheet) | |
297 { | 335 { |
298 try | 336 try |
299 { | 337 { |
300 return new URL(stylesheet.href).origin == this.window.location.origin; | 338 return new URL(stylesheet.href).origin == this.window.location.origin; |
301 } | 339 } |
302 catch (e) | 340 catch (e) |
303 { | 341 { |
304 // Invalid URL, assume that it is first-party. | 342 // Invalid URL, assume that it is first-party. |
305 return true; | 343 return true; |
306 } | 344 } |
307 }, | 345 }, |
308 | 346 |
309 findPseudoClassHasElements: function(node, stylesheets, elements, filters) | 347 addSelectors(stylesheets) |
310 { | 348 { |
311 for (var i = 0; i < this.pseudoHasPatterns.length; i++) | 349 let selectors = []; |
312 { | 350 let selectorFilters = []; |
313 pseudoClassHasMatch(this.pseudoHasPatterns[i], node, stylesheets, elements , filters); | 351 |
314 } | 352 let elements = []; |
315 }, | 353 let elementFilters = []; |
316 | 354 |
317 addSelectors: function(stylesheets) | 355 let cssStyles = []; |
318 { | 356 |
319 var selectors = []; | 357 for (let stylesheet of stylesheets) |
320 var filters = []; | |
321 for (var i = 0; i < stylesheets.length; i++) | |
322 { | 358 { |
323 // Explicitly ignore third-party stylesheets to ensure consistent behavior | 359 // Explicitly ignore third-party stylesheets to ensure consistent behavior |
324 // between Firefox and Chrome. | 360 // between Firefox and Chrome. |
325 if (!this.isSameOrigin(stylesheets[i])) | 361 if (!this.isSameOrigin(stylesheet)) |
326 continue; | 362 continue; |
327 findPropsSelectors(stylesheets[i], this.propSelPatterns, selectors, filter s); | 363 |
328 } | 364 let rules = stylesheet.cssRules; |
329 this.addSelectorsFunc(selectors, filters); | 365 if (!rules) |
330 }, | 366 continue; |
331 | 367 |
332 hideElements: function(stylesheets) | 368 for (let rule of rules) |
333 { | 369 { |
334 var elements = []; | 370 if (rule.type != rule.STYLE_RULE) |
335 var filters = []; | 371 continue; |
336 this.findPseudoClassHasElements(document, stylesheets, elements, filters); | 372 |
337 this.hideElementsFunc(elements, filters); | 373 cssStyles.push(stringifyStyle(rule)); |
338 }, | 374 } |
339 | 375 } |
340 onLoad: function(event) | 376 |
341 { | 377 let {document} = this.window; |
342 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; | |
343 if (stylesheet) | 406 if (stylesheet) |
344 this.addSelectors([stylesheet]); | 407 this.addSelectors([stylesheet]); |
345 this.hideElements([stylesheet]); | 408 }, |
346 }, | 409 |
347 | 410 apply() |
348 apply: function() | 411 { |
349 { | 412 this.getFiltersFunc(patterns => |
350 this.getFiltersFunc(function(patterns) | 413 { |
351 { | 414 let oldReportError = reportError; |
352 this.propSelPatterns = []; | 415 reportError = error => this.window.console.error(error); |
353 this.pseudoHasPatterns = []; | 416 |
354 for (var i = 0; i < patterns.length; i++) | 417 this.patterns = []; |
418 for (let pattern of patterns) | |
355 { | 419 { |
356 var pattern = patterns[i]; | 420 let selectors = parseSelector(pattern.selector); |
357 var parsed = unwrapPattern(pattern); | 421 if (selectors != null && selectors.length > 0) |
358 if (parsed == undefined) | 422 this.patterns.push({selectors, text: pattern.text}); |
359 continue; | |
360 if (parsed.type == "props") | |
361 { | |
362 this.propSelPatterns.push(parsed); | |
363 } | |
364 else if (parsed.type == "has") | |
365 { | |
366 this.pseudoHasPatterns.push(parsed); | |
367 } | |
368 } | 423 } |
369 | 424 |
370 if (this.pseudoHasPatterns.length > 0 || this.propSelPatterns.length > 0) | 425 if (this.patterns.length > 0) |
371 { | 426 { |
372 var document = this.window.document; | 427 let {document} = this.window; |
373 this.addSelectors(document.styleSheets); | 428 this.addSelectors(document.styleSheets); |
374 this.hideElements(document.styleSheets); | |
375 document.addEventListener("load", this.onLoad.bind(this), true); | 429 document.addEventListener("load", this.onLoad.bind(this), true); |
376 } | 430 } |
377 }.bind(this)); | 431 reportError = oldReportError; |
432 }); | |
378 } | 433 } |
379 }; | 434 }; |
LEFT | RIGHT |