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

Delta Between Two Patch Sets: options.js

Issue 5279235799252992: Issue 491 - Validate custom filters (Closed)
Left Patch Set: Created Nov. 18, 2014, 3:57 p.m.
Right Patch Set: Improved test messages Created Nov. 21, 2014, 8:07 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « metadata.common ('k') | qunit/index.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
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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 var backgroundPage = ext.backgroundPage.getWindow(); 18 var backgroundPage = ext.backgroundPage.getWindow();
19 var require = backgroundPage.require; 19 var require = backgroundPage.require;
20 20
21 with(require("filterClasses")) 21 with(require("filterClasses"))
22 { 22 {
23 this.Filter = Filter; 23 this.Filter = Filter;
24 this.WhitelistFilter = WhitelistFilter; 24 this.WhitelistFilter = WhitelistFilter;
25 this.InvalidFilter = InvalidFilter;
26 } 25 }
27 with(require("subscriptionClasses")) 26 with(require("subscriptionClasses"))
28 { 27 {
29 this.Subscription = Subscription; 28 this.Subscription = Subscription;
30 this.SpecialSubscription = SpecialSubscription; 29 this.SpecialSubscription = SpecialSubscription;
31 this.DownloadableSubscription = DownloadableSubscription; 30 this.DownloadableSubscription = DownloadableSubscription;
31 }
32 with(require("filterValidation"))
33 {
34 this.parseFilter = parseFilter;
35 this.parseFilters = parseFilters;
32 } 36 }
33 var FilterStorage = require("filterStorage").FilterStorage; 37 var FilterStorage = require("filterStorage").FilterStorage;
34 var FilterNotifier = require("filterNotifier").FilterNotifier; 38 var FilterNotifier = require("filterNotifier").FilterNotifier;
35 var Prefs = require("prefs").Prefs; 39 var Prefs = require("prefs").Prefs;
36 var Synchronizer = require("synchronizer").Synchronizer; 40 var Synchronizer = require("synchronizer").Synchronizer;
37 var Utils = require("utils").Utils; 41 var Utils = require("utils").Utils;
38 42
39 // Loads options from localStorage and sets UI elements accordingly 43 // Loads options from localStorage and sets UI elements accordingly
40 function loadOptions() 44 function loadOptions()
41 { 45 {
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 470
467 var filterText = "@@||" + domain + "^$document"; 471 var filterText = "@@||" + domain + "^$document";
468 FilterStorage.addFilter(Filter.fromText(filterText)); 472 FilterStorage.addFilter(Filter.fromText(filterText));
469 } 473 }
470 474
471 // Adds filter text that user typed to the selection box 475 // Adds filter text that user typed to the selection box
472 function addTypedFilter(event) 476 function addTypedFilter(event)
473 { 477 {
474 event.preventDefault(); 478 event.preventDefault();
475 479
476 var filterText = Filter.normalize(document.getElementById("newFilter").value); 480 var element = document.getElementById("newFilter");
477 if (filterText) 481 var filter;
478 { 482
kzar 2014/11/18 16:32:10 Seems like the logic here is duplicated from backg
Sebastian Noack 2014/11/18 17:42:55 The only common code is the call to alert(). Note
479 var filter = Filter.fromText(filterText, true) 483 try
480 if (filter instanceof InvalidFilter) 484 {
481 { 485 filter = parseFilter(element.value);
482 alert(filter.reason); 486 }
483 return; 487 catch (error)
kzar 2014/11/18 16:32:10 IMHO it would be more readable if you omitted the
Sebastian Noack 2014/11/18 17:42:55 Then the code below which clears the field would a
484 } 488 {
485 489 alert(error);
490 return;
491 }
492
493 if (filter)
486 FilterStorage.addFilter(filter); 494 FilterStorage.addFilter(filter);
487 } 495
488 496 element.value = "";
489 document.getElementById("newFilter").value = "";
490 } 497 }
491 498
492 // Removes currently selected whitelisted domains 499 // Removes currently selected whitelisted domains
493 function removeSelectedExcludedDomain() 500 function removeSelectedExcludedDomain()
494 { 501 {
495 var excludedDomainsBox = document.getElementById("excludedDomainsBox"); 502 var excludedDomainsBox = document.getElementById("excludedDomainsBox");
496 var remove = []; 503 var remove = [];
497 for (var i = 0; i < excludedDomainsBox.length; i++) 504 for (var i = 0; i < excludedDomainsBox.length; i++)
498 if (excludedDomainsBox.options[i].selected) 505 if (excludedDomainsBox.options[i].selected)
499 remove.push(excludedDomainsBox.options[i].value); 506 remove.push(excludedDomainsBox.options[i].value);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 var text = ""; 538 var text = "";
532 for (var i = 0; i < userFiltersBox.length; i++) 539 for (var i = 0; i < userFiltersBox.length; i++)
533 text += userFiltersBox.options[i].value + "\n"; 540 text += userFiltersBox.options[i].value + "\n";
534 document.getElementById("rawFiltersText").value = text; 541 document.getElementById("rawFiltersText").value = text;
535 } 542 }
536 } 543 }
537 544
538 // Imports filters in the raw text box 545 // Imports filters in the raw text box
539 function importRawFiltersText() 546 function importRawFiltersText()
540 { 547 {
541 var filters = document.getElementById("rawFiltersText").value.split("\n"); 548 var text = document.getElementById("rawFiltersText").value;
549
550 var add;
551 try
552 {
553 add = parseFilters(text, true);
554 }
555 catch (error)
556 {
557 alert(error);
558 return;
559 }
560
542 var seenFilter = {__proto__: null}; 561 var seenFilter = {__proto__: null};
543
544 var add = [];
545 for (var i = 0; i < filters.length; i++)
546 {
547 var text = Filter.normalize(filters[i]);
548 if (!text)
549 continue;
550
551 // Don't import filter list header
552 if (/^\[/.test(text))
553 continue;
Wladimir Palant 2014/11/19 16:03:11 You've removed that logic. People will occasionall
Sebastian Noack 2014/11/20 12:51:19 When I unified the code into a reusable helper fun
554
555 var filter = Filter.fromText(text, true);
kzar 2014/11/18 16:32:10 Again seems like this logic is duplicate of above
556 if (filter instanceof InvalidFilter)
557 {
558 alert(filter.reason);
559 return;
560 }
Wladimir Palant 2014/11/18 16:35:03 It would probably make sense to have a filter vali
561
562 add.push(filter);
563 seenFilter[text] = true;
564 }
565 for (var i = 0; i < add.length; i++) 562 for (var i = 0; i < add.length; i++)
566 FilterStorage.addFilter(add[i]); 563 {
564 var filter = add[i];
565 FilterStorage.addFilter(filter);
566 seenFilter[filter.text] = null;
567 }
567 568
568 var remove = []; 569 var remove = [];
569 for (var i = 0; i < FilterStorage.subscriptions.length; i++) 570 for (var i = 0; i < FilterStorage.subscriptions.length; i++)
570 { 571 {
571 var subscription = FilterStorage.subscriptions[i]; 572 var subscription = FilterStorage.subscriptions[i];
572 if (!(subscription instanceof SpecialSubscription)) 573 if (!(subscription instanceof SpecialSubscription))
573 continue; 574 continue;
574 575
575 for (var j = 0; j < subscription.filters.length; j++) 576 for (var j = 0; j < subscription.filters.length; j++)
576 { 577 {
577 var filter = subscription.filters[j]; 578 var filter = subscription.filters[j];
578 if (filter instanceof WhitelistFilter && /^@@\|\|([^\/:]+)\^\$document$/.t est(filter.text)) 579 if (filter instanceof WhitelistFilter && /^@@\|\|([^\/:]+)\^\$document$/.t est(filter.text))
579 continue; 580 continue;
580 581
581 if (!(filter.text in seenFilter)) 582 if (!(filter.text in seenFilter))
582 remove.push(filter); 583 remove.push(filter);
583 } 584 }
584 } 585 }
586
585 for (var i = 0; i < remove.length; i++) 587 for (var i = 0; i < remove.length; i++)
586 FilterStorage.removeFilter(remove[i]); 588 FilterStorage.removeFilter(remove[i]);
587 589
588 $("#rawFilters").hide(); 590 $("#rawFilters").hide();
589 } 591 }
590 592
591 // Called when user explicitly requests filter list updates 593 // Called when user explicitly requests filter list updates
592 function updateFilterLists() 594 function updateFilterLists()
593 { 595 {
594 for (var i = 0; i < FilterStorage.subscriptions.length; i++) 596 for (var i = 0; i < FilterStorage.subscriptions.length; i++)
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 links[i].href = arguments[i + 1]; 648 links[i].href = arguments[i + 1];
647 links[i].setAttribute("target", "_blank"); 649 links[i].setAttribute("target", "_blank");
648 } 650 }
649 else if (typeof arguments[i + 1] == "function") 651 else if (typeof arguments[i + 1] == "function")
650 { 652 {
651 links[i].href = "javascript:void(0);"; 653 links[i].href = "javascript:void(0);";
652 links[i].addEventListener("click", arguments[i + 1], false); 654 links[i].addEventListener("click", arguments[i + 1], false);
653 } 655 }
654 } 656 }
655 } 657 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld