ghtml 3.0.3 → 3.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/bench/index.js CHANGED
@@ -104,6 +104,10 @@ bench.add("mixed escaped and unescaped expressions", () => {
104
104
  `;
105
105
  });
106
106
 
107
+ bench.add("sparse escape", () => {
108
+ result = html`<p>${`${"noescape".repeat(250)}<>`.repeat(5)}</p>`;
109
+ });
110
+
107
111
  await bench.warmup();
108
112
  await bench.run();
109
113
 
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": "3.0.3",
6
+ "version": "3.0.4",
7
7
  "type": "module",
8
8
  "bin": "./bin/src/index.js",
9
9
  "main": "./src/index.js",
package/src/html.js CHANGED
@@ -1,41 +1,41 @@
1
- const escapeRegExp = /["&'<=>]/;
1
+ const escapeRegExp = /["&'<=>]/g;
2
2
 
3
3
  const escapeFunction = (string) => {
4
+ if (!string || !escapeRegExp.test(string)) {
5
+ return string;
6
+ }
7
+
4
8
  let escaped = "";
5
9
  let start = 0;
6
10
 
7
- for (let end = 0; end !== string.length; ++end) {
8
- switch (string.charCodeAt(end)) {
11
+ do {
12
+ const i = escapeRegExp.lastIndex - 1;
13
+
14
+ switch (string.charCodeAt(i)) {
9
15
  case 34: // "
10
- escaped += string.slice(start, end) + "&#34;";
11
- start = end + 1;
12
- continue;
16
+ escaped += string.slice(start, i) + "&#34;";
17
+ break;
13
18
  case 38: // &
14
- escaped += string.slice(start, end) + "&#38;";
15
- start = end + 1;
16
- continue;
19
+ escaped += string.slice(start, i) + "&#38;";
20
+ break;
17
21
  case 39: // '
18
- escaped += string.slice(start, end) + "&#39;";
19
- start = end + 1;
20
- continue;
22
+ escaped += string.slice(start, i) + "&#39;";
23
+ break;
21
24
  case 60: // <
22
- escaped += string.slice(start, end) + "&#60;";
23
- start = end + 1;
24
- continue;
25
+ escaped += string.slice(start, i) + "&#60;";
26
+ break;
25
27
  case 61: // =
26
- escaped += string.slice(start, end) + "&#61;";
27
- start = end + 1;
28
- continue;
28
+ escaped += string.slice(start, i) + "&#61;";
29
+ break;
29
30
  case 62: // >
30
- escaped += string.slice(start, end) + "&#62;";
31
- start = end + 1;
32
- continue;
31
+ escaped += string.slice(start, i) + "&#62;";
32
+ break;
33
33
  }
34
- }
35
34
 
36
- escaped += string.slice(start);
35
+ start = escapeRegExp.lastIndex;
36
+ } while (escapeRegExp.test(string));
37
37
 
38
- return escaped;
38
+ return escaped + string.slice(start);
39
39
  };
40
40
 
41
41
  /**
@@ -59,16 +59,14 @@ export const html = ({ raw: literals }, ...expressions) => {
59
59
 
60
60
  if (literal && literal.charCodeAt(literal.length - 1) === 33) {
61
61
  literal = literal.slice(0, -1);
62
- } else if (string && escapeRegExp.test(string)) {
62
+ } else {
63
63
  string = escapeFunction(string);
64
64
  }
65
65
 
66
66
  accumulator += literal + string;
67
67
  }
68
68
 
69
- accumulator += literals[expressions.length];
70
-
71
- return accumulator;
69
+ return accumulator + literals[expressions.length];
72
70
  };
73
71
 
74
72
  /**
@@ -120,11 +118,11 @@ export const htmlGenerator = function* ({ raw: literals }, ...expressions) {
120
118
  string = `${expression}`;
121
119
  }
122
120
 
123
- if (string) {
124
- if (!isRaw && escapeRegExp.test(string)) {
125
- string = escapeFunction(string);
126
- }
121
+ if (!isRaw) {
122
+ string = escapeFunction(string);
123
+ }
127
124
 
125
+ if (string) {
128
126
  yield string;
129
127
  }
130
128
  }
@@ -135,11 +133,11 @@ export const htmlGenerator = function* ({ raw: literals }, ...expressions) {
135
133
  string = `${expression}`;
136
134
  }
137
135
 
138
- if (string) {
139
- if (!isRaw && escapeRegExp.test(string)) {
140
- string = escapeFunction(string);
141
- }
136
+ if (!isRaw) {
137
+ string = escapeFunction(string);
138
+ }
142
139
 
140
+ if (string) {
143
141
  yield string;
144
142
  }
145
143
  }
@@ -152,7 +150,7 @@ export const htmlGenerator = function* ({ raw: literals }, ...expressions) {
152
150
 
153
151
  if (literal && literal.charCodeAt(literal.length - 1) === 33) {
154
152
  literal = literal.slice(0, -1);
155
- } else if (string && escapeRegExp.test(string)) {
153
+ } else {
156
154
  string = escapeFunction(string);
157
155
  }
158
156
 
@@ -221,11 +219,11 @@ export const htmlAsyncGenerator = async function* (
221
219
  string = `${expression}`;
222
220
  }
223
221
 
224
- if (string) {
225
- if (!isRaw && escapeRegExp.test(string)) {
226
- string = escapeFunction(string);
227
- }
222
+ if (!isRaw) {
223
+ string = escapeFunction(string);
224
+ }
228
225
 
226
+ if (string) {
229
227
  yield string;
230
228
  }
231
229
  }
@@ -236,11 +234,11 @@ export const htmlAsyncGenerator = async function* (
236
234
  string = `${expression}`;
237
235
  }
238
236
 
239
- if (string) {
240
- if (!isRaw && escapeRegExp.test(string)) {
241
- string = escapeFunction(string);
242
- }
237
+ if (!isRaw) {
238
+ string = escapeFunction(string);
239
+ }
243
240
 
241
+ if (string) {
244
242
  yield string;
245
243
  }
246
244
  }
@@ -253,7 +251,7 @@ export const htmlAsyncGenerator = async function* (
253
251
 
254
252
  if (literal && literal.charCodeAt(literal.length - 1) === 33) {
255
253
  literal = literal.slice(0, -1);
256
- } else if (string && escapeRegExp.test(string)) {
254
+ } else {
257
255
  string = escapeFunction(string);
258
256
  }
259
257