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 |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 /** | 34 /** |
35 * Abstract base class for filters | 35 * Abstract base class for filters |
36 * | 36 * |
37 * @param {string} text string representation of the filter | 37 * @param {string} text string representation of the filter |
38 * @constructor | 38 * @constructor |
39 */ | 39 */ |
40 function Filter(text) | 40 function Filter(text) |
41 { | 41 { |
42 this.text = text; | 42 this.text = text; |
43 this.subscriptions = new Set(); | 43 |
| 44 /** |
| 45 * Subscriptions to which this filter belongs. |
| 46 * @type {(Subscription|Set.<Subscription>)?} |
| 47 * @private |
| 48 */ |
| 49 this._subscriptions = null; |
44 } | 50 } |
45 exports.Filter = Filter; | 51 exports.Filter = Filter; |
46 | 52 |
47 Filter.prototype = | 53 Filter.prototype = |
48 { | 54 { |
49 /** | 55 /** |
50 * String representation of the filter | 56 * String representation of the filter |
51 * @type {string} | 57 * @type {string} |
52 */ | 58 */ |
53 text: null, | 59 text: null, |
54 | 60 |
55 /** | 61 /** |
56 * Filter subscriptions the filter belongs to | |
57 * @type {Set.<Subscription>} | |
58 */ | |
59 subscriptions: null, | |
60 | |
61 /** | |
62 * Filter type as a string, e.g. "blocking". | 62 * Filter type as a string, e.g. "blocking". |
63 * @type {string} | 63 * @type {string} |
64 */ | 64 */ |
65 get type() | 65 get type() |
66 { | 66 { |
67 throw new Error("Please define filter type in the subclass"); | 67 throw new Error("Please define filter type in the subclass"); |
| 68 }, |
| 69 |
| 70 /** |
| 71 * Yields subscriptions to which the filter belongs. |
| 72 * @yields {Subscription} |
| 73 */ |
| 74 *subscriptions() |
| 75 { |
| 76 if (this._subscriptions) |
| 77 { |
| 78 if (this._subscriptions instanceof Set) |
| 79 yield* this._subscriptions; |
| 80 else |
| 81 yield this._subscriptions; |
| 82 } |
| 83 }, |
| 84 |
| 85 /** |
| 86 * The number of subscriptions to which the filter belongs. |
| 87 * @type {number} |
| 88 */ |
| 89 get subscriptionCount() |
| 90 { |
| 91 if (this._subscriptions instanceof Set) |
| 92 return this._subscriptions.size; |
| 93 |
| 94 return this._subscriptions ? 1 : 0; |
| 95 }, |
| 96 |
| 97 /** |
| 98 * Adds a subscription to the set of subscriptions to which the filter |
| 99 * belongs. |
| 100 * @param {Subscription} subscription |
| 101 */ |
| 102 addSubscription(subscription) |
| 103 { |
| 104 // Since we use truthy checks in our logic, we must avoid adding a |
| 105 // subscription that isn't a non-null object. |
| 106 if (subscription === null || typeof subscription != "object") |
| 107 return; |
| 108 |
| 109 if (this._subscriptions) |
| 110 { |
| 111 if (this._subscriptions instanceof Set) |
| 112 this._subscriptions.add(subscription); |
| 113 else if (subscription != this._subscriptions) |
| 114 this._subscriptions = new Set([this._subscriptions, subscription]); |
| 115 } |
| 116 else |
| 117 { |
| 118 this._subscriptions = subscription; |
| 119 } |
| 120 }, |
| 121 |
| 122 /** |
| 123 * Removes a subscription from the set of subscriptions to which the filter |
| 124 * belongs. |
| 125 * @param {Subscription} subscription |
| 126 */ |
| 127 removeSubscription(subscription) |
| 128 { |
| 129 if (this._subscriptions) |
| 130 { |
| 131 if (this._subscriptions instanceof Set) |
| 132 { |
| 133 this._subscriptions.delete(subscription); |
| 134 |
| 135 if (this._subscriptions.size == 1) |
| 136 this._subscriptions = [...this._subscriptions][0]; |
| 137 } |
| 138 else if (subscription == this._subscriptions) |
| 139 { |
| 140 this._subscriptions = null; |
| 141 } |
| 142 } |
68 }, | 143 }, |
69 | 144 |
70 /** | 145 /** |
71 * Serializes the filter to an array of strings for writing out on the disk. | 146 * Serializes the filter to an array of strings for writing out on the disk. |
72 * @param {string[]} buffer buffer to push the serialization results into | 147 * @param {string[]} buffer buffer to push the serialization results into |
73 */ | 148 */ |
74 serialize(buffer) | 149 serialize(buffer) |
75 { | 150 { |
76 buffer.push("[Filter]"); | 151 buffer.push("[Filter]"); |
77 buffer.push("text=" + this.text); | 152 buffer.push("text=" + this.text); |
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
645 * for delayed creation of the regexp property | 720 * for delayed creation of the regexp property |
646 * @type {?string} | 721 * @type {?string} |
647 */ | 722 */ |
648 pattern: null, | 723 pattern: null, |
649 /** | 724 /** |
650 * Regular expression to be used when testing against this filter | 725 * Regular expression to be used when testing against this filter |
651 * @type {RegExp} | 726 * @type {RegExp} |
652 */ | 727 */ |
653 get regexp() | 728 get regexp() |
654 { | 729 { |
655 let source = filterToRegExp(this.pattern); | 730 let source = filterToRegExp(this.pattern, this.rewrite != null); |
656 let regexp = new RegExp(source, this.matchCase ? "" : "i"); | 731 let regexp = new RegExp(source, this.matchCase ? "" : "i"); |
657 Object.defineProperty(this, "regexp", {value: regexp}); | 732 Object.defineProperty(this, "regexp", {value: regexp}); |
658 return regexp; | 733 return regexp; |
659 }, | 734 }, |
660 /** | 735 /** |
661 * Content types the filter applies to, combination of values from | 736 * Content types the filter applies to, combination of values from |
662 * RegExpFilter.typeMap | 737 * RegExpFilter.typeMap |
663 * @type {number} | 738 * @type {number} |
664 */ | 739 */ |
665 contentType: 0x7FFFFFFF, | 740 contentType: 0x7FFFFFFF, |
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1175 | 1250 |
1176 /** | 1251 /** |
1177 * Script that should be executed | 1252 * Script that should be executed |
1178 * @type {string} | 1253 * @type {string} |
1179 */ | 1254 */ |
1180 get script() | 1255 get script() |
1181 { | 1256 { |
1182 return this.body; | 1257 return this.body; |
1183 } | 1258 } |
1184 }); | 1259 }); |
LEFT | RIGHT |