7c9d9c246ccfcab7560fba78b30ffabac1cce9ab1f83fe993871543ca84365546051b1c92b9679db6d0a149fadfac9c9980bafc1f3e441ce659b5fbf555dca 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var index = require('../../../hooks/use-forward-ref/index.js');
  5. var shared = require('@vue/shared');
  6. var index$1 = require('../../../hooks/use-namespace/index.js');
  7. const NAME = "ElOnlyChild";
  8. const OnlyChild = vue.defineComponent({
  9. name: NAME,
  10. setup(_, {
  11. slots,
  12. attrs
  13. }) {
  14. var _a;
  15. const forwardRefInjection = vue.inject(index.FORWARD_REF_INJECTION_KEY);
  16. const forwardRefDirective = index.useForwardRefDirective((_a = forwardRefInjection == null ? void 0 : forwardRefInjection.setForwardRef) != null ? _a : shared.NOOP);
  17. return () => {
  18. var _a2;
  19. const defaultSlot = (_a2 = slots.default) == null ? void 0 : _a2.call(slots, attrs);
  20. if (!defaultSlot)
  21. return null;
  22. const [firstLegitNode, length] = findFirstLegitChild(defaultSlot);
  23. if (!firstLegitNode) {
  24. return null;
  25. }
  26. return vue.withDirectives(vue.cloneVNode(firstLegitNode, attrs), [[forwardRefDirective]]);
  27. };
  28. }
  29. });
  30. function findFirstLegitChild(node) {
  31. if (!node)
  32. return [null, 0];
  33. const children = node;
  34. const len = children.filter((c) => c.type !== vue.Comment).length;
  35. for (const child of children) {
  36. if (shared.isObject(child)) {
  37. switch (child.type) {
  38. case vue.Comment:
  39. continue;
  40. case vue.Text:
  41. case "svg":
  42. return [wrapTextContent(child), len];
  43. case vue.Fragment:
  44. return findFirstLegitChild(child.children);
  45. default:
  46. return [child, len];
  47. }
  48. }
  49. return [wrapTextContent(child), len];
  50. }
  51. return [null, 0];
  52. }
  53. function wrapTextContent(s) {
  54. const ns = index$1.useNamespace("only-child");
  55. return vue.createVNode("span", {
  56. "class": ns.e("content")
  57. }, [s]);
  58. }
  59. exports.OnlyChild = OnlyChild;
  60. //# sourceMappingURL=only-child.js.map