@fjpm03/sanitize-html 2.17.7 → 3.0.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 -10
  2. package/index.js +14 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -55,14 +55,14 @@ But, perhaps you'd like to display sanitized HTML immediately in the browser for
55
55
  * Install the package:
56
56
 
57
57
  ```bash
58
- npm install sanitize-html
58
+ npm install sanitize-html@npm:@fjpm03/sanitize-html
59
59
  ```
60
60
  or
61
61
  ```
62
- yarn add sanitize-html
62
+ yarn add sanitize-html@npm:@fjpm03/sanitize-html
63
63
  ```
64
64
 
65
- The primary change in the 2.x version of sanitize-html is that it no longer includes a build that is ready for browser use. Developers are expected to include sanitize-html in their project builds (e.g., webpack) as they would any other dependency. So while sanitize-html is no longer ready to link to directly in HTML, developers can now more easily process it according to their needs.
65
+ The primary change in the 3.x version of this fork is that sanitize-html loads `htmlparser2` asynchronously because current `htmlparser2` releases are ESM-only. Consumers must await calls to `sanitizeHtml()`.
66
66
 
67
67
  Once built and linked in the browser with other project Javascript, it can be used to sanitize HTML strings in front end code:
68
68
 
@@ -70,10 +70,10 @@ Once built and linked in the browser with other project Javascript, it can be us
70
70
  import sanitizeHtml from 'sanitize-html';
71
71
 
72
72
  const html = "<strong>hello world</strong>";
73
- console.log(sanitizeHtml(html));
74
- console.log(sanitizeHtml("<img src=x onerror=alert('img') />"));
75
- console.log(sanitizeHtml("console.log('hello world')"));
76
- console.log(sanitizeHtml("<script>alert('hello world')</script>"));
73
+ console.log(await sanitizeHtml(html));
74
+ console.log(await sanitizeHtml("<img src=x onerror=alert('img') />"));
75
+ console.log(await sanitizeHtml("console.log('hello world')"));
76
+ console.log(await sanitizeHtml("<script>alert('hello world')</script>"));
77
77
  ```
78
78
 
79
79
  ### Node (Recommended)
@@ -81,7 +81,7 @@ console.log(sanitizeHtml("<script>alert('hello world')</script>"));
81
81
  Install module from console:
82
82
 
83
83
  ```bash
84
- npm install sanitize-html
84
+ npm install sanitize-html@npm:@fjpm03/sanitize-html
85
85
  ```
86
86
 
87
87
  Import the module:
@@ -98,14 +98,14 @@ Use it in your JavaScript app:
98
98
 
99
99
  ```js
100
100
  const dirty = 'some really tacky HTML';
101
- const clean = sanitizeHtml(dirty);
101
+ const clean = await sanitizeHtml(dirty);
102
102
  ```
103
103
 
104
104
  That will allow our [default list of allowed tags and attributes](#default-options) through. It's a nice set, but probably not quite what you want. So:
105
105
 
106
106
  ```js
107
107
  // Allow only a super restricted set of tags and attributes
108
- const clean = sanitizeHtml(dirty, {
108
+ const clean = await sanitizeHtml(dirty, {
109
109
  allowedTags: [ 'b', 'i', 'em', 'strong', 'a' ],
110
110
  allowedAttributes: {
111
111
  'a': [ 'href' ]
package/index.js CHANGED
@@ -1,10 +1,16 @@
1
- const htmlparser = require('htmlparser2');
2
1
  const escapeStringRegexp = require('escape-string-regexp');
3
2
  const { isPlainObject } = require('is-plain-object');
4
3
  const deepmerge = require('deepmerge');
5
4
  const parseSrcset = require('parse-srcset');
6
5
  const { parse: postcssParse } = require('postcss');
7
6
  const { naughtyHref: launderNaughtyHref } = require('launder');
7
+ let htmlparserPromise;
8
+
9
+ function getHtmlparser() {
10
+ htmlparserPromise = htmlparserPromise || import('htmlparser2');
11
+ return htmlparserPromise;
12
+ }
13
+
8
14
  // Tags that can conceivably represent stand-alone media.
9
15
  const mediaTags = [
10
16
  'img', 'audio', 'video', 'picture', 'svg',
@@ -107,7 +113,12 @@ const VALID_HTML_ATTRIBUTE_NAME = /^[^\0\t\n\f\r /<=>]+$/;
107
113
  // invocation as a guard against this exploit:
108
114
  // https://github.com/fb55/htmlparser2/issues/105
109
115
 
110
- function sanitizeHtml(html, options, _recursing) {
116
+ async function sanitizeHtml(html, options, _recursing) {
117
+ const htmlparser = await getHtmlparser();
118
+ return sanitizeHtmlWithParser(htmlparser, html, options, _recursing);
119
+ }
120
+
121
+ function sanitizeHtmlWithParser(htmlparser, html, options, _recursing) {
111
122
  if (html == null) {
112
123
  return '';
113
124
  }
@@ -661,7 +672,7 @@ function sanitizeHtml(html, options, _recursing) {
661
672
  !tagAllowed(tag) &&
662
673
  options.disallowedTagsMode === 'discard'
663
674
  ) {
664
- result += sanitizeHtml(text, options);
675
+ result += sanitizeHtmlWithParser(htmlparser, text, options);
665
676
  } else if (!addedText) {
666
677
  const escaped = escapeHtml(text, false);
667
678
  if (options.textFilter) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fjpm03/sanitize-html",
3
- "version": "2.17.7",
3
+ "version": "3.0.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",