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 +4 -0
- package/package.json +1 -1
- package/src/html.js +43 -41
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.
|
|
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
|
-
|
|
8
|
-
|
|
11
|
+
do {
|
|
12
|
+
const i = escapeRegExp.lastIndex - 1;
|
|
13
|
+
|
|
14
|
+
switch (string.charCodeAt(i)) {
|
|
9
15
|
case 34: // "
|
|
10
|
-
escaped += string.slice(start,
|
|
11
|
-
start =
|
|
16
|
+
escaped += string.slice(start, i) + """;
|
|
17
|
+
start = escapeRegExp.lastIndex;
|
|
12
18
|
continue;
|
|
13
19
|
case 38: // &
|
|
14
|
-
escaped += string.slice(start,
|
|
15
|
-
start =
|
|
20
|
+
escaped += string.slice(start, i) + "&";
|
|
21
|
+
start = escapeRegExp.lastIndex;
|
|
16
22
|
continue;
|
|
17
23
|
case 39: // '
|
|
18
|
-
escaped += string.slice(start,
|
|
19
|
-
start =
|
|
24
|
+
escaped += string.slice(start, i) + "'";
|
|
25
|
+
start = escapeRegExp.lastIndex;
|
|
20
26
|
continue;
|
|
21
27
|
case 60: // <
|
|
22
|
-
escaped += string.slice(start,
|
|
23
|
-
start =
|
|
28
|
+
escaped += string.slice(start, i) + "<";
|
|
29
|
+
start = escapeRegExp.lastIndex;
|
|
24
30
|
continue;
|
|
25
31
|
case 61: // =
|
|
26
|
-
escaped += string.slice(start,
|
|
27
|
-
start =
|
|
32
|
+
escaped += string.slice(start, i) + "=";
|
|
33
|
+
start = escapeRegExp.lastIndex;
|
|
28
34
|
continue;
|
|
29
35
|
case 62: // >
|
|
30
|
-
escaped += string.slice(start,
|
|
31
|
-
start =
|
|
36
|
+
escaped += string.slice(start, i) + ">";
|
|
37
|
+
start = escapeRegExp.lastIndex;
|
|
32
38
|
continue;
|
|
33
39
|
}
|
|
34
|
-
}
|
|
40
|
+
} while (escapeRegExp.test(string));
|
|
35
41
|
|
|
36
|
-
escaped
|
|
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
|
|
66
|
+
} else {
|
|
63
67
|
string = escapeFunction(string);
|
|
64
68
|
}
|
|
65
69
|
|
|
66
70
|
accumulator += literal + string;
|
|
67
71
|
}
|
|
68
72
|
|
|
69
|
-
accumulator
|
|
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 (
|
|
124
|
-
|
|
125
|
-
|
|
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 (
|
|
139
|
-
|
|
140
|
-
|
|
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
|
|
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 (
|
|
225
|
-
|
|
226
|
-
|
|
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 (
|
|
240
|
-
|
|
241
|
-
|
|
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
|
|
258
|
+
} else {
|
|
257
259
|
string = escapeFunction(string);
|
|
258
260
|
}
|
|
259
261
|
|