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

Delta Between Two Patch Sets: lib/iniParser.js

Issue 29867569: Issue 6893, 6741 - Split out INIParser into its own file (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Insert blank lines for readability Created Aug. 28, 2018, 12:14 p.m.
Right Patch Set: Rename value argument to line Created Aug. 28, 2018, 12:49 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/filterStorage.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 13 matching lines...) Expand all
24 const {Filter} = require("./filterClasses"); 24 const {Filter} = require("./filterClasses");
25 const {Subscription} = require("./subscriptionClasses"); 25 const {Subscription} = require("./subscriptionClasses");
26 26
27 /** 27 /**
28 * Parses filter data. 28 * Parses filter data.
29 */ 29 */
30 class INIParser 30 class INIParser
31 { 31 {
32 constructor() 32 constructor()
33 { 33 {
34 /**
35 * Properties of the filter data.
36 * @type {object}
37 */
34 this.fileProperties = {}; 38 this.fileProperties = {};
39
40 /**
41 * The list of subscriptions in the filter data.
42 * @type {Array.<Subscription>}
43 */
35 this.subscriptions = []; 44 this.subscriptions = [];
45
46 /**
47 * Known filter texts mapped to their corresponding {@link Filter} objects.
48 * @type {Map.<string, Filter>}
49 */
36 this.knownFilters = new Map(); 50 this.knownFilters = new Map();
51
52 /**
53 * Known subscription URLs mapped to their corresponding
54 * {@link Subscription} objects.
55 * @type {Map.<string, Subscription>}
56 */
37 this.knownSubscriptions = new Map(); 57 this.knownSubscriptions = new Map();
38 58
39 this._wantObj = true; 59 this._wantObj = true;
40 this._curObj = this.fileProperties; 60 this._curObj = this.fileProperties;
41 this._curSection = null; 61 this._curSection = null;
42 } 62 }
43 63
44 process(value) 64 /**
65 * Processes a line of filter data.
66 *
67 * @param {string?} line The line of filter data to process. This may be
68 * <code>null</code>, which indicates the end of the filter data.
69 */
70 process(line)
45 { 71 {
46 let origKnownFilters = Filter.knownFilters; 72 let origKnownFilters = Filter.knownFilters;
47 Filter.knownFilters = this.knownFilters; 73 Filter.knownFilters = this.knownFilters;
48 74
49 let origKnownSubscriptions = Subscription.knownSubscriptions; 75 let origKnownSubscriptions = Subscription.knownSubscriptions;
50 Subscription.knownSubscriptions = this.knownSubscriptions; 76 Subscription.knownSubscriptions = this.knownSubscriptions;
51 77
52 try 78 try
53 { 79 {
54 let match; 80 let match;
55 if (this._wantObj === true && (match = /^(\w+)=(.*)$/.exec(value))) 81 if (this._wantObj === true && (match = /^(\w+)=(.*)$/.exec(line)))
56 { 82 {
57 this._curObj[match[1]] = match[2]; 83 this._curObj[match[1]] = match[2];
58 } 84 }
59 else if (value === null || (match = /^\s*\[(.+)\]\s*$/.exec(value))) 85 else if (line === null || (match = /^\s*\[(.+)\]\s*$/.exec(line)))
60 { 86 {
61 if (this._curObj) 87 if (this._curObj)
62 { 88 {
63 // Process current object before going to next section 89 // Process current object before going to next section
64 switch (this._curSection) 90 switch (this._curSection)
65 { 91 {
66 case "filter": 92 case "filter":
67 if ("text" in this._curObj) 93 if ("text" in this._curObj)
68 Filter.fromObject(this._curObj); 94 Filter.fromObject(this._curObj);
69 break; 95 break;
(...skipping 14 matching lines...) Expand all
84 { 110 {
85 let filter = Filter.fromText(text); 111 let filter = Filter.fromText(text);
86 currentSubscription.filters.push(filter); 112 currentSubscription.filters.push(filter);
87 filter.subscriptions.add(currentSubscription); 113 filter.subscriptions.add(currentSubscription);
88 } 114 }
89 } 115 }
90 break; 116 break;
91 } 117 }
92 } 118 }
93 119
94 if (value === null) 120 if (line === null)
95 return; 121 return;
96 122
97 this._curSection = match[1].toLowerCase(); 123 this._curSection = match[1].toLowerCase();
98 switch (this._curSection) 124 switch (this._curSection)
99 { 125 {
100 case "filter": 126 case "filter":
101 case "subscription": 127 case "subscription":
102 this._wantObj = true; 128 this._wantObj = true;
103 this._curObj = {}; 129 this._curObj = {};
104 break; 130 break;
105 case "subscription filters": 131 case "subscription filters":
106 this._wantObj = false; 132 this._wantObj = false;
107 this._curObj = []; 133 this._curObj = [];
108 break; 134 break;
109 default: 135 default:
110 this._wantObj = null; 136 this._wantObj = null;
111 this._curObj = null; 137 this._curObj = null;
112 } 138 }
113 } 139 }
114 else if (this._wantObj === false && value) 140 else if (this._wantObj === false && line)
115 { 141 {
116 this._curObj.push(value.replace(/\\\[/g, "[")); 142 this._curObj.push(line.replace(/\\\[/g, "["));
117 } 143 }
118 } 144 }
119 finally 145 finally
120 { 146 {
121 Filter.knownFilters = origKnownFilters; 147 Filter.knownFilters = origKnownFilters;
122 Subscription.knownSubscriptions = origKnownSubscriptions; 148 Subscription.knownSubscriptions = origKnownSubscriptions;
123 } 149 }
124 } 150 }
125 } 151 }
126 152
127 exports.INIParser = INIParser; 153 exports.INIParser = INIParser;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld