web.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. import { createMemo, createRoot, createRenderEffect, sharedConfig, createSignal, onCleanup, splitProps, untrack } from '/lib/ui/solid-js/dist/solid.js';
  2. export { ErrorBoundary, For, Index, Match, Show, Suspense, SuspenseList, Switch, createComponent, createRenderEffect as effect, getOwner, mergeProps } from '/lib/ui/solid-js/dist/solid.js';
  3. const booleans = ["allowfullscreen", "allowpaymentrequest", "async", "autofocus", "autoplay", "checked", "controls", "default", "disabled", "formnovalidate", "hidden", "ismap", "itemscope", "loop", "multiple", "muted", "nomodule", "novalidate", "open", "playsinline", "readonly", "required", "reversed", "seamless", "selected", "truespeed"];
  4. const Properties = new Set(["className", "indeterminate", "value", ...booleans]);
  5. const ChildProperties = new Set(["innerHTML", "textContent", "innerText", "children"]);
  6. const Aliases = {
  7. className: "class",
  8. htmlFor: "for"
  9. };
  10. const DelegatedEvents = new Set(["beforeinput", "click", "dblclick", "focusin", "focusout", "input", "keydown", "keyup", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup", "pointerdown", "pointermove", "pointerout", "pointerover", "pointerup", "touchend", "touchmove", "touchstart"]);
  11. const SVGElements = new Set([
  12. "altGlyph", "altGlyphDef", "altGlyphItem", "animate", "animateColor", "animateMotion", "animateTransform", "circle", "clipPath", "color-profile", "cursor", "defs", "desc", "ellipse", "feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDistantLight", "feFlood", "feFuncA", "feFuncB", "feFuncG", "feFuncR", "feGaussianBlur", "feImage", "feMerge", "feMergeNode", "feMorphology", "feOffset", "fePointLight", "feSpecularLighting", "feSpotLight", "feTile", "feTurbulence", "filter", "font", "font-face", "font-face-format", "font-face-name", "font-face-src", "font-face-uri", "foreignObject", "g", "glyph", "glyphRef", "hkern", "image", "line", "linearGradient", "marker", "mask", "metadata", "missing-glyph", "mpath", "path", "pattern", "polygon", "polyline", "radialGradient", "rect",
  13. "set", "stop",
  14. "svg", "switch", "symbol", "text", "textPath",
  15. "tref", "tspan", "use", "view", "vkern"]);
  16. const SVGNamespace = {
  17. xlink: "http://www.w3.org/1999/xlink",
  18. xml: "http://www.w3.org/XML/1998/namespace"
  19. };
  20. function memo(fn, equals) {
  21. return createMemo(fn, undefined, !equals ? {
  22. equals
  23. } : undefined);
  24. }
  25. function reconcileArrays(parentNode, a, b) {
  26. let bLength = b.length,
  27. aEnd = a.length,
  28. bEnd = bLength,
  29. aStart = 0,
  30. bStart = 0,
  31. after = a[aEnd - 1].nextSibling,
  32. map = null;
  33. while (aStart < aEnd || bStart < bEnd) {
  34. if (aEnd === aStart) {
  35. const node = bEnd < bLength ? bStart ? b[bStart - 1].nextSibling : b[bEnd - bStart] : after;
  36. while (bStart < bEnd) parentNode.insertBefore(b[bStart++], node);
  37. } else if (bEnd === bStart) {
  38. while (aStart < aEnd) {
  39. if (!map || !map.has(a[aStart])) parentNode.removeChild(a[aStart]);
  40. aStart++;
  41. }
  42. } else if (a[aStart] === b[bStart]) {
  43. aStart++;
  44. bStart++;
  45. } else if (a[aEnd - 1] === b[bEnd - 1]) {
  46. aEnd--;
  47. bEnd--;
  48. } else if (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]) {
  49. const node = a[--aEnd].nextSibling;
  50. parentNode.insertBefore(b[bStart++], a[aStart++].nextSibling);
  51. parentNode.insertBefore(b[--bEnd], node);
  52. a[aEnd] = b[bEnd];
  53. } else {
  54. if (!map) {
  55. map = new Map();
  56. let i = bStart;
  57. while (i < bEnd) map.set(b[i], i++);
  58. }
  59. const index = map.get(a[aStart]);
  60. if (index != null) {
  61. if (bStart < index && index < bEnd) {
  62. let i = aStart,
  63. sequence = 1,
  64. t;
  65. while (++i < aEnd && i < bEnd) {
  66. if ((t = map.get(a[i])) == null || t !== index + sequence) break;
  67. sequence++;
  68. }
  69. if (sequence > index - bStart) {
  70. const node = a[aStart];
  71. while (bStart < index) parentNode.insertBefore(b[bStart++], node);
  72. } else parentNode.replaceChild(b[bStart++], a[aStart++]);
  73. } else aStart++;
  74. } else parentNode.removeChild(a[aStart++]);
  75. }
  76. }
  77. }
  78. const $$EVENTS = Symbol("delegated-events");
  79. function render(code, element, init) {
  80. let disposer;
  81. createRoot(dispose => {
  82. disposer = dispose;
  83. insert(element, code(), element.firstChild ? null : undefined, init);
  84. });
  85. return () => {
  86. disposer();
  87. element.textContent = "";
  88. };
  89. }
  90. function template(html, check, isSVG) {
  91. const t = document.createElement("template");
  92. t.innerHTML = html;
  93. let node = t.content.firstChild;
  94. if (isSVG) node = node.firstChild;
  95. return node;
  96. }
  97. function delegateEvents(eventNames) {
  98. const e = document[$$EVENTS] || (document[$$EVENTS] = new Set());
  99. for (let i = 0, l = eventNames.length; i < l; i++) {
  100. const name = eventNames[i];
  101. if (!e.has(name)) {
  102. e.add(name);
  103. document.addEventListener(name, eventHandler);
  104. }
  105. }
  106. }
  107. function clearDelegatedEvents() {
  108. if (document[$$EVENTS]) {
  109. for (let name of document[$$EVENTS].keys()) document.removeEventListener(name, eventHandler);
  110. delete document[$$EVENTS];
  111. }
  112. }
  113. function setAttribute(node, name, value) {
  114. if (value == null) node.removeAttribute(name);else node.setAttribute(name, value);
  115. }
  116. function setAttributeNS(node, namespace, name, value) {
  117. if (value == null) node.removeAttributeNS(namespace, name);else node.setAttributeNS(namespace, name, value);
  118. }
  119. function addEventListener(node, name, handler, delegate) {
  120. if (delegate) {
  121. if (Array.isArray(handler)) {
  122. node[`$$${name}`] = handler[0];
  123. node[`$$${name}Data`] = handler[1];
  124. } else node[`$$${name}`] = handler;
  125. } else if (Array.isArray(handler)) {
  126. node.addEventListener(name, e => handler[0](handler[1], e));
  127. } else node.addEventListener(name, handler);
  128. }
  129. function classList(node, value, prev = {}) {
  130. const classKeys = Object.keys(value),
  131. prevKeys = Object.keys(prev);
  132. let i, len;
  133. for (i = 0, len = prevKeys.length; i < len; i++) {
  134. const key = prevKeys[i];
  135. if (!key || key === "undefined" || key in value) continue;
  136. toggleClassKey(node, key, false);
  137. delete prev[key];
  138. }
  139. for (i = 0, len = classKeys.length; i < len; i++) {
  140. const key = classKeys[i],
  141. classValue = !!value[key];
  142. if (!key || key === "undefined" || prev[key] === classValue) continue;
  143. toggleClassKey(node, key, classValue);
  144. prev[key] = classValue;
  145. }
  146. return prev;
  147. }
  148. function style(node, value, prev = {}) {
  149. const nodeStyle = node.style;
  150. if (typeof value === "string") return nodeStyle.cssText = value;
  151. typeof prev === "string" && (prev = {});
  152. let v, s;
  153. for (s in prev) {
  154. value[s] == null && nodeStyle.removeProperty(s);
  155. delete prev[s];
  156. }
  157. for (s in value) {
  158. v = value[s];
  159. if (v !== prev[s]) {
  160. nodeStyle.setProperty(s, v);
  161. prev[s] = v;
  162. }
  163. }
  164. return prev;
  165. }
  166. function spread(node, accessor, isSVG, skipChildren) {
  167. if (typeof accessor === "function") {
  168. createRenderEffect(current => spreadExpression(node, accessor(), current, isSVG, skipChildren));
  169. } else spreadExpression(node, accessor, undefined, isSVG, skipChildren);
  170. }
  171. function dynamicProperty(props, key) {
  172. const src = props[key];
  173. Object.defineProperty(props, key, {
  174. get() {
  175. return src();
  176. },
  177. enumerable: true
  178. });
  179. return props;
  180. }
  181. function insert(parent, accessor, marker, initial) {
  182. if (marker !== undefined && !initial) initial = [];
  183. if (typeof accessor !== "function") return insertExpression(parent, accessor, initial, marker);
  184. createRenderEffect(current => insertExpression(parent, accessor(), current, marker), initial);
  185. }
  186. function assign(node, props, isSVG, skipChildren, prevProps = {}) {
  187. let isCE, isProp, isChildProp;
  188. for (const prop in props) {
  189. if (prop === "children") {
  190. if (!skipChildren) insertExpression(node, props.children);
  191. continue;
  192. }
  193. const value = props[prop];
  194. if (value === prevProps[prop]) continue;
  195. if (prop === "style") {
  196. style(node, value, prevProps[prop]);
  197. } else if (prop === "class" && !isSVG) {
  198. node.className = value;
  199. } else if (prop === "classList") {
  200. classList(node, value, prevProps[prop]);
  201. } else if (prop === "ref") {
  202. value(node);
  203. } else if (prop.slice(0, 3) === "on:") {
  204. node.addEventListener(prop.slice(3), value);
  205. } else if (prop.slice(0, 10) === "oncapture:") {
  206. node.addEventListener(prop.slice(10), value, true);
  207. } else if (prop.slice(0, 2) === "on") {
  208. const name = prop.slice(2).toLowerCase();
  209. const delegate = DelegatedEvents.has(name);
  210. addEventListener(node, name, value, delegate);
  211. delegate && delegateEvents([name]);
  212. } else if ((isChildProp = ChildProperties.has(prop)) || !isSVG && (isProp = Properties.has(prop)) || (isCE = node.nodeName.includes("-"))) {
  213. if (isCE && !isProp && !isChildProp) node[toPropertyName(prop)] = value;else node[prop] = value;
  214. } else {
  215. const ns = isSVG && prop.indexOf(":") > -1 && SVGNamespace[prop.split(":")[0]];
  216. if (ns) setAttributeNS(node, ns, prop, value);else setAttribute(node, Aliases[prop] || prop, value);
  217. }
  218. prevProps[prop] = value;
  219. }
  220. }
  221. function hydrate(code, element) {
  222. sharedConfig.resources = globalThis._$HYDRATION.resources;
  223. sharedConfig.completed = globalThis._$HYDRATION.completed;
  224. sharedConfig.events = globalThis._$HYDRATION.events;
  225. sharedConfig.context = {
  226. id: "",
  227. count: 0,
  228. loadResource: globalThis._$HYDRATION.loadResource
  229. };
  230. sharedConfig.registry = new Map();
  231. gatherHydratable(element);
  232. const dispose = render(code, element, [...element.childNodes]);
  233. sharedConfig.context = null;
  234. return dispose;
  235. }
  236. function gatherHydratable(element) {
  237. const templates = element.querySelectorAll(`*[data-hk]`);
  238. for (let i = 0; i < templates.length; i++) {
  239. const node = templates[i];
  240. sharedConfig.registry.set(node.getAttribute("data-hk"), node);
  241. }
  242. }
  243. function getNextElement(template) {
  244. let node, key;
  245. if (!sharedConfig.context || !(node = sharedConfig.registry.get(key = getHydrationKey()))) {
  246. return template.cloneNode(true);
  247. }
  248. if (sharedConfig.completed) sharedConfig.completed.add(node);
  249. sharedConfig.registry.delete(key);
  250. return node;
  251. }
  252. function getNextMarker(start) {
  253. let end = start,
  254. count = 0,
  255. current = [];
  256. if (sharedConfig.context) {
  257. while (end) {
  258. if (end.nodeType === 8) {
  259. const v = end.nodeValue;
  260. if (v === "#") count++;else if (v === "/") {
  261. if (count === 0) return [end, current];
  262. count--;
  263. }
  264. }
  265. current.push(end);
  266. end = end.nextSibling;
  267. }
  268. }
  269. return [end, current];
  270. }
  271. function runHydrationEvents() {
  272. if (sharedConfig.events && !sharedConfig.events.queued) {
  273. queueMicrotask(() => {
  274. const {
  275. completed,
  276. events
  277. } = sharedConfig;
  278. events.queued = false;
  279. while (events.length) {
  280. const [el, e] = events[0];
  281. if (!completed.has(el)) return;
  282. eventHandler(e);
  283. events.shift();
  284. }
  285. });
  286. sharedConfig.events.queued = true;
  287. }
  288. }
  289. function toPropertyName(name) {
  290. return name.toLowerCase().replace(/-([a-z])/g, (_, w) => w.toUpperCase());
  291. }
  292. function toggleClassKey(node, key, value) {
  293. const classNames = key.split(/\s+/);
  294. for (let i = 0, nameLen = classNames.length; i < nameLen; i++) node.classList.toggle(classNames[i], value);
  295. }
  296. function eventHandler(e) {
  297. const key = `$$${e.type}`;
  298. let node = e.composedPath && e.composedPath()[0] || e.target;
  299. if (e.target !== node) {
  300. Object.defineProperty(e, "target", {
  301. configurable: true,
  302. value: node
  303. });
  304. }
  305. Object.defineProperty(e, "currentTarget", {
  306. configurable: true,
  307. get() {
  308. return node;
  309. }
  310. });
  311. while (node !== null) {
  312. const handler = node[key];
  313. if (handler) {
  314. const data = node[`${key}Data`];
  315. data !== undefined ? handler(data, e) : handler(e);
  316. if (e.cancelBubble) return;
  317. }
  318. node = node.host && node.host !== node && node.host instanceof Node ? node.host : node.parentNode;
  319. }
  320. }
  321. function spreadExpression(node, props, prevProps = {}, isSVG, skipChildren) {
  322. if (!skipChildren && "children" in props) {
  323. createRenderEffect(() => prevProps.children = insertExpression(node, props.children, prevProps.children));
  324. }
  325. createRenderEffect(() => assign(node, props, isSVG, true, prevProps));
  326. return prevProps;
  327. }
  328. function insertExpression(parent, value, current, marker, unwrapArray) {
  329. while (typeof current === "function") current = current();
  330. if (value === current) return current;
  331. const t = typeof value,
  332. multi = marker !== undefined;
  333. parent = multi && current[0] && current[0].parentNode || parent;
  334. if (t === "string" || t === "number") {
  335. if (t === "number") value = value.toString();
  336. if (multi) {
  337. let node = current[0];
  338. if (node && node.nodeType === 3) {
  339. node.data = value;
  340. } else node = document.createTextNode(value);
  341. current = cleanChildren(parent, current, marker, node);
  342. } else {
  343. if (current !== "" && typeof current === "string") {
  344. current = parent.firstChild.data = value;
  345. } else current = parent.textContent = value;
  346. }
  347. } else if (value == null || t === "boolean") {
  348. if (sharedConfig.context) return current;
  349. current = cleanChildren(parent, current, marker);
  350. } else if (t === "function") {
  351. createRenderEffect(() => {
  352. let v = value();
  353. while (typeof v === "function") v = v();
  354. current = insertExpression(parent, v, current, marker);
  355. });
  356. return () => current;
  357. } else if (Array.isArray(value)) {
  358. const array = [];
  359. if (normalizeIncomingArray(array, value, unwrapArray)) {
  360. createRenderEffect(() => current = insertExpression(parent, array, current, marker, true));
  361. return () => current;
  362. }
  363. if (sharedConfig.context && current.length) return current;
  364. if (array.length === 0) {
  365. current = cleanChildren(parent, current, marker);
  366. if (multi) return current;
  367. } else {
  368. if (Array.isArray(current)) {
  369. if (current.length === 0) {
  370. appendNodes(parent, array, marker);
  371. } else reconcileArrays(parent, current, array);
  372. } else if (current == null || current === "") {
  373. appendNodes(parent, array);
  374. } else {
  375. reconcileArrays(parent, multi && current || [parent.firstChild], array);
  376. }
  377. }
  378. current = array;
  379. } else if (value instanceof Node) {
  380. if (Array.isArray(current)) {
  381. if (multi) return current = cleanChildren(parent, current, marker, value);
  382. cleanChildren(parent, current, null, value);
  383. } else if (current == null || current === "" || !parent.firstChild) {
  384. parent.appendChild(value);
  385. } else parent.replaceChild(value, parent.firstChild);
  386. current = value;
  387. } else ;
  388. return current;
  389. }
  390. function normalizeIncomingArray(normalized, array, unwrap) {
  391. let dynamic = false;
  392. for (let i = 0, len = array.length; i < len; i++) {
  393. let item = array[i],
  394. t;
  395. if (item instanceof Node) {
  396. normalized.push(item);
  397. } else if (item == null || item === true || item === false) ; else if (Array.isArray(item)) {
  398. dynamic = normalizeIncomingArray(normalized, item) || dynamic;
  399. } else if ((t = typeof item) === "string") {
  400. normalized.push(document.createTextNode(item));
  401. } else if (t === "function") {
  402. if (unwrap) {
  403. while (typeof item === "function") item = item();
  404. dynamic = normalizeIncomingArray(normalized, Array.isArray(item) ? item : [item]) || dynamic;
  405. } else {
  406. normalized.push(item);
  407. dynamic = true;
  408. }
  409. } else normalized.push(document.createTextNode(item.toString()));
  410. }
  411. return dynamic;
  412. }
  413. function appendNodes(parent, array, marker) {
  414. for (let i = 0, len = array.length; i < len; i++) parent.insertBefore(array[i], marker);
  415. }
  416. function cleanChildren(parent, current, marker, replacement) {
  417. if (marker === undefined) return parent.textContent = "";
  418. const node = replacement || document.createTextNode("");
  419. if (current.length) {
  420. let inserted = false;
  421. for (let i = current.length - 1; i >= 0; i--) {
  422. const el = current[i];
  423. if (node !== el) {
  424. const isParent = el.parentNode === parent;
  425. if (!inserted && !i) isParent ? parent.replaceChild(node, el) : parent.insertBefore(node, marker);else isParent && parent.removeChild(el);
  426. } else inserted = true;
  427. }
  428. } else parent.insertBefore(node, marker);
  429. return [node];
  430. }
  431. function getHydrationKey() {
  432. const hydrate = sharedConfig.context;
  433. return `${hydrate.id}${hydrate.count++}`;
  434. }
  435. function renderToString(fn, options) {}
  436. function renderToStringAsync(fn, options) {}
  437. function renderToNodeStream(fn, options) {}
  438. function renderToWebStream(fn, options) {}
  439. function ssr(template, ...nodes) {}
  440. function resolveSSRNode(node) {}
  441. function ssrClassList(value) {}
  442. function ssrStyle(value) {}
  443. function ssrSpread(accessor) {}
  444. function ssrBoolean(key, value) {}
  445. function escape(html) {}
  446. const isServer = false;
  447. const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
  448. function createElement(tagName, isSVG = false) {
  449. return isSVG ? document.createElementNS(SVG_NAMESPACE, tagName) : document.createElement(tagName);
  450. }
  451. function Portal(props) {
  452. const {
  453. useShadow
  454. } = props,
  455. marker = document.createTextNode(""),
  456. mount = props.mount || document.body;
  457. function renderPortal() {
  458. if (sharedConfig.context) {
  459. const [s, set] = createSignal(false);
  460. queueMicrotask(() => set(true));
  461. return () => s() && props.children;
  462. } else return () => props.children;
  463. }
  464. if (mount instanceof HTMLHeadElement) {
  465. const [clean, setClean] = createSignal(false);
  466. const cleanup = () => setClean(true);
  467. createRoot(dispose => insert(mount, () => !clean() ? renderPortal()() : dispose(), null));
  468. onCleanup(() => {
  469. if (sharedConfig.context) queueMicrotask(cleanup);else cleanup();
  470. });
  471. } else {
  472. const container = createElement(props.isSVG ? "g" : "div", props.isSVG),
  473. renderRoot = useShadow && container.attachShadow ? container.attachShadow({
  474. mode: "open"
  475. }) : container;
  476. Object.defineProperty(container, "host", {
  477. get() {
  478. return marker.parentNode;
  479. }
  480. });
  481. insert(renderRoot, renderPortal());
  482. mount.appendChild(container);
  483. props.ref && props.ref(container);
  484. onCleanup(() => mount.removeChild(container));
  485. }
  486. return marker;
  487. }
  488. function Dynamic(props) {
  489. const [p, others] = splitProps(props, ["component"]);
  490. return createMemo(() => {
  491. const component = p.component;
  492. switch (typeof component) {
  493. case "function":
  494. return untrack(() => component(others));
  495. case "string":
  496. const isSvg = SVGElements.has(component);
  497. const el = createElement(component, isSvg);
  498. spread(el, others, isSvg);
  499. return el;
  500. }
  501. });
  502. }
  503. export { Aliases, ChildProperties, DelegatedEvents, Dynamic, Portal, Properties, SVGElements, SVGNamespace, addEventListener, assign, classList, clearDelegatedEvents, delegateEvents, dynamicProperty, escape, gatherHydratable, getHydrationKey, getNextElement, getNextMarker, hydrate, insert, isServer, memo, render, renderToNodeStream, renderToString, renderToStringAsync, renderToWebStream, resolveSSRNode, runHydrationEvents, setAttribute, setAttributeNS, spread, ssr, ssrBoolean, ssrClassList, ssrSpread, ssrStyle, style, template };