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 |
(...skipping 18 matching lines...) Expand all Loading... |
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 this.filters = []; | 39 |
| 40 this._filterText = []; |
| 41 this._filters = []; |
| 42 |
40 if (title) | 43 if (title) |
41 this._title = title; | 44 this._title = title; |
| 45 |
42 Subscription.knownSubscriptions.set(url, this); | 46 Subscription.knownSubscriptions.set(url, this); |
43 } | 47 } |
44 exports.Subscription = Subscription; | 48 exports.Subscription = Subscription; |
45 | 49 |
46 Subscription.prototype = | 50 Subscription.prototype = |
47 { | 51 { |
48 /** | 52 /** |
49 * Download location of the subscription | 53 * Download location of the subscription |
50 * @type {string} | 54 * @type {string} |
51 */ | 55 */ |
52 url: null, | 56 url: null, |
53 | 57 |
54 /** | 58 /** |
55 * Type of the subscription | 59 * Type of the subscription |
56 * @type {?string} | 60 * @type {?string} |
57 */ | 61 */ |
58 type: null, | 62 type: null, |
59 | 63 |
60 /** | 64 /** |
61 * Filters contained in the filter subscription | 65 * Filter text contained in the filter subscription. |
62 * @type {Filter[]} | 66 * @type {Array.<string>} |
| 67 * @private |
63 */ | 68 */ |
64 filters: null, | 69 _filterText: null, |
| 70 |
| 71 /** |
| 72 * {@link Filter} objects corresponding to the subscription's filter text. |
| 73 * @type {Array.<Filter>} |
| 74 * @private |
| 75 */ |
| 76 _filters: null, |
| 77 |
| 78 /** |
| 79 * Set of filter text contained in the filter subscription, used for faster |
| 80 * lookup. |
| 81 * @type {Set.<string>} |
| 82 * @private |
| 83 */ |
| 84 _filterTextLookup: null, |
65 | 85 |
66 _title: null, | 86 _title: null, |
67 _fixedTitle: false, | 87 _fixedTitle: false, |
68 _disabled: false, | 88 _disabled: false, |
69 | 89 |
70 /** | 90 /** |
71 * Title of the filter subscription | 91 * Title of the filter subscription |
72 * @type {string} | 92 * @type {string} |
73 */ | 93 */ |
74 get title() | 94 get title() |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 if (value != this._disabled) | 138 if (value != this._disabled) |
119 { | 139 { |
120 let oldValue = this._disabled; | 140 let oldValue = this._disabled; |
121 this._disabled = value; | 141 this._disabled = value; |
122 filterNotifier.emit("subscription.disabled", this, value, oldValue); | 142 filterNotifier.emit("subscription.disabled", this, value, oldValue); |
123 } | 143 } |
124 return this._disabled; | 144 return this._disabled; |
125 }, | 145 }, |
126 | 146 |
127 /** | 147 /** |
| 148 * The number of filters in the subscription. |
| 149 * @type {number} |
| 150 */ |
| 151 get filterCount() |
| 152 { |
| 153 return this._filters.length; |
| 154 }, |
| 155 |
| 156 /** |
| 157 * Yields the text for each filter in the subscription. |
| 158 * @yields {string} |
| 159 */ |
| 160 *filterText() |
| 161 { |
| 162 yield* this._filterText; |
| 163 }, |
| 164 |
| 165 /** |
| 166 * Yields the {@link Filter} object for each filter in the subscription. |
| 167 * @yields {Filter} |
| 168 */ |
| 169 *filters() |
| 170 { |
| 171 yield* this._filters; |
| 172 }, |
| 173 |
| 174 /** |
| 175 * Returns the {@link Filter} object at the given 0-based index. |
| 176 * @param {number} index |
| 177 * @returns {?Filter} |
| 178 */ |
| 179 filterAt(index) |
| 180 { |
| 181 return this._filters[index] || null; |
| 182 }, |
| 183 |
| 184 /** |
| 185 * Returns the 0-based index of the given filter. |
| 186 * @param {Filter} filter |
| 187 * @param {number} [fromIndex] The index from which to start the search. |
| 188 * @return {number} |
| 189 */ |
| 190 searchFilter(filter, fromIndex = 0) |
| 191 { |
| 192 return this._filterText.indexOf(filter.text, fromIndex); |
| 193 }, |
| 194 |
| 195 /** |
| 196 * Checks whether the subscription contains the given filter. |
| 197 * @param {Filter} filter |
| 198 * @return {boolean} |
| 199 */ |
| 200 hasFilter(filter) |
| 201 { |
| 202 if (!this._filterTextLookup) |
| 203 this._filterTextLookup = new Set([...this._filterText]); |
| 204 |
| 205 return this._filterTextLookup.has(filter.text); |
| 206 }, |
| 207 |
| 208 /** |
| 209 * Removes all filters from the subscription. |
| 210 */ |
| 211 clearFilters() |
| 212 { |
| 213 this._filterText = []; |
| 214 this._filters = []; |
| 215 |
| 216 this._filterTextLookup = null; |
| 217 }, |
| 218 |
| 219 /** |
| 220 * Adds a filter to the subscription. |
| 221 * @param {Filter} filter |
| 222 */ |
| 223 addFilter(filter) |
| 224 { |
| 225 this._filterText.push(filter.text); |
| 226 this._filters.push(filter); |
| 227 |
| 228 // Invalidate filter text lookup. |
| 229 this._filterTextLookup = null; |
| 230 }, |
| 231 |
| 232 /** |
| 233 * Inserts a filter into the subscription. |
| 234 * @param {Filter} filter |
| 235 * @param {number} index The index at which to insert the filter. |
| 236 */ |
| 237 insertFilterAt(filter, index) |
| 238 { |
| 239 this._filterText.splice(index, 0, filter.text); |
| 240 this._filters.splice(index, 0, filter); |
| 241 |
| 242 this._filterTextLookup = null; |
| 243 }, |
| 244 |
| 245 /** |
| 246 * Deletes a filter from the subscription. |
| 247 * @param {number} index The index at which to delete the filter. |
| 248 */ |
| 249 deleteFilterAt(index) |
| 250 { |
| 251 // Ignore index if out of bounds on the negative side, for consistency. |
| 252 if (index < 0) |
| 253 return; |
| 254 |
| 255 this._filterText.splice(index, 1); |
| 256 this._filters.splice(index, 1); |
| 257 |
| 258 this._filterTextLookup = null; |
| 259 }, |
| 260 |
| 261 /** |
| 262 * Clears any in-memory caches held by the object. |
| 263 * @package |
| 264 */ |
| 265 clearCaches() |
| 266 { |
| 267 this._filterTextLookup = null; |
| 268 }, |
| 269 |
| 270 /** |
128 * Serializes the subscription for writing out on disk. | 271 * Serializes the subscription for writing out on disk. |
129 * @yields {string} | 272 * @yields {string} |
130 */ | 273 */ |
131 *serialize() | 274 *serialize() |
132 { | 275 { |
133 let {url, type, _title, _fixedTitle, _disabled} = this; | 276 let {url, type, _title, _fixedTitle, _disabled} = this; |
134 | 277 |
135 yield "[Subscription]"; | 278 yield "[Subscription]"; |
136 yield "url=" + url; | 279 yield "url=" + url; |
137 | 280 |
138 if (type) | 281 if (type) |
139 yield "type=" + type; | 282 yield "type=" + type; |
140 if (_title) | 283 if (_title) |
141 yield "title=" + _title; | 284 yield "title=" + _title; |
142 if (_fixedTitle) | 285 if (_fixedTitle) |
143 yield "fixedTitle=true"; | 286 yield "fixedTitle=true"; |
144 if (_disabled) | 287 if (_disabled) |
145 yield "disabled=true"; | 288 yield "disabled=true"; |
146 }, | 289 }, |
147 | 290 |
148 *serializeFilters() | 291 *serializeFilters() |
149 { | 292 { |
150 let {filters} = this; | 293 let {_filterText} = this; |
151 | 294 |
152 yield "[Subscription filters]"; | 295 yield "[Subscription filters]"; |
153 | 296 |
154 for (let filter of filters) | 297 for (let text of _filterText) |
155 yield filter.text.replace(/\[/g, "\\["); | 298 yield text.replace(/\[/g, "\\["); |
156 }, | 299 }, |
157 | 300 |
158 toString() | 301 toString() |
159 { | 302 { |
160 return [...this.serialize()].join("\n"); | 303 return [...this.serialize()].join("\n"); |
161 } | 304 } |
162 }; | 305 }; |
163 | 306 |
164 /** | 307 /** |
165 * Cache for known filter subscriptions, maps URL to subscription objects. | 308 * Cache for known filter subscriptions, maps URL to subscription objects. |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 | 469 |
327 /** | 470 /** |
328 * Creates a new user-defined filter group and adds the given filter to it. | 471 * Creates a new user-defined filter group and adds the given filter to it. |
329 * This group will act as the default group for this filter type. | 472 * This group will act as the default group for this filter type. |
330 * @param {Filter} filter | 473 * @param {Filter} filter |
331 * @return {SpecialSubscription} | 474 * @return {SpecialSubscription} |
332 */ | 475 */ |
333 SpecialSubscription.createForFilter = function(filter) | 476 SpecialSubscription.createForFilter = function(filter) |
334 { | 477 { |
335 let subscription = SpecialSubscription.create(); | 478 let subscription = SpecialSubscription.create(); |
336 subscription.filters.push(filter); | 479 subscription.addFilter(filter); |
337 for (let [type, class_] of SpecialSubscription.defaultsMap) | 480 for (let [type, class_] of SpecialSubscription.defaultsMap) |
338 { | 481 { |
339 if (filter instanceof class_) | 482 if (filter instanceof class_) |
340 subscription.defaults = [type]; | 483 subscription.defaults = [type]; |
341 } | 484 } |
342 if (!subscription.defaults) | 485 if (!subscription.defaults) |
343 subscription.defaults = ["blocking"]; | 486 subscription.defaults = ["blocking"]; |
344 return subscription; | 487 return subscription; |
345 }; | 488 }; |
346 | 489 |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 if (errors) | 725 if (errors) |
583 yield "errors=" + errors; | 726 yield "errors=" + errors; |
584 if (version) | 727 if (version) |
585 yield "version=" + version; | 728 yield "version=" + version; |
586 if (requiredVersion) | 729 if (requiredVersion) |
587 yield "requiredVersion=" + requiredVersion; | 730 yield "requiredVersion=" + requiredVersion; |
588 if (downloadCount) | 731 if (downloadCount) |
589 yield "downloadCount=" + downloadCount; | 732 yield "downloadCount=" + downloadCount; |
590 } | 733 } |
591 }); | 734 }); |
OLD | NEW |