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

Delta Between Two Patch Sets: lib/abp2blocklist.js

Issue 29426594: Issue 3673 - Merge closely matching rules (Closed) Base URL: https://hg.adblockplus.org/abp2blocklist
Left Patch Set: Map async callees asynchronously Created July 28, 2017, 9:07 a.m.
Right Patch Set: Rebase Created July 28, 2017, 1:31 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 | « abp2blocklist.js ('k') | test/abp2blocklist.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 } 116 }
117 } 117 }
118 118
119 function escapeRegExp(s) 119 function escapeRegExp(s)
120 { 120 {
121 return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); 121 return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
122 } 122 }
123 123
124 function matchDomain(domain) 124 function matchDomain(domain)
125 { 125 {
126 if (!domain)
127 return "^https?://";
128
126 return "^https?://([^/:]*\\.)?" + escapeRegExp(domain).toLowerCase() + "[/:]"; 129 return "^https?://([^/:]*\\.)?" + escapeRegExp(domain).toLowerCase() + "[/:]";
127 } 130 }
128 131
129 function getURLSchemes(contentType) 132 function getURLSchemes(contentType)
130 { 133 {
131 // If the given content type includes all supported URL schemes, simply 134 // If the given content type includes all supported URL schemes, simply
132 // return a single generic URL scheme pattern. This minimizes the size of the 135 // return a single generic URL scheme pattern. This minimizes the size of the
133 // generated rule set. The downside to this is that it will also match 136 // generated rule set. The downside to this is that it will also match
134 // schemes that we do not want to match (e.g. "ftp://"), but this can be 137 // schemes that we do not want to match (e.g. "ftp://"), but this can be
135 // mitigated by adding exceptions for those schemes. 138 // mitigated by adding exceptions for those schemes.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 if (parsed.justHostname) 177 if (parsed.justHostname)
175 domains.add(parsed.hostname); 178 domains.add(parsed.hostname);
176 } 179 }
177 return domains; 180 return domains;
178 } 181 }
179 182
180 function convertElemHideFilter(filter, elemhideSelectorExceptions) 183 function convertElemHideFilter(filter, elemhideSelectorExceptions)
181 { 184 {
182 let included = []; 185 let included = [];
183 let excluded = []; 186 let excluded = [];
184 let rules = [];
185 187
186 parseDomains(filter.domains, included, excluded); 188 parseDomains(filter.domains, included, excluded);
187 189
188 if (excluded.length == 0 && !(filter.selector in elemhideSelectorExceptions)) 190 if (excluded.length == 0 && !(filter.selector in elemhideSelectorExceptions))
189 return {matchDomains: included.map(matchDomain), selector: filter.selector}; 191 return {matchDomains: included, selector: filter.selector};
190 } 192 }
191 193
192 /** 194 /**
193 * Parse the given filter "regexpSource" string. Producing a regular expression, 195 * Parse the given filter "regexpSource" string. Producing a regular expression,
194 * extracting the hostname (if any), deciding if the regular expression is safe 196 * extracting the hostname (if any), deciding if the regular expression is safe
195 * to be converted + matched as lower case and noting if the source contains 197 * to be converted + matched as lower case and noting if the source contains
196 * anything after the hostname.) 198 * anything after the hostname.)
197 * 199 *
198 * @param {string} text regexpSource property of a filter 200 * @param {string} text regexpSource property of a filter
199 * @param {string} urlScheme The URL scheme to use in the regular expression 201 * @param {string} urlScheme The URL scheme to use in the regular expression
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 { 633 {
632 newSelector.push(selector.substring(i, pos.start)); 634 newSelector.push(selector.substring(i, pos.start));
633 newSelector.push('[id=', selector.substring(pos.start + 1, pos.end), ']'); 635 newSelector.push('[id=', selector.substring(pos.start + 1, pos.end), ']');
634 i = pos.end; 636 i = pos.end;
635 } 637 }
636 newSelector.push(selector.substring(i)); 638 newSelector.push(selector.substring(i));
637 639
638 return newSelector.join(""); 640 return newSelector.join("");
639 } 641 }
640 642
641 function addCSSRules(rules, selectors, matchDomain, exceptionDomains) 643 function addCSSRules(rules, selectors, domain, exceptionDomains)
642 { 644 {
643 let unlessDomain = exceptionDomains.size > 0 ? [] : null; 645 let unlessDomain = exceptionDomains.size > 0 ? [] : null;
644 646
645 exceptionDomains.forEach(name => unlessDomain.push("*" + name)); 647 exceptionDomains.forEach(name =>
648 {
649 // For domain-specific filters, include the exception domains only if
650 // they're subdomains of the given domain.
651 if (!domain || name.substr(-domain.length - 1) == "." + domain)
652 unlessDomain.push("*" + name);
653 });
646 654
647 while (selectors.length) 655 while (selectors.length)
648 { 656 {
649 let selector = selectors.splice(0, selectorLimit).join(", "); 657 let selector = selectors.splice(0, selectorLimit).join(", ");
650 658
651 // As of Safari 9.0 element IDs are matched as lowercase. We work around 659 // As of Safari 9.0 element IDs are matched as lowercase. We work around
652 // this by converting to the attribute format [id="elementID"] 660 // this by converting to the attribute format [id="elementID"]
653 selector = convertIDSelectorsToAttributeSelectors(selector); 661 selector = convertIDSelectorsToAttributeSelectors(selector);
654 662
655 let rule = { 663 let rule = {
656 trigger: {"url-filter": matchDomain, 664 trigger: {"url-filter": matchDomain(domain),
657 "url-filter-is-case-sensitive": true}, 665 "url-filter-is-case-sensitive": true},
658 action: {type: "css-display-none", 666 action: {type: "css-display-none",
659 selector: selector} 667 selector: selector}
660 }; 668 };
661 669
662 if (unlessDomain) 670 if (unlessDomain)
663 rule.trigger["unless-domain"] = unlessDomain; 671 rule.trigger["unless-domain"] = unlessDomain;
664 672
665 rules.push(rule); 673 rules.push(rule);
666 } 674 }
(...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after
1258 // --max_old_space_size=4096 1266 // --max_old_space_size=4096
1259 let elemhideExceptionDomains = extractFilterDomains(this.elemhideExceptions); 1267 let elemhideExceptionDomains = extractFilterDomains(this.elemhideExceptions);
1260 1268
1261 let genericSelectorExceptionDomains = 1269 let genericSelectorExceptionDomains =
1262 extractFilterDomains(this.generichideExceptions); 1270 extractFilterDomains(this.generichideExceptions);
1263 elemhideExceptionDomains.forEach(name => 1271 elemhideExceptionDomains.forEach(name =>
1264 { 1272 {
1265 genericSelectorExceptionDomains.add(name); 1273 genericSelectorExceptionDomains.add(name);
1266 }); 1274 });
1267 1275
1268 addCSSRules(cssRules, genericSelectors, "^https?://", 1276 addCSSRules(cssRules, genericSelectors, null,
1269 genericSelectorExceptionDomains); 1277 genericSelectorExceptionDomains);
1278
1279 // Filter out whitelisted domains.
1280 elemhideExceptionDomains.forEach(domain =>
1281 groupedElemhideFilters.delete(domain));
1270 1282
1271 groupedElemhideFilters.forEach((selectors, matchDomain) => 1283 groupedElemhideFilters.forEach((selectors, matchDomain) =>
1272 { 1284 {
1273 addCSSRules(cssRules, selectors, matchDomain, elemhideExceptionDomains); 1285 addCSSRules(cssRules, selectors, matchDomain, elemhideExceptionDomains);
1274 }); 1286 });
1275 1287
1276 let requestFilterExceptionDomains = []; 1288 let requestFilterExceptionDomains = [];
1277 for (let filter of this.genericblockExceptions) 1289 for (let filter of this.genericblockExceptions)
1278 { 1290 {
1279 let parsed = parseFilterRegexpSource(filter.regexpSource); 1291 let parsed = parseFilterRegexpSource(filter.regexpSource);
(...skipping 29 matching lines...) Expand all
1309 .then(rules => 1321 .then(rules =>
1310 { 1322 {
1311 ruleGroups[index] = rules; 1323 ruleGroups[index] = rules;
1312 return next(); 1324 return next();
1313 }); 1325 });
1314 } 1326 }
1315 1327
1316 return next(); 1328 return next();
1317 }); 1329 });
1318 }; 1330 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld