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

Side by Side Diff: lib/abp2blocklist.js

Issue 29443587: Noissue - Do not add subdomain wildcard if subdomain excluded (Closed) Base URL: https://hg.adblockplus.org/abp2blocklist
Patch Set: Rebase Created May 23, 2017, 5:03 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 | test/abp2blocklist.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-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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 function escapeRegExp(s) 57 function escapeRegExp(s)
58 { 58 {
59 return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); 59 return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
60 } 60 }
61 61
62 function matchDomain(domain) 62 function matchDomain(domain)
63 { 63 {
64 return "^https?://([^/:]*\\.)?" + escapeRegExp(domain).toLowerCase() + "[/:]"; 64 return "^https?://([^/:]*\\.)?" + escapeRegExp(domain).toLowerCase() + "[/:]";
65 } 65 }
66 66
67 function findSubdomainsInList(domain, list)
68 {
69 let subdomains = [];
70 let suffixLength = domain.length + 1;
71
72 for (let name of list)
73 {
74 if (name.length > suffixLength && name.slice(-suffixLength) == "." + domain)
75 subdomains.push(name.slice(0, -suffixLength));
76 }
77
78 return subdomains;
79 }
80
67 function convertElemHideFilter(filter, elemhideSelectorExceptions) 81 function convertElemHideFilter(filter, elemhideSelectorExceptions)
68 { 82 {
69 let included = []; 83 let included = [];
70 let excluded = []; 84 let excluded = [];
71 let rules = []; 85 let rules = [];
72 86
73 parseDomains(filter.domains, included, excluded); 87 parseDomains(filter.domains, included, excluded);
74 88
75 if (excluded.length == 0 && !(filter.selector in elemhideSelectorExceptions)) 89 if (excluded.length == 0 && !(filter.selector in elemhideSelectorExceptions))
76 return {matchDomains: included.map(matchDomain), selector: filter.selector}; 90 return {matchDomains: included.map(matchDomain), selector: filter.selector};
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 trigger["resource-type"] = getResourceTypes(filter); 275 trigger["resource-type"] = getResourceTypes(filter);
262 276
263 if (trigger["resource-type"].length == 0) 277 if (trigger["resource-type"].length == 0)
264 return; 278 return;
265 } 279 }
266 280
267 if (filter.thirdParty != null) 281 if (filter.thirdParty != null)
268 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; 282 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"];
269 283
270 if (included.length > 0) 284 if (included.length > 0)
271 trigger["if-domain"] = included.map(name => "*" + name); 285 {
286 trigger["if-domain"] = [];
287
288 for (let name of included)
289 {
290 // If this is a blocking filter or an element hiding filter, add the
291 // subdomain wildcard only if no subdomains have been excluded.
292 let notSubdomains = null;
293 if ((filter instanceof filterClasses.BlockingFilter ||
294 filter instanceof filterClasses.ElemHideFilter) &&
295 (notSubdomains = findSubdomainsInList(name, excluded)).length > 0)
296 {
297 trigger["if-domain"].push(name);
298
299 // Add the "www" prefix but only if it hasn't been excluded.
300 if (!notSubdomains.includes("www"))
301 trigger["if-domain"].push("www." + name);
302 }
303 else
304 {
305 trigger["if-domain"].push("*" + name);
306 }
307 }
308 }
272 else if (excluded.length > 0) 309 else if (excluded.length > 0)
310 {
273 trigger["unless-domain"] = excluded.map(name => "*" + name); 311 trigger["unless-domain"] = excluded.map(name => "*" + name);
312 }
274 313
275 rules.push({trigger: trigger, action: {type: action}}); 314 rules.push({trigger: trigger, action: {type: action}});
276 } 315 }
277 316
278 function hasNonASCI(obj) 317 function hasNonASCI(obj)
279 { 318 {
280 if (typeof obj == "string") 319 if (typeof obj == "string")
281 { 320 {
282 if (/[^\x00-\x7F]/.test(obj)) 321 if (/[^\x00-\x7F]/.test(obj))
283 return true; 322 return true;
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
472 511
473 for (let filter of this.elemhideExceptions) 512 for (let filter of this.elemhideExceptions)
474 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); 513 convertFilterAddRules(rules, filter, "ignore-previous-rules", false);
475 for (let filter of this.requestFilters) 514 for (let filter of this.requestFilters)
476 convertFilterAddRules(rules, filter, "block", true); 515 convertFilterAddRules(rules, filter, "block", true);
477 for (let filter of this.requestExceptions) 516 for (let filter of this.requestExceptions)
478 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); 517 convertFilterAddRules(rules, filter, "ignore-previous-rules", true);
479 518
480 return rules.filter(rule => !hasNonASCI(rule)); 519 return rules.filter(rule => !hasNonASCI(rule));
481 }; 520 };
OLDNEW
« no previous file with comments | « no previous file | test/abp2blocklist.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld