/** * Production h() function based on Preact by * Jason Miller (@developit) * Licensed under the MIT License * https://github.com/developit/preact/blob/master/LICENSE * * Modified for Stencil's compiler and vdom */ import { BUILD } from '@app-data'; import { consoleDevError, consoleDevWarn, consoleError, transformTag } from '@platform'; import { isComplexType } from '../../utils/helpers'; import type * as d from '../../declarations'; // export function h(nodeName: string | d.FunctionalComponent, vnodeData: d.PropsType, child?: d.ChildType): d.VNode; // export function h(nodeName: string | d.FunctionalComponent, vnodeData: d.PropsType, ...children: d.ChildType[]): d.VNode; export const h = (nodeName: any, vnodeData: any, ...children: d.ChildType[]): d.VNode => { // custom element names must contain a hyphen; native elements are never transformed if (typeof nodeName === 'string' && nodeName.includes('-')) { nodeName = transformTag(nodeName); } let child = null; let key: string = null; let slotName: string = null; let simple = false; let lastSimple = false; const vNodeChildren: d.VNode[] = []; const walk = (c: any[]) => { for (let i = 0; i < c.length; i++) { child = c[i]; if (Array.isArray(child)) { walk(child); } else if (child != null && typeof child !== 'boolean') { if ((simple = typeof nodeName !== 'function' && !isComplexType(child))) { child = String(child); } else if (typeof nodeName !== 'function' && child.$flags$ === undefined) { // not a primitive and not a VNode - drop it if (BUILD.isDev) { consoleDevError(`vNode passed as children has unexpected type. Make sure it's using the correct h() function. Empty objects can also be the cause, look for JSX comments that became objects.`); } else { consoleError('Invalid vNode child'); } continue; } if (simple && lastSimple) { // If the previous child was simple (string), we merge both vNodeChildren[vNodeChildren.length - 1].$text$ += child; } else { // Append a new vNode, if it's text, we create a text vNode vNodeChildren.push(simple ? newVNode(null, child) : child); } lastSimple = simple; } } }; walk(children); if (vnodeData) { if (BUILD.isDev && nodeName === 'input') { validateInputProperties(vnodeData); } if (BUILD.vdomKey && vnodeData.key) { key = vnodeData.key; } if (BUILD.slotRelocation && vnodeData.name) { slotName = vnodeData.name; } // normalize class / className attributes if (BUILD.vdomClass) { const classData = vnodeData.className || vnodeData.class; if (classData) { vnodeData.class = typeof classData !== 'object' ? classData : Object.keys(classData) .filter((k) => classData[k]) .join(' '); } } } if (BUILD.isDev && vNodeChildren.some(isHost)) { consoleDevError(`The must be the single root component. Make sure: - You are NOT using hostData() and in the same component. - is used once, and it's the single root component of the render() function.`); } if (BUILD.vdomFunctional && typeof nodeName === 'function') { // nodeName is a functional component return (nodeName as d.FunctionalComponent)( vnodeData === null ? {} : vnodeData, vNodeChildren, vdomFnUtils, ) as any; } const vnode = newVNode(nodeName, null); vnode.$attrs$ = vnodeData; if (vNodeChildren.length > 0) { vnode.$children$ = vNodeChildren; } if (BUILD.vdomKey) { vnode.$key$ = key; } if (BUILD.slotRelocation) { vnode.$name$ = slotName; } return vnode; }; /** * A utility function for creating a virtual DOM node from a tag and some * possible text content. * * @param tag the tag for this element * @param text possible text content for the node * @returns a newly-minted virtual DOM node */ export const newVNode = (tag: string, text: string) => { const vnode: d.VNode = { $flags$: 0, $tag$: tag, // Normalize undefined to null to prevent rendering "undefined" as text $text$: text ?? null, $elm$: null, $children$: null, }; if (BUILD.vdomAttribute) { vnode.$attrs$ = null; } if (BUILD.vdomKey) { vnode.$key$ = null; } if (BUILD.slotRelocation) { vnode.$name$ = null; } return vnode; }; export const Host = {}; /** * Check whether a given node is a Host node or not * * @param node the virtual DOM node to check * @returns whether it's a Host node or not */ export const isHost = (node: any): node is d.VNode => node && node.$tag$ === Host; /** * Implementation of {@link d.FunctionalUtilities} for Stencil's VDom. * * Note that these functions convert from {@link d.VNode} to * {@link d.ChildNode} to give functional component developers a friendly * interface. */ const vdomFnUtils: d.FunctionalUtilities = { forEach: (children, cb) => children.map(convertToPublic).forEach(cb), map: (children, cb) => children.map(convertToPublic).map(cb).map(convertToPrivate), }; /** * Convert a {@link d.VNode} to a {@link d.ChildNode} in order to present a * friendlier public interface (hence, 'convertToPublic'). * * @param node the virtual DOM node to convert * @returns a converted child node */ const convertToPublic = (node: d.VNode): d.ChildNode => ({ vattrs: node.$attrs$, vchildren: node.$children$, vkey: node.$key$, vname: node.$name$, vtag: node.$tag$, vtext: node.$text$, }); /** * Convert a {@link d.ChildNode} back to an equivalent {@link d.VNode} in * order to use the resulting object in the virtual DOM. The initial object was * likely created as part of presenting a public API, so converting it back * involved making it 'private' again (hence, `convertToPrivate`). * * @param node the child node to convert * @returns a converted virtual DOM node */ const convertToPrivate = (node: d.ChildNode): d.VNode => { if (typeof node.vtag === 'function') { const vnodeData = { ...node.vattrs }; if (node.vkey) { vnodeData.key = node.vkey; } if (node.vname) { vnodeData.name = node.vname; } return h(node.vtag, vnodeData, ...(node.vchildren || [])); } const vnode = newVNode(node.vtag as any, node.vtext); vnode.$attrs$ = node.vattrs; vnode.$children$ = node.vchildren; vnode.$key$ = node.vkey; vnode.$name$ = node.vname; return vnode; }; /** * Validates the ordering of attributes on an input element * * @param inputElm the element to validate */ const validateInputProperties = (inputElm: HTMLInputElement): void => { const props = Object.keys(inputElm); const value = props.indexOf('value'); if (value === -1) { return; } const typeIndex = props.indexOf('type'); const minIndex = props.indexOf('min'); const maxIndex = props.indexOf('max'); const stepIndex = props.indexOf('step'); if (value < typeIndex || value < minIndex || value < maxIndex || value < stepIndex) { consoleDevWarn(`The "value" prop of should be set after "min", "max", "type" and "step"`); } };