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 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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.<Filter>} filters The new list of filters. |
197 */ | 197 */ |
198 updateSubscriptionFilters(subscription, filters) | 198 updateSubscriptionFilters(subscription, filters) |
199 { | 199 { |
200 disconnectSubscriptionFilters(subscription); | 200 let oldFilters = [...subscription.filters()]; |
201 let oldFilters = subscription.filters; | 201 disconnectSubscriptionFilters(subscription, oldFilters); |
202 subscription.filters = filters; | 202 subscription.clearFilters(); |
203 connectSubscriptionFilters(subscription); | 203 |
| 204 for (let filter of filters) |
| 205 subscription.addFilter(filter); |
| 206 |
| 207 connectSubscriptionFilters(subscription, filters); |
| 208 |
204 filterNotifier.emit("subscription.updated", subscription, oldFilters); | 209 filterNotifier.emit("subscription.updated", subscription, oldFilters); |
205 } | 210 } |
206 | 211 |
207 /** | 212 /** |
208 * Adds a user-defined filter to the storage. | 213 * Adds a user-defined filter to the storage. |
209 * @param {Filter} filter | 214 * @param {Filter} filter |
210 * @param {?SpecialSubscription} [subscription] The subscription that the | 215 * @param {?SpecialSubscription} [subscription] The subscription that the |
211 * filter should be added to. | 216 * filter should be added to. |
212 * @param {number} [position] The position within the subscription at which | 217 * @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 | 218 * the filter should be added. If not specified, the filter is added at the |
(...skipping 15 matching lines...) Expand all Loading... |
229 } | 234 } |
230 if (!subscription) | 235 if (!subscription) |
231 { | 236 { |
232 // No group for this filter exists, create one | 237 // No group for this filter exists, create one |
233 subscription = SpecialSubscription.createForFilter(filter); | 238 subscription = SpecialSubscription.createForFilter(filter); |
234 this.addSubscription(subscription); | 239 this.addSubscription(subscription); |
235 return; | 240 return; |
236 } | 241 } |
237 | 242 |
238 if (typeof position == "undefined") | 243 if (typeof position == "undefined") |
239 position = subscription.filters.length; | 244 position = subscription.filterCount; |
240 | 245 |
241 filter.addSubscription(subscription); | 246 filter.addSubscription(subscription); |
242 subscription.filters.splice(position, 0, filter); | 247 subscription.insertFilterAt(filter, position); |
243 filterNotifier.emit("filter.added", filter, subscription, position); | 248 filterNotifier.emit("filter.added", filter, subscription, position); |
244 } | 249 } |
245 | 250 |
246 /** | 251 /** |
247 * Removes a user-defined filter from the storage. | 252 * Removes a user-defined filter from the storage. |
248 * @param {Filter} filter | 253 * @param {Filter} filter |
249 * @param {?SpecialSubscription} [subscription] The subscription that the | 254 * @param {?SpecialSubscription} [subscription] The subscription that the |
250 * filter should be removed from. If not specified, the filter will be | 255 * filter should be removed from. If not specified, the filter will be |
251 * removed from all subscriptions. | 256 * removed from all subscriptions. |
252 * @param {number} [position] The position within the subscription at which | 257 * @param {number} [position] The position within the subscription at which |
253 * the filter should be removed. If not specified, all instances of the | 258 * the filter should be removed. If not specified, all instances of the |
254 * filter will be removed. | 259 * filter will be removed. |
255 */ | 260 */ |
256 removeFilter(filter, subscription, position) | 261 removeFilter(filter, subscription, position) |
257 { | 262 { |
258 let subscriptions = ( | 263 let subscriptions = ( |
259 subscription ? [subscription] : filter.subscriptions() | 264 subscription ? [subscription] : filter.subscriptions() |
260 ); | 265 ); |
261 for (let currentSubscription of subscriptions) | 266 for (let currentSubscription of subscriptions) |
262 { | 267 { |
263 if (currentSubscription instanceof SpecialSubscription) | 268 if (currentSubscription instanceof SpecialSubscription) |
264 { | 269 { |
265 let positions = []; | 270 let positions = []; |
266 if (typeof position == "undefined") | 271 if (typeof position == "undefined") |
267 { | 272 { |
268 let index = -1; | 273 let index = -1; |
269 do | 274 do |
270 { | 275 { |
271 index = currentSubscription.filters.indexOf(filter, index + 1); | 276 index = currentSubscription.searchFilter(filter, index + 1); |
272 if (index >= 0) | 277 if (index >= 0) |
273 positions.push(index); | 278 positions.push(index); |
274 } while (index >= 0); | 279 } while (index >= 0); |
275 } | 280 } |
276 else | 281 else |
277 positions.push(position); | 282 positions.push(position); |
278 | 283 |
279 for (let j = positions.length - 1; j >= 0; j--) | 284 for (let j = positions.length - 1; j >= 0; j--) |
280 { | 285 { |
281 let currentPosition = positions[j]; | 286 let currentPosition = positions[j]; |
282 if (currentSubscription.filters[currentPosition] == filter) | 287 let currentFilter = currentSubscription.filterAt(currentPosition); |
| 288 if (currentFilter && currentFilter.text == filter.text) |
283 { | 289 { |
284 currentSubscription.filters.splice(currentPosition, 1); | 290 currentSubscription.deleteFilterAt(currentPosition); |
285 if (currentSubscription.filters.indexOf(filter) < 0) | 291 if (currentSubscription.searchFilter(filter) < 0) |
286 filter.removeSubscription(currentSubscription); | 292 filter.removeSubscription(currentSubscription); |
287 filterNotifier.emit("filter.removed", filter, currentSubscription, | 293 filterNotifier.emit("filter.removed", filter, currentSubscription, |
288 currentPosition); | 294 currentPosition); |
289 } | 295 } |
290 } | 296 } |
291 } | 297 } |
292 } | 298 } |
293 } | 299 } |
294 | 300 |
295 /** | 301 /** |
296 * Moves a user-defined filter to a new position. | 302 * Moves a user-defined filter to a new position. |
297 * @param {Filter} filter | 303 * @param {Filter} filter |
298 * @param {SpecialSubscription} subscription The subscription where the | 304 * @param {SpecialSubscription} subscription The subscription where the |
299 * filter is located. | 305 * filter is located. |
300 * @param {number} oldPosition The current position of the filter. | 306 * @param {number} oldPosition The current position of the filter. |
301 * @param {number} newPosition The new position of the filter. | 307 * @param {number} newPosition The new position of the filter. |
302 */ | 308 */ |
303 moveFilter(filter, subscription, oldPosition, newPosition) | 309 moveFilter(filter, subscription, oldPosition, newPosition) |
304 { | 310 { |
305 if (!(subscription instanceof SpecialSubscription) || | 311 if (!(subscription instanceof SpecialSubscription)) |
306 subscription.filters[oldPosition] != filter) | 312 return; |
307 { | 313 |
308 return; | 314 let currentFilter = subscription.filterAt(oldPosition); |
309 } | 315 if (!currentFilter || currentFilter.text != filter.text) |
| 316 return; |
310 | 317 |
311 newPosition = Math.min(Math.max(newPosition, 0), | 318 newPosition = Math.min(Math.max(newPosition, 0), |
312 subscription.filters.length - 1); | 319 subscription.filterCount - 1); |
313 if (oldPosition == newPosition) | 320 if (oldPosition == newPosition) |
314 return; | 321 return; |
315 | 322 |
316 subscription.filters.splice(oldPosition, 1); | 323 subscription.deleteFilterAt(oldPosition); |
317 subscription.filters.splice(newPosition, 0, filter); | 324 subscription.insertFilterAt(filter, newPosition); |
318 filterNotifier.emit("filter.moved", filter, subscription, oldPosition, | 325 filterNotifier.emit("filter.moved", filter, subscription, oldPosition, |
319 newPosition); | 326 newPosition); |
320 } | 327 } |
321 | 328 |
322 /** | 329 /** |
323 * Increases the hit count for a filter by one. | 330 * Increases the hit count for a filter by one. |
324 * @param {Filter} filter | 331 * @param {Filter} filter |
325 */ | 332 */ |
326 increaseHitCount(filter) | 333 increaseHitCount(filter) |
327 { | 334 { |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 { | 487 { |
481 // Do not persist external subscriptions | 488 // Do not persist external subscriptions |
482 if (!(subscription instanceof ExternalSubscription) && | 489 if (!(subscription instanceof ExternalSubscription) && |
483 !(subscription instanceof SpecialSubscription && | 490 !(subscription instanceof SpecialSubscription && |
484 subscription.filters.length == 0)) | 491 subscription.filters.length == 0)) |
485 { | 492 { |
486 yield* subscription.serialize(); | 493 yield* subscription.serialize(); |
487 yield* subscription.serializeFilters(); | 494 yield* subscription.serializeFilters(); |
488 } | 495 } |
489 | 496 |
490 for (let filter of subscription.filters) | 497 // Save filter data |
| 498 for (let subscription of subscriptions) |
| 499 { |
| 500 for (let filter of subscription.filters()) |
491 { | 501 { |
492 // Save filter data | 502 // Save filter data |
493 if (!saved.has(filter.text)) | 503 if (!saved.has(filter.text)) |
494 { | 504 { |
495 yield* filter.serialize(); | 505 yield* filter.serialize(); |
496 saved.add(filter.text); | 506 saved.add(filter.text); |
497 } | 507 } |
498 } | 508 } |
499 } | 509 } |
500 } | 510 } |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 * back to disk. | 641 * back to disk. |
632 */ | 642 */ |
633 let filterStorage = new FilterStorage(); | 643 let filterStorage = new FilterStorage(); |
634 | 644 |
635 exports.filterStorage = filterStorage; | 645 exports.filterStorage = filterStorage; |
636 | 646 |
637 /** | 647 /** |
638 * Connects a subscription to its filters without any notifications. | 648 * Connects a subscription to its filters without any notifications. |
639 * @param {Subscription} subscription The subscription that should be | 649 * @param {Subscription} subscription The subscription that should be |
640 * connected to its filters. | 650 * connected to its filters. |
| 651 * @param {?Array.<Filter>} [filters] A list of filters to which the |
| 652 * subscription should be connected. If this is not given, the subscription |
| 653 * is connected to its own filters. |
641 */ | 654 */ |
642 function connectSubscriptionFilters(subscription) | 655 function connectSubscriptionFilters(subscription, filters) |
643 { | 656 { |
644 if (!filterStorage.knownSubscriptions.has(subscription.url)) | 657 if (!filterStorage.knownSubscriptions.has(subscription.url)) |
645 return; | 658 return; |
646 | 659 |
647 for (let filter of subscription.filters) | 660 for (let filter of filters || subscription.filters()) |
648 filter.addSubscription(subscription); | 661 filter.addSubscription(subscription); |
649 } | 662 } |
650 | 663 |
651 /** | 664 /** |
652 * Disconnects a subscription from its filters without any notifications. | 665 * Disconnects a subscription from its filters without any notifications. |
653 * @param {Subscription} subscription The subscription that should be | 666 * @param {Subscription} subscription The subscription that should be |
654 * disconnected from its filters. | 667 * disconnected from its filters. |
| 668 * @param {?Array.<Filter>} [filters] A list of filters from which the |
| 669 * subscription should be disconnected. If this is not given, the |
| 670 * subscription is disconnected from its own filters. |
655 */ | 671 */ |
656 function disconnectSubscriptionFilters(subscription) | 672 function disconnectSubscriptionFilters(subscription, filters) |
657 { | 673 { |
658 if (!filterStorage.knownSubscriptions.has(subscription.url)) | 674 if (!filterStorage.knownSubscriptions.has(subscription.url)) |
659 return; | 675 return; |
660 | 676 |
661 for (let filter of subscription.filters) | 677 for (let filter of filters || subscription.filters()) |
662 filter.removeSubscription(subscription); | 678 filter.removeSubscription(subscription); |
663 } | 679 } |
LEFT | RIGHT |