Left: | ||
Right: |
OLD | NEW |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /* globals filterToRegExp */ | 18 /* globals filterToRegExp */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
22 let propertySelectorRegExp = /\[-abp-properties=(["'])([^"']+)\1\]/; | |
23 | |
24 function splitSelector(selector) | 22 function splitSelector(selector) |
25 { | 23 { |
26 if (selector.indexOf(",") == -1) | 24 if (selector.indexOf(",") == -1) |
27 return [selector]; | 25 return [selector]; |
28 | 26 |
29 let selectors = []; | 27 let selectors = []; |
30 let start = 0; | 28 let start = 0; |
31 let level = 0; | 29 let level = 0; |
32 let sep = ""; | 30 let sep = ""; |
33 | 31 |
(...skipping 18 matching lines...) Expand all Loading... | |
52 selectors.push(selector.substring(start, i)); | 50 selectors.push(selector.substring(start, i)); |
53 start = i + 1; | 51 start = i + 1; |
54 } | 52 } |
55 } | 53 } |
56 } | 54 } |
57 | 55 |
58 selectors.push(selector.substring(start)); | 56 selectors.push(selector.substring(start)); |
59 return selectors; | 57 return selectors; |
60 } | 58 } |
61 | 59 |
60 // Return position of node from parent. | |
61 // 1 base index like for :nth-child() | |
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.
| |
62 function positionInParent(node) | |
63 { | |
64 let parentNode = node ? node.parentNode : null; | |
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) | |
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) | |
74 break; | |
75 return i + 1; | |
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 } | |
88 | |
89 function makeSelector(node, selector) | |
90 { | |
91 if (node && idValid(node.id)) | |
92 { | |
93 let newSelector = "#" + node.id; | |
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 != "") | |
95 newSelector += " > "; | |
96 return newSelector + selector; | |
97 } | |
98 let idx = positionInParent(node); | |
99 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 { | |
101 let newSelector = `${node.tagName}:nth-child(${idx}) `; | |
102 if (selector != "") | |
103 newSelector += "> "; | |
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); | |
105 } | |
106 | |
107 return selector; | |
108 } | |
109 | |
110 // return the regexString for the properties | |
111 function parsePropSelPattern(propertyExpression) | |
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
| |
112 { | |
113 let regexpString; | |
114 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | |
115 propertyExpression[propertyExpression.length - 1] == "/") | |
116 regexpString = propertyExpression.slice(1, -1) | |
117 .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.
| |
118 else | |
119 regexpString = filterToRegExp(propertyExpression); | |
120 return regexpString; | |
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 } | |
122 | |
123 function parseSelector(selector) | |
124 { | |
125 if (selector.length == 0) | |
126 return []; | |
127 | |
128 let abpSelectorIndex = selector.indexOf(":-abp-"); | |
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) | |
130 return [new PlainSelector(selector)]; | |
131 | |
132 let selectors = []; | |
133 if (abpSelectorIndex > 0) | |
134 selectors.push(new PlainSelector(selector.substr(0, abpSelectorIndex))); | |
135 | |
136 let suffixStart = abpSelectorIndex; | |
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 } | |
270 }; | |
271 | |
272 function HasSelector(selector) | |
273 { | |
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 | |
62 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc) | 335 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc) |
63 { | 336 { |
64 this.window = window; | 337 this.window = window; |
65 this.getFiltersFunc = getFiltersFunc; | 338 this.getFiltersFunc = getFiltersFunc; |
66 this.addSelectorsFunc = addSelectorsFunc; | 339 this.addSelectorsFunc = addSelectorsFunc; |
67 } | 340 } |
68 | 341 |
69 ElemHideEmulation.prototype = { | 342 ElemHideEmulation.prototype = { |
70 stringifyStyle(style) | |
71 { | |
72 let styles = []; | |
73 for (let i = 0; i < style.length; i++) | |
74 { | |
75 let property = style.item(i); | |
76 let value = style.getPropertyValue(property); | |
77 let priority = style.getPropertyPriority(property); | |
78 styles.push(property + ": " + value + (priority ? " !" + priority : "") + | |
79 ";"); | |
80 } | |
81 styles.sort(); | |
82 return styles.join(" "); | |
83 }, | |
84 | 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.
| |
85 isSameOrigin(stylesheet) | 344 isSameOrigin(stylesheet) |
86 { | 345 { |
87 try | 346 try |
88 { | 347 { |
89 return new URL(stylesheet.href).origin == this.window.location.origin; | 348 return new URL(stylesheet.href).origin == this.window.location.origin; |
90 } | 349 } |
91 catch (e) | 350 catch (e) |
92 { | 351 { |
93 // Invalid URL, assume that it is first-party. | 352 // Invalid URL, assume that it is first-party. |
94 return true; | 353 return true; |
95 } | 354 } |
96 }, | 355 }, |
97 | 356 |
98 findSelectors(stylesheet, selectors, filters) | 357 addSelectors(stylesheet) |
99 { | 358 { |
359 let selectors = []; | |
360 let filters = []; | |
361 | |
100 // Explicitly ignore third-party stylesheets to ensure consistent behavior | 362 // Explicitly ignore third-party stylesheets to ensure consistent behavior |
101 // between Firefox and Chrome. | 363 // between Firefox and Chrome. |
102 if (!this.isSameOrigin(stylesheet)) | 364 if (!this.isSameOrigin(stylesheet)) |
103 return; | 365 return; |
104 | 366 |
105 let rules = stylesheet.cssRules; | 367 for (let patterns of this.selPatterns) |
106 if (!rules) | 368 for (let selector of |
107 return; | 369 evaluate(patterns.selectors, 0, "", document, stylesheet)) |
108 | |
109 for (let rule of rules) | |
110 { | |
111 if (rule.type != rule.STYLE_RULE) | |
112 continue; | |
113 | |
114 let style = this.stringifyStyle(rule.style); | |
115 for (let pattern of this.patterns) | |
116 { | 370 { |
117 if (pattern.regexp.test(style)) | 371 selectors.push(selector); |
118 { | 372 filters.push(patterns.text); |
119 let subSelectors = splitSelector(rule.selectorText); | |
120 for (let subSelector of subSelectors) | |
121 { | |
122 selectors.push(pattern.prefix + subSelector + pattern.suffix); | |
123 filters.push(pattern.text); | |
124 } | |
125 } | |
126 } | 373 } |
127 } | 374 |
128 }, | |
129 | |
130 addSelectors(stylesheets) | |
131 { | |
132 let selectors = []; | |
133 let filters = []; | |
134 for (let stylesheet of stylesheets) | |
135 this.findSelectors(stylesheet, selectors, filters); | |
136 this.addSelectorsFunc(selectors, filters); | 375 this.addSelectorsFunc(selectors, filters); |
137 }, | 376 }, |
138 | 377 |
139 onLoad(event) | 378 onLoad(event) |
140 { | 379 { |
141 let stylesheet = event.target.sheet; | 380 let stylesheet = event.target.sheet; |
142 if (stylesheet) | 381 if (stylesheet) |
143 this.addSelectors([stylesheet]); | 382 this.addSelectors(stylesheet); |
144 }, | 383 }, |
145 | 384 |
146 apply() | 385 apply() |
147 { | 386 { |
148 this.getFiltersFunc(patterns => | 387 this.getFiltersFunc(patterns => |
149 { | 388 { |
150 this.patterns = []; | 389 this.selPatterns = []; |
390 | |
151 for (let pattern of patterns) | 391 for (let pattern of patterns) |
152 { | 392 { |
153 let match = propertySelectorRegExp.exec(pattern.selector); | 393 let selectors = parseSelector(pattern.selector); |
154 if (!match) | 394 if (selectors != null && selectors.length > 0) |
155 continue; | 395 this.selPatterns.push({selectors, text: pattern.text}); |
156 | |
157 let propertyExpression = match[2]; | |
158 let regexpString; | |
159 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | |
160 propertyExpression[propertyExpression.length - 1] == "/") | |
161 { | |
162 regexpString = propertyExpression.slice(1, -1) | |
163 .replace("\\x7B ", "{").replace("\\x7D ", "}"); | |
164 } | |
165 else | |
166 regexpString = filterToRegExp(propertyExpression); | |
167 | |
168 this.patterns.push({ | |
169 text: pattern.text, | |
170 regexp: new RegExp(regexpString, "i"), | |
171 prefix: pattern.selector.substr(0, match.index), | |
172 suffix: pattern.selector.substr(match.index + match[0].length) | |
173 }); | |
174 } | 396 } |
175 | 397 |
176 if (this.patterns.length > 0) | 398 if (this.selPatterns.length > 0) |
177 { | 399 { |
178 let {document} = this.window; | 400 let {document} = this.window; |
179 this.addSelectors(document.styleSheets); | 401 for (let stylesheet of document.styleSheets) |
402 this.addSelectors(stylesheet); | |
180 document.addEventListener("load", this.onLoad.bind(this), true); | 403 document.addEventListener("load", this.onLoad.bind(this), true); |
181 } | 404 } |
182 }); | 405 }); |
183 } | 406 } |
184 }; | 407 }; |
OLD | NEW |