sanitize-html 2.5.3 → 2.6.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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.6.0 (2021-11-23)
4
+
5
+ - Support for regular expressions in the `allowedClasses` option. Thanks to [Alex Rantos](https://github.com/alex-rantos).
6
+
3
7
  ## 2.5.3 (2021-11-02):
4
8
 
5
9
  - Fixed bug introduced by klona 2.0.5, by removing klona entirely.
package/README.md CHANGED
@@ -239,6 +239,7 @@ const clean = sanitizeHtml(dirty, {
239
239
  ```
240
240
 
241
241
  Similar to `allowedAttributes`, you can use `*` to allow classes with a certain prefix, or use `*` as a tag name to allow listed classes to be valid for any tag:
242
+
242
243
  ```js
243
244
  allowedClasses: {
244
245
  'code': [ 'language-*', 'lang-*' ],
@@ -246,6 +247,16 @@ allowedClasses: {
246
247
  }
247
248
  ```
248
249
 
250
+ Furthermore, regular expressions are supported too:
251
+
252
+ ```js
253
+ allowedClasses: {
254
+ p: [ /^regex\d{2}$/ ]
255
+ }
256
+ ```
257
+
258
+ > Note: It is advised that your regular expressions always begin with `^` so that you are requiring a known prefix. A regular expression with neither `^` nor `$` just requires that something appear in the middle.
259
+
249
260
  ### Allowed CSS Styles
250
261
 
251
262
  If you wish to allow specific CSS _styles_ on a particular element, you can do that with the `allowedStyles` option. Simply declare your desired attributes as regular expression options within an array for the given attribute. Specific elements will inherit allowlisted attributes from the global (`*`) attribute. Any other CSS classes are discarded.
package/index.js CHANGED
@@ -156,6 +156,7 @@ function sanitizeHtml(html, options, _recursing) {
156
156
  }
157
157
  const allowedClassesMap = {};
158
158
  const allowedClassesGlobMap = {};
159
+ const allowedClassesRegexMap = {};
159
160
  each(options.allowedClasses, function(classes, tag) {
160
161
  // Implicitly allows the class attribute
161
162
  if (allowedAttributesMap) {
@@ -166,10 +167,13 @@ function sanitizeHtml(html, options, _recursing) {
166
167
  }
167
168
 
168
169
  allowedClassesMap[tag] = [];
170
+ allowedClassesRegexMap[tag] = [];
169
171
  const globRegex = [];
170
172
  classes.forEach(function(obj) {
171
173
  if (typeof obj === 'string' && obj.indexOf('*') >= 0) {
172
174
  globRegex.push(escapeStringRegexp(obj).replace(/\\\*/g, '.*'));
175
+ } else if (obj instanceof RegExp) {
176
+ allowedClassesRegexMap[tag].push(obj);
173
177
  } else {
174
178
  allowedClassesMap[tag].push(obj);
175
179
  }
@@ -431,12 +435,16 @@ function sanitizeHtml(html, options, _recursing) {
431
435
  const allowedSpecificClasses = allowedClassesMap[name];
432
436
  const allowedWildcardClasses = allowedClassesMap['*'];
433
437
  const allowedSpecificClassesGlob = allowedClassesGlobMap[name];
438
+ const allowedSpecificClassesRegex = allowedClassesRegexMap[name];
434
439
  const allowedWildcardClassesGlob = allowedClassesGlobMap['*'];
435
- const allowedClassesGlobs = [ allowedSpecificClassesGlob, allowedWildcardClassesGlob ].filter(
436
- function(t) {
440
+ const allowedClassesGlobs = [
441
+ allowedSpecificClassesGlob,
442
+ allowedWildcardClassesGlob
443
+ ]
444
+ .concat(allowedSpecificClassesRegex)
445
+ .filter(function (t) {
437
446
  return t;
438
- }
439
- );
447
+ });
440
448
  if (allowedSpecificClasses && allowedWildcardClasses) {
441
449
  value = filterClasses(value, deepmerge(allowedSpecificClasses, allowedWildcardClasses), allowedClassesGlobs);
442
450
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sanitize-html",
3
- "version": "2.5.3",
3
+ "version": "2.6.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",