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