| LEFT | RIGHT |
| 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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 "use strict"; | 18 "use strict"; |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * @fileOverview Definition of Filter class and its subclasses. | 21 * @fileOverview Definition of Filter class and its subclasses. |
| 22 */ | 22 */ |
| 23 | 23 |
| 24 const {filterNotifier} = require("./filterNotifier"); | 24 const {filterNotifier} = require("./filterNotifier"); |
| 25 const {extend} = require("./coreUtils"); | 25 const {extend} = require("./coreUtils"); |
| 26 const {filterToRegExp} = require("./common"); | 26 const {filterToRegExp} = require("./common"); |
| 27 | 27 |
| 28 let tripleAnchorRegExp = new RegExp(filterToRegExp("|||")); | 28 /** |
| 29 * Regular expression used to match the <code>||</code> prefix in an otherwise |
| 30 * literal pattern. |
| 31 * @type {RegExp} |
| 32 */ |
| 33 let doubleAnchorRegExp = new RegExp(filterToRegExp("||") + "$"); |
| 29 | 34 |
| 30 /** | 35 /** |
| 31 * All known unique domain sources mapped to their parsed values. | 36 * All known unique domain sources mapped to their parsed values. |
| 32 * @type {Map.<string,Map.<string,boolean>>} | 37 * @type {Map.<string,Map.<string,boolean>>} |
| 33 */ | 38 */ |
| 34 let knownDomainMaps = new Map(); | 39 let knownDomainMaps = new Map(); |
| 40 |
| 41 /** |
| 42 * Checks whether the given pattern is a string of literal characters with no |
| 43 * wildcards or any other special characters. If the pattern is prefixed with a |
| 44 * <code>||</code> but otherwise contains no special characters, it is still |
| 45 * considered to be a literal pattern. |
| 46 * @param {string} pattern |
| 47 * @returns {boolean} |
| 48 */ |
| 49 function isLiteralPattern(pattern) |
| 50 { |
| 51 return !/[*^|]/.test(pattern.replace(/^\|{2}/, "")); |
| 52 } |
| 35 | 53 |
| 36 /** | 54 /** |
| 37 * Abstract base class for filters | 55 * Abstract base class for filters |
| 38 * | 56 * |
| 39 * @param {string} text string representation of the filter | 57 * @param {string} text string representation of the filter |
| 40 * @constructor | 58 * @constructor |
| 41 */ | 59 */ |
| 42 function Filter(text) | 60 function Filter(text) |
| 43 { | 61 { |
| 44 this.text = text; | 62 this.text = text; |
| (...skipping 614 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 659 * @param {boolean} [matchCase] | 677 * @param {boolean} [matchCase] |
| 660 * Defines whether the filter should distinguish between lower and upper case | 678 * Defines whether the filter should distinguish between lower and upper case |
| 661 * letters | 679 * letters |
| 662 * @param {string} [domains] | 680 * @param {string} [domains] |
| 663 * Domains that the filter is restricted to, e.g. "foo.com|bar.com|~baz.com" | 681 * Domains that the filter is restricted to, e.g. "foo.com|bar.com|~baz.com" |
| 664 * @param {boolean} [thirdParty] | 682 * @param {boolean} [thirdParty] |
| 665 * Defines whether the filter should apply to third-party or first-party | 683 * Defines whether the filter should apply to third-party or first-party |
| 666 * content only | 684 * content only |
| 667 * @param {string} [sitekeys] | 685 * @param {string} [sitekeys] |
| 668 * Public keys of websites that this filter should apply to | 686 * Public keys of websites that this filter should apply to |
| 669 * @param {?string} [rewrite] | 687 * @constructor |
| 670 * The (optional) rule specifying how to rewrite the URL. | |
| 671 * @augments ActiveFilter | 688 * @augments ActiveFilter |
| 672 */ | 689 */ |
| 673 function RegExpFilter(text, regexpSource, contentType, matchCase, domains, | 690 function RegExpFilter(text, regexpSource, contentType, matchCase, domains, |
| 674 thirdParty, sitekeys, rewrite) | 691 thirdParty, sitekeys) |
| 675 { | 692 { |
| 676 ActiveFilter.call(this, text, domains); | 693 ActiveFilter.call(this, text, domains); |
| 677 | 694 |
| 678 if (contentType != null) | 695 if (contentType != null) |
| 679 this.contentType = contentType; | 696 this.contentType = contentType; |
| 680 if (matchCase) | 697 if (matchCase) |
| 681 this.matchCase = matchCase; | 698 this.matchCase = matchCase; |
| 682 if (thirdParty != null) | 699 if (thirdParty != null) |
| 683 this.thirdParty = thirdParty; | 700 this.thirdParty = thirdParty; |
| 684 if (sitekeys != null) | 701 if (sitekeys != null) |
| 685 this.sitekeySource = sitekeys; | 702 this.sitekeySource = sitekeys; |
| 686 | 703 |
| 687 if (regexpSource.length >= 2 && | 704 if (regexpSource.length >= 2 && |
| 688 regexpSource[0] == "/" && | 705 regexpSource[0] == "/" && |
| 689 regexpSource[regexpSource.length - 1] == "/") | 706 regexpSource[regexpSource.length - 1] == "/") |
| 690 { | 707 { |
| 691 // The filter is a regular expression - convert it immediately to | 708 // The filter is a regular expression - convert it immediately to |
| 692 // catch syntax errors | 709 // catch syntax errors |
| 693 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), | 710 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), |
| 694 this.matchCase ? "" : "i"); | 711 this.matchCase ? "" : "i"); |
| 695 Object.defineProperty(this, "regexp", {value: regexp}); | 712 Object.defineProperty(this, "regexp", {value: regexp}); |
| 696 } | 713 } |
| 697 else | 714 else |
| 698 { | 715 { |
| 716 if (!this.matchCase && isLiteralPattern(regexpSource)) |
| 717 regexpSource = regexpSource.toLowerCase(); |
| 718 |
| 699 // No need to convert this filter to regular expression yet, do it on demand | 719 // No need to convert this filter to regular expression yet, do it on demand |
| 700 this.pattern = regexpSource; | 720 this.pattern = regexpSource; |
| 701 | |
| 702 if (!this.matchCase && rewrite == null && | |
| 703 !/[*^|]/.test(this.pattern.replace(/^\|{2}/, ""))) | |
| 704 { | |
| 705 this.pattern = this.pattern.toLowerCase(); | |
| 706 } | |
| 707 } | 721 } |
| 708 } | 722 } |
| 709 exports.RegExpFilter = RegExpFilter; | 723 exports.RegExpFilter = RegExpFilter; |
| 710 | 724 |
| 711 RegExpFilter.prototype = extend(ActiveFilter, { | 725 RegExpFilter.prototype = extend(ActiveFilter, { |
| 712 /** | 726 /** |
| 713 * Number of filters contained, will always be 1 (required to | 727 * Number of filters contained, will always be 1 (required to |
| 714 * optimize {@link Matcher}). | 728 * optimize {@link Matcher}). |
| 715 * @type {number} | 729 * @type {number} |
| 716 */ | 730 */ |
| (...skipping 12 matching lines...) Expand all Loading... |
| 729 pattern: null, | 743 pattern: null, |
| 730 /** | 744 /** |
| 731 * Regular expression to be used when testing against this filter | 745 * Regular expression to be used when testing against this filter |
| 732 * @type {RegExp} | 746 * @type {RegExp} |
| 733 */ | 747 */ |
| 734 get regexp() | 748 get regexp() |
| 735 { | 749 { |
| 736 let value = null; | 750 let value = null; |
| 737 | 751 |
| 738 let {pattern, rewrite} = this; | 752 let {pattern, rewrite} = this; |
| 739 if (rewrite != null || /[*^|]/.test(pattern.replace(/^\|{2}/, ""))) | 753 if (rewrite != null || !isLiteralPattern(pattern)) |
| 740 { | 754 { |
| 741 value = new RegExp(filterToRegExp(pattern, rewrite != null), | 755 value = new RegExp(filterToRegExp(pattern, rewrite != null), |
| 742 this.matchCase ? "" : "i"); | 756 this.matchCase ? "" : "i"); |
| 743 } | 757 } |
| 744 | 758 |
| 745 Object.defineProperty(this, "regexp", {value}); | 759 Object.defineProperty(this, "regexp", {value}); |
| 746 return value; | 760 return value; |
| 747 }, | 761 }, |
| 748 /** | 762 /** |
| 749 * Content types the filter applies to, combination of values from | 763 * Content types the filter applies to, combination of values from |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 800 * @return {boolean} true in case of a match | 814 * @return {boolean} true in case of a match |
| 801 */ | 815 */ |
| 802 matches(location, typeMask, docDomain, thirdParty, sitekey) | 816 matches(location, typeMask, docDomain, thirdParty, sitekey) |
| 803 { | 817 { |
| 804 return (this.contentType & typeMask) != 0 && | 818 return (this.contentType & typeMask) != 0 && |
| 805 (this.thirdParty == null || this.thirdParty == thirdParty) && | 819 (this.thirdParty == null || this.thirdParty == thirdParty) && |
| 806 this.isActiveOnDomain(docDomain, sitekey) && | 820 this.isActiveOnDomain(docDomain, sitekey) && |
| 807 this.matchesLocation(location); | 821 this.matchesLocation(location); |
| 808 }, | 822 }, |
| 809 | 823 |
| 824 /** |
| 825 * Checks whether the given URL matches this filter's pattern. |
| 826 * @param {string} location The URL to check. |
| 827 * @returns {boolean} <code>true</code> if the URL matches. |
| 828 */ |
| 810 matchesLocation(location) | 829 matchesLocation(location) |
| 811 { | 830 { |
| 812 let {regexp} = this; | 831 let {regexp} = this; |
| 813 | 832 |
| 814 if (regexp) | 833 if (regexp) |
| 815 return regexp.test(location); | 834 return regexp.test(location); |
| 816 | 835 |
| 817 if (!this.matchCase) | 836 if (!this.matchCase) |
| 818 location = location.toLowerCase(); | 837 location = location.toLowerCase(); |
| 819 | 838 |
| 820 let {pattern} = this; | 839 let {pattern} = this; |
| 821 | 840 |
| 822 if (pattern[0] == "|" && pattern[1] == "|") | 841 if (pattern[0] == "|" && pattern[1] == "|") |
| 823 { | 842 { |
| 824 let index = location.indexOf(pattern.substring(2)); | 843 let index = location.indexOf(pattern.substring(2)); |
| 844 |
| 845 // The "||" prefix requires that the text that follows does not start |
| 846 // with a forward slash. |
| 825 return index != -1 && location[index] != "/" && | 847 return index != -1 && location[index] != "/" && |
| 826 tripleAnchorRegExp.test(location.substring(0, index)); | 848 doubleAnchorRegExp.test(location.substring(0, index)); |
| 827 } | 849 } |
| 828 | 850 |
| 829 return location.includes(pattern); | 851 return location.includes(pattern); |
| 830 } | 852 } |
| 831 }); | 853 }); |
| 832 | 854 |
| 833 /** | 855 /** |
| 834 * Yields the filter itself (required to optimize {@link Matcher}). | 856 * Yields the filter itself (required to optimize {@link Matcher}). |
| 835 * @yields {RegExpFilter} | 857 * @yields {RegExpFilter} |
| 836 */ | 858 */ |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1024 * @param {?string} [rewrite] | 1046 * @param {?string} [rewrite] |
| 1025 * The (optional) rule specifying how to rewrite the URL. See | 1047 * The (optional) rule specifying how to rewrite the URL. See |
| 1026 * BlockingFilter.prototype.rewrite. | 1048 * BlockingFilter.prototype.rewrite. |
| 1027 * @constructor | 1049 * @constructor |
| 1028 * @augments RegExpFilter | 1050 * @augments RegExpFilter |
| 1029 */ | 1051 */ |
| 1030 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, | 1052 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, |
| 1031 thirdParty, sitekeys, collapse, csp, rewrite) | 1053 thirdParty, sitekeys, collapse, csp, rewrite) |
| 1032 { | 1054 { |
| 1033 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 1055 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, |
| 1034 thirdParty, sitekeys, rewrite); | 1056 thirdParty, sitekeys); |
| 1035 | 1057 |
| 1036 if (collapse != null) | 1058 if (collapse != null) |
| 1037 this.collapse = collapse; | 1059 this.collapse = collapse; |
| 1038 | 1060 |
| 1039 if (csp != null) | 1061 if (csp != null) |
| 1040 this.csp = csp; | 1062 this.csp = csp; |
| 1041 | 1063 |
| 1042 if (rewrite != null) | 1064 if (rewrite != null) |
| 1043 this.rewrite = rewrite; | 1065 this.rewrite = rewrite; |
| 1044 } | 1066 } |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1314 | 1336 |
| 1315 /** | 1337 /** |
| 1316 * Script that should be executed | 1338 * Script that should be executed |
| 1317 * @type {string} | 1339 * @type {string} |
| 1318 */ | 1340 */ |
| 1319 get script() | 1341 get script() |
| 1320 { | 1342 { |
| 1321 return this.body; | 1343 return this.body; |
| 1322 } | 1344 } |
| 1323 }); | 1345 }); |
| LEFT | RIGHT |