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 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 // This should be the last remaining reference to the Subscription | 186 // This should be the last remaining reference to the Subscription |
187 // object. | 187 // object. |
188 Subscription.knownSubscriptions.delete(subscription.url); | 188 Subscription.knownSubscriptions.delete(subscription.url); |
189 | 189 |
190 filterNotifier.emit("subscription.removed", subscription); | 190 filterNotifier.emit("subscription.removed", subscription); |
191 } | 191 } |
192 | 192 |
193 /** | 193 /** |
194 * Replaces the list of filters in a subscription with a new list. | 194 * Replaces the list of filters in a subscription with a new list. |
195 * @param {Subscription} subscription The subscription to be updated. | 195 * @param {Subscription} subscription The subscription to be updated. |
196 * @param {Array.<Filter>} filters The new list of filters. | 196 * @param {Array.<string>} filterText The new filter text. |
197 */ | 197 */ |
198 updateSubscriptionFilters(subscription, filters) | 198 updateSubscriptionFilters(subscription, filterText) |
199 { | 199 { |
200 let oldFilters = [...subscription.filters()]; | 200 let oldFilterText = [...subscription.filterText()]; |
201 disconnectSubscriptionFilters(subscription, oldFilters); | 201 disconnectSubscriptionFilters(subscription, oldFilterText); |
202 subscription.clearFilters(); | 202 subscription.clearFilters(); |
203 | 203 |
204 for (let filter of filters) | 204 for (let filter of filterText) |
205 subscription.addFilter(filter); | 205 subscription.addFilterText(filter); |
206 | 206 |
207 connectSubscriptionFilters(subscription, filters); | 207 connectSubscriptionFilters(subscription, filterText); |
208 | 208 |
209 filterNotifier.emit("subscription.updated", subscription, oldFilters); | 209 filterNotifier.emit("subscription.updated", subscription, oldFilterText); |
210 } | 210 } |
211 | 211 |
212 /** | 212 /** |
213 * Adds a user-defined filter to the storage. | 213 * Adds a user-defined filter to the storage. |
214 * @param {Filter} filter | 214 * @param {Filter} filter |
215 * @param {?SpecialSubscription} [subscription] The subscription that the | 215 * @param {?SpecialSubscription} [subscription] The subscription that the |
216 * filter should be added to. | 216 * filter should be added to. |
217 * @param {number} [position] The position within the subscription at which | 217 * @param {number} [position] The position within the subscription at which |
218 * the filter should be added. If not specified, the filter is added at the | 218 * the filter should be added. If not specified, the filter is added at the |
219 * end of the subscription. | 219 * end of the subscription. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 if (index >= 0) | 277 if (index >= 0) |
278 positions.push(index); | 278 positions.push(index); |
279 } while (index >= 0); | 279 } while (index >= 0); |
280 } | 280 } |
281 else | 281 else |
282 positions.push(position); | 282 positions.push(position); |
283 | 283 |
284 for (let j = positions.length - 1; j >= 0; j--) | 284 for (let j = positions.length - 1; j >= 0; j--) |
285 { | 285 { |
286 let currentPosition = positions[j]; | 286 let currentPosition = positions[j]; |
287 let currentFilter = currentSubscription.filterAt(currentPosition); | 287 let currentFilterText = |
288 if (currentFilter && currentFilter.text == filter.text) | 288 currentSubscription.filterTextAt(currentPosition); |
| 289 if (currentFilterText && currentFilterText == filter.text) |
289 { | 290 { |
290 currentSubscription.deleteFilterAt(currentPosition); | 291 currentSubscription.deleteFilterAt(currentPosition); |
291 if (currentSubscription.searchFilter(filter) < 0) | 292 if (currentSubscription.searchFilter(filter) < 0) |
292 filter.removeSubscription(currentSubscription); | 293 filter.removeSubscription(currentSubscription); |
293 filterNotifier.emit("filter.removed", filter, currentSubscription, | 294 filterNotifier.emit("filter.removed", filter, currentSubscription, |
294 currentPosition); | 295 currentPosition); |
295 } | 296 } |
296 } | 297 } |
297 } | 298 } |
298 } | 299 } |
299 } | 300 } |
300 | 301 |
301 /** | 302 /** |
302 * Moves a user-defined filter to a new position. | 303 * Moves a user-defined filter to a new position. |
303 * @param {Filter} filter | 304 * @param {Filter} filter |
304 * @param {SpecialSubscription} subscription The subscription where the | 305 * @param {SpecialSubscription} subscription The subscription where the |
305 * filter is located. | 306 * filter is located. |
306 * @param {number} oldPosition The current position of the filter. | 307 * @param {number} oldPosition The current position of the filter. |
307 * @param {number} newPosition The new position of the filter. | 308 * @param {number} newPosition The new position of the filter. |
308 */ | 309 */ |
309 moveFilter(filter, subscription, oldPosition, newPosition) | 310 moveFilter(filter, subscription, oldPosition, newPosition) |
310 { | 311 { |
311 if (!(subscription instanceof SpecialSubscription)) | 312 if (!(subscription instanceof SpecialSubscription)) |
312 return; | 313 return; |
313 | 314 |
314 let currentFilter = subscription.filterAt(oldPosition); | 315 let currentFilterText = subscription.filterTextAt(oldPosition); |
315 if (!currentFilter || currentFilter.text != filter.text) | 316 if (!currentFilterText || currentFilterText != filter.text) |
316 return; | 317 return; |
317 | 318 |
318 newPosition = Math.min(Math.max(newPosition, 0), | 319 newPosition = Math.min(Math.max(newPosition, 0), |
319 subscription.filterCount - 1); | 320 subscription.filterCount - 1); |
320 if (oldPosition == newPosition) | 321 if (oldPosition == newPosition) |
321 return; | 322 return; |
322 | 323 |
323 subscription.deleteFilterAt(oldPosition); | 324 subscription.deleteFilterAt(oldPosition); |
324 subscription.insertFilterAt(filter, newPosition); | 325 subscription.insertFilterAt(filter, newPosition); |
325 filterNotifier.emit("filter.moved", filter, subscription, oldPosition, | 326 filterNotifier.emit("filter.moved", filter, subscription, oldPosition, |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 // Save subscriptions | 498 // Save subscriptions |
498 for (let subscription of subscriptions) | 499 for (let subscription of subscriptions) |
499 { | 500 { |
500 yield* subscription.serialize(); | 501 yield* subscription.serialize(); |
501 yield* subscription.serializeFilters(); | 502 yield* subscription.serializeFilters(); |
502 } | 503 } |
503 | 504 |
504 // Save filter data | 505 // Save filter data |
505 for (let subscription of subscriptions) | 506 for (let subscription of subscriptions) |
506 { | 507 { |
507 for (let filter of subscription.filters()) | 508 for (let text of subscription.filterText()) |
508 { | 509 { |
509 if (!saved.has(filter.text)) | 510 if (!saved.has(text)) |
510 { | 511 { |
511 yield* filter.serialize(); | 512 yield* Filter.fromText(text).serialize(); |
512 saved.add(filter.text); | 513 saved.add(text); |
513 } | 514 } |
514 } | 515 } |
515 } | 516 } |
516 } | 517 } |
517 | 518 |
518 /** | 519 /** |
519 * Saves all subscriptions back to disk. | 520 * Saves all subscriptions back to disk. |
520 * @returns {Promise} A promise resolved or rejected when saving is complete. | 521 * @returns {Promise} A promise resolved or rejected when saving is complete. |
521 */ | 522 */ |
522 saveToDisk() | 523 saveToDisk() |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 * back to disk. | 648 * back to disk. |
648 */ | 649 */ |
649 let filterStorage = new FilterStorage(); | 650 let filterStorage = new FilterStorage(); |
650 | 651 |
651 exports.filterStorage = filterStorage; | 652 exports.filterStorage = filterStorage; |
652 | 653 |
653 /** | 654 /** |
654 * Connects a subscription to its filters without any notifications. | 655 * Connects a subscription to its filters without any notifications. |
655 * @param {Subscription} subscription The subscription that should be | 656 * @param {Subscription} subscription The subscription that should be |
656 * connected to its filters. | 657 * connected to its filters. |
657 * @param {?Array.<Filter>} [filters] A list of filters to which the | 658 * @param {?Array.<string>} [filters] A list of filters to which the |
658 * subscription should be connected. If this is not given, the subscription | 659 * subscription should be connected. If this is not given, the subscription |
659 * is connected to its own filters. | 660 * is connected to its own filters. |
660 */ | 661 */ |
661 function connectSubscriptionFilters(subscription, filters) | 662 function connectSubscriptionFilters(subscription, filters) |
662 { | 663 { |
663 if (!filterStorage.knownSubscriptions.has(subscription.url)) | 664 if (!filterStorage.knownSubscriptions.has(subscription.url)) |
664 return; | 665 return; |
665 | 666 |
666 for (let filter of filters || subscription.filters()) | 667 for (let text of filters || subscription.filterText()) |
| 668 { |
| 669 let filter = Filter.fromText(text); |
667 filter.addSubscription(subscription); | 670 filter.addSubscription(subscription); |
| 671 } |
668 } | 672 } |
669 | 673 |
670 /** | 674 /** |
671 * Disconnects a subscription from its filters without any notifications. | 675 * Disconnects a subscription from its filters without any notifications. |
672 * @param {Subscription} subscription The subscription that should be | 676 * @param {Subscription} subscription The subscription that should be |
673 * disconnected from its filters. | 677 * disconnected from its filters. |
674 * @param {?Array.<Filter>} [filters] A list of filters from which the | 678 * @param {?Array.<string>} [filters] A list of filters from which the |
675 * subscription should be disconnected. If this is not given, the | 679 * subscription should be disconnected. If this is not given, the |
676 * subscription is disconnected from its own filters. | 680 * subscription is disconnected from its own filters. |
677 */ | 681 */ |
678 function disconnectSubscriptionFilters(subscription, filters) | 682 function disconnectSubscriptionFilters(subscription, filters) |
679 { | 683 { |
680 if (!filterStorage.knownSubscriptions.has(subscription.url)) | 684 if (!filterStorage.knownSubscriptions.has(subscription.url)) |
681 return; | 685 return; |
682 | 686 |
683 for (let filter of filters || subscription.filters()) | 687 for (let text of filters || subscription.filterText()) |
| 688 { |
| 689 let filter = Filter.fromText(text); |
684 filter.removeSubscription(subscription); | 690 filter.removeSubscription(subscription); |
| 691 } |
685 } | 692 } |
OLD | NEW |