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

Side by Side Diff: abp2blocklist.js

Issue 29336525: Issue 3584 - Work around WebKit uppercase ID matching bug (Closed)
Patch Set: Addressed more feedback Created Feb. 17, 2016, 3: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 "use strict"; 1 "use strict";
2 2
3 let readline = require("readline"); 3 let readline = require("readline");
4 let punycode = require("punycode"); 4 let punycode = require("punycode");
5 let tldjs = require("tldjs"); 5 let tldjs = require("tldjs");
6 let filterClasses = require("./adblockplus.js"); 6 let filterClasses = require("./adblockplus.js");
7 7
8 let typeMap = filterClasses.RegExpFilter.typeMap; 8 let typeMap = filterClasses.RegExpFilter.typeMap;
9 9
10 const selectorLimit = 5000; 10 const selectorLimit = 5000;
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 return true; 247 return true;
248 248
249 for (let name of Object.getOwnPropertyNames(obj)) 249 for (let name of Object.getOwnPropertyNames(obj))
250 if (hasNonASCI(obj[name])) 250 if (hasNonASCI(obj[name]))
251 return true; 251 return true;
252 } 252 }
253 253
254 return false; 254 return false;
255 } 255 }
256 256
257 function convertIDSelectorsToAttributeSelectors(selector)
258 {
259 // First we figure out where all the IDs are
260 let sep = "";
261 let start = null;
262 let positions = [];
263 for (let i = 0; i < selector.length; i++)
264 {
265 let chr = selector[i];
266
267 if (chr == "\\") // ignore escaped characters
268 i++;
269 else if (chr == sep) // don't split within quoted text
Sebastian Noack 2016/02/17 15:24:37 Nit: This code has bee ncopied from cssProperties.
kzar 2016/02/17 15:35:03 Done.
270 sep = ""; // e.g. [attr=","]
271 else if (sep == "")
272 {
273 if (chr == '"' || chr == "'")
274 sep = chr;
275 else if (start == null) // look for the start of an ID
276 {
277 if (chr == "#")
278 start = i;
279 }
280 else if (chr < "0" && chr != "-" ||
281 chr > "9" && chr < "A" ||
282 chr > "Z" && chr != "_" && chr < "a" ||
283 chr > "z" && chr < "\x80") // look for the end of the ID
284 {
285 positions.push({start: start, end: i});
286 start = null;
287 }
288 }
289 }
290 if (start != null)
291 positions.push({start: start, end: selector.length});
292
293 // Now replace them all with the [id="someID"] form
294 let newSelector = "";
Sebastian Noack 2016/02/17 15:24:37 Concatenating strings in a loop is expensive. You
kzar 2016/02/17 15:35:04 Done.
295 let position = 0;
296 for (let ID of positions)
Sebastian Noack 2016/02/17 15:24:37 Nit: The variable shouldn't start with uppercase.
kzar 2016/02/17 15:35:04 Done.
297 {
298 newSelector += selector.substring(position, ID.start);
299 newSelector += '[id=' + selector.substring(ID.start + 1, ID.end) + ']';
300 position = ID.end;
301 }
302 newSelector += selector.substring(position);
303
304 return newSelector;
305 }
306
257 function logRules() 307 function logRules()
258 { 308 {
259 let rules = []; 309 let rules = [];
260 310
261 function addRule(rule) 311 function addRule(rule)
262 { 312 {
263 if (!hasNonASCI(rule)) 313 if (!hasNonASCI(rule))
264 rules.push(rule); 314 rules.push(rule);
265 } 315 }
266 316
(...skipping 12 matching lines...) Expand all
279 let group = groupedElemhideFilters.get(matchDomain) || []; 329 let group = groupedElemhideFilters.get(matchDomain) || [];
280 group.push(result.selector); 330 group.push(result.selector);
281 groupedElemhideFilters.set(matchDomain, group); 331 groupedElemhideFilters.set(matchDomain, group);
282 } 332 }
283 } 333 }
284 334
285 groupedElemhideFilters.forEach((selectors, matchDomain) => 335 groupedElemhideFilters.forEach((selectors, matchDomain) =>
286 { 336 {
287 while (selectors.length) 337 while (selectors.length)
288 { 338 {
339 let selector = selectors.splice(0, selectorLimit).join(", ");
340
341 // As of Safari 9.0 element IDs are matched as lowercase. We work around
342 // this by converting to the attribute format [id="elementID"]
343 selector = convertIDSelectorsToAttributeSelectors(selector);
344
289 addRule({ 345 addRule({
290 trigger: {"url-filter": matchDomain}, 346 trigger: {"url-filter": matchDomain},
291 action: {type: "css-display-none", 347 action: {type: "css-display-none",
292 selector: selectors.splice(0, selectorLimit).join(", ")} 348 selector: selector}
293 }); 349 });
294 } 350 }
295 }); 351 });
296 352
297 for (let filter of elemhideExceptions) 353 for (let filter of elemhideExceptions)
298 addRule(convertFilter(filter, "ignore-previous-rules", false)); 354 addRule(convertFilter(filter, "ignore-previous-rules", false));
299 355
300 for (let filter of requestFilters) 356 for (let filter of requestFilters)
301 addRule(convertFilter(filter, "block", true)); 357 addRule(convertFilter(filter, "block", true));
302 for (let filter of requestExceptions) 358 for (let filter of requestExceptions)
303 addRule(convertFilter(filter, "ignore-previous-rules", true)); 359 addRule(convertFilter(filter, "ignore-previous-rules", true));
304 360
305 console.log(JSON.stringify(rules, null, "\t")); 361 console.log(JSON.stringify(rules, null, "\t"));
306 } 362 }
307 363
308 let rl = readline.createInterface({input: process.stdin, terminal: false}); 364 let rl = readline.createInterface({input: process.stdin, terminal: false});
309 rl.on("line", parseFilter); 365 rl.on("line", parseFilter);
310 rl.on("close", logRules); 366 rl.on("close", logRules);
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