@snupo/jmc 1.4.1 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/jmc.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"jmc.d.ts","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,CAAC,EAGD,CAAC,EACD,OAAO,EAIV,MAAM,QAAQ,CAAA;AAoJf,wBAAgB,KAAK,CACjB,CAAC,EAAE,IAAI,GAAG,IAAI,EACd,CAAC,EAAE,CAAC,EACJ,KAAK,SAAM,EACX,EAAE,GAAE,CAAM,GACX,WAAW,GAAG,SAAS,CAgBzB;AAED,wBAAgB,UAAU,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAanD"}
1
+ {"version":3,"file":"jmc.d.ts","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,CAAC,EAGD,CAAC,EACD,OAAO,EAIV,MAAM,QAAQ,CAAA;AAkKf,wBAAgB,KAAK,CACjB,CAAC,EAAE,IAAI,GAAG,IAAI,EACd,CAAC,EAAE,CAAC,EACJ,KAAK,SAAM,EACX,EAAE,GAAE,CAAM,GACX,WAAW,GAAG,SAAS,CAgBzB;AAED,wBAAgB,UAAU,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAanD"}
package/dist/jmc.js CHANGED
@@ -11,9 +11,14 @@ let removeexcesschildren = (n, i) => {
11
11
  catch { } // due to blur-event-handlers, this can fail
12
12
  }
13
13
  };
14
+ /** attaches node n, rendered from h by the components hs (outermost first), to them */
15
+ let attach = (n, h, hs) => {
16
+ n.h = hs[0] ?? h;
17
+ n.cs = hs.filter(x => x.kind == 3);
18
+ };
14
19
  /** synchronizes children starting at the i-th node. returns the index after the last child synchronized */
15
20
  let syncchildren = (p, cn, i, depth, uc) => {
16
- cn.flat(999).forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)));
21
+ cn.flat(999).forEach((hc) => (i = sync(p, i, hc, [], depth, uc)));
17
22
  return i;
18
23
  };
19
24
  /** class components must render exactly one root node, they are attached to it and update through it */
@@ -21,12 +26,13 @@ let assertnonfragment = (h, hv, i, j) => {
21
26
  if (j - i != 1 || hv?.kind == 4)
22
27
  throw new Error(`${h.tag.name}.view() must render exactly one root element`);
23
28
  };
24
- function sync(parent, i, h, hfun, depth, uc) {
29
+ function sync(parent, i, h, hs, // the components rendering h, outermost first
30
+ depth, uc) {
25
31
  let c = parent.childNodes[i];
26
32
  // null, undefined and booleans render nothing, like in React, so {cond && <b />} works. 0 is still rendered.
27
33
  // nothing still takes its slot, as a placeholder: so the siblings after it keep their position, and with it their state
28
34
  if (h == null || typeof h == 'boolean') {
29
- if (hfun?.kind != 3)
35
+ if (!hs.some(x => x.kind == 3))
30
36
  h = ''; // an empty text node, invisible
31
37
  else {
32
38
  // a class component rendering nothing is attached to a comment, so it keeps its state and can show itself again
@@ -35,7 +41,7 @@ function sync(parent, i, h, hfun, depth, uc) {
35
41
  c ? c.replaceWith(n) : parent.append(n);
36
42
  c = n;
37
43
  }
38
- c.h = hfun;
44
+ attach(c, hs[0], hs);
39
45
  return i + 1;
40
46
  }
41
47
  }
@@ -55,9 +61,9 @@ function sync(parent, i, h, hfun, depth, uc) {
55
61
  }
56
62
  else
57
63
  n = c;
58
- n.h = hfun ?? h; // attach before update(), which may handle the update itself
59
- if (jmcconfig.debug && hfun)
60
- n.setAttribute('comp', hfun.tag.name);
64
+ attach(n, h, hs); // before update(), which may handle the update itself
65
+ if (jmcconfig.debug && hs[0])
66
+ n.setAttribute('comp', hs[0].tag.name);
61
67
  if (!fresh && p.update?.(n, uc))
62
68
  return i + 1;
63
69
  // a const element is an island: jmc renders it once, then foreign code owns it. update() still runs
@@ -76,19 +82,25 @@ function sync(parent, i, h, hfun, depth, uc) {
76
82
  // function component
77
83
  // TBD REMOVE the second argument once we know it is unused: the children are in h.p.children
78
84
  let hr = h.tag(h.p, h.cn);
79
- return sync(parent, i, hr, hfun ?? h, depth, uc);
85
+ return sync(parent, i, hr, [...hs, h], depth, uc);
80
86
  }
81
87
  case 3: {
82
- // class component. reuses the instance attached to c, if c renders the same class
83
- let old = c?.h?.tag == h.tag ? c.h : undefined;
88
+ // class component. reuses the instance attached to c, if c renders the same class at the same position
89
+ let outer = hs.filter(x => x.kind == 3);
90
+ let old = c?.cs?.[outer.length];
91
+ if (old?.tag != h.tag)
92
+ old = undefined;
84
93
  let ci = old ? old.i : (h.i = rebind(new h.tag(h.p)));
85
94
  if (old) {
86
- ci.props = old.p = h.p; // set attached p too, for a repeatable update through c.h
87
- if (ci.update?.(uc))
88
- return i + 1; // update() substitutes view/sync when returning truthy
95
+ ci.props = old.p = h.p; // set attached p too, for a repeatable update through the node
96
+ if (ci.update?.(uc)) { // update() substitutes view/sync when returning truthy
97
+ c.h = hs[0] ?? old; // the components below old stay attached
98
+ c.cs = [...outer, ...c.cs.slice(outer.length)];
99
+ return i + 1;
100
+ }
89
101
  }
90
102
  let hv = ci.view();
91
- let j = sync(parent, i, hv, old ?? h, depth, uc); // the attached H, so updates find the instance
103
+ let j = sync(parent, i, hv, [...hs, old ?? h], depth, uc); // the attached H, so updates find the instance
92
104
  assertnonfragment(h, hv, i, j);
93
105
  ci.element = parent.childNodes[i];
94
106
  if (!old && ci.mounted)
@@ -96,7 +108,7 @@ function sync(parent, i, h, hfun, depth, uc) {
96
108
  return j;
97
109
  }
98
110
  case 4: {
99
- // fragment. hfun is deliberately not passed on: a component returning a fragment is not attached
111
+ // fragment. hs is deliberately not passed on: a component returning a fragment is not attached
100
112
  // to any node, so it cannot update itself, only its parent can update it
101
113
  return syncchildren(parent, h.cn ?? [], i, --depth, uc);
102
114
  }
@@ -122,7 +134,7 @@ export function patch(e, h, depth = 999, uc = {}) {
122
134
  }
123
135
  let p = e.parentElement;
124
136
  let i = [...p.childNodes].indexOf(e);
125
- sync(p, i, h, undefined, depth, uc);
137
+ sync(p, i, h, [], depth, uc);
126
138
  // lifecycle phase: the dom is complete now. taken first, a mounted callback calling updateview collects anew
127
139
  let ms = mounts;
128
140
  mounts = [];
package/dist/jmc.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"jmc.js","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAClC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAgBhD,8GAA8G;AAC9G,IAAI,MAAM,GAAmB,EAAE,CAAA;AAE/B,IAAI,oBAAoB,GAAG,CAAC,CAAU,EAAE,CAAS,EAAE,EAAE;IACjD,IAAI,CAAY,CAAA;IAChB,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3B,IAAI,CAAC;YACD,CAAC,CAAC,MAAM,EAAE,CAAA;QACd,CAAC;QAAC,MAAM,CAAC,CAAC,CAAC,CAAC,4CAA4C;IAC5D,CAAC;AACL,CAAC,CAAA;AAED,2GAA2G;AAC3G,IAAI,YAAY,GAAG,CACf,CAAU,EACV,EAAY,EACZ,CAAS,EACT,KAAa,EACb,EAAK,EACC,EAAE;IACR,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,CAAA;IACxE,OAAO,CAAC,CAAA;AACZ,CAAC,CAAA;AAED,wGAAwG;AACxG,IAAI,iBAAiB,GAAG,CAAC,CAAkB,EAAE,EAAM,EAAE,CAAS,EAAE,CAAS,EAAE,EAAE;IACzE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,IAAI,IAAI,CAAC;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G,CAAC,CAAA;AACnB,CAAC","sourcesContent":["import type {\r\n H,\r\n HElement,\r\n HComp,\r\n J,\r\n Command,\r\n Children,\r\n HClassComponent,\r\n HU,\r\n} from './h.js'\r\nimport { rebind } from './base.js'\r\nimport { jmcconfig, setprops } from './props.js'\r\n\r\nconst enum NodeType {\r\n // vaporizes (but for that must be in this file, otherwise not)\r\n TextNode = 3,\r\n Comment = 8,\r\n}\r\n\r\n/** a node as jmc sees it. kept module-local, so consumers' Node type stays untouched */\r\ntype JNode<N extends Node = ChildNode> = N & {\r\n /** the H this node updates from: the element's own H, or the outermost component rendering it */\r\n h?: HElement | HComp\r\n /** props last applied to this element. kept apart from h, which may be the owning component's H */\r\n ep?: J\r\n}\r\n\r\n/** mounted callbacks collected by sync, run after it in the lifecycle phase. children before their parents */\r\nlet mounts: (() => void)[] = []\r\n\r\nlet removeexcesschildren = (n: Element, i: number) => {\r\n let c: ChildNode\r\n while ((c = n.childNodes[i])) {\r\n try {\r\n c.remove()\r\n } catch { } // due to blur-event-handlers, this can fail\r\n }\r\n}\r\n\r\n/** synchronizes children starting at the i-th node. returns the index after the last child synchronized */\r\nlet syncchildren = (\r\n p: Element,\r\n cn: Children,\r\n i: number,\r\n depth: number,\r\n uc: J,\r\n): number => {\r\n cn.flat(999).forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)))\r\n return i\r\n}\r\n\r\n/** class components must render exactly one root node, they are attached to it and update through it */\r\nlet assertnonfragment = (h: HClassComponent, hv: HU, i: number, j: number) => {\r\n if (j - i != 1 || hv?.kind == 4) throw new Error(`${h.tag.name}.view() must render exactly one root element`)\r\n}\r\n\r\nfunction sync(\r\n parent: Element,\r\n i: number,\r\n h: HU,\r\n hfun: HComp | undefined,\r\n depth: number,\r\n uc: J,\r\n): number {\r\n let c: JNode | undefined = parent.childNodes[i]\r\n\r\n // null, undefined and booleans render nothing, like in React, so {cond && <b />} works. 0 is still rendered.\r\n // nothing still takes its slot, as a placeholder: so the siblings after it keep their position, and with it their state\r\n if (h == null || typeof h == 'boolean') {\r\n if (hfun?.kind != 3) h = '' // an empty text node, invisible\r\n else {\r\n // a class component rendering nothing is attached to a comment, so it keeps its state and can show itself again\r\n if (c?.nodeType != NodeType.Comment) {\r\n let n = new Comment()\r\n c ? c.replaceWith(n) : parent.append(n)\r\n c = n\r\n }\r\n c.h = hfun\r\n return i + 1\r\n }\r\n }\r\n\r\n switch (h.kind) {\r\n case 1: {\r\n // element\r\n let n: Element & JNode\r\n let { tag, cn = [], p } = h\r\n\r\n let fresh = (<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()\r\n if (fresh) {\r\n let ns =\r\n p.xmlns ??\r\n (parent.tagName == 'foreignObject'\r\n ? 'http://www.w3.org/1999/xhtml'\r\n : parent.namespaceURI)\r\n n = document.createElementNS(ns, tag)\r\n c ? c.replaceWith(n) : parent.appendChild(n)\r\n } else n = c as Element & JNode\r\n\r\n n.h = hfun ?? h // attach before update(), which may handle the update itself\r\n if (jmcconfig.debug && hfun) n.setAttribute('comp', hfun.tag.name)\r\n if (!fresh && p.update?.(n, uc)) return i + 1\r\n\r\n // a const element is an island: jmc renders it once, then foreign code owns it. update() still runs\r\n if (fresh || !p.const) {\r\n n.ep = setprops(n, n.ep, p)\r\n if (--depth && !tag.includes('-')) { // web components render their own children\r\n const j = syncchildren(n, cn, 0, depth, uc)\r\n removeexcesschildren(n, j)\r\n }\r\n }\r\n if (fresh && p.mounted) mounts.push(() => p.mounted(n))\r\n\r\n return i + 1\r\n }\r\n\r\n case 2: {\r\n // function component\r\n // TBD REMOVE the second argument once we know it is unused: the children are in h.p.children\r\n let hr = h.tag(h.p, h.cn)\r\n return sync(parent, i, hr, hfun ?? h, depth, uc)\r\n }\r\n\r\n case 3: {\r\n // class component. reuses the instance attached to c, if c renders the same class\r\n let old = c?.h?.tag == h.tag ? c.h as HClassComponent : undefined\r\n let ci = old ? old.i! : (h.i = rebind(new h.tag(h.p)))\r\n if (old) {\r\n ci.props = old.p = h.p // set attached p too, for a repeatable update through c.h\r\n if (ci.update?.(uc)) return i + 1 // update() substitutes view/sync when returning truthy\r\n }\r\n let hv = ci.view()\r\n let j = sync(parent, i, hv, old ?? h, depth, uc) // the attached H, so updates find the instance\r\n assertnonfragment(h, hv, i, j)\r\n ci.element = parent.childNodes[i]\r\n if (!old && ci.mounted) mounts.push(() => ci.mounted!())\r\n return j\r\n }\r\n\r\n case 4: {\r\n // fragment. hfun is deliberately not passed on: a component returning a fragment is not attached\r\n // to any node, so it cannot update itself, only its parent can update it\r\n return syncchildren(parent, h.cn ?? [], i, --depth, uc)\r\n }\r\n\r\n default:\r\n // textnode\r\n let s = h.toString()\r\n if (c?.nodeType == NodeType.TextNode) {\r\n if (c.textContent != s) c.textContent = s // firefox updates equal text, loosing existing text selection\r\n } else {\r\n c ? c.replaceWith(s) : parent.append(s)\r\n }\r\n return i + 1\r\n }\r\n}\r\n\r\nexport function patch(\r\n e: Node | null,\r\n h: H,\r\n depth = 999,\r\n uc: J = {},\r\n): HTMLElement | undefined {\r\n if (!e) return\r\n if (depth == 0) {\r\n (e as Element).replaceChildren()\r\n depth = 999\r\n }\r\n let p = e.parentElement!\r\n let i = [...p.childNodes].indexOf(e as ChildNode)\r\n sync(p, i, h, undefined, depth, uc)\r\n\r\n // lifecycle phase: the dom is complete now. taken first, a mounted callback calling updateview collects anew\r\n let ms = mounts\r\n mounts = []\r\n ms.forEach(m => m())\r\n\r\n return p.childNodes[i] as HTMLElement\r\n}\r\n\r\nexport function updateview(...cmds: Command[]): void {\r\n if (!cmds.some(c => typeof c == 'string' || c instanceof Node)) cmds.push('body') // no target given: body\r\n let uc: J = {}\r\n let depth = 999\r\n\r\n // isConnected: a selector's matches are collected upfront, patching one can replace a later one\r\n let up = (n: JNode<Node>) => { if (n.h && n.isConnected) patch(n, n.h, depth, uc) }\r\n\r\n for (const c of cmds)\r\n if (typeof c == 'string') document.querySelectorAll(c).forEach(up)\r\n else if (c instanceof Node) up(c)\r\n else if (typeof c == 'number') dep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I,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,QAAQ,IAAI,CAAC,YAAY,IAAI,CAAC;QAAE,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA,CAAC,wBAAwB;IAC1G,IAAI,EAAE,GAAM,EAAE,CAAA;IACd,IAAI,KAAK,GAAG,GAAG,CAAA;IAEf,gGAAgG;IAChG,IAAI,EAAE,GAAG,CAAC,CAAc,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,WAAW;QAAE,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAA,CAAC,CAAC,CAAA;IAEnF,KAAK,MAAM,CAAC,IAAI,IAAI;QAChB,IAAI,OAAO,CAAC,IAAI,QAAQ;YAAE,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;aAC7D,IAAI,CAAC,YAAY,IAAI;YAAE,EAAE,CAAC,CAAC,CAAC,CAAA;aAC5B,IAAI,OAAO,CAAC,IAAI,QAAQ;YAAE,KAAK,GAAG,CAAC,CAAA;;YACnC,EAAE,GAAG,CAAC,CAAA;AACnB,CAAC","sourcesContent":["import type {\n H,\n HElement,\n HComp,\n J,\n Command,\n Children,\n HClassComponent,\n HU,\n} from './h.js'\nimport { rebind } from './base.js'\nimport { jmcconfig, setprops } from './props.js'\n\nconst enum NodeType {\n // vaporizes (but for that must be in this file, otherwise not)\n TextNode = 3,\n Comment = 8,\n}\n\n/** a node as jmc sees it. kept module-local, so consumers' Node type stays untouched */\ntype JNode<N extends Node = ChildNode> = N & {\n /** the H this node updates from: the element's own H, or the outermost component rendering it */\n h?: HElement | HComp\n /** the class components rendering this node, outermost first. they hold the instances, found again by position */\n cs?: HClassComponent[]\n /** props last applied to this element. kept apart from h, which may be the owning component's H */\n ep?: J\n}\n\n/** mounted callbacks collected by sync, run after it in the lifecycle phase. children before their parents */\nlet mounts: (() => void)[] = []\n\nlet removeexcesschildren = (n: Element, i: number) => {\n let c: ChildNode\n while ((c = n.childNodes[i])) {\n try {\n c.remove()\n } catch { } // due to blur-event-handlers, this can fail\n }\n}\n\n/** attaches node n, rendered from h by the components hs (outermost first), to them */\nlet attach = (n: JNode, h: HElement | HComp, hs: HComp[]) => {\n n.h = hs[0] ?? h\n n.cs = hs.filter(x => x.kind == 3)\n}\n\n/** synchronizes children starting at the i-th node. returns the index after the last child synchronized */\nlet syncchildren = (\n p: Element,\n cn: Children,\n i: number,\n depth: number,\n uc: J,\n): number => {\n cn.flat(999).forEach((hc) => (i = sync(p, i, hc, [], depth, uc)))\n return i\n}\n\n/** class components must render exactly one root node, they are attached to it and update through it */\nlet assertnonfragment = (h: HClassComponent, hv: HU, i: number, j: number) => {\n if (j - i != 1 || hv?.kind == 4) throw new Error(`${h.tag.name}.view() must render exactly one root element`)\n}\n\nfunction sync(\n parent: Element,\n i: number,\n h: HU,\n hs: HComp[], // the components rendering h, outermost first\n depth: number,\n uc: J,\n): number {\n let c: JNode | undefined = parent.childNodes[i]\n\n // null, undefined and booleans render nothing, like in React, so {cond && <b />} works. 0 is still rendered.\n // nothing still takes its slot, as a placeholder: so the siblings after it keep their position, and with it their state\n if (h == null || typeof h == 'boolean') {\n if (!hs.some(x => x.kind == 3)) h = '' // an empty text node, invisible\n else {\n // a class component rendering nothing is attached to a comment, so it keeps its state and can show itself again\n if (c?.nodeType != NodeType.Comment) {\n let n = new Comment()\n c ? c.replaceWith(n) : parent.append(n)\n c = n\n }\n attach(c, hs[0], hs)\n return i + 1\n }\n }\n\n switch (h.kind) {\n case 1: {\n // element\n let n: Element & JNode\n let { tag, cn = [], p } = h\n\n let fresh = (<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()\n if (fresh) {\n let ns =\n p.xmlns ??\n (parent.tagName == 'foreignObject'\n ? 'http://www.w3.org/1999/xhtml'\n : parent.namespaceURI)\n n = document.createElementNS(ns, tag)\n c ? c.replaceWith(n) : parent.appendChild(n)\n } else n = c as Element & JNode\n\n attach(n, h, hs) // before update(), which may handle the update itself\n if (jmcconfig.debug && hs[0]) n.setAttribute('comp', hs[0].tag.name)\n if (!fresh && p.update?.(n, uc)) return i + 1\n\n // a const element is an island: jmc renders it once, then foreign code owns it. update() still runs\n if (fresh || !p.const) {\n n.ep = setprops(n, n.ep, p)\n if (--depth && !tag.includes('-')) { // web components render their own children\n const j = syncchildren(n, cn, 0, depth, uc)\n removeexcesschildren(n, j)\n }\n }\n if (fresh && p.mounted) mounts.push(() => p.mounted(n))\n\n return i + 1\n }\n\n case 2: {\n // function component\n // TBD REMOVE the second argument once we know it is unused: the children are in h.p.children\n let hr = h.tag(h.p, h.cn)\n return sync(parent, i, hr, [...hs, h], depth, uc)\n }\n\n case 3: {\n // class component. reuses the instance attached to c, if c renders the same class at the same position\n let outer = hs.filter(x => x.kind == 3)\n let old = c?.cs?.[outer.length]\n if (old?.tag != h.tag) old = undefined\n let ci = old ? old.i! : (h.i = rebind(new h.tag(h.p)))\n if (old) {\n ci.props = old.p = h.p // set attached p too, for a repeatable update through the node\n if (ci.update?.(uc)) { // update() substitutes view/sync when returning truthy\n c!.h = hs[0] ?? old // the components below old stay attached\n c!.cs = [...outer, ...c!.cs!.slice(outer.length)]\n return i + 1\n }\n }\n let hv = ci.view()\n let j = sync(parent, i, hv, [...hs, old ?? h], depth, uc) // the attached H, so updates find the instance\n assertnonfragment(h, hv, i, j)\n ci.element = parent.childNodes[i]\n if (!old && ci.mounted) mounts.push(() => ci.mounted!())\n return j\n }\n\n case 4: {\n // fragment. hs is deliberately not passed on: a component returning a fragment is not attached\n // to any node, so it cannot update itself, only its parent can update it\n return syncchildren(parent, h.cn ?? [], i, --depth, uc)\n }\n\n default:\n // textnode\n let s = h.toString()\n if (c?.nodeType == NodeType.TextNode) {\n if (c.textContent != s) c.textContent = s // firefox updates equal text, loosing existing text selection\n } else {\n c ? c.replaceWith(s) : parent.append(s)\n }\n return i + 1\n }\n}\n\nexport function patch(\n e: Node | null,\n h: H,\n depth = 999,\n uc: J = {},\n): HTMLElement | undefined {\n if (!e) return\n if (depth == 0) {\n (e as Element).replaceChildren()\n depth = 999\n }\n let p = e.parentElement!\n let i = [...p.childNodes].indexOf(e as ChildNode)\n sync(p, i, h, [], depth, uc)\n\n // lifecycle phase: the dom is complete now. taken first, a mounted callback calling updateview collects anew\n let ms = mounts\n mounts = []\n ms.forEach(m => m())\n\n return p.childNodes[i] as HTMLElement\n}\n\nexport function updateview(...cmds: Command[]): void {\n if (!cmds.some(c => typeof c == 'string' || c instanceof Node)) cmds.push('body') // no target given: body\n let uc: J = {}\n let depth = 999\n\n // isConnected: a selector's matches are collected upfront, patching one can replace a later one\n let up = (n: JNode<Node>) => { if (n.h && n.isConnected) patch(n, n.h, depth, uc) }\n\n for (const c of cmds)\n if (typeof c == 'string') document.querySelectorAll(c).forEach(up)\n else if (c instanceof Node) up(c)\n else if (typeof c == 'number') depth = c\n else uc = c\n}\n"]}
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "homepage": "https://github.com/snuup/jmc#readme",
19
19
  "bugs": "https://github.com/snuup/jmc/issues",
20
- "version": "1.4.1",
20
+ "version": "1.4.2",
21
21
  "publishConfig": {
22
22
  "access": "public"
23
23
  },
package/src/jmc.ts CHANGED
@@ -1,194 +1,208 @@
1
- import type {
2
- H,
3
- HElement,
4
- HComp,
5
- J,
6
- Command,
7
- Children,
8
- HClassComponent,
9
- HU,
10
- } from './h.js'
11
- import { rebind } from './base.js'
12
- import { jmcconfig, setprops } from './props.js'
13
-
14
- const enum NodeType {
15
- // vaporizes (but for that must be in this file, otherwise not)
16
- TextNode = 3,
17
- Comment = 8,
18
- }
19
-
20
- /** a node as jmc sees it. kept module-local, so consumers' Node type stays untouched */
21
- type JNode<N extends Node = ChildNode> = N & {
22
- /** the H this node updates from: the element's own H, or the outermost component rendering it */
23
- h?: HElement | HComp
24
- /** props last applied to this element. kept apart from h, which may be the owning component's H */
25
- ep?: J
26
- }
27
-
28
- /** mounted callbacks collected by sync, run after it in the lifecycle phase. children before their parents */
29
- let mounts: (() => void)[] = []
30
-
31
- let removeexcesschildren = (n: Element, i: number) => {
32
- let c: ChildNode
33
- while ((c = n.childNodes[i])) {
34
- try {
35
- c.remove()
36
- } catch { } // due to blur-event-handlers, this can fail
37
- }
38
- }
39
-
40
- /** synchronizes children starting at the i-th node. returns the index after the last child synchronized */
41
- let syncchildren = (
42
- p: Element,
43
- cn: Children,
44
- i: number,
45
- depth: number,
46
- uc: J,
47
- ): number => {
48
- cn.flat(999).forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)))
49
- return i
50
- }
51
-
52
- /** class components must render exactly one root node, they are attached to it and update through it */
53
- let assertnonfragment = (h: HClassComponent, hv: HU, i: number, j: number) => {
54
- if (j - i != 1 || hv?.kind == 4) throw new Error(`${h.tag.name}.view() must render exactly one root element`)
55
- }
56
-
57
- function sync(
58
- parent: Element,
59
- i: number,
60
- h: HU,
61
- hfun: HComp | undefined,
62
- depth: number,
63
- uc: J,
64
- ): number {
65
- let c: JNode | undefined = parent.childNodes[i]
66
-
67
- // null, undefined and booleans render nothing, like in React, so {cond && <b />} works. 0 is still rendered.
68
- // nothing still takes its slot, as a placeholder: so the siblings after it keep their position, and with it their state
69
- if (h == null || typeof h == 'boolean') {
70
- if (hfun?.kind != 3) h = '' // an empty text node, invisible
71
- else {
72
- // a class component rendering nothing is attached to a comment, so it keeps its state and can show itself again
73
- if (c?.nodeType != NodeType.Comment) {
74
- let n = new Comment()
75
- c ? c.replaceWith(n) : parent.append(n)
76
- c = n
77
- }
78
- c.h = hfun
79
- return i + 1
80
- }
81
- }
82
-
83
- switch (h.kind) {
84
- case 1: {
85
- // element
86
- let n: Element & JNode
87
- let { tag, cn = [], p } = h
88
-
89
- let fresh = (<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()
90
- if (fresh) {
91
- let ns =
92
- p.xmlns ??
93
- (parent.tagName == 'foreignObject'
94
- ? 'http://www.w3.org/1999/xhtml'
95
- : parent.namespaceURI)
96
- n = document.createElementNS(ns, tag)
97
- c ? c.replaceWith(n) : parent.appendChild(n)
98
- } else n = c as Element & JNode
99
-
100
- n.h = hfun ?? h // attach before update(), which may handle the update itself
101
- if (jmcconfig.debug && hfun) n.setAttribute('comp', hfun.tag.name)
102
- if (!fresh && p.update?.(n, uc)) return i + 1
103
-
104
- // a const element is an island: jmc renders it once, then foreign code owns it. update() still runs
105
- if (fresh || !p.const) {
106
- n.ep = setprops(n, n.ep, p)
107
- if (--depth && !tag.includes('-')) { // web components render their own children
108
- const j = syncchildren(n, cn, 0, depth, uc)
109
- removeexcesschildren(n, j)
110
- }
111
- }
112
- if (fresh && p.mounted) mounts.push(() => p.mounted(n))
113
-
114
- return i + 1
115
- }
116
-
117
- case 2: {
118
- // function component
119
- // TBD REMOVE the second argument once we know it is unused: the children are in h.p.children
120
- let hr = h.tag(h.p, h.cn)
121
- return sync(parent, i, hr, hfun ?? h, depth, uc)
122
- }
123
-
124
- case 3: {
125
- // class component. reuses the instance attached to c, if c renders the same class
126
- let old = c?.h?.tag == h.tag ? c.h as HClassComponent : undefined
127
- let ci = old ? old.i! : (h.i = rebind(new h.tag(h.p)))
128
- if (old) {
129
- ci.props = old.p = h.p // set attached p too, for a repeatable update through c.h
130
- if (ci.update?.(uc)) return i + 1 // update() substitutes view/sync when returning truthy
131
- }
132
- let hv = ci.view()
133
- let j = sync(parent, i, hv, old ?? h, depth, uc) // the attached H, so updates find the instance
134
- assertnonfragment(h, hv, i, j)
135
- ci.element = parent.childNodes[i]
136
- if (!old && ci.mounted) mounts.push(() => ci.mounted!())
137
- return j
138
- }
139
-
140
- case 4: {
141
- // fragment. hfun is deliberately not passed on: a component returning a fragment is not attached
142
- // to any node, so it cannot update itself, only its parent can update it
143
- return syncchildren(parent, h.cn ?? [], i, --depth, uc)
144
- }
145
-
146
- default:
147
- // textnode
148
- let s = h.toString()
149
- if (c?.nodeType == NodeType.TextNode) {
150
- if (c.textContent != s) c.textContent = s // firefox updates equal text, loosing existing text selection
151
- } else {
152
- c ? c.replaceWith(s) : parent.append(s)
153
- }
154
- return i + 1
155
- }
156
- }
157
-
158
- export function patch(
159
- e: Node | null,
160
- h: H,
161
- depth = 999,
162
- uc: J = {},
163
- ): HTMLElement | undefined {
164
- if (!e) return
165
- if (depth == 0) {
166
- (e as Element).replaceChildren()
167
- depth = 999
168
- }
169
- let p = e.parentElement!
170
- let i = [...p.childNodes].indexOf(e as ChildNode)
171
- sync(p, i, h, undefined, depth, uc)
172
-
173
- // lifecycle phase: the dom is complete now. taken first, a mounted callback calling updateview collects anew
174
- let ms = mounts
175
- mounts = []
176
- ms.forEach(m => m())
177
-
178
- return p.childNodes[i] as HTMLElement
179
- }
180
-
181
- export function updateview(...cmds: Command[]): void {
182
- if (!cmds.some(c => typeof c == 'string' || c instanceof Node)) cmds.push('body') // no target given: body
183
- let uc: J = {}
184
- let depth = 999
185
-
186
- // isConnected: a selector's matches are collected upfront, patching one can replace a later one
187
- let up = (n: JNode<Node>) => { if (n.h && n.isConnected) patch(n, n.h, depth, uc) }
188
-
189
- for (const c of cmds)
190
- if (typeof c == 'string') document.querySelectorAll(c).forEach(up)
191
- else if (c instanceof Node) up(c)
192
- else if (typeof c == 'number') depth = c
193
- else uc = c
194
- }
1
+ import type {
2
+ H,
3
+ HElement,
4
+ HComp,
5
+ J,
6
+ Command,
7
+ Children,
8
+ HClassComponent,
9
+ HU,
10
+ } from './h.js'
11
+ import { rebind } from './base.js'
12
+ import { jmcconfig, setprops } from './props.js'
13
+
14
+ const enum NodeType {
15
+ // vaporizes (but for that must be in this file, otherwise not)
16
+ TextNode = 3,
17
+ Comment = 8,
18
+ }
19
+
20
+ /** a node as jmc sees it. kept module-local, so consumers' Node type stays untouched */
21
+ type JNode<N extends Node = ChildNode> = N & {
22
+ /** the H this node updates from: the element's own H, or the outermost component rendering it */
23
+ h?: HElement | HComp
24
+ /** the class components rendering this node, outermost first. they hold the instances, found again by position */
25
+ cs?: HClassComponent[]
26
+ /** props last applied to this element. kept apart from h, which may be the owning component's H */
27
+ ep?: J
28
+ }
29
+
30
+ /** mounted callbacks collected by sync, run after it in the lifecycle phase. children before their parents */
31
+ let mounts: (() => void)[] = []
32
+
33
+ let removeexcesschildren = (n: Element, i: number) => {
34
+ let c: ChildNode
35
+ while ((c = n.childNodes[i])) {
36
+ try {
37
+ c.remove()
38
+ } catch { } // due to blur-event-handlers, this can fail
39
+ }
40
+ }
41
+
42
+ /** attaches node n, rendered from h by the components hs (outermost first), to them */
43
+ let attach = (n: JNode, h: HElement | HComp, hs: HComp[]) => {
44
+ n.h = hs[0] ?? h
45
+ n.cs = hs.filter(x => x.kind == 3)
46
+ }
47
+
48
+ /** synchronizes children starting at the i-th node. returns the index after the last child synchronized */
49
+ let syncchildren = (
50
+ p: Element,
51
+ cn: Children,
52
+ i: number,
53
+ depth: number,
54
+ uc: J,
55
+ ): number => {
56
+ cn.flat(999).forEach((hc) => (i = sync(p, i, hc, [], depth, uc)))
57
+ return i
58
+ }
59
+
60
+ /** class components must render exactly one root node, they are attached to it and update through it */
61
+ let assertnonfragment = (h: HClassComponent, hv: HU, i: number, j: number) => {
62
+ if (j - i != 1 || hv?.kind == 4) throw new Error(`${h.tag.name}.view() must render exactly one root element`)
63
+ }
64
+
65
+ function sync(
66
+ parent: Element,
67
+ i: number,
68
+ h: HU,
69
+ hs: HComp[], // the components rendering h, outermost first
70
+ depth: number,
71
+ uc: J,
72
+ ): number {
73
+ let c: JNode | undefined = parent.childNodes[i]
74
+
75
+ // null, undefined and booleans render nothing, like in React, so {cond && <b />} works. 0 is still rendered.
76
+ // nothing still takes its slot, as a placeholder: so the siblings after it keep their position, and with it their state
77
+ if (h == null || typeof h == 'boolean') {
78
+ if (!hs.some(x => x.kind == 3)) h = '' // an empty text node, invisible
79
+ else {
80
+ // a class component rendering nothing is attached to a comment, so it keeps its state and can show itself again
81
+ if (c?.nodeType != NodeType.Comment) {
82
+ let n = new Comment()
83
+ c ? c.replaceWith(n) : parent.append(n)
84
+ c = n
85
+ }
86
+ attach(c, hs[0], hs)
87
+ return i + 1
88
+ }
89
+ }
90
+
91
+ switch (h.kind) {
92
+ case 1: {
93
+ // element
94
+ let n: Element & JNode
95
+ let { tag, cn = [], p } = h
96
+
97
+ let fresh = (<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()
98
+ if (fresh) {
99
+ let ns =
100
+ p.xmlns ??
101
+ (parent.tagName == 'foreignObject'
102
+ ? 'http://www.w3.org/1999/xhtml'
103
+ : parent.namespaceURI)
104
+ n = document.createElementNS(ns, tag)
105
+ c ? c.replaceWith(n) : parent.appendChild(n)
106
+ } else n = c as Element & JNode
107
+
108
+ attach(n, h, hs) // before update(), which may handle the update itself
109
+ if (jmcconfig.debug && hs[0]) n.setAttribute('comp', hs[0].tag.name)
110
+ if (!fresh && p.update?.(n, uc)) return i + 1
111
+
112
+ // a const element is an island: jmc renders it once, then foreign code owns it. update() still runs
113
+ if (fresh || !p.const) {
114
+ n.ep = setprops(n, n.ep, p)
115
+ if (--depth && !tag.includes('-')) { // web components render their own children
116
+ const j = syncchildren(n, cn, 0, depth, uc)
117
+ removeexcesschildren(n, j)
118
+ }
119
+ }
120
+ if (fresh && p.mounted) mounts.push(() => p.mounted(n))
121
+
122
+ return i + 1
123
+ }
124
+
125
+ case 2: {
126
+ // function component
127
+ // TBD REMOVE the second argument once we know it is unused: the children are in h.p.children
128
+ let hr = h.tag(h.p, h.cn)
129
+ return sync(parent, i, hr, [...hs, h], depth, uc)
130
+ }
131
+
132
+ case 3: {
133
+ // class component. reuses the instance attached to c, if c renders the same class at the same position
134
+ let outer = hs.filter(x => x.kind == 3)
135
+ let old = c?.cs?.[outer.length]
136
+ if (old?.tag != h.tag) old = undefined
137
+ let ci = old ? old.i! : (h.i = rebind(new h.tag(h.p)))
138
+ if (old) {
139
+ ci.props = old.p = h.p // set attached p too, for a repeatable update through the node
140
+ if (ci.update?.(uc)) { // update() substitutes view/sync when returning truthy
141
+ c!.h = hs[0] ?? old // the components below old stay attached
142
+ c!.cs = [...outer, ...c!.cs!.slice(outer.length)]
143
+ return i + 1
144
+ }
145
+ }
146
+ let hv = ci.view()
147
+ let j = sync(parent, i, hv, [...hs, old ?? h], depth, uc) // the attached H, so updates find the instance
148
+ assertnonfragment(h, hv, i, j)
149
+ ci.element = parent.childNodes[i]
150
+ if (!old && ci.mounted) mounts.push(() => ci.mounted!())
151
+ return j
152
+ }
153
+
154
+ case 4: {
155
+ // fragment. hs is deliberately not passed on: a component returning a fragment is not attached
156
+ // to any node, so it cannot update itself, only its parent can update it
157
+ return syncchildren(parent, h.cn ?? [], i, --depth, uc)
158
+ }
159
+
160
+ default:
161
+ // textnode
162
+ let s = h.toString()
163
+ if (c?.nodeType == NodeType.TextNode) {
164
+ if (c.textContent != s) c.textContent = s // firefox updates equal text, loosing existing text selection
165
+ } else {
166
+ c ? c.replaceWith(s) : parent.append(s)
167
+ }
168
+ return i + 1
169
+ }
170
+ }
171
+
172
+ export function patch(
173
+ e: Node | null,
174
+ h: H,
175
+ depth = 999,
176
+ uc: J = {},
177
+ ): HTMLElement | undefined {
178
+ if (!e) return
179
+ if (depth == 0) {
180
+ (e as Element).replaceChildren()
181
+ depth = 999
182
+ }
183
+ let p = e.parentElement!
184
+ let i = [...p.childNodes].indexOf(e as ChildNode)
185
+ sync(p, i, h, [], depth, uc)
186
+
187
+ // lifecycle phase: the dom is complete now. taken first, a mounted callback calling updateview collects anew
188
+ let ms = mounts
189
+ mounts = []
190
+ ms.forEach(m => m())
191
+
192
+ return p.childNodes[i] as HTMLElement
193
+ }
194
+
195
+ export function updateview(...cmds: Command[]): void {
196
+ if (!cmds.some(c => typeof c == 'string' || c instanceof Node)) cmds.push('body') // no target given: body
197
+ let uc: J = {}
198
+ let depth = 999
199
+
200
+ // isConnected: a selector's matches are collected upfront, patching one can replace a later one
201
+ let up = (n: JNode<Node>) => { if (n.h && n.isConnected) patch(n, n.h, depth, uc) }
202
+
203
+ for (const c of cmds)
204
+ if (typeof c == 'string') document.querySelectorAll(c).forEach(up)
205
+ else if (c instanceof Node) up(c)
206
+ else if (typeof c == 'number') depth = c
207
+ else uc = c
208
+ }