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 +4 -0
- package/package.json +1 -1
- package/src/html.js +44 -46
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.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
|
-
|
|
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
|
-
|
|
12
|
-
continue;
|
|
16
|
+
escaped += string.slice(start, i) + """;
|
|
17
|
+
break;
|
|
13
18
|
case 38: // &
|
|
14
|
-
escaped += string.slice(start,
|
|
15
|
-
|
|
16
|
-
continue;
|
|
19
|
+
escaped += string.slice(start, i) + "&";
|
|
20
|
+
break;
|
|
17
21
|
case 39: // '
|
|
18
|
-
escaped += string.slice(start,
|
|
19
|
-
|
|
20
|
-
continue;
|
|
22
|
+
escaped += string.slice(start, i) + "'";
|
|
23
|
+
break;
|
|
21
24
|
case 60: // <
|
|
22
|
-
escaped += string.slice(start,
|
|
23
|
-
|
|
24
|
-
continue;
|
|
25
|
+
escaped += string.slice(start, i) + "<";
|
|
26
|
+
break;
|
|
25
27
|
case 61: // =
|
|
26
|
-
escaped += string.slice(start,
|
|
27
|
-
|
|
28
|
-
continue;
|
|
28
|
+
escaped += string.slice(start, i) + "=";
|
|
29
|
+
break;
|
|
29
30
|
case 62: // >
|
|
30
|
-
escaped += string.slice(start,
|
|
31
|
-
|
|
32
|
-
continue;
|
|
31
|
+
escaped += string.slice(start, i) + ">";
|
|
32
|
+
break;
|
|
33
33
|
}
|
|
34
|
-
}
|
|
35
34
|
|
|
36
|
-
|
|
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
|
|
62
|
+
} else {
|
|
63
63
|
string = escapeFunction(string);
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
accumulator += literal + string;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
accumulator
|
|
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 (
|
|
124
|
-
|
|
125
|
-
|
|
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 (
|
|
139
|
-
|
|
140
|
-
|
|
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
|
|
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 (
|
|
225
|
-
|
|
226
|
-
|
|
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 (
|
|
240
|
-
|
|
241
|
-
|
|
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
|
|
254
|
+
} else {
|
|
257
255
|
string = escapeFunction(string);
|
|
258
256
|
}
|
|
259
257
|
|