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