Skip to content

Commit

Permalink
Added 'allowedEmptyAttributes' option and kept empty 'alt' value by d…
Browse files Browse the repository at this point in the history
…efault.
  • Loading branch information
zhna123 committed Sep 26, 2023
1 parent cb6efe1 commit 4f6cea6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
# Changelog

## UNRELEASED
- Added `allowedEmptyAttributes` option and kept empty `alt` value by default.

## 2.11.0 (2023-06-21)

- Fix to allow `false` in `allowedClasses` attributes. Thanks to [Kevin Jiang](https://github.com/KevinSJ) for this fix!
Expand Down
11 changes: 9 additions & 2 deletions index.js
Expand Up @@ -295,9 +295,11 @@ function sanitizeHtml(html, options, _recursing) {
delete frame.attribs[a];
return;
}
// If the value is empty, and this is a known non-boolean attribute, delete it
// If the value is empty, check if the attribute is in the allowedEmptyAttributes array.
// If it is not in the allowedEmptyAttributes array, and it is a known non-boolean attribute, delete it
// List taken from https://html.spec.whatwg.org/multipage/indices.html#attributes-3
if (value === '' && (options.nonBooleanAttributes.includes(a) || options.nonBooleanAttributes.includes('*'))) {
if (value === '' && (!options.allowedEmptyAttributes.includes(a)) && (options.nonBooleanAttributes.includes(a) ||
options.nonBooleanAttributes.includes('*'))) {
delete frame.attribs[a];
return;
}
Expand Down Expand Up @@ -474,6 +476,8 @@ function sanitizeHtml(html, options, _recursing) {
result += ' ' + a;
if (value && value.length) {
result += '="' + escapeHtml(value, true) + '"';
} else if (options.allowedEmptyAttributes.includes(a)) {
result += '=""';
}
} else {
delete frame.attribs[a];
Expand Down Expand Up @@ -876,6 +880,9 @@ sanitizeHtml.defaults = {
// these attributes would make sense if we did.
img: [ 'src', 'srcset', 'alt', 'title', 'width', 'height', 'loading' ]
},
allowedEmptyAttributes: [
'alt'
],
// Lots of these won't come up by default because we don't allow them
selfClosing: [ 'img', 'br', 'hr', 'area', 'base', 'basefont', 'input', 'link', 'meta' ],
// URL schemes we permit
Expand Down
23 changes: 23 additions & 0 deletions test/test.js
Expand Up @@ -1616,4 +1616,27 @@ describe('sanitizeHtml', function() {
nonBooleanAttributes: [ '*' ]
}), '<input type="checkbox" />');
});
it('should not remove empty alt attribute value by default', function() {
assert.equal(sanitizeHtml('<img alt="" src="https://example.com/" />', {
allowedAttributes: { img: [ 'alt', 'src' ] },
allowedTags: [ 'img' ]
}), '<img alt="" src="https://example.com/" />');
});
it('should not remove empty alt attribute value by default when disabled', function() {
assert.equal(sanitizeHtml('<img alt="" src="https://example.com/" />', {
allowedAttributes: { img: [ 'alt', 'src' ] },
allowedTags: [ 'img' ],
nonBooleanAttributes: []
}), '<img alt="" src="https://example.com/" />');
});
it('should set empty value to attribute specified in allowedEmptyAttributes option', function() {
assert.equal(sanitizeHtml('<a href target="_blank">hello</a>', {
allowedEmptyAttributes: [ 'href' ]
}), '<a href="" target="_blank">hello</a>');
});
it('should not remove empty attribute specified in allowedEmptyAttributes option', function() {
assert.equal(sanitizeHtml('<a href="" target="_blank">hello</a>', {
allowedEmptyAttributes: [ 'href' ]
}), '<a href="" target="_blank">hello</a>');
});
});

0 comments on commit 4f6cea6

Please sign in to comment.