sanitize-html 2.16.0 → 2.17.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.
Files changed (3) hide show
  1. package/README.md +10 -1
  2. package/index.js +10 -2
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -745,7 +745,7 @@ This will transform `<disallowed>content <allowed>content</allowed> </disallowed
745
745
 
746
746
  #### Escape the disallowed tag and all its children even for allowed tags.
747
747
 
748
- if you set `disallowedTagsMode` to `recursiveEscape`, disallowed tag and its children will be escaped even for allowed tags
748
+ if you set `disallowedTagsMode` to `recursiveEscape`, disallowed tags and their children will be escaped even for allowed tags:
749
749
 
750
750
  ```js
751
751
  disallowedTagsMode: `recursiveEscape`
@@ -753,6 +753,15 @@ disallowedTagsMode: `recursiveEscape`
753
753
 
754
754
  This will transform `<disallowed>hello<p>world</p></disallowed>` to `&lt;disallowed&gt;hello&lt;p&gt;world&lt;/p&gt;&lt;/disallowed&gt;`
755
755
 
756
+ #### Escape the disallowed tag, including all its attributes.
757
+
758
+ By default, attributes are not preserved when tags are escaped. You can set `preserveEscapedAttributes` to `true` to
759
+ keep the attributes, which will also be escaped and therefore have no effect on the browser.
760
+
761
+ ```js
762
+ preserveEscapedAttributes: true
763
+ ```
764
+
756
765
  ### Ignore style attribute contents
757
766
 
758
767
  Instead of discarding faulty style attributes, you can allow them by disabling the parsing of style attributes:
package/index.js CHANGED
@@ -300,7 +300,14 @@ function sanitizeHtml(html, options, _recursing) {
300
300
  }
301
301
  }
302
302
 
303
- if (!allowedAttributesMap || has(allowedAttributesMap, name) || allowedAttributesMap['*']) {
303
+ const isBeingEscaped = skip && (options.disallowedTagsMode === 'escape' || options.disallowedTagsMode === 'recursiveEscape');
304
+ const shouldPreserveEscapedAttributes = isBeingEscaped && options.preserveEscapedAttributes;
305
+
306
+ if (shouldPreserveEscapedAttributes) {
307
+ each(attribs, function(value, a) {
308
+ result += ' ' + a + '="' + escapeHtml((value || ''), true) + '"';
309
+ });
310
+ } else if (!allowedAttributesMap || has(allowedAttributesMap, name) || allowedAttributesMap['*']) {
304
311
  each(attribs, function(value, a) {
305
312
  if (!VALID_HTML_ATTRIBUTE_NAME.test(a)) {
306
313
  // This prevents part of an attribute name in the output from being
@@ -923,7 +930,8 @@ sanitizeHtml.defaults = {
923
930
  allowedSchemesAppliedToAttributes: [ 'href', 'src', 'cite' ],
924
931
  allowProtocolRelative: true,
925
932
  enforceHtmlBoundary: false,
926
- parseStyleAttributes: true
933
+ parseStyleAttributes: true,
934
+ preserveEscapedAttributes: false
927
935
  };
928
936
 
929
937
  sanitizeHtml.simpleTransform = function(newTagName, newAttribs, merge) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sanitize-html",
3
- "version": "2.16.0",
3
+ "version": "2.17.0",
4
4
  "description": "Clean up user-submitted HTML, preserving allowlisted elements and allowlisted attributes on a per-element basis",
5
5
  "sideEffects": false,
6
6
  "main": "index.js",