Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/filterStorage.js

Issue 29852555: Issue 6559 - Convert subscriptions property of Filter to a set (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Aug. 10, 2018, 2:12 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/filterListener.js ('k') | test/filterListener.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 * @param {Filter} filter 203 * @param {Filter} filter
204 * @param {SpecialSubscription} [subscription] 204 * @param {SpecialSubscription} [subscription]
205 * particular group that the filter should be added to 205 * particular group that the filter should be added to
206 * @param {number} [position] 206 * @param {number} [position]
207 * position within the subscription at which the filter should be added 207 * position within the subscription at which the filter should be added
208 */ 208 */
209 addFilter(filter, subscription, position) 209 addFilter(filter, subscription, position)
210 { 210 {
211 if (!subscription) 211 if (!subscription)
212 { 212 {
213 if (filter.subscriptions.some(s => s instanceof SpecialSubscription && 213 for (let currentSubscription of filter.subscriptions)
214 !s.disabled))
215 { 214 {
216 return; // No need to add 215 if (currentSubscription instanceof SpecialSubscription &&
216 !currentSubscription.disabled)
217 {
218 return; // No need to add
219 }
217 } 220 }
218 subscription = FilterStorage.getGroupForFilter(filter); 221 subscription = FilterStorage.getGroupForFilter(filter);
219 } 222 }
220 if (!subscription) 223 if (!subscription)
221 { 224 {
222 // No group for this filter exists, create one 225 // No group for this filter exists, create one
223 subscription = SpecialSubscription.createForFilter(filter); 226 subscription = SpecialSubscription.createForFilter(filter);
224 this.addSubscription(subscription); 227 this.addSubscription(subscription);
225 return; 228 return;
226 } 229 }
227 230
228 if (typeof position == "undefined") 231 if (typeof position == "undefined")
229 position = subscription.filters.length; 232 position = subscription.filters.length;
230 233
231 if (filter.subscriptions.indexOf(subscription) < 0) 234 filter.subscriptions.add(subscription);
232 filter.subscriptions.push(subscription);
233 subscription.filters.splice(position, 0, filter); 235 subscription.filters.splice(position, 0, filter);
234 FilterNotifier.triggerListeners("filter.added", filter, subscription, 236 FilterNotifier.triggerListeners("filter.added", filter, subscription,
235 position); 237 position);
236 }, 238 },
237 239
238 /** 240 /**
239 * Removes a user-defined filter from the list 241 * Removes a user-defined filter from the list
240 * @param {Filter} filter 242 * @param {Filter} filter
241 * @param {SpecialSubscription} [subscription] a particular filter group that 243 * @param {SpecialSubscription} [subscription] a particular filter group that
242 * the filter should be removed from (if ommited will be removed from all 244 * the filter should be removed from (if ommited will be removed from all
243 * subscriptions) 245 * subscriptions)
244 * @param {number} [position] position inside the filter group at which the 246 * @param {number} [position] position inside the filter group at which the
245 * filter should be removed (if ommited all instances will be removed) 247 * filter should be removed (if ommited all instances will be removed)
246 */ 248 */
247 removeFilter(filter, subscription, position) 249 removeFilter(filter, subscription, position)
248 { 250 {
249 let subscriptions = ( 251 let subscriptions = (
250 subscription ? [subscription] : filter.subscriptions.slice() 252 subscription ? [subscription] : filter.subscriptions
251 ); 253 );
252 for (let i = 0; i < subscriptions.length; i++) 254 for (let currentSubscription of subscriptions)
253 { 255 {
254 let currentSubscription = subscriptions[i];
255 if (currentSubscription instanceof SpecialSubscription) 256 if (currentSubscription instanceof SpecialSubscription)
256 { 257 {
257 let positions = []; 258 let positions = [];
258 if (typeof position == "undefined") 259 if (typeof position == "undefined")
259 { 260 {
260 let index = -1; 261 let index = -1;
261 do 262 do
262 { 263 {
263 index = currentSubscription.filters.indexOf(filter, index + 1); 264 index = currentSubscription.filters.indexOf(filter, index + 1);
264 if (index >= 0) 265 if (index >= 0)
265 positions.push(index); 266 positions.push(index);
266 } while (index >= 0); 267 } while (index >= 0);
267 } 268 }
268 else 269 else
269 positions.push(position); 270 positions.push(position);
270 271
271 for (let j = positions.length - 1; j >= 0; j--) 272 for (let j = positions.length - 1; j >= 0; j--)
272 { 273 {
273 let currentPosition = positions[j]; 274 let currentPosition = positions[j];
274 if (currentSubscription.filters[currentPosition] == filter) 275 if (currentSubscription.filters[currentPosition] == filter)
275 { 276 {
276 currentSubscription.filters.splice(currentPosition, 1); 277 currentSubscription.filters.splice(currentPosition, 1);
277 if (currentSubscription.filters.indexOf(filter) < 0) 278 if (currentSubscription.filters.indexOf(filter) < 0)
278 { 279 filter.subscriptions.delete(currentSubscription);
279 let index = filter.subscriptions.indexOf(currentSubscription);
280 if (index >= 0)
281 filter.subscriptions.splice(index, 1);
282 }
283 FilterNotifier.triggerListeners( 280 FilterNotifier.triggerListeners(
284 "filter.removed", filter, currentSubscription, currentPosition 281 "filter.removed", filter, currentSubscription, currentPosition
285 ); 282 );
286 } 283 }
287 } 284 }
288 } 285 }
289 } 286 }
290 }, 287 },
291 288
292 /** 289 /**
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
655 * Joins subscription's filters to the subscription without any notifications. 652 * Joins subscription's filters to the subscription without any notifications.
656 * @param {Subscription} subscription 653 * @param {Subscription} subscription
657 * filter subscription that should be connected to its filters 654 * filter subscription that should be connected to its filters
658 */ 655 */
659 function addSubscriptionFilters(subscription) 656 function addSubscriptionFilters(subscription)
660 { 657 {
661 if (!FilterStorage.knownSubscriptions.has(subscription.url)) 658 if (!FilterStorage.knownSubscriptions.has(subscription.url))
662 return; 659 return;
663 660
664 for (let filter of subscription.filters) 661 for (let filter of subscription.filters)
665 filter.subscriptions.push(subscription); 662 filter.subscriptions.add(subscription);
666 } 663 }
667 664
668 /** 665 /**
669 * Removes subscription's filters from the subscription without any 666 * Removes subscription's filters from the subscription without any
670 * notifications. 667 * notifications.
671 * @param {Subscription} subscription filter subscription to be removed 668 * @param {Subscription} subscription filter subscription to be removed
672 */ 669 */
673 function removeSubscriptionFilters(subscription) 670 function removeSubscriptionFilters(subscription)
674 { 671 {
675 if (!FilterStorage.knownSubscriptions.has(subscription.url)) 672 if (!FilterStorage.knownSubscriptions.has(subscription.url))
676 return; 673 return;
677 674
678 for (let filter of subscription.filters) 675 for (let filter of subscription.filters)
679 { 676 filter.subscriptions.delete(subscription);
680 let i = filter.subscriptions.indexOf(subscription);
681 if (i >= 0)
682 filter.subscriptions.splice(i, 1);
683 }
684 } 677 }
685 678
686 /** 679 /**
687 * Listener returned by FilterStorage.importData(), parses filter data. 680 * Listener returned by FilterStorage.importData(), parses filter data.
688 * @constructor 681 * @constructor
689 */ 682 */
690 function INIParser() 683 function INIParser()
691 { 684 {
692 this.fileProperties = this.curObj = {}; 685 this.fileProperties = this.curObj = {};
693 this.subscriptions = []; 686 this.subscriptions = [];
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
736 case "subscription filters": 729 case "subscription filters":
737 if (this.subscriptions.length) 730 if (this.subscriptions.length)
738 { 731 {
739 let subscription = this.subscriptions[ 732 let subscription = this.subscriptions[
740 this.subscriptions.length - 1 733 this.subscriptions.length - 1
741 ]; 734 ];
742 for (let text of this.curObj) 735 for (let text of this.curObj)
743 { 736 {
744 let filter = Filter.fromText(text); 737 let filter = Filter.fromText(text);
745 subscription.filters.push(filter); 738 subscription.filters.push(filter);
746 filter.subscriptions.push(subscription); 739 filter.subscriptions.add(subscription);
747 } 740 }
748 } 741 }
749 break; 742 break;
750 } 743 }
751 } 744 }
752 745
753 if (val === null) 746 if (val === null)
754 return; 747 return;
755 748
756 this.curSection = match[1].toLowerCase(); 749 this.curSection = match[1].toLowerCase();
(...skipping 16 matching lines...) Expand all
773 else if (this.wantObj === false && val) 766 else if (this.wantObj === false && val)
774 this.curObj.push(val.replace(/\\\[/g, "[")); 767 this.curObj.push(val.replace(/\\\[/g, "["));
775 } 768 }
776 finally 769 finally
777 { 770 {
778 Filter.knownFilters = origKnownFilters; 771 Filter.knownFilters = origKnownFilters;
779 Subscription.knownSubscriptions = origKnownSubscriptions; 772 Subscription.knownSubscriptions = origKnownSubscriptions;
780 } 773 }
781 } 774 }
782 }; 775 };
OLDNEW
« no previous file with comments | « lib/filterListener.js ('k') | test/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld