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

Delta Between Two Patch Sets: abp2blocklist.js

Issue 29336349: Issue 3585 - Merge element hiding rules for the same domain (Closed)
Left Patch Set: Rebased to use ES2015 features everywhere, limit to 5000 selectors per rule and comma delimit selec… Created Feb. 15, 2016, 6:17 p.m.
Right Patch Set: Fixed mistake in generated JSON structure Created Feb. 16, 2016, 7:02 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 95
96 function convertElemHideFilter(filter) 96 function convertElemHideFilter(filter)
97 { 97 {
98 let included = []; 98 let included = [];
99 let excluded = []; 99 let excluded = [];
100 let rules = []; 100 let rules = [];
101 101
102 parseDomains(filter.domains, included, excluded); 102 parseDomains(filter.domains, included, excluded);
103 103
104 if (excluded.length == 0 && !(filter.selector in elemhideSelectorExceptions)) 104 if (excluded.length == 0 && !(filter.selector in elemhideSelectorExceptions))
105 return [included.map(matchDomain), filter.selector]; 105 return {matchDomains: included.map(matchDomain), selector: filter.selector};
Sebastian Noack 2016/02/15 19:48:48 How about returning an object here? That would mak
kzar 2016/02/16 15:05:08 Done.
106 } 106 }
107 107
108 function toRegExp(text) 108 function toRegExp(text)
109 { 109 {
110 let result = ""; 110 let result = "";
111 let lastIndex = text.length - 1; 111 let lastIndex = text.length - 1;
112 112
113 for (let i = 0; i < text.length; i++) 113 for (let i = 0; i < text.length; i++)
114 { 114 {
115 let c = text[i]; 115 let c = text[i];
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 if (!hasNonASCI(rule)) 263 if (!hasNonASCI(rule))
264 rules.push(rule); 264 rules.push(rule);
265 } 265 }
266 266
267 let groupedElemhideFilters = new Map(); 267 let groupedElemhideFilters = new Map();
268 for (let filter of elemhideFilters) 268 for (let filter of elemhideFilters)
269 { 269 {
270 let result = convertElemHideFilter(filter); 270 let result = convertElemHideFilter(filter);
271 if (!result) 271 if (!result)
272 continue; 272 continue;
273 let targetDomains = result[0]; 273
274 let selector = result[1]; 274 if (result.matchDomains.length == 0)
275 275 result.matchDomains = ["^https?://"];
276 if (targetDomains.length == 0) 276
277 targetDomains = ["^https?://"]; 277 for (let matchDomain of result.matchDomains)
278 278 {
279 for (let domain of targetDomains) 279 let group = groupedElemhideFilters.get(matchDomain) || [];
280 { 280 group.push(result.selector);
281 if (!groupedElemhideFilters.has(domain)) 281 groupedElemhideFilters.set(matchDomain, group);
Sebastian Noack 2016/02/15 19:48:48 The additional lookup is redundant. IMO, better:
kzar 2016/02/16 15:05:08 Done, kind of. We're either going to have to perf
282 groupedElemhideFilters.set(domain, []); 282 }
283 groupedElemhideFilters.get(domain).push(selector); 283 }
284 } 284
285 } 285 groupedElemhideFilters.forEach((selectors, matchDomain) =>
286
287 groupedElemhideFilters.forEach((selectors, domain) =>
288 { 286 {
289 while (selectors.length) 287 while (selectors.length)
290 { 288 {
291 addRule({ 289 addRule({
292 trigger: {"url-filter": domain}, 290 trigger: {"url-filter": matchDomain},
Sebastian Noack 2016/02/15 19:48:47 The variable name isn't accurate. It's not a domai
kzar 2016/02/16 15:05:08 Acknowledged.
293 action: {type: "css-display-none"}, 291 action: {type: "css-display-none",
294 selector: selectors.splice(0, selectorLimit).join(", ") 292 selector: selectors.splice(0, selectorLimit).join(", ")}
295 }); 293 });
296 } 294 }
297 }); 295 });
298 296
299 for (let filter of elemhideExceptions) 297 for (let filter of elemhideExceptions)
300 addRule(convertFilter(filter, "ignore-previous-rules", false)); 298 addRule(convertFilter(filter, "ignore-previous-rules", false));
301 299
302 for (let filter of requestFilters) 300 for (let filter of requestFilters)
303 addRule(convertFilter(filter, "block", true)); 301 addRule(convertFilter(filter, "block", true));
304 for (let filter of requestExceptions) 302 for (let filter of requestExceptions)
305 addRule(convertFilter(filter, "ignore-previous-rules", true)); 303 addRule(convertFilter(filter, "ignore-previous-rules", true));
306 304
307 console.log(JSON.stringify(rules, null, "\t")); 305 console.log(JSON.stringify(rules, null, "\t"));
308 } 306 }
309 307
310 let rl = readline.createInterface({input: process.stdin, terminal: false}); 308 let rl = readline.createInterface({input: process.stdin, terminal: false});
311 rl.on("line", parseFilter); 309 rl.on("line", parseFilter);
312 rl.on("close", logRules); 310 rl.on("close", logRules);
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld