diff --git a/src/test/unit/html_sanitizer_test.js b/src/test/unit/html_sanitizer_test.js index 16994cce8..4c7adef16 100644 --- a/src/test/unit/html_sanitizer_test.js +++ b/src/test/unit/html_sanitizer_test.js @@ -27,6 +27,20 @@ testGroup("HTMLSanitizer", () => { assert.equal(document, expectedHTML) }) }) + + test("strips HTML comments", () => { + const html = "
" + const expectedHTML = "
" + const document = HTMLSanitizer.sanitize(html).body.innerHTML + assert.equal(document, expectedHTML) + }) + + test("strips HTML comments in attributes", () => { + const html = "
\">
" + const expectedHTML = "
" + const document = HTMLSanitizer.sanitize(html).body.innerHTML + assert.equal(document, expectedHTML) + }) }) const withDOMPurifyConfig = (attrConfig = {}, fn) => { diff --git a/src/trix/models/html_sanitizer.js b/src/trix/models/html_sanitizer.js index 98360449c..bcab31530 100644 --- a/src/trix/models/html_sanitizer.js +++ b/src/trix/models/html_sanitizer.js @@ -69,9 +69,6 @@ export default class HTMLSanitizer extends BasicObject { this.sanitizeElement(node) } break - case Node.COMMENT_NODE: - nodesToRemove.push(node) - break } } @@ -124,8 +121,8 @@ export default class HTMLSanitizer extends BasicObject { } const createBodyElementForHTML = function(html = "") { - // Remove everything after - html = html.replace(/<\/html[^>]*>[^]*$/i, "") + // Remove everything after and HTML comments + html = html.replace(/<\/html[^>]*>[^]*$/i, "").replace(/()/g, "") const doc = document.implementation.createHTMLDocument("") doc.documentElement.innerHTML = html