OLD | NEW |
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 Loading... |
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 Loading... |
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.some(name => name == "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 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 | 490 |
452 for (let filter of this.elemhideExceptions) | 491 for (let filter of this.elemhideExceptions) |
453 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); | 492 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); |
454 for (let filter of this.requestFilters) | 493 for (let filter of this.requestFilters) |
455 convertFilterAddRules(rules, filter, "block", true); | 494 convertFilterAddRules(rules, filter, "block", true); |
456 for (let filter of this.requestExceptions) | 495 for (let filter of this.requestExceptions) |
457 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); | 496 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); |
458 | 497 |
459 return rules.filter(rule => !hasNonASCI(rule)); | 498 return rules.filter(rule => !hasNonASCI(rule)); |
460 }; | 499 }; |
OLD | NEW |