This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathrender.js
49 lines (44 loc) · 1.44 KB
/
render.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
const updateProps = require('./update-props')
const SVG_TAGS = require('./svg-tags')
function render (virtualNode, options) {
let domNode
if (virtualNode.text != null) {
domNode = document.createTextNode(virtualNode.text)
} else {
const {tag, children} = virtualNode
let {props, context} = virtualNode
if (context) {
options = {refs: context.refs, listenerContext: context}
}
if (typeof tag === 'function') {
let ref
if (props && props.ref) {
ref = props.ref
}
const component = new tag(props || {}, children)
virtualNode.component = component
domNode = component.element
if (typeof ref === "function") {
ref(component)
} else if (options && options.refs && ref) {
options.refs[ref] = component
}
} else if (SVG_TAGS.has(tag)) {
domNode = document.createElementNS("http://www.w3.org/2000/svg", tag);
if (children) addChildren(domNode, children, options)
if (props) updateProps(domNode, null, virtualNode, options)
} else {
domNode = document.createElement(tag)
if (children) addChildren(domNode, children, options)
if (props) updateProps(domNode, null, virtualNode, options)
}
}
virtualNode.domNode = domNode
return domNode
}
function addChildren (parent, children, options) {
for (let i = 0; i < children.length; i++) {
parent.appendChild(render(children[i], options))
}
}
module.exports = render