ghtml 2.0.2 → 2.0.4

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/.eslintrc.json CHANGED
@@ -3,6 +3,7 @@
3
3
  "extends": ["plugin:grules/all"],
4
4
  "rules": {
5
5
  "no-await-in-loop": "off",
6
+ "prefer-template": "off",
6
7
  "require-unicode-regexp": "off"
7
8
  }
8
9
  }
package/README.md CHANGED
@@ -8,7 +8,7 @@ Inspired by [html-template-tag](https://github.com/AntonioVdlC/html-template-tag
8
8
  npm i ghtml
9
9
  ```
10
10
 
11
- ## API Reference
11
+ ## API
12
12
 
13
13
  ### `html`
14
14
 
@@ -26,13 +26,13 @@ Keep in mind that, in Node.js, all else being equal, streaming a response using
26
26
 
27
27
  This version of HTML generator should be preferred for asynchronous and streaming use cases. The output is generated as the promise expressions resolve or stream expressions send data.
28
28
 
29
- **Minor Note:**
29
+ **Note:**
30
30
 
31
31
  Because they return generators instead of strings, a key difference of `htmlGenerator` and `htmlAsyncGenerator` is their ability to recognize and properly handle iterable elements within array expressions. This is to detect nested `htmlGenerator` and `htmlAsyncGenerator` usage, enabling scenarios such as ``${[1, 2, 3].map(i => htmlGenerator`<li>${i}</li>`)}``.
32
32
 
33
33
  ### `includeFile`
34
34
 
35
- Available in Node.js, the `includeFile` function is a wrapper around `readFileSync`. It reads and outputs the content of a file while also caching it in memory for faster future reuse.
35
+ Available in Node.js, the `includeFile` function is a wrapper around `readFileSync`. It reads and returns the content of a file while also caching it in memory for faster future reuse.
36
36
 
37
37
  ## Usage
38
38
 
@@ -171,4 +171,4 @@ console.log(logo);
171
171
 
172
172
  ## Security
173
173
 
174
- Like [similar](https://handlebarsjs.com/guide/#html-escaping) [tools](https://github.com/mde/ejs/blob/main/SECURITY.md#out-of-scope-vulnerabilities), `ghtml` does not prevent all kinds of XSS attacks. It is the responsibility of consumers to sanitize user inputs. Some inherently insecure uses include dynamically generating JavaScript, failing to quote HTML attribute values (especially when they contain expressions), and using unsanitized user-provided URLs.
174
+ Like [similar](https://github.com/mde/ejs/blob/main/SECURITY.md#out-of-scope-vulnerabilities) [tools](https://handlebarsjs.com/guide/#html-escaping), ghtml does not prevent all kinds of XSS attacks. It is the responsibility of developers to sanitize user inputs. Some inherently insecure uses include dynamically generating JavaScript, failing to quote HTML attribute values (especially when they contain expressions), and relying on unsanitized user-provided URLs.
package/SECURITY.md ADDED
@@ -0,0 +1,12 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ | Version | Supported |
6
+ | ------- | ------------------ |
7
+ | ^1 | :x: |
8
+ | ^2 | :white_check_mark: |
9
+
10
+ ## Reporting a Vulnerability
11
+
12
+ Please report all vulnerabilities to [https://github.com/gurgunday/ghtml/security](https://github.com/gurgunday/ghtml/security).
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Replace your template engine with fast JavaScript by leveraging the power of tagged templates.",
4
4
  "author": "Gürgün Dayıoğlu",
5
5
  "license": "MIT",
6
- "version": "2.0.2",
6
+ "version": "2.0.4",
7
7
  "type": "module",
8
8
  "main": "./src/index.js",
9
9
  "exports": {
package/src/html.js CHANGED
@@ -1,20 +1,8 @@
1
1
  const arrayIsArray = Array.isArray;
2
-
3
2
  const symbolIterator = Symbol.iterator;
4
-
5
3
  const symbolAsyncIterator = Symbol.asyncIterator;
6
4
 
7
- const escapeDictionary = {
8
- '"': "&#34;",
9
- "&": "&#38;",
10
- "'": "&#39;",
11
- "<": "&#60;",
12
- ">": "&#62;",
13
- "`": "&#96;",
14
- };
15
-
16
- const escapeRegExp = new RegExp(`[${Object.keys(escapeDictionary).join("")}]`);
17
-
5
+ const escapeRegExp = /["&'<>`]/;
18
6
  const escapeFunction = (string) => {
19
7
  const stringLength = string.length;
20
8
  let start = 0;
@@ -22,15 +10,37 @@ const escapeFunction = (string) => {
22
10
  let escaped = "";
23
11
 
24
12
  do {
25
- const escapedCharacter = escapeDictionary[string[end++]];
26
-
27
- if (escapedCharacter) {
28
- escaped += string.slice(start, end - 1) + escapedCharacter;
29
- start = end;
13
+ switch (string.charCodeAt(end++)) {
14
+ case 34: // "
15
+ escaped += string.slice(start, end - 1) + "&#34;";
16
+ start = end;
17
+ continue;
18
+ case 38: // &
19
+ escaped += string.slice(start, end - 1) + "&#38;";
20
+ start = end;
21
+ continue;
22
+ case 39: // '
23
+ escaped += string.slice(start, end - 1) + "&#39;";
24
+ start = end;
25
+ continue;
26
+ case 60: // <
27
+ escaped += string.slice(start, end - 1) + "&#60;";
28
+ start = end;
29
+ continue;
30
+ case 62: // >
31
+ escaped += string.slice(start, end - 1) + "&#62;";
32
+ start = end;
33
+ continue;
34
+ case 96: // `
35
+ escaped += string.slice(start, end - 1) + "&#96;";
36
+ start = end;
37
+ continue;
30
38
  }
31
39
  } while (end !== stringLength);
32
40
 
33
- return escaped + string.slice(start, end);
41
+ escaped += string.slice(start, end);
42
+
43
+ return escaped;
34
44
  };
35
45
 
36
46
  /**
@@ -49,7 +59,7 @@ const html = ({ raw: literals }, ...expressions) => {
49
59
  let string =
50
60
  typeof expression === "string"
51
61
  ? expression
52
- : expression === undefined || expression === null
62
+ : expression == null
53
63
  ? ""
54
64
  : arrayIsArray(expression)
55
65
  ? expression.join("")
@@ -64,7 +74,9 @@ const html = ({ raw: literals }, ...expressions) => {
64
74
  accumulator += literal + string;
65
75
  }
66
76
 
67
- return accumulator + literals[index];
77
+ accumulator += literals[index];
78
+
79
+ return accumulator;
68
80
  };
69
81
 
70
82
  /**
@@ -83,7 +95,7 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
83
95
 
84
96
  if (typeof expression === "string") {
85
97
  string = expression;
86
- } else if (expression === undefined || expression === null) {
98
+ } else if (expression == null) {
87
99
  string = "";
88
100
  } else {
89
101
  if (expression[symbolIterator]) {
@@ -102,7 +114,7 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
102
114
  if (typeof expression === "string") {
103
115
  string = expression;
104
116
  } else {
105
- if (expression === undefined || expression === null) {
117
+ if (expression == null) {
106
118
  continue;
107
119
  }
108
120
 
@@ -111,7 +123,7 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
111
123
  if (typeof expression === "string") {
112
124
  string = expression;
113
125
  } else {
114
- if (expression === undefined || expression === null) {
126
+ if (expression == null) {
115
127
  continue;
116
128
  }
117
129
 
@@ -180,7 +192,7 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
180
192
 
181
193
  if (typeof expression === "string") {
182
194
  string = expression;
183
- } else if (expression === undefined || expression === null) {
195
+ } else if (expression == null) {
184
196
  string = "";
185
197
  } else {
186
198
  if (expression[symbolIterator] || expression[symbolAsyncIterator]) {
@@ -199,7 +211,7 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
199
211
  if (typeof expression === "string") {
200
212
  string = expression;
201
213
  } else {
202
- if (expression === undefined || expression === null) {
214
+ if (expression == null) {
203
215
  continue;
204
216
  }
205
217
 
@@ -208,7 +220,7 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
208
220
  if (typeof expression === "string") {
209
221
  string = expression;
210
222
  } else {
211
- if (expression === undefined || expression === null) {
223
+ if (expression == null) {
212
224
  continue;
213
225
  }
214
226
 
package/test/index.js CHANGED
@@ -63,6 +63,13 @@ test("renders unsafe content", () => {
63
63
  );
64
64
  });
65
65
 
66
+ test("renders unsafe content /2", () => {
67
+ assert.strictEqual(
68
+ html`<p>${`${descriptionUnsafe}"&\``}</p>`,
69
+ `<p>&#60;script&#62;alert(&#39;This is an unsafe description.&#39;)&#60;/script&#62;&#34;&#38;&#96;</p>`,
70
+ );
71
+ });
72
+
66
73
  test("renders arrays", () => {
67
74
  assert.strictEqual(
68
75
  html`<p>${[descriptionSafe, descriptionUnsafe]}</p>`,