@b9g/crank 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/_utils.d.ts +14 -0
- package/async.cjs +237 -0
- package/async.cjs.map +1 -0
- package/async.d.ts +104 -0
- package/async.js +234 -0
- package/async.js.map +1 -0
- package/crank.cjs +1672 -1313
- package/crank.cjs.map +1 -1
- package/crank.d.ts +230 -247
- package/crank.js +1672 -1314
- package/crank.js.map +1 -1
- package/dom.cjs +356 -212
- package/dom.cjs.map +1 -1
- package/dom.d.ts +5 -5
- package/dom.js +357 -213
- package/dom.js.map +1 -1
- package/event-target.cjs +254 -0
- package/event-target.cjs.map +1 -0
- package/event-target.d.ts +26 -0
- package/event-target.js +249 -0
- package/event-target.js.map +1 -0
- package/html.cjs +32 -43
- package/html.cjs.map +1 -1
- package/html.d.ts +3 -3
- package/html.js +33 -44
- package/html.js.map +1 -1
- package/jsx-runtime.cjs +1 -0
- package/jsx-runtime.cjs.map +1 -1
- package/jsx-runtime.js +1 -0
- package/jsx-runtime.js.map +1 -1
- package/jsx-tag.cjs +6 -2
- package/jsx-tag.cjs.map +1 -1
- package/jsx-tag.d.ts +2 -0
- package/jsx-tag.js +6 -3
- package/jsx-tag.js.map +1 -1
- package/package.json +46 -30
- package/standalone.cjs +3 -0
- package/standalone.cjs.map +1 -1
- package/standalone.d.ts +1 -1
- package/standalone.js +3 -2
- package/standalone.js.map +1 -1
- package/umd.js +2292 -1562
- package/umd.js.map +1 -1
package/dom.js
CHANGED
|
@@ -1,11 +1,52 @@
|
|
|
1
1
|
/// <reference types="dom.d.ts" />
|
|
2
|
-
import {
|
|
2
|
+
import { Renderer, Portal } from './crank.js';
|
|
3
|
+
import './event-target.js';
|
|
3
4
|
|
|
4
5
|
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
function isWritableProperty(element, name) {
|
|
7
|
+
// walk up the object's prototype chain to find the owner
|
|
8
|
+
let propOwner = element;
|
|
9
|
+
do {
|
|
10
|
+
if (Object.prototype.hasOwnProperty.call(propOwner, name)) {
|
|
11
|
+
break;
|
|
12
|
+
}
|
|
13
|
+
} while ((propOwner = Object.getPrototypeOf(propOwner)));
|
|
14
|
+
if (propOwner === null) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
// get the descriptor for the named property and check whether it implies
|
|
18
|
+
// that the property is writable
|
|
19
|
+
const descriptor = Object.getOwnPropertyDescriptor(propOwner, name);
|
|
20
|
+
if (descriptor != null &&
|
|
21
|
+
(descriptor.writable === true || descriptor.set !== undefined)) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
function emitHydrationWarning(propName, quietProps, expectedValue, actualValue, element, displayName) {
|
|
27
|
+
const checkName = propName;
|
|
28
|
+
const showName = displayName || propName;
|
|
29
|
+
if (!quietProps || !quietProps.has(checkName)) {
|
|
30
|
+
if (expectedValue === null || expectedValue === false) {
|
|
31
|
+
console.warn(`Expected "${showName}" to be missing but found ${String(actualValue)} while hydrating:`, element);
|
|
32
|
+
}
|
|
33
|
+
else if (expectedValue === true || expectedValue === "") {
|
|
34
|
+
console.warn(`Expected "${showName}" to be ${expectedValue === true ? "present" : '""'} but found ${String(actualValue)} while hydrating:`, element);
|
|
35
|
+
}
|
|
36
|
+
else if (typeof window !== "undefined" &&
|
|
37
|
+
window.location &&
|
|
38
|
+
new URL(expectedValue, window.location.origin).href ===
|
|
39
|
+
new URL(actualValue, window.location.origin).href) ;
|
|
40
|
+
else {
|
|
41
|
+
console.warn(`Expected "${showName}" to be "${String(expectedValue)}" but found ${String(actualValue)} while hydrating:`, element);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const adapter = {
|
|
46
|
+
scope({ scope: xmlns, tag, props, }) {
|
|
7
47
|
switch (tag) {
|
|
8
48
|
case Portal:
|
|
49
|
+
// TODO: read the namespace from the portal root element
|
|
9
50
|
xmlns = undefined;
|
|
10
51
|
break;
|
|
11
52
|
case "svg":
|
|
@@ -14,9 +55,9 @@ const impl = {
|
|
|
14
55
|
}
|
|
15
56
|
return props.xmlns || xmlns;
|
|
16
57
|
},
|
|
17
|
-
create(tag,
|
|
58
|
+
create({ tag, tagName, scope: xmlns, }) {
|
|
18
59
|
if (typeof tag !== "string") {
|
|
19
|
-
throw new Error(`Unknown tag: ${
|
|
60
|
+
throw new Error(`Unknown tag: ${tagName}`);
|
|
20
61
|
}
|
|
21
62
|
else if (tag.toLowerCase() === "svg") {
|
|
22
63
|
xmlns = SVG_NAMESPACE;
|
|
@@ -25,262 +66,362 @@ const impl = {
|
|
|
25
66
|
? document.createElementNS(xmlns, tag)
|
|
26
67
|
: document.createElement(tag);
|
|
27
68
|
},
|
|
28
|
-
|
|
69
|
+
adopt({ tag, tagName, node, }) {
|
|
29
70
|
if (typeof tag !== "string" && tag !== Portal) {
|
|
30
|
-
throw new Error(`Unknown tag: ${
|
|
71
|
+
throw new Error(`Unknown tag: ${tagName}`);
|
|
31
72
|
}
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
73
|
+
if (node === document.body ||
|
|
74
|
+
node === document.head ||
|
|
75
|
+
node === document.documentElement ||
|
|
76
|
+
node === document) {
|
|
77
|
+
console.warn(`Hydrating ${node.nodeName.toLowerCase()} is discouraged as it is destructive and may remove unknown nodes.`);
|
|
37
78
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
else if (child.nodeType === Node.ELEMENT_NODE) {
|
|
45
|
-
children.push(child);
|
|
46
|
-
}
|
|
79
|
+
if (node == null ||
|
|
80
|
+
(typeof tag === "string" &&
|
|
81
|
+
(node.nodeType !== Node.ELEMENT_NODE ||
|
|
82
|
+
tag.toLowerCase() !== node.tagName.toLowerCase()))) {
|
|
83
|
+
console.warn(`Expected <${tagName}> while hydrating but found: `, node);
|
|
84
|
+
return;
|
|
47
85
|
}
|
|
48
|
-
|
|
49
|
-
return { props, children };
|
|
86
|
+
return Array.from(node.childNodes);
|
|
50
87
|
},
|
|
51
|
-
patch(
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
88
|
+
patch({ tagName, node, props, oldProps, scope: xmlns, copyProps, quietProps, isHydrating, }) {
|
|
89
|
+
if (node.nodeType !== Node.ELEMENT_NODE) {
|
|
90
|
+
throw new TypeError(`Cannot patch node: ${String(node)}`);
|
|
91
|
+
}
|
|
92
|
+
else if (props.class && props.className) {
|
|
93
|
+
console.error(`Both "class" and "className" set in props for <${tagName}>. Use one or the other.`);
|
|
94
|
+
}
|
|
95
|
+
const element = node;
|
|
56
96
|
const isSVG = xmlns === SVG_NAMESPACE;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
else if (value == null || value === false) {
|
|
64
|
-
node.removeAttribute("style");
|
|
97
|
+
for (let name in { ...oldProps, ...props }) {
|
|
98
|
+
let value = props[name];
|
|
99
|
+
const oldValue = oldProps ? oldProps[name] : undefined;
|
|
100
|
+
{
|
|
101
|
+
if (copyProps != null && copyProps.has(name)) {
|
|
102
|
+
continue;
|
|
65
103
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
104
|
+
// handle prop:name or attr:name properties
|
|
105
|
+
const colonIndex = name.indexOf(":");
|
|
106
|
+
if (colonIndex !== -1) {
|
|
107
|
+
const [ns, name1] = [
|
|
108
|
+
name.slice(0, colonIndex),
|
|
109
|
+
name.slice(colonIndex + 1),
|
|
110
|
+
];
|
|
111
|
+
switch (ns) {
|
|
112
|
+
case "prop":
|
|
113
|
+
node[name1] = value;
|
|
114
|
+
continue;
|
|
115
|
+
case "attr":
|
|
116
|
+
if (value == null || value === false) {
|
|
117
|
+
if (isHydrating && element.hasAttribute(name1)) {
|
|
118
|
+
emitHydrationWarning(name, quietProps, value, element.getAttribute(name1), element);
|
|
119
|
+
}
|
|
120
|
+
element.removeAttribute(name1);
|
|
121
|
+
}
|
|
122
|
+
else if (value === true) {
|
|
123
|
+
if (isHydrating && !element.hasAttribute(name1)) {
|
|
124
|
+
emitHydrationWarning(name, quietProps, value, null, element);
|
|
125
|
+
}
|
|
126
|
+
element.setAttribute(name1, "");
|
|
127
|
+
}
|
|
128
|
+
else if (typeof value !== "string") {
|
|
129
|
+
value = String(value);
|
|
130
|
+
}
|
|
131
|
+
if (isHydrating && element.getAttribute(name1) !== value) {
|
|
132
|
+
emitHydrationWarning(name, quietProps, value, element.getAttribute(name1), element);
|
|
133
|
+
}
|
|
134
|
+
element.setAttribute(name1, String(value));
|
|
135
|
+
continue;
|
|
72
136
|
}
|
|
73
137
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
138
|
+
}
|
|
139
|
+
switch (name) {
|
|
140
|
+
// TODO: fix hydration warnings for the style prop
|
|
141
|
+
case "style": {
|
|
142
|
+
const style = element.style;
|
|
143
|
+
if (value == null || value === false) {
|
|
144
|
+
if (isHydrating && style.cssText !== "") {
|
|
145
|
+
emitHydrationWarning(name, quietProps, value, style.cssText, element);
|
|
146
|
+
}
|
|
147
|
+
element.removeAttribute("style");
|
|
77
148
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
style.removeProperty(styleName);
|
|
149
|
+
else if (value === true) {
|
|
150
|
+
if (isHydrating && style.cssText !== "") {
|
|
151
|
+
emitHydrationWarning(name, quietProps, "", style.cssText, element);
|
|
82
152
|
}
|
|
83
|
-
|
|
84
|
-
|
|
153
|
+
element.setAttribute("style", "");
|
|
154
|
+
}
|
|
155
|
+
else if (typeof value === "string") {
|
|
156
|
+
if (style.cssText !== value) {
|
|
157
|
+
// TODO: Fix hydration warnings for styles
|
|
158
|
+
//if (isHydrating) {
|
|
159
|
+
// emitHydrationWarning(
|
|
160
|
+
// name,
|
|
161
|
+
// quietProps,
|
|
162
|
+
// value,
|
|
163
|
+
// style.cssText,
|
|
164
|
+
// element,
|
|
165
|
+
// );
|
|
166
|
+
//}
|
|
167
|
+
style.cssText = value;
|
|
85
168
|
}
|
|
86
169
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
170
|
+
else {
|
|
171
|
+
if (typeof oldValue === "string") {
|
|
172
|
+
// if the old value was a string, we need to clear the style
|
|
173
|
+
// TODO: only clear the styles enumerated in the old value
|
|
174
|
+
style.cssText = "";
|
|
175
|
+
}
|
|
176
|
+
for (const styleName in { ...oldValue, ...value }) {
|
|
177
|
+
const styleValue = value && value[styleName];
|
|
178
|
+
if (styleValue == null) {
|
|
179
|
+
if (isHydrating && style.getPropertyValue(styleName) !== "") {
|
|
180
|
+
emitHydrationWarning(name, quietProps, null, style.getPropertyValue(styleName), element, `style.${styleName}`);
|
|
181
|
+
}
|
|
182
|
+
style.removeProperty(styleName);
|
|
183
|
+
}
|
|
184
|
+
else if (style.getPropertyValue(styleName) !== styleValue) {
|
|
185
|
+
// TODO: hydration warnings for style props
|
|
186
|
+
//if (isHydrating) {
|
|
187
|
+
// emitHydrationWarning(
|
|
188
|
+
// name,
|
|
189
|
+
// quietProps,
|
|
190
|
+
// styleValue,
|
|
191
|
+
// style.getPropertyValue(styleName),
|
|
192
|
+
// element,
|
|
193
|
+
// `style.${styleName}`,
|
|
194
|
+
// );
|
|
195
|
+
//}
|
|
196
|
+
style.setProperty(styleName, styleValue);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
101
199
|
}
|
|
200
|
+
break;
|
|
102
201
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (value !== oldValue) {
|
|
109
|
-
node.innerHTML = value;
|
|
110
|
-
}
|
|
111
|
-
break;
|
|
112
|
-
default: {
|
|
113
|
-
if (name[0] === "o" &&
|
|
114
|
-
name[1] === "n" &&
|
|
115
|
-
name[2] === name[2].toUpperCase() &&
|
|
116
|
-
typeof value === "function") {
|
|
117
|
-
// Support React-style event names (onClick, onChange, etc.)
|
|
118
|
-
name = name.toLowerCase();
|
|
119
|
-
}
|
|
120
|
-
if (name in node &&
|
|
121
|
-
// boolean properties will coerce strings, but sometimes they map to
|
|
122
|
-
// enumerated attributes, where truthy strings ("false", "no") map to
|
|
123
|
-
// falsy properties, so we use attributes in this case.
|
|
124
|
-
!(typeof value === "string" &&
|
|
125
|
-
typeof node[name] === "boolean")) {
|
|
126
|
-
// walk up the object's prototype chain to find the owner of the
|
|
127
|
-
// named property
|
|
128
|
-
let obj = node;
|
|
129
|
-
do {
|
|
130
|
-
if (Object.prototype.hasOwnProperty.call(obj, name)) {
|
|
131
|
-
break;
|
|
202
|
+
case "class":
|
|
203
|
+
case "className":
|
|
204
|
+
if (value === true) {
|
|
205
|
+
if (isHydrating && element.getAttribute("class") !== "") {
|
|
206
|
+
emitHydrationWarning(name, quietProps, "", element.getAttribute("class"), element);
|
|
132
207
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
(descriptor.writable === true || descriptor.set !== undefined)) {
|
|
139
|
-
if (node[name] !== value || oldValue === undefined) {
|
|
140
|
-
node[name] = value;
|
|
208
|
+
element.setAttribute("class", "");
|
|
209
|
+
}
|
|
210
|
+
else if (value == null) {
|
|
211
|
+
if (isHydrating && element.hasAttribute("class")) {
|
|
212
|
+
emitHydrationWarning(name, quietProps, value, element.getAttribute("class"), element);
|
|
141
213
|
}
|
|
142
|
-
|
|
214
|
+
element.removeAttribute("class");
|
|
143
215
|
}
|
|
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
|
-
while (oldChild !== null && i < children.length) {
|
|
179
|
-
const newChild = children[i];
|
|
180
|
-
if (oldChild === newChild) {
|
|
181
|
-
oldChild = oldChild.nextSibling;
|
|
182
|
-
i++;
|
|
216
|
+
else if (typeof value === "object") {
|
|
217
|
+
// class={{"included-class": true, "excluded-class": false}} syntax
|
|
218
|
+
if (typeof oldValue === "string") {
|
|
219
|
+
// if the old value was a string, we need to clear all classes
|
|
220
|
+
element.setAttribute("class", "");
|
|
221
|
+
}
|
|
222
|
+
let shouldIssueWarning = false;
|
|
223
|
+
const hydratingClasses = isHydrating
|
|
224
|
+
? new Set(Array.from(element.classList))
|
|
225
|
+
: undefined;
|
|
226
|
+
const hydratingClassName = isHydrating
|
|
227
|
+
? element.getAttribute("class")
|
|
228
|
+
: undefined;
|
|
229
|
+
for (const className in { ...oldValue, ...value }) {
|
|
230
|
+
const classValue = value && value[className];
|
|
231
|
+
if (classValue) {
|
|
232
|
+
element.classList.add(className);
|
|
233
|
+
if (hydratingClasses && hydratingClasses.has(className)) {
|
|
234
|
+
hydratingClasses.delete(className);
|
|
235
|
+
}
|
|
236
|
+
else if (isHydrating) {
|
|
237
|
+
shouldIssueWarning = true;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
element.classList.remove(className);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (shouldIssueWarning ||
|
|
245
|
+
(hydratingClasses && hydratingClasses.size > 0)) {
|
|
246
|
+
emitHydrationWarning(name, quietProps, Object.keys(value)
|
|
247
|
+
.filter((k) => value[k])
|
|
248
|
+
.join(" "), hydratingClassName || "", element);
|
|
249
|
+
}
|
|
183
250
|
}
|
|
184
|
-
else if (
|
|
185
|
-
if (
|
|
186
|
-
if (
|
|
187
|
-
|
|
251
|
+
else if (!isSVG) {
|
|
252
|
+
if (element.className !== value) {
|
|
253
|
+
if (isHydrating) {
|
|
254
|
+
emitHydrationWarning(name, quietProps, value, element.className, element);
|
|
188
255
|
}
|
|
189
|
-
|
|
256
|
+
element.className = value;
|
|
190
257
|
}
|
|
191
|
-
|
|
192
|
-
|
|
258
|
+
}
|
|
259
|
+
else if (element.getAttribute("class") !== value) {
|
|
260
|
+
if (isHydrating) {
|
|
261
|
+
emitHydrationWarning(name, quietProps, value, element.getAttribute("class"), element);
|
|
193
262
|
}
|
|
194
|
-
|
|
263
|
+
element.setAttribute("class", value);
|
|
195
264
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
265
|
+
break;
|
|
266
|
+
case "innerHTML":
|
|
267
|
+
if (value !== oldValue) {
|
|
268
|
+
if (isHydrating) {
|
|
269
|
+
emitHydrationWarning(name, quietProps, value, element.innerHTML, element);
|
|
270
|
+
}
|
|
271
|
+
element.innerHTML = value;
|
|
200
272
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
273
|
+
break;
|
|
274
|
+
default: {
|
|
275
|
+
if (name[0] === "o" &&
|
|
276
|
+
name[1] === "n" &&
|
|
277
|
+
name[2] === name[2].toUpperCase() &&
|
|
278
|
+
typeof value === "function") {
|
|
279
|
+
// Support React-style event names (onClick, onChange, etc.)
|
|
280
|
+
name = name.toLowerCase();
|
|
281
|
+
}
|
|
282
|
+
// try to set the property directly
|
|
283
|
+
if (name in element &&
|
|
284
|
+
// boolean properties will coerce strings, but sometimes they map to
|
|
285
|
+
// enumerated attributes, where truthy strings ("false", "no") map to
|
|
286
|
+
// falsy properties, so we force using setAttribute.
|
|
287
|
+
!(typeof value === "string" &&
|
|
288
|
+
typeof element[name] === "boolean") &&
|
|
289
|
+
isWritableProperty(element, name)) {
|
|
290
|
+
if (element[name] !== value || oldValue === undefined) {
|
|
291
|
+
if (isHydrating &&
|
|
292
|
+
typeof element[name] === "string" &&
|
|
293
|
+
element[name] !== value) {
|
|
294
|
+
emitHydrationWarning(name, quietProps, value, element[name], element);
|
|
295
|
+
}
|
|
296
|
+
// if the property is writable, assign it directly
|
|
297
|
+
element[name] = value;
|
|
298
|
+
}
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if (value === true) {
|
|
302
|
+
value = "";
|
|
303
|
+
}
|
|
304
|
+
else if (value == null || value === false) {
|
|
305
|
+
if (isHydrating && element.hasAttribute(name)) {
|
|
306
|
+
emitHydrationWarning(name, quietProps, value, element.getAttribute(name), element);
|
|
209
307
|
}
|
|
308
|
+
element.removeAttribute(name);
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
else if (typeof value !== "string") {
|
|
312
|
+
value = String(value);
|
|
313
|
+
}
|
|
314
|
+
if (element.getAttribute(name) !== value) {
|
|
315
|
+
if (isHydrating) {
|
|
316
|
+
emitHydrationWarning(name, quietProps, value, element.getAttribute(name), element);
|
|
317
|
+
}
|
|
318
|
+
element.setAttribute(name, value);
|
|
210
319
|
}
|
|
211
320
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
},
|
|
324
|
+
arrange({ tag, node, props, children, }) {
|
|
325
|
+
if (tag === Portal && (node == null || typeof node.nodeType !== "number")) {
|
|
326
|
+
throw new TypeError(`<Portal> root is not a node. Received: ${String(node)}`);
|
|
327
|
+
}
|
|
328
|
+
if (!("innerHTML" in props)) {
|
|
329
|
+
let oldChild = node.firstChild;
|
|
330
|
+
for (let i = 0; i < children.length; i++) {
|
|
331
|
+
const newChild = children[i];
|
|
332
|
+
if (oldChild === newChild) {
|
|
333
|
+
// the child is already in the right place, so we can skip it
|
|
334
|
+
oldChild = oldChild.nextSibling;
|
|
217
335
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
336
|
+
else {
|
|
337
|
+
node.insertBefore(newChild, oldChild);
|
|
338
|
+
if (tag !== Portal &&
|
|
339
|
+
oldChild &&
|
|
340
|
+
i + 1 < children.length &&
|
|
341
|
+
oldChild !== children[i + 1]) {
|
|
342
|
+
oldChild = oldChild.nextSibling;
|
|
343
|
+
}
|
|
224
344
|
}
|
|
225
345
|
}
|
|
226
346
|
}
|
|
227
347
|
},
|
|
228
|
-
|
|
229
|
-
if (
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
348
|
+
remove({ node, parentNode, isNested, }) {
|
|
349
|
+
if (!isNested && node.parentNode === parentNode) {
|
|
350
|
+
parentNode.removeChild(node);
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
text({ value, oldNode, hydrationNodes, }) {
|
|
354
|
+
if (hydrationNodes != null) {
|
|
355
|
+
let node = hydrationNodes.shift();
|
|
356
|
+
if (!node || node.nodeType !== Node.TEXT_NODE) {
|
|
357
|
+
console.warn(`Expected "${value}" while hydrating but found:`, node);
|
|
235
358
|
}
|
|
359
|
+
else {
|
|
360
|
+
// value is a text node, check if it matches the expected text
|
|
361
|
+
const textData = node.data;
|
|
362
|
+
if (textData.length > value.length) {
|
|
363
|
+
if (textData.startsWith(value)) {
|
|
364
|
+
// the text node is longer than the expected text, so we
|
|
365
|
+
// reuse the existing text node, but truncate it and unshift the rest
|
|
366
|
+
node.data = value;
|
|
367
|
+
hydrationNodes.unshift(document.createTextNode(textData.slice(value.length)));
|
|
368
|
+
return node;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
else if (textData === value) {
|
|
372
|
+
return node;
|
|
373
|
+
}
|
|
374
|
+
// We log textData and not node because node will be mutated
|
|
375
|
+
console.warn(`Expected "${value}" while hydrating but found:`, textData);
|
|
376
|
+
oldNode = node;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (oldNode != null) {
|
|
380
|
+
if (oldNode.data !== value) {
|
|
381
|
+
oldNode.data = value;
|
|
382
|
+
}
|
|
383
|
+
return oldNode;
|
|
236
384
|
}
|
|
237
|
-
return
|
|
385
|
+
return document.createTextNode(value);
|
|
238
386
|
},
|
|
239
|
-
raw(value, xmlns,
|
|
240
|
-
let
|
|
387
|
+
raw({ value, scope: xmlns, hydrationNodes, }) {
|
|
388
|
+
let nodes;
|
|
241
389
|
if (typeof value === "string") {
|
|
242
390
|
const el = xmlns == null
|
|
243
391
|
? document.createElement("div")
|
|
244
392
|
: document.createElementNS(xmlns, "svg");
|
|
245
393
|
el.innerHTML = value;
|
|
246
|
-
|
|
247
|
-
result = undefined;
|
|
248
|
-
}
|
|
249
|
-
else if (el.childNodes.length === 1) {
|
|
250
|
-
result = el.childNodes[0];
|
|
251
|
-
}
|
|
252
|
-
else {
|
|
253
|
-
result = Array.from(el.childNodes);
|
|
254
|
-
}
|
|
394
|
+
nodes = Array.from(el.childNodes);
|
|
255
395
|
}
|
|
256
396
|
else {
|
|
257
|
-
|
|
397
|
+
nodes = value == null ? [] : Array.isArray(value) ? [...value] : [value];
|
|
258
398
|
}
|
|
259
|
-
if (
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
399
|
+
if (hydrationNodes != null) {
|
|
400
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
401
|
+
const node = nodes[i];
|
|
402
|
+
// check if node is equal to the next node in the hydration array
|
|
403
|
+
const hydrationNode = hydrationNodes.shift();
|
|
404
|
+
if (hydrationNode &&
|
|
405
|
+
typeof hydrationNode === "object" &&
|
|
406
|
+
typeof hydrationNode.nodeType === "number" &&
|
|
407
|
+
node.isEqualNode(hydrationNode)) {
|
|
408
|
+
nodes[i] = hydrationNode;
|
|
269
409
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
if (result.nodeType === Node.ELEMENT_NODE ||
|
|
273
|
-
result.nodeType === Node.TEXT_NODE) {
|
|
274
|
-
hydrationData.children.shift();
|
|
410
|
+
else {
|
|
411
|
+
console.warn(`Expected <Raw value="${String(value)}"> while hydrating but found:`, hydrationNode);
|
|
275
412
|
}
|
|
276
413
|
}
|
|
277
414
|
}
|
|
278
|
-
return
|
|
415
|
+
return nodes.length === 0
|
|
416
|
+
? undefined
|
|
417
|
+
: nodes.length === 1
|
|
418
|
+
? nodes[0]
|
|
419
|
+
: nodes;
|
|
279
420
|
},
|
|
280
421
|
};
|
|
281
422
|
class DOMRenderer extends Renderer {
|
|
282
423
|
constructor() {
|
|
283
|
-
super(
|
|
424
|
+
super(adapter);
|
|
284
425
|
}
|
|
285
426
|
render(children, root, ctx) {
|
|
286
427
|
validateRoot(root);
|
|
@@ -292,12 +433,15 @@ class DOMRenderer extends Renderer {
|
|
|
292
433
|
}
|
|
293
434
|
}
|
|
294
435
|
function validateRoot(root) {
|
|
295
|
-
if (root
|
|
436
|
+
if (root == null ||
|
|
296
437
|
(typeof root === "object" && typeof root.nodeType !== "number")) {
|
|
297
|
-
throw new TypeError(`Render root is not a node. Received: ${
|
|
438
|
+
throw new TypeError(`Render root is not a node. Received: ${String(root)}`);
|
|
439
|
+
}
|
|
440
|
+
else if (root.nodeType !== Node.ELEMENT_NODE) {
|
|
441
|
+
throw new TypeError(`Render root must be an element node. Received: ${String(root)}`);
|
|
298
442
|
}
|
|
299
443
|
}
|
|
300
444
|
const renderer = new DOMRenderer();
|
|
301
445
|
|
|
302
|
-
export { DOMRenderer,
|
|
446
|
+
export { DOMRenderer, adapter, renderer };
|
|
303
447
|
//# sourceMappingURL=dom.js.map
|