Skip to content

Commit

Permalink
feat: support wildcard on empty attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanarmstrong committed Jun 5, 2023
1 parent cb8d1e2 commit cebdc07
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ Very simple! Set `nonBooleanAttributes` to `[]`.
nonBooleanAttributes: []
```

#### "What if I want to delete all empty attributes?"

Also very simple! Set `nonBooleanAttributes` to `['*']`.

```js
nonBooleanAttributes: ['*']
```

#### "What if I don't want to allow *any* tags?"

Also simple! Set `allowedTags` to `[]` and `allowedAttributes` to `{}`.
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ function sanitizeHtml(html, options, _recursing) {
}
// If the value is empty, and this 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)) {
if (value === '' && (options.nonBooleanAttributes.includes(a) || options.nonBooleanAttributes.includes('*'))) {
delete frame.attribs[a];
return;
}
Expand Down
9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1593,4 +1593,13 @@ describe('sanitizeHtml', function() {
}
}), '<input checked type="checkbox" />');
});
it('should remove boolean attributes that are empty when wildcard * passed in', function() {
assert.equal(sanitizeHtml('<input checked form type="checkbox" />', {
allowedTags: 'input',
allowedAttributes: {
input: [ 'checked', 'form', 'type' ]
},
nonBooleanAttributes: [ '*' ]
}), '<input type="checkbox" />');
});
});

0 comments on commit cebdc07

Please sign in to comment.