|
| 1 | +import type { Namespace } from './attribute-resolution.js' |
| 2 | +import type { JsxComponent, JsxRenderable } from './jsx-types.js' |
| 3 | + |
| 4 | +export const Fragment = Symbol.for('@knighted/jsx::Fragment') |
| 5 | + |
| 6 | +type JsxPropsRecord = Record<string, unknown> |
| 7 | + |
| 8 | +export type JsxCreateElement = { |
| 9 | + (type: typeof Fragment, props: null, ...children: JsxRenderable[]): DocumentFragment |
| 10 | + <Props extends JsxPropsRecord>( |
| 11 | + type: JsxComponent<Props>, |
| 12 | + props: (Props & { children?: JsxRenderable | JsxRenderable[] }) | null, |
| 13 | + ...children: JsxRenderable[] |
| 14 | + ): JsxRenderable |
| 15 | + ( |
| 16 | + type: string, |
| 17 | + props: JsxPropsRecord | null, |
| 18 | + ...children: JsxRenderable[] |
| 19 | + ): JsxRenderable |
| 20 | +} |
| 21 | + |
| 22 | +type DomCreateElementHelpers = { |
| 23 | + ensureDomAvailable: () => void |
| 24 | + appendChildValue: (parent: Node & ParentNode, value: JsxRenderable) => void |
| 25 | + setDomProp: ( |
| 26 | + element: Element, |
| 27 | + name: string, |
| 28 | + value: unknown, |
| 29 | + namespace: Namespace, |
| 30 | + ) => void |
| 31 | + isPromiseLike: (value: unknown) => value is PromiseLike<unknown> |
| 32 | +} |
| 33 | + |
| 34 | +const resolveChildrenForCreateElement = ( |
| 35 | + props: JsxPropsRecord, |
| 36 | + children: JsxRenderable[], |
| 37 | +) => { |
| 38 | + if (children.length > 0) { |
| 39 | + return children |
| 40 | + } |
| 41 | + |
| 42 | + if (!Object.prototype.hasOwnProperty.call(props, 'children')) { |
| 43 | + return [] |
| 44 | + } |
| 45 | + |
| 46 | + return [props.children as JsxRenderable] |
| 47 | +} |
| 48 | + |
| 49 | +const createPropsForComponent = (props: JsxPropsRecord, children: JsxRenderable[]) => { |
| 50 | + const nextProps = { ...props } |
| 51 | + |
| 52 | + if (children.length === 1) { |
| 53 | + nextProps.children = children[0] |
| 54 | + } else if (children.length > 1) { |
| 55 | + nextProps.children = children |
| 56 | + } else { |
| 57 | + delete nextProps.children |
| 58 | + } |
| 59 | + |
| 60 | + return nextProps |
| 61 | +} |
| 62 | + |
| 63 | +export const createDomCreateElement = ({ |
| 64 | + ensureDomAvailable, |
| 65 | + appendChildValue, |
| 66 | + setDomProp, |
| 67 | + isPromiseLike, |
| 68 | +}: DomCreateElementHelpers): JsxCreateElement => { |
| 69 | + function createElement( |
| 70 | + type: typeof Fragment, |
| 71 | + props: null, |
| 72 | + ...children: JsxRenderable[] |
| 73 | + ): DocumentFragment |
| 74 | + function createElement<Props extends JsxPropsRecord>( |
| 75 | + type: JsxComponent<Props>, |
| 76 | + props: (Props & { children?: JsxRenderable | JsxRenderable[] }) | null, |
| 77 | + ...children: JsxRenderable[] |
| 78 | + ): JsxRenderable |
| 79 | + function createElement( |
| 80 | + type: string, |
| 81 | + props: JsxPropsRecord | null, |
| 82 | + ...children: JsxRenderable[] |
| 83 | + ): JsxRenderable |
| 84 | + function createElement( |
| 85 | + type: string | JsxComponent | typeof Fragment, |
| 86 | + props: JsxPropsRecord | null, |
| 87 | + ...children: JsxRenderable[] |
| 88 | + ): JsxRenderable { |
| 89 | + ensureDomAvailable() |
| 90 | + |
| 91 | + const nextProps = props ? { ...props } : {} |
| 92 | + const resolvedChildren = resolveChildrenForCreateElement(nextProps, children) |
| 93 | + |
| 94 | + if (type === Fragment) { |
| 95 | + const fragment = document.createDocumentFragment() |
| 96 | + resolvedChildren.forEach(child => appendChildValue(fragment, child)) |
| 97 | + return fragment |
| 98 | + } |
| 99 | + |
| 100 | + if (typeof type === 'function') { |
| 101 | + const result = type(createPropsForComponent(nextProps, resolvedChildren)) |
| 102 | + |
| 103 | + if (isPromiseLike(result)) { |
| 104 | + throw new Error('Async jsx components are not supported.') |
| 105 | + } |
| 106 | + |
| 107 | + return result |
| 108 | + } |
| 109 | + |
| 110 | + if (typeof type !== 'string') { |
| 111 | + throw new Error(`Unsupported jsx createElement type: ${String(type)}`) |
| 112 | + } |
| 113 | + |
| 114 | + delete nextProps.children |
| 115 | + |
| 116 | + const nextNamespace: Namespace = type === 'svg' ? 'svg' : null |
| 117 | + const domElement = |
| 118 | + nextNamespace === 'svg' |
| 119 | + ? document.createElementNS('http://www.w3.org/2000/svg', type) |
| 120 | + : document.createElement(type) |
| 121 | + |
| 122 | + Object.entries(nextProps).forEach(([name, value]) => { |
| 123 | + if (name === 'key') { |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + setDomProp(domElement, name, value, nextNamespace) |
| 128 | + }) |
| 129 | + |
| 130 | + resolvedChildren.forEach(value => appendChildValue(domElement, value)) |
| 131 | + |
| 132 | + return domElement |
| 133 | + } |
| 134 | + |
| 135 | + return createElement |
| 136 | +} |
0 commit comments