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

Delta Between Two Patch Sets: background.js

Issue 29674584: Issue 5549 - Implement missing error handlings for custom filter lists (Closed)
Left Patch Set: applied required changes Created Jan. 24, 2018, 4:05 p.m.
Right Patch Set: changed custom-filters-edit-area to custom-filters-control as suggested Created Feb. 5, 2018, 9:05 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « README.md ('k') | desktop-options.html » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 if (parts.length == 2 && parts[0] in data) 64 if (parts.length == 2 && parts[0] in data)
65 data[parts[0]] = decodeURIComponent(parts[1]); 65 data[parts[0]] = decodeURIComponent(parts[1]);
66 } 66 }
67 } 67 }
68 } 68 }
69 69
70 let params = { 70 let params = {
71 blockedURLs: "", 71 blockedURLs: "",
72 filterlistsReinitialized: false, 72 filterlistsReinitialized: false,
73 addSubscription: false, 73 addSubscription: false,
74 filterError: false,
75 downloadStatus: "synchronize_ok", 74 downloadStatus: "synchronize_ok",
76 showNotificationUI: false, 75 showNotificationUI: false,
77 showPageOptions: false 76 showPageOptions: false
78 }; 77 };
79 updateFromURL(params); 78 updateFromURL(params);
80 79
81 let modules = {}; 80 let modules = {};
82 window.require = function(module) 81 window.require = function(module)
83 { 82 {
84 return modules[module]; 83 return modules[module];
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 } 305 }
307 } 306 }
308 } 307 }
309 } 308 }
310 }; 309 };
311 310
312 function Filter(text) 311 function Filter(text)
313 { 312 {
314 this.text = text; 313 this.text = text;
315 this.disabled = false; 314 this.disabled = false;
316 } 315 if (Filter.elemhideRegExp.test(text))
316 this.selector = RegExp.$3;
317 }
318 Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/;
317 Filter.fromText = (text) => new Filter(text); 319 Filter.fromText = (text) => new Filter(text);
318 320
319 function BlockingFilter() 321 function BlockingFilter()
320 { 322 {
321 } 323 }
322 324
323 function RegExpFilter() 325 function RegExpFilter()
324 { 326 {
325 } 327 }
326 RegExpFilter.typeMap = Object.create(null); 328 RegExpFilter.typeMap = Object.create(null);
327 329
328 modules.filterClasses = { 330 modules.filterClasses = {
329 BlockingFilter, 331 BlockingFilter,
330 Filter, 332 Filter,
331 RegExpFilter 333 RegExpFilter
332 }; 334 };
333 335
336 const isValidCSSSelector = selector =>
337 {
338 if (!selector)
339 return true;
340 try
341 {
342 document.documentElement.matches(selector);
343 return true;
344 }
345 catch (error)
346 {
347 return false;
348 }
349 };
350
334 modules.filterValidation = 351 modules.filterValidation =
335 { 352 {
353 // to test failing filters
354 // use one or more bad CSS selectors
355 // or start the line with a [
336 parseFilter(text) 356 parseFilter(text)
337 { 357 {
338 if (params.filterError) 358 let filter = null;
339 return {error: "Invalid filter"}; 359 if (text)
340 return {filter: modules.filterClasses.Filter.fromText(text)}; 360 {
361 if (text[0] == "[")
362 return {error: {reason: "Unexpected filter list header"}};
363
364 filter = modules.filterClasses.Filter.fromText(text);
365
366 if (!isValidCSSSelector(filter.selector))
367 {
368 return {error: {reason: "Invalid CSS selector"}};
369 }
370 }
371
372 return {filter};
341 }, 373 },
342 parseFilters(text) 374 parseFilters(text)
343 { 375 {
344 if (params.filterError) 376 let lines = text.split("\n");
345 return {errors: ["Invalid filter"]}; 377 let filters = [];
346 return { 378 let errors = [];
347 filters: text.split("\n") 379
348 .filter((filter) => !!filter) 380 for (let i = 0; i < lines.length; i++)
349 .map(modules.filterClasses.Filter.fromText), 381 {
350 errors: [] 382 let {filter, error} = this.parseFilter(lines[i]);
351 }; 383
384 if (filter)
385 filters.push(filter);
386
387 if (error)
388 {
389 error.lineno = i + 1;
390 errors.push(error);
391 }
392 }
393
394 return {filters, errors};
352 } 395 }
353 }; 396 };
354 397
355 modules.synchronizer = { 398 modules.synchronizer = {
356 Synchronizer: { 399 Synchronizer: {
357 _downloading: false, 400 _downloading: false,
358 execute(subscription, manual) 401 execute(subscription, manual)
359 { 402 {
360 modules.synchronizer.Synchronizer._downloading = true; 403 modules.synchronizer.Synchronizer._downloading = true;
361 modules.filterNotifier.FilterNotifier.emit( 404 modules.filterNotifier.FilterNotifier.emit(
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 }, 655 },
613 filter: { 656 filter: {
614 text: "||example.com/some-annoying-popup$popup", 657 text: "||example.com/some-annoying-popup$popup",
615 whitelisted: false, 658 whitelisted: false,
616 userDefined: true, 659 userDefined: true,
617 subscription: null 660 subscription: null
618 } 661 }
619 }); 662 });
620 }); 663 });
621 }()); 664 }());
LEFTRIGHT

Powered by Google App Engine
This is Rietveld