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

Side by Side Diff: lib/abp2blocklist.js

Issue 29441592: Issue 4329 - Add $genericblock support (Closed) Base URL: https://hg.adblockplus.org/abp2blocklist
Patch Set: Fix bug: Make a copy of the exceptionDomains array Created May 20, 2017, 1:59 a.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 | test/abp2blocklist.js » ('j') | 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-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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 typeMap.OBJECT_SUBREQUEST | 207 typeMap.OBJECT_SUBREQUEST |
208 typeMap.PING | 208 typeMap.PING |
209 typeMap.OTHER)) 209 typeMap.OTHER))
210 types.push("raw"); 210 types.push("raw");
211 if (filter.contentType & typeMap.SUBDOCUMENT) 211 if (filter.contentType & typeMap.SUBDOCUMENT)
212 types.push("document"); 212 types.push("document");
213 213
214 return types; 214 return types;
215 } 215 }
216 216
217 function convertFilterAddRules(rules, filter, action, withResourceTypes) 217 function convertFilterAddRules(rules, filter, action, withResourceTypes,
218 exceptionDomains)
218 { 219 {
219 let parsed = parseFilterRegexpSource(filter.regexpSource); 220 let parsed = parseFilterRegexpSource(filter.regexpSource);
220 221
221 // For the special case of $document whitelisting filters with just a domain 222 // For the special case of $document whitelisting filters with just a domain
222 // we can generate an equivalent blocking rule exception using if-domain. 223 // we can generate an equivalent blocking rule exception using if-domain.
223 if (filter instanceof filterClasses.WhitelistFilter && 224 if (filter instanceof filterClasses.WhitelistFilter &&
224 filter.contentType & typeMap.DOCUMENT && 225 filter.contentType & typeMap.DOCUMENT &&
225 parsed.justHostname) 226 parsed.justHostname)
226 { 227 {
227 rules.push({ 228 rules.push({
(...skipping 17 matching lines...) Expand all
245 246
246 // For rules containing only a hostname we know that we're matching against 247 // For rules containing only a hostname we know that we're matching against
247 // a lowercase string unless the matchCase option was passed. 248 // a lowercase string unless the matchCase option was passed.
248 if (parsed.canSafelyMatchAsLowercase && !filter.matchCase) 249 if (parsed.canSafelyMatchAsLowercase && !filter.matchCase)
249 trigger["url-filter"] = trigger["url-filter"].toLowerCase(); 250 trigger["url-filter"] = trigger["url-filter"].toLowerCase();
250 251
251 if (parsed.canSafelyMatchAsLowercase || filter.matchCase) 252 if (parsed.canSafelyMatchAsLowercase || filter.matchCase)
252 trigger["url-filter-is-case-sensitive"] = true; 253 trigger["url-filter-is-case-sensitive"] = true;
253 254
254 let included = []; 255 let included = [];
255 let excluded = []; 256 let excluded = exceptionDomains && exceptionDomains.slice() || [];
Sebastian Noack 2017/05/20 06:25:28 Nit: The ternary operator seems more appropriate h
Manish Jethani 2017/05/20 19:30:22 Actually now that I think of it, the $genericblock
256 257
257 parseDomains(filter.domains, included, excluded); 258 parseDomains(filter.domains, included, excluded);
258 259
259 if (withResourceTypes) 260 if (withResourceTypes)
260 { 261 {
261 trigger["resource-type"] = getResourceTypes(filter); 262 trigger["resource-type"] = getResourceTypes(filter);
262 263
263 if (trigger["resource-type"].length == 0) 264 if (trigger["resource-type"].length == 0)
264 return; 265 return;
265 } 266 }
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 * Create a new Adblock Plus filter to content blocker list converter 356 * Create a new Adblock Plus filter to content blocker list converter
356 * 357 *
357 * @constructor 358 * @constructor
358 */ 359 */
359 exports.ContentBlockerList = function () 360 exports.ContentBlockerList = function ()
360 { 361 {
361 this.requestFilters = []; 362 this.requestFilters = [];
362 this.requestExceptions = []; 363 this.requestExceptions = [];
363 this.elemhideFilters = []; 364 this.elemhideFilters = [];
364 this.elemhideExceptions = []; 365 this.elemhideExceptions = [];
366 this.genericblockExceptions = [];
365 this.elemhideSelectorExceptions = new Map(); 367 this.elemhideSelectorExceptions = new Map();
366 }; 368 };
367 369
368 /** 370 /**
369 * Add Adblock Plus filter to be converted 371 * Add Adblock Plus filter to be converted
370 * 372 *
371 * @param {Filter} filter Filter to convert 373 * @param {Filter} filter Filter to convert
372 */ 374 */
373 ContentBlockerList.prototype.addFilter = function(filter) 375 ContentBlockerList.prototype.addFilter = function(filter)
374 { 376 {
375 if (filter.sitekeys) 377 if (filter.sitekeys)
376 return; 378 return;
377 if (filter instanceof filterClasses.RegExpFilter && 379 if (filter instanceof filterClasses.RegExpFilter &&
378 filter.regexpSource == null) 380 filter.regexpSource == null)
379 return; 381 return;
380 382
381 if (filter instanceof filterClasses.BlockingFilter) 383 if (filter instanceof filterClasses.BlockingFilter)
382 this.requestFilters.push(filter); 384 this.requestFilters.push(filter);
383 385
384 if (filter instanceof filterClasses.WhitelistFilter) 386 if (filter instanceof filterClasses.WhitelistFilter)
385 { 387 {
386 if (filter.contentType & (typeMap.DOCUMENT | whitelistableRequestTypes)) 388 if (filter.contentType & (typeMap.DOCUMENT | whitelistableRequestTypes))
387 this.requestExceptions.push(filter); 389 this.requestExceptions.push(filter);
388 390
389 if (filter.contentType & typeMap.ELEMHIDE) 391 if (filter.contentType & typeMap.ELEMHIDE)
390 this.elemhideExceptions.push(filter); 392 this.elemhideExceptions.push(filter);
393
394 if (filter.contentType & typeMap.GENERICBLOCK)
395 this.genericblockExceptions.push(filter);
391 } 396 }
392 397
393 if (filter instanceof filterClasses.ElemHideFilter) 398 if (filter instanceof filterClasses.ElemHideFilter)
394 this.elemhideFilters.push(filter); 399 this.elemhideFilters.push(filter);
395 400
396 if (filter instanceof filterClasses.ElemHideException) 401 if (filter instanceof filterClasses.ElemHideException)
397 { 402 {
398 let domains = this.elemhideSelectorExceptions[filter.selector]; 403 let domains = this.elemhideSelectorExceptions[filter.selector];
399 if (!domains) 404 if (!domains)
400 domains = this.elemhideSelectorExceptions[filter.selector] = []; 405 domains = this.elemhideSelectorExceptions[filter.selector] = [];
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 trigger: {"url-filter": matchDomain, 449 trigger: {"url-filter": matchDomain,
445 "url-filter-is-case-sensitive": true}, 450 "url-filter-is-case-sensitive": true},
446 action: {type: "css-display-none", 451 action: {type: "css-display-none",
447 selector: selector} 452 selector: selector}
448 }); 453 });
449 } 454 }
450 }); 455 });
451 456
452 for (let filter of this.elemhideExceptions) 457 for (let filter of this.elemhideExceptions)
453 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); 458 convertFilterAddRules(rules, filter, "ignore-previous-rules", false);
459
460 let requestFilterExceptionDomains = [];
461 for (let filter of this.genericblockExceptions)
462 {
463 let parsed = parseFilterRegexpSource(filter.regexpSource);
464 if (parsed.hostname)
465 requestFilterExceptionDomains.push(parsed.hostname);
466 }
467
454 for (let filter of this.requestFilters) 468 for (let filter of this.requestFilters)
455 convertFilterAddRules(rules, filter, "block", true); 469 {
470 convertFilterAddRules(rules, filter, "block", true,
471 requestFilterExceptionDomains);
472 }
473
456 for (let filter of this.requestExceptions) 474 for (let filter of this.requestExceptions)
457 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); 475 convertFilterAddRules(rules, filter, "ignore-previous-rules", true);
458 476
459 return rules.filter(rule => !hasNonASCI(rule)); 477 return rules.filter(rule => !hasNonASCI(rule));
460 }; 478 };
OLDNEW
« no previous file with comments | « no previous file | test/abp2blocklist.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld