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

Unified Diff: lib/filterStorage.js

Issue 29416573: Issue 5153 - Simplify patterns.ini parsing (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created April 18, 2017, 1:33 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/data/patterns.ini » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/filterStorage.js
===================================================================
--- a/lib/filterStorage.js
+++ b/lib/filterStorage.js
@@ -28,17 +28,17 @@ const {Filter, ActiveFilter} = require("
const {Subscription, SpecialSubscription,
ExternalSubscription} = require("subscriptionClasses");
const {FilterNotifier} = require("filterNotifier");
/**
* Version number of the filter storage file format.
* @type {number}
*/
-let formatVersion = 4;
+let formatVersion = 5;
/**
* This class reads user's filters from disk, manages them in memory
* and writes them back.
* @class
*/
let FilterStorage = exports.FilterStorage =
{
@@ -116,50 +116,44 @@ let FilterStorage = exports.FilterStorag
}
}
return generalSubscription;
},
/**
* Adds a filter subscription to the list
* @param {Subscription} subscription filter subscription to be added
- * @param {boolean} silent if true, no listeners will be triggered
- * (to be used when filter list is reloaded)
*/
- addSubscription(subscription, silent)
+ addSubscription(subscription)
{
if (subscription.url in FilterStorage.knownSubscriptions)
return;
FilterStorage.subscriptions.push(subscription);
FilterStorage.knownSubscriptions[subscription.url] = subscription;
addSubscriptionFilters(subscription);
- if (!silent)
- FilterNotifier.triggerListeners("subscription.added", subscription);
+ FilterNotifier.triggerListeners("subscription.added", subscription);
},
/**
* Removes a filter subscription from the list
* @param {Subscription} subscription filter subscription to be removed
- * @param {boolean} silent if true, no listeners will be triggered
- * (to be used when filter list is reloaded)
*/
- removeSubscription(subscription, silent)
+ removeSubscription(subscription)
{
for (let i = 0; i < FilterStorage.subscriptions.length; i++)
{
if (FilterStorage.subscriptions[i].url == subscription.url)
{
removeSubscriptionFilters(subscription);
FilterStorage.subscriptions.splice(i--, 1);
delete FilterStorage.knownSubscriptions[subscription.url];
- if (!silent)
- FilterNotifier.triggerListeners("subscription.removed", subscription);
+ FilterNotifier.triggerListeners("subscription.removed", subscription);
return;
}
}
},
/**
* Moves a subscription in the list to a new position.
* @param {Subscription} subscription filter subscription to be moved
@@ -206,21 +200,18 @@ let FilterStorage = exports.FilterStorag
/**
* Adds a user-defined filter to the list
* @param {Filter} filter
* @param {SpecialSubscription} [subscription]
* particular group that the filter should be added to
* @param {number} [position]
* position within the subscription at which the filter should be added
- * @param {boolean} silent
- * if true, no listeners will be triggered (to be used when filter list is
- * reloaded)
*/
- addFilter(filter, subscription, position, silent)
+ addFilter(filter, subscription, position)
{
if (!subscription)
{
if (filter.subscriptions.some(s => s instanceof SpecialSubscription &&
!s.disabled))
{
return; // No need to add
}
@@ -235,21 +226,18 @@ let FilterStorage = exports.FilterStorag
}
if (typeof position == "undefined")
position = subscription.filters.length;
if (filter.subscriptions.indexOf(subscription) < 0)
filter.subscriptions.push(subscription);
subscription.filters.splice(position, 0, filter);
- if (!silent)
- {
- FilterNotifier.triggerListeners("filter.added", filter, subscription,
- position);
- }
+ FilterNotifier.triggerListeners("filter.added", filter, subscription,
+ position);
},
/**
* Removes a user-defined filter from the list
* @param {Filter} filter
* @param {SpecialSubscription} [subscription] a particular filter group that
* the filter should be removed from (if ommited will be removed from all
* subscriptions)
@@ -379,45 +367,26 @@ let FilterStorage = exports.FilterStorag
importData(silent)
{
let parser = new INIParser();
return line =>
{
parser.process(line);
if (line === null)
{
- // Old special groups might have been converted, remove them if
- // they are empty
- let specialMap = new Set(["~il~", "~wl~", "~fl~", "~eh~"]);
let knownSubscriptions = Object.create(null);
- for (let i = 0; i < parser.subscriptions.length; i++)
- {
- let subscription = parser.subscriptions[i];
- if (subscription instanceof SpecialSubscription &&
- subscription.filters.length == 0 &&
- specialMap.has(subscription.url))
- {
- parser.subscriptions.splice(i--, 1);
- }
- else
- knownSubscriptions[subscription.url] = subscription;
- }
+ for (let subscription of parser.subscriptions)
+ knownSubscriptions[subscription.url] = subscription;
this.fileProperties = parser.fileProperties;
this.subscriptions = parser.subscriptions;
this.knownSubscriptions = knownSubscriptions;
Filter.knownFilters = parser.knownFilters;
Subscription.knownSubscriptions = parser.knownSubscriptions;
- if (parser.userFilters)
- {
- for (let filter of parser.userFilters)
- this.addFilter(Filter.fromText(filter), null, undefined, true);
- }
-
if (!silent)
FilterNotifier.triggerListeners("load");
}
};
},
/**
* Loads all subscriptions from the disk.
@@ -508,47 +477,47 @@ let FilterStorage = exports.FilterStorag
);
yield "# Adblock Plus preferences";
yield "version=" + formatVersion;
let saved = new Set();
let buf = [];
+ // Save subscriptions
+ for (let subscription of subscriptions)
+ {
+ yield "";
+
+ subscription.serialize(buf);
+ if (subscription.filters.length)
+ {
+ buf.push("", "[Subscription filters]");
+ subscription.serializeFilters(buf);
+ }
+ for (let line of buf)
+ yield line;
+ buf.splice(0);
+ }
+
// Save filter data
for (let subscription of subscriptions)
{
for (let filter of subscription.filters)
{
if (!saved.has(filter.text))
{
filter.serialize(buf);
saved.add(filter.text);
for (let line of buf)
yield line;
buf.splice(0);
}
}
}
-
- // Save subscriptions
- for (let subscription of subscriptions)
- {
- yield "";
-
- subscription.serialize(buf);
- if (subscription.filters.length)
- {
- buf.push("", "[Subscription filters]");
- subscription.serializeFilters(buf);
- }
- for (let line of buf)
- yield line;
- buf.splice(0);
- }
},
/**
* Will be set to true if saveToDisk() is running (reentrance protection).
* @type {boolean}
*/
_saving: false,
@@ -734,17 +703,16 @@ INIParser.prototype =
linesProcessed: 0,
subscriptions: null,
knownFilters: null,
knownSubscriptions: null,
wantObj: true,
fileProperties: null,
curObj: null,
curSection: null,
- userFilters: null,
process(val)
{
let origKnownFilters = Filter.knownFilters;
Filter.knownFilters = this.knownFilters;
let origKnownSubscriptions = Subscription.knownSubscriptions;
Subscription.knownSubscriptions = this.knownSubscriptions;
let match;
@@ -755,62 +723,54 @@ INIParser.prototype =
else if (val === null || (match = /^\s*\[(.+)\]\s*$/.exec(val)))
{
if (this.curObj)
{
// Process current object before going to next section
switch (this.curSection)
{
case "filter":
- case "pattern":
if ("text" in this.curObj)
Filter.fromObject(this.curObj);
break;
case "subscription": {
let subscription = Subscription.fromObject(this.curObj);
if (subscription)
this.subscriptions.push(subscription);
break;
}
case "subscription filters":
- case "subscription patterns":
if (this.subscriptions.length)
{
let subscription = this.subscriptions[
this.subscriptions.length - 1
];
for (let text of this.curObj)
{
let filter = Filter.fromText(text);
subscription.filters.push(filter);
filter.subscriptions.push(subscription);
}
}
break;
- case "user patterns":
- this.userFilters = this.curObj;
- break;
}
}
if (val === null)
return;
this.curSection = match[1].toLowerCase();
switch (this.curSection)
{
case "filter":
- case "pattern":
case "subscription":
this.wantObj = true;
this.curObj = {};
break;
case "subscription filters":
- case "subscription patterns":
- case "user patterns":
this.wantObj = false;
this.curObj = [];
break;
default:
this.wantObj = undefined;
this.curObj = null;
}
}
« no previous file with comments | « no previous file | test/data/patterns.ini » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld