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

Side by Side Diff: lib/filterClasses.js

Issue 30002580: Noissue - Abstract hostname normalization (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Feb. 8, 2019, 10:26 a.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 | « lib/elemHide.js ('k') | lib/matcher.js » ('j') | 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-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
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 "use strict"; 18 "use strict";
19 19
20 /** 20 /**
21 * @fileOverview Definition of Filter class and its subclasses. 21 * @fileOverview Definition of Filter class and its subclasses.
22 */ 22 */
23 23
24 const {extend} = require("./coreUtils"); 24 const {extend} = require("./coreUtils");
25 const {filterToRegExp} = require("./common"); 25 const {filterToRegExp} = require("./common");
26 const {suffixes} = require("./domain"); 26 const {normalizeHostname, suffixes} = require("./domain");
27 const {filterNotifier} = require("./filterNotifier"); 27 const {filterNotifier} = require("./filterNotifier");
28 28
29 const resources = require("../data/resources.json"); 29 const resources = require("../data/resources.json");
30 30
31 /** 31 /**
32 * Map of internal resources for URL rewriting. 32 * Map of internal resources for URL rewriting.
33 * @type {Map.<string,string>} 33 * @type {Map.<string,string>}
34 */ 34 */
35 let resourceMap = new Map( 35 let resourceMap = new Map(
36 Object.keys(resources).map(key => [key, resources[key]]) 36 Object.keys(resources).map(key => [key, resources[key]])
(...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 599
600 // If no domains are set the rule matches everywhere 600 // If no domains are set the rule matches everywhere
601 if (!domains) 601 if (!domains)
602 return true; 602 return true;
603 603
604 // If the document has no host name, match only if the filter 604 // If the document has no host name, match only if the filter
605 // isn't restricted to specific domains 605 // isn't restricted to specific domains
606 if (!docDomain) 606 if (!docDomain)
607 return domains.get(""); 607 return domains.get("");
608 608
609 if (docDomain[docDomain.length - 1] == ".") 609 for (docDomain of suffixes(normalizeHostname(docDomain)))
610 docDomain = docDomain.replace(/\.+$/, "");
611
612 docDomain = docDomain.toLowerCase();
613
614 for (docDomain of suffixes(docDomain))
615 { 610 {
616 let isDomainIncluded = domains.get(docDomain); 611 let isDomainIncluded = domains.get(docDomain);
617 if (typeof isDomainIncluded != "undefined") 612 if (typeof isDomainIncluded != "undefined")
618 return isDomainIncluded; 613 return isDomainIncluded;
619 } 614 }
620 615
621 return domains.get(""); 616 return domains.get("");
622 }, 617 },
623 618
624 /** 619 /**
625 * Checks whether this filter is active only on a domain and its subdomains. 620 * Checks whether this filter is active only on a domain and its subdomains.
626 * @param {string} docDomain 621 * @param {string} docDomain
627 * @return {boolean} 622 * @return {boolean}
628 */ 623 */
629 isActiveOnlyOnDomain(docDomain) 624 isActiveOnlyOnDomain(docDomain)
630 { 625 {
631 let {domains} = this; 626 let {domains} = this;
632 627
633 if (!docDomain || !domains || domains.get("")) 628 if (!docDomain || !domains || domains.get(""))
634 return false; 629 return false;
635 630
636 if (docDomain[docDomain.length - 1] == ".") 631 docDomain = normalizeHostname(docDomain);
637 docDomain = docDomain.replace(/\.+$/, "");
638
639 docDomain = docDomain.toLowerCase();
640 632
641 for (let [domain, isIncluded] of domains) 633 for (let [domain, isIncluded] of domains)
642 { 634 {
643 if (isIncluded && domain != docDomain) 635 if (isIncluded && domain != docDomain)
644 { 636 {
645 if (domain.length <= docDomain.length) 637 if (domain.length <= docDomain.length)
646 return false; 638 return false;
647 639
648 if (!domain.endsWith("." + docDomain)) 640 if (!domain.endsWith("." + docDomain))
649 return false; 641 return false;
(...skipping 796 matching lines...) Expand 10 before | Expand all | Expand 10 after
1446 1438
1447 /** 1439 /**
1448 * Script that should be executed 1440 * Script that should be executed
1449 * @type {string} 1441 * @type {string}
1450 */ 1442 */
1451 get script() 1443 get script()
1452 { 1444 {
1453 return this.body; 1445 return this.body;
1454 } 1446 }
1455 }); 1447 });
OLDNEW
« no previous file with comments | « lib/elemHide.js ('k') | lib/matcher.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld