UNPKG

1.31 kBJavaScriptView Raw
1import { extend } from '../util';
2
3
4/** Check if two nodes are equivalent.
5 * @param {Element} node
6 * @param {VNode} vnode
7 * @private
8 */
9export function isSameNodeType(node, vnode, hydrating) {
10 if (typeof vnode==='string' || typeof vnode==='number') {
11 return node.splitText!==undefined;
12 }
13 if (typeof vnode.nodeName==='string') {
14 return !node._componentConstructor && isNamedNode(node, vnode.nodeName);
15 }
16 return hydrating || node._componentConstructor===vnode.nodeName;
17}
18
19
20/** Check if an Element has a given normalized name.
21* @param {Element} node
22* @param {String} nodeName
23 */
24export function isNamedNode(node, nodeName) {
25 return node.normalizedNodeName===nodeName || node.nodeName.toLowerCase()===nodeName.toLowerCase();
26}
27
28
29/**
30 * Reconstruct Component-style `props` from a VNode.
31 * Ensures default/fallback values from `defaultProps`:
32 * Own-properties of `defaultProps` not present in `vnode.attributes` are added.
33 * @param {VNode} vnode
34 * @returns {Object} props
35 */
36export function getNodeProps(vnode) {
37 let props = extend({}, vnode.attributes);
38 props.children = vnode.children;
39
40 let defaultProps = vnode.nodeName.defaultProps;
41 if (defaultProps!==undefined) {
42 for (let i in defaultProps) {
43 if (props[i]===undefined) {
44 props[i] = defaultProps[i];
45 }
46 }
47 }
48
49 return props;
50}