ghtml 3.0.2 → 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 +3 -3
- package/src/html.d.ts +3 -24
- package/src/html.js +58 -57
- package/src/includeFile.d.ts +0 -4
- package/src/includeFile.js +2 -4
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",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@fastify/pre-commit": "^2.1.0",
|
|
29
29
|
"c8": "^10.1.2",
|
|
30
|
-
"grules": "^0.23.
|
|
31
|
-
"tinybench": "^2.
|
|
30
|
+
"grules": "^0.23.3",
|
|
31
|
+
"tinybench": "^2.9.0",
|
|
32
32
|
"typescript": ">=5.5.4"
|
|
33
33
|
},
|
|
34
34
|
"repository": {
|
package/src/html.d.ts
CHANGED
|
@@ -1,24 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @returns {string} The HTML string.
|
|
5
|
-
*/
|
|
6
|
-
export function html({ raw: literals }: {
|
|
7
|
-
raw: Readonly<string[]>;
|
|
8
|
-
}, ...expressions: any[]): string;
|
|
9
|
-
/**
|
|
10
|
-
* @param {{ raw: Readonly<string[]> }} literals Tagged template literals.
|
|
11
|
-
* @param {...any} expressions Expressions to interpolate.
|
|
12
|
-
* @yields {string} The HTML strings.
|
|
13
|
-
*/
|
|
14
|
-
export function htmlGenerator({ raw: literals }: {
|
|
15
|
-
raw: Readonly<string[]>;
|
|
16
|
-
}, ...expressions: any[]): {};
|
|
17
|
-
/**
|
|
18
|
-
* @param {{ raw: Readonly<string[]> }} literals Tagged template literals.
|
|
19
|
-
* @param {...any} expressions Expressions to interpolate.
|
|
20
|
-
* @yields {string} The HTML strings.
|
|
21
|
-
*/
|
|
22
|
-
export function htmlAsyncGenerator({ raw: literals }: {
|
|
23
|
-
raw: Readonly<string[]>;
|
|
24
|
-
}, ...expressions: any[]): {};
|
|
1
|
+
export function html({ raw: literals }: TemplateStringsArray, ...expressions: any[]): string;
|
|
2
|
+
export function htmlGenerator({ raw: literals }: TemplateStringsArray, ...expressions: any[]): Generator<string, void, void>;
|
|
3
|
+
export function htmlAsyncGenerator({ raw: literals }: TemplateStringsArray, ...expressions: any[]): AsyncGenerator<string, void, void>;
|
package/src/html.js
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
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
|
/**
|
|
42
|
-
* @param {
|
|
42
|
+
* @param {TemplateStringsArray} literals Tagged template literals.
|
|
43
43
|
* @param {...any} expressions Expressions to interpolate.
|
|
44
|
-
* @returns {string} The HTML string.
|
|
44
|
+
* @returns {string} The processed HTML string.
|
|
45
45
|
*/
|
|
46
|
-
const html = ({ raw: literals }, ...expressions) => {
|
|
46
|
+
export const html = ({ raw: literals }, ...expressions) => {
|
|
47
47
|
let accumulator = "";
|
|
48
48
|
|
|
49
49
|
for (let i = 0; i !== expressions.length; ++i) {
|
|
@@ -59,24 +59,23 @@ 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
|
/**
|
|
75
|
-
* @param {
|
|
73
|
+
* @param {TemplateStringsArray} literals Tagged template literals.
|
|
76
74
|
* @param {...any} expressions Expressions to interpolate.
|
|
77
|
-
* @yields
|
|
75
|
+
* @yields Processed HTML strings.
|
|
76
|
+
* @returns {Generator<string, void, void>} The HTML generator.
|
|
78
77
|
*/
|
|
79
|
-
const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
78
|
+
export const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
80
79
|
for (let i = 0; i !== expressions.length; ++i) {
|
|
81
80
|
let expression = expressions[i];
|
|
82
81
|
let literal = literals[i];
|
|
@@ -119,11 +118,11 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
119
118
|
string = `${expression}`;
|
|
120
119
|
}
|
|
121
120
|
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
121
|
+
if (!isRaw) {
|
|
122
|
+
string = escapeFunction(string);
|
|
123
|
+
}
|
|
126
124
|
|
|
125
|
+
if (string) {
|
|
127
126
|
yield string;
|
|
128
127
|
}
|
|
129
128
|
}
|
|
@@ -134,11 +133,11 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
134
133
|
string = `${expression}`;
|
|
135
134
|
}
|
|
136
135
|
|
|
137
|
-
if (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
136
|
+
if (!isRaw) {
|
|
137
|
+
string = escapeFunction(string);
|
|
138
|
+
}
|
|
141
139
|
|
|
140
|
+
if (string) {
|
|
142
141
|
yield string;
|
|
143
142
|
}
|
|
144
143
|
}
|
|
@@ -151,7 +150,7 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
151
150
|
|
|
152
151
|
if (literal && literal.charCodeAt(literal.length - 1) === 33) {
|
|
153
152
|
literal = literal.slice(0, -1);
|
|
154
|
-
} else
|
|
153
|
+
} else {
|
|
155
154
|
string = escapeFunction(string);
|
|
156
155
|
}
|
|
157
156
|
|
|
@@ -166,11 +165,15 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
166
165
|
};
|
|
167
166
|
|
|
168
167
|
/**
|
|
169
|
-
* @param {
|
|
168
|
+
* @param {TemplateStringsArray} literals Tagged template literals.
|
|
170
169
|
* @param {...any} expressions Expressions to interpolate.
|
|
171
|
-
* @yields
|
|
170
|
+
* @yields Processed HTML strings.
|
|
171
|
+
* @returns {AsyncGenerator<string, void, void>} The HTML generator.
|
|
172
172
|
*/
|
|
173
|
-
const htmlAsyncGenerator = async function* (
|
|
173
|
+
export const htmlAsyncGenerator = async function* (
|
|
174
|
+
{ raw: literals },
|
|
175
|
+
...expressions
|
|
176
|
+
) {
|
|
174
177
|
for (let i = 0; i !== expressions.length; ++i) {
|
|
175
178
|
let expression = await expressions[i];
|
|
176
179
|
let literal = literals[i];
|
|
@@ -216,11 +219,11 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
|
|
|
216
219
|
string = `${expression}`;
|
|
217
220
|
}
|
|
218
221
|
|
|
219
|
-
if (
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
222
|
+
if (!isRaw) {
|
|
223
|
+
string = escapeFunction(string);
|
|
224
|
+
}
|
|
223
225
|
|
|
226
|
+
if (string) {
|
|
224
227
|
yield string;
|
|
225
228
|
}
|
|
226
229
|
}
|
|
@@ -231,11 +234,11 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
|
|
|
231
234
|
string = `${expression}`;
|
|
232
235
|
}
|
|
233
236
|
|
|
234
|
-
if (
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}
|
|
237
|
+
if (!isRaw) {
|
|
238
|
+
string = escapeFunction(string);
|
|
239
|
+
}
|
|
238
240
|
|
|
241
|
+
if (string) {
|
|
239
242
|
yield string;
|
|
240
243
|
}
|
|
241
244
|
}
|
|
@@ -248,7 +251,7 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
|
|
|
248
251
|
|
|
249
252
|
if (literal && literal.charCodeAt(literal.length - 1) === 33) {
|
|
250
253
|
literal = literal.slice(0, -1);
|
|
251
|
-
} else
|
|
254
|
+
} else {
|
|
252
255
|
string = escapeFunction(string);
|
|
253
256
|
}
|
|
254
257
|
|
|
@@ -261,5 +264,3 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
|
|
|
261
264
|
yield literals[expressions.length];
|
|
262
265
|
}
|
|
263
266
|
};
|
|
264
|
-
|
|
265
|
-
export { html, htmlGenerator, htmlAsyncGenerator };
|
package/src/includeFile.d.ts
CHANGED
package/src/includeFile.js
CHANGED
|
@@ -4,9 +4,9 @@ const cache = new Map();
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @param {string} path The path to the file to render.
|
|
7
|
-
* @returns {string} The
|
|
7
|
+
* @returns {string} The content of the file.
|
|
8
8
|
*/
|
|
9
|
-
const includeFile = (path) => {
|
|
9
|
+
export const includeFile = (path) => {
|
|
10
10
|
let file = cache.get(path);
|
|
11
11
|
|
|
12
12
|
if (file === undefined) {
|
|
@@ -16,5 +16,3 @@ const includeFile = (path) => {
|
|
|
16
16
|
|
|
17
17
|
return file;
|
|
18
18
|
};
|
|
19
|
-
|
|
20
|
-
export { includeFile };
|