UNPKG

element-plus

Version:
97 lines (96 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNormalizedProps = exports.renderBlock = exports.renderIf = exports.getFirstValidNode = exports.isValidElementNode = exports.isTemplate = exports.isComment = exports.isText = exports.isFragment = exports.PatchFlags = exports.SCOPE = void 0; const vue_1 = require("vue"); const shared_1 = require("@vue/shared"); const error_1 = require("./error"); const TEMPLATE = 'template'; exports.SCOPE = 'VNode'; var PatchFlags; (function (PatchFlags) { PatchFlags[PatchFlags["TEXT"] = 1] = "TEXT"; PatchFlags[PatchFlags["CLASS"] = 2] = "CLASS"; PatchFlags[PatchFlags["STYLE"] = 4] = "STYLE"; PatchFlags[PatchFlags["PROPS"] = 8] = "PROPS"; PatchFlags[PatchFlags["FULL_PROPS"] = 16] = "FULL_PROPS"; PatchFlags[PatchFlags["HYDRATE_EVENTS"] = 32] = "HYDRATE_EVENTS"; PatchFlags[PatchFlags["STABLE_FRAGMENT"] = 64] = "STABLE_FRAGMENT"; PatchFlags[PatchFlags["KEYED_FRAGMENT"] = 128] = "KEYED_FRAGMENT"; PatchFlags[PatchFlags["UNKEYED_FRAGMENT"] = 256] = "UNKEYED_FRAGMENT"; PatchFlags[PatchFlags["NEED_PATCH"] = 512] = "NEED_PATCH"; PatchFlags[PatchFlags["DYNAMIC_SLOTS"] = 1024] = "DYNAMIC_SLOTS"; PatchFlags[PatchFlags["HOISTED"] = -1] = "HOISTED"; PatchFlags[PatchFlags["BAIL"] = -2] = "BAIL"; })(PatchFlags = exports.PatchFlags || (exports.PatchFlags = {})); const isFragment = (node) => node.type === vue_1.Fragment; exports.isFragment = isFragment; const isText = (node) => node.type === vue_1.Text; exports.isText = isText; const isComment = (node) => node.type === vue_1.Comment; exports.isComment = isComment; const isTemplate = (node) => node.type === TEMPLATE; exports.isTemplate = isTemplate; /** * get a valid child node (not fragment nor comment) * @param node {VNode} node to be searched * @param depth {number} depth to be searched */ function getChildren(node, depth) { if (exports.isComment(node)) return; if (exports.isFragment(node) || exports.isTemplate(node)) { return depth > 0 ? exports.getFirstValidNode(node.children, depth - 1) : undefined; } return node; } /** * determine if the element is a valid element type rather than fragments and comment e.g. <template> v-if * @param node {VNode} node to be tested */ const isValidElementNode = (node) => !(exports.isFragment(node) || exports.isComment(node)); exports.isValidElementNode = isValidElementNode; const getFirstValidNode = (nodes, maxDepth = 3) => { if (Array.isArray(nodes)) { return getChildren(nodes[0], maxDepth); } else { return getChildren(nodes, maxDepth); } }; exports.getFirstValidNode = getFirstValidNode; function renderIf(condition, node, props, children, patchFlag, patchProps) { return (condition ? renderBlock(node, props, children, patchFlag, patchProps) : vue_1.createCommentVNode('v-if', true)); } exports.renderIf = renderIf; function renderBlock(node, props, children, patchFlag, patchProps) { return (vue_1.openBlock(), vue_1.createBlock(node, props, children, patchFlag, patchProps)); } exports.renderBlock = renderBlock; /** * todo * get normalized props from VNode * @param node */ const getNormalizedProps = (node) => { if (!vue_1.isVNode(node)) { error_1.warn(exports.SCOPE, 'value must be a VNode'); return; } const raw = node.props || {}; const type = node.type.props || {}; const props = {}; Object.keys(type).forEach(key => { if (shared_1.hasOwn(type[key], 'default')) { props[key] = type[key].default; } }); Object.keys(raw).forEach(key => { props[vue_1.camelize(key)] = raw[key]; }); return props; }; exports.getNormalizedProps = getNormalizedProps;