sanitize-html 2.11.0 → 2.12.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 +15 -2
  2. package/index.js +9 -2
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -1,7 +1,5 @@
1
1
  # sanitize-html
2
2
 
3
- [![CircleCI](https://circleci.com/gh/apostrophecms/sanitize-html/tree/main.svg?style=svg)](https://circleci.com/gh/apostrophecms/sanitize-html/tree/main)
4
-
5
3
  <a href="https://apostrophecms.com/"><img src="https://raw.githubusercontent.com/apostrophecms/sanitize-html/main/logos/logo-box-madefor.png" align="right" /></a>
6
4
 
7
5
  sanitize-html provides a simple HTML sanitizer with a clear API.
@@ -267,6 +265,21 @@ allowedAttributes: {
267
265
 
268
266
  With `multiple: true`, several allowed values may appear in the same attribute, separated by spaces. Otherwise the attribute must exactly match one and only one of the allowed values.
269
267
 
268
+ #### "What if I want to maintain the original case for SVG elements and attributes?"
269
+
270
+ If you're incorporating SVG elements like `linearGradient` into your content and notice that they're not rendering as expected due to case sensitivity issues, it's essential to prevent `sanitize-html` from converting element and attribute names to lowercase. This situation often arises when SVGs fail to display correctly because their case-sensitive tags, such as `linearGradient` and attributes like `viewBox`, are inadvertently lowercased.
271
+
272
+ To address this, ensure you set `lowerCaseTags: false` and `lowerCaseAttributeNames: false` in the parser options of your sanitize-html configuration. This adjustment stops the library from altering the case of your tags and attributes, preserving the integrity of your SVG content.
273
+
274
+ ```js
275
+ allowedTags: [ 'svg', 'g', 'defs', 'linearGradient', 'stop', 'circle' ],
276
+ allowedAttributes: false,
277
+ parser: {
278
+ lowerCaseTags: false,
279
+ lowerCaseAttributeNames: false
280
+ }
281
+ ```
282
+
270
283
  ### Wildcards for attributes
271
284
 
272
285
  You can use the `*` wildcard to allow all attributes with a certain prefix:
package/index.js CHANGED
@@ -295,9 +295,11 @@ function sanitizeHtml(html, options, _recursing) {
295
295
  delete frame.attribs[a];
296
296
  return;
297
297
  }
298
- // If the value is empty, and this is a known non-boolean attribute, delete it
298
+ // If the value is empty, check if the attribute is in the allowedEmptyAttributes array.
299
+ // If it is not in the allowedEmptyAttributes array, and it is a known non-boolean attribute, delete it
299
300
  // List taken from https://html.spec.whatwg.org/multipage/indices.html#attributes-3
300
- if (value === '' && (options.nonBooleanAttributes.includes(a) || options.nonBooleanAttributes.includes('*'))) {
301
+ if (value === '' && (!options.allowedEmptyAttributes.includes(a)) &&
302
+ (options.nonBooleanAttributes.includes(a) || options.nonBooleanAttributes.includes('*'))) {
301
303
  delete frame.attribs[a];
302
304
  return;
303
305
  }
@@ -474,6 +476,8 @@ function sanitizeHtml(html, options, _recursing) {
474
476
  result += ' ' + a;
475
477
  if (value && value.length) {
476
478
  result += '="' + escapeHtml(value, true) + '"';
479
+ } else if (options.allowedEmptyAttributes.includes(a)) {
480
+ result += '=""';
477
481
  }
478
482
  } else {
479
483
  delete frame.attribs[a];
@@ -876,6 +880,9 @@ sanitizeHtml.defaults = {
876
880
  // these attributes would make sense if we did.
877
881
  img: [ 'src', 'srcset', 'alt', 'title', 'width', 'height', 'loading' ]
878
882
  },
883
+ allowedEmptyAttributes: [
884
+ 'alt'
885
+ ],
879
886
  // Lots of these won't come up by default because we don't allow them
880
887
  selfClosing: [ 'img', 'br', 'hr', 'area', 'base', 'basefont', 'input', 'link', 'meta' ],
881
888
  // URL schemes we permit
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sanitize-html",
3
- "version": "2.11.0",
3
+ "version": "2.12.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",
@@ -41,4 +41,4 @@
41
41
  "mocha": "^10.2.0",
42
42
  "sinon": "^9.0.2"
43
43
  }
44
- }
44
+ }