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

Powered by Google App Engine
This is Rietveld