diff options
Diffstat (limited to 'thirdparty/preact/src/vdom/index.js')
-rw-r--r-- | thirdparty/preact/src/vdom/index.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/thirdparty/preact/src/vdom/index.js b/thirdparty/preact/src/vdom/index.js new file mode 100644 index 000000000..50d4ca2b9 --- /dev/null +++ b/thirdparty/preact/src/vdom/index.js @@ -0,0 +1,50 @@ +import { clone, isString, isFunction, toLowerCase } from '../util'; +import { isFunctionalComponent } from './functional-component'; + + +/** Check if two nodes are equivalent. + * @param {Element} node + * @param {VNode} vnode + * @private + */ +export function isSameNodeType(node, vnode) { + if (isString(vnode)) { + return node instanceof Text; + } + if (isString(vnode.nodeName)) { + return isNamedNode(node, vnode.nodeName); + } + if (isFunction(vnode.nodeName)) { + return node._componentConstructor===vnode.nodeName || isFunctionalComponent(vnode); + } +} + + +export function isNamedNode(node, nodeName) { + return node.normalizedNodeName===nodeName || toLowerCase(node.nodeName)===toLowerCase(nodeName); +} + + +/** + * Reconstruct Component-style `props` from a VNode. + * Ensures default/fallback values from `defaultProps`: + * Own-properties of `defaultProps` not present in `vnode.attributes` are added. + * @param {VNode} vnode + * @returns {Object} props + */ +export function getNodeProps(vnode) { + let defaultProps = vnode.nodeName.defaultProps, + props = clone(vnode.attributes); + + if (defaultProps) { + for (let i in defaultProps) { + if (props[i]===undefined) { + props[i] = defaultProps[i]; + } + } + } + + if (vnode.children) props.children = vnode.children; + + return props; +} |