OLD | NEW |
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 20 matching lines...) Expand all Loading... |
31 | typeMap.FONT | 31 | typeMap.FONT |
32 | typeMap.MEDIA | 32 | typeMap.MEDIA |
33 | typeMap.POPUP | 33 | typeMap.POPUP |
34 | typeMap.OBJECT | 34 | typeMap.OBJECT |
35 | typeMap.OBJECT_SUBREQUEST | 35 | typeMap.OBJECT_SUBREQUEST |
36 | typeMap.XMLHTTPREQUEST | 36 | typeMap.XMLHTTPREQUEST |
37 | typeMap.PING | 37 | typeMap.PING |
38 | typeMap.SUBDOCUMENT | 38 | typeMap.SUBDOCUMENT |
39 | typeMap.OTHER); | 39 | typeMap.OTHER); |
40 | 40 |
| 41 function callLater(func) |
| 42 { |
| 43 return new Promise(resolve => |
| 44 { |
| 45 let call = () => resolve(func()); |
| 46 |
| 47 // If this looks like Node.js, call process.nextTick, otherwise call |
| 48 // setTimeout. |
| 49 if (typeof process != "undefined") |
| 50 process.nextTick(call); |
| 51 else |
| 52 setTimeout(call, 0); |
| 53 }); |
| 54 } |
| 55 |
| 56 function async(funcs) |
| 57 { |
| 58 if (!Array.isArray(funcs)) |
| 59 funcs = Array.from(arguments); |
| 60 |
| 61 let lastPause = Date.now(); |
| 62 |
| 63 return funcs.reduce((promise, next) => promise.then(() => |
| 64 { |
| 65 // If it has been 100ms or longer since the last call, take a pause. This |
| 66 // keeps the browser from freezing up. |
| 67 let now = Date.now(); |
| 68 if (now - lastPause >= 100) |
| 69 { |
| 70 lastPause = now; |
| 71 return callLater(next); |
| 72 } |
| 73 |
| 74 return next(); |
| 75 }), |
| 76 Promise.resolve()); |
| 77 } |
| 78 |
41 function parseDomains(domains, included, excluded) | 79 function parseDomains(domains, included, excluded) |
42 { | 80 { |
43 for (let domain in domains) | 81 for (let domain in domains) |
44 { | 82 { |
45 if (domain != "") | 83 if (domain != "") |
46 { | 84 { |
47 let enabled = domains[domain]; | 85 let enabled = domains[domain]; |
48 domain = punycode.toASCII(domain.toLowerCase()); | 86 domain = punycode.toASCII(domain.toLowerCase()); |
49 | 87 |
50 if (!enabled) | 88 if (!enabled) |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 { | 397 { |
360 newSelector.push(selector.substring(i, pos.start)); | 398 newSelector.push(selector.substring(i, pos.start)); |
361 newSelector.push('[id=', selector.substring(pos.start + 1, pos.end), ']'); | 399 newSelector.push('[id=', selector.substring(pos.start + 1, pos.end), ']'); |
362 i = pos.end; | 400 i = pos.end; |
363 } | 401 } |
364 newSelector.push(selector.substring(i)); | 402 newSelector.push(selector.substring(i)); |
365 | 403 |
366 return newSelector.join(""); | 404 return newSelector.join(""); |
367 } | 405 } |
368 | 406 |
| 407 /** |
| 408 * Check if two strings are a close match |
| 409 * |
| 410 * This function returns an edit operation, one of "substitute", "delete", and |
| 411 * "insert", along with an index in the source string where the edit must occur |
| 412 * in order to arrive at the target string. If the strings are not a close |
| 413 * match, it returns null. |
| 414 * |
| 415 * Two strings are considered to be a close match if they are one edit |
| 416 * operation apart. |
| 417 * |
| 418 * Deletions or insertions of a contiguous range of characters from one string |
| 419 * into the other, at the same index, are treated as a single edit. For |
| 420 * example, "internal" and "international" are considered to be one edit apart |
| 421 * and therefore a close match. |
| 422 * |
| 423 * A few things to note: |
| 424 * |
| 425 * 1) This function does not care about the format of the input strings. For |
| 426 * example, the caller may pass in regular expressions, where "[ab]" and |
| 427 * "[bc]" could be considered to be a close match, since the order within the |
| 428 * brackets doesn't matter. This function will still return null for this set |
| 429 * of inputs since they are two edits apart. |
| 430 * |
| 431 * 2) To be friendly to calling code that might be passing in regular |
| 432 * expressions, this function will simply return null if it encounters a |
| 433 * special character (e.g. "\", "?", "+", etc.) in the delta. For example, |
| 434 * given "Hello" and "Hello, how are you?", it will return null. |
| 435 * |
| 436 * 3) If the caller does indeed pass in regular expressions, it must make the |
| 437 * important assumption that the parts where two such regular expressions may |
| 438 * differ can always be treated as normal strings. For example, |
| 439 * "^https?://example.com/ads" and "^https?://example.com/adv" differ only in |
| 440 * the last character, therefore the regular expressions can safely be merged |
| 441 * into "^https?://example.com/ad[sv]". |
| 442 * |
| 443 * @param {string} s The source string |
| 444 * @param {string} t The target string |
| 445 * |
| 446 * @returns {object} An object describing the single edit operation that must |
| 447 * occur in the source string in order to arrive at the |
| 448 * target string |
| 449 */ |
| 450 function closeMatch(s, t) |
| 451 { |
| 452 let diff = s.length - t.length; |
| 453 |
| 454 // If target is longer than source, swap them for the purpose of our |
| 455 // calculation. |
| 456 if (diff < 0) |
| 457 { |
| 458 let tmp = s; |
| 459 s = t; |
| 460 t = tmp; |
| 461 } |
| 462 |
| 463 let edit = null; |
| 464 |
| 465 let i = 0, j = 0; |
| 466 |
| 467 // Start from the beginning and keep going until we hit a character that |
| 468 // doesn't match. |
| 469 for (; i < s.length; i++) |
| 470 { |
| 471 if (s[i] != t[i]) |
| 472 break; |
| 473 } |
| 474 |
| 475 // Now do exactly the same from the end, but also stop if we reach the |
| 476 // position where we terminated the previous loop. |
| 477 for (; j < t.length; j++) |
| 478 { |
| 479 if (t.length - j == i || s[s.length - j - 1] != t[t.length - j - 1]) |
| 480 break; |
| 481 } |
| 482 |
| 483 if (diff == 0) |
| 484 { |
| 485 // If the strings are equal in length and the delta isn't exactly one |
| 486 // character, it's not a close match. |
| 487 if (t.length - j - i != 1) |
| 488 return null; |
| 489 } |
| 490 else if (i != t.length - j) |
| 491 { |
| 492 // For strings of unequal length, if we haven't found a match for every |
| 493 // single character in the shorter string counting from both the beginning |
| 494 // and the end, it's not a close match. |
| 495 return null; |
| 496 } |
| 497 |
| 498 for (let k = i; k < s.length - j; k++) |
| 499 { |
| 500 // If the delta contains any special characters, it's not a close match. |
| 501 if (s[k] == "." || s[k] == "+" || s[k] == "$" || s[k] == "?" || |
| 502 s[k] == "{" || s[k] == "}" || s[k] == "(" || s[k] == ")" || |
| 503 s[k] == "[" || s[k] == "]" || s[k] == "\\") |
| 504 return null; |
| 505 } |
| 506 |
| 507 if (diff == 0) |
| 508 { |
| 509 edit = {type: "substitute", index: i}; |
| 510 } |
| 511 else if (diff > 0) |
| 512 { |
| 513 edit = {type: "delete", index: i}; |
| 514 |
| 515 if (diff > 1) |
| 516 edit.endIndex = s.length - j; |
| 517 } |
| 518 else |
| 519 { |
| 520 edit = {type: "insert", index: i}; |
| 521 |
| 522 if (diff < -1) |
| 523 edit.endIndex = s.length - j; |
| 524 } |
| 525 |
| 526 return edit; |
| 527 } |
| 528 |
| 529 function eliminateRedundantRulesByURLFilter(rulesInfo) |
| 530 { |
| 531 for (let i = 0; i < rulesInfo.length; i++) |
| 532 { |
| 533 // If this rule is already marked as redundant, don't bother comparing it |
| 534 // with other rules. |
| 535 if (rulesInfo[i].redundant) |
| 536 continue; |
| 537 |
| 538 for (let j = i + 1; j < rulesInfo.length; j++) |
| 539 { |
| 540 if (rulesInfo[j].redundant) |
| 541 continue; |
| 542 |
| 543 let source = rulesInfo[i].rule.trigger["url-filter"]; |
| 544 let target = rulesInfo[j].rule.trigger["url-filter"]; |
| 545 |
| 546 if (source.length >= target.length) |
| 547 { |
| 548 // If one URL filter is a substring of the other starting at the |
| 549 // beginning, the other one is clearly redundant. |
| 550 if (source.substring(0, target.length) == target) |
| 551 { |
| 552 rulesInfo[i].redundant = true; |
| 553 break; |
| 554 } |
| 555 } |
| 556 else if (target.substring(0, source.length) == source) |
| 557 { |
| 558 rulesInfo[j].redundant = true; |
| 559 } |
| 560 } |
| 561 } |
| 562 |
| 563 return rulesInfo.filter(ruleInfo => !ruleInfo.redundant); |
| 564 } |
| 565 |
| 566 function mergeRulesByURLFilter(rulesInfo, exhaustive) |
| 567 { |
| 568 // Closely matching rules are likely to be within a certain range. We only |
| 569 // look for matches within this range by default. If we increase this value, |
| 570 // it can give us more matches and a smaller resulting rule set, but possibly |
| 571 // at a significant performance cost. |
| 572 // |
| 573 // If the exhaustive option is true, we simply ignore this value and look for |
| 574 // matches throughout the rule set. |
| 575 const heuristicRange = 10; |
| 576 |
| 577 return async(() => |
| 578 { |
| 579 if (exhaustive) |
| 580 { |
| 581 // Throw out obviously redundant rules. |
| 582 rulesInfo = eliminateRedundantRulesByURLFilter(rulesInfo); |
| 583 } |
| 584 }) |
| 585 .then(() => |
| 586 { |
| 587 if (rulesInfo.length <= 1) |
| 588 return; |
| 589 |
| 590 return async(rulesInfo.map((_, i) => () => |
| 591 { |
| 592 let limit = exhaustive ? rulesInfo.length : |
| 593 Math.min(i + heuristicRange, rulesInfo.length); |
| 594 |
| 595 for (let j = i + 1; j < limit; j++) |
| 596 { |
| 597 let source = rulesInfo[i].rule.trigger["url-filter"]; |
| 598 let target = rulesInfo[j].rule.trigger["url-filter"]; |
| 599 |
| 600 let edit = closeMatch(source, target); |
| 601 |
| 602 if (edit) |
| 603 { |
| 604 let urlFilter, ruleInfo, match = {edit}; |
| 605 |
| 606 if (edit.type == "insert") |
| 607 { |
| 608 // Convert the insertion into a deletion and stick it on the target |
| 609 // rule instead. We can only group deletions and substitutions; |
| 610 // therefore insertions must be treated as deletions on the target |
| 611 // rule. |
| 612 urlFilter = target; |
| 613 ruleInfo = rulesInfo[j]; |
| 614 match.index = i; |
| 615 edit.type = "delete"; |
| 616 } |
| 617 else |
| 618 { |
| 619 urlFilter = source; |
| 620 ruleInfo = rulesInfo[i]; |
| 621 match.index = j; |
| 622 } |
| 623 |
| 624 // If the edit has an end index, it represents a multiple character |
| 625 // edit. |
| 626 let multiEdit = !!edit.endIndex; |
| 627 |
| 628 if (multiEdit) |
| 629 { |
| 630 // We only care about a single multiple character edit because the |
| 631 // number of characters for such a match doesn't matter, we can |
| 632 // only merge with one other rule. |
| 633 if (!ruleInfo.multiEditMatch) |
| 634 ruleInfo.multiEditMatch = match; |
| 635 } |
| 636 else |
| 637 { |
| 638 // For single character edits, multiple rules can be merged into |
| 639 // one. e.g. "ad", "ads", and "adv" can be merged into "ad[sv]?". |
| 640 if (!ruleInfo.matches) |
| 641 ruleInfo.matches = new Array(urlFilter.length); |
| 642 |
| 643 // Matches at a particular index. For example, for a source string |
| 644 // "ads", both target strings "ad" (deletion) and "adv" |
| 645 // (substitution) match at index 2, hence they are grouped together |
| 646 // to possibly be merged later into "ad[sv]?". |
| 647 let matchesForIndex = ruleInfo.matches[edit.index]; |
| 648 |
| 649 if (matchesForIndex) |
| 650 { |
| 651 matchesForIndex.push(match); |
| 652 } |
| 653 else |
| 654 { |
| 655 matchesForIndex = [match]; |
| 656 ruleInfo.matches[edit.index] = matchesForIndex; |
| 657 } |
| 658 |
| 659 // Keep track of the best set of matches. We later sort by this to |
| 660 // get best results. |
| 661 if (!ruleInfo.bestMatches || |
| 662 matchesForIndex.length > ruleInfo.bestMatches.length) |
| 663 ruleInfo.bestMatches = matchesForIndex; |
| 664 } |
| 665 } |
| 666 } |
| 667 })); |
| 668 }) |
| 669 .then(() => |
| 670 { |
| 671 // Filter out rules that have no matches at all. |
| 672 let candidateRulesInfo = rulesInfo.filter(ruleInfo => |
| 673 { |
| 674 return ruleInfo.bestMatches || ruleInfo.multiEditMatch |
| 675 }); |
| 676 |
| 677 // For best results, we have to sort the candidates by the largest set of |
| 678 // matches. |
| 679 // |
| 680 // For example, we want "ads", "bds", "adv", "bdv", "adx", and "bdx" to |
| 681 // generate "ad[svx]" and "bd[svx]" (2 rules), not "[ab]ds", "[ab]dv", and |
| 682 // "[ab]dx" (3 rules). |
| 683 candidateRulesInfo.sort((ruleInfo1, ruleInfo2) => |
| 684 { |
| 685 let weight1 = ruleInfo1.bestMatches ? ruleInfo1.bestMatches.length : |
| 686 ruleInfo1.multiEditMatch ? 1 : 0; |
| 687 let weight2 = ruleInfo2.bestMatches ? ruleInfo2.bestMatches.length : |
| 688 ruleInfo2.multiEditMatch ? 1 : 0; |
| 689 |
| 690 return weight2 - weight1; |
| 691 }); |
| 692 |
| 693 for (let ruleInfo of candidateRulesInfo) |
| 694 { |
| 695 let rule = ruleInfo.rule; |
| 696 |
| 697 // If this rule has already been merged into another rule, we skip it. |
| 698 if (ruleInfo.merged) |
| 699 continue; |
| 700 |
| 701 // Find the best set of rules to group, which is simply the largest set. |
| 702 let best = (ruleInfo.matches || []).reduce((best, matchesForIndex) => |
| 703 { |
| 704 matchesForIndex = (matchesForIndex || []).filter(match => |
| 705 { |
| 706 // Filter out rules that have either already been merged into other |
| 707 // rules or have had other rules merged into them. |
| 708 return !rulesInfo[match.index].merged && |
| 709 !rulesInfo[match.index].mergedInto; |
| 710 }); |
| 711 |
| 712 return matchesForIndex.length > best.length ? matchesForIndex : best; |
| 713 }, |
| 714 []); |
| 715 |
| 716 let multiEdit = false; |
| 717 |
| 718 // If we couldn't find a single rule to merge with, let's see if we have a |
| 719 // multiple character edit. e.g. we could merge "ad" and "adserver" into |
| 720 // "ad(server)?". |
| 721 if (best.length == 0 && ruleInfo.multiEditMatch && |
| 722 !rulesInfo[ruleInfo.multiEditMatch.index].merged && |
| 723 !rulesInfo[ruleInfo.multiEditMatch.index].mergedInto) |
| 724 { |
| 725 best = [ruleInfo.multiEditMatch]; |
| 726 multiEdit = true; |
| 727 } |
| 728 |
| 729 if (best.length > 0) |
| 730 { |
| 731 let urlFilter = rule.trigger["url-filter"]; |
| 732 |
| 733 let editIndex = best[0].edit.index; |
| 734 |
| 735 if (!multiEdit) |
| 736 { |
| 737 // Merge all the matching rules into this one. |
| 738 |
| 739 let characters = []; |
| 740 let quantifier = ""; |
| 741 |
| 742 for (let match of best) |
| 743 { |
| 744 if (match.edit.type == "delete") |
| 745 { |
| 746 quantifier = "?"; |
| 747 } |
| 748 else |
| 749 { |
| 750 let character = rulesInfo[match.index].rule |
| 751 .trigger["url-filter"][editIndex]; |
| 752 characters.push(character); |
| 753 } |
| 754 |
| 755 // Mark the target rule as merged so other rules don't try to merge |
| 756 // it again. |
| 757 rulesInfo[match.index].merged = true; |
| 758 } |
| 759 |
| 760 urlFilter = urlFilter.substring(0, editIndex + 1) + quantifier + |
| 761 urlFilter.substring(editIndex + 1); |
| 762 if (characters.length > 0) |
| 763 { |
| 764 urlFilter = urlFilter.substring(0, editIndex) + "[" + |
| 765 urlFilter[editIndex] + characters.join("") + "]" + |
| 766 urlFilter.substring(editIndex + 1); |
| 767 } |
| 768 } |
| 769 else |
| 770 { |
| 771 let editEndIndex = best[0].edit.endIndex; |
| 772 |
| 773 // Mark the target rule as merged so other rules don't try to merge it |
| 774 // again. |
| 775 rulesInfo[best[0].index].merged = true; |
| 776 |
| 777 urlFilter = urlFilter.substring(0, editIndex) + "(" + |
| 778 urlFilter.substring(editIndex, editEndIndex) + ")?" + |
| 779 urlFilter.substring(editEndIndex); |
| 780 } |
| 781 |
| 782 rule.trigger["url-filter"] = urlFilter; |
| 783 |
| 784 // Mark this rule as one that has had other rules merged into it. |
| 785 ruleInfo.mergedInto = true; |
| 786 } |
| 787 } |
| 788 }); |
| 789 } |
| 790 |
| 791 function mergeRulesByArrayProperty(rulesInfo, propertyType, property) |
| 792 { |
| 793 if (rulesInfo.length <= 1) |
| 794 return; |
| 795 |
| 796 let oneRuleInfo = rulesInfo.shift(); |
| 797 let valueSet = new Set(oneRuleInfo.rule[propertyType][property]); |
| 798 |
| 799 for (let ruleInfo of rulesInfo) |
| 800 { |
| 801 if (ruleInfo.rule[propertyType][property]) |
| 802 { |
| 803 for (let value of ruleInfo.rule[propertyType][property]) |
| 804 valueSet.add(value); |
| 805 } |
| 806 |
| 807 ruleInfo.merged = true; |
| 808 } |
| 809 |
| 810 if (valueSet.size > 0) |
| 811 oneRuleInfo.rule[propertyType][property] = Array.from(valueSet); |
| 812 |
| 813 oneRuleInfo.mergedInto = true; |
| 814 } |
| 815 |
| 816 function groupRulesByMergeableProperty(rulesInfo, propertyType, property) |
| 817 { |
| 818 let mergeableRulesInfoByGroup = new Map(); |
| 819 |
| 820 for (let ruleInfo of rulesInfo) |
| 821 { |
| 822 let copy = { |
| 823 trigger: Object.assign({}, ruleInfo.rule.trigger), |
| 824 action: Object.assign({}, ruleInfo.rule.action) |
| 825 }; |
| 826 |
| 827 delete copy[propertyType][property]; |
| 828 |
| 829 let groupKey = JSON.stringify(copy); |
| 830 |
| 831 let mergeableRulesInfo = mergeableRulesInfoByGroup.get(groupKey); |
| 832 |
| 833 if (mergeableRulesInfo) |
| 834 mergeableRulesInfo.push(ruleInfo); |
| 835 else |
| 836 mergeableRulesInfoByGroup.set(groupKey, [ruleInfo]); |
| 837 } |
| 838 |
| 839 return mergeableRulesInfoByGroup; |
| 840 } |
| 841 |
| 842 function mergeRules(rules, exhaustive) |
| 843 { |
| 844 let rulesInfo = rules.map(rule => ({rule})); |
| 845 |
| 846 return async(() => |
| 847 { |
| 848 let map = groupRulesByMergeableProperty(rulesInfo, "trigger", "url-filter"); |
| 849 return async(Array.from(map.values()).map(mergeableRulesInfo => () => |
| 850 { |
| 851 if (mergeableRulesInfo.length > 1) |
| 852 return mergeRulesByURLFilter(mergeableRulesInfo, exhaustive); |
| 853 })); |
| 854 }) |
| 855 .then(() => |
| 856 { |
| 857 // Filter out rules that are redundant or have been merged into other rules. |
| 858 rulesInfo = rulesInfo.filter(ruleInfo => !ruleInfo.redundant && |
| 859 !ruleInfo.merged); |
| 860 }) |
| 861 .then(() => async(["resource-type", "if-domain"].map(arrayProperty => () => |
| 862 { |
| 863 let map = groupRulesByMergeableProperty(rulesInfo, "trigger", |
| 864 arrayProperty); |
| 865 return async(Array.from(map.values()).map(mergeableRulesInfo => () => |
| 866 { |
| 867 if (mergeableRulesInfo.length > 1) |
| 868 mergeRulesByArrayProperty(mergeableRulesInfo, "trigger", arrayProperty); |
| 869 })) |
| 870 .then(() => |
| 871 { |
| 872 rulesInfo = rulesInfo.filter(ruleInfo => !ruleInfo.merged); |
| 873 }); |
| 874 }))) |
| 875 .then(() => rulesInfo.map(ruleInfo => ruleInfo.rule)); |
| 876 } |
| 877 |
369 let ContentBlockerList = | 878 let ContentBlockerList = |
370 /** | 879 /** |
371 * Create a new Adblock Plus filter to content blocker list converter | 880 * Create a new Adblock Plus filter to content blocker list converter |
372 * | 881 * |
| 882 * @param {object} options Options for content blocker list generation |
| 883 * |
373 * @constructor | 884 * @constructor |
374 */ | 885 */ |
375 exports.ContentBlockerList = function () | 886 exports.ContentBlockerList = function(options) |
376 { | 887 { |
| 888 const defaultOptions = { |
| 889 merge: false, |
| 890 exhaustiveMerge: false |
| 891 }; |
| 892 |
| 893 this.options = Object.assign({}, defaultOptions, options); |
| 894 |
377 this.requestFilters = []; | 895 this.requestFilters = []; |
378 this.requestExceptions = []; | 896 this.requestExceptions = []; |
379 this.elemhideFilters = []; | 897 this.elemhideFilters = []; |
380 this.elemhideExceptions = []; | 898 this.elemhideExceptions = []; |
381 this.elemhideSelectorExceptions = new Map(); | 899 this.elemhideSelectorExceptions = new Map(); |
382 }; | 900 }; |
383 | 901 |
384 /** | 902 /** |
385 * Add Adblock Plus filter to be converted | 903 * Add Adblock Plus filter to be converted |
386 * | 904 * |
(...skipping 27 matching lines...) Expand all Loading... |
414 let domains = this.elemhideSelectorExceptions[filter.selector]; | 932 let domains = this.elemhideSelectorExceptions[filter.selector]; |
415 if (!domains) | 933 if (!domains) |
416 domains = this.elemhideSelectorExceptions[filter.selector] = []; | 934 domains = this.elemhideSelectorExceptions[filter.selector] = []; |
417 | 935 |
418 parseDomains(filter.domains, domains, []); | 936 parseDomains(filter.domains, domains, []); |
419 } | 937 } |
420 }; | 938 }; |
421 | 939 |
422 /** | 940 /** |
423 * Generate content blocker list for all filters that were added | 941 * Generate content blocker list for all filters that were added |
424 * | |
425 * @returns {Filter} filter Filter to convert | |
426 */ | 942 */ |
427 ContentBlockerList.prototype.generateRules = function(filter) | 943 ContentBlockerList.prototype.generateRules = function() |
428 { | 944 { |
429 let rules = []; | 945 let cssRules = []; |
| 946 let cssExceptionRules = []; |
| 947 let blockingRules = []; |
| 948 let blockingExceptionRules = []; |
| 949 |
| 950 let ruleGroups = [cssRules, cssExceptionRules, |
| 951 blockingRules, blockingExceptionRules]; |
430 | 952 |
431 let groupedElemhideFilters = new Map(); | 953 let groupedElemhideFilters = new Map(); |
432 for (let filter of this.elemhideFilters) | 954 for (let filter of this.elemhideFilters) |
433 { | 955 { |
434 let result = convertElemHideFilter(filter, this.elemhideSelectorExceptions); | 956 let result = convertElemHideFilter(filter, this.elemhideSelectorExceptions); |
435 if (!result) | 957 if (!result) |
436 continue; | 958 continue; |
437 | 959 |
438 if (result.matchDomains.length == 0) | 960 if (result.matchDomains.length == 0) |
439 result.matchDomains = ["^https?://"]; | 961 result.matchDomains = ["^https?://"]; |
440 | 962 |
441 for (let matchDomain of result.matchDomains) | 963 for (let matchDomain of result.matchDomains) |
442 { | 964 { |
443 let group = groupedElemhideFilters.get(matchDomain) || []; | 965 let group = groupedElemhideFilters.get(matchDomain) || []; |
444 group.push(result.selector); | 966 group.push(result.selector); |
445 groupedElemhideFilters.set(matchDomain, group); | 967 groupedElemhideFilters.set(matchDomain, group); |
446 } | 968 } |
447 } | 969 } |
448 | 970 |
449 groupedElemhideFilters.forEach((selectors, matchDomain) => | 971 groupedElemhideFilters.forEach((selectors, matchDomain) => |
450 { | 972 { |
451 while (selectors.length) | 973 while (selectors.length) |
452 { | 974 { |
453 let selector = selectors.splice(0, selectorLimit).join(", "); | 975 let selector = selectors.splice(0, selectorLimit).join(", "); |
454 | 976 |
455 // As of Safari 9.0 element IDs are matched as lowercase. We work around | 977 // As of Safari 9.0 element IDs are matched as lowercase. We work around |
456 // this by converting to the attribute format [id="elementID"] | 978 // this by converting to the attribute format [id="elementID"] |
457 selector = convertIDSelectorsToAttributeSelectors(selector); | 979 selector = convertIDSelectorsToAttributeSelectors(selector); |
458 | 980 |
459 rules.push({ | 981 cssRules.push({ |
460 trigger: {"url-filter": matchDomain, | 982 trigger: {"url-filter": matchDomain, |
461 "url-filter-is-case-sensitive": true}, | 983 "url-filter-is-case-sensitive": true}, |
462 action: {type: "css-display-none", | 984 action: {type: "css-display-none", |
463 selector: selector} | 985 selector: selector} |
464 }); | 986 }); |
465 } | 987 } |
466 }); | 988 }); |
467 | 989 |
468 for (let filter of this.elemhideExceptions) | 990 for (let filter of this.elemhideExceptions) |
469 convertFilterAddRules(rules, filter, "ignore-previous-rules", false); | 991 { |
| 992 convertFilterAddRules(cssExceptionRules, filter, |
| 993 "ignore-previous-rules", false); |
| 994 } |
| 995 |
470 for (let filter of this.requestFilters) | 996 for (let filter of this.requestFilters) |
471 convertFilterAddRules(rules, filter, "block", true); | 997 convertFilterAddRules(blockingRules, filter, "block", true); |
| 998 |
472 for (let filter of this.requestExceptions) | 999 for (let filter of this.requestExceptions) |
473 convertFilterAddRules(rules, filter, "ignore-previous-rules", true); | 1000 { |
| 1001 convertFilterAddRules(blockingExceptionRules, filter, |
| 1002 "ignore-previous-rules", true); |
| 1003 } |
474 | 1004 |
475 return rules.filter(rule => !hasNonASCI(rule)); | 1005 return async(ruleGroups.map((group, index) => () => |
| 1006 { |
| 1007 let next = () => |
| 1008 { |
| 1009 if (index == ruleGroups.length - 1) |
| 1010 return ruleGroups.reduce((all, rules) => all.concat(rules), []); |
| 1011 }; |
| 1012 |
| 1013 ruleGroups[index] = ruleGroups[index].filter(rule => !hasNonASCI(rule)); |
| 1014 |
| 1015 if (this.options.merge) |
| 1016 { |
| 1017 return mergeRules(ruleGroups[index], this.options.exhaustiveMerge) |
| 1018 .then(rules => |
| 1019 { |
| 1020 ruleGroups[index] = rules; |
| 1021 return next(); |
| 1022 }); |
| 1023 } |
| 1024 |
| 1025 return next(); |
| 1026 })); |
476 }; | 1027 }; |
OLD | NEW |