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

Side by Side Diff: abp2blocklist.js

Issue 29336349: Issue 3585 - Merge element hiding rules for the same domain (Closed)
Patch Set: Created Feb. 13, 2016, 7:27 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";
2
1 var readline = require("readline"); 3 var readline = require("readline");
2 var punycode = require("punycode"); 4 var punycode = require("punycode");
3 var tldjs = require("tldjs"); 5 var tldjs = require("tldjs");
4 var filterClasses = require("./adblockplus.js"); 6 var filterClasses = require("./adblockplus.js");
5 7
6 var typeMap = filterClasses.RegExpFilter.typeMap; 8 var typeMap = filterClasses.RegExpFilter.typeMap;
7 9
8 var requestFilters = []; 10 var requestFilters = [];
9 var requestExceptions = []; 11 var requestExceptions = [];
10 var elemhideFilters = []; 12 var elemhideFilters = [];
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 93
92 function convertElemHideFilter(filter) 94 function convertElemHideFilter(filter)
93 { 95 {
94 var included = []; 96 var included = [];
95 var excluded = []; 97 var excluded = [];
96 var rules = []; 98 var rules = [];
97 99
98 parseDomains(filter.domains, included, excluded); 100 parseDomains(filter.domains, included, excluded);
99 101
100 if (excluded.length == 0 && !(filter.selector in elemhideSelectorExceptions)) 102 if (excluded.length == 0 && !(filter.selector in elemhideSelectorExceptions))
101 { 103 return [included.map(matchDomain), filter.selector];
102 var action = {
103 type: "css-display-none",
104 selector: filter.selector
105 };
106
107 for (var i = 0; i < included.length; i++)
108 rules.push({
109 trigger: {"url-filter": matchDomain(included[i])},
110 action: action
111 });
112
113 if (included.length == 0)
114 rules.push({
115 trigger: {"url-filter": "^https?://"},
116 action: action
117 });
118 }
119
120 return rules;
121 } 104 }
122 105
123 function toRegExp(text) 106 function toRegExp(text)
124 { 107 {
125 var result = ""; 108 var result = "";
126 var lastIndex = text.length - 1; 109 var lastIndex = text.length - 1;
127 110
128 for (var i = 0; i < text.length; i++) 111 for (var i = 0; i < text.length; i++)
129 { 112 {
130 var c = text[i]; 113 var c = text[i];
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 { 259 {
277 var rules = []; 260 var rules = [];
278 var i; 261 var i;
279 262
280 function addRule(rule) 263 function addRule(rule)
281 { 264 {
282 if (!hasNonASCI(rule)) 265 if (!hasNonASCI(rule))
283 rules.push(rule); 266 rules.push(rule);
284 } 267 }
285 268
286 // HACK: We ignore element hiding filter for now to get the list of 269 let groupedElemhideFilters = new Map();
287 // rules down below 50K. This limit is enforced by iOS and Safari. 270 for (let filter of elemhideFilters)
Sebastian Noack 2016/02/15 14:33:11 Does these ES2015 features even work in node.js wi
kzar 2016/02/15 18:19:20 (As discussed in IRC we will use ES2015 features t
288 // To be undone with https://issues.adblockplus.org/ticket/3585 271 {
272 let result = convertElemHideFilter(filter);
273 if (!result)
274 continue;
275 let targetDomains = result[0];
276 let selector = result[1];
289 277
290 //for (i = 0; i < elemhideFilters.length; i++) 278 if (targetDomains.length == 0)
291 // convertElemHideFilter(elemhideFilters[i]).forEach(addRule); 279 targetDomains = ["^https?://"];
292 //for (i = 0; i < elemhideExceptions.length; i++) 280
293 // addRule(convertFilter(elemhideExceptions[i], "ignore-previous-rules", fals e)); 281 for (let domain of targetDomains)
282 {
283 if (!groupedElemhideFilters.has(domain))
284 groupedElemhideFilters.set(domain, []);
285 groupedElemhideFilters.get(domain).push(selector);
286 }
287 }
288
289 groupedElemhideFilters.forEach((selectors, domain) =>
290 {
291 let rule = {trigger: {"url-filter": domain},
292 action: {type: "css-display-none"}};
293
294 if (selectors.length == 1)
295 rule["action"]["selector"] = selectors[0];
296 else
297 rule["action"]["selector"] = ":matches(" + selectors.join(", ") + ")";
kzar 2016/02/13 19:33:24 Note: I wasn't sure if we need to escape the selec
Sebastian Noack 2016/02/15 14:33:11 Well, if any CSS selector is invalid it will break
kzar 2016/02/15 18:19:20 Acknowledged.
298
299 addRule(rule);
300 });
301
302 for (i = 0; i < elemhideExceptions.length; i++)
303 addRule(convertFilter(elemhideExceptions[i], "ignore-previous-rules", false) );
294 304
295 for (i = 0; i < requestFilters.length; i++) 305 for (i = 0; i < requestFilters.length; i++)
296 addRule(convertFilter(requestFilters[i], "block", true)); 306 addRule(convertFilter(requestFilters[i], "block", true));
297 for (i = 0; i < requestExceptions.length; i++) 307 for (i = 0; i < requestExceptions.length; i++)
298 addRule(convertFilter(requestExceptions[i], "ignore-previous-rules", true)); 308 addRule(convertFilter(requestExceptions[i], "ignore-previous-rules", true));
299 309
300 console.log(JSON.stringify(rules, null, "\t")); 310 console.log(JSON.stringify(rules, null, "\t"));
301 } 311 }
302 312
303 var rl = readline.createInterface({input: process.stdin, terminal: false}); 313 var rl = readline.createInterface({input: process.stdin, terminal: false});
304 rl.on("line", parseFilter); 314 rl.on("line", parseFilter);
305 rl.on("close", logRules); 315 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