| 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-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 import {FilterNotifier} from "./filterNotifier"; |
| 25 const {extend} = require("./coreUtils"); | 25 import {extend} from "./coreUtils"; |
| 26 const {filterToRegExp} = require("./common"); | 26 import {filterToRegExp} from "./common"; |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * Abstract base class for filters | 29 * Abstract base class for filters |
| 30 * | 30 * |
| 31 * @param {string} text string representation of the filter | 31 * @param {string} text string representation of the filter |
| 32 * @constructor | 32 * @constructor |
| 33 */ | 33 */ |
| 34 function Filter(text) | 34 export function Filter(text) |
| 35 { | 35 { |
| 36 this.text = text; | 36 this.text = text; |
| 37 this.subscriptions = []; | 37 this.subscriptions = []; |
| 38 } | 38 } |
| 39 exports.Filter = Filter; | |
| 40 | 39 |
| 41 Filter.prototype = | 40 Filter.prototype = |
| 42 { | 41 { |
| 43 /** | 42 /** |
| 44 * String representation of the filter | 43 * String representation of the filter |
| 45 * @type {string} | 44 * @type {string} |
| 46 */ | 45 */ |
| 47 text: null, | 46 text: null, |
| 48 | 47 |
| 49 /** | 48 /** |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 */ | 194 */ |
| 196 Filter.toRegExp = filterToRegExp; | 195 Filter.toRegExp = filterToRegExp; |
| 197 | 196 |
| 198 /** | 197 /** |
| 199 * Class for invalid filters | 198 * Class for invalid filters |
| 200 * @param {string} text see Filter() | 199 * @param {string} text see Filter() |
| 201 * @param {string} reason Reason why this filter is invalid | 200 * @param {string} reason Reason why this filter is invalid |
| 202 * @constructor | 201 * @constructor |
| 203 * @augments Filter | 202 * @augments Filter |
| 204 */ | 203 */ |
| 205 function InvalidFilter(text, reason) | 204 export function InvalidFilter(text, reason) |
| 206 { | 205 { |
| 207 Filter.call(this, text); | 206 Filter.call(this, text); |
| 208 | 207 |
| 209 this.reason = reason; | 208 this.reason = reason; |
| 210 } | 209 } |
| 211 exports.InvalidFilter = InvalidFilter; | |
| 212 | 210 |
| 213 InvalidFilter.prototype = extend(Filter, { | 211 InvalidFilter.prototype = extend(Filter, { |
| 214 type: "invalid", | 212 type: "invalid", |
| 215 | 213 |
| 216 /** | 214 /** |
| 217 * Reason why this filter is invalid | 215 * Reason why this filter is invalid |
| 218 * @type {string} | 216 * @type {string} |
| 219 */ | 217 */ |
| 220 reason: null, | 218 reason: null, |
| 221 | 219 |
| 222 /** | 220 /** |
| 223 * See Filter.serialize() | 221 * See Filter.serialize() |
| 224 * @inheritdoc | 222 * @inheritdoc |
| 225 */ | 223 */ |
| 226 serialize(buffer) {} | 224 serialize(buffer) {} |
| 227 }); | 225 }); |
| 228 | 226 |
| 229 /** | 227 /** |
| 230 * Class for comments | 228 * Class for comments |
| 231 * @param {string} text see Filter() | 229 * @param {string} text see Filter() |
| 232 * @constructor | 230 * @constructor |
| 233 * @augments Filter | 231 * @augments Filter |
| 234 */ | 232 */ |
| 235 function CommentFilter(text) | 233 export function CommentFilter(text) |
| 236 { | 234 { |
| 237 Filter.call(this, text); | 235 Filter.call(this, text); |
| 238 } | 236 } |
| 239 exports.CommentFilter = CommentFilter; | |
| 240 | 237 |
| 241 CommentFilter.prototype = extend(Filter, { | 238 CommentFilter.prototype = extend(Filter, { |
| 242 type: "comment", | 239 type: "comment", |
| 243 | 240 |
| 244 /** | 241 /** |
| 245 * See Filter.serialize() | 242 * See Filter.serialize() |
| 246 * @inheritdoc | 243 * @inheritdoc |
| 247 */ | 244 */ |
| 248 serialize(buffer) {} | 245 serialize(buffer) {} |
| 249 }); | 246 }); |
| 250 | 247 |
| 251 /** | 248 /** |
| 252 * Abstract base class for filters that can get hits | 249 * Abstract base class for filters that can get hits |
| 253 * @param {string} text | 250 * @param {string} text |
| 254 * see Filter() | 251 * see Filter() |
| 255 * @param {string} [domains] | 252 * @param {string} [domains] |
| 256 * Domains that the filter is restricted to separated by domainSeparator | 253 * Domains that the filter is restricted to separated by domainSeparator |
| 257 * e.g. "foo.com|bar.com|~baz.com" | 254 * e.g. "foo.com|bar.com|~baz.com" |
| 258 * @constructor | 255 * @constructor |
| 259 * @augments Filter | 256 * @augments Filter |
| 260 */ | 257 */ |
| 261 function ActiveFilter(text, domains) | 258 export function ActiveFilter(text, domains) |
| 262 { | 259 { |
| 263 Filter.call(this, text); | 260 Filter.call(this, text); |
| 264 | 261 |
| 265 this.domainSource = domains; | 262 this.domainSource = domains; |
| 266 } | 263 } |
| 267 exports.ActiveFilter = ActiveFilter; | |
| 268 | 264 |
| 269 ActiveFilter.prototype = extend(Filter, { | 265 ActiveFilter.prototype = extend(Filter, { |
| 270 _disabled: false, | 266 _disabled: false, |
| 271 _hitCount: 0, | 267 _hitCount: 0, |
| 272 _lastHit: 0, | 268 _lastHit: 0, |
| 273 | 269 |
| 274 /** | 270 /** |
| 275 * Defines whether the filter is disabled | 271 * Defines whether the filter is disabled |
| 276 * @type {boolean} | 272 * @type {boolean} |
| 277 */ | 273 */ |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 * @param {string} [domains] | 544 * @param {string} [domains] |
| 549 * Domains that the filter is restricted to, e.g. "foo.com|bar.com|~baz.com" | 545 * Domains that the filter is restricted to, e.g. "foo.com|bar.com|~baz.com" |
| 550 * @param {boolean} [thirdParty] | 546 * @param {boolean} [thirdParty] |
| 551 * Defines whether the filter should apply to third-party or first-party | 547 * Defines whether the filter should apply to third-party or first-party |
| 552 * content only | 548 * content only |
| 553 * @param {string} [sitekeys] | 549 * @param {string} [sitekeys] |
| 554 * Public keys of websites that this filter should apply to | 550 * Public keys of websites that this filter should apply to |
| 555 * @constructor | 551 * @constructor |
| 556 * @augments ActiveFilter | 552 * @augments ActiveFilter |
| 557 */ | 553 */ |
| 558 function RegExpFilter(text, regexpSource, contentType, matchCase, domains, | 554 export function RegExpFilter(text, regexpSource, contentType, matchCase, |
| 559 thirdParty, sitekeys) | 555 domains, thirdParty, sitekeys) |
| 560 { | 556 { |
| 561 ActiveFilter.call(this, text, domains, sitekeys); | 557 ActiveFilter.call(this, text, domains, sitekeys); |
| 562 | 558 |
| 563 if (contentType != null) | 559 if (contentType != null) |
| 564 this.contentType = contentType; | 560 this.contentType = contentType; |
| 565 if (matchCase) | 561 if (matchCase) |
| 566 this.matchCase = matchCase; | 562 this.matchCase = matchCase; |
| 567 if (thirdParty != null) | 563 if (thirdParty != null) |
| 568 this.thirdParty = thirdParty; | 564 this.thirdParty = thirdParty; |
| 569 if (sitekeys != null) | 565 if (sitekeys != null) |
| 570 this.sitekeySource = sitekeys; | 566 this.sitekeySource = sitekeys; |
| 571 | 567 |
| 572 if (regexpSource.length >= 2 && | 568 if (regexpSource.length >= 2 && |
| 573 regexpSource[0] == "/" && | 569 regexpSource[0] == "/" && |
| 574 regexpSource[regexpSource.length - 1] == "/") | 570 regexpSource[regexpSource.length - 1] == "/") |
| 575 { | 571 { |
| 576 // The filter is a regular expression - convert it immediately to | 572 // The filter is a regular expression - convert it immediately to |
| 577 // catch syntax errors | 573 // catch syntax errors |
| 578 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), | 574 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), |
| 579 this.matchCase ? "" : "i"); | 575 this.matchCase ? "" : "i"); |
| 580 Object.defineProperty(this, "regexp", {value: regexp}); | 576 Object.defineProperty(this, "regexp", {value: regexp}); |
| 581 } | 577 } |
| 582 else | 578 else |
| 583 { | 579 { |
| 584 // No need to convert this filter to regular expression yet, do it on demand | 580 // No need to convert this filter to regular expression yet, do it on demand |
| 585 this.regexpSource = regexpSource; | 581 this.regexpSource = regexpSource; |
| 586 } | 582 } |
| 587 } | 583 } |
| 588 exports.RegExpFilter = RegExpFilter; | |
| 589 | 584 |
| 590 RegExpFilter.prototype = extend(ActiveFilter, { | 585 RegExpFilter.prototype = extend(ActiveFilter, { |
| 591 /** | 586 /** |
| 592 * @see ActiveFilter.domainSourceIsUpperCase | 587 * @see ActiveFilter.domainSourceIsUpperCase |
| 593 */ | 588 */ |
| 594 domainSourceIsUpperCase: true, | 589 domainSourceIsUpperCase: true, |
| 595 | 590 |
| 596 /** | 591 /** |
| 597 * Number of filters contained, will always be 1 (required to | 592 * Number of filters contained, will always be 1 (required to |
| 598 * optimize Matcher). | 593 * optimize Matcher). |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 836 * @param {number} contentType see RegExpFilter() | 831 * @param {number} contentType see RegExpFilter() |
| 837 * @param {boolean} matchCase see RegExpFilter() | 832 * @param {boolean} matchCase see RegExpFilter() |
| 838 * @param {string} domains see RegExpFilter() | 833 * @param {string} domains see RegExpFilter() |
| 839 * @param {boolean} thirdParty see RegExpFilter() | 834 * @param {boolean} thirdParty see RegExpFilter() |
| 840 * @param {string} sitekeys see RegExpFilter() | 835 * @param {string} sitekeys see RegExpFilter() |
| 841 * @param {boolean} collapse | 836 * @param {boolean} collapse |
| 842 * defines whether the filter should collapse blocked content, can be null | 837 * defines whether the filter should collapse blocked content, can be null |
| 843 * @constructor | 838 * @constructor |
| 844 * @augments RegExpFilter | 839 * @augments RegExpFilter |
| 845 */ | 840 */ |
| 846 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, | 841 export function BlockingFilter(text, regexpSource, contentType, matchCase, |
| 847 thirdParty, sitekeys, collapse) | 842 domains, thirdParty, sitekeys, collapse) |
| 848 { | 843 { |
| 849 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 844 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, |
| 850 thirdParty, sitekeys); | 845 thirdParty, sitekeys); |
| 851 | 846 |
| 852 this.collapse = collapse; | 847 this.collapse = collapse; |
| 853 } | 848 } |
| 854 exports.BlockingFilter = BlockingFilter; | |
| 855 | 849 |
| 856 BlockingFilter.prototype = extend(RegExpFilter, { | 850 BlockingFilter.prototype = extend(RegExpFilter, { |
| 857 type: "blocking", | 851 type: "blocking", |
| 858 | 852 |
| 859 /** | 853 /** |
| 860 * Defines whether the filter should collapse blocked content. | 854 * Defines whether the filter should collapse blocked content. |
| 861 * Can be null (use the global preference). | 855 * Can be null (use the global preference). |
| 862 * @type {boolean} | 856 * @type {boolean} |
| 863 */ | 857 */ |
| 864 collapse: null | 858 collapse: null |
| 865 }); | 859 }); |
| 866 | 860 |
| 867 /** | 861 /** |
| 868 * Class for whitelist filters | 862 * Class for whitelist filters |
| 869 * @param {string} text see Filter() | 863 * @param {string} text see Filter() |
| 870 * @param {string} regexpSource see RegExpFilter() | 864 * @param {string} regexpSource see RegExpFilter() |
| 871 * @param {number} contentType see RegExpFilter() | 865 * @param {number} contentType see RegExpFilter() |
| 872 * @param {boolean} matchCase see RegExpFilter() | 866 * @param {boolean} matchCase see RegExpFilter() |
| 873 * @param {string} domains see RegExpFilter() | 867 * @param {string} domains see RegExpFilter() |
| 874 * @param {boolean} thirdParty see RegExpFilter() | 868 * @param {boolean} thirdParty see RegExpFilter() |
| 875 * @param {string} sitekeys see RegExpFilter() | 869 * @param {string} sitekeys see RegExpFilter() |
| 876 * @constructor | 870 * @constructor |
| 877 * @augments RegExpFilter | 871 * @augments RegExpFilter |
| 878 */ | 872 */ |
| 879 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, | 873 export function WhitelistFilter(text, regexpSource, contentType, matchCase, |
| 880 thirdParty, sitekeys) | 874 domains, thirdParty, sitekeys) |
| 881 { | 875 { |
| 882 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, | 876 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, |
| 883 thirdParty, sitekeys); | 877 thirdParty, sitekeys); |
| 884 } | 878 } |
| 885 exports.WhitelistFilter = WhitelistFilter; | |
| 886 | 879 |
| 887 WhitelistFilter.prototype = extend(RegExpFilter, { | 880 WhitelistFilter.prototype = extend(RegExpFilter, { |
| 888 type: "whitelist" | 881 type: "whitelist" |
| 889 }); | 882 }); |
| 890 | 883 |
| 891 /** | 884 /** |
| 892 * Base class for element hiding filters | 885 * Base class for element hiding filters |
| 893 * @param {string} text see Filter() | 886 * @param {string} text see Filter() |
| 894 * @param {string} [domains] Host names or domains the filter should be | 887 * @param {string} [domains] Host names or domains the filter should be |
| 895 * restricted to | 888 * restricted to |
| 896 * @param {string} selector CSS selector for the HTML elements that should be | 889 * @param {string} selector CSS selector for the HTML elements that should be |
| 897 * hidden | 890 * hidden |
| 898 * @constructor | 891 * @constructor |
| 899 * @augments ActiveFilter | 892 * @augments ActiveFilter |
| 900 */ | 893 */ |
| 901 function ElemHideBase(text, domains, selector) | 894 export function ElemHideBase(text, domains, selector) |
| 902 { | 895 { |
| 903 ActiveFilter.call(this, text, domains || null); | 896 ActiveFilter.call(this, text, domains || null); |
| 904 | 897 |
| 905 if (domains) | 898 if (domains) |
| 906 { | 899 { |
| 907 this.selectorDomain = domains.replace(/,~[^,]+/g, "") | 900 this.selectorDomain = domains.replace(/,~[^,]+/g, "") |
| 908 .replace(/^~[^,]+,?/, "").toLowerCase(); | 901 .replace(/^~[^,]+,?/, "").toLowerCase(); |
| 909 } | 902 } |
| 910 | 903 |
| 911 // Braces are being escaped to prevent CSS rule injection. | 904 // Braces are being escaped to prevent CSS rule injection. |
| 912 this.selector = selector.replace("{", "\\7B ").replace("}", "\\7D "); | 905 this.selector = selector.replace("{", "\\7B ").replace("}", "\\7D "); |
| 913 } | 906 } |
| 914 exports.ElemHideBase = ElemHideBase; | |
| 915 | 907 |
| 916 ElemHideBase.prototype = extend(ActiveFilter, { | 908 ElemHideBase.prototype = extend(ActiveFilter, { |
| 917 /** | 909 /** |
| 918 * @see ActiveFilter.domainSeparator | 910 * @see ActiveFilter.domainSeparator |
| 919 */ | 911 */ |
| 920 domainSeparator: ",", | 912 domainSeparator: ",", |
| 921 | 913 |
| 922 /** | 914 /** |
| 923 * @see ActiveFilter.ignoreTrailingDot | 915 * @see ActiveFilter.ignoreTrailingDot |
| 924 */ | 916 */ |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 978 * @param {string} text see Filter() | 970 * @param {string} text see Filter() |
| 979 * @param {string} domains see ElemHideBase() | 971 * @param {string} domains see ElemHideBase() |
| 980 * @param {string} selector see ElemHideBase() | 972 * @param {string} selector see ElemHideBase() |
| 981 * @constructor | 973 * @constructor |
| 982 * @augments ElemHideBase | 974 * @augments ElemHideBase |
| 983 */ | 975 */ |
| 984 function ElemHideFilter(text, domains, selector) | 976 function ElemHideFilter(text, domains, selector) |
| 985 { | 977 { |
| 986 ElemHideBase.call(this, text, domains, selector); | 978 ElemHideBase.call(this, text, domains, selector); |
| 987 } | 979 } |
| 988 exports.ElemHideFilter = ElemHideFilter; | |
| 989 | 980 |
| 990 ElemHideFilter.prototype = extend(ElemHideBase, { | 981 ElemHideFilter.prototype = extend(ElemHideBase, { |
| 991 type: "elemhide" | 982 type: "elemhide" |
| 992 }); | 983 }); |
| 993 | 984 |
| 994 /** | 985 /** |
| 995 * Class for element hiding exceptions | 986 * Class for element hiding exceptions |
| 996 * @param {string} text see Filter() | 987 * @param {string} text see Filter() |
| 997 * @param {string} domains see ElemHideBase() | 988 * @param {string} domains see ElemHideBase() |
| 998 * @param {string} selector see ElemHideBase() | 989 * @param {string} selector see ElemHideBase() |
| 999 * @constructor | 990 * @constructor |
| 1000 * @augments ElemHideBase | 991 * @augments ElemHideBase |
| 1001 */ | 992 */ |
| 1002 function ElemHideException(text, domains, selector) | 993 export function ElemHideException(text, domains, selector) |
| 1003 { | 994 { |
| 1004 ElemHideBase.call(this, text, domains, selector); | 995 ElemHideBase.call(this, text, domains, selector); |
| 1005 } | 996 } |
| 1006 exports.ElemHideException = ElemHideException; | |
| 1007 | 997 |
| 1008 ElemHideException.prototype = extend(ElemHideBase, { | 998 ElemHideException.prototype = extend(ElemHideBase, { |
| 1009 type: "elemhideexception" | 999 type: "elemhideexception" |
| 1010 }); | 1000 }); |
| 1011 | 1001 |
| 1012 /** | 1002 /** |
| 1013 * Class for element hiding emulation filters | 1003 * Class for element hiding emulation filters |
| 1014 * @param {string} text see Filter() | 1004 * @param {string} text see Filter() |
| 1015 * @param {string} domains see ElemHideBase() | 1005 * @param {string} domains see ElemHideBase() |
| 1016 * @param {string} selector see ElemHideBase() | 1006 * @param {string} selector see ElemHideBase() |
| 1017 * @constructor | 1007 * @constructor |
| 1018 * @augments ElemHideBase | 1008 * @augments ElemHideBase |
| 1019 */ | 1009 */ |
| 1020 function ElemHideEmulationFilter(text, domains, selector) | 1010 export function ElemHideEmulationFilter(text, domains, selector) |
| 1021 { | 1011 { |
| 1022 ElemHideBase.call(this, text, domains, selector); | 1012 ElemHideBase.call(this, text, domains, selector); |
| 1023 } | 1013 } |
| 1024 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; | |
| 1025 | 1014 |
| 1026 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { | 1015 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { |
| 1027 type: "elemhideemulation" | 1016 type: "elemhideemulation" |
| 1028 }); | 1017 }); |
| OLD | NEW |