LEFT | RIGHT |
(no file at all) | |
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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 } | 43 } |
44 } | 44 } |
45 | 45 |
46 function escapeRegExp(s) | 46 function escapeRegExp(s) |
47 { | 47 { |
48 return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | 48 return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
49 } | 49 } |
50 | 50 |
51 function matchDomain(domain) | 51 function matchDomain(domain) |
52 { | 52 { |
53 return "^https?://([^/:]*\\.)?" + escapeRegExp(domain) + "[/:]"; | 53 return "^https?://([^/:]*\\.)?" + escapeRegExp(domain).toLowerCase() + "[/:]"; |
54 } | 54 } |
55 | 55 |
56 function convertElemHideFilter(filter, elemhideSelectorExceptions) | 56 function convertElemHideFilter(filter, elemhideSelectorExceptions) |
57 { | 57 { |
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 /** |
| 69 * Convert the given filter "regexpSource" string into a regular expression. |
| 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 */ |
68 function toRegExp(text) | 78 function toRegExp(text) |
69 { | 79 { |
70 let result = []; | 80 let result = []; |
71 let lastIndex = text.length - 1; | 81 let lastIndex = text.length - 1; |
| 82 let hostnameStarted = false; |
| 83 let hostnameFinished = false; |
| 84 let caseSensitive = false; |
72 | 85 |
73 for (let i = 0; i < text.length; i++) | 86 for (let i = 0; i < text.length; i++) |
74 { | 87 { |
75 let c = text[i]; | 88 let c = text[i]; |
76 | 89 |
77 switch (c) | 90 switch (c) |
78 { | 91 { |
79 case "*": | 92 case "*": |
| 93 if (hostnameStarted) |
| 94 hostnameFinished = true; |
80 if (result.length > 0 && i < lastIndex && text[i + 1] != "*") | 95 if (result.length > 0 && i < lastIndex && text[i + 1] != "*") |
81 result.push(".*"); | 96 result.push(".*"); |
82 break; | 97 break; |
83 case "^": | 98 case "^": |
| 99 if (hostnameStarted) |
| 100 hostnameFinished = true; |
84 if (i < lastIndex) | 101 if (i < lastIndex) |
85 result.push("."); | 102 result.push("."); |
86 break; | 103 break; |
87 case "|": | 104 case "|": |
88 if (i == 0) | 105 if (i == 0) |
89 { | 106 { |
90 result.push("^"); | 107 result.push("^"); |
91 break; | 108 break; |
92 } | 109 } |
93 if (i == lastIndex) | 110 if (i == lastIndex) |
94 { | 111 { |
95 result.push("$"); | 112 result.push("$"); |
96 break; | 113 break; |
97 } | 114 } |
98 if (i == 1 && text[0] == "|") | 115 if (i == 1 && text[0] == "|") |
99 { | 116 { |
| 117 hostnameStarted = caseSensitive = true; |
100 result.push("https?://"); | 118 result.push("https?://"); |
101 break; | 119 break; |
102 } | 120 } |
103 case ".": case "+": case "?": case "$": | |
104 case "{": case "}": case "(": case ")": | |
105 case "[": case "]": case "\\": | |
106 result.push("\\", c); | 121 result.push("\\", c); |
107 break; | 122 break; |
| 123 case "?": |
| 124 if (hostnameStarted) |
| 125 hostnameFinished = true; |
| 126 case ".": case "+": case "$": case "{": case "}": |
| 127 case "(": case ")": case "[": case "]": case "\\": |
| 128 result.push("\\", c); |
| 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; |
108 default: | 135 default: |
| 136 if (hostnameFinished && (c >= "a" && c <= "z" || |
| 137 c >= "A" && c <= "Z")) |
| 138 caseSensitive = false; |
109 result.push(c); | 139 result.push(c); |
110 } | 140 } |
111 } | 141 } |
112 | 142 |
113 return result.join(""); | 143 return {regexp: result.join(""), caseSensitive: caseSensitive}; |
114 } | 144 } |
115 | 145 |
116 function getRegExpSource(filter) | 146 function getRegExpTrigger(filter) |
117 { | 147 { |
118 let source = toRegExp(filter.regexpSource.replace( | 148 let result = toRegExp(filter.regexpSource.replace( |
119 // Safari expects punycode, filter lists use unicode | 149 // Safari expects punycode, filter lists use unicode |
120 /^(\|\||\|?https?:\/\/)([\w\-.*\u0080-\uFFFF]+)/i, | 150 /^(\|\||\|?https?:\/\/)([\w\-.*\u0080-\uFFFF]+)/i, |
121 function (match, prefix, domain) | 151 function (match, prefix, domain) |
122 { | 152 { |
123 return prefix + punycode.toASCII(domain); | 153 return prefix + punycode.toASCII(domain); |
124 } | 154 } |
125 )); | 155 )); |
126 | 156 |
| 157 let trigger = {"url-filter": result.regexp}; |
| 158 |
127 // Limit rules to to HTTP(S) URLs | 159 // Limit rules to to HTTP(S) URLs |
128 if (!/^(\^|http)/i.test(source)) | 160 if (!/^(\^|http)/i.test(trigger["url-filter"])) |
129 source = "^https?://.*" + source; | 161 trigger["url-filter"] = "^https?://.*" + trigger["url-filter"]; |
130 | 162 |
131 return source; | 163 // For rules containing only a hostname we know that we're matching against |
| 164 // a lowercase string unless the matchCase option was passed. |
| 165 if (result.caseSensitive && !filter.matchCase) |
| 166 trigger["url-filter"] = trigger["url-filter"].toLowerCase(); |
| 167 |
| 168 if (result.caseSensitive || filter.matchCase) |
| 169 trigger["url-filter-is-case-sensitive"] = true; |
| 170 |
| 171 return trigger; |
132 } | 172 } |
133 | 173 |
134 function getResourceTypes(filter) | 174 function getResourceTypes(filter) |
135 { | 175 { |
136 let types = []; | 176 let types = []; |
137 | 177 |
138 if (filter.contentType & typeMap.IMAGE) | 178 if (filter.contentType & typeMap.IMAGE) |
139 types.push("image"); | 179 types.push("image"); |
140 if (filter.contentType & typeMap.STYLESHEET) | 180 if (filter.contentType & typeMap.STYLESHEET) |
141 types.push("style-sheet"); | 181 types.push("style-sheet"); |
(...skipping 26 matching lines...) Expand all Loading... |
168 | 208 |
169 if (tldjs.getDomain(domain) == domain) | 209 if (tldjs.getDomain(domain) == domain) |
170 result.push("www." + domain); | 210 result.push("www." + domain); |
171 } | 211 } |
172 | 212 |
173 return result; | 213 return result; |
174 } | 214 } |
175 | 215 |
176 function convertFilter(filter, action, withResourceTypes) | 216 function convertFilter(filter, action, withResourceTypes) |
177 { | 217 { |
178 let trigger = {"url-filter": getRegExpSource(filter)}; | 218 let trigger = getRegExpTrigger(filter); |
179 let included = []; | 219 let included = []; |
180 let excluded = []; | 220 let excluded = []; |
181 | 221 |
182 parseDomains(filter.domains, included, excluded); | 222 parseDomains(filter.domains, included, excluded); |
183 | 223 |
184 if (withResourceTypes) | 224 if (withResourceTypes) |
185 trigger["resource-type"] = getResourceTypes(filter); | 225 trigger["resource-type"] = getResourceTypes(filter); |
186 if (filter.thirdParty != null) | 226 if (filter.thirdParty != null) |
187 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; | 227 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; |
188 | 228 |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 { | 410 { |
371 while (selectors.length) | 411 while (selectors.length) |
372 { | 412 { |
373 let selector = selectors.splice(0, selectorLimit).join(", "); | 413 let selector = selectors.splice(0, selectorLimit).join(", "); |
374 | 414 |
375 // As of Safari 9.0 element IDs are matched as lowercase. We work around | 415 // As of Safari 9.0 element IDs are matched as lowercase. We work around |
376 // this by converting to the attribute format [id="elementID"] | 416 // this by converting to the attribute format [id="elementID"] |
377 selector = convertIDSelectorsToAttributeSelectors(selector); | 417 selector = convertIDSelectorsToAttributeSelectors(selector); |
378 | 418 |
379 addRule({ | 419 addRule({ |
380 trigger: {"url-filter": matchDomain}, | 420 trigger: {"url-filter": matchDomain, |
| 421 "url-filter-is-case-sensitive": true}, |
381 action: {type: "css-display-none", | 422 action: {type: "css-display-none", |
382 selector: selector} | 423 selector: selector} |
383 }); | 424 }); |
384 } | 425 } |
385 }); | 426 }); |
386 | 427 |
387 for (let filter of this.elemhideExceptions) | 428 for (let filter of this.elemhideExceptions) |
388 addRule(convertFilter(filter, "ignore-previous-rules", false)); | 429 addRule(convertFilter(filter, "ignore-previous-rules", false)); |
389 for (let filter of this.requestFilters) | 430 for (let filter of this.requestFilters) |
390 addRule(convertFilter(filter, "block", true)); | 431 addRule(convertFilter(filter, "block", true)); |
391 for (let filter of this.requestExceptions) | 432 for (let filter of this.requestExceptions) |
392 addRule(convertFilter(filter, "ignore-previous-rules", true)); | 433 addRule(convertFilter(filter, "ignore-previous-rules", true)); |
393 | 434 |
394 return rules; | 435 return rules; |
395 }; | 436 }; |
LEFT | RIGHT |