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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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.<Filter>} filters The new list of filters. |
197 */ | 197 */ |
198 updateSubscriptionFilters(subscription, filters) | 198 updateSubscriptionFilters(subscription, filters) |
199 { | 199 { |
200 disconnectSubscriptionFilters(subscription); | 200 disconnectSubscriptionFilters(subscription); |
| 201 |
201 let oldFilters = subscription.filters; | 202 let oldFilters = subscription.filters; |
202 subscription.filters = filters; | 203 subscription.filters = []; |
203 connectSubscriptionFilters(subscription); | 204 for (let filter of filters) |
| 205 subscription.filters.push(filter.text); |
| 206 |
| 207 connectSubscriptionFilters(subscription, filters); |
204 filterNotifier.emit("subscription.updated", subscription, oldFilters); | 208 filterNotifier.emit("subscription.updated", subscription, oldFilters); |
205 } | 209 } |
206 | 210 |
207 /** | 211 /** |
208 * Adds a user-defined filter to the storage. | 212 * Adds a user-defined filter to the storage. |
209 * @param {Filter} filter | 213 * @param {Filter} filter |
210 * @param {?SpecialSubscription} [subscription] The subscription that the | 214 * @param {?SpecialSubscription} [subscription] The subscription that the |
211 * filter should be added to. | 215 * filter should be added to. |
212 * @param {number} [position] The position within the subscription at which | 216 * @param {number} [position] The position within the subscription at which |
213 * the filter should be added. If not specified, the filter is added at the | 217 * the filter should be added. If not specified, the filter is added at the |
(...skipping 18 matching lines...) Expand all Loading... |
232 // No group for this filter exists, create one | 236 // No group for this filter exists, create one |
233 subscription = SpecialSubscription.createForFilter(filter); | 237 subscription = SpecialSubscription.createForFilter(filter); |
234 this.addSubscription(subscription); | 238 this.addSubscription(subscription); |
235 return; | 239 return; |
236 } | 240 } |
237 | 241 |
238 if (typeof position == "undefined") | 242 if (typeof position == "undefined") |
239 position = subscription.filters.length; | 243 position = subscription.filters.length; |
240 | 244 |
241 filter.addSubscription(subscription); | 245 filter.addSubscription(subscription); |
242 subscription.filters.splice(position, 0, filter); | 246 subscription.filters.splice(position, 0, filter.text); |
243 filterNotifier.emit("filter.added", filter, subscription, position); | 247 filterNotifier.emit("filter.added", filter, subscription, position); |
244 } | 248 } |
245 | 249 |
246 /** | 250 /** |
247 * Removes a user-defined filter from the storage. | 251 * Removes a user-defined filter from the storage. |
248 * @param {Filter} filter | 252 * @param {Filter} filter |
249 * @param {?SpecialSubscription} [subscription] The subscription that the | 253 * @param {?SpecialSubscription} [subscription] The subscription that the |
250 * filter should be removed from. If not specified, the filter will be | 254 * filter should be removed from. If not specified, the filter will be |
251 * removed from all subscriptions. | 255 * removed from all subscriptions. |
252 * @param {number} [position] The position within the subscription at which | 256 * @param {number} [position] The position within the subscription at which |
253 * the filter should be removed. If not specified, all instances of the | 257 * the filter should be removed. If not specified, all instances of the |
254 * filter will be removed. | 258 * filter will be removed. |
255 */ | 259 */ |
256 removeFilter(filter, subscription, position) | 260 removeFilter(filter, subscription, position) |
257 { | 261 { |
258 let subscriptions = ( | 262 let subscriptions = ( |
259 subscription ? [subscription] : filter.subscriptions() | 263 subscription ? [subscription] : filter.subscriptions() |
260 ); | 264 ); |
261 for (let currentSubscription of subscriptions) | 265 for (let currentSubscription of subscriptions) |
262 { | 266 { |
263 if (currentSubscription instanceof SpecialSubscription) | 267 if (currentSubscription instanceof SpecialSubscription) |
264 { | 268 { |
265 let positions = []; | 269 let positions = []; |
266 if (typeof position == "undefined") | 270 if (typeof position == "undefined") |
267 { | 271 { |
268 let index = -1; | 272 let index = -1; |
269 do | 273 do |
270 { | 274 { |
271 index = currentSubscription.filters.indexOf(filter, index + 1); | 275 index = currentSubscription.filters.indexOf(filter.text, index + 1); |
272 if (index >= 0) | 276 if (index >= 0) |
273 positions.push(index); | 277 positions.push(index); |
274 } while (index >= 0); | 278 } while (index >= 0); |
275 } | 279 } |
276 else | 280 else |
277 positions.push(position); | 281 positions.push(position); |
278 | 282 |
279 for (let j = positions.length - 1; j >= 0; j--) | 283 for (let j = positions.length - 1; j >= 0; j--) |
280 { | 284 { |
281 let currentPosition = positions[j]; | 285 let currentPosition = positions[j]; |
282 if (currentSubscription.filters[currentPosition] == filter) | 286 if (currentSubscription.filters[currentPosition] == filter.text) |
283 { | 287 { |
284 currentSubscription.filters.splice(currentPosition, 1); | 288 currentSubscription.filters.splice(currentPosition, 1); |
285 if (currentSubscription.filters.indexOf(filter) < 0) | 289 if (currentSubscription.filters.indexOf(filter.text) < 0) |
286 filter.removeSubscription(currentSubscription); | 290 filter.removeSubscription(currentSubscription); |
287 filterNotifier.emit("filter.removed", filter, currentSubscription, | 291 filterNotifier.emit("filter.removed", filter, currentSubscription, |
288 currentPosition); | 292 currentPosition); |
289 } | 293 } |
290 } | 294 } |
291 } | 295 } |
292 } | 296 } |
293 } | 297 } |
294 | 298 |
295 /** | 299 /** |
296 * Moves a user-defined filter to a new position. | 300 * Moves a user-defined filter to a new position. |
297 * @param {Filter} filter | 301 * @param {Filter} filter |
298 * @param {SpecialSubscription} subscription The subscription where the | 302 * @param {SpecialSubscription} subscription The subscription where the |
299 * filter is located. | 303 * filter is located. |
300 * @param {number} oldPosition The current position of the filter. | 304 * @param {number} oldPosition The current position of the filter. |
301 * @param {number} newPosition The new position of the filter. | 305 * @param {number} newPosition The new position of the filter. |
302 */ | 306 */ |
303 moveFilter(filter, subscription, oldPosition, newPosition) | 307 moveFilter(filter, subscription, oldPosition, newPosition) |
304 { | 308 { |
305 if (!(subscription instanceof SpecialSubscription) || | 309 if (!(subscription instanceof SpecialSubscription) || |
306 subscription.filters[oldPosition] != filter) | 310 subscription.filters[oldPosition] != filter.text) |
307 { | 311 { |
308 return; | 312 return; |
309 } | 313 } |
310 | 314 |
311 newPosition = Math.min(Math.max(newPosition, 0), | 315 newPosition = Math.min(Math.max(newPosition, 0), |
312 subscription.filters.length - 1); | 316 subscription.filters.length - 1); |
313 if (oldPosition == newPosition) | 317 if (oldPosition == newPosition) |
314 return; | 318 return; |
315 | 319 |
316 subscription.filters.splice(oldPosition, 1); | 320 subscription.filters.splice(oldPosition, 1); |
317 subscription.filters.splice(newPosition, 0, filter); | 321 subscription.filters.splice(newPosition, 0, filter.text); |
318 filterNotifier.emit("filter.moved", filter, subscription, oldPosition, | 322 filterNotifier.emit("filter.moved", filter, subscription, oldPosition, |
319 newPosition); | 323 newPosition); |
320 } | 324 } |
321 | 325 |
322 /** | 326 /** |
323 * Increases the hit count for a filter by one. | 327 * Increases the hit count for a filter by one. |
324 * @param {Filter} filter | 328 * @param {Filter} filter |
325 */ | 329 */ |
326 increaseHitCount(filter) | 330 increaseHitCount(filter) |
327 { | 331 { |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 // Save subscriptions | 494 // Save subscriptions |
491 for (let subscription of subscriptions) | 495 for (let subscription of subscriptions) |
492 { | 496 { |
493 yield* subscription.serialize(); | 497 yield* subscription.serialize(); |
494 yield* subscription.serializeFilters(); | 498 yield* subscription.serializeFilters(); |
495 } | 499 } |
496 | 500 |
497 // Save filter data | 501 // Save filter data |
498 for (let subscription of subscriptions) | 502 for (let subscription of subscriptions) |
499 { | 503 { |
500 for (let filter of subscription.filters) | 504 for (let text of subscription.filters) |
501 { | 505 { |
502 if (!saved.has(filter.text)) | 506 if (!saved.has(text)) |
503 { | 507 { |
504 yield* filter.serialize(); | 508 yield* Filter.fromText(text).serialize(); |
505 saved.add(filter.text); | 509 saved.add(text); |
506 } | 510 } |
507 } | 511 } |
508 } | 512 } |
509 } | 513 } |
510 | 514 |
511 /** | 515 /** |
512 * Saves all subscriptions back to disk. | 516 * Saves all subscriptions back to disk. |
513 * @returns {Promise} A promise resolved or rejected when saving is complete. | 517 * @returns {Promise} A promise resolved or rejected when saving is complete. |
514 */ | 518 */ |
515 saveToDisk() | 519 saveToDisk() |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
640 * back to disk. | 644 * back to disk. |
641 */ | 645 */ |
642 let filterStorage = new FilterStorage(); | 646 let filterStorage = new FilterStorage(); |
643 | 647 |
644 exports.filterStorage = filterStorage; | 648 exports.filterStorage = filterStorage; |
645 | 649 |
646 /** | 650 /** |
647 * Connects a subscription to its filters without any notifications. | 651 * Connects a subscription to its filters without any notifications. |
648 * @param {Subscription} subscription The subscription that should be | 652 * @param {Subscription} subscription The subscription that should be |
649 * connected to its filters. | 653 * connected to its filters. |
| 654 * @param {?Array.<Filter>} [filters] A list of filters to which the |
| 655 * subscription should be connected. If this is not given, the subscription |
| 656 * is connected to its own filters. |
650 */ | 657 */ |
651 function connectSubscriptionFilters(subscription) | 658 function connectSubscriptionFilters(subscription, filters) |
652 { | 659 { |
653 if (!filterStorage.knownSubscriptions.has(subscription.url)) | 660 if (!filterStorage.knownSubscriptions.has(subscription.url)) |
654 return; | 661 return; |
655 | 662 |
656 for (let filter of subscription.filters) | 663 if (filters) |
657 filter.addSubscription(subscription); | 664 { |
| 665 for (let filter of filters) |
| 666 filter.addSubscription(subscription); |
| 667 } |
| 668 else |
| 669 { |
| 670 for (let text of subscription.filters) |
| 671 Filter.fromText(text).addSubscription(subscription); |
| 672 } |
658 } | 673 } |
659 | 674 |
660 /** | 675 /** |
661 * Disconnects a subscription from its filters without any notifications. | 676 * Disconnects a subscription from its filters without any notifications. |
662 * @param {Subscription} subscription The subscription that should be | 677 * @param {Subscription} subscription The subscription that should be |
663 * disconnected from its filters. | 678 * disconnected from its filters. |
| 679 * @param {?Array.<Filter>} [filters] A list of filters from which the |
| 680 * subscription should be disconnected. If this is not given, the |
| 681 * subscription is disconnected from its own filters. |
664 */ | 682 */ |
665 function disconnectSubscriptionFilters(subscription) | 683 function disconnectSubscriptionFilters(subscription, filters) |
666 { | 684 { |
667 if (!filterStorage.knownSubscriptions.has(subscription.url)) | 685 if (!filterStorage.knownSubscriptions.has(subscription.url)) |
668 return; | 686 return; |
669 | 687 |
670 for (let filter of subscription.filters) | 688 if (filters) |
671 filter.removeSubscription(subscription); | 689 { |
| 690 for (let filter of filters) |
| 691 filter.removeSubscription(subscription); |
| 692 } |
| 693 else |
| 694 { |
| 695 for (let text of subscription.filters) |
| 696 Filter.fromText(text).removeSubscription(subscription); |
| 697 } |
672 } | 698 } |
OLD | NEW |