vnode.js 3.9 KB

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