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

Side by Side Diff: lib/abp2blocklist.js

Issue 29337803: Issue 3710 - Unify hostname logic (Closed)
Patch Set: Consider the case of | ending a hostname Created March 8, 2016, 12:34 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 | « abp2blocklist.js ('k') | 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 /** 68 /**
69 * Convert the given filter "regexpSource" string into a regular expression. 69 * Convert the given filter "regexpSource" string into a regular expression,
70 * handling the conversion of unicode inside hostnames to punycode.
70 * (Also deciding if the regular expression can be safely converted to and 71 * (Also deciding if the regular expression can be safely converted to and
71 * matched as lower case or not.) 72 * matched as lower case or not.)
72 * 73 *
73 * @param {string} text regexpSource property of a filter 74 * @param {string} text regexpSource property of a filter
74 * @returns {object} An object containing a regular expression string and a bool 75 * @returns {object} An object containing a regular expression string and a bool
75 * indicating if the filter can be safely matched as lower 76 * indicating if the filter can be safely matched as lower
76 * case: {regexp: "...", caseSenstive: true/false} 77 * case: {regexp: "...", canSafelyMatchAsLowercase: true/false }
77 */ 78 */
78 function toRegExp(text) 79 function toRegExp(text)
79 { 80 {
80 let result = []; 81 let result = [];
81 let lastIndex = text.length - 1; 82 let lastIndex = text.length - 1;
82 let hostnameStarted = false; 83 let hostnameStart = null;
83 let hostnameFinished = false; 84 let hostnameFinished = false;
84 let caseSensitive = false; 85 let canSafelyMatchAsLowercase = false;
85 86
86 for (let i = 0; i < text.length; i++) 87 for (let i = 0; i < text.length; i++)
87 { 88 {
88 let c = text[i]; 89 let c = text[i];
89 90
91 // If we're currently inside the hostname we have to be careful not to
92 // escape any characters until after we have converted it to punycode.
93 if (hostnameStart != null && !hostnameFinished)
94 {
95 let endingChar = (c == "*" || c == "^" ||
96 c == "?" || c == "/" || c == "|");
97 if (!endingChar && i != lastIndex)
98 continue;
99
100 let hostname = text.substring(hostnameStart, endingChar ? i : i + 1);
101 hostnameFinished = true;
102 result.push(escapeRegExp(punycode.toASCII(hostname)));
103 if (!endingChar)
104 break;
105 }
106
90 switch (c) 107 switch (c)
91 { 108 {
92 case "*": 109 case "*":
93 if (hostnameStarted)
94 hostnameFinished = true;
95 if (result.length > 0 && i < lastIndex && text[i + 1] != "*") 110 if (result.length > 0 && i < lastIndex && text[i + 1] != "*")
96 result.push(".*"); 111 result.push(".*");
97 break; 112 break;
98 case "^": 113 case "^":
99 if (hostnameStarted)
100 hostnameFinished = true;
101 if (i < lastIndex) 114 if (i < lastIndex)
102 result.push("."); 115 result.push(".");
103 break; 116 break;
104 case "|": 117 case "|":
105 if (i == 0) 118 if (i == 0)
106 { 119 {
107 result.push("^"); 120 result.push("^");
108 break; 121 break;
109 } 122 }
110 if (i == lastIndex) 123 if (i == lastIndex)
111 { 124 {
112 result.push("$"); 125 result.push("$");
113 break; 126 break;
114 } 127 }
115 if (i == 1 && text[0] == "|") 128 if (i == 1 && text[0] == "|")
116 { 129 {
117 hostnameStarted = caseSensitive = true; 130 hostnameStart = i + 1;
131 canSafelyMatchAsLowercase = true;
118 result.push("https?://"); 132 result.push("https?://");
119 break; 133 break;
120 } 134 }
135 result.push("\\|");
136 break;
137 case "/":
138 if (!hostnameFinished &&
139 text.charAt(i-2) == ":" && text.charAt(i-1) == "/")
140 {
141 hostnameStart = i + 1;
142 canSafelyMatchAsLowercase = true;
143 }
144 result.push("/");
145 break;
146 case ".": case "+": case "$": case "?":
147 case "{": case "}": case "(": case ")":
148 case "[": case "]": case "\\":
121 result.push("\\", c); 149 result.push("\\", c);
122 break; 150 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;
135 default: 151 default:
136 if (hostnameFinished && (c >= "a" && c <= "z" || 152 if (hostnameFinished && (c >= "a" && c <= "z" ||
137 c >= "A" && c <= "Z")) 153 c >= "A" && c <= "Z"))
138 caseSensitive = false; 154 canSafelyMatchAsLowercase = false;
139 result.push(c); 155 result.push(c);
140 } 156 }
141 } 157 }
142 158
143 return {regexp: result.join(""), caseSensitive: caseSensitive}; 159 return {regexp: result.join(""),
160 canSafelyMatchAsLowercase: canSafelyMatchAsLowercase};
144 } 161 }
145 162
146 function getRegExpTrigger(filter) 163 function getRegExpTrigger(filter)
147 { 164 {
148 let result = toRegExp(filter.regexpSource.replace( 165 let result = toRegExp(filter.regexpSource);
149 // Safari expects punycode, filter lists use unicode
150 /^(\|\||\|?https?:\/\/)([\w\-.*\u0080-\uFFFF]+)/i,
151 function (match, prefix, domain)
152 {
153 return prefix + punycode.toASCII(domain);
154 }
155 ));
156 166
157 let trigger = {"url-filter": result.regexp}; 167 let trigger = {"url-filter": result.regexp};
158 168
159 // Limit rules to to HTTP(S) URLs 169 // Limit rules to to HTTP(S) URLs
160 if (!/^(\^|http)/i.test(trigger["url-filter"])) 170 if (!/^(\^|http)/i.test(trigger["url-filter"]))
161 trigger["url-filter"] = "^https?://.*" + trigger["url-filter"]; 171 trigger["url-filter"] = "^https?://.*" + trigger["url-filter"];
162 172
163 // For rules containing only a hostname we know that we're matching against 173 // For rules containing only a hostname we know that we're matching against
164 // a lowercase string unless the matchCase option was passed. 174 // a lowercase string unless the matchCase option was passed.
165 if (result.caseSensitive && !filter.matchCase) 175 if (result.canSafelyMatchAsLowercase && !filter.matchCase)
166 trigger["url-filter"] = trigger["url-filter"].toLowerCase(); 176 trigger["url-filter"] = trigger["url-filter"].toLowerCase();
167 177
168 if (result.caseSensitive || filter.matchCase) 178 if (result.canSafelyMatchAsLowercase || filter.matchCase)
169 trigger["url-filter-is-case-sensitive"] = true; 179 trigger["url-filter-is-case-sensitive"] = true;
170 180
171 return trigger; 181 return trigger;
172 } 182 }
173 183
174 function getResourceTypes(filter) 184 function getResourceTypes(filter)
175 { 185 {
176 let types = []; 186 let types = [];
177 187
178 if (filter.contentType & typeMap.IMAGE) 188 if (filter.contentType & typeMap.IMAGE)
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 437
428 for (let filter of this.elemhideExceptions) 438 for (let filter of this.elemhideExceptions)
429 addRule(convertFilter(filter, "ignore-previous-rules", false)); 439 addRule(convertFilter(filter, "ignore-previous-rules", false));
430 for (let filter of this.requestFilters) 440 for (let filter of this.requestFilters)
431 addRule(convertFilter(filter, "block", true)); 441 addRule(convertFilter(filter, "block", true));
432 for (let filter of this.requestExceptions) 442 for (let filter of this.requestExceptions)
433 addRule(convertFilter(filter, "ignore-previous-rules", true)); 443 addRule(convertFilter(filter, "ignore-previous-rules", true));
434 444
435 return rules; 445 return rules;
436 }; 446 };
OLDNEW
« no previous file with comments | « abp2blocklist.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld