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