-
Notifications
You must be signed in to change notification settings - Fork 10
/
dom-handlers.js
402 lines (381 loc) · 13.5 KB
/
dom-handlers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import { startRenderSubscription } from '.';
import {
findRenderableByVDOMPointer,
isChildVDOMPointer,
findRootVDOMPointers,
isPrimitiveElement,
} from './vdom-helpers';
import { diffType, propsDiffType, diffApplicationOrder } from './diff';
// map of eventHandlers that the ToDo app requires
const eventHandlersMap = {
onClick: 'click',
// for the `change` event to trigger, the user is required to leave the field and come back
// so it seems like React decided to use the `input` event under the hood
onChange: 'input',
onSubmit: 'submit',
};
const isEventHandlerProp = key => Object.keys(eventHandlersMap).includes(key);
const addEventHandler = (domElement, { key, value }) => {
domElement.addEventListener(eventHandlersMap[key], value);
};
const removeEventHandler = (domElement, { key, value }) => {
domElement.removeEventListener(eventHandlersMap[key], value);
};
const applyPropToHTMLElement = ({ key, value }, element) => {
if (isEventHandlerProp(key)) {
addEventHandler(element, { key, value });
return;
}
if (element[key] !== undefined) {
element[key] = value;
} else {
element.setAttribute(key, value);
}
};
const removePropFromHTMLElement = ({ key, oldValue }, element) => {
if (isEventHandlerProp(key)) {
removeEventHandler(renderedElementsMap[VDOMPointer], { key, oldValue });
return;
}
element.removeAttribute(key);
};
// expects a JSX element and returns an HTML element
const renderTagElementToHtml = (
{ props: { children, ...props }, type },
renderedElementsMap,
) => {
const domElement = document.createElement(type);
Object.entries(props).forEach(([key, value]) => {
applyPropToHTMLElement({ key, value }, domElement);
});
if (children) {
const childrenAsDomElement = children.map(child =>
renderElementToHtml(child, renderedElementsMap),
);
childrenAsDomElement.forEach(childElement => {
if (childElement) {
domElement.appendChild(childElement);
}
});
}
return domElement;
};
const renderPrimitiveToHtml = ({ value }) => {
switch (typeof value) {
case 'string':
case 'number':
return document.createTextNode(value);
case 'undefined':
return;
case 'boolean':
return value ? renderPrimitiveToHtml('true') : undefined;
default:
throw new Error(`Type ${value} is not a known renderable type.`);
}
};
const renderElementToHtml = (element, renderedElementsMap) => {
const renderedElement = isPrimitiveElement(element)
? renderPrimitiveToHtml(element)
: renderTagElementToHtml(element, renderedElementsMap);
renderedElementsMap[element.VDOMPointer] = renderedElement;
return renderedElement;
};
/*
This is a helper function to find rendered children pertaining to a specific VDOMPointer
It is especially useful to find children of a component, as the component itself is not rendered
within the browser's DOM, it makes retrieving its children a non-trivial exercise.
Input:
- renderedElementsMap: a map with keys VDOMPointer and values elements rendered in the browser
- VDOMPointer: pointer to the element (most useful for a component) which children you want to retrieve
Output:
- renderedChildren: an array of all the children rendered to the browser's DOM for the element at the provided pointer
*/
const findRenderedChildrenByVDOMPointer = (
renderedElementsMap,
VDOMPointer,
) => {
return Object.entries(renderedElementsMap).filter(([pointer]) =>
isChildVDOMPointer(pointer, VDOMPointer),
);
};
/*
This is a helper function to retrieve the insertion point of an element within the
browser's DOM (which isn't trivial, as some elements such as `false` or `undefined` are part of the
renderable VDOM, but not part of the actual DOM).
Input
- {
renderedElementsMap: a map with keys VDOMPointer and values elements rendered in the browser
renderableVDOM: the renderableVDOM for the current update cycle
}
- VDOMPointer: the pointer to the element for which we want to find the next sibling
- parentPointer: the pointer to the parent of the VDOMPointer and the sibling
Output
- The next sibling of the VDOMPointer: a dom element currently rendered in the browser if found, undefined
if the node pointed by the VDOMPointer is the last rendered child of the parent
*/
const findNextSiblingOfVDOMPointer = (
{ renderedElementsMap, renderableVDOM },
VDOMPointer,
parentPointer,
) => {
/*
Given the following VDOM
<App>
<div>
<Component>
<div>
<Component>
<span>First child</span>
<span>Second child</span>
</Component>
<span>Hello world!</span>
</div>
</Component>
</div>
</App>
we get the following renderableVDOM (with VDOMPointer)
<div> [0]
<div> [0, 0, 0]
<span>First child</span> [0, 0, 0, 0, 0]
<span>Second child</span> [0, 0, 0, 0, 1]
<span>Hello world!</span> [0, 0, 0, 1]
</div>
</div>
So that the sibling of VDOMPointer [0, 0, 0, 1] in the renderableVDOM
is the span Second child with VDOMPointer [0, 0, 0, 0, 1]
*/
const parentElementFromRenderableVDOM = findRenderableByVDOMPointer(
renderableVDOM,
parentPointer,
);
const parentChildren = parentElementFromRenderableVDOM.props.children;
const childVDOMIndex = parentChildren.findIndex(
child => child.VDOMPointer === VDOMPointer,
);
let nextSiblingVDOMIndex = childVDOMIndex + 1;
let nextSibling;
// The next sibling could be a value that doesn't render to the DOM such as `false`
// or a sibling that has not yet been rendered to the DOM (if it was added in the same update cycle)
// so we need to continue searching for the first rendered one here
while (
nextSiblingVDOMIndex < parentChildren.length &&
nextSibling === undefined
) {
const nextSiblingFromVDOM =
parentElementFromRenderableVDOM.props.children[nextSiblingVDOMIndex];
if (nextSiblingFromVDOM) {
nextSibling = renderedElementsMap[nextSiblingFromVDOM.VDOMPointer];
}
nextSiblingVDOMIndex += 1;
}
return nextSibling;
};
/*
Function to apply to the browser's DOM a node removed diff item
Input:
- {
renderedElementsMap: a map with keys VDOMPointer and values elements rendered in the browser
}
- VDOMPointer: the pointer to the position for the node that should be removed
Side effects:
Removes the node from the browser's DOM, updates the renderedElementsMap to reflect the changes
*/
const applyNodeRemoved = ({ renderedElementsMap }, { VDOMPointer }) => {
const elementToRemove = renderedElementsMap[VDOMPointer];
if (elementToRemove) {
elementToRemove.parentNode.removeChild(elementToRemove);
findRenderedChildrenByVDOMPointer(renderedElementsMap, VDOMPointer).forEach(
([pointer]) => {
delete renderedElementsMap[pointer];
},
);
} else {
// for a pointer to a component at 0,1,2
// and rendered children 0,1,2,0 and 0,1,2,1,0
// we want to remove those children
const allChildren = findRenderedChildrenByVDOMPointer(
renderedElementsMap,
VDOMPointer,
);
const rootVDOMPointers = findRootVDOMPointers(
allChildren.map(([pointer]) => pointer),
);
rootVDOMPointers.forEach(pointer => {
applyNodeRemoved({ renderedElementsMap }, { VDOMPointer: pointer });
});
}
delete renderedElementsMap[VDOMPointer];
};
/*
Function to apply to the browser's DOM a node added diff item
Input:
- {
renderedElementsMap: a map with keys VDOMPointer and values elements rendered in the browser
renderableVDOM: the renderableVDOM for the current update cycle
}
- VDOMPointer: the pointer to the position at which we want to insert the node
- node: the renderableVDOMElement we want to insert into the DOM
- parentPointer: the pointer to the parent of the VDOMPointer and the sibling
Side effects:
Updates the browser's DOM with the newly added node as well as the reference in the renderedElementsMap.
*/
const applyNodeAdded = (
{ renderedElementsMap, renderableVDOM },
{ VDOMPointer, payload: { node, parentPointer } },
) => {
const parentElement = renderedElementsMap[parentPointer];
const nextSibling = findNextSiblingOfVDOMPointer(
{ renderedElementsMap, renderableVDOM },
VDOMPointer,
parentPointer,
);
const addedElement = renderElementToHtml(node, renderedElementsMap);
// The addedElement could be a value that doesn't render to the DOM such as `false`
if (!addedElement) {
renderedElementsMap[VDOMPointer] = addedElement;
return;
}
parentElement.insertBefore(addedElement, nextSibling);
renderedElementsMap[VDOMPointer] = addedElement;
};
/*
Function to apply to the browser's DOM a node replaced diff item
Input:
- {
renderedElementsMap: a map with keys VDOMPointer and values elements rendered in the browser
renderableVDOM: the renderableVDOM for the current update cycle
}
- VDOMPointer: the pointer to the position at which we want to replace the node
- newNode: the renderableVDOMElement we want the old node to be replaced with
- parentPointer: the pointer to the parent of the VDOMPointer
Side effects:
Replace the node old node with the node newNode in the browser's DOM,
updates the renderedElementsMap to reflect the changes
*/
const applyNodeReplaced = (
{ renderedElementsMap, renderableVDOM },
{ VDOMPointer, payload: { newNode, parentPointer } },
) => {
applyNodeRemoved(
{ renderedElementsMap, renderableVDOM },
{ VDOMPointer, payload: { parentPointer } },
);
applyNodeAdded(
{ renderedElementsMap, renderableVDOM },
{ VDOMPointer, payload: { node: newNode, parentPointer } },
);
};
/*
Function to apply to the browser's DOM a primitive node update diff item
Input:
- {
renderedElementsMap: a map with keys VDOMPointer and values elements rendered in the browser
}
- VDOMPointer: the pointer to the position at which we want to insert the node
- newElement: the renderableVDOMElement primitive element we want to update the browser's DOM with
Side effects:
Updates the browser's DOM
*/
const applyPrimitiveNodeUpdate = (
{ renderedElementsMap },
{ VDOMPointer, payload: { newElement } },
) => {
renderedElementsMap[VDOMPointer].nodeValue = newElement.value;
};
/*
Function to apply to the browser's DOM a props diff item
Input:
- {
renderedElementsMap: a map with keys VDOMPointer and values elements rendered in the browser
}
- VDOMPointer: the pointer to the element which props we want to update
- propsChanged: the list of prop diff item we want to apply
Side effects:
Updates the browser's DOM with the applied props
*/
const applyProps = (
{ renderedElementsMap },
{ VDOMPointer, payload: propsChanged },
) => {
// Loop through all prop diff items
Object.entries(propsChanged).forEach(
// Extracting for each the propDiffType (updated or removed)
// As well as the oldValue and the newValue for the given prop
// Example of what each of those values could be:
// [key = 'id', [propdDiffType = 'updated', { oldValue: 'main-title', newValue: 'sub-title' }]]
([key, [propDiffType, { oldValue, newValue }]]) => {
if (propDiffType === propsDiffType.updated) {
if (isEventHandlerProp(key)) {
removeEventHandler(renderedElementsMap[VDOMPointer], {
key,
value: oldValue,
});
}
applyPropToHTMLElement(
{ key, value: newValue },
renderedElementsMap[VDOMPointer],
);
} else if (propDiffType === propsDiffType.removed) {
removePropFromHTMLElement(
{ key, oldValue },
renderedElementsMap[VDOMPointer],
);
}
},
);
};
// A map of applicators to simplify application
const diffApplicators = {
[diffType.nodeRemoved]: applyNodeRemoved,
[diffType.nodeAdded]: applyNodeAdded,
[diffType.nodeReplaced]: applyNodeReplaced,
[diffType.primitiveNodeUpdate]: applyPrimitiveNodeUpdate,
[diffType.props]: applyProps,
};
/*
Function to apply the diff to the browser's DOM
Input:
- diff: the diff we want to apply to the DOM
- renderedElementsMap: a map with keys VDOMPointer and values elements rendered in the browser
- renderableVDOM: the renderableVDOM for the current update cycle
Side effects:
Updates the browser's DOM as well as the references in the renderedElementsMap.
*/
const applyDiff = (diff, renderedElementsMap, renderableVDOM) => {
// Sort diff item so applying them is safe
const sortedDiffs = diff.sort(
(a, b) =>
diffApplicationOrder.indexOf(a.type) -
diffApplicationOrder.indexOf(b.type),
);
// Apply them all!
sortedDiffs.forEach(diffItem =>
diffApplicators[diffItem.type](
{ renderedElementsMap, renderableVDOM },
diffItem,
),
);
};
const createRoot = rootElement => ({
rootElement,
render: rootChild => {
let renderedElementsMap = {};
// subscribes to DOM changes which are driven by state changes
startRenderSubscription(rootChild, (renderableVDOM, diff) => {
if (Object.keys(renderedElementsMap).length === 0) {
const rootChildAsHTML = renderElementToHtml(
renderableVDOM,
renderedElementsMap,
);
rootElement.appendChild(rootChildAsHTML);
} else {
// Now on update instead of replacing the whole browser's DOM
// we will try to only do targeted updates of it
applyDiff(diff, renderedElementsMap, renderableVDOM);
}
});
},
});
export const DOMHandlers = {
createRoot,
};