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

Delta Between Two Patch Sets: lib/abp2blocklist.js

Issue 29439639: Issue 4329 - Add $generichide support (Closed) Base URL: https://hg.adblockplus.org/abp2blocklist
Left Patch Set: Created May 17, 2017, 3:33 a.m.
Right Patch Set: Fix indentation Created May 20, 2017, 12:20 a.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 | 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 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 { 343 {
344 newSelector.push(selector.substring(i, pos.start)); 344 newSelector.push(selector.substring(i, pos.start));
345 newSelector.push('[id=', selector.substring(pos.start + 1, pos.end), ']'); 345 newSelector.push('[id=', selector.substring(pos.start + 1, pos.end), ']');
346 i = pos.end; 346 i = pos.end;
347 } 347 }
348 newSelector.push(selector.substring(i)); 348 newSelector.push(selector.substring(i));
349 349
350 return newSelector.join(""); 350 return newSelector.join("");
351 } 351 }
352 352
353 function addCSSRules(rules, selectors, matchDomain)
354 {
355 while (selectors.length)
356 {
357 let selector = selectors.splice(0, selectorLimit).join(", ");
358
359 // As of Safari 9.0 element IDs are matched as lowercase. We work around
360 // this by converting to the attribute format [id="elementID"]
361 selector = convertIDSelectorsToAttributeSelectors(selector);
362
363 rules.push({
364 trigger: {"url-filter": matchDomain,
365 "url-filter-is-case-sensitive": true},
366 action: {type: "css-display-none",
367 selector: selector}
368 });
369 }
370 }
371
353 let ContentBlockerList = 372 let ContentBlockerList =
354 /** 373 /**
355 * Create a new Adblock Plus filter to content blocker list converter 374 * Create a new Adblock Plus filter to content blocker list converter
356 * 375 *
357 * @constructor 376 * @constructor
358 */ 377 */
359 exports.ContentBlockerList = function () 378 exports.ContentBlockerList = function ()
360 { 379 {
361 this.requestFilters = []; 380 this.requestFilters = [];
362 this.requestExceptions = []; 381 this.requestExceptions = [];
(...skipping 17 matching lines...) Expand all
380 return; 399 return;
381 400
382 if (filter instanceof filterClasses.BlockingFilter) 401 if (filter instanceof filterClasses.BlockingFilter)
383 this.requestFilters.push(filter); 402 this.requestFilters.push(filter);
384 403
385 if (filter instanceof filterClasses.WhitelistFilter) 404 if (filter instanceof filterClasses.WhitelistFilter)
386 { 405 {
387 if (filter.contentType & (typeMap.DOCUMENT | whitelistableRequestTypes)) 406 if (filter.contentType & (typeMap.DOCUMENT | whitelistableRequestTypes))
388 this.requestExceptions.push(filter); 407 this.requestExceptions.push(filter);
389 408
390 if (filter.contentType & typeMap.ELEMHIDE) 409 if (filter.contentType & typeMap.ELEMHIDE)
391 this.elemhideExceptions.push(filter); 410 this.elemhideExceptions.push(filter);
392 else if (filter.contentType & typeMap.GENERICHIDE) 411 else if (filter.contentType & typeMap.GENERICHIDE)
393 this.generichideExceptions.push(filter); 412 this.generichideExceptions.push(filter);
394 } 413 }
395 414
396 if (filter instanceof filterClasses.ElemHideFilter) 415 if (filter instanceof filterClasses.ElemHideFilter)
397 this.elemhideFilters.push(filter); 416 this.elemhideFilters.push(filter);
398 417
399 if (filter instanceof filterClasses.ElemHideException) 418 if (filter instanceof filterClasses.ElemHideException)
400 { 419 {
401 let domains = this.elemhideSelectorExceptions[filter.selector]; 420 let domains = this.elemhideSelectorExceptions[filter.selector];
402 if (!domains) 421 if (!domains)
403 domains = this.elemhideSelectorExceptions[filter.selector] = []; 422 domains = this.elemhideSelectorExceptions[filter.selector] = [];
404 423
405 parseDomains(filter.domains, domains, []); 424 parseDomains(filter.domains, domains, []);
406 } 425 }
407 }; 426 };
408 427
409 /** 428 /**
410 * Generate content blocker list for all filters that were added 429 * Generate content blocker list for all filters that were added
411 * 430 *
412 * @returns {Filter} filter Filter to convert 431 * @returns {Filter} filter Filter to convert
413 */ 432 */
414 ContentBlockerList.prototype.generateRules = function(filter) 433 ContentBlockerList.prototype.generateRules = function(filter)
415 { 434 {
416 let rules = []; 435 let rules = [];
417 436
437 let genericSelectors = [];
418 let groupedElemhideFilters = new Map(); 438 let groupedElemhideFilters = new Map();
419
420 // Make sure the generic element hiding filters are first in the map so they
421 // get generated first.
422 groupedElemhideFilters.set("^https?://", []);
Sebastian Noack 2017/05/17 06:37:10 I'm not sure if it is a good idea, to split up gen
423 439
424 for (let filter of this.elemhideFilters) 440 for (let filter of this.elemhideFilters)
425 { 441 {
426 let result = convertElemHideFilter(filter, this.elemhideSelectorExceptions); 442 let result = convertElemHideFilter(filter, this.elemhideSelectorExceptions);
427 if (!result) 443 if (!result)
428 continue; 444 continue;
429 445
430 if (result.matchDomains.length == 0) 446 if (result.matchDomains.length == 0)
431 result.matchDomains = ["^https?://"]; 447 {
432 448 genericSelectors.push(result.selector);
433 for (let matchDomain of result.matchDomains) 449 }
434 { 450 else
435 let group = groupedElemhideFilters.get(matchDomain) || []; 451 {
436 group.push(result.selector); 452 for (let matchDomain of result.matchDomains)
437 groupedElemhideFilters.set(matchDomain, group); 453 {
438 } 454 let group = groupedElemhideFilters.get(matchDomain) || [];
439 } 455 group.push(result.selector);
456 groupedElemhideFilters.set(matchDomain, group);
457 }
458 }
459 }
460
461 addCSSRules(rules, genericSelectors, "^https?://");
462
463 // Right after the generic element hiding filters, add the exceptions that
464 // should apply only to those filters.
465 for (let filter of this.generichideExceptions)
466 convertFilterAddRules(rules, filter, "ignore-previous-rules", false);
440 467
441 groupedElemhideFilters.forEach((selectors, matchDomain) => 468 groupedElemhideFilters.forEach((selectors, matchDomain) =>
442 { 469 {
443 while (selectors.length) 470 addCSSRules(rules, selectors, matchDomain);
444 {
445 let selector = selectors.splice(0, selectorLimit).join(", ");
446
447 // As of Safari 9.0 element IDs are matched as lowercase. We work around
448 // this by converting to the attribute format [id="elementID"]
449 selector = convertIDSelectorsToAttributeSelectors(selector);
450
451 rules.push({
452 trigger: {"url-filter": matchDomain,
453 "url-filter-is-case-sensitive": true},
454 action: {type: "css-display-none",
455 selector: selector}
456 });
457 }
458
459 if (matchDomain == "^https?://")
460 {
461 // Right after the generic element hiding filters, add the exceptions
462 // that should apply only to those filters.
463 for (let filter of this.generichideExceptions)
464 convertFilterAddRules(rules, filter, "ignore-previous-rules", false);
465 }
466 }); 471 });
467 472
468 for (let filter of this.elemhideExceptions) 473 for (let filter of this.elemhideExceptions)
469 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); 474 convertFilterAddRules(rules, filter, "ignore-previous-rules", false);
470 for (let filter of this.requestFilters) 475 for (let filter of this.requestFilters)
471 convertFilterAddRules(rules, filter, "block", true); 476 convertFilterAddRules(rules, filter, "block", true);
472 for (let filter of this.requestExceptions) 477 for (let filter of this.requestExceptions)
473 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); 478 convertFilterAddRules(rules, filter, "ignore-previous-rules", true);
474 479
475 return rules.filter(rule => !hasNonASCI(rule)); 480 return rules.filter(rule => !hasNonASCI(rule));
476 }; 481 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld