From c1112fb45305f43b6bb134cb3448bde635430d0f Mon Sep 17 00:00:00 2001 From: Alex Rantos Date: Sat, 4 Dec 2021 21:15:13 +0200 Subject: [PATCH] fix trailing text issue --- index.js | 1 + test/test.js | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/index.js b/index.js index ed87028..3f0ff55 100644 --- a/index.js +++ b/index.js @@ -582,6 +582,7 @@ function sanitizeHtml(html, options, _recursing) { result = tempResult + escapeHtml(result); tempResult = ''; } + addedText = false; } }, options.parser); parser.write(html); diff --git a/test/test.js b/test/test.js index 9dddc7a..5b343c1 100644 --- a/test/test.js +++ b/test/test.js @@ -228,6 +228,19 @@ describe('sanitizeHtml', function() { }), 'some good text'); }); + it('should preserve trailing text when replacing the tagName and adding new text via transforming function', function () { + assert.equal(sanitizeHtml('

text before
text after

', { + transformTags: { + br: function (_tagName, _attribs) { + return { + tagName: 'span', + text: ' ' + }; + } + } + }), '

text before text after

'); + }); + it('should add new text when not initially set and replace attributes when they are changed by transforming function', function () { assert.equal(sanitizeHtml('', { transformTags: {