Index: lib/iniParser.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/lib/iniParser.js |
@@ -0,0 +1,120 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-present eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+"use strict"; |
+ |
+/** |
+ * @fileOverview INI parsing. |
+ */ |
+ |
+const {Filter} = require("./filterClasses"); |
+const {Subscription} = require("./subscriptionClasses"); |
+ |
+/** |
+ * Parses filter data. |
+ */ |
+class INIParser |
+{ |
+ constructor() |
+ { |
+ this.fileProperties = {}; |
+ this.subscriptions = []; |
+ this.knownFilters = new Map(); |
+ this.knownSubscriptions = new Map(); |
+ |
+ this.wantObj = true; |
+ this.curObj = this.fileProperties; |
+ this.curSection = null; |
+ } |
+ |
+ process(val) |
+ { |
+ let origKnownFilters = Filter.knownFilters; |
+ Filter.knownFilters = this.knownFilters; |
+ let origKnownSubscriptions = Subscription.knownSubscriptions; |
+ Subscription.knownSubscriptions = this.knownSubscriptions; |
+ let match; |
+ try |
+ { |
+ if (this.wantObj === true && (match = /^(\w+)=(.*)$/.exec(val))) |
+ this.curObj[match[1]] = match[2]; |
+ 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": |
+ 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": |
+ 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.add(subscription); |
+ } |
+ } |
+ break; |
+ } |
+ } |
+ |
+ if (val === null) |
+ return; |
+ |
+ this.curSection = match[1].toLowerCase(); |
+ switch (this.curSection) |
+ { |
+ case "filter": |
+ case "subscription": |
+ this.wantObj = true; |
+ this.curObj = {}; |
+ break; |
+ case "subscription filters": |
+ this.wantObj = false; |
+ this.curObj = []; |
+ break; |
+ default: |
+ this.wantObj = null; |
+ this.curObj = null; |
+ } |
+ } |
+ else if (this.wantObj === false && val) |
+ this.curObj.push(val.replace(/\\\[/g, "[")); |
+ } |
+ finally |
+ { |
+ Filter.knownFilters = origKnownFilters; |
+ Subscription.knownSubscriptions = origKnownSubscriptions; |
+ } |
+ } |
+} |
+ |
+exports.INIParser = INIParser; |