You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

sanitize-html

Package Overview
Dependencies
Maintainers
16
Versions
120
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sanitize-html - npm Package Compare versions

Comparing version
2.15.0
to
2.16.0
+7
-0
index.js

@@ -223,2 +223,6 @@ const htmlparser = require('htmlparser2');

onopentag: function(name, attribs) {
if (options.onOpenTag) {
options.onOpenTag(name, attribs);
}
// If `enforceHtmlBoundary` is `true` and this has found the opening

@@ -548,2 +552,5 @@ // `html` tag, reset the state.

onclosetag: function(name, isImplied) {
if (options.onCloseTag) {
options.onCloseTag(name, isImplied);
}

@@ -550,0 +557,0 @@ if (skipText) {

+1
-1
{
"name": "sanitize-html",
"version": "2.15.0",
"version": "2.16.0",
"description": "Clean up user-submitted HTML, preserving allowlisted elements and allowlisted attributes on a per-element basis",

@@ -5,0 +5,0 @@ "sideEffects": false,

@@ -784,2 +784,46 @@ # sanitize-html

### Advanced filtering
For more advanced filtering you can hook directly into the parsing process using tag open and tag close events.
The `onOpenTag` event is triggered when an opening tag is encountered. It has two arguments:
- `tagName`: The name of the tag.
- `attribs`: An object containing the tag's attributes, e.g. `{ src: "/path/to/tux.png" }`.
The `onCloseTag` event is triggered when a closing tag is encountered. It has the following arguments:
- `tagName`: The name of the tag.
- `isImplied`: A boolean indicating whether the closing tag is implied (e.g. `<p>foo<p>bar`) or explicit (e.g. `<p>foo</p><p>bar</p>`).
For example, you may want to add spaces around a removed tag, like this:
```js
const allowedTags = [ 'b' ];
let addSpace = false;
const sanitizedHtml = sanitizeHtml(
'There should be<div><p>spaces</p></div>between <b>these</b> words.',
{
allowedTags,
onOpenTag: (tagName, attribs) => {
addSpace = !allowedTags.includes(tagName);
},
onCloseTag: (tagName, isImplied) => {
addSpace = !allowedTags.includes(tagName);
},
textFilter: (text) => {
if (addSpace) {
addSpace = false;
return ' ' + text;
}
return text;
}
}
);
```
In this example, we are setting a flag when a tag that will be removed has been opened or closed. Then we use the `textFilter` to modify the text to include spaces. The example should produce:
```
There should be spaces between <b>these</b> words.
```
This is a simplified example that is not meant to be production-ready. For your specific case, you may want to keep track of currently open tags, using the open and close events to push and pop items on the stack, or only insert spaces next to a subset of disallowed tags.
## About ApostropheCMS

@@ -786,0 +830,0 @@