-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
index.ts
229 lines (202 loc) · 5.4 KB
/
index.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
223
224
225
226
227
228
229
import { escapeToBuffer } from '../../utils/html.ts'
import type { StringBuffer, HtmlEscaped, HtmlEscapedString } from '../../utils/html.ts'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Props = Record<string, any>
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace JSX {
type Element = HtmlEscapedString
interface IntrinsicElements {
[tagName: string]: Props
}
}
}
const emptyTags = [
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'meta',
'param',
'source',
'track',
'wbr',
]
const booleanAttributes = [
'allowfullscreen',
'async',
'autofocus',
'autoplay',
'checked',
'controls',
'default',
'defer',
'disabled',
'formnovalidate',
'hidden',
'inert',
'ismap',
'itemscope',
'loop',
'multiple',
'muted',
'nomodule',
'novalidate',
'open',
'playsinline',
'readonly',
'required',
'reversed',
'selected',
]
const childrenToStringToBuffer = (children: Child[], buffer: StringBuffer): void => {
for (let i = 0, len = children.length; i < len; i++) {
const child = children[i]
if (typeof child === 'string') {
escapeToBuffer(child, buffer)
} else if (typeof child === 'boolean' || child === null || child === undefined) {
continue
} else if (child instanceof JSXNode) {
child.toStringToBuffer(buffer)
} else if (
typeof child === 'number' ||
(child as unknown as { isEscaped: boolean }).isEscaped
) {
buffer[0] += child
} else {
// `child` type is `Child[]`, so stringify recursively
childrenToStringToBuffer(child, buffer)
}
}
}
type Child = string | number | JSXNode | Child[]
export class JSXNode implements HtmlEscaped {
tag: string | Function
props: Props
children: Child[]
isEscaped: true = true
constructor(tag: string | Function, props: Props, children: Child[]) {
this.tag = tag
this.props = props
this.children = children
}
toString(): string {
const buffer: StringBuffer = ['']
this.toStringToBuffer(buffer)
return buffer[0]
}
toStringToBuffer(buffer: StringBuffer): void {
const tag = this.tag as string
const props = this.props
let { children } = this
buffer[0] += `<${tag}`
const propsKeys = Object.keys(props || {})
for (let i = 0, len = propsKeys.length; i < len; i++) {
const v = props[propsKeys[i]]
if (typeof v === 'string') {
buffer[0] += ` ${propsKeys[i]}="`
escapeToBuffer(v, buffer)
buffer[0] += '"'
} else if (typeof v === 'number') {
buffer[0] += ` ${propsKeys[i]}="${v}"`
} else if (v === null || v === undefined) {
// Do nothing
} else if (typeof v === 'boolean' && booleanAttributes.includes(propsKeys[i])) {
if (v) {
buffer[0] += ` ${propsKeys[i]}=""`
}
} else if (propsKeys[i] === 'dangerouslySetInnerHTML') {
if (children.length > 0) {
throw 'Can only set one of `children` or `props.dangerouslySetInnerHTML`.'
}
const escapedString = new String(v.__html) as HtmlEscapedString
escapedString.isEscaped = true
children = [escapedString]
} else {
buffer[0] += ` ${propsKeys[i]}="`
escapeToBuffer(v.toString(), buffer)
buffer[0] += '"'
}
}
if (emptyTags.includes(tag as string)) {
buffer[0] += '/>'
return
}
buffer[0] += '>'
childrenToStringToBuffer(children, buffer)
buffer[0] += `</${tag}>`
}
}
class JSXFunctionNode extends JSXNode {
toStringToBuffer(buffer: StringBuffer): void {
const { children } = this
const res = (this.tag as Function).call(null, {
...this.props,
children: children.length <= 1 ? children[0] : children,
})
if (res instanceof JSXNode) {
res.toStringToBuffer(buffer)
} else if (typeof res === 'number' || (res as HtmlEscaped).isEscaped) {
buffer[0] += res
} else {
escapeToBuffer(res, buffer)
}
}
}
class JSXFragmentNode extends JSXNode {
toStringToBuffer(buffer: StringBuffer): void {
childrenToStringToBuffer(this.children, buffer)
}
}
export { jsxFn as jsx }
const jsxFn = (
tag: string | Function,
props: Props,
...children: (string | HtmlEscapedString)[]
): JSXNode => {
if (typeof tag === 'function') {
return new JSXFunctionNode(tag, props, children)
} else {
return new JSXNode(tag, props, children)
}
}
type FC<T = Props> = (props: T) => HtmlEscapedString
const shallowEqual = (a: Props, b: Props): boolean => {
if (a === b) {
return true
}
const aKeys = Object.keys(a)
const bKeys = Object.keys(b)
if (aKeys.length !== bKeys.length) {
return false
}
for (let i = 0, len = aKeys.length; i < len; i++) {
if (a[aKeys[i]] !== b[aKeys[i]]) {
return false
}
}
return true
}
export const memo = <T>(
component: FC<T>,
propsAreEqual: (prevProps: Readonly<T>, nextProps: Readonly<T>) => boolean = shallowEqual
): FC<T> => {
let computed = undefined
let prevProps: T | undefined = undefined
return ((props: T): HtmlEscapedString => {
if (prevProps && !propsAreEqual(prevProps, props)) {
computed = undefined
}
prevProps = props
return (computed ||= component(props))
}) as FC<T>
}
export const Fragment = (props: { key?: string; children?: Child[] }): JSXNode => {
return new JSXFragmentNode('', {}, props.children || [])
}