Skip to content

Commit

Permalink
Merge pull request #521 from alex-rantos/fix-trailing-text
Browse files Browse the repository at this point in the history
Fix trailing text issue on transformTag
  • Loading branch information
abea authored Dec 7, 2021
2 parents d737f39 + d905b3b commit d077c9f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## UNRELEASED

- 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)

- Support for regular expressions in the `allowedClasses` option. Thanks to [Alex Rantos](https://github.com/alex-rantos).
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ function sanitizeHtml(html, options, _recursing) {
result = tempResult + escapeHtml(result);
tempResult = '';
}
addedText = false;
}
}, options.parser);
parser.write(html);
Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,19 @@ describe('sanitizeHtml', function() {
}), '<a href="http://somelink">some good text</a>');
});

it('should preserve trailing text when replacing the tagName and adding new text via transforming function', function () {
assert.equal(sanitizeHtml('<p>text before <br> text after</p>', {
transformTags: {
br: function (_tagName, _attribs) {
return {
tagName: 'span',
text: ' '
};
}
}
}), '<p>text before <span> </span> text after</p>');
});

it('should add new text when not initially set and replace attributes when they are changed by transforming function', function () {
assert.equal(sanitizeHtml('<a href="http://somelink"></a>', {
transformTags: {
Expand Down

0 comments on commit d077c9f

Please sign in to comment.