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