From c1112fb45305f43b6bb134cb3448bde635430d0f Mon Sep 17 00:00:00 2001 From: Alex Rantos Date: Sat, 4 Dec 2021 21:15:13 +0200 Subject: [PATCH 1/3] 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: { From 836c5ab39fc67df8b98363581a719f67b1e406ef Mon Sep 17 00:00:00 2001 From: Alex Rantos Date: Tue, 7 Dec 2021 17:28:44 +0200 Subject: [PATCH 2/3] add CHANGELOG entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ba3de1..e74412e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## UNRELEASED + +- Fix trailing text bug on `transformTags` options that was reported on [issue #506](https://github.com/punkave/sanitize-html/issues/506). Thanks to [Alex Rantos](https://github.com/alex-rantos). + ## 2.6.0 (2021-11-23) - Support for regular expressions in the `allowedClasses` option. Thanks to [Alex Rantos](https://github.com/alex-rantos). From d905b3b101bcca3fcb37375052b930bbe89f40e3 Mon Sep 17 00:00:00 2001 From: Alex Rantos Date: Tue, 7 Dec 2021 17:31:07 +0200 Subject: [PATCH 3/3] typo on changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e74412e..7ec3c55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## UNRELEASED -- Fix trailing text bug on `transformTags` options that was reported on [issue #506](https://github.com/punkave/sanitize-html/issues/506). Thanks to [Alex Rantos](https://github.com/alex-rantos). +- Fixed trailing text bug on `transformTags` options that was reported on [issue #506](https://github.com/punkave/sanitize-html/issues/506). Thanks to [Alex Rantos](https://github.com/alex-rantos). ## 2.6.0 (2021-11-23)