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 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 selectors.push(selector.substring(start, i)); | 52 selectors.push(selector.substring(start, i)); |
53 start = i + 1; | 53 start = i + 1; |
54 } | 54 } |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 selectors.push(selector.substring(start)); | 58 selectors.push(selector.substring(start)); |
59 return selectors; | 59 return selectors; |
60 } | 60 } |
61 | 61 |
62 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc) | 62 /** Return position of node from parent. |
63 * @param {Node} node the node to find the position of. | |
64 * @return {number} One-based index like for :nth-child(), or 0 on error. | |
65 */ | |
66 function positionInParent(node) | |
67 { | |
68 let {children} = node.parentNode; | |
69 for (let i = 0; i < children.length; i++) | |
70 if (children[i] == node) | |
71 return i + 1; | |
72 return 0; | |
73 } | |
74 | |
75 function makeSelector(node, selector) | |
76 { | |
77 if (!node.parentElement) | |
78 { | |
79 let newSelector = ":root"; | |
80 if (selector) | |
81 newSelector += " > " + selector; | |
82 return newSelector; | |
83 } | |
84 let idx = positionInParent(node); | |
85 if (idx > 0) | |
86 { | |
87 let newSelector = `${node.tagName}:nth-child(${idx})`; | |
88 if (selector) | |
89 newSelector += " > " + selector; | |
90 return makeSelector(node.parentElement, newSelector); | |
91 } | |
92 | |
93 return selector; | |
94 } | |
95 | |
96 function parseSelectorContent(content, startIndex) | |
97 { | |
98 let parens = 1; | |
99 let quote = null; | |
100 let i = startIndex; | |
101 for (; i < content.length; i++) | |
102 { | |
103 let c = content[i]; | |
104 if (c == "\\") | |
105 { | |
106 // Ignore escaped characters | |
107 i++; | |
108 } | |
109 else if (quote) | |
110 { | |
111 if (c == quote) | |
112 quote = null; | |
113 } | |
114 else if (c == "'" || c == '"') | |
115 quote = c; | |
116 else if (c == "(") | |
117 parens++; | |
118 else if (c == ")") | |
119 { | |
120 parens--; | |
121 if (parens == 0) | |
122 break; | |
123 } | |
124 } | |
125 | |
126 if (parens > 0) | |
127 return null; | |
128 return {text: content.substring(startIndex, i), end: i}; | |
129 } | |
130 | |
131 /** Parse the selector | |
132 * @param {string} selector the selector to parse | |
133 * @return {Object} selectors is an array of objects, | |
134 * or null in case of errors. hide is true if we'll hide | |
135 * elements instead of styles.. | |
136 */ | |
137 function parseSelector(selector) | |
138 { | |
139 if (selector.length == 0) | |
140 return []; | |
141 | |
142 let match = abpSelectorRegexp.exec(selector); | |
143 if (!match) | |
144 return [new PlainSelector(selector)]; | |
145 | |
146 let selectors = []; | |
147 if (match.index > 0) | |
148 selectors.push(new PlainSelector(selector.substr(0, match.index))); | |
149 | |
150 let startIndex = match.index + match[0].length; | |
151 let content = parseSelectorContent(selector, startIndex); | |
152 if (content == null) | |
Sebastian Noack
2017/06/08 08:28:47
Nit: Given that parseSelectorContent() either retu
hub
2017/06/08 15:45:47
Done.
| |
153 { | |
154 console.error(new SyntaxError("Failed parsing AdBlock Plus " + | |
Sebastian Noack
2017/06/08 08:28:48
Nit: It's Adblock Plus (with lower case "B"). But
hub
2017/06/08 15:45:48
Maybe we could change this to "Failed parsing cont
Felix Dahlke
2017/06/09 09:02:33
Well, we're actually parsing what we call an "ABP
| |
155 `selector ${selector}, didn't ` + | |
156 "find closing parenthesis.")); | |
157 return null; | |
158 } | |
159 if (match[1] == "properties") | |
160 selectors.push(new PropsSelector(content.text)); | |
161 else if (match[1] == "has") | |
162 { | |
163 let hasSelector = new HasSelector(content.text); | |
164 if (!hasSelector.valid()) | |
165 return null; | |
166 selectors.push(hasSelector); | |
167 } | |
168 else | |
169 { | |
170 // this is an error, can't parse selector. | |
171 console.error(new SyntaxError("Failed parsing AdBlock Plus " + | |
172 `selector ${selector}, invalid ` + | |
173 `pseudo-class -abp-${match[1]}().`)); | |
174 return null; | |
175 } | |
176 | |
177 let suffix = parseSelector(selector.substr(content.end + 1)); | |
178 if (suffix == null) | |
179 return null; | |
180 | |
181 selectors.push(...suffix); | |
182 | |
183 return selectors; | |
184 } | |
185 | |
186 /** Stringified style objects | |
187 * @typedef {Object} StringifiedStyle | |
188 * @property {string} style CSS style represented a string. | |
Wladimir Palant
2017/06/08 13:12:40
This doesn't seem to be quite English to me, also
hub
2017/06/08 15:45:47
Acknowledged.
| |
189 * @property {string[]} subSelectors selectors for the rule. | |
Wladimir Palant
2017/06/08 13:12:39
"selectors that the CSS properties apply to"?
hub
2017/06/08 15:45:46
Acknowledged.
| |
190 */ | |
191 | |
192 /** | |
193 * Turn a CSSStyleRule into a StringifiedStyle | |
Wladimir Palant
2017/06/08 13:12:39
You have the types listed below, no need to repeat
hub
2017/06/08 15:45:47
Acknowledged.
| |
194 * @param {CSSStyleRule} rule the CSS style rule. | |
195 * @return {StringifiedStyle} the stringified style. | |
196 */ | |
197 function stringifyStyle(rule) | |
198 { | |
199 let styles = []; | |
200 for (let i = 0; i < rule.style.length; i++) | |
201 { | |
202 let property = rule.style.item(i); | |
203 let value = rule.style.getPropertyValue(property); | |
204 let priority = rule.style.getPropertyPriority(property); | |
205 styles.push(property + ": " + value + (priority ? " !" + priority : "") + | |
Sebastian Noack
2017/06/08 08:28:47
Nit: If you'd use a template literal here you woud
hub
2017/06/08 15:45:46
This code was simply moved and written pre-ES6. I'
| |
206 ";"); | |
207 } | |
208 styles.sort(); | |
209 return { | |
210 style: styles.join(" "), | |
211 subSelectors: splitSelector(rule.selectorText) | |
212 }; | |
213 } | |
214 | |
215 function* evaluate(chain, index, prefix, subtree, styles) | |
216 { | |
217 if (index >= chain.length) | |
218 { | |
219 yield prefix; | |
220 return; | |
221 } | |
222 for (let [selector, element] of | |
223 chain[index].getSelectors(prefix, subtree, styles)) | |
224 yield* evaluate(chain, index + 1, selector, element, styles); | |
225 } | |
226 | |
227 function PlainSelector(selector) | |
228 { | |
229 this._selector = selector; | |
230 } | |
231 | |
232 PlainSelector.prototype = { | |
233 /** | |
234 * Generator function returning a pair of selector | |
235 * string and subtree. | |
236 * @param {string} prefix the prefix for the selector. | |
237 * @param {Node} subtree the subtree we work on. | |
238 * @param {StringifiedStyle[]} styles the stringified style objects. | |
239 */ | |
240 *getSelectors(prefix, subtree, styles) | |
241 { | |
242 yield [prefix + this._selector, subtree]; | |
243 } | |
244 }; | |
245 | |
246 const incompletePrefixRegexp = /[\s>+~]$/; | |
247 | |
248 function HasSelector(selector) | |
249 { | |
250 this._innerSelectors = parseSelector(selector); | |
251 } | |
252 | |
253 HasSelector.prototype = { | |
254 requiresHiding: true, | |
255 | |
256 valid() | |
257 { | |
258 return this._innerSelectors != null; | |
259 }, | |
260 | |
261 *getSelectors(prefix, subtree, styles) | |
262 { | |
263 for (let element of this.getElements(prefix, subtree, styles)) | |
264 yield [makeSelector(element, ""), element]; | |
265 }, | |
266 | |
267 /** | |
268 * Generator function returning selected elements. | |
269 * @param {string} prefix the prefix for the selector. | |
270 * @param {Node} subtree the subtree we work on. | |
271 * @param {StringifiedStyle[]} styles the stringified style objects. | |
272 */ | |
273 *getElements(prefix, subtree, styles) | |
274 { | |
275 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? | |
276 prefix + "*" : prefix; | |
277 let elements = subtree.querySelectorAll(actualPrefix); | |
278 for (let element of elements) | |
279 { | |
280 let newPrefix = makeSelector(element, ""); | |
281 let iter = evaluate(this._innerSelectors, 0, newPrefix + " ", | |
282 element, styles); | |
283 for (let selector of iter) | |
284 // we insert a space between the two. It becomes a no-op if selector | |
285 // doesn't have a combinator | |
286 if (subtree.querySelector(selector)) | |
287 yield element; | |
288 } | |
289 } | |
290 }; | |
291 | |
292 function PropsSelector(propertyExpression) | |
293 { | |
294 let regexpString; | |
295 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | |
296 propertyExpression[propertyExpression.length - 1] == "/") | |
297 { | |
298 regexpString = propertyExpression.slice(1, -1) | |
299 .replace("\\x7B ", "{").replace("\\x7D ", "}"); | |
300 } | |
301 else | |
302 regexpString = filterToRegExp(propertyExpression); | |
303 | |
304 this._regexp = new RegExp(regexpString, "i"); | |
305 } | |
306 | |
307 PropsSelector.prototype = { | |
308 *findPropsSelectors(styles, prefix, regexp) | |
309 { | |
310 for (let style of styles) | |
311 if (regexp.test(style.style)) | |
312 for (let subSelector of style.subSelectors) | |
313 yield prefix + subSelector; | |
314 }, | |
315 | |
316 *getSelectors(prefix, subtree, styles) | |
317 { | |
318 for (let selector of this.findPropsSelectors(styles, prefix, this._regexp)) | |
319 yield [selector, subtree]; | |
320 } | |
321 }; | |
322 | |
323 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc, | |
324 hideElemsFunc) | |
63 { | 325 { |
64 this.window = window; | 326 this.window = window; |
65 this.getFiltersFunc = getFiltersFunc; | 327 this.getFiltersFunc = getFiltersFunc; |
66 this.addSelectorsFunc = addSelectorsFunc; | 328 this.addSelectorsFunc = addSelectorsFunc; |
329 this.hideElemsFunc = hideElemsFunc; | |
67 } | 330 } |
68 | 331 |
69 ElemHideEmulation.prototype = { | 332 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 | |
85 isSameOrigin(stylesheet) | 333 isSameOrigin(stylesheet) |
86 { | 334 { |
87 try | 335 try |
88 { | 336 { |
89 return new URL(stylesheet.href).origin == this.window.location.origin; | 337 return new URL(stylesheet.href).origin == this.window.location.origin; |
90 } | 338 } |
91 catch (e) | 339 catch (e) |
92 { | 340 { |
93 // Invalid URL, assume that it is first-party. | 341 // Invalid URL, assume that it is first-party. |
94 return true; | 342 return true; |
95 } | 343 } |
96 }, | 344 }, |
97 | 345 |
98 findSelectors(stylesheet, selectors, filters) | 346 addSelectors(stylesheets) |
99 { | 347 { |
100 // Explicitly ignore third-party stylesheets to ensure consistent behavior | 348 let selectors = []; |
101 // between Firefox and Chrome. | 349 let selectorFilters = []; |
102 if (!this.isSameOrigin(stylesheet)) | 350 |
103 return; | 351 let elements = []; |
104 | 352 let elementFilters = []; |
105 let rules = stylesheet.cssRules; | 353 |
106 if (!rules) | 354 let cssStyles = []; |
107 return; | 355 |
108 | 356 for (let stylesheet of stylesheets) |
109 for (let rule of rules) | 357 { |
110 { | 358 // Explicitly ignore third-party stylesheets to ensure consistent behavior |
111 if (rule.type != rule.STYLE_RULE) | 359 // between Firefox and Chrome. |
360 if (!this.isSameOrigin(stylesheet)) | |
112 continue; | 361 continue; |
113 | 362 |
114 let style = this.stringifyStyle(rule.style); | 363 let rules = stylesheet.cssRules; |
115 for (let pattern of this.patterns) | 364 if (!rules) |
365 continue; | |
366 | |
367 for (let rule of rules) | |
116 { | 368 { |
117 if (pattern.regexp.test(style)) | 369 if (rule.type != rule.STYLE_RULE) |
370 continue; | |
371 | |
372 cssStyles.push(stringifyStyle(rule)); | |
373 } | |
374 } | |
375 | |
376 for (let pattern of this.patterns) | |
377 { | |
378 for (let selector of evaluate(pattern.selectors, | |
379 0, "", document, cssStyles)) | |
380 { | |
381 if (!pattern.selectors.some(s => s.requiresHiding)) | |
118 { | 382 { |
119 let subSelectors = splitSelector(rule.selectorText); | 383 selectors.push(selector); |
120 for (let subSelector of subSelectors) | 384 selectorFilters.push(pattern.text); |
385 } | |
386 else | |
387 { | |
388 for (let element of document.querySelectorAll(selector)) | |
121 { | 389 { |
122 selectors.push(pattern.prefix + subSelector + pattern.suffix); | 390 elements.push(element); |
123 filters.push(pattern.text); | 391 elementFilters.push(pattern.text); |
124 } | 392 } |
125 } | 393 } |
126 } | 394 } |
127 } | 395 } |
128 }, | 396 |
129 | 397 this.addSelectorsFunc(selectors, selectorFilters); |
130 addSelectors(stylesheets) | 398 this.hideElemsFunc(elements, elementFilters); |
131 { | |
132 let selectors = []; | |
133 let filters = []; | |
134 for (let stylesheet of stylesheets) | |
135 this.findSelectors(stylesheet, selectors, filters); | |
136 this.addSelectorsFunc(selectors, filters); | |
137 }, | 399 }, |
138 | 400 |
139 onLoad(event) | 401 onLoad(event) |
140 { | 402 { |
141 let stylesheet = event.target.sheet; | 403 let stylesheet = event.target.sheet; |
142 if (stylesheet) | 404 if (stylesheet) |
143 this.addSelectors([stylesheet]); | 405 this.addSelectors([stylesheet]); |
144 }, | 406 }, |
145 | 407 |
146 apply() | 408 apply() |
147 { | 409 { |
148 this.getFiltersFunc(patterns => | 410 this.getFiltersFunc(patterns => |
149 { | 411 { |
150 this.patterns = []; | 412 this.patterns = []; |
151 for (let pattern of patterns) | 413 for (let pattern of patterns) |
152 { | 414 { |
153 let match = abpSelectorRegexp.exec(pattern.selector); | 415 let selectors = parseSelector(pattern.selector); |
154 if (!match || match[1] != "properties") | 416 if (selectors != null && selectors.length > 0) |
155 { | 417 this.patterns.push({selectors, text: pattern.text}); |
156 console.error(new SyntaxError( | |
157 `Failed to parse Adblock Plus selector ${pattern.selector}, ` + | |
158 `invalid pseudo-class :-abp-${match[1]}().` | |
159 )); | |
160 continue; | |
161 } | |
162 | |
163 let expressionStart = match.index + match[0].length; | |
164 let parens = 1; | |
165 let quote = null; | |
166 let i; | |
167 for (i = expressionStart; i < pattern.selector.length; i++) | |
168 { | |
169 let c = pattern.selector[i]; | |
170 if (c == "\\") | |
171 { | |
172 // Ignore escaped characters | |
173 i++; | |
174 } | |
175 else if (quote) | |
176 { | |
177 if (c == quote) | |
178 quote = null; | |
179 } | |
180 else if (c == "'" || c == '"') | |
181 quote = c; | |
182 else if (c == "(") | |
183 parens++; | |
184 else if (c == ")") | |
185 { | |
186 parens--; | |
187 if (parens == 0) | |
188 break; | |
189 } | |
190 } | |
191 | |
192 if (parens > 0) | |
193 { | |
194 console.error(new SyntaxError( | |
195 `Failed to parse Adblock Plus selector ${pattern.selector} ` + | |
196 "due to unmatched parentheses." | |
197 )); | |
198 continue; | |
199 } | |
200 | |
201 let propertyExpression = pattern.selector.substring(expressionStart, i); | |
202 let regexpString; | |
203 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | |
204 propertyExpression[propertyExpression.length - 1] == "/") | |
205 { | |
206 regexpString = propertyExpression.slice(1, -1) | |
207 .replace("\\x7B ", "{").replace("\\x7D ", "}"); | |
208 } | |
209 else | |
210 regexpString = filterToRegExp(propertyExpression); | |
211 | |
212 this.patterns.push({ | |
213 text: pattern.text, | |
214 regexp: new RegExp(regexpString, "i"), | |
215 prefix: pattern.selector.substr(0, match.index), | |
216 suffix: pattern.selector.substr(i + 1) | |
217 }); | |
218 } | 418 } |
219 | 419 |
220 if (this.patterns.length > 0) | 420 if (this.patterns.length > 0) |
221 { | 421 { |
222 let {document} = this.window; | 422 let {document} = this.window; |
223 this.addSelectors(document.styleSheets); | 423 this.addSelectors(document.styleSheets); |
224 document.addEventListener("load", this.onLoad.bind(this), true); | 424 document.addEventListener("load", this.onLoad.bind(this), true); |
225 } | 425 } |
226 }); | 426 }); |
227 } | 427 } |
228 }; | 428 }; |
OLD | NEW |