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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 }, | 135 }, |
136 | 136 |
137 /** | 137 /** |
138 * Removes a filter subscription from the list | 138 * Removes a filter subscription from the list |
139 * @param {Subscription} subscription filter subscription to be removed | 139 * @param {Subscription} subscription filter subscription to be removed |
140 */ | 140 */ |
141 removeSubscription(subscription) | 141 removeSubscription(subscription) |
142 { | 142 { |
143 for (let i = 0; i < FilterStorage.subscriptions.length; i++) | 143 for (let i = 0; i < FilterStorage.subscriptions.length; i++) |
144 { | 144 { |
145 if (FilterStorage.subscriptions[i].url == subscription.url) | 145 if (FilterStorage.subscriptions[i].url === subscription.url) |
146 { | 146 { |
147 removeSubscriptionFilters(subscription); | 147 removeSubscriptionFilters(subscription); |
148 | 148 |
149 FilterStorage.subscriptions.splice(i--, 1); | 149 FilterStorage.subscriptions.splice(i--, 1); |
150 FilterStorage.knownSubscriptions.delete(subscription.url); | 150 FilterStorage.knownSubscriptions.delete(subscription.url); |
151 FilterNotifier.triggerListeners("subscription.removed", subscription); | 151 FilterNotifier.triggerListeners("subscription.removed", subscription); |
152 return; | 152 return; |
153 } | 153 } |
154 } | 154 } |
155 }, | 155 }, |
(...skipping 12 matching lines...) Expand all Loading... |
168 | 168 |
169 let newPos = -1; | 169 let newPos = -1; |
170 if (insertBefore) | 170 if (insertBefore) |
171 newPos = FilterStorage.subscriptions.indexOf(insertBefore); | 171 newPos = FilterStorage.subscriptions.indexOf(insertBefore); |
172 | 172 |
173 if (newPos < 0) | 173 if (newPos < 0) |
174 newPos = FilterStorage.subscriptions.length; | 174 newPos = FilterStorage.subscriptions.length; |
175 | 175 |
176 if (currentPos < newPos) | 176 if (currentPos < newPos) |
177 newPos--; | 177 newPos--; |
178 if (currentPos == newPos) | 178 if (currentPos === newPos) |
179 return; | 179 return; |
180 | 180 |
181 FilterStorage.subscriptions.splice(currentPos, 1); | 181 FilterStorage.subscriptions.splice(currentPos, 1); |
182 FilterStorage.subscriptions.splice(newPos, 0, subscription); | 182 FilterStorage.subscriptions.splice(newPos, 0, subscription); |
183 FilterNotifier.triggerListeners("subscription.moved", subscription); | 183 FilterNotifier.triggerListeners("subscription.moved", subscription); |
184 }, | 184 }, |
185 | 185 |
186 /** | 186 /** |
187 * Replaces the list of filters in a subscription by a new list | 187 * Replaces the list of filters in a subscription by a new list |
188 * @param {Subscription} subscription filter subscription to be updated | 188 * @param {Subscription} subscription filter subscription to be updated |
(...skipping 29 matching lines...) Expand all Loading... |
218 subscription = FilterStorage.getGroupForFilter(filter); | 218 subscription = FilterStorage.getGroupForFilter(filter); |
219 } | 219 } |
220 if (!subscription) | 220 if (!subscription) |
221 { | 221 { |
222 // No group for this filter exists, create one | 222 // No group for this filter exists, create one |
223 subscription = SpecialSubscription.createForFilter(filter); | 223 subscription = SpecialSubscription.createForFilter(filter); |
224 this.addSubscription(subscription); | 224 this.addSubscription(subscription); |
225 return; | 225 return; |
226 } | 226 } |
227 | 227 |
228 if (typeof position == "undefined") | 228 if (typeof position === "undefined") |
229 position = subscription.filters.length; | 229 position = subscription.filters.length; |
230 | 230 |
231 if (filter.subscriptions.indexOf(subscription) < 0) | 231 if (filter.subscriptions.indexOf(subscription) < 0) |
232 filter.subscriptions.push(subscription); | 232 filter.subscriptions.push(subscription); |
233 subscription.filters.splice(position, 0, filter); | 233 subscription.filters.splice(position, 0, filter); |
234 FilterNotifier.triggerListeners("filter.added", filter, subscription, | 234 FilterNotifier.triggerListeners("filter.added", filter, subscription, |
235 position); | 235 position); |
236 }, | 236 }, |
237 | 237 |
238 /** | 238 /** |
239 * Removes a user-defined filter from the list | 239 * Removes a user-defined filter from the list |
240 * @param {Filter} filter | 240 * @param {Filter} filter |
241 * @param {SpecialSubscription} [subscription] a particular filter group that | 241 * @param {SpecialSubscription} [subscription] a particular filter group that |
242 * the filter should be removed from (if ommited will be removed from all | 242 * the filter should be removed from (if ommited will be removed from all |
243 * subscriptions) | 243 * subscriptions) |
244 * @param {number} [position] position inside the filter group at which the | 244 * @param {number} [position] position inside the filter group at which the |
245 * filter should be removed (if ommited all instances will be removed) | 245 * filter should be removed (if ommited all instances will be removed) |
246 */ | 246 */ |
247 removeFilter(filter, subscription, position) | 247 removeFilter(filter, subscription, position) |
248 { | 248 { |
249 let subscriptions = ( | 249 let subscriptions = ( |
250 subscription ? [subscription] : filter.subscriptions.slice() | 250 subscription ? [subscription] : filter.subscriptions.slice() |
251 ); | 251 ); |
252 for (let i = 0; i < subscriptions.length; i++) | 252 for (let i = 0; i < subscriptions.length; i++) |
253 { | 253 { |
254 let currentSubscription = subscriptions[i]; | 254 let currentSubscription = subscriptions[i]; |
255 if (currentSubscription instanceof SpecialSubscription) | 255 if (currentSubscription instanceof SpecialSubscription) |
256 { | 256 { |
257 let positions = []; | 257 let positions = []; |
258 if (typeof position == "undefined") | 258 if (typeof position === "undefined") |
259 { | 259 { |
260 let index = -1; | 260 let index = -1; |
261 do | 261 do |
262 { | 262 { |
263 index = currentSubscription.filters.indexOf(filter, index + 1); | 263 index = currentSubscription.filters.indexOf(filter, index + 1); |
264 if (index >= 0) | 264 if (index >= 0) |
265 positions.push(index); | 265 positions.push(index); |
266 } while (index >= 0); | 266 } while (index >= 0); |
267 } | 267 } |
268 else | 268 else |
269 positions.push(position); | 269 positions.push(position); |
270 | 270 |
271 for (let j = positions.length - 1; j >= 0; j--) | 271 for (let j = positions.length - 1; j >= 0; j--) |
272 { | 272 { |
273 let currentPosition = positions[j]; | 273 let currentPosition = positions[j]; |
274 if (currentSubscription.filters[currentPosition] == filter) | 274 if (currentSubscription.filters[currentPosition] === filter) |
275 { | 275 { |
276 currentSubscription.filters.splice(currentPosition, 1); | 276 currentSubscription.filters.splice(currentPosition, 1); |
277 if (currentSubscription.filters.indexOf(filter) < 0) | 277 if (currentSubscription.filters.indexOf(filter) < 0) |
278 { | 278 { |
279 let index = filter.subscriptions.indexOf(currentSubscription); | 279 let index = filter.subscriptions.indexOf(currentSubscription); |
280 if (index >= 0) | 280 if (index >= 0) |
281 filter.subscriptions.splice(index, 1); | 281 filter.subscriptions.splice(index, 1); |
282 } | 282 } |
283 FilterNotifier.triggerListeners( | 283 FilterNotifier.triggerListeners( |
284 "filter.removed", filter, currentSubscription, currentPosition | 284 "filter.removed", filter, currentSubscription, currentPosition |
285 ); | 285 ); |
286 } | 286 } |
287 } | 287 } |
288 } | 288 } |
289 } | 289 } |
290 }, | 290 }, |
291 | 291 |
292 /** | 292 /** |
293 * Moves a user-defined filter to a new position | 293 * Moves a user-defined filter to a new position |
294 * @param {Filter} filter | 294 * @param {Filter} filter |
295 * @param {SpecialSubscription} subscription filter group where the filter is | 295 * @param {SpecialSubscription} subscription filter group where the filter is |
296 * located | 296 * located |
297 * @param {number} oldPosition current position of the filter | 297 * @param {number} oldPosition current position of the filter |
298 * @param {number} newPosition new position of the filter | 298 * @param {number} newPosition new position of the filter |
299 */ | 299 */ |
300 moveFilter(filter, subscription, oldPosition, newPosition) | 300 moveFilter(filter, subscription, oldPosition, newPosition) |
301 { | 301 { |
302 if (!(subscription instanceof SpecialSubscription) || | 302 if (!(subscription instanceof SpecialSubscription) || |
303 subscription.filters[oldPosition] != filter) | 303 subscription.filters[oldPosition] !== filter) |
304 { | 304 { |
305 return; | 305 return; |
306 } | 306 } |
307 | 307 |
308 newPosition = Math.min(Math.max(newPosition, 0), | 308 newPosition = Math.min(Math.max(newPosition, 0), |
309 subscription.filters.length - 1); | 309 subscription.filters.length - 1); |
310 if (oldPosition == newPosition) | 310 if (oldPosition === newPosition) |
311 return; | 311 return; |
312 | 312 |
313 subscription.filters.splice(oldPosition, 1); | 313 subscription.filters.splice(oldPosition, 1); |
314 subscription.filters.splice(newPosition, 0, filter); | 314 subscription.filters.splice(newPosition, 0, filter); |
315 FilterNotifier.triggerListeners("filter.moved", filter, subscription, | 315 FilterNotifier.triggerListeners("filter.moved", filter, subscription, |
316 oldPosition, newPosition); | 316 oldPosition, newPosition); |
317 }, | 317 }, |
318 | 318 |
319 /** | 319 /** |
320 * Increases the hit count for a filter by one | 320 * Increases the hit count for a filter by one |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 /** | 387 /** |
388 * Loads all subscriptions from the disk. | 388 * Loads all subscriptions from the disk. |
389 * @return {Promise} promise resolved or rejected when loading is complete | 389 * @return {Promise} promise resolved or rejected when loading is complete |
390 */ | 390 */ |
391 loadFromDisk() | 391 loadFromDisk() |
392 { | 392 { |
393 let tryBackup = backupIndex => | 393 let tryBackup = backupIndex => |
394 { | 394 { |
395 return this.restoreBackup(backupIndex, true).then(() => | 395 return this.restoreBackup(backupIndex, true).then(() => |
396 { | 396 { |
397 if (this.subscriptions.length == 0) | 397 if (this.subscriptions.length === 0) |
398 return tryBackup(backupIndex + 1); | 398 return tryBackup(backupIndex + 1); |
399 }).catch(error => | 399 }).catch(error => |
400 { | 400 { |
401 // Give up | 401 // Give up |
402 }); | 402 }); |
403 }; | 403 }; |
404 | 404 |
405 return IO.statFile(this.sourceFile).then(statData => | 405 return IO.statFile(this.sourceFile).then(statData => |
406 { | 406 { |
407 if (!statData.exists) | 407 if (!statData.exists) |
408 { | 408 { |
409 this.firstRun = true; | 409 this.firstRun = true; |
410 return; | 410 return; |
411 } | 411 } |
412 | 412 |
413 let parser = this.importData(true); | 413 let parser = this.importData(true); |
414 return IO.readFromFile(this.sourceFile, parser).then(() => | 414 return IO.readFromFile(this.sourceFile, parser).then(() => |
415 { | 415 { |
416 parser(null); | 416 parser(null); |
417 if (this.subscriptions.length == 0) | 417 if (this.subscriptions.length === 0) |
418 { | 418 { |
419 // No filter subscriptions in the file, this isn't right. | 419 // No filter subscriptions in the file, this isn't right. |
420 throw new Error("No data in the file"); | 420 throw new Error("No data in the file"); |
421 } | 421 } |
422 }); | 422 }); |
423 }).catch(error => | 423 }).catch(error => |
424 { | 424 { |
425 Cu.reportError(error); | 425 Cu.reportError(error); |
426 return tryBackup(1); | 426 return tryBackup(1); |
427 }).then(() => | 427 }).then(() => |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
773 else if (this.wantObj === false && val) | 773 else if (this.wantObj === false && val) |
774 this.curObj.push(val.replace(/\\\[/g, "[")); | 774 this.curObj.push(val.replace(/\\\[/g, "[")); |
775 } | 775 } |
776 finally | 776 finally |
777 { | 777 { |
778 Filter.knownFilters = origKnownFilters; | 778 Filter.knownFilters = origKnownFilters; |
779 Subscription.knownSubscriptions = origKnownSubscriptions; | 779 Subscription.knownSubscriptions = origKnownSubscriptions; |
780 } | 780 } |
781 } | 781 } |
782 }; | 782 }; |
OLD | NEW |