-
Notifications
You must be signed in to change notification settings - Fork 1
/
html.js
141 lines (134 loc) · 4.21 KB
/
html.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import htm from "./third_party/htm_mini.js";
import { isObject, isTemplate, stringify } from "./util.js";
/**
* @typedef {
* | string
* | number
* | boolean
* | null
* | undefined
* | HReturn
* | EventListener
* | Record<string, any>
* | AllowedExpressionsArray} AllowedExpressions
* @typedef {AllowedExpressions[]} AllowedExpressionsArray
* @typedef {{ event: string; listener: EventListener }} EventAndListener
* @typedef {{
* element: HTMLElement | SVGElement;
* collection: Collection;
* }} HReturn
* @typedef {(event: any) => any} EventListener
* @typedef {{
* kind: "id" | "class";
* selector: string;
* }} Query
* @typedef {{
* target: HTMLElement | SVGElement;
* queries: Query[];
* eventsAndListeners: EventAndListener[];
* }[]} Collection
*/
const SVG_NS = "http://www.w3.org/2000/svg";
/**
* @param {any[]} input
* @returns {boolean}
*/
function isArrayOfListeners(input) {
return input.every((i) => typeof i === "function");
}
/**
* @param {string} input
* @returns {boolean}
*/
function isSpecialKey(input) {
return input === "id" || input === "class";
}
/**
* @param {any} input
* @returns {boolean}
*/
export function isHReturn(input) {
return isObject(input) && input.element instanceof Element;
}
/**
* @param {string} type
* @param {Record<string, any>} props
* @param {AllowedExpressions[]} children
* @returns {HReturn}
*/
export function h(type, props, ...children) {
const eventsAndListeners = [];
const queries = [];
const collection = [];
const element = type === "svg"
? document.createElementNS(SVG_NS, "svg")
: document.createElement(type);
for (const key in props) {
if (typeof props[key] === "function") {
eventsAndListeners.push({ event: key, listener: props[key] });
} else if (Array.isArray(props[key]) && isArrayOfListeners(props[key])) {
props[key].forEach(/**@type {EventListener}*/ (listener) =>
eventsAndListeners.push({ event: key, listener })
);
} else if (key[0] === "@") {
const idOrClass = key.slice(1);
if (isSpecialKey(idOrClass)) {
queries.push({
kind: idOrClass,
selector: props[key].replace(/ .*/, ""),
});
element.setAttribute(idOrClass, props[key]);
}
} else if (props[key] === true) {
element.setAttribute(key, "");
} else if (typeof props[key] === "object" && props[key] !== null) {
element.setAttribute(key, JSON.stringify(props[key]));
} else if (typeof props[key] === "string") {
element.setAttribute(key, props[key]);
} else if (
props[key] === null || props[key] === false || props[key] === undefined
) {
element.removeAttribute(key);
}
}
// TODO: Improve SVG parsing.
if (type === "svg") {
element.innerHTML = children.flat(2).reduce((acc, child) => {
return acc +
(isHReturn(child) ? child.element.outerHTML : stringify(child));
}, "");
} else {
for (const child of children.flat(2)) {
if (isTemplate(child)) {
element.appendChild(child.content.cloneNode(true));
} else if (isHReturn(child)) {
collection.push(...child.collection);
element.appendChild(child.element);
} else {
const str = stringify(child);
if (str) {
element.appendChild(document.createTextNode(str));
}
}
}
}
if (queries.length || eventsAndListeners.length) {
collection.push({ target: element, queries, eventsAndListeners });
}
return { element, collection };
}
/**
* Uses [htm (Hyperscript Tagged Markup)](https://github.com/developit/htm) under
* the hood and works in all modern browsers. The function 'html' takes a
* _tagged template_ and processes the 'AllowedExpressions' where _falsy_ values
* are converted to an empty string and 'number's are _stringified_.
* The children, who match the 'id' and 'class' selectors marked with an '@' sign,
* will be added to the object 'this.dom' in the process.
* It adds 'EventListeners' with 'addEventListener(event, listener.bind(this))',
* so you don't need to use arrow functions anymore.
* @type{(
* strings: TemplateStringsArray,
* ...values: AllowedExpressions[]
* ) => AllowedExpressions}
*/
export const html = htm.bind(h);