sanitize-html
Advanced tools
+7
-0
@@ -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, |
+44
-0
@@ -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 @@ |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
68046
2.91%872
0.69%835
5.56%