sanitize-html 2.15.0 → 2.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -0
- package/index.js +7 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -781,6 +781,50 @@ nestingLimit: 6
|
|
|
781
781
|
|
|
782
782
|
This will prevent the user from nesting tags more than 6 levels deep. Tags deeper than that are stripped out exactly as if they were disallowed. Note that this means text is preserved in the usual ways where appropriate.
|
|
783
783
|
|
|
784
|
+
### Advanced filtering
|
|
785
|
+
|
|
786
|
+
For more advanced filtering you can hook directly into the parsing process using tag open and tag close events.
|
|
787
|
+
|
|
788
|
+
The `onOpenTag` event is triggered when an opening tag is encountered. It has two arguments:
|
|
789
|
+
- `tagName`: The name of the tag.
|
|
790
|
+
- `attribs`: An object containing the tag's attributes, e.g. `{ src: "/path/to/tux.png" }`.
|
|
791
|
+
|
|
792
|
+
The `onCloseTag` event is triggered when a closing tag is encountered. It has the following arguments:
|
|
793
|
+
- `tagName`: The name of the tag.
|
|
794
|
+
- `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>`).
|
|
795
|
+
|
|
796
|
+
For example, you may want to add spaces around a removed tag, like this:
|
|
797
|
+
```js
|
|
798
|
+
const allowedTags = [ 'b' ];
|
|
799
|
+
let addSpace = false;
|
|
800
|
+
const sanitizedHtml = sanitizeHtml(
|
|
801
|
+
'There should be<div><p>spaces</p></div>between <b>these</b> words.',
|
|
802
|
+
{
|
|
803
|
+
allowedTags,
|
|
804
|
+
onOpenTag: (tagName, attribs) => {
|
|
805
|
+
addSpace = !allowedTags.includes(tagName);
|
|
806
|
+
},
|
|
807
|
+
onCloseTag: (tagName, isImplied) => {
|
|
808
|
+
addSpace = !allowedTags.includes(tagName);
|
|
809
|
+
},
|
|
810
|
+
textFilter: (text) => {
|
|
811
|
+
if (addSpace) {
|
|
812
|
+
addSpace = false;
|
|
813
|
+
return ' ' + text;
|
|
814
|
+
}
|
|
815
|
+
return text;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
);
|
|
819
|
+
```
|
|
820
|
+
|
|
821
|
+
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:
|
|
822
|
+
```
|
|
823
|
+
There should be spaces between <b>these</b> words.
|
|
824
|
+
```
|
|
825
|
+
|
|
826
|
+
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.
|
|
827
|
+
|
|
784
828
|
## About ApostropheCMS
|
|
785
829
|
|
|
786
830
|
sanitize-html was created at [P'unk Avenue](https://punkave.com) for use in [ApostropheCMS](https://apostrophecms.com), an open-source content management system built on Node.js. If you like sanitize-html you should definitely check out ApostropheCMS.
|
package/index.js
CHANGED
|
@@ -220,6 +220,10 @@ function sanitizeHtml(html, options, _recursing) {
|
|
|
220
220
|
|
|
221
221
|
const parser = new htmlparser.Parser({
|
|
222
222
|
onopentag: function(name, attribs) {
|
|
223
|
+
if (options.onOpenTag) {
|
|
224
|
+
options.onOpenTag(name, attribs);
|
|
225
|
+
}
|
|
226
|
+
|
|
223
227
|
// If `enforceHtmlBoundary` is `true` and this has found the opening
|
|
224
228
|
// `html` tag, reset the state.
|
|
225
229
|
if (options.enforceHtmlBoundary && name === 'html') {
|
|
@@ -544,6 +548,9 @@ function sanitizeHtml(html, options, _recursing) {
|
|
|
544
548
|
}
|
|
545
549
|
},
|
|
546
550
|
onclosetag: function(name, isImplied) {
|
|
551
|
+
if (options.onCloseTag) {
|
|
552
|
+
options.onCloseTag(name, isImplied);
|
|
553
|
+
}
|
|
547
554
|
|
|
548
555
|
if (skipText) {
|
|
549
556
|
skipTextDepth--;
|
package/package.json
CHANGED