Left: | ||
Right: |
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 Subscription class and its subclasses. | 21 * @fileOverview Definition of Subscription class and its subclasses. |
22 */ | 22 */ |
23 | 23 |
24 const {Filter, ActiveFilter, BlockingFilter, | 24 const {ActiveFilter, BlockingFilter, |
25 WhitelistFilter, ElemHideBase} = require("./filterClasses"); | 25 WhitelistFilter, ElemHideBase} = require("./filterClasses"); |
26 const {filterNotifier} = require("./filterNotifier"); | 26 const {filterNotifier} = require("./filterNotifier"); |
27 const {extend} = require("./coreUtils"); | 27 const {extend} = require("./coreUtils"); |
28 | 28 |
29 /** | 29 /** |
30 * Abstract base class for filter subscriptions | 30 * Abstract base class for filter subscriptions |
31 * | 31 * |
32 * @param {string} url download location of the subscription | 32 * @param {string} url download location of the subscription |
33 * @param {string} [title] title of the filter subscription | 33 * @param {string} [title] title of the filter subscription |
34 * @constructor | 34 * @constructor |
35 */ | 35 */ |
36 function Subscription(url, title) | 36 function Subscription(url, title) |
37 { | 37 { |
38 this.url = url; | 38 this.url = url; |
39 | |
39 this._filterText = []; | 40 this._filterText = []; |
40 this._filters = []; | 41 this._filters = []; |
42 | |
41 if (title) | 43 if (title) |
42 this._title = title; | 44 this._title = title; |
45 | |
43 Subscription.knownSubscriptions.set(url, this); | 46 Subscription.knownSubscriptions.set(url, this); |
44 } | 47 } |
45 exports.Subscription = Subscription; | 48 exports.Subscription = Subscription; |
46 | 49 |
47 Subscription.prototype = | 50 Subscription.prototype = |
48 { | 51 { |
49 /** | 52 /** |
50 * Download location of the subscription | 53 * Download location of the subscription |
51 * @type {string} | 54 * @type {string} |
52 */ | 55 */ |
53 url: null, | 56 url: null, |
54 | 57 |
55 /** | 58 /** |
56 * Type of the subscription | 59 * Type of the subscription |
57 * @type {?string} | 60 * @type {?string} |
58 */ | 61 */ |
59 type: null, | 62 type: null, |
60 | 63 |
61 /** | 64 /** |
62 * Filter text contained in the filter subscription. | 65 * Filter text contained in the filter subscription. |
63 * @type {Array.<string>} | 66 * @type {Array.<string>} |
64 * @private | 67 * @private |
65 */ | 68 */ |
66 _filterText: null, | 69 _filterText: null, |
hub
2018/11/19 17:47:49
shouldn't this be named `_filtersText` since it is
Manish Jethani
2018/11/20 00:19:39
I thought about calling it _filtersText but that s
hub
2018/11/20 19:30:19
my bad. let's leave it at that.
| |
67 | 70 |
68 /** | 71 /** |
69 * Optional {@link Filter} objects corresponding to the subscription's filter | 72 * {@link Filter} objects corresponding to the subscription's filter text. |
70 * text. | 73 * @type {Array.<Filter>} |
71 * @type {Array.<?Filter>} | |
72 * @private | 74 * @private |
73 */ | 75 */ |
74 _filters: null, | 76 _filters: null, |
75 | 77 |
76 _title: null, | 78 _title: null, |
77 _fixedTitle: false, | 79 _fixedTitle: false, |
78 _disabled: false, | 80 _disabled: false, |
79 | 81 |
80 /** | 82 /** |
81 * Title of the filter subscription | 83 * Title of the filter subscription |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 { | 153 { |
152 yield* this._filterText; | 154 yield* this._filterText; |
153 }, | 155 }, |
154 | 156 |
155 /** | 157 /** |
156 * Yields the {@link Filter} object for each filter in the subscription. | 158 * Yields the {@link Filter} object for each filter in the subscription. |
157 * @yields {Filter} | 159 * @yields {Filter} |
158 */ | 160 */ |
159 *filters() | 161 *filters() |
160 { | 162 { |
161 for (let i = 0; i < this._filters.length; i++) | 163 yield* this._filters; |
162 yield this._filters[i] || Filter.fromText(this._filterText[i]); | |
163 }, | 164 }, |
164 | 165 |
165 /** | 166 /** |
166 * Returns the {@link Filter} object at the given 0-based index. | 167 * Returns the {@link Filter} object at the given 0-based index. |
167 * @param {number} index | 168 * @param {number} index |
168 * @returns {?Filter} | 169 * @returns {?Filter} |
169 */ | 170 */ |
170 filterAt(index) | 171 filterAt(index) |
171 { | 172 { |
172 return this._filters[index] || | 173 return this._filters[index] || null; |
173 (index < this._filters.length ? | |
174 Filter.fromText(this._filterText[index]) : null); | |
175 }, | 174 }, |
176 | 175 |
177 /** | 176 /** |
178 * Returns the 0-based index of the given filter. | 177 * Returns the 0-based index of the given filter. |
179 * @param {Filter} filter | 178 * @param {Filter} filter |
180 * @param {number} [fromIndex] The index from which to start the search. | 179 * @param {number} [fromIndex] The index from which to start the search. |
181 * @return {number} | 180 * @return {number} |
182 */ | 181 */ |
183 searchFilter(filter, fromIndex = 0) | 182 searchFilter(filter, fromIndex = 0) |
184 { | 183 { |
185 return this._filterText.indexOf(filter.text, fromIndex); | 184 return this._filterText.indexOf(filter.text, fromIndex); |
186 }, | 185 }, |
187 | 186 |
188 /** | 187 /** |
189 * Removes all filters from the subscription. | 188 * Removes all filters from the subscription. |
190 */ | 189 */ |
191 clearFilters() | 190 clearFilters() |
192 { | 191 { |
193 this._filterText = []; | 192 this._filterText = []; |
194 this._filters = []; | 193 this._filters = []; |
195 }, | 194 }, |
196 | 195 |
197 /** | 196 /** |
198 * Adds a filter to the subscription. | 197 * Adds a filter to the subscription. |
199 * @param {Filter} filter | 198 * @param {Filter} filter |
200 * @param {boolean} [forceCache] If set to <code>true</code>, the | 199 */ |
201 * {@link Filter} object is cached regardless of whether it's lightweight. | 200 addFilter(filter) |
202 * By default lightweight objects are not cached but are recreated on | |
203 * demand. | |
204 */ | |
205 addFilter(filter, forceCache = false) | |
206 { | 201 { |
207 this._filterText.push(filter.text); | 202 this._filterText.push(filter.text); |
208 this._filters.push(!forceCache && filter.lightweight ? null : filter); | 203 this._filters.push(filter); |
209 }, | 204 }, |
210 | 205 |
211 /** | 206 /** |
212 * Inserts a filter into the subscription. | 207 * Inserts a filter into the subscription. |
213 * @param {Filter} filter | 208 * @param {Filter} filter |
214 * @param {number} [index] The index at which to insert the filter. | 209 * @param {number} index The index at which to insert the filter. |
215 */ | 210 */ |
216 insertFilterAt(filter, index) | 211 insertFilterAt(filter, index) |
217 { | 212 { |
218 this._filterText.splice(index, 0, filter.text); | 213 this._filterText.splice(index, 0, filter.text); |
219 this._filters.splice(index, 0, filter.lightweight ? null : filter); | 214 this._filters.splice(index, 0, filter); |
220 }, | 215 }, |
221 | 216 |
222 /** | 217 /** |
223 * Deletes a filter from the subscription. | 218 * Deletes a filter from the subscription. |
224 * @param {number} [index] The index at which to delete the filter. | 219 * @param {number} index The index at which to delete the filter. |
225 */ | 220 */ |
226 deleteFilterAt(index) | 221 deleteFilterAt(index) |
227 { | 222 { |
223 // Ignore index if out of bounds on the negative side, for consistency. | |
224 if (index < 0) | |
225 return; | |
226 | |
228 this._filterText.splice(index, 1); | 227 this._filterText.splice(index, 1); |
229 this._filters.splice(index, 1); | 228 this._filters.splice(index, 1); |
230 }, | |
231 | |
232 /** | |
233 * Frees up any force-cached {@link Filter} objects. | |
234 * @see #addFilter | |
235 */ | |
236 freeFilters() | |
237 { | |
238 for (let i = 0; i < this._filters.length; i++) | |
239 { | |
240 let filter = this._filters[i]; | |
241 if (filter && filter.lightweight) | |
242 this._filters[i] = null; | |
243 } | |
244 }, | 229 }, |
245 | 230 |
246 /** | 231 /** |
247 * Serializes the subscription for writing out on disk. | 232 * Serializes the subscription for writing out on disk. |
248 * @yields {string} | 233 * @yields {string} |
249 */ | 234 */ |
250 *serialize() | 235 *serialize() |
251 { | 236 { |
252 let {url, type, _title, _fixedTitle, _disabled} = this; | 237 let {url, type, _title, _fixedTitle, _disabled} = this; |
253 | 238 |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
701 if (errors) | 686 if (errors) |
702 yield "errors=" + errors; | 687 yield "errors=" + errors; |
703 if (version) | 688 if (version) |
704 yield "version=" + version; | 689 yield "version=" + version; |
705 if (requiredVersion) | 690 if (requiredVersion) |
706 yield "requiredVersion=" + requiredVersion; | 691 yield "requiredVersion=" + requiredVersion; |
707 if (downloadCount) | 692 if (downloadCount) |
708 yield "downloadCount=" + downloadCount; | 693 yield "downloadCount=" + downloadCount; |
709 } | 694 } |
710 }); | 695 }); |
LEFT | RIGHT |