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
|
export type Text = {
kind: "text";
content: string;
};
export type RawHTML = {
kind: "raw";
html: string;
};
export type Element = {
kind: "element";
name: string;
attributes: Record<string, string>;
children: Node[];
};
export type Node = Element | Text | RawHTML;
export type NodeLike = Node | string | null | undefined | false | NodeLike[];
function flattenChildren(children: NodeLike[]): Node[] {
const result: Node[] = [];
for (const child of children) {
if (child === null || child === undefined || child === false) {
continue;
}
if (typeof child === "string") {
result.push(text(child));
} else if (Array.isArray(child)) {
result.push(...flattenChildren(child));
} else {
result.push(child);
}
}
return result;
}
export function text(content: string): Text {
return {
kind: "text",
content,
};
}
export function rawHTML(html: string): RawHTML {
return {
kind: "raw",
html,
};
}
export function elem(
name: string,
attributes?: Record<string, string>,
...children: NodeLike[]
): Element {
return {
kind: "element",
name,
attributes: attributes || {},
children: flattenChildren(children),
};
}
// Helper functions for commonly used elements
export const a = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("a", attributes, ...children);
export const article = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("article", attributes, ...children);
export const button = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("button", attributes, ...children);
export const div = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("div", attributes, ...children);
export const footer = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("footer", attributes, ...children);
export const h1 = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("h1", attributes, ...children);
export const h2 = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("h2", attributes, ...children);
export const h3 = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("h3", attributes, ...children);
export const h4 = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("h4", attributes, ...children);
export const h5 = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("h5", attributes, ...children);
export const h6 = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("h6", attributes, ...children);
export const header = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("header", attributes, ...children);
export const img = (attributes?: Record<string, string>) =>
elem("img", attributes);
export const li = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("li", attributes, ...children);
export const link = (attributes?: Record<string, string>) =>
elem("link", attributes);
export const meta = (attributes?: Record<string, string>) =>
elem("meta", attributes);
export const nav = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("nav", attributes, ...children);
export const ol = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("ol", attributes, ...children);
export const p = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("p", attributes, ...children);
export const script = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("script", attributes, ...children);
export const section = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("section", attributes, ...children);
export const span = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("span", attributes, ...children);
export const ul = (
attributes?: Record<string, string>,
...children: NodeLike[]
) => elem("ul", attributes, ...children);
export function addClass(e: Element, klass: string) {
const classes = e.attributes.class;
if (classes === undefined) {
e.attributes.class = klass;
} else {
const classList = classes.split(" ");
classList.push(klass);
classList.sort();
e.attributes.class = classList.join(" ");
}
}
export function findFirstChildElement(
e: Element,
name: string,
): Element | null {
for (const c of e.children) {
if (c.kind === "element" && c.name === name) {
return c;
}
}
return null;
}
export function findChildElements(e: Element, name: string): Element[] {
const cs = [];
for (const c of e.children) {
if (c.kind === "element" && c.name === name) {
cs.push(c);
}
}
return cs;
}
export function innerText(e: Element): string {
let t = "";
forEachChild(e, (c) => {
if (c.kind === "text") {
t += c.content;
}
});
return t;
}
export function forEachChild(e: Element, f: (n: Node) => void) {
for (const c of e.children) {
f(c);
}
}
export async function forEachChildAsync(
e: Element,
f: (n: Node) => Promise<void>,
): Promise<void> {
for (const c of e.children) {
await f(c);
}
}
export function forEachChildRecursively(e: Element, f: (n: Node) => void) {
const g = (c: Node) => {
f(c);
if (c.kind === "element") {
forEachChild(c, g);
}
};
forEachChild(e, g);
}
export async function forEachChildRecursivelyAsync(
e: Element,
f: (n: Node) => Promise<void>,
): Promise<void> {
const g = async (c: Node) => {
await f(c);
if (c.kind === "element") {
await forEachChildAsync(c, g);
}
};
await forEachChildAsync(e, g);
}
export function forEachElementOfType(
root: Element,
elementName: string,
f: (e: Element) => void,
) {
forEachChildRecursively(root, (n) => {
if (n.kind === "element" && n.name === elementName) {
f(n);
}
});
}
export function processTextNodesInElement(
e: Element,
f: (text: string) => Node[],
) {
const newChildren: Node[] = [];
for (const child of e.children) {
if (child.kind === "text") {
newChildren.push(...f(child.content));
} else {
newChildren.push(child);
}
}
e.children = newChildren;
}
|