@b9g/crank 0.5.6 → 0.6.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
@@ -2,295 +2,302 @@
2
2
 
3
3
  var crank = require('./crank.cjs');
4
4
 
5
- const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
6
- const impl = {
7
- scope(xmlns, tag, props) {
8
- switch (tag) {
9
- case crank.Portal:
10
- xmlns = undefined;
11
- break;
12
- case "svg":
13
- xmlns = SVG_NAMESPACE;
14
- break;
15
- }
16
- return props.xmlns || xmlns;
17
- },
18
- create(tag, _props, xmlns) {
19
- if (typeof tag !== "string") {
20
- throw new Error(`Unknown tag: ${tag.toString()}`);
21
- }
22
- else if (tag.toLowerCase() === "svg") {
23
- xmlns = SVG_NAMESPACE;
24
- }
25
- return xmlns
26
- ? document.createElementNS(xmlns, tag)
27
- : document.createElement(tag);
28
- },
29
- hydrate(tag, node, props) {
30
- if (typeof tag !== "string" && tag !== crank.Portal) {
31
- throw new Error(`Unknown tag: ${tag.toString()}`);
32
- }
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;
38
- }
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
- }
48
- }
49
- // TODO: extract props from nodes
50
- return { props, children };
51
- },
52
- patch(_tag,
53
- // TODO: Why does this assignment work?
54
- node, name,
55
- // TODO: Stricter typings?
56
- value, oldValue, xmlns) {
57
- const isSVG = xmlns === SVG_NAMESPACE;
58
- switch (name) {
59
- case "style": {
60
- const style = node.style;
61
- if (style == null) {
62
- node.setAttribute("style", value);
63
- }
64
- else if (value == null || value === false) {
65
- node.removeAttribute("style");
66
- }
67
- else if (value === true) {
68
- node.setAttribute("style", "");
69
- }
70
- else if (typeof value === "string") {
71
- if (style.cssText !== value) {
72
- style.cssText = value;
73
- }
74
- }
75
- else {
76
- if (typeof oldValue === "string") {
77
- style.cssText = "";
78
- }
79
- for (const styleName in { ...oldValue, ...value }) {
80
- const styleValue = value && value[styleName];
81
- if (styleValue == null) {
82
- style.removeProperty(styleName);
83
- }
84
- else if (style.getPropertyValue(styleName) !== styleValue) {
85
- style.setProperty(styleName, styleValue);
86
- }
87
- }
88
- }
89
- break;
90
- }
91
- case "class":
92
- case "className":
93
- if (value === true) {
94
- node.setAttribute("class", "");
95
- }
96
- else if (value == null) {
97
- node.removeAttribute("class");
98
- }
99
- else if (!isSVG) {
100
- if (node.className !== value) {
101
- node["className"] = value;
102
- }
103
- }
104
- else if (node.getAttribute("class") !== value) {
105
- node.setAttribute("class", value);
106
- }
107
- break;
108
- case "innerHTML":
109
- if (value !== oldValue) {
110
- node.innerHTML = value;
111
- }
112
- break;
113
- default: {
114
- if (name in node &&
115
- // boolean properties will coerce strings, but sometimes they map to
116
- // enumerated attributes, where truthy strings ("false", "no") map to
117
- // falsy properties, so we use attributes in this case.
118
- !(typeof value === "string" &&
119
- typeof node[name] === "boolean")) {
120
- // walk up the object's prototype chain to find the owner of the
121
- // named property
122
- let obj = node;
123
- do {
124
- if (Object.prototype.hasOwnProperty.call(obj, name)) {
125
- break;
126
- }
127
- } while ((obj = Object.getPrototypeOf(obj)));
128
- // get the descriptor for the named property and check whether it
129
- // implies that the property is writable
130
- const descriptor = Object.getOwnPropertyDescriptor(obj, name);
131
- if (descriptor != null &&
132
- (descriptor.writable === true || descriptor.set !== undefined)) {
133
- if (node[name] !== value || oldValue === undefined) {
134
- node[name] = value;
135
- }
136
- return;
137
- }
138
- // if the property wasn't writable, fall through to the code below
139
- // which uses setAttribute() instead of assigning directly.
140
- }
141
- if (value === true) {
142
- value = "";
143
- }
144
- else if (value == null || value === false) {
145
- node.removeAttribute(name);
146
- return;
147
- }
148
- if (node.getAttribute(name) !== value) {
149
- node.setAttribute(name, value);
150
- }
151
- }
152
- }
153
- },
154
- arrange(tag, node, props, children, _oldProps, oldChildren) {
155
- if (tag === crank.Portal && (node == null || typeof node.nodeType !== "number")) {
156
- throw new TypeError(`Portal root is not a node. Received: ${JSON.stringify(node && node.toString())}`);
157
- }
158
- if (!("innerHTML" in props) &&
159
- // We don’t want to update elements without explicit children (<div/>),
160
- // because these elements sometimes have child nodes added via raw
161
- // DOM manipulations.
162
- // However, if an element has previously rendered children, we clear the
163
- // them because it would be surprising not to clear Crank managed
164
- // children, even if the new element does not have explicit children.
165
- ("children" in props || (oldChildren && oldChildren.length))) {
166
- if (children.length === 0) {
167
- node.textContent = "";
168
- }
169
- else {
170
- let oldChild = node.firstChild;
171
- let i = 0;
172
- while (oldChild !== null && i < children.length) {
173
- const newChild = children[i];
174
- if (oldChild === newChild) {
175
- oldChild = oldChild.nextSibling;
176
- i++;
177
- }
178
- else if (typeof newChild === "string") {
179
- if (oldChild.nodeType === Node.TEXT_NODE) {
180
- if (oldChild.data !== newChild) {
181
- oldChild.data = newChild;
182
- }
183
- oldChild = oldChild.nextSibling;
184
- }
185
- else {
186
- node.insertBefore(document.createTextNode(newChild), oldChild);
187
- }
188
- i++;
189
- }
190
- else if (oldChild.nodeType === Node.TEXT_NODE) {
191
- const nextSibling = oldChild.nextSibling;
192
- node.removeChild(oldChild);
193
- oldChild = nextSibling;
194
- }
195
- else {
196
- node.insertBefore(newChild, oldChild);
197
- i++;
198
- // TODO: This is an optimization but we need to think a little more about other cases like prepending.
199
- if (oldChild !== children[i]) {
200
- const nextSibling = oldChild.nextSibling;
201
- node.removeChild(oldChild);
202
- oldChild = nextSibling;
203
- }
204
- }
205
- }
206
- // remove excess DOM nodes
207
- while (oldChild !== null) {
208
- const nextSibling = oldChild.nextSibling;
209
- node.removeChild(oldChild);
210
- oldChild = nextSibling;
211
- }
212
- // append excess children
213
- for (; i < children.length; i++) {
214
- const newChild = children[i];
215
- node.appendChild(typeof newChild === "string"
216
- ? document.createTextNode(newChild)
217
- : newChild);
218
- }
219
- }
220
- }
221
- },
222
- text(text, _scope, hydrationData) {
223
- if (hydrationData != null) {
224
- let value = hydrationData.children.shift();
225
- if (typeof value !== "string" || !value.startsWith(text)) ;
226
- else if (text.length < value.length) {
227
- value = value.slice(text.length);
228
- hydrationData.children.unshift(value);
229
- }
230
- }
231
- return text;
232
- },
233
- raw(value, xmlns, hydrationData) {
234
- let result;
235
- if (typeof value === "string") {
236
- const el = xmlns == null
237
- ? document.createElement("div")
238
- : document.createElementNS(xmlns, "svg");
239
- el.innerHTML = value;
240
- if (el.childNodes.length === 0) {
241
- result = undefined;
242
- }
243
- else if (el.childNodes.length === 1) {
244
- result = el.childNodes[0];
245
- }
246
- else {
247
- result = Array.from(el.childNodes);
248
- }
249
- }
250
- else {
251
- result = value;
252
- }
253
- if (hydrationData != null) {
254
- // TODO: maybe we should warn on incorrect values
255
- if (Array.isArray(result)) {
256
- for (let i = 0; i < result.length; i++) {
257
- const node = result[i];
258
- if (typeof node !== "string" &&
259
- (node.nodeType === Node.ELEMENT_NODE ||
260
- node.nodeType === Node.TEXT_NODE)) {
261
- hydrationData.children.shift();
262
- }
263
- }
264
- }
265
- else if (result != null && typeof result !== "string") {
266
- if (result.nodeType === Node.ELEMENT_NODE ||
267
- result.nodeType === Node.TEXT_NODE) {
268
- hydrationData.children.shift();
269
- }
270
- }
271
- }
272
- return result;
273
- },
274
- };
275
- class DOMRenderer extends crank.Renderer {
276
- constructor() {
277
- super(impl);
278
- }
279
- render(children, root, ctx) {
280
- validateRoot(root);
281
- return super.render(children, root, ctx);
282
- }
283
- hydrate(children, root, ctx) {
284
- validateRoot(root);
285
- return super.hydrate(children, root, ctx);
286
- }
287
- }
288
- function validateRoot(root) {
289
- if (root === null ||
290
- (typeof root === "object" && typeof root.nodeType !== "number")) {
291
- throw new TypeError(`Render root is not a node. Received: ${JSON.stringify(root && root.toString())}`);
292
- }
293
- }
5
+ const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
6
+ const impl = {
7
+ scope(xmlns, tag, props) {
8
+ switch (tag) {
9
+ case crank.Portal:
10
+ xmlns = undefined;
11
+ break;
12
+ case "svg":
13
+ xmlns = SVG_NAMESPACE;
14
+ break;
15
+ }
16
+ return props.xmlns || xmlns;
17
+ },
18
+ create(tag, _props, xmlns) {
19
+ if (typeof tag !== "string") {
20
+ throw new Error(`Unknown tag: ${tag.toString()}`);
21
+ }
22
+ else if (tag.toLowerCase() === "svg") {
23
+ xmlns = SVG_NAMESPACE;
24
+ }
25
+ return xmlns
26
+ ? document.createElementNS(xmlns, tag)
27
+ : document.createElement(tag);
28
+ },
29
+ hydrate(tag, node, props) {
30
+ if (typeof tag !== "string" && tag !== crank.Portal) {
31
+ throw new Error(`Unknown tag: ${tag.toString()}`);
32
+ }
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;
38
+ }
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
+ }
48
+ }
49
+ // TODO: extract props from nodes
50
+ return { props, children };
51
+ },
52
+ patch(_tag,
53
+ // TODO: Why does this assignment work?
54
+ node, name,
55
+ // TODO: Stricter typings?
56
+ value, oldValue, xmlns) {
57
+ const isSVG = xmlns === SVG_NAMESPACE;
58
+ switch (name) {
59
+ case "style": {
60
+ const style = node.style;
61
+ if (style == null) {
62
+ node.setAttribute("style", value);
63
+ }
64
+ else if (value == null || value === false) {
65
+ node.removeAttribute("style");
66
+ }
67
+ else if (value === true) {
68
+ node.setAttribute("style", "");
69
+ }
70
+ else if (typeof value === "string") {
71
+ if (style.cssText !== value) {
72
+ style.cssText = value;
73
+ }
74
+ }
75
+ else {
76
+ if (typeof oldValue === "string") {
77
+ style.cssText = "";
78
+ }
79
+ for (const styleName in { ...oldValue, ...value }) {
80
+ const styleValue = value && value[styleName];
81
+ if (styleValue == null) {
82
+ style.removeProperty(styleName);
83
+ }
84
+ else if (style.getPropertyValue(styleName) !== styleValue) {
85
+ style.setProperty(styleName, styleValue);
86
+ }
87
+ }
88
+ }
89
+ break;
90
+ }
91
+ case "class":
92
+ case "className":
93
+ if (value === true) {
94
+ node.setAttribute("class", "");
95
+ }
96
+ else if (value == null) {
97
+ node.removeAttribute("class");
98
+ }
99
+ else if (!isSVG) {
100
+ if (node.className !== value) {
101
+ node["className"] = value;
102
+ }
103
+ }
104
+ else if (node.getAttribute("class") !== value) {
105
+ node.setAttribute("class", value);
106
+ }
107
+ break;
108
+ case "innerHTML":
109
+ if (value !== oldValue) {
110
+ node.innerHTML = value;
111
+ }
112
+ break;
113
+ default: {
114
+ if (name[0] === "o" &&
115
+ name[1] === "n" &&
116
+ name[2] === name[2].toUpperCase() &&
117
+ typeof value === "function") {
118
+ // Support React-style event names (onClick, onChange, etc.)
119
+ name = name.toLowerCase();
120
+ }
121
+ if (name in node &&
122
+ // boolean properties will coerce strings, but sometimes they map to
123
+ // enumerated attributes, where truthy strings ("false", "no") map to
124
+ // falsy properties, so we use attributes in this case.
125
+ !(typeof value === "string" &&
126
+ typeof node[name] === "boolean")) {
127
+ // walk up the object's prototype chain to find the owner of the
128
+ // named property
129
+ let obj = node;
130
+ do {
131
+ if (Object.prototype.hasOwnProperty.call(obj, name)) {
132
+ break;
133
+ }
134
+ } while ((obj = Object.getPrototypeOf(obj)));
135
+ // get the descriptor for the named property and check whether it
136
+ // implies that the property is writable
137
+ const descriptor = Object.getOwnPropertyDescriptor(obj, name);
138
+ if (descriptor != null &&
139
+ (descriptor.writable === true || descriptor.set !== undefined)) {
140
+ if (node[name] !== value || oldValue === undefined) {
141
+ node[name] = value;
142
+ }
143
+ return;
144
+ }
145
+ // if the property wasn't writable, fall through to the code below
146
+ // which uses setAttribute() instead of assigning directly.
147
+ }
148
+ if (value === true) {
149
+ value = "";
150
+ }
151
+ else if (value == null || value === false) {
152
+ node.removeAttribute(name);
153
+ return;
154
+ }
155
+ if (node.getAttribute(name) !== value) {
156
+ node.setAttribute(name, value);
157
+ }
158
+ }
159
+ }
160
+ },
161
+ arrange(tag, node, props, children, _oldProps, oldChildren) {
162
+ if (tag === crank.Portal && (node == null || typeof node.nodeType !== "number")) {
163
+ throw new TypeError(`Portal root is not a node. Received: ${JSON.stringify(node && node.toString())}`);
164
+ }
165
+ if (!("innerHTML" in props) &&
166
+ // We don’t want to update elements without explicit children (<div/>),
167
+ // because these elements sometimes have child nodes added via raw
168
+ // DOM manipulations.
169
+ // However, if an element has previously rendered children, we clear the
170
+ // them because it would be surprising not to clear Crank managed
171
+ // children, even if the new element does not have explicit children.
172
+ ("children" in props || (oldChildren && oldChildren.length))) {
173
+ if (children.length === 0) {
174
+ node.textContent = "";
175
+ }
176
+ else {
177
+ let oldChild = node.firstChild;
178
+ let i = 0;
179
+ while (oldChild !== null && i < children.length) {
180
+ const newChild = children[i];
181
+ if (oldChild === newChild) {
182
+ oldChild = oldChild.nextSibling;
183
+ i++;
184
+ }
185
+ else if (typeof newChild === "string") {
186
+ if (oldChild.nodeType === Node.TEXT_NODE) {
187
+ if (oldChild.data !== newChild) {
188
+ oldChild.data = newChild;
189
+ }
190
+ oldChild = oldChild.nextSibling;
191
+ }
192
+ else {
193
+ node.insertBefore(document.createTextNode(newChild), oldChild);
194
+ }
195
+ i++;
196
+ }
197
+ else if (oldChild.nodeType === Node.TEXT_NODE) {
198
+ const nextSibling = oldChild.nextSibling;
199
+ node.removeChild(oldChild);
200
+ oldChild = nextSibling;
201
+ }
202
+ else {
203
+ node.insertBefore(newChild, oldChild);
204
+ i++;
205
+ // TODO: This is an optimization but we need to think a little more about other cases like prepending.
206
+ if (oldChild !== children[i]) {
207
+ const nextSibling = oldChild.nextSibling;
208
+ node.removeChild(oldChild);
209
+ oldChild = nextSibling;
210
+ }
211
+ }
212
+ }
213
+ // remove excess DOM nodes
214
+ while (oldChild !== null) {
215
+ const nextSibling = oldChild.nextSibling;
216
+ node.removeChild(oldChild);
217
+ oldChild = nextSibling;
218
+ }
219
+ // append excess children
220
+ for (; i < children.length; i++) {
221
+ const newChild = children[i];
222
+ node.appendChild(typeof newChild === "string"
223
+ ? document.createTextNode(newChild)
224
+ : newChild);
225
+ }
226
+ }
227
+ }
228
+ },
229
+ text(text, _scope, hydrationData) {
230
+ if (hydrationData != null) {
231
+ let value = hydrationData.children.shift();
232
+ if (typeof value !== "string" || !value.startsWith(text)) ;
233
+ else if (text.length < value.length) {
234
+ value = value.slice(text.length);
235
+ hydrationData.children.unshift(value);
236
+ }
237
+ }
238
+ return text;
239
+ },
240
+ raw(value, xmlns, hydrationData) {
241
+ let result;
242
+ if (typeof value === "string") {
243
+ const el = xmlns == null
244
+ ? document.createElement("div")
245
+ : document.createElementNS(xmlns, "svg");
246
+ el.innerHTML = value;
247
+ if (el.childNodes.length === 0) {
248
+ result = undefined;
249
+ }
250
+ else if (el.childNodes.length === 1) {
251
+ result = el.childNodes[0];
252
+ }
253
+ else {
254
+ result = Array.from(el.childNodes);
255
+ }
256
+ }
257
+ else {
258
+ result = value;
259
+ }
260
+ if (hydrationData != null) {
261
+ // TODO: maybe we should warn on incorrect values
262
+ if (Array.isArray(result)) {
263
+ for (let i = 0; i < result.length; i++) {
264
+ const node = result[i];
265
+ if (typeof node !== "string" &&
266
+ (node.nodeType === Node.ELEMENT_NODE ||
267
+ node.nodeType === Node.TEXT_NODE)) {
268
+ hydrationData.children.shift();
269
+ }
270
+ }
271
+ }
272
+ else if (result != null && typeof result !== "string") {
273
+ if (result.nodeType === Node.ELEMENT_NODE ||
274
+ result.nodeType === Node.TEXT_NODE) {
275
+ hydrationData.children.shift();
276
+ }
277
+ }
278
+ }
279
+ return result;
280
+ },
281
+ };
282
+ class DOMRenderer extends crank.Renderer {
283
+ constructor() {
284
+ super(impl);
285
+ }
286
+ render(children, root, ctx) {
287
+ validateRoot(root);
288
+ return super.render(children, root, ctx);
289
+ }
290
+ hydrate(children, root, ctx) {
291
+ validateRoot(root);
292
+ return super.hydrate(children, root, ctx);
293
+ }
294
+ }
295
+ function validateRoot(root) {
296
+ if (root === null ||
297
+ (typeof root === "object" && typeof root.nodeType !== "number")) {
298
+ throw new TypeError(`Render root is not a node. Received: ${JSON.stringify(root && root.toString())}`);
299
+ }
300
+ }
294
301
  const renderer = new DOMRenderer();
295
302
 
296
303
  exports.DOMRenderer = DOMRenderer;