-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.ts
More file actions
398 lines (370 loc) · 9.35 KB
/
html.ts
File metadata and controls
398 lines (370 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
* This module provides a simple way to generate html source code.
*/
/** Html represent a tree of html tags */
export type HtmlNode =
| HtmlTag
| HtmlNode[]
| string
| null
| undefined
| ToHtml
| RawHtml;
type Attrs = { [attr: string]: AttrValue };
type AttrValue = string | number | boolean | null | undefined;
type HtmlTag = {
tag: string;
attrs?: Attrs;
children: HtmlNode;
};
const voidElements =
"area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr"
.split(", ");
const rawTextElements = "script, style, title, textarea".split(", ");
function isHtmlNode(value: HtmlNode | Attrs): value is HtmlNode {
return (
value instanceof Tag ||
value instanceof RawHtml ||
Array.isArray(value) ||
typeof value === "string" ||
value === null ||
value === undefined
);
}
/**
* Escape text into html source
* @param text - the text to escape
* @returns the escaped text
*/
export function escapeHtml(text: string): string {
return text.replaceAll(/[<>&"]/g, (c: string) => {
switch (c) {
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
case '"':
return """;
default:
throw new Error("unreachable");
}
});
}
function attr([key, value]: [string, AttrValue]): string[] {
if (value === false || value === null || value === undefined) {
return [];
}
if (value === true) {
return [" ", key];
}
return [" ", key, '="', escapeHtml(`${value}`), '"'];
}
function emptyChildren(children: HtmlNode): boolean {
return children === undefined ||
children === null ||
children === "" ||
(Array.isArray(children) && children.length === 0);
}
class Tag {
constructor(
public tag: string,
public attrs: Attrs = {},
public children: HtmlNode = [],
) {}
}
/*
* XML tag names are case-sensitive, while HTML tag names are case-insensitive.
* In XML, colons (:) are allowed for namespaces, but they are rarely used in HTML.
*/
function validTagName(name: string): boolean {
return /^[a-zA-Z_:][a-zA-Z0-9:_.-]*$/i.test(name);
}
function validAttrName(name: string): boolean {
return /^[a-zA-Z_:][a-zA-Z0-9:_.-]*$/i.test(name);
}
// Only allow primitive values for attributes
// Ignore the structural type `toString` and `valueOf` interfaces
function parseAttrValue(value: AttrValue): AttrValue {
if (
typeof value === "string" ||
typeof value === "number" ||
typeof value === "boolean" ||
value === null ||
value === undefined
) {
return value;
}
throw new Error(`Invalid attribute value: ${value}`);
}
function parseAttrs(attrs: HtmlNode | Attrs): Attrs {
if (isHtmlNode(attrs)) {
throw new Error("unreachable");
}
const parsed: Attrs = {};
for (const [key, value] of Object.entries(attrs)) {
if (!validAttrName(key)) {
throw new Error(`Invalid attribute name: ${key}`);
}
parsed[key] = parseAttrValue(value);
}
return parsed;
}
/**
* Create an html tag
* @param tag - the tag name
* @param children - the children
* @returns the html tag
*/
export function tag(tag: string, children?: HtmlNode): HtmlTag;
/**
* Create an html tag
* @param tag - the tag name
* @param attrs - the attributes
* @param children - the children
* @returns the html tag
*/
export function tag(
tag: string,
attrs?: Attrs,
...children: HtmlNode[]
): HtmlTag;
export function tag(
tag: string,
attrs?: HtmlNode | Attrs,
...children: HtmlNode[]
): HtmlTag {
if (!validTagName(tag)) {
throw new Error(`Invalid tag name: ${tag}`);
}
if (children.length > 0) {
return new Tag(tag, parseAttrs(attrs), children);
}
if (isHtmlNode(attrs)) {
const children = attrs;
return new Tag(tag, {}, children);
}
return new Tag(tag, parseAttrs(attrs), []);
}
class RawHtml {
constructor(public html: string) {}
}
/**
* Create a raw html node
* @param html raw html to insert
* @returns
*/
export function raw(html: string): HtmlNode {
return new RawHtml(html);
}
// https://html.spec.whatwg.org/multipage/syntax.html#comments
function isValidComment(text: string): boolean {
return !text.startsWith(">") &&
!text.startsWith("->") &&
!text.includes("<!--") &&
!text.includes("-->") &&
!text.includes("--!>");
}
/**
* Create a comment node
* @param text text of the comment
* @returns
*/
export function comment(text: string): HtmlNode {
if (!isValidComment(text)) {
throw new Error(`Invalid comment text: ${text}`);
}
return raw(`<!-- ${text} -->`);
}
/** Options for the `html` function */
interface HtmlOptions {
/** indentation level (defaults to 0) */
indentLevel?: number;
/** raw text indicator (defaults to false) */
rawText?: boolean;
/** text used for indent (defaults to " ") */
indentText?: string;
/** insert new line at the end of each html item (defaults to true) */
insertNewLines?: boolean;
/** doctype declaration */
doctype?: string;
}
/**
* InnerHtml builds an array of strings that can be joined more efficiently
* than direct string concatenation.
*/
function innerHtml(
content: HtmlNode,
options?: HtmlOptions,
): string[] {
const indentLevel = options?.indentLevel ?? 0;
const rawText = options?.rawText ?? false;
const indentText = options?.indentText ?? " ";
const insertNewLines = options?.insertNewLines ?? true;
const nl = insertNewLines ? "\n" : "";
const indent = indentText.repeat(indentLevel);
if (content === undefined || content === null) {
return [];
}
if (typeof content === "string") {
if (rawText || rawTextElements.includes(content)) {
return [indent, content, nl];
}
return [indent, escapeHtml(content), nl];
}
if (Array.isArray(content)) {
return content.map((content) => innerHtml(content, options)).flat();
}
if (content instanceof RawHtml) {
return [indent, content.html, nl];
}
if (content instanceof Tag) {
const { tag, attrs, children } = content;
const quotedAttrs = Object.entries(attrs)
.flatMap(
([key, value]) => attr([key, value]),
);
const prefix = [indent, "<", tag, ...quotedAttrs];
const endTag = ["</", tag, ">"];
if (emptyChildren(children)) {
if (voidElements.includes(tag)) {
return [...prefix, ">", nl];
}
return [...prefix, ">", ...endTag, nl];
}
const childrenStr = innerHtml(children, {
indentLevel: indentLevel + 1,
rawText: rawTextElements.includes(tag),
indentText,
insertNewLines,
});
return [...prefix, ">", nl, ...childrenStr, indent, ...endTag, nl];
}
if (isToHtml(content)) {
return innerHtml(content.toHtml(), options);
}
throw new Error("Cannot convert object to HTML");
}
/**
* Convert a html tree into an html source
*
* Features:
* - void elements are not closed
* - boolean attributes are ommited if false
* - raw text elements are not escaped
* - empty nodes are ignored
*
* # Example
*
* filename: `example.ts`
* ```ts
* import { html, type HtmlNode, tag } from "@sander/html";
* import { assertEquals } from "@std/assert";
*
* const title = "Cool Projects";
*
* interface Project {
* title: string;
* url: string;
* }
*
* const projects: Project[] = [{
* title: "Deno",
* url: "https://deno.com/",
* }, {
* title: "TypeScript",
* url: "https://www.typescriptlang.org/",
* }];
*
* const cssRules = [
* "* { --ts-blue: #3178c6; }",
* "body { font-family: sans-serif; line-height: 1.6; }",
* "li > a { color: var(--ts-blue); text-decoration: none; }",
* ];
*
* function list(items: HtmlNode[]) {
* return tag("ul", items.map((item) => tag("li", item)));
* }
*
* function link({ title, url }: Project) {
* return tag("a", { href: url }, title);
* }
*
* const result = html(
* tag("html", { lang: "en" }, [
* tag("head", [
* tag("meta", { charset: "utf-8" }),
* tag("title", title),
* tag("style", cssRules),
* ]),
* tag("body", [
* tag("h1", title),
* list(projects.map(link)),
* ]),
* ]),
* {
* doctype: "html",
* },
* );
*
* assertEquals(
* result,
* `\
* <!doctype html>
* <html lang="en">
* <head>
* <meta charset="utf-8">
* <title>
* Cool Projects
* </title>
* <style>
* * { --ts-blue: #3178c6; }
* body { font-family: sans-serif; line-height: 1.6; }
* li > a { color: var(--ts-blue); text-decoration: none; }
* </style>
* </head>
* <body>
* <h1>
* Cool Projects
* </h1>
* <ul>
* <li>
* <a href="https://deno.com/">
* Deno
* </a>
* </li>
* <li>
* <a href="https://www.typescriptlang.org/">
* TypeScript
* </a>
* </li>
* </ul>
* </body>
* </html>
* `,
* );
* ```
*
* @param content - the html tree
* @param options - the options for the html source
* @returns the html source
*/
export function html(
content: HtmlNode,
options?: HtmlOptions,
): string {
const firstLine = options?.doctype ? `<!doctype ${options.doctype}>\n` : "";
return firstLine + innerHtml(content, options).join("");
}
/**
* Interface for objects that can be converted to html
*/
export interface ToHtml {
toHtml(): HtmlNode;
}
function isToHtml(value: unknown): value is ToHtml {
return value !== undefined &&
typeof value === "object" &&
typeof (value as ToHtml).toHtml === "function";
}