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