Skip to content

Commit 2c7ac45

Browse files
committed
Added more tests and modified CHANGELOG
1 parent 4f6cea6 commit 2c7ac45

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Changelog
22

33
## UNRELEASED
4-
- Added `allowedEmptyAttributes` option and kept empty `alt` value by default.
4+
5+
- Introduced the `allowedEmptyAttributes` option, enabling explicit specification of empty string values for select attributes, with the default attribute set to `alt`.
56

67
## 2.11.0 (2023-06-21)
78

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ function sanitizeHtml(html, options, _recursing) {
298298
// If the value is empty, check if the attribute is in the allowedEmptyAttributes array.
299299
// If it is not in the allowedEmptyAttributes array, and it is a known non-boolean attribute, delete it
300300
// List taken from https://html.spec.whatwg.org/multipage/indices.html#attributes-3
301-
if (value === '' && (!options.allowedEmptyAttributes.includes(a)) && (options.nonBooleanAttributes.includes(a) ||
302-
options.nonBooleanAttributes.includes('*'))) {
301+
if (value === '' && (!options.allowedEmptyAttributes.includes(a)) &&
302+
(options.nonBooleanAttributes.includes(a) || options.nonBooleanAttributes.includes('*'))) {
303303
delete frame.attribs[a];
304304
return;
305305
}

test/test.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -1622,21 +1622,31 @@ describe('sanitizeHtml', function() {
16221622
allowedTags: [ 'img' ]
16231623
}), '<img alt="" src="https://example.com/" />');
16241624
});
1625-
it('should not remove empty alt attribute value by default when disabled', function() {
1625+
it('should convert the implicit empty alt attribute value to be an empty string by default', function() {
1626+
assert.equal(sanitizeHtml('<img alt src="https://example.com/" />', {
1627+
allowedAttributes: { img: [ 'alt', 'src' ] },
1628+
allowedTags: [ 'img' ]
1629+
}), '<img alt="" src="https://example.com/" />');
1630+
});
1631+
it('should not remove empty alt attribute value by default when an empty nonBooleanAttributes option passed in', function() {
16261632
assert.equal(sanitizeHtml('<img alt="" src="https://example.com/" />', {
16271633
allowedAttributes: { img: [ 'alt', 'src' ] },
16281634
allowedTags: [ 'img' ],
16291635
nonBooleanAttributes: []
16301636
}), '<img alt="" src="https://example.com/" />');
16311637
});
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>');
1638+
it('should not remove the empty attributes specified in allowedEmptyAttributes option', function() {
1639+
assert.equal(sanitizeHtml('<img alt="" src="" />', {
1640+
allowedAttributes: { img: [ 'alt', 'src' ] },
1641+
allowedTags: [ 'img' ],
1642+
allowedEmptyAttributes: [ 'alt', 'src' ]
1643+
}), '<img alt="" src="" />');
16361644
});
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>');
1645+
it('should remove all the empty attributes when an empty allowedEmptyAttributes option passed in', function() {
1646+
assert.equal(sanitizeHtml('<img alt="" src="https://example.com/" target="" />', {
1647+
allowedAttributes: { img: [ 'alt', 'src' ] },
1648+
allowedTags: [ 'img' ],
1649+
allowedEmptyAttributes: []
1650+
}), '<img src="https://example.com/" />');
16411651
});
16421652
});

0 commit comments

Comments
 (0)