OLD | NEW |
(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 /** |
| 35 * Properties of the filter data. |
| 36 * @type {object} |
| 37 */ |
| 38 this.fileProperties = {}; |
| 39 |
| 40 /** |
| 41 * The list of subscriptions in the filter data. |
| 42 * @type {Array.<Subscription>} |
| 43 */ |
| 44 this.subscriptions = []; |
| 45 |
| 46 /** |
| 47 * Known filter texts mapped to their corresponding {@link Filter} objects. |
| 48 * @type {Map.<string, Filter>} |
| 49 */ |
| 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 */ |
| 57 this.knownSubscriptions = new Map(); |
| 58 |
| 59 this._wantObj = true; |
| 60 this._curObj = this.fileProperties; |
| 61 this._curSection = null; |
| 62 } |
| 63 |
| 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) |
| 71 { |
| 72 let origKnownFilters = Filter.knownFilters; |
| 73 Filter.knownFilters = this.knownFilters; |
| 74 |
| 75 let origKnownSubscriptions = Subscription.knownSubscriptions; |
| 76 Subscription.knownSubscriptions = this.knownSubscriptions; |
| 77 |
| 78 try |
| 79 { |
| 80 let match; |
| 81 if (this._wantObj === true && (match = /^(\w+)=(.*)$/.exec(line))) |
| 82 { |
| 83 this._curObj[match[1]] = match[2]; |
| 84 } |
| 85 else if (line === null || (match = /^\s*\[(.+)\]\s*$/.exec(line))) |
| 86 { |
| 87 if (this._curObj) |
| 88 { |
| 89 // Process current object before going to next section |
| 90 switch (this._curSection) |
| 91 { |
| 92 case "filter": |
| 93 if ("text" in this._curObj) |
| 94 Filter.fromObject(this._curObj); |
| 95 break; |
| 96 |
| 97 case "subscription": |
| 98 let subscription = Subscription.fromObject(this._curObj); |
| 99 if (subscription) |
| 100 this.subscriptions.push(subscription); |
| 101 break; |
| 102 |
| 103 case "subscription filters": |
| 104 if (this.subscriptions.length) |
| 105 { |
| 106 let currentSubscription = this.subscriptions[ |
| 107 this.subscriptions.length - 1 |
| 108 ]; |
| 109 for (let text of this._curObj) |
| 110 { |
| 111 let filter = Filter.fromText(text); |
| 112 currentSubscription.filters.push(filter); |
| 113 filter.subscriptions.add(currentSubscription); |
| 114 } |
| 115 } |
| 116 break; |
| 117 } |
| 118 } |
| 119 |
| 120 if (line === null) |
| 121 return; |
| 122 |
| 123 this._curSection = match[1].toLowerCase(); |
| 124 switch (this._curSection) |
| 125 { |
| 126 case "filter": |
| 127 case "subscription": |
| 128 this._wantObj = true; |
| 129 this._curObj = {}; |
| 130 break; |
| 131 case "subscription filters": |
| 132 this._wantObj = false; |
| 133 this._curObj = []; |
| 134 break; |
| 135 default: |
| 136 this._wantObj = null; |
| 137 this._curObj = null; |
| 138 } |
| 139 } |
| 140 else if (this._wantObj === false && line) |
| 141 { |
| 142 this._curObj.push(line.replace(/\\\[/g, "[")); |
| 143 } |
| 144 } |
| 145 finally |
| 146 { |
| 147 Filter.knownFilters = origKnownFilters; |
| 148 Subscription.knownSubscriptions = origKnownSubscriptions; |
| 149 } |
| 150 } |
| 151 } |
| 152 |
| 153 exports.INIParser = INIParser; |
OLD | NEW |