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) |
113 caseSensitive = false; | 125 hostnameFinished = true; |
114 else if (i >= 2 && text[i-2] == ":" && text[i-1] == "/" && c == "/") | 126 case ".": case "+": case "$": case "{": case "}": |
115 containsHostname = caseSensitive = true; | 127 case "(": case ")": case "[": case "]": case "\\": |
116 if (c != "?") | |
117 { | |
118 result.push(c); | |
119 break; | |
120 } | |
121 case ".": case "+": case "$": case "{": | |
122 case "}": case "(": case ")": case "[": | |
123 case "]": case "\\": | |
124 result.push("\\", c); | 128 result.push("\\", c); |
125 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; |
126 default: | 135 default: |
| 136 if (hostnameFinished && (c >= "a" && c <= "z" || |
| 137 c >= "A" && c <= "Z")) |
| 138 caseSensitive = false; |
127 result.push(c); | 139 result.push(c); |
128 } | 140 } |
129 } | 141 } |
130 | 142 |
131 return {regexp: result.join(""), caseSensitive: caseSensitive}; | 143 return {regexp: result.join(""), caseSensitive: caseSensitive}; |
132 } | 144 } |
133 | 145 |
134 function getRegExpTrigger(filter) | 146 function getRegExpTrigger(filter) |
135 { | 147 { |
136 let result = toRegExp(filter.regexpSource.replace( | 148 let result = toRegExp(filter.regexpSource.replace( |
137 // Safari expects punycode, filter lists use unicode | 149 // Safari expects punycode, filter lists use unicode |
138 /^(\|\||\|?https?:\/\/)([\w\-.*\u0080-\uFFFF]+)/i, | 150 /^(\|\||\|?https?:\/\/)([\w\-.*\u0080-\uFFFF]+)/i, |
139 function (match, prefix, domain) | 151 function (match, prefix, domain) |
140 { | 152 { |
141 return prefix + punycode.toASCII(domain); | 153 return prefix + punycode.toASCII(domain); |
142 } | 154 } |
143 )); | 155 )); |
144 | 156 |
145 let trigger = {"url-filter": result.regexp}; | 157 let trigger = {"url-filter": result.regexp}; |
146 | 158 |
147 // Limit rules to to HTTP(S) URLs | 159 // Limit rules to to HTTP(S) URLs |
148 if (!/^(\^|http)/i.test(trigger["url-filter"])) | 160 if (!/^(\^|http)/i.test(trigger["url-filter"])) |
149 trigger["url-filter"] = "^https?://.*" + trigger["url-filter"]; | 161 trigger["url-filter"] = "^https?://.*" + trigger["url-filter"]; |
150 | 162 |
151 // 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 |
152 // a lowercase string and can therefore enable case sensitive matching. | 164 // a lowercase string unless the matchCase option was passed. |
153 if (result.caseSensitive) | 165 if (result.caseSensitive && !filter.matchCase) |
154 { | |
155 trigger["url-filter"] = trigger["url-filter"].toLowerCase(); | 166 trigger["url-filter"] = trigger["url-filter"].toLowerCase(); |
| 167 |
| 168 if (result.caseSensitive || filter.matchCase) |
156 trigger["url-filter-is-case-sensitive"] = true; | 169 trigger["url-filter-is-case-sensitive"] = true; |
157 } | |
158 | 170 |
159 return trigger; | 171 return trigger; |
160 } | 172 } |
161 | 173 |
162 function getResourceTypes(filter) | 174 function getResourceTypes(filter) |
163 { | 175 { |
164 let types = []; | 176 let types = []; |
165 | 177 |
166 if (filter.contentType & typeMap.IMAGE) | 178 if (filter.contentType & typeMap.IMAGE) |
167 types.push("image"); | 179 types.push("image"); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 | 212 |
201 return result; | 213 return result; |
202 } | 214 } |
203 | 215 |
204 function convertFilter(filter, action, withResourceTypes) | 216 function convertFilter(filter, action, withResourceTypes) |
205 { | 217 { |
206 let trigger = getRegExpTrigger(filter); | 218 let trigger = getRegExpTrigger(filter); |
207 let included = []; | 219 let included = []; |
208 let excluded = []; | 220 let excluded = []; |
209 | 221 |
210 if (filter.matchCase) | 222 parseDomains(filter.domains, included, excluded); |
211 trigger["url-filter-is-case-sensitive"] = true; | |
212 | |
213 parseDomains(filter.domains, included, excluded); | |
214 | 223 |
215 if (withResourceTypes) | 224 if (withResourceTypes) |
216 trigger["resource-type"] = getResourceTypes(filter); | 225 trigger["resource-type"] = getResourceTypes(filter); |
217 if (filter.thirdParty != null) | 226 if (filter.thirdParty != null) |
218 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; | 227 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; |
219 | 228 |
220 if (included.length > 0) | 229 if (included.length > 0) |
221 trigger["if-domain"] = addDomainPrefix(included); | 230 trigger["if-domain"] = addDomainPrefix(included); |
222 else if (excluded.length > 0) | 231 else if (excluded.length > 0) |
223 trigger["unless-domain"] = addDomainPrefix(excluded); | 232 trigger["unless-domain"] = addDomainPrefix(excluded); |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 | 427 |
419 for (let filter of this.elemhideExceptions) | 428 for (let filter of this.elemhideExceptions) |
420 addRule(convertFilter(filter, "ignore-previous-rules", false)); | 429 addRule(convertFilter(filter, "ignore-previous-rules", false)); |
421 for (let filter of this.requestFilters) | 430 for (let filter of this.requestFilters) |
422 addRule(convertFilter(filter, "block", true)); | 431 addRule(convertFilter(filter, "block", true)); |
423 for (let filter of this.requestExceptions) | 432 for (let filter of this.requestExceptions) |
424 addRule(convertFilter(filter, "ignore-previous-rules", true)); | 433 addRule(convertFilter(filter, "ignore-previous-rules", true)); |
425 | 434 |
426 return rules; | 435 return rules; |
427 }; | 436 }; |
LEFT | RIGHT |