h.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { vnode } from "./vnode";
  2. import * as is from "./is";
  3. export function addNS(data, children, sel) {
  4. data.ns = "http://www.w3.org/2000/svg";
  5. if (sel !== "foreignObject" && children !== undefined) {
  6. for (let i = 0; i < children.length; ++i) {
  7. const child = children[i];
  8. if (typeof child === "string")
  9. continue;
  10. const childData = child.data;
  11. if (childData !== undefined) {
  12. addNS(childData, child.children, child.sel);
  13. }
  14. }
  15. }
  16. }
  17. export function h(sel, b, c) {
  18. let data = {};
  19. let children;
  20. let text;
  21. let i;
  22. if (c !== undefined) {
  23. if (b !== null) {
  24. data = b;
  25. }
  26. if (is.array(c)) {
  27. children = c;
  28. }
  29. else if (is.primitive(c)) {
  30. text = c.toString();
  31. }
  32. else if (c && c.sel) {
  33. children = [c];
  34. }
  35. }
  36. else if (b !== undefined && b !== null) {
  37. if (is.array(b)) {
  38. children = b;
  39. }
  40. else if (is.primitive(b)) {
  41. text = b.toString();
  42. }
  43. else if (b && b.sel) {
  44. children = [b];
  45. }
  46. else {
  47. data = b;
  48. }
  49. }
  50. if (children !== undefined) {
  51. for (i = 0; i < children.length; ++i) {
  52. if (is.primitive(children[i]))
  53. children[i] = vnode(undefined, undefined, undefined, children[i], undefined);
  54. }
  55. }
  56. if (sel[0] === "s" &&
  57. sel[1] === "v" &&
  58. sel[2] === "g" &&
  59. (sel.length === 3 || sel[3] === "." || sel[3] === "#")) {
  60. addNS(data, children, sel);
  61. }
  62. return vnode(sel, data, children, text, undefined);
  63. }
  64. /**
  65. * @experimental
  66. */
  67. export function fragment(children) {
  68. let c;
  69. let text;
  70. if (is.array(children)) {
  71. c = children;
  72. }
  73. else if (is.primitive(c)) {
  74. text = children;
  75. }
  76. else if (c && c.sel) {
  77. c = [children];
  78. }
  79. if (c !== undefined) {
  80. for (let i = 0; i < c.length; ++i) {
  81. if (is.primitive(c[i]))
  82. c[i] = vnode(undefined, undefined, undefined, c[i], undefined);
  83. }
  84. }
  85. return vnode(undefined, {}, c, text, undefined);
  86. }
  87. //# sourceMappingURL=h.js.map