-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsx-runtime.ts
222 lines (189 loc) · 4.8 KB
/
jsx-runtime.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
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
import { HTMLElements } from "./html.ts";
import { CSSProperties } from "./css.ts";
interface RawHtml {
__html?: string;
}
type Props = Record<string, unknown>;
type Content =
| string
| number
| boolean
| RawHtml
| ((...args: unknown[]) => Content)
| Content[];
const ssxElement = Symbol.for("ssx.element");
interface Component {
// deno-lint-ignore no-explicit-any
type: string | ((props: Props) => any);
props: Props;
}
const voidElements = new Set([
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"link",
"meta",
"param",
"source",
"track",
"wbr",
]);
const proto = Object.create(null, {
[ssxElement]: {
value: true,
enumerable: false,
},
});
/** The jsx function to create elements */
export function jsx(
type: string,
props: Props,
): Component {
const element = Object.create(proto);
element.type = type;
element.props = props;
return element;
}
/** Alias jsxs to jsx for compatibility with automatic runtime */
export { jsx as jsxs };
/** Fragment component to group multiple elements */
export function Fragment(props: { children: unknown }) {
return props.children;
}
/** Required for "precompile" mode */
export async function jsxTemplate(
strings: string[],
...values: unknown[]
): Promise<string> {
let result = strings[0];
for (let i = 0; i < values.length; i++) {
const value = values[i];
if (typeof value === "string") {
result += value;
} else if (isComponent(value)) {
result += await renderComponent(value);
} else {
result += await value;
}
result += strings[i + 1];
}
return result;
}
/** Required for "precompile" mode: render content */
export async function jsxEscape(content: Content): Promise<string> {
if (Array.isArray(content)) {
return (await Promise.all(content.map(jsxEscape))).join("");
}
if (content == null || content === undefined) {
return "";
}
switch (typeof content) {
case "string":
return content.replace(/&/g, "&").replace(/</g, "<").replace(
/>/g,
">",
);
case "object":
if ("__html" in content) {
return (content as RawHtml).__html ?? "";
}
if (isComponent(content)) {
return await renderComponent(content);
}
break;
case "number":
case "boolean":
return content.toString();
}
return content as Promise<string>;
}
// deno-lint-ignore no-explicit-any
function isComponent(value: any): value is Component {
return value !== null && typeof value === "object" &&
value[ssxElement] === true;
}
export async function renderComponent(
component: Component | Component[],
): Promise<string> {
if (Array.isArray(component)) {
return (await Promise.all(component.map(renderComponent))).join("");
}
const { type, props } = component;
// A Fragment
if (type === Fragment) {
return await jsxEscape(props.children as Content);
}
// An HTML tag
if (typeof type === "string") {
const isVoid = voidElements.has(type);
const attrs: string[] = [type];
let content = "";
if (props) {
for (
const [key, val] of Object.entries(props)
) {
if (key === "dangerouslySetInnerHTML") {
content = (val as RawHtml).__html ?? "";
continue;
}
if (key === "children") {
content = await jsxEscape(val as Content);
continue;
}
attrs.push(jsxAttr(key, val));
}
}
if (isVoid) {
if (content) {
throw new Error(`Void element "${type}" cannot have children`);
}
return `<${attrs.join(" ")}>`;
}
return `<${attrs.join(" ")}>${content}</${type}>`;
}
const comp = await type(props);
return typeof comp === "string" ? comp : await renderComponent(comp);
}
/** Required for "precompile" mode: render attributes */
export function jsxAttr(name: string, value: unknown): string {
if (name === "style" && typeof value === "object") {
value = renderStyles(value as CSSProperties);
}
if (typeof value === "string") {
return `${name}="${value.replace(/"/g, """)}"`;
}
if (typeof value === "boolean") {
return value ? name : "";
}
if (value == null || value === undefined) {
return "";
}
return `${name}="${value}"`;
}
/** Make JSX global */
declare global {
export namespace JSX {
export type Children =
| HTMLElements
| RawHtml
| string
| number
| boolean
| Children[];
export interface IntrinsicElements extends HTMLElements {}
export interface ElementChildrenAttribute {
children: Children;
}
}
}
function renderStyles(properties: CSSProperties) {
return Object.entries(properties)
.filter(([, value]) => value !== undefined && value !== null)
.map(([name, value]) => `${name}:${value};`)
.join("");
}