@b9g/crank 0.6.1 → 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 +1671 -1311
- package/crank.cjs.map +1 -1
- package/crank.d.ts +229 -248
- package/crank.js +1671 -1312
- package/crank.js.map +1 -1
- package/dom.cjs +350 -230
- package/dom.cjs.map +1 -1
- package/dom.d.ts +5 -5
- package/dom.js +350 -230
- 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 +7 -23
- package/html.cjs.map +1 -1
- package/html.d.ts +3 -3
- package/html.js +7 -23
- 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 +3 -2
- package/jsx-tag.cjs.map +1 -1
- package/jsx-tag.js +3 -2
- package/jsx-tag.js.map +1 -1
- package/package.json +17 -1
- package/standalone.cjs +2 -0
- package/standalone.cjs.map +1 -1
- package/standalone.js +2 -1
- package/standalone.js.map +1 -1
- package/umd.js +2268 -1566
- package/umd.js.map +1 -1
package/dom.js
CHANGED
|
@@ -1,11 +1,52 @@
|
|
|
1
1
|
/// <reference types="dom.d.ts" />
|
|
2
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,286 +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
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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;
|
|
103
|
+
}
|
|
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;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
switch (name) {
|
|
140
|
+
// TODO: fix hydration warnings for the style prop
|
|
141
|
+
case "style": {
|
|
142
|
+
const style = element.style;
|
|
66
143
|
if (value == null || value === false) {
|
|
67
|
-
|
|
144
|
+
if (isHydrating && style.cssText !== "") {
|
|
145
|
+
emitHydrationWarning(name, quietProps, value, style.cssText, element);
|
|
146
|
+
}
|
|
147
|
+
element.removeAttribute("style");
|
|
68
148
|
}
|
|
69
149
|
else if (value === true) {
|
|
70
|
-
|
|
150
|
+
if (isHydrating && style.cssText !== "") {
|
|
151
|
+
emitHydrationWarning(name, quietProps, "", style.cssText, element);
|
|
152
|
+
}
|
|
153
|
+
element.setAttribute("style", "");
|
|
71
154
|
}
|
|
72
155
|
else if (typeof value === "string") {
|
|
73
|
-
|
|
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;
|
|
168
|
+
}
|
|
74
169
|
}
|
|
75
170
|
else {
|
|
76
|
-
|
|
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
|
+
}
|
|
77
199
|
}
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
switch (name) {
|
|
82
|
-
case "style": {
|
|
83
|
-
const style = node.style;
|
|
84
|
-
if (style == null) {
|
|
85
|
-
node.setAttribute("style", value);
|
|
86
|
-
}
|
|
87
|
-
else if (value == null || value === false) {
|
|
88
|
-
node.removeAttribute("style");
|
|
89
|
-
}
|
|
90
|
-
else if (value === true) {
|
|
91
|
-
node.setAttribute("style", "");
|
|
200
|
+
break;
|
|
92
201
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
202
|
+
case "class":
|
|
203
|
+
case "className":
|
|
204
|
+
if (value === true) {
|
|
205
|
+
if (isHydrating && element.getAttribute("class") !== "") {
|
|
206
|
+
emitHydrationWarning(name, quietProps, "", element.getAttribute("class"), element);
|
|
207
|
+
}
|
|
208
|
+
element.setAttribute("class", "");
|
|
96
209
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
210
|
+
else if (value == null) {
|
|
211
|
+
if (isHydrating && element.hasAttribute("class")) {
|
|
212
|
+
emitHydrationWarning(name, quietProps, value, element.getAttribute("class"), element);
|
|
213
|
+
}
|
|
214
|
+
element.removeAttribute("class");
|
|
101
215
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (
|
|
105
|
-
|
|
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
|
+
}
|
|
106
243
|
}
|
|
107
|
-
|
|
108
|
-
|
|
244
|
+
if (shouldIssueWarning ||
|
|
245
|
+
(hydratingClasses && hydratingClasses.size > 0)) {
|
|
246
|
+
emitHydrationWarning(name, quietProps, Object.keys(value)
|
|
247
|
+
.filter((k) => value[k])
|
|
248
|
+
.join(" "), hydratingClassName || "", element);
|
|
109
249
|
}
|
|
110
250
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
else if (value == null) {
|
|
120
|
-
node.removeAttribute("class");
|
|
121
|
-
}
|
|
122
|
-
else if (!isSVG) {
|
|
123
|
-
if (node.className !== value) {
|
|
124
|
-
node["className"] = value;
|
|
251
|
+
else if (!isSVG) {
|
|
252
|
+
if (element.className !== value) {
|
|
253
|
+
if (isHydrating) {
|
|
254
|
+
emitHydrationWarning(name, quietProps, value, element.className, element);
|
|
255
|
+
}
|
|
256
|
+
element.className = value;
|
|
257
|
+
}
|
|
125
258
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
break;
|
|
131
|
-
case "innerHTML":
|
|
132
|
-
if (value !== oldValue) {
|
|
133
|
-
node.innerHTML = value;
|
|
134
|
-
}
|
|
135
|
-
break;
|
|
136
|
-
default: {
|
|
137
|
-
if (name[0] === "o" &&
|
|
138
|
-
name[1] === "n" &&
|
|
139
|
-
name[2] === name[2].toUpperCase() &&
|
|
140
|
-
typeof value === "function") {
|
|
141
|
-
// Support React-style event names (onClick, onChange, etc.)
|
|
142
|
-
name = name.toLowerCase();
|
|
143
|
-
}
|
|
144
|
-
if (name in node &&
|
|
145
|
-
// boolean properties will coerce strings, but sometimes they map to
|
|
146
|
-
// enumerated attributes, where truthy strings ("false", "no") map to
|
|
147
|
-
// falsy properties, so we use attributes in this case.
|
|
148
|
-
!(typeof value === "string" &&
|
|
149
|
-
typeof node[name] === "boolean")) {
|
|
150
|
-
// walk up the object's prototype chain to find the owner of the
|
|
151
|
-
// named property
|
|
152
|
-
let obj = node;
|
|
153
|
-
do {
|
|
154
|
-
if (Object.prototype.hasOwnProperty.call(obj, name)) {
|
|
155
|
-
break;
|
|
259
|
+
else if (element.getAttribute("class") !== value) {
|
|
260
|
+
if (isHydrating) {
|
|
261
|
+
emitHydrationWarning(name, quietProps, value, element.getAttribute("class"), element);
|
|
156
262
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (
|
|
162
|
-
(
|
|
163
|
-
|
|
164
|
-
node[name] = value;
|
|
263
|
+
element.setAttribute("class", value);
|
|
264
|
+
}
|
|
265
|
+
break;
|
|
266
|
+
case "innerHTML":
|
|
267
|
+
if (value !== oldValue) {
|
|
268
|
+
if (isHydrating) {
|
|
269
|
+
emitHydrationWarning(name, quietProps, value, element.innerHTML, element);
|
|
165
270
|
}
|
|
166
|
-
|
|
271
|
+
element.innerHTML = value;
|
|
167
272
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
if (node.getAttribute(name) !== value) {
|
|
179
|
-
node.setAttribute(name, value);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
},
|
|
184
|
-
arrange(tag, node, props, children, _oldProps, oldChildren) {
|
|
185
|
-
if (tag === Portal && (node == null || typeof node.nodeType !== "number")) {
|
|
186
|
-
throw new TypeError(`Portal root is not a node. Received: ${JSON.stringify(node && node.toString())}`);
|
|
187
|
-
}
|
|
188
|
-
if (!("innerHTML" in props) &&
|
|
189
|
-
// We don’t want to update elements without explicit children (<div/>),
|
|
190
|
-
// because these elements sometimes have child nodes added via raw
|
|
191
|
-
// DOM manipulations.
|
|
192
|
-
// However, if an element has previously rendered children, we clear the
|
|
193
|
-
// them because it would be surprising not to clear Crank managed
|
|
194
|
-
// children, even if the new element does not have explicit children.
|
|
195
|
-
("children" in props || (oldChildren && oldChildren.length))) {
|
|
196
|
-
if (children.length === 0) {
|
|
197
|
-
node.textContent = "";
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
let oldChild = node.firstChild;
|
|
201
|
-
let i = 0;
|
|
202
|
-
while (oldChild !== null && i < children.length) {
|
|
203
|
-
const newChild = children[i];
|
|
204
|
-
if (oldChild === newChild) {
|
|
205
|
-
oldChild = oldChild.nextSibling;
|
|
206
|
-
i++;
|
|
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();
|
|
207
281
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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);
|
|
212
295
|
}
|
|
213
|
-
|
|
296
|
+
// if the property is writable, assign it directly
|
|
297
|
+
element[name] = value;
|
|
214
298
|
}
|
|
215
|
-
|
|
216
|
-
|
|
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);
|
|
217
307
|
}
|
|
218
|
-
|
|
308
|
+
element.removeAttribute(name);
|
|
309
|
+
continue;
|
|
219
310
|
}
|
|
220
|
-
else if (
|
|
221
|
-
|
|
222
|
-
node.removeChild(oldChild);
|
|
223
|
-
oldChild = nextSibling;
|
|
311
|
+
else if (typeof value !== "string") {
|
|
312
|
+
value = String(value);
|
|
224
313
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
// TODO: This is an optimization but we need to think a little more about other cases like prepending.
|
|
229
|
-
if (oldChild !== children[i]) {
|
|
230
|
-
const nextSibling = oldChild.nextSibling;
|
|
231
|
-
node.removeChild(oldChild);
|
|
232
|
-
oldChild = nextSibling;
|
|
314
|
+
if (element.getAttribute(name) !== value) {
|
|
315
|
+
if (isHydrating) {
|
|
316
|
+
emitHydrationWarning(name, quietProps, value, element.getAttribute(name), element);
|
|
233
317
|
}
|
|
318
|
+
element.setAttribute(name, value);
|
|
234
319
|
}
|
|
235
320
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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;
|
|
241
335
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
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
|
+
}
|
|
248
344
|
}
|
|
249
345
|
}
|
|
250
346
|
}
|
|
251
347
|
},
|
|
252
|
-
|
|
253
|
-
if (
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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);
|
|
259
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;
|
|
260
384
|
}
|
|
261
|
-
return
|
|
385
|
+
return document.createTextNode(value);
|
|
262
386
|
},
|
|
263
|
-
raw(value, xmlns,
|
|
264
|
-
let
|
|
387
|
+
raw({ value, scope: xmlns, hydrationNodes, }) {
|
|
388
|
+
let nodes;
|
|
265
389
|
if (typeof value === "string") {
|
|
266
390
|
const el = xmlns == null
|
|
267
391
|
? document.createElement("div")
|
|
268
392
|
: document.createElementNS(xmlns, "svg");
|
|
269
393
|
el.innerHTML = value;
|
|
270
|
-
|
|
271
|
-
result = undefined;
|
|
272
|
-
}
|
|
273
|
-
else if (el.childNodes.length === 1) {
|
|
274
|
-
result = el.childNodes[0];
|
|
275
|
-
}
|
|
276
|
-
else {
|
|
277
|
-
result = Array.from(el.childNodes);
|
|
278
|
-
}
|
|
394
|
+
nodes = Array.from(el.childNodes);
|
|
279
395
|
}
|
|
280
396
|
else {
|
|
281
|
-
|
|
397
|
+
nodes = value == null ? [] : Array.isArray(value) ? [...value] : [value];
|
|
282
398
|
}
|
|
283
|
-
if (
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
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;
|
|
293
409
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
if (result.nodeType === Node.ELEMENT_NODE ||
|
|
297
|
-
result.nodeType === Node.TEXT_NODE) {
|
|
298
|
-
hydrationData.children.shift();
|
|
410
|
+
else {
|
|
411
|
+
console.warn(`Expected <Raw value="${String(value)}"> while hydrating but found:`, hydrationNode);
|
|
299
412
|
}
|
|
300
413
|
}
|
|
301
414
|
}
|
|
302
|
-
return
|
|
415
|
+
return nodes.length === 0
|
|
416
|
+
? undefined
|
|
417
|
+
: nodes.length === 1
|
|
418
|
+
? nodes[0]
|
|
419
|
+
: nodes;
|
|
303
420
|
},
|
|
304
421
|
};
|
|
305
422
|
class DOMRenderer extends Renderer {
|
|
306
423
|
constructor() {
|
|
307
|
-
super(
|
|
424
|
+
super(adapter);
|
|
308
425
|
}
|
|
309
426
|
render(children, root, ctx) {
|
|
310
427
|
validateRoot(root);
|
|
@@ -316,12 +433,15 @@ class DOMRenderer extends Renderer {
|
|
|
316
433
|
}
|
|
317
434
|
}
|
|
318
435
|
function validateRoot(root) {
|
|
319
|
-
if (root
|
|
436
|
+
if (root == null ||
|
|
320
437
|
(typeof root === "object" && typeof root.nodeType !== "number")) {
|
|
321
|
-
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)}`);
|
|
322
442
|
}
|
|
323
443
|
}
|
|
324
444
|
const renderer = new DOMRenderer();
|
|
325
445
|
|
|
326
|
-
export { DOMRenderer,
|
|
446
|
+
export { DOMRenderer, adapter, renderer };
|
|
327
447
|
//# sourceMappingURL=dom.js.map
|