ghtml 3.0.6 → 3.0.7
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/bin/src/utils.js +1 -3
- package/package.json +1 -1
- package/src/html.d.ts +3 -3
- package/src/html.js +35 -39
package/bin/src/utils.js
CHANGED
|
@@ -74,7 +74,7 @@ const updateFilePathsWithHashes = async (
|
|
|
74
74
|
}
|
|
75
75
|
};
|
|
76
76
|
|
|
77
|
-
const generateHashesAndReplace = async ({
|
|
77
|
+
export const generateHashesAndReplace = async ({
|
|
78
78
|
roots,
|
|
79
79
|
refs,
|
|
80
80
|
prefix,
|
|
@@ -124,5 +124,3 @@ const generateHashesAndReplace = async ({
|
|
|
124
124
|
skipPatterns,
|
|
125
125
|
);
|
|
126
126
|
};
|
|
127
|
-
|
|
128
|
-
export { generateHashesAndReplace };
|
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.7",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"bin": "./bin/src/index.js",
|
|
9
9
|
"main": "./src/index.js",
|
package/src/html.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export function html(
|
|
2
|
-
export function htmlGenerator(
|
|
3
|
-
export function htmlAsyncGenerator(
|
|
1
|
+
export function html(literals: TemplateStringsArray, ...expressions: any[]): string;
|
|
2
|
+
export function htmlGenerator(literals: TemplateStringsArray, ...expressions: any[]): Generator<string, void, void>;
|
|
3
|
+
export function htmlAsyncGenerator(literals: TemplateStringsArray, ...expressions: any[]): AsyncGenerator<string, void, void>;
|
package/src/html.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
const escapeRegExp = /["&'<=>]/g;
|
|
2
2
|
|
|
3
3
|
const escapeFunction = (string) => {
|
|
4
|
-
if (!string || !escapeRegExp.test(string)) {
|
|
5
|
-
return string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
4
|
let escaped = "";
|
|
9
5
|
let start = 0;
|
|
10
6
|
|
|
11
|
-
|
|
7
|
+
while (escapeRegExp.test(string)) {
|
|
12
8
|
const i = escapeRegExp.lastIndex - 1;
|
|
13
9
|
|
|
14
10
|
switch (string.charCodeAt(i)) {
|
|
@@ -33,21 +29,22 @@ const escapeFunction = (string) => {
|
|
|
33
29
|
}
|
|
34
30
|
|
|
35
31
|
start = escapeRegExp.lastIndex;
|
|
36
|
-
}
|
|
32
|
+
}
|
|
37
33
|
|
|
38
34
|
return escaped + string.slice(start);
|
|
39
35
|
};
|
|
40
36
|
|
|
41
37
|
/**
|
|
38
|
+
* The `html` function is designed to tag template literals and automatically escape their expressions.
|
|
42
39
|
* @param {TemplateStringsArray} literals Tagged template literals.
|
|
43
40
|
* @param {...any} expressions Expressions to interpolate.
|
|
44
41
|
* @returns {string} The processed HTML string.
|
|
45
42
|
*/
|
|
46
|
-
export const html = (
|
|
43
|
+
export const html = (literals, ...expressions) => {
|
|
47
44
|
let accumulator = "";
|
|
48
45
|
|
|
49
46
|
for (let i = 0; i !== expressions.length; ++i) {
|
|
50
|
-
let literal = literals[i];
|
|
47
|
+
let literal = literals.raw[i];
|
|
51
48
|
let string =
|
|
52
49
|
typeof expressions[i] === "string"
|
|
53
50
|
? expressions[i]
|
|
@@ -60,25 +57,26 @@ export const html = ({ raw: literals }, ...expressions) => {
|
|
|
60
57
|
if (literal && literal.charCodeAt(literal.length - 1) === 33) {
|
|
61
58
|
literal = literal.slice(0, -1);
|
|
62
59
|
} else {
|
|
63
|
-
string
|
|
60
|
+
string &&= escapeFunction(string);
|
|
64
61
|
}
|
|
65
62
|
|
|
66
63
|
accumulator += literal + string;
|
|
67
64
|
}
|
|
68
65
|
|
|
69
|
-
return accumulator + literals[expressions.length];
|
|
66
|
+
return accumulator + literals.raw[expressions.length];
|
|
70
67
|
};
|
|
71
68
|
|
|
72
69
|
/**
|
|
70
|
+
* The `htmlGenerator` function acts as the generator version of the `html` function.
|
|
73
71
|
* @param {TemplateStringsArray} literals Tagged template literals.
|
|
74
72
|
* @param {...any} expressions Expressions to interpolate.
|
|
75
73
|
* @yields Processed HTML strings.
|
|
76
74
|
* @returns {Generator<string, void, void>} The HTML generator.
|
|
77
75
|
*/
|
|
78
|
-
export const htmlGenerator = function* (
|
|
76
|
+
export const htmlGenerator = function* (literals, ...expressions) {
|
|
79
77
|
for (let i = 0; i !== expressions.length; ++i) {
|
|
80
78
|
let expression = expressions[i];
|
|
81
|
-
let literal = literals[i];
|
|
79
|
+
let literal = literals.raw[i];
|
|
82
80
|
let string;
|
|
83
81
|
|
|
84
82
|
if (typeof expression === "string") {
|
|
@@ -118,11 +116,11 @@ export const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
118
116
|
string = `${expression}`;
|
|
119
117
|
}
|
|
120
118
|
|
|
121
|
-
if (!isRaw) {
|
|
122
|
-
string = escapeFunction(string);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
119
|
if (string) {
|
|
120
|
+
if (!isRaw) {
|
|
121
|
+
string = escapeFunction(string);
|
|
122
|
+
}
|
|
123
|
+
|
|
126
124
|
yield string;
|
|
127
125
|
}
|
|
128
126
|
}
|
|
@@ -133,11 +131,11 @@ export const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
133
131
|
string = `${expression}`;
|
|
134
132
|
}
|
|
135
133
|
|
|
136
|
-
if (!isRaw) {
|
|
137
|
-
string = escapeFunction(string);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
134
|
if (string) {
|
|
135
|
+
if (!isRaw) {
|
|
136
|
+
string = escapeFunction(string);
|
|
137
|
+
}
|
|
138
|
+
|
|
141
139
|
yield string;
|
|
142
140
|
}
|
|
143
141
|
}
|
|
@@ -151,7 +149,7 @@ export const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
151
149
|
if (literal && literal.charCodeAt(literal.length - 1) === 33) {
|
|
152
150
|
literal = literal.slice(0, -1);
|
|
153
151
|
} else {
|
|
154
|
-
string
|
|
152
|
+
string &&= escapeFunction(string);
|
|
155
153
|
}
|
|
156
154
|
|
|
157
155
|
if (literal || string) {
|
|
@@ -159,24 +157,22 @@ export const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
159
157
|
}
|
|
160
158
|
}
|
|
161
159
|
|
|
162
|
-
if (literals[expressions.length]) {
|
|
163
|
-
yield literals[expressions.length];
|
|
160
|
+
if (literals.raw[expressions.length]) {
|
|
161
|
+
yield literals.raw[expressions.length];
|
|
164
162
|
}
|
|
165
163
|
};
|
|
166
164
|
|
|
167
165
|
/**
|
|
166
|
+
* This version of HTML generator should be preferred for asynchronous and streaming use cases.
|
|
168
167
|
* @param {TemplateStringsArray} literals Tagged template literals.
|
|
169
168
|
* @param {...any} expressions Expressions to interpolate.
|
|
170
169
|
* @yields Processed HTML strings.
|
|
171
170
|
* @returns {AsyncGenerator<string, void, void>} The HTML generator.
|
|
172
171
|
*/
|
|
173
|
-
export const htmlAsyncGenerator = async function* (
|
|
174
|
-
{ raw: literals },
|
|
175
|
-
...expressions
|
|
176
|
-
) {
|
|
172
|
+
export const htmlAsyncGenerator = async function* (literals, ...expressions) {
|
|
177
173
|
for (let i = 0; i !== expressions.length; ++i) {
|
|
178
174
|
let expression = await expressions[i];
|
|
179
|
-
let literal = literals[i];
|
|
175
|
+
let literal = literals.raw[i];
|
|
180
176
|
let string;
|
|
181
177
|
|
|
182
178
|
if (typeof expression === "string") {
|
|
@@ -219,11 +215,11 @@ export const htmlAsyncGenerator = async function* (
|
|
|
219
215
|
string = `${expression}`;
|
|
220
216
|
}
|
|
221
217
|
|
|
222
|
-
if (!isRaw) {
|
|
223
|
-
string = escapeFunction(string);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
218
|
if (string) {
|
|
219
|
+
if (!isRaw) {
|
|
220
|
+
string = escapeFunction(string);
|
|
221
|
+
}
|
|
222
|
+
|
|
227
223
|
yield string;
|
|
228
224
|
}
|
|
229
225
|
}
|
|
@@ -234,11 +230,11 @@ export const htmlAsyncGenerator = async function* (
|
|
|
234
230
|
string = `${expression}`;
|
|
235
231
|
}
|
|
236
232
|
|
|
237
|
-
if (!isRaw) {
|
|
238
|
-
string = escapeFunction(string);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
233
|
if (string) {
|
|
234
|
+
if (!isRaw) {
|
|
235
|
+
string = escapeFunction(string);
|
|
236
|
+
}
|
|
237
|
+
|
|
242
238
|
yield string;
|
|
243
239
|
}
|
|
244
240
|
}
|
|
@@ -252,7 +248,7 @@ export const htmlAsyncGenerator = async function* (
|
|
|
252
248
|
if (literal && literal.charCodeAt(literal.length - 1) === 33) {
|
|
253
249
|
literal = literal.slice(0, -1);
|
|
254
250
|
} else {
|
|
255
|
-
string
|
|
251
|
+
string &&= escapeFunction(string);
|
|
256
252
|
}
|
|
257
253
|
|
|
258
254
|
if (literal || string) {
|
|
@@ -260,7 +256,7 @@ export const htmlAsyncGenerator = async function* (
|
|
|
260
256
|
}
|
|
261
257
|
}
|
|
262
258
|
|
|
263
|
-
if (literals[expressions.length]) {
|
|
264
|
-
yield literals[expressions.length];
|
|
259
|
+
if (literals.raw[expressions.length]) {
|
|
260
|
+
yield literals.raw[expressions.length];
|
|
265
261
|
}
|
|
266
262
|
};
|