ghtml 3.0.3 → 3.0.5

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.5",
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,45 @@
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;
16
+ escaped += string.slice(start, i) + "&#34;";
17
+ start = escapeRegExp.lastIndex;
12
18
  continue;
13
19
  case 38: // &
14
- escaped += string.slice(start, end) + "&#38;";
15
- start = end + 1;
20
+ escaped += string.slice(start, i) + "&#38;";
21
+ start = escapeRegExp.lastIndex;
16
22
  continue;
17
23
  case 39: // '
18
- escaped += string.slice(start, end) + "&#39;";
19
- start = end + 1;
24
+ escaped += string.slice(start, i) + "&#39;";
25
+ start = escapeRegExp.lastIndex;
20
26
  continue;
21
27
  case 60: // <
22
- escaped += string.slice(start, end) + "&#60;";
23
- start = end + 1;
28
+ escaped += string.slice(start, i) + "&#60;";
29
+ start = escapeRegExp.lastIndex;
24
30
  continue;
25
31
  case 61: // =
26
- escaped += string.slice(start, end) + "&#61;";
27
- start = end + 1;
32
+ escaped += string.slice(start, i) + "&#61;";
33
+ start = escapeRegExp.lastIndex;
28
34
  continue;
29
35
  case 62: // >
30
- escaped += string.slice(start, end) + "&#62;";
31
- start = end + 1;
36
+ escaped += string.slice(start, i) + "&#62;";
37
+ start = escapeRegExp.lastIndex;
32
38
  continue;
33
39
  }
34
- }
40
+ } while (escapeRegExp.test(string));
35
41
 
36
- escaped += string.slice(start);
37
-
38
- return escaped;
42
+ return escaped + string.slice(start);
39
43
  };
40
44
 
41
45
  /**
@@ -59,16 +63,14 @@ export const html = ({ raw: literals }, ...expressions) => {
59
63
 
60
64
  if (literal && literal.charCodeAt(literal.length - 1) === 33) {
61
65
  literal = literal.slice(0, -1);
62
- } else if (string && escapeRegExp.test(string)) {
66
+ } else {
63
67
  string = escapeFunction(string);
64
68
  }
65
69
 
66
70
  accumulator += literal + string;
67
71
  }
68
72
 
69
- accumulator += literals[expressions.length];
70
-
71
- return accumulator;
73
+ return accumulator + literals[expressions.length];
72
74
  };
73
75
 
74
76
  /**
@@ -120,11 +122,11 @@ export const htmlGenerator = function* ({ raw: literals }, ...expressions) {
120
122
  string = `${expression}`;
121
123
  }
122
124
 
123
- if (string) {
124
- if (!isRaw && escapeRegExp.test(string)) {
125
- string = escapeFunction(string);
126
- }
125
+ if (!isRaw) {
126
+ string = escapeFunction(string);
127
+ }
127
128
 
129
+ if (string) {
128
130
  yield string;
129
131
  }
130
132
  }
@@ -135,11 +137,11 @@ export const htmlGenerator = function* ({ raw: literals }, ...expressions) {
135
137
  string = `${expression}`;
136
138
  }
137
139
 
138
- if (string) {
139
- if (!isRaw && escapeRegExp.test(string)) {
140
- string = escapeFunction(string);
141
- }
140
+ if (!isRaw) {
141
+ string = escapeFunction(string);
142
+ }
142
143
 
144
+ if (string) {
143
145
  yield string;
144
146
  }
145
147
  }
@@ -152,7 +154,7 @@ export const htmlGenerator = function* ({ raw: literals }, ...expressions) {
152
154
 
153
155
  if (literal && literal.charCodeAt(literal.length - 1) === 33) {
154
156
  literal = literal.slice(0, -1);
155
- } else if (string && escapeRegExp.test(string)) {
157
+ } else {
156
158
  string = escapeFunction(string);
157
159
  }
158
160
 
@@ -221,11 +223,11 @@ export const htmlAsyncGenerator = async function* (
221
223
  string = `${expression}`;
222
224
  }
223
225
 
224
- if (string) {
225
- if (!isRaw && escapeRegExp.test(string)) {
226
- string = escapeFunction(string);
227
- }
226
+ if (!isRaw) {
227
+ string = escapeFunction(string);
228
+ }
228
229
 
230
+ if (string) {
229
231
  yield string;
230
232
  }
231
233
  }
@@ -236,11 +238,11 @@ export const htmlAsyncGenerator = async function* (
236
238
  string = `${expression}`;
237
239
  }
238
240
 
239
- if (string) {
240
- if (!isRaw && escapeRegExp.test(string)) {
241
- string = escapeFunction(string);
242
- }
241
+ if (!isRaw) {
242
+ string = escapeFunction(string);
243
+ }
243
244
 
245
+ if (string) {
244
246
  yield string;
245
247
  }
246
248
  }
@@ -253,7 +255,7 @@ export const htmlAsyncGenerator = async function* (
253
255
 
254
256
  if (literal && literal.charCodeAt(literal.length - 1) === 33) {
255
257
  literal = literal.slice(0, -1);
256
- } else if (string && escapeRegExp.test(string)) {
258
+ } else {
257
259
  string = escapeFunction(string);
258
260
  }
259
261