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

Side by Side Diff: lib/iniParser.js

Issue 29867569: Issue 6893, 6741 - Split out INIParser into its own file (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Fix inconsistencies in brace usage Created Aug. 28, 2018, noon
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/filterStorage.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 "use strict";
19
20 /**
21 * @fileOverview INI parsing.
22 */
23
24 const {Filter} = require("./filterClasses");
25 const {Subscription} = require("./subscriptionClasses");
26
27 /**
28 * Parses filter data.
29 */
30 class INIParser
31 {
32 constructor()
33 {
34 this.fileProperties = {};
35 this.subscriptions = [];
36 this.knownFilters = new Map();
37 this.knownSubscriptions = new Map();
38
39 this._wantObj = true;
40 this._curObj = this.fileProperties;
41 this._curSection = null;
42 }
43
44 process(val)
45 {
46 let origKnownFilters = Filter.knownFilters;
47 Filter.knownFilters = this.knownFilters;
48 let origKnownSubscriptions = Subscription.knownSubscriptions;
49 Subscription.knownSubscriptions = this.knownSubscriptions;
50 let match;
51 try
52 {
53 if (this._wantObj === true && (match = /^(\w+)=(.*)$/.exec(val)))
54 {
55 this._curObj[match[1]] = match[2];
56 }
57 else if (val === null || (match = /^\s*\[(.+)\]\s*$/.exec(val)))
58 {
59 if (this._curObj)
60 {
61 // Process current object before going to next section
62 switch (this._curSection)
63 {
64 case "filter":
65 if ("text" in this._curObj)
66 Filter.fromObject(this._curObj);
67 break;
68 case "subscription":
69 let subscription = Subscription.fromObject(this._curObj);
70 if (subscription)
71 this.subscriptions.push(subscription);
72 break;
73 case "subscription filters":
74 if (this.subscriptions.length)
75 {
76 let subscription = this.subscriptions[
77 this.subscriptions.length - 1
78 ];
79 for (let text of this._curObj)
80 {
81 let filter = Filter.fromText(text);
82 subscription.filters.push(filter);
83 filter.subscriptions.add(subscription);
84 }
85 }
86 break;
87 }
88 }
89
90 if (val === null)
91 return;
92
93 this._curSection = match[1].toLowerCase();
94 switch (this._curSection)
95 {
96 case "filter":
97 case "subscription":
98 this._wantObj = true;
99 this._curObj = {};
100 break;
101 case "subscription filters":
102 this._wantObj = false;
103 this._curObj = [];
104 break;
105 default:
106 this._wantObj = null;
107 this._curObj = null;
108 }
109 }
110 else if (this._wantObj === false && val)
111 {
112 this._curObj.push(val.replace(/\\\[/g, "["));
113 }
114 }
115 finally
116 {
117 Filter.knownFilters = origKnownFilters;
118 Subscription.knownSubscriptions = origKnownSubscriptions;
119 }
120 }
121 }
122
123 exports.INIParser = INIParser;
OLDNEW
« no previous file with comments | « lib/filterStorage.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld