| 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 "use strict"; |
| 21 | 21 |
| 22 const {Filter, InvalidFilter, ElemHideBase, ElemHideEmulationFilter} = | 22 const {Filter, InvalidFilter, ElemHideBase, ElemHideException, |
| 23 require("filterClasses"); | 23 ElemHideEmulationFilter} = require("filterClasses"); |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * An error returned by | 26 * An error returned by |
| 27 * {@link module:filterValidation.parseFilter parseFilter()} or | 27 * {@link module:filterValidation.parseFilter parseFilter()} or |
| 28 * {@link module:filterValidation.parseFilters parseFilters()} | 28 * {@link module:filterValidation.parseFilters parseFilters()} |
| 29 * indicating that a given filter cannot be parsed, | 29 * indicating that a given filter cannot be parsed, |
| 30 * contains an invalid CSS selector or is a filter list header. | 30 * contains an invalid CSS selector or is a filter list header. |
| 31 * | 31 * |
| 32 * @param {string} type See documentation in the constructor below. | 32 * @param {string} type See documentation in the constructor below. |
| 33 * @param {Object} [details] Contains the "reason" and / or "selector" | 33 * @param {Object} [details] Contains the "reason" and / or "selector" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 document.querySelector(selector); | 103 document.querySelector(selector); |
| 104 sheet.insertRule(selector + "{}", 0); | 104 sheet.insertRule(selector + "{}", 0); |
| 105 } | 105 } |
| 106 catch (e) | 106 catch (e) |
| 107 { | 107 { |
| 108 return false; | 108 return false; |
| 109 } | 109 } |
| 110 return true; | 110 return true; |
| 111 } | 111 } |
| 112 | 112 |
| 113 function isValidFilterSelector(filter) |
| 114 { |
| 115 // Only ElemHideBase has selectors. |
| 116 if (!(filter instanceof ElemHideBase)) |
| 117 return true; |
| 118 |
| 119 // We don't check the syntax of ElemHideEmulationFilter yet. |
| 120 if (filter instanceof ElemHideEmulationFilter) |
| 121 return true; |
| 122 |
| 123 // If it is an ElemHideException, and it has an extended CSS |
| 124 // selector we don't validate and assume it is valid. |
| 125 if (filter instanceof ElemHideException && |
| 126 filter.selector.includes(":-abp-")) |
| 127 { |
| 128 return true; |
| 129 } |
| 130 |
| 131 return isValidCSSSelector(filter.selector); |
| 132 } |
| 133 |
| 113 /** | 134 /** |
| 114 * @typedef ParsedFilter | 135 * @typedef ParsedFilter |
| 115 * @property {?Filter} [filter] | 136 * @property {?Filter} [filter] |
| 116 * The parsed filter if it is valid. Or null if the given string is empty. | 137 * The parsed filter if it is valid. Or null if the given string is empty. |
| 117 * @property {FilterParsingError} [error] | 138 * @property {FilterParsingError} [error] |
| 118 * See {@link module:filterValidation~FilterParsingError FilterParsingError} | 139 * See {@link module:filterValidation~FilterParsingError FilterParsingError} |
| 119 */ | 140 */ |
| 120 | 141 |
| 121 let parseFilter = | 142 let parseFilter = |
| 122 /** | 143 /** |
| (...skipping 12 matching lines...) Expand all Loading... |
| 135 if (text[0] == "[") | 156 if (text[0] == "[") |
| 136 return {error: new FilterParsingError("unexpected-filter-list-header")}; | 157 return {error: new FilterParsingError("unexpected-filter-list-header")}; |
| 137 | 158 |
| 138 filter = Filter.fromText(text); | 159 filter = Filter.fromText(text); |
| 139 | 160 |
| 140 if (filter instanceof InvalidFilter) | 161 if (filter instanceof InvalidFilter) |
| 141 { | 162 { |
| 142 return {error: new FilterParsingError("invalid-filter", | 163 return {error: new FilterParsingError("invalid-filter", |
| 143 {reason: filter.reason})}; | 164 {reason: filter.reason})}; |
| 144 } | 165 } |
| 145 if (filter instanceof ElemHideBase && | 166 if (!isValidFilterSelector(filter)) |
| 146 !(filter instanceof ElemHideEmulationFilter) && | |
| 147 !isValidCSSSelector(filter.selector)) | |
| 148 { | 167 { |
| 149 return {error: new FilterParsingError("invalid-css-selector", | 168 return {error: new FilterParsingError("invalid-css-selector", |
| 150 {selector: filter.selector})}; | 169 {selector: filter.selector})}; |
| 151 } | 170 } |
| 152 } | 171 } |
| 153 | 172 |
| 154 return {filter}; | 173 return {filter}; |
| 155 }; | 174 }; |
| 156 | 175 |
| 157 /** | 176 /** |
| (...skipping 25 matching lines...) Expand all Loading... |
| 183 | 202 |
| 184 if (error) | 203 if (error) |
| 185 { | 204 { |
| 186 error.lineno = i + 1; | 205 error.lineno = i + 1; |
| 187 errors.push(error); | 206 errors.push(error); |
| 188 } | 207 } |
| 189 } | 208 } |
| 190 | 209 |
| 191 return {filters, errors}; | 210 return {filters, errors}; |
| 192 }; | 211 }; |
| OLD | NEW |