|
| 1 | +/** |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * A loose JavaScript version of Magento\Framework\Escaper |
| 8 | + * |
| 9 | + * Due to differences in how XML/HTML is processed in PHP vs JS there are a couple of minor differences in behavior |
| 10 | + * from the PHP counterpart. |
| 11 | + * |
| 12 | + * The first difference is that the default invocation of escapeHtml without allowedTags will double-escape existing |
| 13 | + * entities as the intention of such an invocation is that the input isn't supposed to contain any HTML. |
| 14 | + * |
| 15 | + * The second difference is that escapeHtml will not escape quotes. Since the input is actually being processed by the |
| 16 | + * DOM there is no chance of quotes being mixed with HTML syntax. And, since escapeHtml is not |
| 17 | + * intended to be used with raw injection into a HTML attribute, this is acceptable. |
| 18 | + * |
| 19 | + * @api |
| 20 | + */ |
| 21 | +define([], function () { |
| 22 | + 'use strict'; |
| 23 | + |
| 24 | + return { |
| 25 | + neverAllowedElements: ['script', 'img', 'embed', 'iframe', 'video', 'source', 'object', 'audio'], |
| 26 | + generallyAllowedAttributes: ['id', 'class', 'href', 'title', 'style'], |
| 27 | + forbiddenAttributesByElement: { |
| 28 | + a: ['style'] |
| 29 | + }, |
| 30 | + |
| 31 | + /** |
| 32 | + * Escape a string for safe injection into HTML |
| 33 | + * |
| 34 | + * @param {String} data |
| 35 | + * @param {Array|null} allowedTags |
| 36 | + * @returns {String} |
| 37 | + */ |
| 38 | + escapeHtml: function (data, allowedTags) { |
| 39 | + var domParser = new DOMParser(), |
| 40 | + fragment = domParser.parseFromString('<div></div>', 'text/html'); |
| 41 | + |
| 42 | + fragment = fragment.body.childNodes[0]; |
| 43 | + allowedTags = typeof allowedTags === 'object' && allowedTags.length ? allowedTags : null; |
| 44 | + |
| 45 | + if (allowedTags) { |
| 46 | + fragment.innerHTML = data || ''; |
| 47 | + allowedTags = this._filterProhibitedTags(allowedTags); |
| 48 | + |
| 49 | + this._removeComments(fragment); |
| 50 | + this._removeNotAllowedElements(fragment, allowedTags); |
| 51 | + this._removeNotAllowedAttributes(fragment); |
| 52 | + |
| 53 | + return fragment.innerHTML; |
| 54 | + } |
| 55 | + |
| 56 | + fragment.textContent = data || ''; |
| 57 | + |
| 58 | + return fragment.innerHTML; |
| 59 | + }, |
| 60 | + |
| 61 | + /** |
| 62 | + * Remove the always forbidden tags from a list of provided tags |
| 63 | + * |
| 64 | + * @param {Array} tags |
| 65 | + * @returns {Array} |
| 66 | + * @private |
| 67 | + */ |
| 68 | + _filterProhibitedTags: function (tags) { |
| 69 | + return tags.filter(function (n) { |
| 70 | + return this.neverAllowedElements.indexOf(n) === -1; |
| 71 | + }.bind(this)); |
| 72 | + }, |
| 73 | + |
| 74 | + /** |
| 75 | + * Remove comment nodes from the given node |
| 76 | + * |
| 77 | + * @param {Node} node |
| 78 | + * @private |
| 79 | + */ |
| 80 | + _removeComments: function (node) { |
| 81 | + var treeWalker = node.ownerDocument.createTreeWalker( |
| 82 | + node, |
| 83 | + NodeFilter.SHOW_COMMENT, |
| 84 | + function () { |
| 85 | + return NodeFilter.FILTER_ACCEPT; |
| 86 | + }, |
| 87 | + false |
| 88 | + ), |
| 89 | + nodesToRemove = []; |
| 90 | + |
| 91 | + while (treeWalker.nextNode()) { |
| 92 | + nodesToRemove.push(treeWalker.currentNode); |
| 93 | + } |
| 94 | + |
| 95 | + nodesToRemove.forEach(function (nodeToRemove) { |
| 96 | + nodeToRemove.parentNode.removeChild(nodeToRemove); |
| 97 | + }); |
| 98 | + }, |
| 99 | + |
| 100 | + /** |
| 101 | + * Strip the given node of all disallowed tags while permitting any nested text nodes |
| 102 | + * |
| 103 | + * @param {Node} node |
| 104 | + * @param {Array|null} allowedTags |
| 105 | + * @private |
| 106 | + */ |
| 107 | + _removeNotAllowedElements: function (node, allowedTags) { |
| 108 | + var treeWalker = node.ownerDocument.createTreeWalker( |
| 109 | + node, |
| 110 | + NodeFilter.SHOW_ELEMENT, |
| 111 | + function (currentNode) { |
| 112 | + return allowedTags.indexOf(currentNode.nodeName.toLowerCase()) === -1 ? |
| 113 | + NodeFilter.FILTER_ACCEPT |
| 114 | + // SKIP instead of REJECT because REJECT also rejects child nodes |
| 115 | + : NodeFilter.FILTER_SKIP; |
| 116 | + }, |
| 117 | + false |
| 118 | + ), |
| 119 | + nodesToRemove = []; |
| 120 | + |
| 121 | + while (treeWalker.nextNode()) { |
| 122 | + if (allowedTags.indexOf(treeWalker.currentNode.nodeName.toLowerCase()) === -1) { |
| 123 | + nodesToRemove.push(treeWalker.currentNode); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + nodesToRemove.forEach(function (nodeToRemove) { |
| 128 | + nodeToRemove.parentNode.replaceChild( |
| 129 | + node.ownerDocument.createTextNode(nodeToRemove.textContent), |
| 130 | + nodeToRemove |
| 131 | + ); |
| 132 | + }); |
| 133 | + }, |
| 134 | + |
| 135 | + /** |
| 136 | + * Remove any invalid attributes from the given node |
| 137 | + * |
| 138 | + * @param {Node} node |
| 139 | + * @private |
| 140 | + */ |
| 141 | + _removeNotAllowedAttributes: function (node) { |
| 142 | + var treeWalker = node.ownerDocument.createTreeWalker( |
| 143 | + node, |
| 144 | + NodeFilter.SHOW_ELEMENT, |
| 145 | + function () { |
| 146 | + return NodeFilter.FILTER_ACCEPT; |
| 147 | + }, |
| 148 | + false |
| 149 | + ), |
| 150 | + i, |
| 151 | + attribute, |
| 152 | + nodeName, |
| 153 | + attributesToRemove = []; |
| 154 | + |
| 155 | + while (treeWalker.nextNode()) { |
| 156 | + for (i = 0; i < treeWalker.currentNode.attributes.length; i++) { |
| 157 | + attribute = treeWalker.currentNode.attributes[i]; |
| 158 | + nodeName = treeWalker.currentNode.nodeName.toLowerCase(); |
| 159 | + |
| 160 | + if (this.generallyAllowedAttributes.indexOf(attribute.name) === -1 || // eslint-disable-line max-depth,max-len |
| 161 | + this.forbiddenAttributesByElement[nodeName] && |
| 162 | + this.forbiddenAttributesByElement[nodeName].indexOf(attribute.name) !== -1 |
| 163 | + ) { |
| 164 | + attributesToRemove.push(attribute); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + attributesToRemove.forEach(function (attributeToRemove) { |
| 170 | + attributeToRemove.ownerElement.removeAttribute(attributeToRemove.name); |
| 171 | + }); |
| 172 | + } |
| 173 | + }; |
| 174 | +}); |
0 commit comments