/
create.js
75 lines (64 loc) · 2.34 KB
/
create.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
import { NodeCache, MiddlewareCache } from '../util/caches';
import { namespace } from '../util/svg';
import process from '../util/process';
const { CreateNodeHookCache } = MiddlewareCache;
/**
* Takes in a Virtual Tree Element (VTree) and creates a DOM Node from it.
* Sets the node into the Node cache. If this VTree already has an
* associated node, it will reuse that.
*
* @param {Object} - A Virtual Tree Element or VTree-like element
* @param {Object} - Document to create Nodes in
* @param {Boolean} - Is their a root SVG element?
* @return {Object} - A DOM Node matching the vTree
*/
export default function createNode(vTree, ownerDocument = document, isSVG) {
if (process.env.NODE_ENV !== 'production') {
if (!vTree) {
throw new Error('Missing VTree when trying to create DOM Node');
}
}
const existingNode = NodeCache.get(vTree);
// If the DOM Node was already created, reuse the existing node.
if (existingNode) {
return existingNode;
}
const { nodeName, childNodes = [] } = vTree;
isSVG = isSVG || nodeName === 'svg';
// Will vary based on the properties of the VTree.
let domNode = null;
CreateNodeHookCache.forEach((fn, retVal) => {
// Invoke all the `createNodeHook` functions passing along the vTree as the
// only argument. These functions must return a valid DOM Node value.
if (retVal = fn(vTree)) {
domNode = retVal;
}
});
if (!domNode) {
// Create empty text elements. They will get filled in during the patch
// process.
if (nodeName === '#text') {
domNode = ownerDocument.createTextNode(vTree.nodeValue);
}
// Support dynamically creating document fragments.
else if (nodeName === '#document-fragment') {
domNode = ownerDocument.createDocumentFragment();
}
// Support SVG.
else if (isSVG) {
domNode = ownerDocument.createElementNS(namespace, nodeName);
}
// If not a Text or SVG Node, then create with the standard method.
else {
domNode = ownerDocument.createElement(nodeName);
}
}
// Add to the domNodes cache.
NodeCache.set(vTree, domNode);
// Append all the children into the domNode, making sure to run them
// through this `createNode` function as well.
for (let i = 0; i < childNodes.length; i++) {
domNode.appendChild(createNode(childNodes[i], ownerDocument, isSVG));
}
return domNode;
}