Left: | ||
Right: |
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 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i; | |
23 | |
24 let reportError = () => {}; | |
25 | |
22 function splitSelector(selector) | 26 function splitSelector(selector) |
23 { | 27 { |
24 if (selector.indexOf(",") == -1) | 28 if (selector.indexOf(",") == -1) |
25 return [selector]; | 29 return [selector]; |
26 | 30 |
27 let selectors = []; | 31 let selectors = []; |
28 let start = 0; | 32 let start = 0; |
29 let level = 0; | 33 let level = 0; |
30 let sep = ""; | 34 let sep = ""; |
31 | 35 |
(...skipping 18 matching lines...) Expand all Loading... | |
50 selectors.push(selector.substring(start, i)); | 54 selectors.push(selector.substring(start, i)); |
51 start = i + 1; | 55 start = i + 1; |
52 } | 56 } |
53 } | 57 } |
54 } | 58 } |
55 | 59 |
56 selectors.push(selector.substring(start)); | 60 selectors.push(selector.substring(start)); |
57 return selectors; | 61 return selectors; |
58 } | 62 } |
59 | 63 |
60 // Return position of node from parent. | 64 /** Return position of node from parent. |
61 // 1 base index like for :nth-child() | 65 * @param {Node} node the node to find the position of. |
Wladimir Palant
2017/05/16 13:50:38
Nit: Here and below, if you add documentation you
hub
2017/05/25 13:02:29
Done.
| |
66 * @return {number} One-based index like for :nth-child(), or 0 on error. | |
67 */ | |
62 function positionInParent(node) | 68 function positionInParent(node) |
63 { | 69 { |
64 let parentNode = node ? node.parentNode : null; | 70 let {children} = node.parentNode; |
Wladimir Palant
2017/05/16 13:50:34
I guess you should be consistent here:
if (!node)
hub
2017/05/16 21:35:54
Done.
| |
65 if (parentNode == null) | 71 for (let i = 0; i < children.length; i++) |
66 return 0; | |
67 | |
68 let {children} = parentNode; | |
69 if (!children) | |
70 return 0; | |
71 let i = 0; | |
72 for (i = 0; i < children.length; i++) | |
73 if (children[i] == node) | 72 if (children[i] == node) |
74 break; | 73 return i + 1; |
75 return i + 1; | 74 return 0; |
Wladimir Palant
2017/05/16 13:50:37
I suggest that you simplify this, no need to decla
hub
2017/05/16 21:35:56
Re-reading the code, it almost make it correct. If
| |
76 } | |
77 | |
78 function idValid(id) | |
Wladimir Palant
2017/05/16 13:50:34
Nit: how about isValidID() as function name?
hub
2017/05/16 21:35:53
Acknowledged.
| |
79 { | |
80 if (!id) | |
81 return false; | |
82 if (id === "") | |
Wladimir Palant
2017/05/16 13:50:33
id cannot be an empty string at this point because
hub
2017/05/16 21:35:51
https://www.w3.org/TR/CSS2/syndata.html#value-def-
| |
83 return false; | |
84 if (id.match(/^(-?[0-9]|--)/)) | |
85 return false; | |
86 return true; | |
87 } | 75 } |
88 | 76 |
89 function makeSelector(node, selector) | 77 function makeSelector(node, selector) |
90 { | 78 { |
91 if (node && idValid(node.id)) | 79 if (!node.parentElement) |
92 { | 80 { |
93 let newSelector = "#" + node.id; | 81 let newSelector = ":root"; |
Wladimir Palant
2017/05/16 13:50:33
I can see how this shortcut is tempting. The issue
hub
2017/05/16 21:35:56
:-/ This is really unfortunate.
| |
94 if (selector != "") | 82 if (selector) |
95 newSelector += " > "; | 83 newSelector += " > " + selector; |
96 return newSelector + selector; | 84 return newSelector; |
97 } | 85 } |
98 let idx = positionInParent(node); | 86 let idx = positionInParent(node); |
99 if (idx > 0) | 87 if (idx > 0) |
Wladimir Palant
2017/05/16 13:50:33
Usually, this will work because there is only one
hub
2017/05/25 13:02:28
Done.
| |
100 { | 88 { |
101 let newSelector = `${node.tagName}:nth-child(${idx}) `; | 89 let newSelector = `${node.tagName}:nth-child(${idx})`; |
102 if (selector != "") | 90 if (selector) |
103 newSelector += "> "; | 91 newSelector += " > " + selector; |
Wladimir Palant
2017/05/16 13:50:38
Nit: you should remove the trailing whitespace on
hub
2017/05/16 21:35:53
Acknowledged.
Wladimir Palant
2017/06/01 10:16:32
Acknowledged but the code is unchanged :)
Well, I
hub
2017/06/01 18:22:54
it is done patch set 19, definitely.
Wladimir Palant
2017/06/07 08:35:19
The trailing whitespace part - yes. Only doing the
hub
2017/06/07 14:15:06
Current code is:
let newSelector = `${node.ta
Wladimir Palant
2017/06/07 14:53:40
It's about the concatenation on the next line ;)
hub
2017/06/07 15:08:09
Ah !.
But if selector is empty, it doesn't matte
| |
104 return makeSelector(node.parentNode, newSelector + selector); | 92 return makeSelector(node.parentElement, newSelector); |
105 } | 93 } |
106 | 94 |
107 return selector; | 95 return selector; |
108 } | 96 } |
109 | 97 |
110 // return the regexString for the properties | 98 function parseSelectorContent(content, startIndex) |
111 function parsePropSelPattern(propertyExpression) | 99 { |
Wladimir Palant
2017/05/16 13:50:34
This is logic which is only used by the PropSelect
hub
2017/05/16 21:35:54
I'll move it to the PropsSelector constructor. Tha
| |
100 let parens = 1; | |
101 let quote = null; | |
102 let i = startIndex; | |
103 for (; i < content.length; i++) | |
104 { | |
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); | |
145 if (!match) | |
146 return [new PlainSelector(selector)]; | |
147 | |
148 let selectors = []; | |
149 if (match.index > 0) | |
150 selectors.push(new PlainSelector(selector.substr(0, match.index))); | |
151 | |
152 let startIndex = match.index + match[0].length; | |
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) | |
112 { | 294 { |
113 let regexpString; | 295 let regexpString; |
114 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | 296 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && |
115 propertyExpression[propertyExpression.length - 1] == "/") | 297 propertyExpression[propertyExpression.length - 1] == "/") |
298 { | |
116 regexpString = propertyExpression.slice(1, -1) | 299 regexpString = propertyExpression.slice(1, -1) |
117 .replace("\\x7B ", "{").replace("\\x7D ", "}"); | 300 .replace("\\x7B ", "{").replace("\\x7D ", "}"); |
Wladimir Palant
2017/05/16 13:50:39
Nit: You need braces around the if body here. Also
hub
2017/05/16 21:35:51
ESLint doesn't flag this.
| |
301 } | |
118 else | 302 else |
119 regexpString = filterToRegExp(propertyExpression); | 303 regexpString = filterToRegExp(propertyExpression); |
120 return regexpString; | 304 |
Wladimir Palant
2017/05/16 13:50:36
IMHO this should return the actual RegExp rather t
hub
2017/05/16 21:35:55
Acknowledged.
| |
121 } | 305 this._regexp = new RegExp(regexpString, "i"); |
122 | 306 } |
123 function parseSelector(selector) | 307 |
124 { | 308 PropsSelector.prototype = { |
125 if (selector.length == 0) | 309 *findPropsSelectors(styles, prefix, regexp) |
126 return []; | 310 { |
127 | 311 for (let style of styles) |
128 let abpSelectorIndex = selector.indexOf(":-abp-"); | 312 if (regexp.test(style.style)) |
Wladimir Palant
2017/05/16 13:50:36
How about you don't just go looking for anything w
hub
2017/05/25 13:02:29
I think I took the "don't use regexp" from the pre
| |
129 if (abpSelectorIndex == -1) | 313 for (let subSelector of style.subSelectors) |
130 return [new PlainSelector(selector)]; | 314 yield prefix + subSelector; |
131 | 315 }, |
132 let selectors = []; | 316 |
133 if (abpSelectorIndex > 0) | 317 *getSelectors(prefix, subtree, styles) |
134 selectors.push(new PlainSelector(selector.substr(0, abpSelectorIndex))); | 318 { |
135 | 319 for (let selector of this.findPropsSelectors(styles, prefix, this._regexp)) |
136 let suffixStart = abpSelectorIndex; | 320 yield [selector, subtree]; |
137 | |
138 if (selector.indexOf(":-abp-properties(", abpSelectorIndex) == | |
139 abpSelectorIndex) | |
140 { | |
141 let startIndex = abpSelectorIndex + 17; | |
142 let endquoteIndex = selector.indexOf(selector[startIndex], startIndex + 1); | |
143 if ((endquoteIndex == -1) || (selector[endquoteIndex + 1] != ")")) | |
144 return null; | |
145 | |
146 selectors.push(new PropsSelector( | |
147 selector.substr(startIndex + 1, endquoteIndex - startIndex - 1))); | |
148 suffixStart = endquoteIndex + 2; | |
149 } | |
150 else if (selector.indexOf(":-abp-has(", abpSelectorIndex) == | |
151 abpSelectorIndex) | |
152 { | |
153 let startIndex = abpSelectorIndex + 10; | |
154 let parens = 1; | |
155 let i; | |
156 for (i = startIndex; i < selector.length; i++) | |
157 { | |
158 if (selector[i] == "(") | |
159 parens++; | |
160 else if (selector[i] == ")") | |
161 parens--; | |
162 | |
163 if (parens == 0) | |
164 break; | |
165 } | |
166 if (parens != 0) | |
167 return null; | |
168 | |
169 let hasSelector = new HasSelector( | |
170 selector.substr(startIndex, i - startIndex)); | |
171 if (!hasSelector.ok()) | |
172 return null; | |
173 selectors.push(hasSelector); | |
174 suffixStart = i + 1; | |
Wladimir Palant
2017/05/16 13:50:33
I'm not really happy with the way the contents of
hub
2017/05/25 13:02:28
They are parsed differently because -abp-propertie
| |
175 } | |
176 else | |
177 { | |
178 // this is an error, can't parse selector. | |
179 return null; | |
180 } | |
181 | |
182 let suffix = parseSelector(selector.substr(suffixStart)); | |
183 if (suffix == null) | |
184 return null; | |
185 | |
186 selectors.push(...suffix); | |
187 | |
188 return selectors; | |
189 } | |
190 | |
191 function matchStyleProps(style, rule, pattern, selectors, filters) | |
Wladimir Palant
2017/05/16 13:50:36
There is something wrong with this function. Pleas
hub
2017/05/16 21:35:54
I did merge that function into findPropsSelectors(
| |
192 { | |
193 if (pattern.regexp.test(style)) | |
194 { | |
195 let subSelectors = splitSelector(rule.selectorText); | |
196 for (let i = 0; i < subSelectors.length; i++) | |
197 { | |
198 let subSelector = subSelectors[i]; | |
Wladimir Palant
2017/05/16 13:50:37
This should be a for..of loop.
hub
2017/05/16 21:35:54
Acknowledged.
| |
199 selectors.push(pattern.prefix + subSelector + pattern.suffix); | |
200 filters.push(pattern.text); | |
201 } | |
202 } | |
203 } | |
204 | |
205 function findPropsSelectors(stylesheet, pattern, selectors, filters) | |
206 { | |
207 let rules = stylesheet.cssRules; | |
208 if (!rules) | |
209 return; | |
210 | |
211 for (let rule of rules) | |
212 { | |
213 if (rule.type != rule.STYLE_RULE) | |
214 continue; | |
215 | |
216 let style = stringifyStyle(rule.style); | |
217 matchStyleProps(style, rule, pattern, selectors, filters); | |
218 } | |
Wladimir Palant
2017/05/16 13:50:37
We are still iterating through all stylesheets for
hub
2017/05/31 02:16:49
Done.
| |
219 } | |
220 | |
221 function stringifyStyle(style) | |
222 { | |
223 let styles = []; | |
224 for (let i = 0; i < style.length; i++) | |
225 { | |
226 let property = style.item(i); | |
227 let value = style.getPropertyValue(property); | |
228 let priority = style.getPropertyPriority(property); | |
229 styles.push(property + ": " + value + (priority ? " !" + priority : "") + | |
230 ";"); | |
231 } | |
232 styles.sort(); | |
233 return styles.join(" "); | |
234 } | |
235 | |
236 function* evaluate(chain, index, prefix, subtree, stylesheet) | |
237 { | |
238 if (index >= chain.length) | |
239 { | |
240 yield prefix; | |
241 return; | |
242 } | |
243 for (let [selector, element] of | |
244 chain[index].getSelectors(prefix, subtree, stylesheet)) | |
245 yield* evaluate(chain, index + 1, selector, element, stylesheet); | |
246 } | |
247 | |
248 /* | |
249 * getSelector() is a generator function returning a pair of selector | |
250 * string and subtree. | |
251 * getElements() is a generator function returning elements selected. | |
Wladimir Palant
2017/05/16 13:50:39
This should be two proper JSDoc comments on the re
hub
2017/05/16 21:35:54
Acknowledged.
| |
252 */ | |
253 function PlainSelector(selector) | |
254 { | |
255 this._selector = selector; | |
256 } | |
257 | |
258 PlainSelector.prototype = { | |
259 *getSelectors(prefix, subtree, stylesheet) | |
260 { | |
261 yield [prefix + this._selector, subtree]; | |
262 }, | |
263 | |
264 *getElements(prefix, subtree, stylesheet) | |
Wladimir Palant
2017/05/16 13:50:37
getElements() is never being called. On the other
hub
2017/05/26 12:42:46
This is something that I still need to address.
hub
2017/05/31 02:16:49
I removed the unused getElements. But I don't addr
Wladimir Palant
2017/06/01 10:16:32
We (Felix, Sebastian and me) discussed this in the
| |
265 { | |
266 for (let selector of this.getSelectors(prefix, subtree, stylesheet)) | |
Wladimir Palant
2017/05/16 13:50:36
Please use proper destructuring - the result isn't
hub
2017/05/16 21:35:50
It would be `let [selector] of `. The `, _` part c
| |
267 for (let element of subtree.querySelectorAll(selector[0])) | |
268 yield element; | |
269 } | 321 } |
270 }; | 322 }; |
271 | 323 |
272 function HasSelector(selector) | 324 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc, |
273 { | 325 hideElemsFunc) |
274 this._innerSelectors = parseSelector(selector); | |
275 } | |
276 | |
277 HasSelector.prototype = { | |
278 ok() | |
Wladimir Palant
2017/05/16 13:50:38
Rename this into valid()?
hub
2017/05/16 21:35:55
Acknowledged.
| |
279 { | |
280 return this._innerSelectors != null; | |
281 }, | |
282 | |
283 *getSelectors(prefix, subtree, stylesheet) | |
284 { | |
285 for (let element of this.getElements(prefix, subtree, stylesheet)) | |
286 yield [prefix + makeSelector(element, ""), subtree]; | |
Wladimir Palant
2017/05/16 13:50:39
The prefix should be ignored here - the result of
hub
2017/05/16 21:35:52
Done.
| |
287 }, | |
288 | |
289 *getElements(prefix, subtree, stylesheet) | |
290 { | |
291 let elements = subtree.querySelectorAll(prefix ? prefix : "*"); | |
Wladimir Palant
2017/05/16 13:50:34
This still won't do the right thing for something
hub
2017/05/16 21:35:52
Acknowledged.
| |
292 for (let element of elements) | |
293 { | |
294 let newPrefix = makeSelector(element, ""); | |
295 let iter = evaluate(this._innerSelectors, 0, "", element, stylesheet); | |
Wladimir Palant
2017/05/16 13:50:34
Why pass empty string rather than newPrefix + " "
hub
2017/05/16 21:35:55
I pass `element` as the subtree so from here I don
Wladimir Palant
2017/06/01 10:16:33
But you need it so that you get the right selector
hub
2017/06/01 18:22:54
Then below, line 299 I just pass "selector".
Done
| |
296 for (let selector of iter) | |
297 // we insert a space between the two. It becomes a no-op if selector | |
298 // doesn't have a combinator | |
299 if (subtree.querySelector(newPrefix + " " + selector)) | |
300 yield element; | |
301 } | |
302 } | |
303 }; | |
304 | |
305 function PropsSelector(selector) | |
306 { | |
307 this._regexp = new RegExp(parsePropSelPattern(selector), "i"); | |
308 } | |
309 | |
310 PropsSelector.prototype = { | |
311 *getSelectors(prefix, subtree, stylesheet) | |
312 { | |
313 let selectors = []; | |
314 let filters = []; | |
315 let selPattern = { | |
316 prefix, | |
317 suffix: "", | |
Wladimir Palant
2017/05/16 13:50:36
Why do we still have the suffix here if it is alwa
hub
2017/05/16 21:35:55
I think `suffix` left was an oversight from when I
| |
318 regexp: this._regexp | |
319 }; | |
320 | |
321 findPropsSelectors(stylesheet, selPattern, selectors, filters); | |
Wladimir Palant
2017/05/16 13:50:37
findPropsSelectors() should really be a generator
hub
2017/05/16 21:35:53
we don't even need filter at the point. they are t
| |
322 for (let selector of selectors) | |
323 yield [selector, subtree]; | |
324 }, | |
325 | |
326 *getElements(prefix, subtree, stylesheet) | |
327 { | |
328 for (let [selector, element] of | |
329 this.getSelectors(prefix, subtree, stylesheet)) | |
330 for (let subElement of element.querySelectorAll(selector)) | |
Wladimir Palant
2017/05/16 13:50:38
Please don't pretend that element is meaningful he
hub
2017/05/16 21:35:50
Done.
| |
331 yield subElement; | |
332 } | |
333 }; | |
334 | |
335 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc) | |
336 { | 326 { |
337 this.window = window; | 327 this.window = window; |
338 this.getFiltersFunc = getFiltersFunc; | 328 this.getFiltersFunc = getFiltersFunc; |
339 this.addSelectorsFunc = addSelectorsFunc; | 329 this.addSelectorsFunc = addSelectorsFunc; |
330 this.hideElemsFunc = hideElemsFunc; | |
340 } | 331 } |
341 | 332 |
342 ElemHideEmulation.prototype = { | 333 ElemHideEmulation.prototype = { |
343 | |
Wladimir Palant
2017/05/16 13:50:35
Nit: This empty line needs to go (ESLint should wa
hub
2017/05/16 21:35:53
Sadly it doesn't.
| |
344 isSameOrigin(stylesheet) | 334 isSameOrigin(stylesheet) |
345 { | 335 { |
346 try | 336 try |
347 { | 337 { |
348 return new URL(stylesheet.href).origin == this.window.location.origin; | 338 return new URL(stylesheet.href).origin == this.window.location.origin; |
349 } | 339 } |
350 catch (e) | 340 catch (e) |
351 { | 341 { |
352 // Invalid URL, assume that it is first-party. | 342 // Invalid URL, assume that it is first-party. |
353 return true; | 343 return true; |
354 } | 344 } |
355 }, | 345 }, |
356 | 346 |
357 addSelectors(stylesheet) | 347 addSelectors(stylesheets) |
358 { | 348 { |
359 let selectors = []; | 349 let selectors = []; |
360 let filters = []; | 350 let selectorFilters = []; |
361 | 351 |
362 // Explicitly ignore third-party stylesheets to ensure consistent behavior | 352 let elements = []; |
363 // between Firefox and Chrome. | 353 let elementFilters = []; |
364 if (!this.isSameOrigin(stylesheet)) | 354 |
365 return; | 355 let cssStyles = []; |
366 | 356 |
367 for (let patterns of this.selPatterns) | 357 for (let stylesheet of stylesheets) |
368 for (let selector of | 358 { |
369 evaluate(patterns.selectors, 0, "", document, stylesheet)) | 359 // Explicitly ignore third-party stylesheets to ensure consistent behavior |
360 // between Firefox and Chrome. | |
361 if (!this.isSameOrigin(stylesheet)) | |
362 continue; | |
363 | |
364 let rules = stylesheet.cssRules; | |
365 if (!rules) | |
366 continue; | |
367 | |
368 for (let rule of rules) | |
370 { | 369 { |
371 selectors.push(selector); | 370 if (rule.type != rule.STYLE_RULE) |
372 filters.push(patterns.text); | 371 continue; |
372 | |
373 cssStyles.push(stringifyStyle(rule)); | |
373 } | 374 } |
374 | 375 } |
375 this.addSelectorsFunc(selectors, filters); | 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); | |
376 }, | 401 }, |
377 | 402 |
378 onLoad(event) | 403 onLoad(event) |
379 { | 404 { |
380 let stylesheet = event.target.sheet; | 405 let stylesheet = event.target.sheet; |
381 if (stylesheet) | 406 if (stylesheet) |
382 this.addSelectors(stylesheet); | 407 this.addSelectors([stylesheet]); |
383 }, | 408 }, |
384 | 409 |
385 apply() | 410 apply() |
386 { | 411 { |
387 this.getFiltersFunc(patterns => | 412 this.getFiltersFunc(patterns => |
388 { | 413 { |
389 this.selPatterns = []; | 414 let oldReportError = reportError; |
390 | 415 reportError = error => this.window.console.error(error); |
416 | |
417 this.patterns = []; | |
391 for (let pattern of patterns) | 418 for (let pattern of patterns) |
392 { | 419 { |
393 let selectors = parseSelector(pattern.selector); | 420 let selectors = parseSelector(pattern.selector); |
394 if (selectors != null && selectors.length > 0) | 421 if (selectors != null && selectors.length > 0) |
395 this.selPatterns.push({selectors, text: pattern.text}); | 422 this.patterns.push({selectors, text: pattern.text}); |
396 } | 423 } |
397 | 424 |
398 if (this.selPatterns.length > 0) | 425 if (this.patterns.length > 0) |
399 { | 426 { |
400 let {document} = this.window; | 427 let {document} = this.window; |
401 for (let stylesheet of document.styleSheets) | 428 this.addSelectors(document.styleSheets); |
402 this.addSelectors(stylesheet); | |
403 document.addEventListener("load", this.onLoad.bind(this), true); | 429 document.addEventListener("load", this.onLoad.bind(this), true); |
404 } | 430 } |
431 reportError = oldReportError; | |
405 }); | 432 }); |
406 } | 433 } |
407 }; | 434 }; |
LEFT | RIGHT |