Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes important stripping #522

Merged
merged 2 commits into from
Dec 7, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## UNRELEASED

- Fixes style filtering to retain `!important` when used.
- Fixed trailing text bug on `transformTags` options that was reported on [issue #506](https://github.com/punkave/sanitize-html/issues/506). Thanks to [Alex Rantos](https://github.com/alex-rantos).

## 2.6.0 (2021-11-23)
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,17 +691,17 @@ function sanitizeHtml(html, options, _recursing) {
}

/**
* Extracts the style attribues from an AbstractSyntaxTree and formats those
* Extracts the style attributes from an AbstractSyntaxTree and formats those
* values in the inline style attribute format.
*
* @param {AbstractSyntaxTree} filteredAST
* @return {string} - Example: "color:yellow;text-align:center;font-family:helvetica;"
* @return {string} - Example: "color:yellow;text-align:center !important;font-family:helvetica;"
*/
function stringifyStyleAttributes(filteredAST) {
return filteredAST.nodes[0].nodes
.reduce(function(extractedAttributes, attributeObject) {
.reduce(function(extractedAttributes, attrObject) {
extractedAttributes.push(
attributeObject.prop + ':' + attributeObject.value
`${attrObject.prop}:${attrObject.value}${attrObject.important ? ' !important' : ''}`
);
return extractedAttributes;
}, [])
Expand Down
15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,21 @@ describe('sanitizeHtml', function() {
}), '<span style="color:blue"></span>'
);
});
it('Should support !important styles', function() {
assert.equal(
sanitizeHtml('<span style=\'color: blue !important\'></span>', {
allowedTags: false,
allowedAttributes: {
span: [ 'style' ]
},
allowedStyles: {
span: {
color: [ /blue/ ]
}
}
}), '<span style="color:blue !important"></span>'
);
});
it('Should allow a specific style from global', function() {
assert.equal(
sanitizeHtml('<span style=\'color: yellow; text-align: center; font-family: helvetica\'></span>', {
Expand Down