Repository navigation
Expand file tree
/
Copy pathdom.ts
More file actions
194 lines (175 loc) · 4.83 KB
/
Copy pathdom.ts
File metadata and controls
194 lines (175 loc) · 4.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import {
HostContext,
Default,
Environment,
Intrinsic,
Props,
Raw,
Renderer,
Portal,
} from "./index";
declare module "./index" {
interface EventMap extends GlobalEventHandlersEventMap {}
}
// TODO: create an allowlist/blocklist of props
function updateProps(el: Element, props: Props, newProps: Props): void {
for (const name in {...props, ...newProps}) {
const value = props[name];
const newValue = newProps[name];
switch (true) {
case name === "children":
break;
case name === "class":
case name === "className": {
(el as any)["className"] = newValue;
break;
}
case name === "style" && "style" in el: {
const style: CSSStyleDeclaration = (el as any).style;
if (newValue == null) {
el.removeAttribute("style");
} else if (typeof newValue === "string") {
style.cssText = newValue;
} else {
for (const styleName in Object.assign({}, value, newValue)) {
const styleValue = value && value[styleName];
const newStyleValue = newValue && newValue[styleName];
if (newStyleValue == null) {
style.removeProperty(styleName);
} else if (styleValue !== newStyleValue) {
style.setProperty(styleName, newStyleValue);
}
}
}
break;
}
default: {
if (name in el) {
(el as any)[name] = newValue;
break;
} else if (newValue === true) {
el.setAttribute(name, "");
} else if (newValue === false || newValue == null) {
el.removeAttribute(name);
} else {
el.setAttribute(name, newValue);
}
break;
}
}
}
}
// TODO: improve this algorithm
// https://stackoverflow.com/questions/59418120/what-is-the-most-efficient-way-to-update-the-childnodes-of-a-dom-node-with-an-ar
function updateChildren(el: Element, children: Array<Node | string>): void {
if (el.childNodes.length === 0) {
const fragment = document.createDocumentFragment();
for (let child of children) {
if (typeof child === "string") {
child = document.createTextNode(child);
}
fragment.appendChild(child);
}
el.appendChild(fragment);
return;
}
let oldChild = el.firstChild;
for (const newChild of children) {
if (oldChild === null) {
el.appendChild(
typeof newChild === "string"
? document.createTextNode(newChild)
: newChild,
);
} else if (typeof newChild === "string") {
if (oldChild.nodeType === Node.TEXT_NODE) {
if (oldChild.nodeValue !== newChild) {
oldChild.nodeValue = newChild;
}
oldChild = oldChild.nextSibling;
} else {
el.insertBefore(document.createTextNode(newChild), oldChild);
}
} else if (oldChild !== newChild) {
el.insertBefore(newChild, oldChild);
} else {
oldChild = oldChild.nextSibling;
}
}
while (oldChild !== null) {
const nextSibling = oldChild.nextSibling;
el.removeChild(oldChild);
oldChild = nextSibling;
}
}
function createDocumentFragmentFromHTML(html: string): DocumentFragment {
if (typeof document.createRange === "function") {
return document.createRange().createContextualFragment(html);
} else {
const fragment = document.createDocumentFragment();
const childNodes = new DOMParser().parseFromString(html, "text/html").body
.childNodes;
for (let i = 0; i < childNodes.length; i++) {
fragment.appendChild(childNodes[i]);
}
return fragment;
}
}
// TODO: Element should be ParentNode maybe?
export const env: Environment<Element> = {
[Default](tag: string): Intrinsic<Element> {
return function* defaultDOM(this: HostContext): Generator<Element> {
const node = document.createElement(tag);
let props: Props = {};
let nextProps: Props;
let children: Array<Element | string> = [];
let nextChildren: Array<Element | string>;
for ({children: nextChildren, ...nextProps} of this) {
updateProps(node, props, nextProps);
props = nextProps;
if (
!("innerHTML" in nextProps) &&
(children.length > 0 || nextChildren.length > 0)
) {
updateChildren(node, nextChildren);
children = nextChildren;
}
yield node;
}
};
},
[Raw]({value}): Element {
if (typeof value === "string") {
// TODO: figure out what the type of element should actually be
return (createDocumentFragmentFromHTML(value) as unknown) as Element;
} else {
return value;
}
},
*[Portal](this: HostContext, {root}): Generator<Element> {
if (root == null) {
throw new TypeError("Portal element is missing root node");
}
try {
for (const {root: newRoot, children} of this) {
if (newRoot == null) {
throw new TypeError("Portal element is missing root node");
}
if (root !== newRoot) {
updateChildren(root, []);
root = newRoot;
}
updateChildren(root, children);
yield root;
}
} finally {
updateChildren(root, []);
}
},
};
export class DOMRenderer extends Renderer<Element> {
constructor() {
super(env);
}
}
export const renderer = new DOMRenderer();