import { ManagedElement, Node, TextNode } from "../src"; import { ElementClass } from "../src/context"; import { ElementClass as EC } from "../src/context"; import { DefaultElement } from "../src/node/element/default"; import { FragmentElement } from "../src/node/element/fragment"; export function jsxs(element: ElementClass | typeof Fragment | string, props: { children?: DefaultElement | string | number | boolean | (DefaultElement | string | number | boolean)[] } & { [key: string]: string }, key?: string) { return jsx(element, props, key) } export function jsx(element: ElementClass | typeof Fragment | string, props: { children?: DefaultElement | string | number | boolean | (DefaultElement | string | number | boolean)[] } & { [key: string]: string }, key?: string): FragmentElement { if (element === Fragment) { const children = props.children !== undefined ? Array.isArray(props.children) ? props.children : [ props.children ] : []; const elsChildren: Node[] = []; for (const child of children) { if (child instanceof Node) elsChildren.push(child) else elsChildren.push(new TextNode(child.toString())); } return new FragmentElement(elsChildren); } const children = props.children ?? []; delete props.children; if (key) props.key = key; for (const key of Object.keys(props)) { if (props[key] === undefined) delete props[key]; props[key] = props[key].toString(); } let realChildren: Node[] = []; if (!Array.isArray(children)) { if (children instanceof Node) realChildren.push(children); else realChildren.push(new TextNode(children.toString())) } else { for (const child of children) { if (child instanceof Node) { realChildren.push(child); continue; } realChildren.push(new TextNode(child.toString())); } } if (realChildren.length === 0) { return new FragmentElement(DefaultElement.selfClosing(typeof element === "string" ? element : element.name, props)); } return new FragmentElement(new DefaultElement(typeof element === "string" ? element : element.name, props, realChildren)); } export const Fragment = Symbol("Fragment");