vnode.js 3.9 KB

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