-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhtml.ts
129 lines (109 loc) · 2.61 KB
/
html.ts
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
import { concat, flat, map, pipe, reduce, zip } from '@fxts/core';
import { escapeHtml } from './helper';
const joinTT = <T>(strings: TemplateStringsArray, values: T[], f: (value: T) => string) =>
pipe(
zip(
strings,
concat(
map((v) => f(v), values),
[''],
),
),
flat,
reduce((a, b) => a + b),
);
function upper(strings, ...values: string[]) {
return joinTT(strings, values, (v) => v.toUpperCase());
}
// function* concat(...arrs) {
// for (const arr of arrs) {
// yield* arr;
// }
// }
class Tmpl {
constructor(
private strings: TemplateStringsArray,
private values: unknown[],
) {}
private _merge = (value: unknown) =>
Array.isArray(value) ? value.reduce((a, b) => html`${a}${b}`) : value;
private _escapeHtml = (value: unknown) =>
value instanceof Tmpl ? value.toHtml() : escapeHtml(value);
toHtml() {
return joinTT(this.strings, this.values, (v) => this._escapeHtml(this._merge(v)));
}
}
const html = (strings: TemplateStringsArray, ...values: unknown[]) => new Tmpl(strings, values);
abstract class View<T> {
constructor(public data: T) {}
template(data: T) {
return html``;
}
render() {
const wrapEl = document.createElement('div');
wrapEl.innerHTML = this.template(this.data).toHtml();
return wrapEl.children[0];
}
}
interface User {
name: string;
age: number;
}
class UsersView extends View<User[]> {
override template(): Tmpl {
return html`
<div class="users">
${this.data.map(
(user) => html`
<div class="user">
<input type="checkbox" />
<span>${user.name}</span>
<span>${user.age}</span>
</div>
`,
)}
</div>
`;
}
}
export function main() {
const a = 'a';
const b = '<script>';
const c = '<img onload="">';
const result = html`
<ul>
<li>${a}</li>
<li>${b}</li>
<li>${c}</li>
<li>
${html`
<ul>
${[a, b, c].map((v) => html` <li>${v}</li> `)}
</ul>
`}
</li>
</ul>
`;
console.log('result: ', result);
console.log('result: ', result.toHtml());
console.log(
upper`
1${a}2${b}3${c}
`,
);
const users: User[] = [
{ name: 'idy', age: 20 },
{ name: 'idy', age: 22 },
{ name: 'idy', age: 23 },
{ name: 'idy', age: 25 },
{ name: 'idy', age: 26 },
];
console.log(
new UsersView([
{ name: 'idy', age: 20 },
{ name: 'idy', age: 22 },
{ name: 'idy', age: 23 },
]).render(),
);
document.body.append(new UsersView(users).render());
}