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: Make internal properties private Created Aug. 28, 2018, 11:56 a.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(val) 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;
74
48 let origKnownSubscriptions = Subscription.knownSubscriptions; 75 let origKnownSubscriptions = Subscription.knownSubscriptions;
49 Subscription.knownSubscriptions = this.knownSubscriptions; 76 Subscription.knownSubscriptions = this.knownSubscriptions;
50 let match; 77
51 try 78 try
52 { 79 {
53 if (this._wantObj === true && (match = /^(\w+)=(.*)$/.exec(val))) 80 let match;
81 if (this._wantObj === true && (match = /^(\w+)=(.*)$/.exec(line)))
82 {
54 this._curObj[match[1]] = match[2]; 83 this._curObj[match[1]] = match[2];
55 else if (val === null || (match = /^\s*\[(.+)\]\s*$/.exec(val))) 84 }
85 else if (line === null || (match = /^\s*\[(.+)\]\s*$/.exec(line)))
56 { 86 {
57 if (this._curObj) 87 if (this._curObj)
58 { 88 {
59 // Process current object before going to next section 89 // Process current object before going to next section
60 switch (this._curSection) 90 switch (this._curSection)
61 { 91 {
62 case "filter": 92 case "filter":
63 if ("text" in this._curObj) 93 if ("text" in this._curObj)
64 Filter.fromObject(this._curObj); 94 Filter.fromObject(this._curObj);
65 break; 95 break;
66 case "subscription": { 96
97 case "subscription":
67 let subscription = Subscription.fromObject(this._curObj); 98 let subscription = Subscription.fromObject(this._curObj);
68 if (subscription) 99 if (subscription)
69 this.subscriptions.push(subscription); 100 this.subscriptions.push(subscription);
70 break; 101 break;
71 } 102
72 case "subscription filters": 103 case "subscription filters":
73 if (this.subscriptions.length) 104 if (this.subscriptions.length)
74 { 105 {
75 let subscription = this.subscriptions[ 106 let currentSubscription = this.subscriptions[
76 this.subscriptions.length - 1 107 this.subscriptions.length - 1
77 ]; 108 ];
78 for (let text of this._curObj) 109 for (let text of this._curObj)
79 { 110 {
80 let filter = Filter.fromText(text); 111 let filter = Filter.fromText(text);
81 subscription.filters.push(filter); 112 currentSubscription.filters.push(filter);
82 filter.subscriptions.add(subscription); 113 filter.subscriptions.add(currentSubscription);
83 } 114 }
84 } 115 }
85 break; 116 break;
86 } 117 }
87 } 118 }
88 119
89 if (val === null) 120 if (line === null)
90 return; 121 return;
91 122
92 this._curSection = match[1].toLowerCase(); 123 this._curSection = match[1].toLowerCase();
93 switch (this._curSection) 124 switch (this._curSection)
94 { 125 {
95 case "filter": 126 case "filter":
96 case "subscription": 127 case "subscription":
97 this._wantObj = true; 128 this._wantObj = true;
98 this._curObj = {}; 129 this._curObj = {};
99 break; 130 break;
100 case "subscription filters": 131 case "subscription filters":
101 this._wantObj = false; 132 this._wantObj = false;
102 this._curObj = []; 133 this._curObj = [];
103 break; 134 break;
104 default: 135 default:
105 this._wantObj = null; 136 this._wantObj = null;
106 this._curObj = null; 137 this._curObj = null;
107 } 138 }
108 } 139 }
109 else if (this._wantObj === false && val) 140 else if (this._wantObj === false && line)
110 this._curObj.push(val.replace(/\\\[/g, "[")); 141 {
142 this._curObj.push(line.replace(/\\\[/g, "["));
143 }
111 } 144 }
112 finally 145 finally
113 { 146 {
114 Filter.knownFilters = origKnownFilters; 147 Filter.knownFilters = origKnownFilters;
115 Subscription.knownSubscriptions = origKnownSubscriptions; 148 Subscription.knownSubscriptions = origKnownSubscriptions;
116 } 149 }
117 } 150 }
118 } 151 }
119 152
120 exports.INIParser = INIParser; 153 exports.INIParser = INIParser;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld