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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 /** | 18 /** |
19 * @fileOverview Definition of Filter class and its subclasses. | 19 * @fileOverview Definition of Filter class and its subclasses. |
20 */ | 20 */ |
21 | 21 |
22 let {FilterNotifier} = require("filterNotifier"); | 22 let {FilterNotifier} = require("filterNotifier"); |
23 let {desc} = require("coreUtils"); | 23 let {extend} = require("coreUtils"); |
24 | 24 |
25 /** | 25 /** |
26 * Abstract base class for filters | 26 * Abstract base class for filters |
27 * | 27 * |
28 * @param {String} text string representation of the filter | 28 * @param {String} text string representation of the filter |
29 * @constructor | 29 * @constructor |
30 */ | 30 */ |
31 function Filter(text) | 31 function Filter(text) |
32 { | 32 { |
33 this.text = text; | 33 this.text = text; |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 * @augments Filter | 204 * @augments Filter |
205 */ | 205 */ |
206 function InvalidFilter(text, reason) | 206 function InvalidFilter(text, reason) |
207 { | 207 { |
208 Filter.call(this, text); | 208 Filter.call(this, text); |
209 | 209 |
210 this.reason = reason; | 210 this.reason = reason; |
211 } | 211 } |
212 exports.InvalidFilter = InvalidFilter; | 212 exports.InvalidFilter = InvalidFilter; |
213 | 213 |
214 InvalidFilter.prototype = Object.create(Filter.prototype, desc({ | 214 InvalidFilter.prototype = extend(Filter, { |
215 type: "invalid", | 215 type: "invalid", |
216 | 216 |
217 /** | 217 /** |
218 * Reason why this filter is invalid | 218 * Reason why this filter is invalid |
219 * @type String | 219 * @type String |
220 */ | 220 */ |
221 reason: null, | 221 reason: null, |
222 | 222 |
223 /** | 223 /** |
224 * See Filter.serialize() | 224 * See Filter.serialize() |
225 */ | 225 */ |
226 serialize: function(buffer) {} | 226 serialize: function(buffer) {} |
227 })); | 227 }); |
228 | 228 |
229 /** | 229 /** |
230 * Class for comments | 230 * Class for comments |
231 * @param {String} text see Filter() | 231 * @param {String} text see Filter() |
232 * @constructor | 232 * @constructor |
233 * @augments Filter | 233 * @augments Filter |
234 */ | 234 */ |
235 function CommentFilter(text) | 235 function CommentFilter(text) |
236 { | 236 { |
237 Filter.call(this, text); | 237 Filter.call(this, text); |
238 } | 238 } |
239 exports.CommentFilter = CommentFilter; | 239 exports.CommentFilter = CommentFilter; |
240 | 240 |
241 CommentFilter.prototype = Object.create(Filter.prototype, desc({ | 241 CommentFilter.prototype = extend(Filter, { |
242 type: "comment", | 242 type: "comment", |
243 | 243 |
244 /** | 244 /** |
245 * See Filter.serialize() | 245 * See Filter.serialize() |
246 */ | 246 */ |
247 serialize: function(buffer) {} | 247 serialize: function(buffer) {} |
248 })); | 248 }); |
249 | 249 |
250 /** | 250 /** |
251 * Abstract base class for filters that can get hits | 251 * Abstract base class for filters that can get hits |
252 * @param {String} text see Filter() | 252 * @param {String} text see Filter() |
253 * @param {String} [domains] Domains that the filter is restricted to separated
by domainSeparator e.g. "foo.com|bar.com|~baz.com" | 253 * @param {String} [domains] Domains that the filter is restricted to separated
by domainSeparator e.g. "foo.com|bar.com|~baz.com" |
254 * @constructor | 254 * @constructor |
255 * @augments Filter | 255 * @augments Filter |
256 */ | 256 */ |
257 function ActiveFilter(text, domains) | 257 function ActiveFilter(text, domains) |
258 { | 258 { |
259 Filter.call(this, text); | 259 Filter.call(this, text); |
260 | 260 |
261 this.domainSource = domains; | 261 this.domainSource = domains; |
262 } | 262 } |
263 exports.ActiveFilter = ActiveFilter; | 263 exports.ActiveFilter = ActiveFilter; |
264 | 264 |
265 ActiveFilter.prototype = Object.create(Filter.prototype, desc({ | 265 ActiveFilter.prototype = extend(Filter, { |
266 _disabled: false, | 266 _disabled: false, |
267 _hitCount: 0, | 267 _hitCount: 0, |
268 _lastHit: 0, | 268 _lastHit: 0, |
269 | 269 |
270 /** | 270 /** |
271 * Defines whether the filter is disabled | 271 * Defines whether the filter is disabled |
272 * @type Boolean | 272 * @type Boolean |
273 */ | 273 */ |
274 get disabled() | 274 get disabled() |
275 { | 275 { |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 { | 500 { |
501 Filter.prototype.serialize.call(this, buffer); | 501 Filter.prototype.serialize.call(this, buffer); |
502 if (this._disabled) | 502 if (this._disabled) |
503 buffer.push("disabled=true"); | 503 buffer.push("disabled=true"); |
504 if (this._hitCount) | 504 if (this._hitCount) |
505 buffer.push("hitCount=" + this._hitCount); | 505 buffer.push("hitCount=" + this._hitCount); |
506 if (this._lastHit) | 506 if (this._lastHit) |
507 buffer.push("lastHit=" + this._lastHit); | 507 buffer.push("lastHit=" + this._lastHit); |
508 } | 508 } |
509 } | 509 } |
510 })); | 510 }); |
511 | 511 |
512 /** | 512 /** |
513 * Abstract base class for RegExp-based filters | 513 * Abstract base class for RegExp-based filters |
514 * @param {String} text see Filter() | 514 * @param {String} text see Filter() |
515 * @param {String} regexpSource filter part that the regular expression should b
e build from | 515 * @param {String} regexpSource filter part that the regular expression should b
e build from |
516 * @param {Number} [contentType] Content types the filter applies to, combinatio
n of values from RegExpFilter.typeMap | 516 * @param {Number} [contentType] Content types the filter applies to, combinatio
n of values from RegExpFilter.typeMap |
517 * @param {Boolean} [matchCase] Defines whether the filter should distinguish be
tween lower and upper case letters | 517 * @param {Boolean} [matchCase] Defines whether the filter should distinguish be
tween lower and upper case letters |
518 * @param {String} [domains] Domains that the filter is restricted to, e.g. "foo
.com|bar.com|~baz.com" | 518 * @param {String} [domains] Domains that the filter is restricted to, e.g. "foo
.com|bar.com|~baz.com" |
519 * @param {Boolean} [thirdParty] Defines whether the filter should apply to thir
d-party or first-party content only | 519 * @param {Boolean} [thirdParty] Defines whether the filter should apply to thir
d-party or first-party content only |
520 * @param {String} [sitekeys] Public keys of websites that this filter should ap
ply to | 520 * @param {String} [sitekeys] Public keys of websites that this filter should ap
ply to |
(...skipping 20 matching lines...) Expand all Loading... |
541 Object.defineProperty(this, "regexp", {value: regexp}); | 541 Object.defineProperty(this, "regexp", {value: regexp}); |
542 } | 542 } |
543 else | 543 else |
544 { | 544 { |
545 // No need to convert this filter to regular expression yet, do it on demand | 545 // No need to convert this filter to regular expression yet, do it on demand |
546 this.regexpSource = regexpSource; | 546 this.regexpSource = regexpSource; |
547 } | 547 } |
548 } | 548 } |
549 exports.RegExpFilter = RegExpFilter; | 549 exports.RegExpFilter = RegExpFilter; |
550 | 550 |
551 RegExpFilter.prototype = Object.create(ActiveFilter.prototype, desc({ | 551 RegExpFilter.prototype = extend(ActiveFilter, { |
552 /** | 552 /** |
553 * @see ActiveFilter.domainSourceIsUpperCase | 553 * @see ActiveFilter.domainSourceIsUpperCase |
554 */ | 554 */ |
555 domainSourceIsUpperCase: true, | 555 domainSourceIsUpperCase: true, |
556 | 556 |
557 /** | 557 /** |
558 * Number of filters contained, will always be 1 (required to optimize Matcher
). | 558 * Number of filters contained, will always be 1 (required to optimize Matcher
). |
559 * @type Integer | 559 * @type Integer |
560 */ | 560 */ |
561 length: 1, | 561 length: 1, |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
646 { | 646 { |
647 if (this.contentType & typeMask && | 647 if (this.contentType & typeMask && |
648 (this.thirdParty == null || this.thirdParty == thirdParty) && | 648 (this.thirdParty == null || this.thirdParty == thirdParty) && |
649 this.isActiveOnDomain(docDomain, sitekey) && this.regexp.test(location)) | 649 this.isActiveOnDomain(docDomain, sitekey) && this.regexp.test(location)) |
650 { | 650 { |
651 return true; | 651 return true; |
652 } | 652 } |
653 | 653 |
654 return false; | 654 return false; |
655 } | 655 } |
656 })); | 656 }); |
657 | 657 |
658 // Required to optimize Matcher, see also RegExpFilter.prototype.length | 658 // Required to optimize Matcher, see also RegExpFilter.prototype.length |
659 Object.defineProperty(RegExpFilter.prototype, "0", | 659 Object.defineProperty(RegExpFilter.prototype, "0", |
660 { | 660 { |
661 get: function() { return this; } | 661 get: function() { return this; } |
662 }); | 662 }); |
663 | 663 |
664 /** | 664 /** |
665 * Creates a RegExp filter from its text representation | 665 * Creates a RegExp filter from its text representation |
666 * @param {String} text same as in Filter() | 666 * @param {String} text same as in Filter() |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
792 * @augments RegExpFilter | 792 * @augments RegExpFilter |
793 */ | 793 */ |
794 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, thi
rdParty, sitekeys, collapse) | 794 function BlockingFilter(text, regexpSource, contentType, matchCase, domains, thi
rdParty, sitekeys, collapse) |
795 { | 795 { |
796 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, t
hirdParty, sitekeys); | 796 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, t
hirdParty, sitekeys); |
797 | 797 |
798 this.collapse = collapse; | 798 this.collapse = collapse; |
799 } | 799 } |
800 exports.BlockingFilter = BlockingFilter; | 800 exports.BlockingFilter = BlockingFilter; |
801 | 801 |
802 BlockingFilter.prototype = Object.create(RegExpFilter.prototype, desc({ | 802 BlockingFilter.prototype = extend(RegExpFilter, { |
803 type: "blocking", | 803 type: "blocking", |
804 | 804 |
805 /** | 805 /** |
806 * Defines whether the filter should collapse blocked content. Can be null (us
e the global preference). | 806 * Defines whether the filter should collapse blocked content. Can be null (us
e the global preference). |
807 * @type Boolean | 807 * @type Boolean |
808 */ | 808 */ |
809 collapse: null | 809 collapse: null |
810 })); | 810 }); |
811 | 811 |
812 /** | 812 /** |
813 * Class for whitelist filters | 813 * Class for whitelist filters |
814 * @param {String} text see Filter() | 814 * @param {String} text see Filter() |
815 * @param {String} regexpSource see RegExpFilter() | 815 * @param {String} regexpSource see RegExpFilter() |
816 * @param {Number} contentType see RegExpFilter() | 816 * @param {Number} contentType see RegExpFilter() |
817 * @param {Boolean} matchCase see RegExpFilter() | 817 * @param {Boolean} matchCase see RegExpFilter() |
818 * @param {String} domains see RegExpFilter() | 818 * @param {String} domains see RegExpFilter() |
819 * @param {Boolean} thirdParty see RegExpFilter() | 819 * @param {Boolean} thirdParty see RegExpFilter() |
820 * @param {String} sitekeys see RegExpFilter() | 820 * @param {String} sitekeys see RegExpFilter() |
821 * @constructor | 821 * @constructor |
822 * @augments RegExpFilter | 822 * @augments RegExpFilter |
823 */ | 823 */ |
824 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, th
irdParty, sitekeys) | 824 function WhitelistFilter(text, regexpSource, contentType, matchCase, domains, th
irdParty, sitekeys) |
825 { | 825 { |
826 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, t
hirdParty, sitekeys); | 826 RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains, t
hirdParty, sitekeys); |
827 } | 827 } |
828 exports.WhitelistFilter = WhitelistFilter; | 828 exports.WhitelistFilter = WhitelistFilter; |
829 | 829 |
830 WhitelistFilter.prototype = Object.create(RegExpFilter.prototype, desc({ | 830 WhitelistFilter.prototype = extend(RegExpFilter, { |
831 type: "whitelist" | 831 type: "whitelist" |
832 })); | 832 }); |
833 | 833 |
834 /** | 834 /** |
835 * Base class for element hiding filters | 835 * Base class for element hiding filters |
836 * @param {String} text see Filter() | 836 * @param {String} text see Filter() |
837 * @param {String} [domains] Host names or domains the filter should be restrict
ed to | 837 * @param {String} [domains] Host names or domains the filter should be restrict
ed to |
838 * @param {String} selector CSS selector for the HTML elements that should be
hidden | 838 * @param {String} selector CSS selector for the HTML elements that should be
hidden |
839 * @constructor | 839 * @constructor |
840 * @augments ActiveFilter | 840 * @augments ActiveFilter |
841 */ | 841 */ |
842 function ElemHideBase(text, domains, selector) | 842 function ElemHideBase(text, domains, selector) |
843 { | 843 { |
844 ActiveFilter.call(this, text, domains || null); | 844 ActiveFilter.call(this, text, domains || null); |
845 | 845 |
846 if (domains) | 846 if (domains) |
847 this.selectorDomain = domains.replace(/,~[^,]+/g, "").replace(/^~[^,]+,?/, "
").toLowerCase(); | 847 this.selectorDomain = domains.replace(/,~[^,]+/g, "").replace(/^~[^,]+,?/, "
").toLowerCase(); |
848 this.selector = selector; | 848 this.selector = selector; |
849 } | 849 } |
850 exports.ElemHideBase = ElemHideBase; | 850 exports.ElemHideBase = ElemHideBase; |
851 | 851 |
852 ElemHideBase.prototype = Object.create(ActiveFilter.prototype, desc({ | 852 ElemHideBase.prototype = extend(ActiveFilter, { |
853 /** | 853 /** |
854 * @see ActiveFilter.domainSeparator | 854 * @see ActiveFilter.domainSeparator |
855 */ | 855 */ |
856 domainSeparator: ",", | 856 domainSeparator: ",", |
857 | 857 |
858 /** | 858 /** |
859 * @see ActiveFilter.ignoreTrailingDot | 859 * @see ActiveFilter.ignoreTrailingDot |
860 */ | 860 */ |
861 ignoreTrailingDot: false, | 861 ignoreTrailingDot: false, |
862 | 862 |
863 /** | 863 /** |
864 * Host name or domain the filter should be restricted to (can be null for no
restriction) | 864 * Host name or domain the filter should be restricted to (can be null for no
restriction) |
865 * @type String | 865 * @type String |
866 */ | 866 */ |
867 selectorDomain: null, | 867 selectorDomain: null, |
868 /** | 868 /** |
869 * CSS selector for the HTML elements that should be hidden | 869 * CSS selector for the HTML elements that should be hidden |
870 * @type String | 870 * @type String |
871 */ | 871 */ |
872 selector: null | 872 selector: null |
873 })); | 873 }); |
874 | 874 |
875 /** | 875 /** |
876 * Creates an element hiding filter from a pre-parsed text representation | 876 * Creates an element hiding filter from a pre-parsed text representation |
877 * | 877 * |
878 * @param {String} text same as in Filter() | 878 * @param {String} text same as in Filter() |
879 * @param {String} domain domain part of the text representation (can be e
mpty) | 879 * @param {String} domain domain part of the text representation (can be e
mpty) |
880 * @param {Boolean} isException exception rule indicator | 880 * @param {Boolean} isException exception rule indicator |
881 * @param {String} tagName tag name part (can be empty) | 881 * @param {String} tagName tag name part (can be empty) |
882 * @param {String} attrRules attribute matching rules (can be empty) | 882 * @param {String} attrRules attribute matching rules (can be empty) |
883 * @param {String} selector raw CSS selector (can be empty) | 883 * @param {String} selector raw CSS selector (can be empty) |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
948 * @param {String} selector see ElemHideBase() | 948 * @param {String} selector see ElemHideBase() |
949 * @constructor | 949 * @constructor |
950 * @augments ElemHideBase | 950 * @augments ElemHideBase |
951 */ | 951 */ |
952 function ElemHideFilter(text, domains, selector) | 952 function ElemHideFilter(text, domains, selector) |
953 { | 953 { |
954 ElemHideBase.call(this, text, domains, selector); | 954 ElemHideBase.call(this, text, domains, selector); |
955 } | 955 } |
956 exports.ElemHideFilter = ElemHideFilter; | 956 exports.ElemHideFilter = ElemHideFilter; |
957 | 957 |
958 ElemHideFilter.prototype = Object.create(ElemHideBase.prototype, desc({ | 958 ElemHideFilter.prototype = extend(ElemHideBase, { |
959 type: "elemhide" | 959 type: "elemhide" |
960 })); | 960 }); |
961 | 961 |
962 /** | 962 /** |
963 * Class for element hiding exceptions | 963 * Class for element hiding exceptions |
964 * @param {String} text see Filter() | 964 * @param {String} text see Filter() |
965 * @param {String} domains see ElemHideBase() | 965 * @param {String} domains see ElemHideBase() |
966 * @param {String} selector see ElemHideBase() | 966 * @param {String} selector see ElemHideBase() |
967 * @constructor | 967 * @constructor |
968 * @augments ElemHideBase | 968 * @augments ElemHideBase |
969 */ | 969 */ |
970 function ElemHideException(text, domains, selector) | 970 function ElemHideException(text, domains, selector) |
971 { | 971 { |
972 ElemHideBase.call(this, text, domains, selector); | 972 ElemHideBase.call(this, text, domains, selector); |
973 } | 973 } |
974 exports.ElemHideException = ElemHideException; | 974 exports.ElemHideException = ElemHideException; |
975 | 975 |
976 ElemHideException.prototype = Object.create(ElemHideBase.prototype, desc({ | 976 ElemHideException.prototype = extend(ElemHideBase, { |
977 type: "elemhideexception" | 977 type: "elemhideexception" |
978 })); | 978 }); |
979 | 979 |
980 /** | 980 /** |
981 * Class for CSS property filters | 981 * Class for CSS property filters |
982 * @param {String} text see Filter() | 982 * @param {String} text see Filter() |
983 * @param {String} domains see ElemHideBase() | 983 * @param {String} domains see ElemHideBase() |
984 * @param {String} selector see ElemHideBase() | 984 * @param {String} selector see ElemHideBase() |
985 * @param {String} regexpSource see CSSPropertyFilter.regexpSource | 985 * @param {String} regexpSource see CSSPropertyFilter.regexpSource |
986 * @param {String} selectorPrefix see CSSPropertyFilter.selectorPrefix | 986 * @param {String} selectorPrefix see CSSPropertyFilter.selectorPrefix |
987 * @param {String} selectorSuffix see CSSPropertyFilter.selectorSuffix | 987 * @param {String} selectorSuffix see CSSPropertyFilter.selectorSuffix |
988 * @constructor | 988 * @constructor |
989 * @augments ElemHideBase | 989 * @augments ElemHideBase |
990 */ | 990 */ |
991 function CSSPropertyFilter(text, domains, selector, regexpSource, | 991 function CSSPropertyFilter(text, domains, selector, regexpSource, |
992 selectorPrefix, selectorSuffix) | 992 selectorPrefix, selectorSuffix) |
993 { | 993 { |
994 ElemHideBase.call(this, text, domains, selector); | 994 ElemHideBase.call(this, text, domains, selector); |
995 | 995 |
996 this.regexpSource = regexpSource; | 996 this.regexpSource = regexpSource; |
997 this.selectorPrefix = selectorPrefix; | 997 this.selectorPrefix = selectorPrefix; |
998 this.selectorSuffix = selectorSuffix; | 998 this.selectorSuffix = selectorSuffix; |
999 } | 999 } |
1000 exports.CSSPropertyFilter = CSSPropertyFilter; | 1000 exports.CSSPropertyFilter = CSSPropertyFilter; |
1001 | 1001 |
1002 CSSPropertyFilter.prototype = Object.create(ElemHideBase.prototype, desc({ | 1002 CSSPropertyFilter.prototype = extend(ElemHideBase, { |
1003 type: "cssproperty", | 1003 type: "cssproperty", |
1004 | 1004 |
1005 /** | 1005 /** |
1006 * Expression from which a regular expression should be generated for matching | 1006 * Expression from which a regular expression should be generated for matching |
1007 * CSS properties - for delayed creation of the regexpString property | 1007 * CSS properties - for delayed creation of the regexpString property |
1008 * @type String | 1008 * @type String |
1009 */ | 1009 */ |
1010 regexpSource: null, | 1010 regexpSource: null, |
1011 /** | 1011 /** |
1012 * Substring of CSS selector before properties for the HTML elements that | 1012 * Substring of CSS selector before properties for the HTML elements that |
(...skipping 18 matching lines...) Expand all Loading... |
1031 // Despite this property being cached, the getter is called | 1031 // Despite this property being cached, the getter is called |
1032 // several times on Safari, due to WebKit bug 132872 | 1032 // several times on Safari, due to WebKit bug 132872 |
1033 let prop = Object.getOwnPropertyDescriptor(this, "regexpString"); | 1033 let prop = Object.getOwnPropertyDescriptor(this, "regexpString"); |
1034 if (prop) | 1034 if (prop) |
1035 return prop.value; | 1035 return prop.value; |
1036 | 1036 |
1037 let regexp = Filter.toRegExp(this.regexpSource); | 1037 let regexp = Filter.toRegExp(this.regexpSource); |
1038 Object.defineProperty(this, "regexpString", {value: regexp}); | 1038 Object.defineProperty(this, "regexpString", {value: regexp}); |
1039 return regexp; | 1039 return regexp; |
1040 } | 1040 } |
1041 })); | 1041 }); |
LEFT | RIGHT |