h.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { spread, assign, insert, createComponent, dynamicProperty, SVGElements } from '/lib/ui/solid-js/web/dist/web.js';
  2. function createHyperScript(r) {
  3. function h() {
  4. let args = [].slice.call(arguments),
  5. e,
  6. multiExpression = false;
  7. function item(l) {
  8. const type = typeof l;
  9. if (l == null) ;else if ("string" === type) {
  10. if (!e) parseClass(l);else e.appendChild(document.createTextNode(l));
  11. } else if ("number" === type || "boolean" === type || l instanceof Date || l instanceof RegExp) {
  12. e.appendChild(document.createTextNode(l.toString()));
  13. } else if (Array.isArray(l)) {
  14. for (let i = 0; i < l.length; i++) item(l[i]);
  15. } else if (l instanceof Element) {
  16. r.insert(e, l, multiExpression ? null : undefined);
  17. } else if ("object" === type) {
  18. let dynamic = false;
  19. const d = Object.getOwnPropertyDescriptors(l);
  20. for (const k in d) {
  21. if (k !== "ref" && k.slice(0, 2) !== "on" && typeof d[k].value === "function") {
  22. r.dynamicProperty(l, k);
  23. dynamic = true;
  24. } else if (d[k].get) dynamic = true;
  25. }
  26. dynamic ? r.spread(e, l, e instanceof SVGElement, !!args.length) : r.assign(e, l, e instanceof SVGElement, !!args.length);
  27. } else if ("function" === type) {
  28. if (!e) {
  29. let props,
  30. next = args[0];
  31. if (next == null || typeof next === "object" && !Array.isArray(next) && !(next instanceof Element)) props = args.shift();
  32. props || (props = {});
  33. if (args.length) {
  34. props.children = args.length > 1 ? args : args[0];
  35. }
  36. const d = Object.getOwnPropertyDescriptors(props);
  37. for (const k in d) {
  38. if (typeof d[k].value === "function" && !d[k].value.length) r.dynamicProperty(props, k);
  39. }
  40. e = r.createComponent(l, props);
  41. args = [];
  42. } else r.insert(e, l, multiExpression ? null : undefined);
  43. }
  44. }
  45. typeof args[0] === "string" && detectMultiExpression(args);
  46. while (args.length) item(args.shift());
  47. return e;
  48. function parseClass(string) {
  49. const m = string.split(/([\.#]?[^\s#.]+)/);
  50. if (/^\.|#/.test(m[1])) e = document.createElement("div");
  51. for (let i = 0; i < m.length; i++) {
  52. const v = m[i],
  53. s = v.substring(1, v.length);
  54. if (!v) continue;
  55. if (!e) e = r.SVGElements.has(v) ? document.createElementNS("http://www.w3.org/2000/svg", v) : document.createElement(v);else if (v[0] === ".") e.classList.add(s);else if (v[0] === "#") e.setAttribute("id", s);
  56. }
  57. }
  58. function detectMultiExpression(list) {
  59. for (let i = 1; i < list.length; i++) {
  60. if (typeof list[i] === "function") {
  61. multiExpression = true;
  62. return;
  63. } else if (Array.isArray(list[i])) {
  64. detectMultiExpression(list[i]);
  65. }
  66. }
  67. }
  68. }
  69. return h;
  70. }
  71. var index = createHyperScript({
  72. spread,
  73. assign,
  74. insert,
  75. createComponent,
  76. dynamicProperty,
  77. SVGElements
  78. });
  79. export default index;