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 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(value) |
| 45 { |
| 46 let origKnownFilters = Filter.knownFilters; |
| 47 Filter.knownFilters = this.knownFilters; |
| 48 |
| 49 let origKnownSubscriptions = Subscription.knownSubscriptions; |
| 50 Subscription.knownSubscriptions = this.knownSubscriptions; |
| 51 |
| 52 try |
| 53 { |
| 54 let match; |
| 55 if (this._wantObj === true && (match = /^(\w+)=(.*)$/.exec(value))) |
| 56 { |
| 57 this._curObj[match[1]] = match[2]; |
| 58 } |
| 59 else if (value === null || (match = /^\s*\[(.+)\]\s*$/.exec(value))) |
| 60 { |
| 61 if (this._curObj) |
| 62 { |
| 63 // Process current object before going to next section |
| 64 switch (this._curSection) |
| 65 { |
| 66 case "filter": |
| 67 if ("text" in this._curObj) |
| 68 Filter.fromObject(this._curObj); |
| 69 break; |
| 70 |
| 71 case "subscription": |
| 72 let subscription = Subscription.fromObject(this._curObj); |
| 73 if (subscription) |
| 74 this.subscriptions.push(subscription); |
| 75 break; |
| 76 |
| 77 case "subscription filters": |
| 78 if (this.subscriptions.length) |
| 79 { |
| 80 let currentSubscription = this.subscriptions[ |
| 81 this.subscriptions.length - 1 |
| 82 ]; |
| 83 for (let text of this._curObj) |
| 84 { |
| 85 let filter = Filter.fromText(text); |
| 86 currentSubscription.filters.push(filter); |
| 87 filter.subscriptions.add(currentSubscription); |
| 88 } |
| 89 } |
| 90 break; |
| 91 } |
| 92 } |
| 93 |
| 94 if (value === null) |
| 95 return; |
| 96 |
| 97 this._curSection = match[1].toLowerCase(); |
| 98 switch (this._curSection) |
| 99 { |
| 100 case "filter": |
| 101 case "subscription": |
| 102 this._wantObj = true; |
| 103 this._curObj = {}; |
| 104 break; |
| 105 case "subscription filters": |
| 106 this._wantObj = false; |
| 107 this._curObj = []; |
| 108 break; |
| 109 default: |
| 110 this._wantObj = null; |
| 111 this._curObj = null; |
| 112 } |
| 113 } |
| 114 else if (this._wantObj === false && value) |
| 115 { |
| 116 this._curObj.push(value.replace(/\\\[/g, "[")); |
| 117 } |
| 118 } |
| 119 finally |
| 120 { |
| 121 Filter.knownFilters = origKnownFilters; |
| 122 Subscription.knownSubscriptions = origKnownSubscriptions; |
| 123 } |
| 124 } |
| 125 } |
| 126 |
| 127 exports.INIParser = INIParser; |
OLD | NEW |