| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 let included = []; | 58 let included = []; |
| 59 let excluded = []; | 59 let excluded = []; |
| 60 let rules = []; | 60 let rules = []; |
| 61 | 61 |
| 62 parseDomains(filter.domains, included, excluded); | 62 parseDomains(filter.domains, included, excluded); |
| 63 | 63 |
| 64 if (excluded.length == 0 && !(filter.selector in elemhideSelectorExceptions)) | 64 if (excluded.length == 0 && !(filter.selector in elemhideSelectorExceptions)) |
| 65 return {matchDomains: included.map(matchDomain), selector: filter.selector}; | 65 return {matchDomains: included.map(matchDomain), selector: filter.selector}; |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Convert the "regexpSource" part of a filter's text to a regular expression, | 68 /** |
| 69 // also deciding if the expression can safely be converted to and matched as | 69 * Convert the given filter "regexpSource" string into a regular expression. |
| 70 // lowercase. | 70 * (Also deciding if the regular expression can be safely converted to and |
| 71 * matched as lower case or not.) | |
| 72 * | |
| 73 * @param {string} text regexpSource property of a filter | |
| 74 * @returns {object} An object containing a regular expression string and a bool | |
| 75 * indicating if the filter can be safely matched as lower | |
| 76 * case: {regexp: "...", caseSenstive: true/false} | |
| 77 */ | |
| 71 function toRegExp(text) | 78 function toRegExp(text) |
| 72 { | 79 { |
| 73 let result = []; | 80 let result = []; |
| 74 let lastIndex = text.length - 1; | 81 let lastIndex = text.length - 1; |
| 75 let containsHostname = false; | 82 let hostnameStarted = false; |
| 83 let hostnameFinished = false; | |
| 76 let caseSensitive = false; | 84 let caseSensitive = false; |
| 77 | 85 |
| 78 for (let i = 0; i < text.length; i++) | 86 for (let i = 0; i < text.length; i++) |
| 79 { | 87 { |
| 80 let c = text[i]; | 88 let c = text[i]; |
| 81 | 89 |
| 82 switch (c) | 90 switch (c) |
| 83 { | 91 { |
| 84 case "*": | 92 case "*": |
| 93 if (hostnameStarted) | |
| 94 hostnameFinished = true; | |
| 85 if (result.length > 0 && i < lastIndex && text[i + 1] != "*") | 95 if (result.length > 0 && i < lastIndex && text[i + 1] != "*") |
| 86 result.push(".*"); | 96 result.push(".*"); |
| 87 break; | 97 break; |
| 88 case "^": | 98 case "^": |
| 99 if (hostnameStarted) | |
| 100 hostnameFinished = true; | |
| 89 if (i < lastIndex) | 101 if (i < lastIndex) |
| 90 result.push("."); | 102 result.push("."); |
| 91 break; | 103 break; |
| 92 case "|": | 104 case "|": |
| 93 if (i == 0) | 105 if (i == 0) |
| 94 { | 106 { |
| 95 result.push("^"); | 107 result.push("^"); |
| 96 break; | 108 break; |
| 97 } | 109 } |
| 98 if (i == lastIndex) | 110 if (i == lastIndex) |
| 99 { | 111 { |
| 100 result.push("$"); | 112 result.push("$"); |
| 101 break; | 113 break; |
| 102 } | 114 } |
| 103 if (i == 1 && text[0] == "|") | 115 if (i == 1 && text[0] == "|") |
| 104 { | 116 { |
| 117 hostnameStarted = caseSensitive = true; | |
| 105 result.push("https?://"); | 118 result.push("https?://"); |
| 106 containsHostname = caseSensitive = true; | |
| 107 break; | 119 break; |
| 108 } | 120 } |
| 109 result.push("\\", c); | 121 result.push("\\", c); |
| 110 break; | 122 break; |
| 111 case "/": case "?": case "*": case "^": | 123 case "?": |
| 112 if (containsHostname && i < lastIndex) | 124 if (hostnameStarted) |
|
Sebastian Noack
2016/02/24 20:31:05
I think caseSensitive shouldn't be reset to false
kzar
2016/02/24 21:20:47
Done.
| |
| 113 caseSensitive = false; | 125 hostnameFinished = true; |
| 114 else if (!containsHostname && | 126 case ".": case "+": case "$": case "{": case "}": |
| 115 i >= 2 && text[i-2] == ":" && text[i-1] == "/" && c == "/") | 127 case "(": case ")": case "[": case "]": case "\\": |
|
Sebastian Noack
2016/02/24 20:31:05
I think it would make more sense to handle "/" sep
kzar
2016/02/24 21:20:47
Done.
| |
| 116 containsHostname = caseSensitive = true; | |
| 117 if (c != "?") | |
| 118 { | |
| 119 result.push(c); | |
| 120 break; | |
| 121 } | |
| 122 case ".": case "+": case "$": case "{": | |
| 123 case "}": case "(": case ")": case "[": | |
| 124 case "]": case "\\": | |
| 125 result.push("\\", c); | 128 result.push("\\", c); |
| 126 break; | 129 break; |
| 130 case "/": | |
| 131 if (hostnameStarted) | |
| 132 hostnameFinished = true; | |
| 133 else if (text.charAt(i-2) == ":" && text.charAt(i-1) == "/") | |
| 134 hostnameStarted = caseSensitive = true; | |
| 127 default: | 135 default: |
| 136 if (hostnameFinished && (c >= "a" && c <= "z" || | |
| 137 c >= "A" && c <= "Z")) | |
| 138 caseSensitive = false; | |
| 128 result.push(c); | 139 result.push(c); |
| 129 } | 140 } |
| 130 } | 141 } |
| 131 | 142 |
| 132 return {regexp: result.join(""), caseSensitive: caseSensitive}; | 143 return {regexp: result.join(""), caseSensitive: caseSensitive}; |
| 133 } | 144 } |
| 134 | 145 |
| 135 function getRegExpTrigger(filter) | 146 function getRegExpTrigger(filter) |
| 136 { | 147 { |
| 137 let result = toRegExp(filter.regexpSource.replace( | 148 let result = toRegExp(filter.regexpSource.replace( |
| 138 // Safari expects punycode, filter lists use unicode | 149 // Safari expects punycode, filter lists use unicode |
| 139 /^(\|\||\|?https?:\/\/)([\w\-.*\u0080-\uFFFF]+)/i, | 150 /^(\|\||\|?https?:\/\/)([\w\-.*\u0080-\uFFFF]+)/i, |
| 140 function (match, prefix, domain) | 151 function (match, prefix, domain) |
| 141 { | 152 { |
| 142 return prefix + punycode.toASCII(domain); | 153 return prefix + punycode.toASCII(domain); |
| 143 } | 154 } |
| 144 )); | 155 )); |
| 145 | 156 |
| 146 let trigger = {"url-filter": result.regexp}; | 157 let trigger = {"url-filter": result.regexp}; |
| 147 | 158 |
| 148 // Limit rules to to HTTP(S) URLs | 159 // Limit rules to to HTTP(S) URLs |
| 149 if (!/^(\^|http)/i.test(trigger["url-filter"])) | 160 if (!/^(\^|http)/i.test(trigger["url-filter"])) |
| 150 trigger["url-filter"] = "^https?://.*" + trigger["url-filter"]; | 161 trigger["url-filter"] = "^https?://.*" + trigger["url-filter"]; |
| 151 | 162 |
| 152 // For rules containing only a hostname we know that we're matching against | 163 // For rules containing only a hostname we know that we're matching against |
| 153 // a lowercase string and can therefore enable case sensitive matching. | 164 // a lowercase string unless the matchCase option was passed. |
| 154 if (result.caseSensitive) | 165 if (result.caseSensitive && !filter.matchCase) |
| 155 { | |
| 156 trigger["url-filter"] = trigger["url-filter"].toLowerCase(); | 166 trigger["url-filter"] = trigger["url-filter"].toLowerCase(); |
| 167 | |
| 168 if (result.caseSensitive || filter.matchCase) | |
| 157 trigger["url-filter-is-case-sensitive"] = true; | 169 trigger["url-filter-is-case-sensitive"] = true; |
| 158 } | |
| 159 | 170 |
| 160 return trigger; | 171 return trigger; |
| 161 } | 172 } |
| 162 | 173 |
| 163 function getResourceTypes(filter) | 174 function getResourceTypes(filter) |
| 164 { | 175 { |
| 165 let types = []; | 176 let types = []; |
| 166 | 177 |
| 167 if (filter.contentType & typeMap.IMAGE) | 178 if (filter.contentType & typeMap.IMAGE) |
| 168 types.push("image"); | 179 types.push("image"); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 | 212 |
| 202 return result; | 213 return result; |
| 203 } | 214 } |
| 204 | 215 |
| 205 function convertFilter(filter, action, withResourceTypes) | 216 function convertFilter(filter, action, withResourceTypes) |
| 206 { | 217 { |
| 207 let trigger = getRegExpTrigger(filter); | 218 let trigger = getRegExpTrigger(filter); |
| 208 let included = []; | 219 let included = []; |
| 209 let excluded = []; | 220 let excluded = []; |
| 210 | 221 |
| 211 if (filter.matchCase) | 222 parseDomains(filter.domains, included, excluded); |
| 212 trigger["url-filter-is-case-sensitive"] = true; | |
| 213 | |
| 214 parseDomains(filter.domains, included, excluded); | |
| 215 | 223 |
| 216 if (withResourceTypes) | 224 if (withResourceTypes) |
| 217 trigger["resource-type"] = getResourceTypes(filter); | 225 trigger["resource-type"] = getResourceTypes(filter); |
| 218 if (filter.thirdParty != null) | 226 if (filter.thirdParty != null) |
| 219 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; | 227 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; |
| 220 | 228 |
| 221 if (included.length > 0) | 229 if (included.length > 0) |
| 222 trigger["if-domain"] = addDomainPrefix(included); | 230 trigger["if-domain"] = addDomainPrefix(included); |
| 223 else if (excluded.length > 0) | 231 else if (excluded.length > 0) |
| 224 trigger["unless-domain"] = addDomainPrefix(excluded); | 232 trigger["unless-domain"] = addDomainPrefix(excluded); |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 419 | 427 |
| 420 for (let filter of this.elemhideExceptions) | 428 for (let filter of this.elemhideExceptions) |
| 421 addRule(convertFilter(filter, "ignore-previous-rules", false)); | 429 addRule(convertFilter(filter, "ignore-previous-rules", false)); |
| 422 for (let filter of this.requestFilters) | 430 for (let filter of this.requestFilters) |
| 423 addRule(convertFilter(filter, "block", true)); | 431 addRule(convertFilter(filter, "block", true)); |
| 424 for (let filter of this.requestExceptions) | 432 for (let filter of this.requestExceptions) |
| 425 addRule(convertFilter(filter, "ignore-previous-rules", true)); | 433 addRule(convertFilter(filter, "ignore-previous-rules", true)); |
| 426 | 434 |
| 427 return rules; | 435 return rules; |
| 428 }; | 436 }; |
| LEFT | RIGHT |