Skip to content
This repository was archived by the owner on Feb 26, 2026. It is now read-only.

Commit 4f6cea6

Browse files
committed
Added 'allowedEmptyAttributes' option and kept empty 'alt' value by default.
1 parent cb6efe1 commit 4f6cea6

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## UNRELEASED
4+
- Added `allowedEmptyAttributes` option and kept empty `alt` value by default.
5+
36
## 2.11.0 (2023-06-21)
47

58
- Fix to allow `false` in `allowedClasses` attributes. Thanks to [Kevin Jiang](https://github.com/KevinSJ) for this fix!

index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,11 @@ function sanitizeHtml(html, options, _recursing) {
295295
delete frame.attribs[a];
296296
return;
297297
}
298-
// If the value is empty, and this is a known non-boolean attribute, delete it
298+
// If the value is empty, check if the attribute is in the allowedEmptyAttributes array.
299+
// If it is not in the allowedEmptyAttributes array, and it is a known non-boolean attribute, delete it
299300
// List taken from https://html.spec.whatwg.org/multipage/indices.html#attributes-3
300-
if (value === '' && (options.nonBooleanAttributes.includes(a) || options.nonBooleanAttributes.includes('*'))) {
301+
if (value === '' && (!options.allowedEmptyAttributes.includes(a)) && (options.nonBooleanAttributes.includes(a) ||
302+
options.nonBooleanAttributes.includes('*'))) {
301303
delete frame.attribs[a];
302304
return;
303305
}
@@ -474,6 +476,8 @@ function sanitizeHtml(html, options, _recursing) {
474476
result += ' ' + a;
475477
if (value && value.length) {
476478
result += '="' + escapeHtml(value, true) + '"';
479+
} else if (options.allowedEmptyAttributes.includes(a)) {
480+
result += '=""';
477481
}
478482
} else {
479483
delete frame.attribs[a];
@@ -876,6 +880,9 @@ sanitizeHtml.defaults = {
876880
// these attributes would make sense if we did.
877881
img: [ 'src', 'srcset', 'alt', 'title', 'width', 'height', 'loading' ]
878882
},
883+
allowedEmptyAttributes: [
884+
'alt'
885+
],
879886
// Lots of these won't come up by default because we don't allow them
880887
selfClosing: [ 'img', 'br', 'hr', 'area', 'base', 'basefont', 'input', 'link', 'meta' ],
881888
// URL schemes we permit

test/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,4 +1616,27 @@ describe('sanitizeHtml', function() {
16161616
nonBooleanAttributes: [ '*' ]
16171617
}), '<input type="checkbox" />');
16181618
});
1619+
it('should not remove empty alt attribute value by default', function() {
1620+
assert.equal(sanitizeHtml('<img alt="" src="https://example.com/" />', {
1621+
allowedAttributes: { img: [ 'alt', 'src' ] },
1622+
allowedTags: [ 'img' ]
1623+
}), '<img alt="" src="https://example.com/" />');
1624+
});
1625+
it('should not remove empty alt attribute value by default when disabled', function() {
1626+
assert.equal(sanitizeHtml('<img alt="" src="https://example.com/" />', {
1627+
allowedAttributes: { img: [ 'alt', 'src' ] },
1628+
allowedTags: [ 'img' ],
1629+
nonBooleanAttributes: []
1630+
}), '<img alt="" src="https://example.com/" />');
1631+
});
1632+
it('should set empty value to attribute specified in allowedEmptyAttributes option', function() {
1633+
assert.equal(sanitizeHtml('<a href target="_blank">hello</a>', {
1634+
allowedEmptyAttributes: [ 'href' ]
1635+
}), '<a href="" target="_blank">hello</a>');
1636+
});
1637+
it('should not remove empty attribute specified in allowedEmptyAttributes option', function() {
1638+
assert.equal(sanitizeHtml('<a href="" target="_blank">hello</a>', {
1639+
allowedEmptyAttributes: [ 'href' ]
1640+
}), '<a href="" target="_blank">hello</a>');
1641+
});
16191642
});

0 commit comments

Comments
 (0)