| 1 | /* |
| 2 | Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/ |
| 3 | |
| 4 | This program is free software; |
| 5 | you can redistribute it and/or modify it under the terms of the |
| 6 | GNU General Public License as published by the Free Software Foundation; |
| 7 | either version 3 of the License, or any later version. |
| 8 | |
| 9 | This program 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 this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | Changes: |
| 21 | Doc reviewed ... |
| 22 | Tests ... |
| 23 | */ |
| 24 | |
| 25 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 26 | /** |
| 27 | this class contains the validity map for the HTMLSanitizer |
| 28 | */ |
| 29 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 30 | |
| 31 | class HTMLSanitizerData { |
| 32 | |
| 33 | /** |
| 34 | The validity map |
| 35 | @type {Map} |
| 36 | */ |
| 37 | |
| 38 | #validityMap = new Map ( ); |
| 39 | |
| 40 | /** |
| 41 | The constructor |
| 42 | */ |
| 43 | |
| 44 | constructor ( ) { |
| 45 | |
| 46 | Object.freeze ( this ); |
| 47 | |
| 48 | /* |
| 49 | WARNING : |
| 50 | |
| 51 | never put script as valid tag !!! |
| 52 | |
| 53 | never put event handler starting with on... as valid attribute !!! |
| 54 | |
| 55 | */ |
| 56 | |
| 57 | this.#validityMap.set ( 'a', [ 'href', 'target' ] ); |
| 58 | this.#validityMap.set ( 'div', [ ] ); |
| 59 | this.#validityMap.set ( 'del', [ ] ); |
| 60 | this.#validityMap.set ( 'em', [ ] ); |
| 61 | this.#validityMap.set ( 'figcaption', [ ] ); |
| 62 | this.#validityMap.set ( 'figure', [ ] ); |
| 63 | this.#validityMap.set ( 'h1', [ ] ); |
| 64 | this.#validityMap.set ( 'h2', [ ] ); |
| 65 | this.#validityMap.set ( 'h3', [ ] ); |
| 66 | this.#validityMap.set ( 'h4', [ ] ); |
| 67 | this.#validityMap.set ( 'h5', [ ] ); |
| 68 | this.#validityMap.set ( 'h6', [ ] ); |
| 69 | this.#validityMap.set ( 'hr', [ ] ); |
| 70 | this.#validityMap.set ( 'img', [ 'src', 'alt', 'width', 'height' ] ); |
| 71 | this.#validityMap.set ( 'ins', [ ] ); |
| 72 | this.#validityMap.set ( 'li', [ ] ); |
| 73 | this.#validityMap.set ( 'mark', [ ] ); |
| 74 | this.#validityMap.set ( 'ol', [ ] ); |
| 75 | this.#validityMap.set ( 'p', [ ] ); |
| 76 | this.#validityMap.set ( 's', [ ] ); |
| 77 | this.#validityMap.set ( 'small', [ ] ); |
| 78 | this.#validityMap.set ( 'strong', [ ] ); |
| 79 | this.#validityMap.set ( 'span', [ ] ); |
| 80 | this.#validityMap.set ( 'sub', [ ] ); |
| 81 | this.#validityMap.set ( 'sup', [ ] ); |
| 82 | this.#validityMap.set ( 'ul', [ ] ); |
| 83 | |
| 84 | this.#validityMap.set ( 'svg', [ 'xmlns', 'viewBox', 'class' ] ); |
| 85 | this.#validityMap.set ( 'text', [ 'x', 'y', 'text-anchor' ] ); |
| 86 | this.#validityMap.set ( 'polyline', [ 'points', 'class', 'transform' ] ); |
| 87 | |
| 88 | this.#validityMap.set ( '\u0023text', [] ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | get the valid attributes for a node name |
| 93 | @param {String} nodeName the name of the node for witch the valid attrbutes are asked. |
| 94 | Warning: the node name must be a valid node name verified with the getValidNodeName. |
| 95 | @return {Array.<String>} the valid attributes names |
| 96 | */ |
| 97 | |
| 98 | getValidAttributesNames ( nodeName ) { |
| 99 | return this.#validityMap.get ( nodeName ).concat ( [ 'id', 'class', 'dir', 'title' ] ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | verify that a node name is a valid node name |
| 104 | @param {String} nodeName The node name |
| 105 | @return {String} the node name or an empty string if the given node name is invalid |
| 106 | */ |
| 107 | |
| 108 | getValidNodeName ( nodeName ) { |
| 109 | const validNodeName = nodeName.toLowerCase ( ); |
| 110 | return this.#validityMap.get ( validNodeName ) ? validNodeName : ''; |
| 111 | } |
| 112 | |
| 113 | } |
| 114 | |
| 115 | export default HTMLSanitizerData; |
| 116 | |
| 117 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
| 118 |