h.cjs 3.1 KB

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