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: Created Aug. 28, 2018, 11:52 a.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/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 this.curObj[match[1]] = match[2];
55 else if (val === null || (match = /^\s*\[(.+)\]\s*$/.exec(val)))
56 {
57 if (this.curObj)
58 {
59 // Process current object before going to next section
60 switch (this.curSection)
61 {
62 case "filter":
63 if ("text" in this.curObj)
64 Filter.fromObject(this.curObj);
65 break;
66 case "subscription": {
67 let subscription = Subscription.fromObject(this.curObj);
68 if (subscription)
69 this.subscriptions.push(subscription);
70 break;
71 }
72 case "subscription filters":
73 if (this.subscriptions.length)
74 {
75 let subscription = this.subscriptions[
76 this.subscriptions.length - 1
77 ];
78 for (let text of this.curObj)
79 {
80 let filter = Filter.fromText(text);
81 subscription.filters.push(filter);
82 filter.subscriptions.add(subscription);
83 }
84 }
85 break;
86 }
87 }
88
89 if (val === null)
90 return;
91
92 this.curSection = match[1].toLowerCase();
93 switch (this.curSection)
94 {
95 case "filter":
96 case "subscription":
97 this.wantObj = true;
98 this.curObj = {};
99 break;
100 case "subscription filters":
101 this.wantObj = false;
102 this.curObj = [];
103 break;
104 default:
105 this.wantObj = null;
106 this.curObj = null;
107 }
108 }
109 else if (this.wantObj === false && val)
110 this.curObj.push(val.replace(/\\\[/g, "["));
111 }
112 finally
113 {
114 Filter.knownFilters = origKnownFilters;
115 Subscription.knownSubscriptions = origKnownSubscriptions;
116 }
117 }
118 }
119
120 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