You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
6.3 KiB
1 line
6.3 KiB
11 months ago
|
{"version":3,"file":"vnode.mjs","sources":["../../../../packages/utils/vnode.ts"],"sourcesContent":["import {\n Fragment,\n Text,\n Comment,\n createBlock,\n openBlock,\n createCommentVNode,\n isVNode,\n camelize,\n} from 'vue'\n\nimport { hasOwn } from '@vue/shared'\nimport { debugWarn } from './error'\nimport type { VNode, VNodeTypes, VNodeChild } from 'vue'\n\ntype Children = VNodeTypes[] | VNodeTypes\n\nconst TEMPLATE = 'template'\n\nexport const SCOPE = 'VNode'\n\nexport enum PatchFlags {\n TEXT = 1,\n CLASS = 2,\n STYLE = 4,\n PROPS = 8,\n FULL_PROPS = 16,\n HYDRATE_EVENTS = 32,\n STABLE_FRAGMENT = 64,\n KEYED_FRAGMENT = 128,\n UNKEYED_FRAGMENT = 256,\n NEED_PATCH = 512,\n DYNAMIC_SLOTS = 1024,\n HOISTED = -1,\n BAIL = -2,\n}\n\nexport const isFragment = (node: unknown): node is VNode =>\n isVNode(node) && node.type === Fragment\n\nexport const isText = (node: VNodeChild) => (node as VNode).type === Text\n\nexport const isComment = (node: VNodeChild) => (node as VNode).type === Comment\n\nexport const isTemplate = (node: VNodeChild) =>\n (node as VNode).type === TEMPLATE\n\n/**\n * get a valid child node (not fragment nor comment)\n * @param node {VNode} node to be searched\n * @param depth {number} depth to be searched\n */\nfunction getChildren(node: VNode, depth: number): undefined | VNode {\n if (isComment(node)) return\n if (isFragment(node) || isTemplate(node)) {\n return depth > 0\n ? getFirstValidNode(node.children as VNodeChild, depth - 1)\n : undefined\n }\n return node\n}\n\n/**\n * determine if the element is a valid element type rather than fragments and comment e.g. <template> v-if\n * @param node {VNode} node to be tested\n */\nexport const isValidElementNode = (node: unknown): node is VNode =>\n isVNode(node) && !isFragment(node) && !isComment(node)\n\nexport const getFirstValidNode = (\n nodes: VNodeChild,\n maxDepth = 3\n): ReturnType<typeof getChildren> => {\n if (Array.isArray(nodes)) {\n return getChildren(nodes[0] as VNode, maxDepth)\n } else {\n return getChildren(nodes as VNode, maxDepth)\n }\n}\n\nexport function renderIf(\n condition: boolean,\n node: VNodeTypes,\n props: any,\n children?: Children,\n patchFlag?: number,\n patchProps?: string[]\n) {\n return condition\n ? renderBlock(node, props, children, patchFlag, patchProps)\n : createCommentVNode('v-if', true)\n}\n\nexport function renderBlock(\n node: VNodeTypes,\n props: any,\n children?: Children,\n patchFlag?: number,\n patchProps?: string[]\n) {\n return openBlock(), createBlock(node, props, children, patchFlag, patchProps)\n}\n\n/**\n * todo\n * get normalized props from VNode\n * @param node\n */\nexport const getNormalizedProps = (node: VNode) => {\n if (!isVNode(node)) {\n debugWarn(SCOPE, 'value must be a VNode')\n return\n }\n\n const raw = node.props || {}\n const type = (node.type as any).props || {}\n const props = {} as any\n\n Object.keys(type).forEach((key) => {\n if (hasOwn(type[key], 'default')) {\n props[key] = type[key].default\n }\n })\n\n Object.keys(raw).forEach((key) => {\n props[camelize(key)] = raw[key]\n })\n\n return props\n}\n"],"names":[],"mappings":";;;;AAYA,MAAM,QAAQ,GAAG,UAAU,CAAC;AAChB,MAAC,KAAK,GAAG,QAAQ;AACnB,IAAC,UAAU,mBAAmB,CAAC,CAAC,WAAW,KAAK;AAC1D,EAAE,WAAW,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC;AAChD,EAAE,WAAW,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC;AAClD,EAAE,WAAW,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC;AAClD,EAAE,WAAW,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC;AAClD,EAAE,WAAW,CAAC,WAAW,CAAC,YAAY,CAAC,GAAG,EAAE,CAAC,GAAG,YAAY,CAAC;AAC7D,EAAE,WAAW,CAAC,WAAW,CAAC,gBAAgB,CAAC,GAAG,EAAE,CAAC,GAAG,gBAAgB,CAAC;AACrE,EAAE,WAAW,CAAC,WAAW,CAAC,iBAAiB,CAAC,GAAG,EAAE,CAAC,GAAG,iBAAiB,CAAC;AACvE,EAAE,WAAW,CAAC,WAAW,CAAC,gBAAgB,CAAC,GAAG,GAAG,CAAC,GAAG,gBAAgB,CAAC;AACtE,EAAE,WAAW,CAAC,WAAW,CAAC,kBAAkB,CAAC,GAAG,GAAG,CAAC,GAAG,kBAAkB,CAAC;AAC1E,EAAE,WAAW,CAAC,WAAW,CAAC,YAAY,CAAC,GAAG,GAAG,CAAC,GAAG,YAAY,CAAC;AAC9D,EAAE,WAAW,CAAC,WAAW,CAAC,eAAe,CAAC,
|