| OLD | NEW |
| 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 /** @module filterValidation */ | 18 /** @module filterValidation */ |
| 19 | 19 |
| 20 "use strict"; | 20 import {Filter, InvalidFilter, ElemHideBase, |
| 21 | 21 ElemHideEmulationFilter} from "filterClasses"; |
| 22 const {Filter, InvalidFilter, ElemHideBase, ElemHideEmulationFilter} = | |
| 23 require("filterClasses"); | |
| 24 | 22 |
| 25 /** | 23 /** |
| 26 * An error returned by | 24 * An error returned by |
| 27 * {@link module:filterValidation.parseFilter parseFilter()} or | 25 * {@link module:filterValidation.parseFilter parseFilter()} or |
| 28 * {@link module:filterValidation.parseFilters parseFilters()} | 26 * {@link module:filterValidation.parseFilters parseFilters()} |
| 29 * indicating that a given filter cannot be parsed, | 27 * indicating that a given filter cannot be parsed, |
| 30 * contains an invalid CSS selector or is a filter list header. | 28 * contains an invalid CSS selector or is a filter list header. |
| 31 * | 29 * |
| 32 * @param {string} type See documentation in the constructor below. | 30 * @param {string} type See documentation in the constructor below. |
| 33 * @param {Object} [details] Contains the "reason" and / or "selector" | 31 * @param {Object} [details] Contains the "reason" and / or "selector" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 } | 109 } |
| 112 | 110 |
| 113 /** | 111 /** |
| 114 * @typedef ParsedFilter | 112 * @typedef ParsedFilter |
| 115 * @property {?Filter} [filter] | 113 * @property {?Filter} [filter] |
| 116 * The parsed filter if it is valid. Or null if the given string is empty. | 114 * The parsed filter if it is valid. Or null if the given string is empty. |
| 117 * @property {FilterParsingError} [error] | 115 * @property {FilterParsingError} [error] |
| 118 * See {@link module:filterValidation~FilterParsingError FilterParsingError} | 116 * See {@link module:filterValidation~FilterParsingError FilterParsingError} |
| 119 */ | 117 */ |
| 120 | 118 |
| 121 let parseFilter = | |
| 122 /** | 119 /** |
| 123 * Parses and validates a filter given by the user. | 120 * Parses and validates a filter given by the user. |
| 124 * | 121 * |
| 125 * @param {string} text | 122 * @param {string} text |
| 126 * @return {ParsedFilter} | 123 * @return {ParsedFilter} |
| 127 */ | 124 */ |
| 128 exports.parseFilter = text => | 125 export const parseFilter = text => |
| 129 { | 126 { |
| 130 let filter = null; | 127 let filter = null; |
| 131 text = Filter.normalize(text); | 128 text = Filter.normalize(text); |
| 132 | 129 |
| 133 if (text) | 130 if (text) |
| 134 { | 131 { |
| 135 if (text[0] == "[") | 132 if (text[0] == "[") |
| 136 return {error: new FilterParsingError("unexpected-filter-list-header")}; | 133 return {error: new FilterParsingError("unexpected-filter-list-header")}; |
| 137 | 134 |
| 138 filter = Filter.fromText(text); | 135 filter = Filter.fromText(text); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 161 * @property {FilterParsingError[]} errors | 158 * @property {FilterParsingError[]} errors |
| 162 * See {@link module:filterValidation~FilterParsingError FilterParsingError} | 159 * See {@link module:filterValidation~FilterParsingError FilterParsingError} |
| 163 */ | 160 */ |
| 164 | 161 |
| 165 /** | 162 /** |
| 166 * Parses and validates a newline-separated list of filters given by the user. | 163 * Parses and validates a newline-separated list of filters given by the user. |
| 167 * | 164 * |
| 168 * @param {string} text | 165 * @param {string} text |
| 169 * @return {ParsedFilters} | 166 * @return {ParsedFilters} |
| 170 */ | 167 */ |
| 171 exports.parseFilters = text => | 168 export const parseFilters = text => |
| 172 { | 169 { |
| 173 let lines = text.split("\n"); | 170 let lines = text.split("\n"); |
| 174 let filters = []; | 171 let filters = []; |
| 175 let errors = []; | 172 let errors = []; |
| 176 | 173 |
| 177 for (let i = 0; i < lines.length; i++) | 174 for (let i = 0; i < lines.length; i++) |
| 178 { | 175 { |
| 179 let {filter, error} = parseFilter(lines[i]); | 176 let {filter, error} = parseFilter(lines[i]); |
| 180 | 177 |
| 181 if (filter) | 178 if (filter) |
| 182 filters.push(filter); | 179 filters.push(filter); |
| 183 | 180 |
| 184 if (error) | 181 if (error) |
| 185 { | 182 { |
| 186 error.lineno = i + 1; | 183 error.lineno = i + 1; |
| 187 errors.push(error); | 184 errors.push(error); |
| 188 } | 185 } |
| 189 } | 186 } |
| 190 | 187 |
| 191 return {filters, errors}; | 188 return {filters, errors}; |
| 192 }; | 189 }; |
| OLD | NEW |