vnode.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { isVNode, Fragment, Text, Comment, openBlock, createBlock, createCommentVNode } from 'vue';
  2. import { debugWarn } from '../error.mjs';
  3. import { isArray, hasOwn, camelize } from '@vue/shared';
  4. const SCOPE = "utils/vue/vnode";
  5. var PatchFlags = /* @__PURE__ */ ((PatchFlags2) => {
  6. PatchFlags2[PatchFlags2["TEXT"] = 1] = "TEXT";
  7. PatchFlags2[PatchFlags2["CLASS"] = 2] = "CLASS";
  8. PatchFlags2[PatchFlags2["STYLE"] = 4] = "STYLE";
  9. PatchFlags2[PatchFlags2["PROPS"] = 8] = "PROPS";
  10. PatchFlags2[PatchFlags2["FULL_PROPS"] = 16] = "FULL_PROPS";
  11. PatchFlags2[PatchFlags2["HYDRATE_EVENTS"] = 32] = "HYDRATE_EVENTS";
  12. PatchFlags2[PatchFlags2["STABLE_FRAGMENT"] = 64] = "STABLE_FRAGMENT";
  13. PatchFlags2[PatchFlags2["KEYED_FRAGMENT"] = 128] = "KEYED_FRAGMENT";
  14. PatchFlags2[PatchFlags2["UNKEYED_FRAGMENT"] = 256] = "UNKEYED_FRAGMENT";
  15. PatchFlags2[PatchFlags2["NEED_PATCH"] = 512] = "NEED_PATCH";
  16. PatchFlags2[PatchFlags2["DYNAMIC_SLOTS"] = 1024] = "DYNAMIC_SLOTS";
  17. PatchFlags2[PatchFlags2["HOISTED"] = -1] = "HOISTED";
  18. PatchFlags2[PatchFlags2["BAIL"] = -2] = "BAIL";
  19. return PatchFlags2;
  20. })(PatchFlags || {});
  21. function isFragment(node) {
  22. return isVNode(node) && node.type === Fragment;
  23. }
  24. function isText(node) {
  25. return isVNode(node) && node.type === Text;
  26. }
  27. function isComment(node) {
  28. return isVNode(node) && node.type === Comment;
  29. }
  30. const TEMPLATE = "template";
  31. function isTemplate(node) {
  32. return isVNode(node) && node.type === TEMPLATE;
  33. }
  34. function isValidElementNode(node) {
  35. return isVNode(node) && !isFragment(node) && !isComment(node);
  36. }
  37. function getChildren(node, depth) {
  38. if (isComment(node))
  39. return;
  40. if (isFragment(node) || isTemplate(node)) {
  41. return depth > 0 ? getFirstValidNode(node.children, depth - 1) : void 0;
  42. }
  43. return node;
  44. }
  45. const getFirstValidNode = (nodes, maxDepth = 3) => {
  46. if (isArray(nodes)) {
  47. return getChildren(nodes[0], maxDepth);
  48. } else {
  49. return getChildren(nodes, maxDepth);
  50. }
  51. };
  52. function renderIf(condition, ...args) {
  53. return condition ? renderBlock(...args) : createCommentVNode("v-if", true);
  54. }
  55. function renderBlock(...args) {
  56. return openBlock(), createBlock(...args);
  57. }
  58. const getNormalizedProps = (node) => {
  59. if (!isVNode(node)) {
  60. debugWarn(SCOPE, "[getNormalizedProps] must be a VNode");
  61. return {};
  62. }
  63. const raw = node.props || {};
  64. const type = (isVNode(node.type) ? node.type.props : void 0) || {};
  65. const props = {};
  66. Object.keys(type).forEach((key) => {
  67. if (hasOwn(type[key], "default")) {
  68. props[key] = type[key].default;
  69. }
  70. });
  71. Object.keys(raw).forEach((key) => {
  72. props[camelize(key)] = raw[key];
  73. });
  74. return props;
  75. };
  76. const ensureOnlyChild = (children) => {
  77. if (!isArray(children) || children.length > 1) {
  78. throw new Error("expect to receive a single Vue element child");
  79. }
  80. return children[0];
  81. };
  82. const flattedChildren = (children) => {
  83. const vNodes = isArray(children) ? children : [children];
  84. const result = [];
  85. vNodes.forEach((child) => {
  86. var _a;
  87. if (isArray(child)) {
  88. result.push(...flattedChildren(child));
  89. } else if (isVNode(child) && ((_a = child.component) == null ? void 0 : _a.subTree)) {
  90. result.push(child, ...flattedChildren(child.component.subTree));
  91. } else if (isVNode(child) && isArray(child.children)) {
  92. result.push(...flattedChildren(child.children));
  93. } else {
  94. result.push(child);
  95. }
  96. });
  97. return result;
  98. };
  99. export { PatchFlags, ensureOnlyChild, flattedChildren, getFirstValidNode, getNormalizedProps, isComment, isFragment, isTemplate, isText, isValidElementNode, renderBlock, renderIf };
  100. //# sourceMappingURL=vnode.mjs.map