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; | 22 const abpSelectorRegexp = /:-abp-([\w-]+)\(/i; |
23 | 23 |
| 24 let reportError = () => {}; |
| 25 |
24 function splitSelector(selector) | 26 function splitSelector(selector) |
25 { | 27 { |
26 if (selector.indexOf(",") == -1) | 28 if (selector.indexOf(",") == -1) |
27 return [selector]; | 29 return [selector]; |
28 | 30 |
29 let selectors = []; | 31 let selectors = []; |
30 let start = 0; | 32 let start = 0; |
31 let level = 0; | 33 let level = 0; |
32 let sep = ""; | 34 let sep = ""; |
33 | 35 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 return [new PlainSelector(selector)]; | 146 return [new PlainSelector(selector)]; |
145 | 147 |
146 let selectors = []; | 148 let selectors = []; |
147 if (match.index > 0) | 149 if (match.index > 0) |
148 selectors.push(new PlainSelector(selector.substr(0, match.index))); | 150 selectors.push(new PlainSelector(selector.substr(0, match.index))); |
149 | 151 |
150 let startIndex = match.index + match[0].length; | 152 let startIndex = match.index + match[0].length; |
151 let content = parseSelectorContent(selector, startIndex); | 153 let content = parseSelectorContent(selector, startIndex); |
152 if (!content) | 154 if (!content) |
153 { | 155 { |
154 console.error(new SyntaxError("Failed parsing content filter " + | 156 reportError(new SyntaxError("Failed to parse Adblock Plus " + |
155 `selector ${selector}, didn't ` + | 157 `selector ${selector}, ` + |
156 "find closing parenthesis.")); | 158 "due to unmatched parentheses.")); |
157 return null; | 159 return null; |
158 } | 160 } |
159 if (match[1] == "properties") | 161 if (match[1] == "properties") |
160 selectors.push(new PropsSelector(content.text)); | 162 selectors.push(new PropsSelector(content.text)); |
161 else if (match[1] == "has") | 163 else if (match[1] == "has") |
162 { | 164 { |
163 let hasSelector = new HasSelector(content.text); | 165 let hasSelector = new HasSelector(content.text); |
164 if (!hasSelector.valid()) | 166 if (!hasSelector.valid()) |
165 return null; | 167 return null; |
166 selectors.push(hasSelector); | 168 selectors.push(hasSelector); |
167 } | 169 } |
168 else | 170 else |
169 { | 171 { |
170 // this is an error, can't parse selector. | 172 // this is an error, can't parse selector. |
171 console.error(new SyntaxError("Failed parsing content filter " + | 173 reportError(new SyntaxError("Failed to parse Adblock Plus " + |
172 `selector ${selector}, invalid ` + | 174 `selector ${selector}, invalid ` + |
173 `pseudo-class -abp-${match[1]}().`)); | 175 `pseudo-class :-abp-${match[1]}().`)); |
174 return null; | 176 return null; |
175 } | 177 } |
176 | 178 |
177 let suffix = parseSelector(selector.substr(content.end + 1)); | 179 let suffix = parseSelector(selector.substr(content.end + 1)); |
178 if (suffix == null) | 180 if (suffix == null) |
179 return null; | 181 return null; |
180 | 182 |
181 selectors.push(...suffix); | 183 selectors.push(...suffix); |
182 | 184 |
183 return selectors; | 185 return selectors; |
184 } | 186 } |
185 | 187 |
186 /** Stringified style objects | 188 /** Stringified style objects |
187 * @typedef {Object} StringifiedStyle | 189 * @typedef {Object} StringifiedStyle |
188 * @property {string} style CSS style represented by a string. | 190 * @property {string} style CSS style represented by a string. |
189 * @property {string[]} subSelectors selectors the CSS properties apply to. | 191 * @property {string[]} subSelectors selectors the CSS properties apply to. |
190 */ | 192 */ |
191 | 193 |
192 /** | 194 /** |
193 * Stringify a CSS style rule object. | 195 * Produce a string representation of the stylesheet entry. |
194 * @param {CSSStyleRule} rule the CSS style rule. | 196 * @param {CSSStyleRule} rule the CSS style rule. |
195 * @return {StringifiedStyle} the stringified style. | 197 * @return {StringifiedStyle} the stringified style. |
196 */ | 198 */ |
197 function stringifyStyle(rule) | 199 function stringifyStyle(rule) |
198 { | 200 { |
199 let styles = []; | 201 let styles = []; |
200 for (let i = 0; i < rule.style.length; i++) | 202 for (let i = 0; i < rule.style.length; i++) |
201 { | 203 { |
202 let property = rule.style.item(i); | 204 let property = rule.style.item(i); |
203 let value = rule.style.getPropertyValue(property); | 205 let value = rule.style.getPropertyValue(property); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 | 367 |
366 for (let rule of rules) | 368 for (let rule of rules) |
367 { | 369 { |
368 if (rule.type != rule.STYLE_RULE) | 370 if (rule.type != rule.STYLE_RULE) |
369 continue; | 371 continue; |
370 | 372 |
371 cssStyles.push(stringifyStyle(rule)); | 373 cssStyles.push(stringifyStyle(rule)); |
372 } | 374 } |
373 } | 375 } |
374 | 376 |
| 377 let {document} = this.window; |
375 for (let pattern of this.patterns) | 378 for (let pattern of this.patterns) |
376 { | 379 { |
377 for (let selector of evaluate(pattern.selectors, | 380 for (let selector of evaluate(pattern.selectors, |
378 0, "", document, cssStyles)) | 381 0, "", document, cssStyles)) |
379 { | 382 { |
380 if (!pattern.selectors.some(s => s.requiresHiding)) | 383 if (!pattern.selectors.some(s => s.requiresHiding)) |
381 { | 384 { |
382 selectors.push(selector); | 385 selectors.push(selector); |
383 selectorFilters.push(pattern.text); | 386 selectorFilters.push(pattern.text); |
384 } | 387 } |
(...skipping 16 matching lines...) Expand all Loading... |
401 { | 404 { |
402 let stylesheet = event.target.sheet; | 405 let stylesheet = event.target.sheet; |
403 if (stylesheet) | 406 if (stylesheet) |
404 this.addSelectors([stylesheet]); | 407 this.addSelectors([stylesheet]); |
405 }, | 408 }, |
406 | 409 |
407 apply() | 410 apply() |
408 { | 411 { |
409 this.getFiltersFunc(patterns => | 412 this.getFiltersFunc(patterns => |
410 { | 413 { |
| 414 let oldReportError = reportError; |
| 415 reportError = error => this.window.console.error(error); |
| 416 |
411 this.patterns = []; | 417 this.patterns = []; |
412 for (let pattern of patterns) | 418 for (let pattern of patterns) |
413 { | 419 { |
414 let selectors = parseSelector(pattern.selector); | 420 let selectors = parseSelector(pattern.selector); |
415 if (selectors != null && selectors.length > 0) | 421 if (selectors != null && selectors.length > 0) |
416 this.patterns.push({selectors, text: pattern.text}); | 422 this.patterns.push({selectors, text: pattern.text}); |
417 } | 423 } |
418 | 424 |
419 if (this.patterns.length > 0) | 425 if (this.patterns.length > 0) |
420 { | 426 { |
421 let {document} = this.window; | 427 let {document} = this.window; |
422 this.addSelectors(document.styleSheets); | 428 this.addSelectors(document.styleSheets); |
423 document.addEventListener("load", this.onLoad.bind(this), true); | 429 document.addEventListener("load", this.onLoad.bind(this), true); |
424 } | 430 } |
| 431 reportError = oldReportError; |
425 }); | 432 }); |
426 } | 433 } |
427 }; | 434 }; |
LEFT | RIGHT |