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 += " > "; | |
82 return newSelector + selector; | |
83 } | |
84 let idx = positionInParent(node); | |
85 if (idx > 0) | |
86 { | |
87 let newSelector = `${node.tagName}:nth-child(${idx})`; | |
88 if (selector) | |
89 newSelector += " > "; | |
90 return makeSelector(node.parentElement, newSelector + selector); | |
91 } | |
92 | |
93 return selector; | |
94 } | |
95 | |
96 function parseSelectorContent(content) | |
97 { | |
98 let parens = 1; | |
99 let quote = null; | |
100 let i; | |
101 for (i = 0; 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.substr(0, i), end: i}; | |
129 } | |
130 | |
131 /** Parse the selector | |
132 * @param {string} selector the selector to parse | |
133 * @param {Number} level the depth level. 0 is top | |
134 * @return {Object} selectors is an array of objects, | |
135 * or null in case of errors. hide is true if we'll hide | |
136 * elements instead of styles.. | |
137 */ | |
138 function parseSelector(selector, level = 0) | |
Wladimir Palant
2017/06/07 08:32:58
Level parameter is no longer used.
hub
2017/06/07 14:15:08
I meant to remove it. it is gone now.
| |
139 { | |
140 if (selector.length == 0) | |
141 return {selectors: [], hide: false}; | |
142 | |
143 let match = abpSelectorRegexp.exec(selector); | |
144 if (!match) | |
145 return {selectors: [new PlainSelector(selector)], hide: false}; | |
146 | |
147 let hide = false; | |
148 let selectors = []; | |
149 let suffixStart = match.index; | |
150 if (suffixStart > 0) | |
151 selectors.push(new PlainSelector(selector.substr(0, match.index))); | |
Wladimir Palant
2017/06/07 08:32:58
I meant - suffixStart variable shouldn't be declar
hub
2017/06/07 14:15:08
done
| |
152 | |
153 let startIndex = match.index + match[0].length; | |
154 let content = parseSelectorContent(selector.substr(startIndex)); | |
155 if (content == null) | |
156 { | |
157 console.error(new SyntaxError("Failed parsing AdBlock Plus " + | |
158 `selector ${selector}, didn't ` + | |
159 "find closing parenthesis.")); | |
160 return {selectors: null, hide: false}; | |
161 } | |
162 if (match[1] == "properties") | |
163 selectors.push(new PropsSelector(content.text)); | |
164 else if (match[1] == "has") | |
165 { | |
166 let hasSelector = new HasSelector(content.text); | |
167 if (!hasSelector.valid()) | |
168 return {selectors: null, hide: false}; | |
169 selectors.push(hasSelector); | |
170 hide = true; | |
171 } | |
172 else | |
173 { | |
174 // this is an error, can't parse selector. | |
175 console.error(new SyntaxError("Failed parsing AdBlock Plus " + | |
176 `selector ${selector}, invalid ` + | |
177 `pseudo-class -abp-${match[1]}().`)); | |
178 return {selectors: null, hide: false}; | |
179 } | |
180 | |
181 suffixStart = startIndex + content.end + 1; | |
182 | |
183 let suffix = parseSelector(selector.substr(suffixStart), level); | |
184 if (suffix.selectors == null) | |
185 return {selectors: null, hide: false}; | |
186 | |
187 selectors.push(...suffix.selectors); | |
188 hide |= suffix.hide; | |
Wladimir Palant
2017/06/07 08:32:58
Using numerical operators on boolean values isn't
hub
2017/06/07 14:15:09
The problem with moving this out is that the decis
Wladimir Palant
2017/06/07 14:53:41
Assuming that I understood this sentence correctly
hub
2017/06/08 00:17:19
done
| |
189 | |
190 return {selectors, hide}; | |
191 } | |
192 | |
193 function stringifyStyle(style) | |
194 { | |
195 let styles = []; | |
196 for (let i = 0; i < style.length; i++) | |
197 { | |
198 let property = style.item(i); | |
199 let value = style.getPropertyValue(property); | |
200 let priority = style.getPropertyPriority(property); | |
201 styles.push(property + ": " + value + (priority ? " !" + priority : "") + | |
202 ";"); | |
203 } | |
204 styles.sort(); | |
205 return styles.join(" "); | |
206 } | |
207 | |
208 function* evaluate(chain, index, prefix, subtree, styles) | |
209 { | |
210 if (index >= chain.length) | |
211 { | |
212 yield prefix; | |
213 return; | |
214 } | |
215 for (let [selector, element] of | |
216 chain[index].getSelectors(prefix, subtree, styles)) | |
217 yield* evaluate(chain, index + 1, selector, element, styles); | |
218 } | |
219 | |
220 function PlainSelector(selector) | |
221 { | |
222 this._selector = selector; | |
223 } | |
224 | |
225 PlainSelector.prototype = { | |
226 /** | |
227 * Generator function returning a pair of selector | |
228 * string and subtree. | |
229 * @param {string} prefix the prefix for the selector. | |
230 * @param {Node} subtree the subtree we work on. | |
231 * @param {Array} styles the stringified stylesheet objects. | |
232 */ | |
233 *getSelectors(prefix, subtree, styles) | |
234 { | |
235 yield [prefix + this._selector, subtree]; | |
236 } | |
237 }; | |
238 | |
239 const incompletePrefixRegexp = /[\s>+~]$/; | |
240 | |
241 function HasSelector(selector, level = 0) | |
242 { | |
243 let inner = parseSelector(selector, level + 1); | |
244 this._innerSelectors = inner ? inner.selectors : null; | |
245 } | |
246 | |
247 HasSelector.prototype = { | |
248 valid() | |
249 { | |
250 return this._innerSelectors != null; | |
251 }, | |
252 | |
253 *getSelectors(prefix, subtree, styles) | |
254 { | |
255 for (let element of this.getElements(prefix, subtree, styles)) | |
256 yield [makeSelector(element, ""), element]; | |
257 }, | |
258 | |
259 /** | |
260 * Generator function returning selected elements. | |
261 * @param {string} prefix the prefix for the selector. | |
262 * @param {Node} subtree the subtree we work on. | |
263 * @param {Array} styles the stringified stylesheet objects. | |
264 */ | |
265 *getElements(prefix, subtree, styles) | |
266 { | |
267 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? | |
268 prefix + "*" : prefix; | |
269 let elements = subtree.querySelectorAll(actualPrefix); | |
270 for (let element of elements) | |
271 { | |
272 let newPrefix = makeSelector(element, ""); | |
273 let iter = evaluate(this._innerSelectors, 0, newPrefix + " ", | |
274 element, styles); | |
275 for (let selector of iter) | |
276 // we insert a space between the two. It becomes a no-op if selector | |
277 // doesn't have a combinator | |
278 if (subtree.querySelector(selector)) | |
279 yield element; | |
280 } | |
281 } | |
282 }; | |
283 | |
284 function PropsSelector(propertyExpression) | |
285 { | |
286 let regexpString; | |
287 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | |
288 propertyExpression[propertyExpression.length - 1] == "/") | |
289 { | |
290 regexpString = propertyExpression.slice(1, -1) | |
291 .replace("\\x7B ", "{").replace("\\x7D ", "}"); | |
292 } | |
293 else | |
294 regexpString = filterToRegExp(propertyExpression); | |
295 | |
296 this._regexp = new RegExp(regexpString, "i"); | |
297 } | |
298 | |
299 PropsSelector.prototype = { | |
300 *findPropsSelectors(styles, prefix, regexp) | |
301 { | |
302 for (let style of styles) | |
303 if (regexp.test(style.style)) | |
304 for (let subSelector of style.subSelectors) | |
305 yield prefix + subSelector; | |
306 }, | |
307 | |
308 *getSelectors(prefix, subtree, styles) | |
309 { | |
310 for (let selector of this.findPropsSelectors(styles, prefix, this._regexp)) | |
311 yield [selector, subtree]; | |
312 } | |
313 }; | |
314 | |
315 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc, | |
316 hideElemsFunc) | |
63 { | 317 { |
64 this.window = window; | 318 this.window = window; |
65 this.getFiltersFunc = getFiltersFunc; | 319 this.getFiltersFunc = getFiltersFunc; |
66 this.addSelectorsFunc = addSelectorsFunc; | 320 this.addSelectorsFunc = addSelectorsFunc; |
321 this.hideElemsFunc = hideElemsFunc; | |
67 } | 322 } |
68 | 323 |
69 ElemHideEmulation.prototype = { | 324 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) | 325 isSameOrigin(stylesheet) |
86 { | 326 { |
87 try | 327 try |
88 { | 328 { |
89 return new URL(stylesheet.href).origin == this.window.location.origin; | 329 return new URL(stylesheet.href).origin == this.window.location.origin; |
90 } | 330 } |
91 catch (e) | 331 catch (e) |
92 { | 332 { |
93 // Invalid URL, assume that it is first-party. | 333 // Invalid URL, assume that it is first-party. |
94 return true; | 334 return true; |
95 } | 335 } |
96 }, | 336 }, |
97 | 337 |
98 findSelectors(stylesheet, selectors, filters) | |
99 { | |
100 // Explicitly ignore third-party stylesheets to ensure consistent behavior | |
101 // between Firefox and Chrome. | |
102 if (!this.isSameOrigin(stylesheet)) | |
103 return; | |
104 | |
105 let rules = stylesheet.cssRules; | |
106 if (!rules) | |
107 return; | |
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 { | |
117 if (pattern.regexp.test(style)) | |
118 { | |
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 } | |
127 } | |
128 }, | |
129 | |
130 addSelectors(stylesheets) | 338 addSelectors(stylesheets) |
131 { | 339 { |
132 let selectors = []; | 340 let selectors = []; |
133 let filters = []; | 341 let filters = []; |
342 | |
343 let hideElements = []; | |
344 let filterElements = []; | |
Wladimir Palant
2017/06/07 08:32:59
This array contains filters, not elements. How abo
hub
2017/06/07 14:15:08
make sense. done.
| |
345 | |
346 let cssStyles = []; | |
347 | |
134 for (let stylesheet of stylesheets) | 348 for (let stylesheet of stylesheets) |
135 this.findSelectors(stylesheet, selectors, filters); | 349 { |
350 // Explicitly ignore third-party stylesheets to ensure consistent behavior | |
351 // between Firefox and Chrome. | |
352 if (!this.isSameOrigin(stylesheet)) | |
353 continue; | |
354 | |
355 let rules = stylesheet.cssRules; | |
356 if (!rules) | |
357 continue; | |
358 | |
359 for (let rule of rules) | |
360 { | |
361 if (rule.type != rule.STYLE_RULE) | |
362 continue; | |
363 | |
364 let style = stringifyStyle(rule.style); | |
365 let subSelectors = splitSelector(rule.selectorText); | |
366 cssStyles.push({style, subSelectors}); | |
367 } | |
368 } | |
369 | |
370 for (let pattern of this.patterns) | |
371 for (let selector of evaluate(pattern.selectors, | |
372 0, "", document, cssStyles)) | |
373 if (!pattern.hide) | |
374 { | |
375 selectors.push(selector); | |
376 filters.push(pattern.text); | |
377 } | |
378 else | |
379 for (let element of document.querySelectorAll(selector)) | |
Wladimir Palant
2017/06/07 08:32:59
Nit: we require brackets around multi-line blocks,
hub
2017/06/07 14:15:09
Done.
| |
380 { | |
381 hideElements.push(element); | |
382 filterElements.push(pattern.text); | |
383 } | |
384 | |
136 this.addSelectorsFunc(selectors, filters); | 385 this.addSelectorsFunc(selectors, filters); |
386 this.hideElemsFunc(hideElements, filterElements); | |
137 }, | 387 }, |
138 | 388 |
139 onLoad(event) | 389 onLoad(event) |
140 { | 390 { |
141 let stylesheet = event.target.sheet; | 391 let stylesheet = event.target.sheet; |
142 if (stylesheet) | 392 if (stylesheet) |
143 this.addSelectors([stylesheet]); | 393 this.addSelectors([stylesheet]); |
144 }, | 394 }, |
145 | 395 |
146 apply() | 396 apply() |
147 { | 397 { |
148 this.getFiltersFunc(patterns => | 398 this.getFiltersFunc(patterns => |
149 { | 399 { |
150 this.patterns = []; | 400 this.patterns = []; |
151 for (let pattern of patterns) | 401 for (let pattern of patterns) |
152 { | 402 { |
153 let match = abpSelectorRegexp.exec(pattern.selector); | 403 let {selectors, hide} = parseSelector(pattern.selector); |
154 if (!match || match[1] != "properties") | 404 if (selectors != null && selectors.length > 0) |
155 { | 405 this.patterns.push({selectors, hide, 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 } | 406 } |
219 | 407 |
220 if (this.patterns.length > 0) | 408 if (this.patterns.length > 0) |
221 { | 409 { |
222 let {document} = this.window; | 410 let {document} = this.window; |
223 this.addSelectors(document.styleSheets); | 411 this.addSelectors(document.styleSheets); |
224 document.addEventListener("load", this.onLoad.bind(this), true); | 412 document.addEventListener("load", this.onLoad.bind(this), true); |
225 } | 413 } |
226 }); | 414 }); |
227 } | 415 } |
228 }; | 416 }; |
OLD | NEW |