-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnode.ts
More file actions
103 lines (85 loc) · 2.83 KB
/
Copy pathnode.ts
File metadata and controls
103 lines (85 loc) · 2.83 KB
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
import type { Tag } from "./lib/tags.ts";
import type { EmptyElements } from "./lib/emptyElements.ts";
import type { Attributes } from "./attributes.ts";
import { Falsy, isFalsy, isNonNullable } from "./util.ts";
import { ReadonlyState, State } from "./state.ts";
import * as Context from "./context.internal.ts";
import { Context as Context2 } from "./context.ts";
import { List, ReadonlyList } from "./list.ts";
export type NonEmptyElement = Exclude<Tag, EmptyElements>;
export type HyperTextNode = string;
export class HyperHTMLStringNode {
constructor(public htmlString: string) {}
}
export class HyperComment {
constructor(public text: string) {}
}
export class HyperNode<T extends Tag> {
constructor(public tag: T, public attrs: Attributes<T>, public children: HyperNodeish[]) {}
}
export type HyperChild<T extends Tag> =
| HyperNode<T>
| HyperHTMLStringNode
| HyperTextNode
| HyperComment;
export type HyperNodeish =
| HyperChild<any>
| Falsy
| Array<HyperNodeish>
| ReadonlyState<HyperNodeish>
| ReadonlyList<HyperNodeish>
| List<HyperNodeish>
| Context.Provider<any>
| Context.Consumer<any>;
export const isHyperChild = (n: any): n is HyperChild<any> =>
n instanceof HyperNode ||
n instanceof HyperHTMLStringNode ||
n instanceof HyperComment ||
typeof n === "string" ||
Array.isArray(n) ||
ReadonlyState.isState(n) ||
List.isList(n);
export const isHyperNodeish = (x: any): x is HyperNodeish =>
isHyperChild(x) || isFalsy(x) || State.isState(x) || Context2.isContext(x);
export function normaliseParams<T extends Tag>(
props?: Attributes<T> | HyperNodeish,
childNodes?: HyperNodeish[],
) {
const [attrs, children]: [Attributes<T>, HyperNodeish[]] = isHyperNodeish(props)
? [{}, [props, ...(childNodes || [])]]
: [props || {}, childNodes || []];
return { attrs, children };
}
export function h<Tag extends NonEmptyElement, Attrs extends Attributes<Tag>>(
elem: Tag,
props?: Attrs | Falsy,
): HyperNode<Tag>;
export function h<Tag extends NonEmptyElement>(
elem: Tag,
...children: HyperNodeish[]
): HyperNode<Tag>;
export function h<Tag extends NonEmptyElement, Attrs extends Attributes<Tag>>(
elem: Tag,
props: Attrs,
...children: HyperNodeish[]
): HyperNode<Tag>;
export function h<Tag extends NonEmptyElement, Attrs extends Attributes<Tag>>(
elem: Tag,
props?: Attrs | HyperNodeish | Falsy,
...children: HyperNodeish[]
): HyperNode<Tag>;
export function h<Tag extends EmptyElements, Attrs extends Attributes<Tag>>(
elem: Tag,
props?: Attrs | HyperNodeish | Falsy,
): HyperNode<Tag>;
export function h<T extends Tag>(
tag: T,
props?: Attributes<T> | HyperNodeish,
...childNodes: HyperNodeish[]
): HyperNode<T> {
const { attrs, children } = normaliseParams(props, childNodes);
return new HyperNode(tag, attrs, children.filter(isNonNullable));
}
export function trust(html: string) {
return new HyperHTMLStringNode(html);
}