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 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 * @param {Filter} filter | 208 * @param {Filter} filter |
209 * @param {SpecialSubscription} [subscription] | 209 * @param {SpecialSubscription} [subscription] |
210 * particular group that the filter should be added to | 210 * particular group that the filter should be added to |
211 * @param {number} [position] | 211 * @param {number} [position] |
212 * position within the subscription at which the filter should be added | 212 * position within the subscription at which the filter should be added |
213 */ | 213 */ |
214 addFilter(filter, subscription, position) | 214 addFilter(filter, subscription, position) |
215 { | 215 { |
216 if (!subscription) | 216 if (!subscription) |
217 { | 217 { |
218 if (filter.subscriptions.some(s => s instanceof SpecialSubscription && | 218 for (let currentSubscription of filter.subscriptions) |
219 !s.disabled)) | 219 { |
220 { | 220 if (currentSubscription instanceof SpecialSubscription && |
221 return; // No need to add | 221 !currentSubscription.disabled) |
| 222 { |
| 223 return; // No need to add |
| 224 } |
222 } | 225 } |
223 subscription = FilterStorage.getGroupForFilter(filter); | 226 subscription = FilterStorage.getGroupForFilter(filter); |
224 } | 227 } |
225 if (!subscription) | 228 if (!subscription) |
226 { | 229 { |
227 // No group for this filter exists, create one | 230 // No group for this filter exists, create one |
228 subscription = SpecialSubscription.createForFilter(filter); | 231 subscription = SpecialSubscription.createForFilter(filter); |
229 this.addSubscription(subscription); | 232 this.addSubscription(subscription); |
230 return; | 233 return; |
231 } | 234 } |
232 | 235 |
233 if (typeof position == "undefined") | 236 if (typeof position == "undefined") |
234 position = subscription.filters.length; | 237 position = subscription.filters.length; |
235 | 238 |
236 if (filter.subscriptions.indexOf(subscription) < 0) | 239 filter.subscriptions.add(subscription); |
237 filter.subscriptions.push(subscription); | |
238 subscription.filters.splice(position, 0, filter); | 240 subscription.filters.splice(position, 0, filter); |
239 FilterNotifier.triggerListeners("filter.added", filter, subscription, | 241 FilterNotifier.triggerListeners("filter.added", filter, subscription, |
240 position); | 242 position); |
241 }, | 243 }, |
242 | 244 |
243 /** | 245 /** |
244 * Removes a user-defined filter from the list | 246 * Removes a user-defined filter from the list |
245 * @param {Filter} filter | 247 * @param {Filter} filter |
246 * @param {SpecialSubscription} [subscription] a particular filter group that | 248 * @param {SpecialSubscription} [subscription] a particular filter group that |
247 * the filter should be removed from (if ommited will be removed from all | 249 * the filter should be removed from (if ommited will be removed from all |
248 * subscriptions) | 250 * subscriptions) |
249 * @param {number} [position] position inside the filter group at which the | 251 * @param {number} [position] position inside the filter group at which the |
250 * filter should be removed (if ommited all instances will be removed) | 252 * filter should be removed (if ommited all instances will be removed) |
251 */ | 253 */ |
252 removeFilter(filter, subscription, position) | 254 removeFilter(filter, subscription, position) |
253 { | 255 { |
254 let subscriptions = ( | 256 let subscriptions = ( |
255 subscription ? [subscription] : filter.subscriptions.slice() | 257 subscription ? [subscription] : filter.subscriptions |
256 ); | 258 ); |
257 for (let i = 0; i < subscriptions.length; i++) | 259 for (let currentSubscription of subscriptions) |
258 { | 260 { |
259 let currentSubscription = subscriptions[i]; | |
260 if (currentSubscription instanceof SpecialSubscription) | 261 if (currentSubscription instanceof SpecialSubscription) |
261 { | 262 { |
262 let positions = []; | 263 let positions = []; |
263 if (typeof position == "undefined") | 264 if (typeof position == "undefined") |
264 { | 265 { |
265 let index = -1; | 266 let index = -1; |
266 do | 267 do |
267 { | 268 { |
268 index = currentSubscription.filters.indexOf(filter, index + 1); | 269 index = currentSubscription.filters.indexOf(filter, index + 1); |
269 if (index >= 0) | 270 if (index >= 0) |
270 positions.push(index); | 271 positions.push(index); |
271 } while (index >= 0); | 272 } while (index >= 0); |
272 } | 273 } |
273 else | 274 else |
274 positions.push(position); | 275 positions.push(position); |
275 | 276 |
276 for (let j = positions.length - 1; j >= 0; j--) | 277 for (let j = positions.length - 1; j >= 0; j--) |
277 { | 278 { |
278 let currentPosition = positions[j]; | 279 let currentPosition = positions[j]; |
279 if (currentSubscription.filters[currentPosition] == filter) | 280 if (currentSubscription.filters[currentPosition] == filter) |
280 { | 281 { |
281 currentSubscription.filters.splice(currentPosition, 1); | 282 currentSubscription.filters.splice(currentPosition, 1); |
282 if (currentSubscription.filters.indexOf(filter) < 0) | 283 if (currentSubscription.filters.indexOf(filter) < 0) |
283 { | 284 filter.subscriptions.delete(currentSubscription); |
284 let index = filter.subscriptions.indexOf(currentSubscription); | |
285 if (index >= 0) | |
286 filter.subscriptions.splice(index, 1); | |
287 } | |
288 FilterNotifier.triggerListeners( | 285 FilterNotifier.triggerListeners( |
289 "filter.removed", filter, currentSubscription, currentPosition | 286 "filter.removed", filter, currentSubscription, currentPosition |
290 ); | 287 ); |
291 } | 288 } |
292 } | 289 } |
293 } | 290 } |
294 } | 291 } |
295 }, | 292 }, |
296 | 293 |
297 /** | 294 /** |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 * Joins subscription's filters to the subscription without any notifications. | 657 * Joins subscription's filters to the subscription without any notifications. |
661 * @param {Subscription} subscription | 658 * @param {Subscription} subscription |
662 * filter subscription that should be connected to its filters | 659 * filter subscription that should be connected to its filters |
663 */ | 660 */ |
664 function addSubscriptionFilters(subscription) | 661 function addSubscriptionFilters(subscription) |
665 { | 662 { |
666 if (!FilterStorage.knownSubscriptions.has(subscription.url)) | 663 if (!FilterStorage.knownSubscriptions.has(subscription.url)) |
667 return; | 664 return; |
668 | 665 |
669 for (let filter of subscription.filters) | 666 for (let filter of subscription.filters) |
670 filter.subscriptions.push(subscription); | 667 filter.subscriptions.add(subscription); |
671 } | 668 } |
672 | 669 |
673 /** | 670 /** |
674 * Removes subscription's filters from the subscription without any | 671 * Removes subscription's filters from the subscription without any |
675 * notifications. | 672 * notifications. |
676 * @param {Subscription} subscription filter subscription to be removed | 673 * @param {Subscription} subscription filter subscription to be removed |
677 */ | 674 */ |
678 function removeSubscriptionFilters(subscription) | 675 function removeSubscriptionFilters(subscription) |
679 { | 676 { |
680 if (!FilterStorage.knownSubscriptions.has(subscription.url)) | 677 if (!FilterStorage.knownSubscriptions.has(subscription.url)) |
681 return; | 678 return; |
682 | 679 |
683 for (let filter of subscription.filters) | 680 for (let filter of subscription.filters) |
684 { | 681 filter.subscriptions.delete(subscription); |
685 let index = 0; | |
686 | |
687 // The same filter can occur more than once in a subscription. We could | |
688 // avoid making duplicate subscription entries in a Filter object in | |
689 // INIParser, but we don't do this in order to avoid slowing down the | |
690 // loading of the initial subscriptions from disk. | |
691 while ((index = filter.subscriptions.indexOf(subscription), index) != -1) | |
692 filter.subscriptions.splice(index, 1); | |
693 } | |
694 } | 682 } |
695 | 683 |
696 /** | 684 /** |
697 * Listener returned by FilterStorage.importData(), parses filter data. | 685 * Listener returned by FilterStorage.importData(), parses filter data. |
698 * @constructor | 686 * @constructor |
699 */ | 687 */ |
700 function INIParser() | 688 function INIParser() |
701 { | 689 { |
702 this.fileProperties = this.curObj = {}; | 690 this.fileProperties = this.curObj = {}; |
703 this.subscriptions = []; | 691 this.subscriptions = []; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
746 case "subscription filters": | 734 case "subscription filters": |
747 if (this.subscriptions.length) | 735 if (this.subscriptions.length) |
748 { | 736 { |
749 let subscription = this.subscriptions[ | 737 let subscription = this.subscriptions[ |
750 this.subscriptions.length - 1 | 738 this.subscriptions.length - 1 |
751 ]; | 739 ]; |
752 for (let text of this.curObj) | 740 for (let text of this.curObj) |
753 { | 741 { |
754 let filter = Filter.fromText(text); | 742 let filter = Filter.fromText(text); |
755 subscription.filters.push(filter); | 743 subscription.filters.push(filter); |
756 filter.subscriptions.push(subscription); | 744 filter.subscriptions.add(subscription); |
757 } | 745 } |
758 } | 746 } |
759 break; | 747 break; |
760 } | 748 } |
761 } | 749 } |
762 | 750 |
763 if (val === null) | 751 if (val === null) |
764 return; | 752 return; |
765 | 753 |
766 this.curSection = match[1].toLowerCase(); | 754 this.curSection = match[1].toLowerCase(); |
(...skipping 16 matching lines...) Expand all Loading... |
783 else if (this.wantObj === false && val) | 771 else if (this.wantObj === false && val) |
784 this.curObj.push(val.replace(/\\\[/g, "[")); | 772 this.curObj.push(val.replace(/\\\[/g, "[")); |
785 } | 773 } |
786 finally | 774 finally |
787 { | 775 { |
788 Filter.knownFilters = origKnownFilters; | 776 Filter.knownFilters = origKnownFilters; |
789 Subscription.knownSubscriptions = origKnownSubscriptions; | 777 Subscription.knownSubscriptions = origKnownSubscriptions; |
790 } | 778 } |
791 } | 779 } |
792 }; | 780 }; |
LEFT | RIGHT |