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

Side by Side Diff: lib/subscriptionClasses.js

Issue 29934588: Issue 7094 - Encapsulate management of subscription filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Remove hasFilter and related code Created Nov. 18, 2018, 10:21 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/iniParser.js ('k') | lib/synchronizer.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 18 matching lines...) Expand all
29 /** 29 /**
30 * Abstract base class for filter subscriptions 30 * Abstract base class for filter subscriptions
31 * 31 *
32 * @param {string} url download location of the subscription 32 * @param {string} url download location of the subscription
33 * @param {string} [title] title of the filter subscription 33 * @param {string} [title] title of the filter subscription
34 * @constructor 34 * @constructor
35 */ 35 */
36 function Subscription(url, title) 36 function Subscription(url, title)
37 { 37 {
38 this.url = url; 38 this.url = url;
39 this.filters = []; 39
40 this._filterText = [];
41 this._filters = [];
42
40 if (title) 43 if (title)
41 this._title = title; 44 this._title = title;
45
42 Subscription.knownSubscriptions.set(url, this); 46 Subscription.knownSubscriptions.set(url, this);
43 } 47 }
44 exports.Subscription = Subscription; 48 exports.Subscription = Subscription;
45 49
46 Subscription.prototype = 50 Subscription.prototype =
47 { 51 {
48 /** 52 /**
49 * Download location of the subscription 53 * Download location of the subscription
50 * @type {string} 54 * @type {string}
51 */ 55 */
52 url: null, 56 url: null,
53 57
54 /** 58 /**
55 * Type of the subscription 59 * Type of the subscription
56 * @type {?string} 60 * @type {?string}
57 */ 61 */
58 type: null, 62 type: null,
59 63
60 /** 64 /**
61 * Filters contained in the filter subscription 65 * Filter text contained in the filter subscription.
62 * @type {Filter[]} 66 * @type {Array.<string>}
67 * @private
63 */ 68 */
64 filters: null, 69 _filterText: null,
hub 2018/11/19 17:47:49 shouldn't this be named `_filtersText` since it is
Manish Jethani 2018/11/20 00:19:39 I thought about calling it _filtersText but that s
hub 2018/11/20 19:30:19 my bad. let's leave it at that.
70
71 /**
72 * {@link Filter} objects corresponding to the subscription's filter text.
73 * @type {Array.<Filter>}
74 * @private
75 */
76 _filters: null,
65 77
66 _title: null, 78 _title: null,
67 _fixedTitle: false, 79 _fixedTitle: false,
68 _disabled: false, 80 _disabled: false,
69 81
70 /** 82 /**
71 * Title of the filter subscription 83 * Title of the filter subscription
72 * @type {string} 84 * @type {string}
73 */ 85 */
74 get title() 86 get title()
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 if (value != this._disabled) 130 if (value != this._disabled)
119 { 131 {
120 let oldValue = this._disabled; 132 let oldValue = this._disabled;
121 this._disabled = value; 133 this._disabled = value;
122 filterNotifier.emit("subscription.disabled", this, value, oldValue); 134 filterNotifier.emit("subscription.disabled", this, value, oldValue);
123 } 135 }
124 return this._disabled; 136 return this._disabled;
125 }, 137 },
126 138
127 /** 139 /**
140 * The number of filters in the subscription.
141 * @type {number}
142 */
143 get filterCount()
144 {
145 return this._filters.length;
146 },
147
148 /**
149 * Yields the text for each filter in the subscription.
150 * @yields {string}
151 */
152 *filterText()
153 {
154 yield* this._filterText;
155 },
156
157 /**
158 * Yields the {@link Filter} object for each filter in the subscription.
159 * @yields {Filter}
160 */
161 *filters()
162 {
163 yield* this._filters;
164 },
165
166 /**
167 * Returns the {@link Filter} object at the given 0-based index.
168 * @param {number} index
169 * @returns {?Filter}
170 */
171 filterAt(index)
172 {
173 return this._filters[index] || null;
174 },
175
176 /**
177 * Returns the 0-based index of the given filter.
178 * @param {Filter} filter
179 * @param {number} [fromIndex] The index from which to start the search.
180 * @return {number}
181 */
182 searchFilter(filter, fromIndex = 0)
183 {
184 return this._filterText.indexOf(filter.text, fromIndex);
185 },
186
187 /**
188 * Removes all filters from the subscription.
189 */
190 clearFilters()
191 {
192 this._filterText = [];
193 this._filters = [];
194 },
195
196 /**
197 * Adds a filter to the subscription.
198 * @param {Filter} filter
199 */
200 addFilter(filter)
201 {
202 this._filterText.push(filter.text);
203 this._filters.push(filter);
204 },
205
206 /**
207 * Inserts a filter into the subscription.
208 * @param {Filter} filter
209 * @param {number} index The index at which to insert the filter.
210 */
211 insertFilterAt(filter, index)
212 {
213 this._filterText.splice(index, 0, filter.text);
214 this._filters.splice(index, 0, filter);
215 },
216
217 /**
218 * Deletes a filter from the subscription.
219 * @param {number} index The index at which to delete the filter.
220 */
221 deleteFilterAt(index)
222 {
223 // Ignore index if out of bounds on the negative side, for consistency.
224 if (index < 0)
225 return;
226
227 this._filterText.splice(index, 1);
228 this._filters.splice(index, 1);
229 },
230
231 /**
128 * Serializes the subscription for writing out on disk. 232 * Serializes the subscription for writing out on disk.
129 * @yields {string} 233 * @yields {string}
130 */ 234 */
131 *serialize() 235 *serialize()
132 { 236 {
133 let {url, type, _title, _fixedTitle, _disabled} = this; 237 let {url, type, _title, _fixedTitle, _disabled} = this;
134 238
135 yield "[Subscription]"; 239 yield "[Subscription]";
136 yield "url=" + url; 240 yield "url=" + url;
137 241
138 if (type) 242 if (type)
139 yield "type=" + type; 243 yield "type=" + type;
140 if (_title) 244 if (_title)
141 yield "title=" + _title; 245 yield "title=" + _title;
142 if (_fixedTitle) 246 if (_fixedTitle)
143 yield "fixedTitle=true"; 247 yield "fixedTitle=true";
144 if (_disabled) 248 if (_disabled)
145 yield "disabled=true"; 249 yield "disabled=true";
146 }, 250 },
147 251
148 *serializeFilters() 252 *serializeFilters()
149 { 253 {
150 let {filters} = this; 254 let {_filterText} = this;
151 255
152 yield "[Subscription filters]"; 256 yield "[Subscription filters]";
153 257
154 for (let filter of filters) 258 for (let text of _filterText)
155 yield filter.text.replace(/\[/g, "\\["); 259 yield text.replace(/\[/g, "\\[");
156 }, 260 },
157 261
158 toString() 262 toString()
159 { 263 {
160 return [...this.serialize()].join("\n"); 264 return [...this.serialize()].join("\n");
161 } 265 }
162 }; 266 };
163 267
164 /** 268 /**
165 * Cache for known filter subscriptions, maps URL to subscription objects. 269 * Cache for known filter subscriptions, maps URL to subscription objects.
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 430
327 /** 431 /**
328 * Creates a new user-defined filter group and adds the given filter to it. 432 * Creates a new user-defined filter group and adds the given filter to it.
329 * This group will act as the default group for this filter type. 433 * This group will act as the default group for this filter type.
330 * @param {Filter} filter 434 * @param {Filter} filter
331 * @return {SpecialSubscription} 435 * @return {SpecialSubscription}
332 */ 436 */
333 SpecialSubscription.createForFilter = function(filter) 437 SpecialSubscription.createForFilter = function(filter)
334 { 438 {
335 let subscription = SpecialSubscription.create(); 439 let subscription = SpecialSubscription.create();
336 subscription.filters.push(filter); 440 subscription.addFilter(filter);
337 for (let [type, class_] of SpecialSubscription.defaultsMap) 441 for (let [type, class_] of SpecialSubscription.defaultsMap)
338 { 442 {
339 if (filter instanceof class_) 443 if (filter instanceof class_)
340 subscription.defaults = [type]; 444 subscription.defaults = [type];
341 } 445 }
342 if (!subscription.defaults) 446 if (!subscription.defaults)
343 subscription.defaults = ["blocking"]; 447 subscription.defaults = ["blocking"];
344 return subscription; 448 return subscription;
345 }; 449 };
346 450
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 if (errors) 686 if (errors)
583 yield "errors=" + errors; 687 yield "errors=" + errors;
584 if (version) 688 if (version)
585 yield "version=" + version; 689 yield "version=" + version;
586 if (requiredVersion) 690 if (requiredVersion)
587 yield "requiredVersion=" + requiredVersion; 691 yield "requiredVersion=" + requiredVersion;
588 if (downloadCount) 692 if (downloadCount)
589 yield "downloadCount=" + downloadCount; 693 yield "downloadCount=" + downloadCount;
590 } 694 }
591 }); 695 });
OLDNEW
« no previous file with comments | « lib/iniParser.js ('k') | lib/synchronizer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld