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: Addressed more feedback Created Feb. 24, 2016, 11:12 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,
Sebastian Noack 2016/02/25 02:28:35 Sorry for commenting after LGTM. Just one more nit
kzar 2016/02/25 15:22:28 Done.
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 "*":
86 if (hostnameStarted)
87 hostnameFinished = true;
80 if (result.length > 0 && i < lastIndex && text[i + 1] != "*") 88 if (result.length > 0 && i < lastIndex && text[i + 1] != "*")
81 result.push(".*"); 89 result.push(".*");
82 break; 90 break;
83 case "^": 91 case "^":
92 if (hostnameStarted)
93 hostnameFinished = true;
84 if (i < lastIndex) 94 if (i < lastIndex)
85 result.push("."); 95 result.push(".");
86 break; 96 break;
87 case "|": 97 case "|":
88 if (i == 0) 98 if (i == 0)
89 { 99 {
90 result.push("^"); 100 result.push("^");
91 break; 101 break;
92 } 102 }
93 if (i == lastIndex) 103 if (i == lastIndex)
94 { 104 {
95 result.push("$"); 105 result.push("$");
96 break; 106 break;
97 } 107 }
98 if (i == 1 && text[0] == "|") 108 if (i == 1 && text[0] == "|")
99 { 109 {
110 hostnameStarted = caseSensitive = true;
100 result.push("https?://"); 111 result.push("https?://");
101 break; 112 break;
102 } 113 }
103 case ".": case "+": case "?": case "$":
104 case "{": case "}": case "(": case ")":
105 case "[": case "]": case "\\":
106 result.push("\\", c); 114 result.push("\\", c);
107 break; 115 break;
116 case "?":
117 if (hostnameStarted)
118 hostnameFinished = true;
119 case ".": case "+": case "$": case "{": case "}":
120 case "(": case ")": case "[": case "]": case "\\":
121 result.push("\\", c);
122 break;
123 case "/":
124 if (hostnameStarted)
125 hostnameFinished = true;
126 else if (text.charAt(i-2) == ":" && text.charAt(i-1) == "/")
127 hostnameStarted = caseSensitive = true;
108 default: 128 default:
129 if (hostnameFinished && (c >= "a" && c <= "z" ||
130 c >= "A" && c <= "Z"))
131 caseSensitive = false;
109 result.push(c); 132 result.push(c);
110 } 133 }
111 } 134 }
112 135
113 return result.join(""); 136 return {regexp: result.join(""), caseSensitive: caseSensitive};
114 } 137 }
115 138
116 function getRegExpSource(filter) 139 function getRegExpTrigger(filter)
117 { 140 {
118 let source = toRegExp(filter.regexpSource.replace( 141 let result = toRegExp(filter.regexpSource.replace(
119 // Safari expects punycode, filter lists use unicode 142 // Safari expects punycode, filter lists use unicode
120 /^(\|\||\|?https?:\/\/)([\w\-.*\u0080-\uFFFF]+)/i, 143 /^(\|\||\|?https?:\/\/)([\w\-.*\u0080-\uFFFF]+)/i,
121 function (match, prefix, domain) 144 function (match, prefix, domain)
122 { 145 {
123 return prefix + punycode.toASCII(domain); 146 return prefix + punycode.toASCII(domain);
124 } 147 }
125 )); 148 ));
126 149
150 let trigger = {"url-filter": result.regexp};
151
127 // Limit rules to to HTTP(S) URLs 152 // Limit rules to to HTTP(S) URLs
128 if (!/^(\^|http)/i.test(source)) 153 if (!/^(\^|http)/i.test(trigger["url-filter"]))
129 source = "^https?://.*" + source; 154 trigger["url-filter"] = "^https?://.*" + trigger["url-filter"];
130 155
131 return source; 156 // For rules containing only a hostname we know that we're matching against
157 // a lowercase string unless the matchCase option was passed.
158 if (result.caseSensitive && !filter.matchCase)
159 trigger["url-filter"] = trigger["url-filter"].toLowerCase();
160
161 if (result.caseSensitive || filter.matchCase)
162 trigger["url-filter-is-case-sensitive"] = true;
163
164 return trigger;
132 } 165 }
133 166
134 function getResourceTypes(filter) 167 function getResourceTypes(filter)
135 { 168 {
136 let types = []; 169 let types = [];
137 170
138 if (filter.contentType & typeMap.IMAGE) 171 if (filter.contentType & typeMap.IMAGE)
139 types.push("image"); 172 types.push("image");
140 if (filter.contentType & typeMap.STYLESHEET) 173 if (filter.contentType & typeMap.STYLESHEET)
141 types.push("style-sheet"); 174 types.push("style-sheet");
(...skipping 26 matching lines...) Expand all
168 201
169 if (tldjs.getDomain(domain) == domain) 202 if (tldjs.getDomain(domain) == domain)
170 result.push("www." + domain); 203 result.push("www." + domain);
171 } 204 }
172 205
173 return result; 206 return result;
174 } 207 }
175 208
176 function convertFilter(filter, action, withResourceTypes) 209 function convertFilter(filter, action, withResourceTypes)
177 { 210 {
178 let trigger = {"url-filter": getRegExpSource(filter)}; 211 let trigger = getRegExpTrigger(filter);
179 let included = []; 212 let included = [];
180 let excluded = []; 213 let excluded = [];
181 214
182 parseDomains(filter.domains, included, excluded); 215 parseDomains(filter.domains, included, excluded);
183 216
184 if (withResourceTypes) 217 if (withResourceTypes)
185 trigger["resource-type"] = getResourceTypes(filter); 218 trigger["resource-type"] = getResourceTypes(filter);
186 if (filter.thirdParty != null) 219 if (filter.thirdParty != null)
187 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"]; 220 trigger["load-type"] = [filter.thirdParty ? "third-party" : "first-party"];
188 221
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 { 403 {
371 while (selectors.length) 404 while (selectors.length)
372 { 405 {
373 let selector = selectors.splice(0, selectorLimit).join(", "); 406 let selector = selectors.splice(0, selectorLimit).join(", ");
374 407
375 // As of Safari 9.0 element IDs are matched as lowercase. We work around 408 // As of Safari 9.0 element IDs are matched as lowercase. We work around
376 // this by converting to the attribute format [id="elementID"] 409 // this by converting to the attribute format [id="elementID"]
377 selector = convertIDSelectorsToAttributeSelectors(selector); 410 selector = convertIDSelectorsToAttributeSelectors(selector);
378 411
379 addRule({ 412 addRule({
380 trigger: {"url-filter": matchDomain}, 413 trigger: {"url-filter": matchDomain,
414 "url-filter-is-case-sensitive": true},
381 action: {type: "css-display-none", 415 action: {type: "css-display-none",
382 selector: selector} 416 selector: selector}
383 }); 417 });
384 } 418 }
385 }); 419 });
386 420
387 for (let filter of this.elemhideExceptions) 421 for (let filter of this.elemhideExceptions)
388 addRule(convertFilter(filter, "ignore-previous-rules", false)); 422 addRule(convertFilter(filter, "ignore-previous-rules", false));
389 for (let filter of this.requestFilters) 423 for (let filter of this.requestFilters)
390 addRule(convertFilter(filter, "block", true)); 424 addRule(convertFilter(filter, "block", true));
391 for (let filter of this.requestExceptions) 425 for (let filter of this.requestExceptions)
392 addRule(convertFilter(filter, "ignore-previous-rules", true)); 426 addRule(convertFilter(filter, "ignore-previous-rules", true));
393 427
394 return rules; 428 return rules;
395 }; 429 };
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