Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: chrome/content/elemHideEmulation.js

Issue 29464703: Issue 5313 - Make error reporting more robust. (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: addressed nits Created June 19, 2017, 1:24 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 if (parens == 0) 123 if (parens == 0)
124 break; 124 break;
125 } 125 }
126 } 126 }
127 127
128 if (parens > 0) 128 if (parens > 0)
129 return null; 129 return null;
130 return {text: content.substring(startIndex, i), end: i}; 130 return {text: content.substring(startIndex, i), end: i};
131 } 131 }
132 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 133 /** Stringified style objects
189 * @typedef {Object} StringifiedStyle 134 * @typedef {Object} StringifiedStyle
190 * @property {string} style CSS style represented by a string. 135 * @property {string} style CSS style represented by a string.
191 * @property {string[]} subSelectors selectors the CSS properties apply to. 136 * @property {string[]} subSelectors selectors the CSS properties apply to.
192 */ 137 */
193 138
194 /** 139 /**
195 * Produce a string representation of the stylesheet entry. 140 * Produce a string representation of the stylesheet entry.
196 * @param {CSSStyleRule} rule the CSS style rule. 141 * @param {CSSStyleRule} rule the CSS style rule.
197 * @return {StringifiedStyle} the stringified style. 142 * @return {StringifiedStyle} the stringified style.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 * @param {StringifiedStyle[]} styles the stringified style objects. 184 * @param {StringifiedStyle[]} styles the stringified style objects.
240 */ 185 */
241 *getSelectors(prefix, subtree, styles) 186 *getSelectors(prefix, subtree, styles)
242 { 187 {
243 yield [prefix + this._selector, subtree]; 188 yield [prefix + this._selector, subtree];
244 } 189 }
245 }; 190 };
246 191
247 const incompletePrefixRegexp = /[\s>+~]$/; 192 const incompletePrefixRegexp = /[\s>+~]$/;
248 193
249 function HasSelector(selector) 194 function HasSelector(selectors)
250 { 195 {
251 this._innerSelectors = parseSelector(selector); 196 this._innerSelectors = selectors;
252 } 197 }
253 198
254 HasSelector.prototype = { 199 HasSelector.prototype = {
255 requiresHiding: true, 200 requiresHiding: true,
256 201
257 valid()
258 {
259 return this._innerSelectors != null;
260 },
261
262 *getSelectors(prefix, subtree, styles) 202 *getSelectors(prefix, subtree, styles)
263 { 203 {
264 for (let element of this.getElements(prefix, subtree, styles)) 204 for (let element of this.getElements(prefix, subtree, styles))
265 yield [makeSelector(element, ""), element]; 205 yield [makeSelector(element, ""), element];
266 }, 206 },
267 207
268 /** 208 /**
269 * Generator function returning selected elements. 209 * Generator function returning selected elements.
270 * @param {string} prefix the prefix for the selector. 210 * @param {string} prefix the prefix for the selector.
271 * @param {Node} subtree the subtree we work on. 211 * @param {Node} subtree the subtree we work on.
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 { 277 {
338 return new URL(stylesheet.href).origin == this.window.location.origin; 278 return new URL(stylesheet.href).origin == this.window.location.origin;
339 } 279 }
340 catch (e) 280 catch (e)
341 { 281 {
342 // Invalid URL, assume that it is first-party. 282 // Invalid URL, assume that it is first-party.
343 return true; 283 return true;
344 } 284 }
345 }, 285 },
346 286
287 /** Parse the selector
288 * @param {string} selector the selector to parse
289 * @return {Array} selectors is an array of objects,
290 * or null in case of errors.
291 */
292 parseSelector(selector)
293 {
294 if (selector.length == 0)
295 return [];
296
297 let match = abpSelectorRegexp.exec(selector);
298 if (!match)
299 return [new PlainSelector(selector)];
300
301 let selectors = [];
302 if (match.index > 0)
303 selectors.push(new PlainSelector(selector.substr(0, match.index)));
304
305 let startIndex = match.index + match[0].length;
306 let content = parseSelectorContent(selector, startIndex);
307 if (!content)
308 {
309 this.window.console.error(
310 new SyntaxError("Failed to parse Adblock Plus " +
311 `selector ${selector} ` +
312 "due to unmatched parentheses."));
313 return null;
314 }
315 if (match[1] == "properties")
316 selectors.push(new PropsSelector(content.text));
317 else if (match[1] == "has")
318 {
319 let hasSelectors = this.parseSelector(content.text);
320 if (hasSelectors == null)
321 return null;
322 selectors.push(new HasSelector(hasSelectors));
323 }
324 else
325 {
326 // this is an error, can't parse selector.
327 this.window.console.error(
328 new SyntaxError("Failed to parse Adblock Plus " +
329 `selector ${selector}, invalid ` +
330 `pseudo-class :-abp-${match[1]}().`));
331 return null;
332 }
333
334 let suffix = this.parseSelector(selector.substr(content.end + 1));
335 if (suffix == null)
336 return null;
337
338 selectors.push(...suffix);
339
340 return selectors;
341 },
342
347 addSelectors(stylesheets) 343 addSelectors(stylesheets)
348 { 344 {
349 let selectors = []; 345 let selectors = [];
350 let selectorFilters = []; 346 let selectorFilters = [];
351 347
352 let elements = []; 348 let elements = [];
353 let elementFilters = []; 349 let elementFilters = [];
354 350
355 let cssStyles = []; 351 let cssStyles = [];
356 352
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 { 400 {
405 let stylesheet = event.target.sheet; 401 let stylesheet = event.target.sheet;
406 if (stylesheet) 402 if (stylesheet)
407 this.addSelectors([stylesheet]); 403 this.addSelectors([stylesheet]);
408 }, 404 },
409 405
410 apply() 406 apply()
411 { 407 {
412 this.getFiltersFunc(patterns => 408 this.getFiltersFunc(patterns =>
413 { 409 {
414 let oldReportError = reportError;
415 reportError = error => this.window.console.error(error);
416
417 this.patterns = []; 410 this.patterns = [];
418 for (let pattern of patterns) 411 for (let pattern of patterns)
419 { 412 {
420 let selectors = parseSelector(pattern.selector); 413 let selectors = this.parseSelector(pattern.selector);
421 if (selectors != null && selectors.length > 0) 414 if (selectors != null && selectors.length > 0)
422 this.patterns.push({selectors, text: pattern.text}); 415 this.patterns.push({selectors, text: pattern.text});
423 } 416 }
424 417
425 if (this.patterns.length > 0) 418 if (this.patterns.length > 0)
426 { 419 {
427 let {document} = this.window; 420 let {document} = this.window;
428 this.addSelectors(document.styleSheets); 421 this.addSelectors(document.styleSheets);
429 document.addEventListener("load", this.onLoad.bind(this), true); 422 document.addEventListener("load", this.onLoad.bind(this), true);
430 } 423 }
431 reportError = oldReportError;
432 }); 424 });
433 } 425 }
434 }; 426 };
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld