You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
2.6 KiB
115 lines
2.6 KiB
import { vnode, VNode, VNodeData } from "./vnode";
|
|
import * as is from "./is";
|
|
|
|
export type VNodes = VNode[];
|
|
export type VNodeChildElement =
|
|
| VNode
|
|
| string
|
|
| number
|
|
| String
|
|
| Number
|
|
| undefined
|
|
| null;
|
|
export type ArrayOrElement<T> = T | T[];
|
|
export type VNodeChildren = ArrayOrElement<VNodeChildElement>;
|
|
|
|
export function addNS(
|
|
data: any,
|
|
children: Array<VNode | string> | undefined,
|
|
sel: string | undefined
|
|
): void {
|
|
data.ns = "http://www.w3.org/2000/svg";
|
|
if (sel !== "foreignObject" && children !== undefined) {
|
|
for (let i = 0; i < children.length; ++i) {
|
|
const child = children[i];
|
|
if (typeof child === "string") continue;
|
|
const childData = child.data;
|
|
if (childData !== undefined) {
|
|
addNS(childData, child.children as VNodes, child.sel);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export function h(sel: string): VNode;
|
|
export function h(sel: string, data: VNodeData | null): VNode;
|
|
export function h(sel: string, children: VNodeChildren): VNode;
|
|
export function h(
|
|
sel: string,
|
|
data: VNodeData | null,
|
|
children: VNodeChildren
|
|
): VNode;
|
|
export function h(sel: any, b?: any, c?: any): VNode {
|
|
let data: VNodeData = {};
|
|
let children: any;
|
|
let text: any;
|
|
let i: number;
|
|
if (c !== undefined) {
|
|
if (b !== null) {
|
|
data = b;
|
|
}
|
|
if (is.array(c)) {
|
|
children = c;
|
|
} else if (is.primitive(c)) {
|
|
text = c.toString();
|
|
} else if (c && c.sel) {
|
|
children = [c];
|
|
}
|
|
} else if (b !== undefined && b !== null) {
|
|
if (is.array(b)) {
|
|
children = b;
|
|
} else if (is.primitive(b)) {
|
|
text = b.toString();
|
|
} else if (b && b.sel) {
|
|
children = [b];
|
|
} else {
|
|
data = b;
|
|
}
|
|
}
|
|
if (children !== undefined) {
|
|
for (i = 0; i < children.length; ++i) {
|
|
if (is.primitive(children[i]))
|
|
children[i] = vnode(
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
children[i],
|
|
undefined
|
|
);
|
|
}
|
|
}
|
|
if (
|
|
sel[0] === "s" &&
|
|
sel[1] === "v" &&
|
|
sel[2] === "g" &&
|
|
(sel.length === 3 || sel[3] === "." || sel[3] === "#")
|
|
) {
|
|
addNS(data, children, sel);
|
|
}
|
|
return vnode(sel, data, children, text, undefined);
|
|
}
|
|
|
|
/**
|
|
* @experimental
|
|
*/
|
|
export function fragment(children: VNodeChildren): VNode {
|
|
let c: any;
|
|
let text: any;
|
|
|
|
if (is.array(children)) {
|
|
c = children;
|
|
} else if (is.primitive(c)) {
|
|
text = children;
|
|
} else if (c && c.sel) {
|
|
c = [children];
|
|
}
|
|
|
|
if (c !== undefined) {
|
|
for (let i = 0; i < c.length; ++i) {
|
|
if (is.primitive(c[i]))
|
|
c[i] = vnode(undefined, undefined, undefined, c[i], undefined);
|
|
}
|
|
}
|
|
|
|
return vnode(undefined, {}, c, text, undefined);
|
|
}
|
|
|