ghtml 1.5.1 → 1.5.2
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/package.json +1 -1
- package/src/html.js +23 -19
- package/test.js +0 -27
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": "1.5.
|
|
6
|
+
"version": "1.5.2",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"main": "./src/index.js",
|
|
9
9
|
"exports": {
|
package/src/html.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const escapeDictionary = {
|
|
2
2
|
'"': """,
|
|
3
3
|
"'": "'",
|
|
4
4
|
"&": "&",
|
|
@@ -6,10 +6,13 @@ const escapeDict = {
|
|
|
6
6
|
">": ">",
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
-
const
|
|
9
|
+
const escapeRegExp = new RegExp(
|
|
10
|
+
`[${Object.keys(escapeDictionary).join("")}]`,
|
|
11
|
+
"gu",
|
|
12
|
+
);
|
|
10
13
|
|
|
11
|
-
const
|
|
12
|
-
return
|
|
14
|
+
const escapeFunction = (key) => {
|
|
15
|
+
return escapeDictionary[key];
|
|
13
16
|
};
|
|
14
17
|
|
|
15
18
|
/**
|
|
@@ -22,20 +25,21 @@ const html = ({ raw: literals }, ...expressions) => {
|
|
|
22
25
|
let index = 0;
|
|
23
26
|
|
|
24
27
|
for (; index !== expressions.length; ++index) {
|
|
28
|
+
const expression = expressions[index];
|
|
25
29
|
let literal = literals[index];
|
|
26
30
|
let string =
|
|
27
|
-
|
|
31
|
+
expression === undefined || expression === null
|
|
28
32
|
? ""
|
|
29
|
-
: typeof
|
|
30
|
-
?
|
|
31
|
-
: Array.isArray(
|
|
32
|
-
?
|
|
33
|
-
: `${
|
|
33
|
+
: typeof expression === "string"
|
|
34
|
+
? expression
|
|
35
|
+
: Array.isArray(expression)
|
|
36
|
+
? expression.join("")
|
|
37
|
+
: `${expression}`;
|
|
34
38
|
|
|
35
39
|
if (literal.length && literal.charCodeAt(literal.length - 1) === 33) {
|
|
36
40
|
literal = literal.slice(0, -1);
|
|
37
41
|
} else if (string.length) {
|
|
38
|
-
string = string.replace(
|
|
42
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
39
43
|
}
|
|
40
44
|
|
|
41
45
|
accumulator += literal + string;
|
|
@@ -53,8 +57,8 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
53
57
|
let index = 0;
|
|
54
58
|
|
|
55
59
|
for (; index !== expressions.length; ++index) {
|
|
56
|
-
let literal = literals[index];
|
|
57
60
|
let expression = expressions[index];
|
|
61
|
+
let literal = literals[index];
|
|
58
62
|
let string;
|
|
59
63
|
|
|
60
64
|
if (expression === undefined || expression === null) {
|
|
@@ -92,7 +96,7 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
92
96
|
|
|
93
97
|
if (string.length) {
|
|
94
98
|
if (!isRaw) {
|
|
95
|
-
string = string.replace(
|
|
99
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
96
100
|
}
|
|
97
101
|
|
|
98
102
|
yield string;
|
|
@@ -107,7 +111,7 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
107
111
|
|
|
108
112
|
if (string.length) {
|
|
109
113
|
if (!isRaw) {
|
|
110
|
-
string = string.replace(
|
|
114
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
111
115
|
}
|
|
112
116
|
|
|
113
117
|
yield string;
|
|
@@ -123,7 +127,7 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
|
|
|
123
127
|
if (literal.length && literal.charCodeAt(literal.length - 1) === 33) {
|
|
124
128
|
literal = literal.slice(0, -1);
|
|
125
129
|
} else if (string.length) {
|
|
126
|
-
string = string.replace(
|
|
130
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
127
131
|
}
|
|
128
132
|
|
|
129
133
|
if (literal.length || string.length) {
|
|
@@ -145,8 +149,8 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
|
|
|
145
149
|
let index = 0;
|
|
146
150
|
|
|
147
151
|
for (; index !== expressions.length; ++index) {
|
|
148
|
-
let literal = literals[index];
|
|
149
152
|
let expression = await expressions[index];
|
|
153
|
+
let literal = literals[index];
|
|
150
154
|
let string;
|
|
151
155
|
|
|
152
156
|
if (expression === undefined || expression === null) {
|
|
@@ -187,7 +191,7 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
|
|
|
187
191
|
|
|
188
192
|
if (string.length) {
|
|
189
193
|
if (!isRaw) {
|
|
190
|
-
string = string.replace(
|
|
194
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
191
195
|
}
|
|
192
196
|
|
|
193
197
|
yield string;
|
|
@@ -202,7 +206,7 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
|
|
|
202
206
|
|
|
203
207
|
if (string.length) {
|
|
204
208
|
if (!isRaw) {
|
|
205
|
-
string = string.replace(
|
|
209
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
206
210
|
}
|
|
207
211
|
|
|
208
212
|
yield string;
|
|
@@ -218,7 +222,7 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
|
|
|
218
222
|
if (literal.length && literal.charCodeAt(literal.length - 1) === 33) {
|
|
219
223
|
literal = literal.slice(0, -1);
|
|
220
224
|
} else if (string.length) {
|
|
221
|
-
string = string.replace(
|
|
225
|
+
string = string.replace(escapeRegExp, escapeFunction);
|
|
222
226
|
}
|
|
223
227
|
|
|
224
228
|
if (literal.length || string.length) {
|
package/test.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { htmlAsyncGenerator as html } from "ghtml";
|
|
2
|
-
import { createReadStream } from "node:fs";
|
|
3
|
-
import { readFile } from "node:fs/promises";
|
|
4
|
-
import { Readable } from "node:stream";
|
|
5
|
-
import http from "node:http";
|
|
6
|
-
|
|
7
|
-
const asyncGenerator = async function* () {
|
|
8
|
-
const Hello = await new Promise((resolve) => {
|
|
9
|
-
setTimeout(() => {
|
|
10
|
-
resolve("Hello");
|
|
11
|
-
}, 1000);
|
|
12
|
-
});
|
|
13
|
-
yield `${Hello}!`;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
http
|
|
17
|
-
.createServer((req, res) => {
|
|
18
|
-
const htmlContent = html`<html>
|
|
19
|
-
<p>${asyncGenerator()}</p>
|
|
20
|
-
<code>${readFile("./README.md", "utf8")}</code>
|
|
21
|
-
<code>${createReadStream("./README.md", "utf8")}</code>
|
|
22
|
-
</html>`;
|
|
23
|
-
const readableStream = Readable.from(htmlContent);
|
|
24
|
-
res.writeHead(200, { "Content-Type": "text/html;charset=utf-8" });
|
|
25
|
-
readableStream.pipe(res);
|
|
26
|
-
})
|
|
27
|
-
.listen(3000);
|