Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/contentBlockerList.js

Issue 29336787: Issue 3670 - Make rules case sensitive where possible (Closed)
Patch Set: Keep rules with non alpha characters after hostname ends case sensitive Created Feb. 24, 2016, 9 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // Convert the "regexpSource" part of a filter's text to a regular expression,
69 // also deciding if the expression can safely be converted to and matched as
70 // lowercase.
68 function toRegExp(text) 71 function toRegExp(text)
69 { 72 {
70 let result = []; 73 let result = [];
71 let lastIndex = text.length - 1; 74 let lastIndex = text.length - 1;
75 let hostnameStarted = false;
76 let hostnameFinished = false;
77 let caseSensitive = false;
72 78
73 for (let i = 0; i < text.length; i++) 79 for (let i = 0; i < text.length; i++)
74 { 80 {
75 let c = text[i]; 81 let c = text[i];
76 82
77 switch (c) 83 switch (c)
78 { 84 {
79 case "*": 85 case "*":
80 if (result.length > 0 && i < lastIndex && text[i + 1] != "*") 86 if (result.length > 0 && i < lastIndex && text[i + 1] != "*")
81 result.push(".*"); 87 result.push(".*");
82 break; 88 break;
83 case "^": 89 case "^":
84 if (i < lastIndex) 90 if (i < lastIndex)
85 result.push("."); 91 result.push(".");
86 break; 92 break;
87 case "|": 93 case "|":
88 if (i == 0) 94 if (i == 0)
89 { 95 {
90 result.push("^"); 96 result.push("^");
91 break; 97 break;
92 } 98 }
93 if (i == lastIndex) 99 if (i == lastIndex)
94 { 100 {
95 result.push("$"); 101 result.push("$");
96 break; 102 break;
97 } 103 }
98 if (i == 1 && text[0] == "|") 104 if (i == 1 && text[0] == "|")
99 { 105 {
100 result.push("https?://"); 106 result.push("https?://");
107 hostnameStarted = caseSensitive = true;
101 break; 108 break;
102 } 109 }
103 case ".": case "+": case "?": case "$": 110 result.push("\\", c);
104 case "{": case "}": case "(": case ")": 111 break;
105 case "[": case "]": case "\\": 112 case "/": case "?": case "*": case "^":
113 if (hostnameStarted)
114 hostnameFinished = true;
115 else if (i >= 2 && text[i-2] == ":" && text[i-1] == "/" && c == "/")
116 hostnameStarted = 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 "\\":
106 result.push("\\", c); 125 result.push("\\", c);
107 break; 126 break;
108 default: 127 default:
128 if (hostnameFinished && (c >= "a" && c <= "z" || c >= "A" && c <= "Z"))
129 caseSensitive = false;
109 result.push(c); 130 result.push(c);
110 } 131 }
111 } 132 }
112 133
113 return result.join(""); 134 return {regexp: result.join(""), caseSensitive: caseSensitive};
114 } 135 }
115 136
116 function getRegExpSource(filter) 137 function getRegExpTrigger(filter)
117 { 138 {
118 let source = toRegExp(filter.regexpSource.replace( 139 let result = toRegExp(filter.regexpSource.replace(
119 // Safari expects punycode, filter lists use unicode 140 // Safari expects punycode, filter lists use unicode
120 /^(\|\||\|?https?:\/\/)([\w\-.*\u0080-\uFFFF]+)/i, 141 /^(\|\||\|?https?:\/\/)([\w\-.*\u0080-\uFFFF]+)/i,
121 function (match, prefix, domain) 142 function (match, prefix, domain)
122 { 143 {
123 return prefix + punycode.toASCII(domain); 144 return prefix + punycode.toASCII(domain);
124 } 145 }
125 )); 146 ));
126 147
148 let trigger = {"url-filter": result.regexp};
149
127 // Limit rules to to HTTP(S) URLs 150 // Limit rules to to HTTP(S) URLs
128 if (!/^(\^|http)/i.test(source)) 151 if (!/^(\^|http)/i.test(trigger["url-filter"]))
129 source = "^https?://.*" + source; 152 trigger["url-filter"] = "^https?://.*" + trigger["url-filter"];
130 153
131 return source; 154 // For rules containing only a hostname we know that we're matching against
155 // a lowercase string and can therefore enable case sensitive matching.
156 if (result.caseSensitive)
157 {
158 trigger["url-filter"] = trigger["url-filter"].toLowerCase();
159 trigger["url-filter-is-case-sensitive"] = true;
160 }
161
162 return trigger;
132 } 163 }
133 164
134 function getResourceTypes(filter) 165 function getResourceTypes(filter)
135 { 166 {
136 let types = []; 167 let types = [];
137 168
138 if (filter.contentType & typeMap.IMAGE) 169 if (filter.contentType & typeMap.IMAGE)
139 types.push("image"); 170 types.push("image");
140 if (filter.contentType & typeMap.STYLESHEET) 171 if (filter.contentType & typeMap.STYLESHEET)
141 types.push("style-sheet"); 172 types.push("style-sheet");
(...skipping 26 matching lines...) Expand all
168 199
169 if (tldjs.getDomain(domain) == domain) 200 if (tldjs.getDomain(domain) == domain)
170 result.push("www." + domain); 201 result.push("www." + domain);
171 } 202 }
172 203
173 return result; 204 return result;
174 } 205 }
175 206
176 function convertFilter(filter, action, withResourceTypes) 207 function convertFilter(filter, action, withResourceTypes)
177 { 208 {
178 let trigger = {"url-filter": getRegExpSource(filter)}; 209 let trigger = getRegExpTrigger(filter);
179 let included = []; 210 let included = [];
180 let excluded = []; 211 let excluded = [];
181 212
182 parseDomains(filter.domains, included, excluded); 213 if (filter.matchCase)
214 trigger["url-filter-is-case-sensitive"] = true;
215
216 parseDomains(filter.domains, included, excluded);
183 217
184 if (withResourceTypes) 218 if (withResourceTypes)
185 trigger["resource-type"] = getResourceTypes(filter); 219 trigger["resource-type"] = getResourceTypes(filter);
186 if (filter.thirdParty != null) 220 if (filter.thirdParty != null)
187 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; 221 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"];
188 222
189 if (included.length > 0) 223 if (included.length > 0)
190 trigger["if-domain"] = addDomainPrefix(included); 224 trigger["if-domain"] = addDomainPrefix(included);
191 else if (excluded.length > 0) 225 else if (excluded.length > 0)
192 trigger["unless-domain"] = addDomainPrefix(excluded); 226 trigger["unless-domain"] = addDomainPrefix(excluded);
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 { 404 {
371 while (selectors.length) 405 while (selectors.length)
372 { 406 {
373 let selector = selectors.splice(0, selectorLimit).join(", "); 407 let selector = selectors.splice(0, selectorLimit).join(", ");
374 408
375 // As of Safari 9.0 element IDs are matched as lowercase. We work around 409 // As of Safari 9.0 element IDs are matched as lowercase. We work around
376 // this by converting to the attribute format [id="elementID"] 410 // this by converting to the attribute format [id="elementID"]
377 selector = convertIDSelectorsToAttributeSelectors(selector); 411 selector = convertIDSelectorsToAttributeSelectors(selector);
378 412
379 addRule({ 413 addRule({
380 trigger: {"url-filter": matchDomain}, 414 trigger: {"url-filter": matchDomain,
415 "url-filter-is-case-sensitive": true},
381 action: {type: "css-display-none", 416 action: {type: "css-display-none",
382 selector: selector} 417 selector: selector}
383 }); 418 });
384 } 419 }
385 }); 420 });
386 421
387 for (let filter of this.elemhideExceptions) 422 for (let filter of this.elemhideExceptions)
388 addRule(convertFilter(filter, "ignore-previous-rules", false)); 423 addRule(convertFilter(filter, "ignore-previous-rules", false));
389 for (let filter of this.requestFilters) 424 for (let filter of this.requestFilters)
390 addRule(convertFilter(filter, "block", true)); 425 addRule(convertFilter(filter, "block", true));
391 for (let filter of this.requestExceptions) 426 for (let filter of this.requestExceptions)
392 addRule(convertFilter(filter, "ignore-previous-rules", true)); 427 addRule(convertFilter(filter, "ignore-previous-rules", true));
393 428
394 return rules; 429 return rules;
395 }; 430 };
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld