Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/sanitize-html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,13 @@ function sanitizeHtml(html, options, _recursing) {
result += ' />';
} else {
result += '>';
if (frame.innerText && !hasText && !options.textFilter) {
result += escapeHtml(frame.innerText);
if (frame.innerText && !hasText) {
const escaped = escapeHtml(frame.innerText);
if (options.textFilter) {
result += options.textFilter(escaped, name);
} else {
result += escaped;
}
addedText = true;
}
}
Expand Down
18 changes: 18 additions & 0 deletions packages/sanitize-html/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,24 @@ describe('sanitizeHtml', function() {
}), '<a href="http://somelink">some new text</a>');
});

it('should add new text to an empty tag when transforming function sets it and an identity textFilter is present', function () {
assert.equal(sanitizeHtml('<a></a>', {
allowedTags: [ 'a' ],
textFilter: function (text) {
return text;
},
transformTags: {
a: function (tagName, attribs) {
return {
tagName,
attribs,
text: 'some new text'
};
}
}
}), '<a>some new text</a>');
});

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