blob: 6b1c8038561fc9e3d4f573c3eb13d04184300c67 (
plain)
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
|
type Attribute = string | boolean | ((e: Event) => void)
type Child = Element | string
export function h(tag: string, attrs: {[key: string]: Attribute}, children: Array<Child> = []): Element {
let element = document.createElement(tag)
for (let name in attrs) {
let value = attrs[name]
if (typeof value === 'boolean') {
if (value) {
element.setAttribute(name, name)
}
} else if (typeof value === 'function') {
(element as any)[name] = (e: Event) => {
(value as ((e: Event) => void))(e)
}
} else {
element.setAttribute(name, value)
}
}
children.forEach(child => {
if (typeof child === 'string') {
element.appendChild(document.createTextNode(child))
} else {
element.appendChild(child)
}
})
return element
}
export function toggleClassName(node: Element, className: string) {
if (node.className === className) {
node.className = ''
} else {
node.className = className
}
}
export function nodeListToArray(nodeList: NodeListOf<HTMLElement>): HTMLElement[] {
const xs: HTMLElement[] = [];
nodeList.forEach(node => xs.push(node))
return xs
}
export function replace(node: Node, replacement: Node) {
const parentNode = node.parentNode
if (parentNode) {
parentNode.replaceChild(replacement, node)
}
}
|