@snupo/jmc 1.0.60 → 1.0.62

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.
@@ -0,0 +1,9 @@
1
+ declare global {
2
+ interface Element {
3
+ eventmap?: {
4
+ [type: string]: EventListener | null;
5
+ };
6
+ }
7
+ }
8
+ export declare function setevent(el: Element, type: string, handler: EventListener | null): void;
9
+ //# sourceMappingURL=events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAEA,OAAO,CAAC,MAAM,CAAC;IACX,UAAU,OAAO;QACb,QAAQ,CAAC,EAAE;YAAE,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,GAAG,IAAI,CAAA;SAAE,CAAA;KACtD;CACJ;AAED,wBAAgB,QAAQ,CACpB,EAAE,EAAE,OAAO,EACX,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,aAAa,GAAG,IAAI,QAahC"}
package/dist/events.js ADDED
@@ -0,0 +1,16 @@
1
+ export function setevent(el, type, handler) {
2
+ let map = el.eventmap ??= {};
3
+ const old = map[type];
4
+ if (old === handler)
5
+ return;
6
+ if (old)
7
+ el.removeEventListener(type, old);
8
+ if (handler) {
9
+ el.addEventListener(type, handler);
10
+ map[type] = handler;
11
+ }
12
+ else {
13
+ delete map[type];
14
+ }
15
+ }
16
+ //# sourceMappingURL=events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.js","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAQA,MAAM,UAAU,QAAQ,CACpB,EAAW,EACX,IAAY,EACZ,OAA6B;IAE7B,IAAI,GAAG,GAAG,EAAE,CAAC,QAAQ,KAAK,EAAE,CAAA;IAE5B,MAAM,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC,CAAA;IACrB,IAAI,GAAG,KAAK,OAAO;QAAE,OAAM;IAC3B,IAAI,GAAG;QAAE,EAAE,CAAC,mBAAmB,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;IAC1C,IAAI,OAAO,EAAE,CAAC;QACV,EAAE,CAAC,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QAClC,GAAG,CAAC,IAAI,CAAC,GAAG,OAAO,CAAA;IACvB,CAAC;SAAM,CAAC;QACJ,OAAO,GAAG,CAAC,IAAI,CAAC,CAAA;IACpB,CAAC;AACL,CAAC","sourcesContent":["\r\n\r\ndeclare global {\r\n interface Element {\r\n eventmap?: { [type: string]: EventListener | null }\r\n }\r\n}\r\n\r\nexport function setevent(\r\n el: Element,\r\n type: string,\r\n handler: EventListener | null) {\r\n\r\n let map = el.eventmap ??= {}\r\n\r\n const old = map[type]\r\n if (old === handler) return\r\n if (old) el.removeEventListener(type, old)\r\n if (handler) {\r\n el.addEventListener(type, handler)\r\n map[type] = handler\r\n } else {\r\n delete map[type]\r\n }\r\n}"]}
package/dist/jmc.d.ts CHANGED
@@ -6,6 +6,9 @@ declare global {
6
6
  };
7
7
  }
8
8
  }
9
+ export declare let jmcconfig: {
10
+ attributeNames: Set<string>;
11
+ };
9
12
  export declare function patch(e: Node | null, h: H, depth?: number, uc?: J): HTMLElement | undefined;
10
13
  export type Command = Selector | J | number;
11
14
  export declare function updateview(...cmds: Command[]): void;
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,EAAE,CAAC,EAAY,CAAC,EAAE,QAAQ,EAAiC,MAAM,KAAK,CAAA;AAOlF,OAAO,CAAC,MAAM,CAAC;IACX,UAAU,IAAI;QACV,CAAC,CAAC,EAAE,CAAC,GAAG;YAAE,CAAC,CAAC,EAAE,CAAC,CAAA;SAAE,CAAA;KACpB;CACJ;AAiID,wBAAgB,KAAK,CAAC,CAAC,EAAE,IAAI,GAAG,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,SAAM,EAAE,EAAE,GAAS,CAAC,GAAG,WAAW,GAAG,SAAS,CAU9F;AAED,MAAM,MAAM,OAAO,GAAG,QAAQ,GAAG,CAAC,GAAG,MAAM,CAAA;AAE3C,wBAAgB,UAAU,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CA2BnD"}
1
+ {"version":3,"file":"jmc.d.ts","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,CAAC,EAED,CAAC,EACD,QAAQ,EAIX,MAAM,KAAK,CAAA;AASZ,OAAO,CAAC,MAAM,CAAC;IACX,UAAU,IAAI;QACV,CAAC,CAAC,EAAE,CAAC,GAAG;YAAE,CAAC,CAAC,EAAE,CAAC,CAAA;SAAE,CAAA;KACpB;CACJ;AAWD,eAAO,IAAI,SAAS;;CAEnB,CAAA;AAyJD,wBAAgB,KAAK,CACjB,CAAC,EAAE,IAAI,GAAG,IAAI,EACd,CAAC,EAAE,CAAC,EACJ,KAAK,SAAM,EACX,EAAE,GAAS,CAAC,GACb,WAAW,GAAG,SAAS,CAUzB;AAED,MAAM,MAAM,OAAO,GAAG,QAAQ,GAAG,CAAC,GAAG,MAAM,CAAA;AAE3C,wBAAgB,UAAU,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CA0BnD"}
package/dist/jmc.js CHANGED
@@ -1,26 +1,47 @@
1
1
  import { rebind } from './base.js';
2
+ import { setevent } from './events';
2
3
  let iswebcomponent = (h) => h.tag.includes('-');
3
- const isproperty = (e, name, value) => name !== "viewBox" &&
4
+ let attributeNames = new Set([
5
+ 'selected',
6
+ 'viewBox',
7
+ 'list'
8
+ // other HTML attributes where your framework wants attribute semantics
9
+ ]);
10
+ export let jmcconfig = {
11
+ attributeNames
12
+ };
13
+ const isproperty = (e, name, value) => !attributeNames.has(name) &&
4
14
  (value instanceof Object ||
5
- value instanceof Function ||
6
- (e.namespaceURI !== "http://www.w3.org/2000/svg" && name in e && name != 'style'));
15
+ value instanceof Function || (e.namespaceURI !== 'http://www.w3.org/2000/svg' &&
16
+ name in e &&
17
+ name !== 'style'));
7
18
  let setprops = (e, old, now) => {
8
19
  old ??= {};
9
20
  now ??= {};
10
21
  // removals
11
- for (let p in old)
12
- if (!(p in now))
13
- isproperty(e, p, old[p])
14
- ? e[p] = typeof old[p] === 'boolean' ? false : ''
15
- : e.removeAttribute(p);
22
+ for (let p in old) {
23
+ if (!(p in now)) {
24
+ if (p.startsWith('on'))
25
+ setevent(e, p.slice(2), null);
26
+ else if (isproperty(e, p, old[p]))
27
+ e[p] = typeof old[p] === 'boolean' ? false : '';
28
+ else
29
+ e.removeAttribute(p);
30
+ }
31
+ }
16
32
  // updates
17
33
  for (let p in now) {
18
34
  let v = now[p];
19
35
  v = v?.thunk ? v() : v;
20
- if (v != old[p]) {
21
- isproperty(e, p, v)
22
- ? e[p] = v
23
- : e.setAttribute(p, v);
36
+ if (p.startsWith('on'))
37
+ setevent(e, p.slice(2), v);
38
+ else if (v != old[p]) {
39
+ if (isproperty(e, p, v))
40
+ e[p] = v;
41
+ else if ((v === false || v == null) && !p.startsWith('aria-'))
42
+ e.removeAttribute(p);
43
+ else
44
+ e.setAttribute(p, v);
24
45
  }
25
46
  }
26
47
  };
@@ -36,8 +57,8 @@ let removeexcesschildren = (n, i) => {
36
57
  /** synchronizes children starting at the i-th element. returns the index of the last child synchronized */
37
58
  let syncchildren = (p, cn, i, depth, uc) => {
38
59
  cn.flat(999)
39
- .filter(hc => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)
40
- .forEach(hc => (i = sync(p, i, hc, undefined, depth, uc)));
60
+ .filter((hc) => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)
61
+ .forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)));
41
62
  return i;
42
63
  };
43
64
  function sync(parent, i, h, hfun, depth, uc) {
@@ -46,53 +67,57 @@ function sync(parent, i, h, hfun, depth, uc) {
46
67
  return i; // skip. keep 0 and false renderable, per comment below
47
68
  let c = parent.childNodes[i];
48
69
  switch (h.kind) {
49
- case 1:
50
- {
51
- let n;
52
- let { tag, cn, p } = h;
53
- cn ??= [];
54
- let process = () => {
55
- setprops(n, n.h?.p, p);
56
- n.h = hfun ?? h; // sets the new property values
57
- if (--depth && !n.hasAttribute('const') && !iswebcomponent(h)) {
58
- const j = syncchildren(n, cn, 0, depth, uc);
59
- removeexcesschildren(n, j);
60
- }
61
- };
62
- if (c?.tagName?.toLowerCase() != tag.toLowerCase()) {
63
- let ns = p?.xmlns ?? (parent.tagName == "foreignObject" ? 'http://www.w3.org/1999/xhtml' : parent.namespaceURI);
64
- n = document.createElementNS(ns, tag);
65
- c ? c.replaceWith(n) : parent.appendChild(n);
66
- process();
67
- p?.mounted?.(n);
70
+ case 1: {
71
+ let n;
72
+ let { tag, cn, p } = h;
73
+ cn ??= [];
74
+ let process = () => {
75
+ setprops(n, n.h?.p, p);
76
+ n.h = hfun ?? h; // sets the new property values
77
+ if (--depth &&
78
+ !n.hasAttribute('const') &&
79
+ !iswebcomponent(h)) {
80
+ const j = syncchildren(n, cn, 0, depth, uc);
81
+ removeexcesschildren(n, j);
68
82
  }
69
- else {
70
- n = c;
71
- if (p?.update?.(c, uc))
72
- return i + 1;
73
- process();
74
- }
75
- return i + 1;
76
- }
77
- case 4:
78
- {
79
- return syncchildren(parent, h.cn ?? [], i, --depth, uc);
83
+ };
84
+ if (c?.tagName?.toLowerCase() != tag.toLowerCase()) {
85
+ let ns = p?.xmlns ??
86
+ (parent.tagName == 'foreignObject'
87
+ ? 'http://www.w3.org/1999/xhtml'
88
+ : parent.namespaceURI);
89
+ n = document.createElementNS(ns, tag);
90
+ c ? c.replaceWith(n) : parent.appendChild(n);
91
+ process();
92
+ p?.mounted?.(n);
80
93
  }
81
- case 2:
82
- {
83
- let hr = h.tag(h.p, h.cn);
84
- return sync(parent, i, hr, hfun ?? h, depth, uc);
94
+ else {
95
+ n = c;
96
+ if (p?.update?.(c, uc))
97
+ return i + 1;
98
+ process();
85
99
  }
100
+ return i + 1;
101
+ }
102
+ case 4: {
103
+ return syncchildren(parent, h.cn ?? [], i, --depth, uc);
104
+ }
105
+ case 2: {
106
+ let hr = h.tag(h.p, h.cn);
107
+ return sync(parent, i, hr, hfun ?? h, depth, uc);
108
+ }
86
109
  case 3: {
87
110
  if (c?.h?.tag == h.tag) {
88
111
  let ci = c.h.i;
89
112
  ci.props = c.h.p = h.p;
90
113
  c.h.p = h.p; // for repatable update, set attached h.p. alternatively, we could use ci.props in case where h.p is undefined
91
114
  // update() substitutes view/sync when returning truthy
92
- return (ci.update?.(uc)) ? i + 1 : sync(parent, i, ci.view(), c.h, depth, uc); // hfun because outermost function shall be attached
115
+ return ci.update?.(uc)
116
+ ? i + 1
117
+ : sync(parent, i, ci.view(), c.h, depth, uc); // hfun because outermost function shall be attached
93
118
  }
94
119
  else {
95
- let ci = h.i = rebind(new h.tag(h.p));
120
+ let ci = (h.i = rebind(new h.tag(h.p)));
96
121
  let hv = ci.view();
97
122
  let j = sync(parent, i, hv, h, depth, uc);
98
123
  let n = parent.childNodes[i];
@@ -118,6 +143,7 @@ export function patch(e, h, depth = 999, uc = {}) {
118
143
  if (!e)
119
144
  return;
120
145
  if (depth == 0) {
146
+ ;
121
147
  e.replaceChildren();
122
148
  depth = 999;
123
149
  }
@@ -152,7 +178,6 @@ export function updateview(...cmds) {
152
178
  }
153
179
  }
154
180
  f(cmds);
155
- if (!patchcount)
156
- console.warn("updateview did not update anything. cmds=", ...cmds);
181
+ // if (!patchcount) console.warn("updateview did not update anything. cmds=", ...cmds)
157
182
  }
158
183
  //# sourceMappingURL=jmc.js.map
package/dist/jmc.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"jmc.js","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAYlC,IAAI,cAAc,GAAG,CAAC,CAAW,EAAE,EA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,IAAI,CAAC,CAAA;AACvF,CAAC","sourcesContent":["import type { H, HElement, J, Selector, Children, HClassComponent, HU } from './h'\r\nimport { rebind } from './base.js'\r\n\r\nconst enum NodeType { // vaporizes (but for that must be in this file, otherwise not)\r\n TextNode = 3\r\n}\r\n\r\ndeclare global {\r\n interface Node {\r\n h?: H & { p?: J }\r\n }\r\n}\r\n\r\nlet iswebcomponent = (h: HElement) => h.tag.includes('-')\r\n\r\nconst isproperty = (e: Element, name: string, value: any) =>\r\n name !== \"viewBox\" &&\r\n (value instanceof Object ||\r\n value instanceof Function ||\r\n (e.namespaceURI !== \"http://www.w3.org/2000/svg\" && name in e && name != 'style'))\r\n\r\nlet setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {\r\n\r\n old ??= {}\r\n now ??= {}\r\n\r\n // removals\r\n for (let p in old)\r\n if (!(p in now))\r\n isproperty(e, p, old[p])\r\n ? e[p] = typeof old[p] === 'boolean' ? false : ''\r\n : e.removeAttribute(p)\r\n // updates\r\n for (let p in now) {\r\n let v = now[p]\r\n v = v?.thunk ? v() : v\r\n if (v != old[p]) {\r\n isproperty(e, p, v)\r\n ? e[p] = v\r\n : e.setAttribute(p, v)\r\n }\r\n }\r\n}\r\n\r\nlet removeexcesschildren = (n: Element, i: number) => {\r\n let c: ChildNode\r\n while ((c = n.childNodes[i])) {\r\n try { c.remove() } catch (_) { } // due to blur-event-handlers, this can fail\r\n }\r\n}\r\n\r\n/** synchronizes children starting at the i-th element. returns the index of the last child synchronized */\r\nlet syncchildren = (p: Element, cn: Children, i: number, depth: number, uc: J): number => {\r\n cn.flat(999)\r\n .filter(hc => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)\r\n .forEach(hc => (i = sync(p, i, hc, undefined, depth, uc)))\r\n return i\r\n}\r\n\r\nfunction sync(parent: Element, i: number, h: HU, hfun: H | undefined, depth: number, uc: J): number {\r\n\r\n // console.log('%csync', \"background:orange\", parent.tagName, i, h, hfun, depth)\r\n\r\n if (h === null || h === undefined) return i // skip. keep 0 and false renderable, per comment below\r\n\r\n let c = parent.childNodes[i]\r\n\r\n switch (h.kind) {\r\n case 1:\r\n {\r\n let n: Element\r\n let { tag, cn, p } = h\r\n cn ??= []\r\n\r\n let process = () => {\r\n setprops(n, n.h?.p, p)\r\n n.h = hfun ?? h // sets the new property values\r\n if (--depth && !n.hasAttribute('const') && !iswebcomponent(h as HElement)) {\r\n const j = syncchildren(n, cn, 0, depth, uc)\r\n removeexcesschildren(n, j)\r\n }\r\n }\r\n\r\n if ((<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()) {\r\n let ns = p?.xmlns ?? (parent.tagName == \"foreignObject\" ? 'http://www.w3.org/1999/xhtml' : parent.namespaceURI)\r\n n = document.createElementNS(ns, tag)\r\n c ? c.replaceWith(n) : parent.appendChild(n)\r\n process()\r\n p?.mounted?.(n)\r\n } else {\r\n n = c as Element\r\n if (p?.update?.(c, uc)) return i + 1\r\n process()\r\n }\r\n\r\n return i + 1\r\n }\r\n\r\n case 4:\r\n {\r\n return syncchildren(parent, h.cn ?? [], i, --depth, uc)\r\n }\r\n\r\n case 2:\r\n {\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 if ((c?.h as any)?.tag == h.tag) {\r\n let ci = (c.h as HClassComponent).i!\r\n ci.props = c.h!.p = h.p\r\n c.h!.p = h.p // for repatable update, set attached h.p. alternatively, we could use ci.props in case where h.p is undefined\r\n // update() substitutes view/sync when returning truthy\r\n return (ci.update?.(uc)) ? i + 1 : sync(parent, i, ci.view(), c.h, depth, uc) // hfun because outermost function shall be attached\r\n }\r\n else {\r\n let ci = h.i = rebind(new h.tag(h.p))\r\n let hv = ci.view()\r\n let j = sync(parent, i, hv, h, depth, uc)\r\n let n = parent.childNodes[i]!\r\n ci.element = n\r\n ci.mounted?.()\r\n return j\r\n }\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(e: Node | null, h: H, depth = 999, uc = {} as J): HTMLElement | undefined {\r\n if (!e) return\r\n if (depth == 0) {\r\n (e as HTMLElement).replaceChildren()\r\n depth = 999\r\n }\r\n let p = e.parentElement as HTMLElement\r\n let i = [].indexOf.call<any, any, any>(p.childNodes, e)\r\n sync(p, i, h, undefined, depth, uc)\r\n return p.childNodes[i] as HTMLElement\r\n}\r\n\r\nexport type Command = Selector | J | number\r\n\r\nexport function updateview(...cmds: Command[]): void {\r\n\r\n if (!cmds.length) cmds.push('body')\r\n let uc = {}\r\n let depth = 999\r\n let patchcount = 0\r\n\r\n function f(cmds: Command[]) {\r\n for (const c of cmds) {\r\n if (typeof c === 'string') {\r\n f([...document.querySelectorAll(c)])\r\n } else if (c instanceof Node) {\r\n if (c.h) {\r\n patch(c, c.h, depth, uc)\r\n patchcount++\r\n }\r\n } else if (typeof c === 'number') {\r\n depth = c\r\n } else {\r\n uc = c\r\n }\r\n }\r\n }\r\n\r\n f(cmds)\r\n\r\n if (!patchcount) console.warn(\"updateview did not update anything. cmds=\", ...cmds)\r\n}\r\n"]}
1
+ {"version":3,"file":"jmc.js","sourceRoot":"","sources":["../src/jmc.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAClC,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAanC,IAAI,cAAc,GAAG,CAAC,CAAW,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;AAEzD,IAAI,cAAc,GAAG,IAAI,GAAG,CAAC;IACzB,UAAU;IACV,SAAS;IACT,MAAM;IACN,uEAAuE;CAC1E,CAAC,CAAA;AAEF,MAAM,CAAC,IAAI,SA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,YAAY,IAAI,EAAE,CAAC;gBAC3B,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;oBACN,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAA;oBACxB,UAAU,EAAE,CAAA;gBAChB,CAAC;YACL,CAAC;iBAAM,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;gBAC/B,KAAK,GAAG,CAAC,CAAA;YACb,CAAC;iBAAM,CAAC;gBACJ,EAAE,GAAG,CAAC,CAAA;YACV,CAAC;QACL,CAAC;IACL,CAAC;IAED,CAAC,CAAC,IAAI,CAAC,CAAA;IAEP,sFAAsF;AAC1F,CAAC","sourcesContent":["import type {\r\n H,\r\n HElement,\r\n J,\r\n Selector,\r\n Children,\r\n HClassComponent,\r\n HU,\r\n} from './h'\r\nimport { rebind } from './base.js'\r\nimport { setevent } from './events'\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}\r\n\r\ndeclare global {\r\n interface Node {\r\n h?: H & { p?: J }\r\n }\r\n}\r\n\r\nlet iswebcomponent = (h: HElement) => h.tag.includes('-')\r\n\r\nlet attributeNames = new Set([\r\n 'selected',\r\n 'viewBox', \r\n 'list'\r\n // other HTML attributes where your framework wants attribute semantics\r\n])\r\n\r\nexport let jmcconfig = {\r\n attributeNames\r\n}\r\n\r\nconst isproperty = (e: Element, name: string, value: any) =>\r\n !attributeNames.has(name) &&\r\n (value instanceof Object ||\r\n value instanceof Function || (\r\n e.namespaceURI !== 'http://www.w3.org/2000/svg' &&\r\n name in e &&\r\n name !== 'style'))\r\n\r\nlet setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {\r\n old ??= {}\r\n now ??= {}\r\n\r\n // removals\r\n for (let p in old) {\r\n if (!(p in now)) {\r\n if (p.startsWith('on')) setevent(e, p.slice(2), null)\r\n else if (isproperty(e, p, old[p])) e[p] = typeof old[p] === 'boolean' ? false : ''\r\n else e.removeAttribute(p)\r\n }\r\n }\r\n\r\n // updates\r\n for (let p in now) {\r\n let v = now[p]\r\n v = v?.thunk ? v() : v\r\n\r\n if (p.startsWith('on')) setevent(e, p.slice(2), v)\r\n else if (v != old[p]) {\r\n if (isproperty(e, p, v)) e[p] = v\r\n else if ((v === false || v == null) && !p.startsWith('aria-')) e.removeAttribute(p)\r\n else e.setAttribute(p, v)\r\n }\r\n }\r\n}\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 element. returns the index of 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)\r\n .filter((hc) => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)\r\n .forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)))\r\n return i\r\n}\r\n\r\nfunction sync(\r\n parent: Element,\r\n i: number,\r\n h: HU,\r\n hfun: H | undefined,\r\n depth: number,\r\n uc: J,\r\n): number {\r\n // console.log('%csync', \"background:orange\", parent.tagName, i, h, hfun, depth)\r\n\r\n if (h === null || h === undefined) return i // skip. keep 0 and false renderable, per comment below\r\n\r\n let c = parent.childNodes[i]\r\n\r\n switch (h.kind) {\r\n case 1: {\r\n let n: Element\r\n let { tag, cn, p } = h\r\n cn ??= []\r\n\r\n let process = () => {\r\n setprops(n, n.h?.p, p)\r\n n.h = hfun ?? h // sets the new property values\r\n if (\r\n --depth &&\r\n !n.hasAttribute('const') &&\r\n !iswebcomponent(h as HElement)\r\n ) {\r\n const j = syncchildren(n, cn, 0, depth, uc)\r\n removeexcesschildren(n, j)\r\n }\r\n }\r\n\r\n if ((<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()) {\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 process()\r\n p?.mounted?.(n)\r\n } else {\r\n n = c as Element\r\n if (p?.update?.(c, uc)) return i + 1\r\n process()\r\n }\r\n\r\n return i + 1\r\n }\r\n\r\n case 4: {\r\n return syncchildren(parent, h.cn ?? [], i, --depth, uc)\r\n }\r\n\r\n case 2: {\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 if ((c?.h as any)?.tag == h.tag) {\r\n let ci = (c.h as HClassComponent).i!\r\n ci.props = c.h!.p = h.p\r\n c.h!.p = h.p // for repatable update, set attached h.p. alternatively, we could use ci.props in case where h.p is undefined\r\n // update() substitutes view/sync when returning truthy\r\n return ci.update?.(uc)\r\n ? i + 1\r\n : sync(parent, i, ci.view(), c.h, depth, uc) // hfun because outermost function shall be attached\r\n } else {\r\n let ci = (h.i = rebind(new h.tag(h.p)))\r\n let hv = ci.view()\r\n let j = sync(parent, i, hv, h, depth, uc)\r\n let n = parent.childNodes[i]!\r\n ci.element = n\r\n ci.mounted?.()\r\n return j\r\n }\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 = {} as J,\r\n): HTMLElement | undefined {\r\n if (!e) return\r\n if (depth == 0) {\r\n ; (e as HTMLElement).replaceChildren()\r\n depth = 999\r\n }\r\n let p = e.parentElement as HTMLElement\r\n let i = [].indexOf.call<any, any, any>(p.childNodes, e)\r\n sync(p, i, h, undefined, depth, uc)\r\n return p.childNodes[i] as HTMLElement\r\n}\r\n\r\nexport type Command = Selector | J | number\r\n\r\nexport function updateview(...cmds: Command[]): void {\r\n if (!cmds.length) cmds.push('body')\r\n let uc = {}\r\n let depth = 999\r\n let patchcount = 0\r\n\r\n function f(cmds: Command[]) {\r\n for (const c of cmds) {\r\n if (typeof c === 'string') {\r\n f([...document.querySelectorAll(c)])\r\n } else if (c instanceof Node) {\r\n if (c.h) {\r\n patch(c, c.h, depth, uc)\r\n patchcount++\r\n }\r\n } else if (typeof c === 'number') {\r\n depth = c\r\n } else {\r\n uc = c\r\n }\r\n }\r\n }\r\n\r\n f(cmds)\r\n\r\n // if (!patchcount) console.warn(\"updateview did not update anything. cmds=\", ...cmds)\r\n}\r\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@snupo/jmc",
3
3
  "author": "snupo",
4
- "version": "1.0.60",
4
+ "version": "1.0.62",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
package/src/events.ts ADDED
@@ -0,0 +1,25 @@
1
+
2
+
3
+ declare global {
4
+ interface Element {
5
+ eventmap?: { [type: string]: EventListener | null }
6
+ }
7
+ }
8
+
9
+ export function setevent(
10
+ el: Element,
11
+ type: string,
12
+ handler: EventListener | null) {
13
+
14
+ let map = el.eventmap ??= {}
15
+
16
+ const old = map[type]
17
+ if (old === handler) return
18
+ if (old) el.removeEventListener(type, old)
19
+ if (handler) {
20
+ el.addEventListener(type, handler)
21
+ map[type] = handler
22
+ } else {
23
+ delete map[type]
24
+ }
25
+ }
package/src/jmc.ts CHANGED
@@ -1,8 +1,18 @@
1
- import type { H, HElement, J, Selector, Children, HClassComponent, HU } from './h'
1
+ import type {
2
+ H,
3
+ HElement,
4
+ J,
5
+ Selector,
6
+ Children,
7
+ HClassComponent,
8
+ HU,
9
+ } from './h'
2
10
  import { rebind } from './base.js'
11
+ import { setevent } from './events'
3
12
 
4
- const enum NodeType { // vaporizes (but for that must be in this file, otherwise not)
5
- TextNode = 3
13
+ const enum NodeType {
14
+ // vaporizes (but for that must be in this file, otherwise not)
15
+ TextNode = 3,
6
16
  }
7
17
 
8
18
  declare global {
@@ -13,31 +23,48 @@ declare global {
13
23
 
14
24
  let iswebcomponent = (h: HElement) => h.tag.includes('-')
15
25
 
26
+ let attributeNames = new Set([
27
+ 'selected',
28
+ 'viewBox',
29
+ 'list'
30
+ // other HTML attributes where your framework wants attribute semantics
31
+ ])
32
+
33
+ export let jmcconfig = {
34
+ attributeNames
35
+ }
36
+
16
37
  const isproperty = (e: Element, name: string, value: any) =>
17
- name !== "viewBox" &&
38
+ !attributeNames.has(name) &&
18
39
  (value instanceof Object ||
19
- value instanceof Function ||
20
- (e.namespaceURI !== "http://www.w3.org/2000/svg" && name in e && name != 'style'))
40
+ value instanceof Function || (
41
+ e.namespaceURI !== 'http://www.w3.org/2000/svg' &&
42
+ name in e &&
43
+ name !== 'style'))
21
44
 
22
45
  let setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {
23
-
24
46
  old ??= {}
25
47
  now ??= {}
26
48
 
27
49
  // removals
28
- for (let p in old)
29
- if (!(p in now))
30
- isproperty(e, p, old[p])
31
- ? e[p] = typeof old[p] === 'boolean' ? false : ''
32
- : e.removeAttribute(p)
50
+ for (let p in old) {
51
+ if (!(p in now)) {
52
+ if (p.startsWith('on')) setevent(e, p.slice(2), null)
53
+ else if (isproperty(e, p, old[p])) e[p] = typeof old[p] === 'boolean' ? false : ''
54
+ else e.removeAttribute(p)
55
+ }
56
+ }
57
+
33
58
  // updates
34
59
  for (let p in now) {
35
60
  let v = now[p]
36
61
  v = v?.thunk ? v() : v
37
- if (v != old[p]) {
38
- isproperty(e, p, v)
39
- ? e[p] = v
40
- : e.setAttribute(p, v)
62
+
63
+ if (p.startsWith('on')) setevent(e, p.slice(2), v)
64
+ else if (v != old[p]) {
65
+ if (isproperty(e, p, v)) e[p] = v
66
+ else if ((v === false || v == null) && !p.startsWith('aria-')) e.removeAttribute(p)
67
+ else e.setAttribute(p, v)
41
68
  }
42
69
  }
43
70
  }
@@ -45,20 +72,34 @@ let setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {
45
72
  let removeexcesschildren = (n: Element, i: number) => {
46
73
  let c: ChildNode
47
74
  while ((c = n.childNodes[i])) {
48
- try { c.remove() } catch (_) { } // due to blur-event-handlers, this can fail
75
+ try {
76
+ c.remove()
77
+ } catch (_) { } // due to blur-event-handlers, this can fail
49
78
  }
50
79
  }
51
80
 
52
81
  /** synchronizes children starting at the i-th element. returns the index of the last child synchronized */
53
- let syncchildren = (p: Element, cn: Children, i: number, depth: number, uc: J): number => {
82
+ let syncchildren = (
83
+ p: Element,
84
+ cn: Children,
85
+ i: number,
86
+ depth: number,
87
+ uc: J,
88
+ ): number => {
54
89
  cn.flat(999)
55
- .filter(hc => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)
56
- .forEach(hc => (i = sync(p, i, hc, undefined, depth, uc)))
90
+ .filter((hc) => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)
91
+ .forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)))
57
92
  return i
58
93
  }
59
94
 
60
- function sync(parent: Element, i: number, h: HU, hfun: H | undefined, depth: number, uc: J): number {
61
-
95
+ function sync(
96
+ parent: Element,
97
+ i: number,
98
+ h: HU,
99
+ hfun: H | undefined,
100
+ depth: number,
101
+ uc: J,
102
+ ): number {
62
103
  // console.log('%csync', "background:orange", parent.tagName, i, h, hfun, depth)
63
104
 
64
105
  if (h === null || h === undefined) return i // skip. keep 0 and false renderable, per comment below
@@ -66,46 +107,51 @@ function sync(parent: Element, i: number, h: HU, hfun: H | undefined, depth: num
66
107
  let c = parent.childNodes[i]
67
108
 
68
109
  switch (h.kind) {
69
- case 1:
70
- {
71
- let n: Element
72
- let { tag, cn, p } = h
73
- cn ??= []
74
-
75
- let process = () => {
76
- setprops(n, n.h?.p, p)
77
- n.h = hfun ?? h // sets the new property values
78
- if (--depth && !n.hasAttribute('const') && !iswebcomponent(h as HElement)) {
79
- const j = syncchildren(n, cn, 0, depth, uc)
80
- removeexcesschildren(n, j)
81
- }
110
+ case 1: {
111
+ let n: Element
112
+ let { tag, cn, p } = h
113
+ cn ??= []
114
+
115
+ let process = () => {
116
+ setprops(n, n.h?.p, p)
117
+ n.h = hfun ?? h // sets the new property values
118
+ if (
119
+ --depth &&
120
+ !n.hasAttribute('const') &&
121
+ !iswebcomponent(h as HElement)
122
+ ) {
123
+ const j = syncchildren(n, cn, 0, depth, uc)
124
+ removeexcesschildren(n, j)
82
125
  }
83
-
84
- if ((<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()) {
85
- let ns = p?.xmlns ?? (parent.tagName == "foreignObject" ? 'http://www.w3.org/1999/xhtml' : parent.namespaceURI)
86
- n = document.createElementNS(ns, tag)
87
- c ? c.replaceWith(n) : parent.appendChild(n)
88
- process()
89
- p?.mounted?.(n)
90
- } else {
91
- n = c as Element
92
- if (p?.update?.(c, uc)) return i + 1
93
- process()
94
- }
95
-
96
- return i + 1
97
126
  }
98
127
 
99
- case 4:
100
- {
101
- return syncchildren(parent, h.cn ?? [], i, --depth, uc)
128
+ if ((<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()) {
129
+ let ns =
130
+ p?.xmlns ??
131
+ (parent.tagName == 'foreignObject'
132
+ ? 'http://www.w3.org/1999/xhtml'
133
+ : parent.namespaceURI)
134
+ n = document.createElementNS(ns, tag)
135
+ c ? c.replaceWith(n) : parent.appendChild(n)
136
+ process()
137
+ p?.mounted?.(n)
138
+ } else {
139
+ n = c as Element
140
+ if (p?.update?.(c, uc)) return i + 1
141
+ process()
102
142
  }
103
143
 
104
- case 2:
105
- {
106
- let hr = h.tag(h.p, h.cn)
107
- return sync(parent, i, hr, hfun ?? h, depth, uc)
108
- }
144
+ return i + 1
145
+ }
146
+
147
+ case 4: {
148
+ return syncchildren(parent, h.cn ?? [], i, --depth, uc)
149
+ }
150
+
151
+ case 2: {
152
+ let hr = h.tag(h.p, h.cn)
153
+ return sync(parent, i, hr, hfun ?? h, depth, uc)
154
+ }
109
155
 
110
156
  case 3: {
111
157
  if ((c?.h as any)?.tag == h.tag) {
@@ -113,10 +159,11 @@ function sync(parent: Element, i: number, h: HU, hfun: H | undefined, depth: num
113
159
  ci.props = c.h!.p = h.p
114
160
  c.h!.p = h.p // for repatable update, set attached h.p. alternatively, we could use ci.props in case where h.p is undefined
115
161
  // update() substitutes view/sync when returning truthy
116
- return (ci.update?.(uc)) ? i + 1 : sync(parent, i, ci.view(), c.h, depth, uc) // hfun because outermost function shall be attached
117
- }
118
- else {
119
- let ci = h.i = rebind(new h.tag(h.p))
162
+ return ci.update?.(uc)
163
+ ? i + 1
164
+ : sync(parent, i, ci.view(), c.h, depth, uc) // hfun because outermost function shall be attached
165
+ } else {
166
+ let ci = (h.i = rebind(new h.tag(h.p)))
120
167
  let hv = ci.view()
121
168
  let j = sync(parent, i, hv, h, depth, uc)
122
169
  let n = parent.childNodes[i]!
@@ -138,10 +185,15 @@ function sync(parent: Element, i: number, h: HU, hfun: H | undefined, depth: num
138
185
  }
139
186
  }
140
187
 
141
- export function patch(e: Node | null, h: H, depth = 999, uc = {} as J): HTMLElement | undefined {
188
+ export function patch(
189
+ e: Node | null,
190
+ h: H,
191
+ depth = 999,
192
+ uc = {} as J,
193
+ ): HTMLElement | undefined {
142
194
  if (!e) return
143
195
  if (depth == 0) {
144
- (e as HTMLElement).replaceChildren()
196
+ ; (e as HTMLElement).replaceChildren()
145
197
  depth = 999
146
198
  }
147
199
  let p = e.parentElement as HTMLElement
@@ -153,7 +205,6 @@ export function patch(e: Node | null, h: H, depth = 999, uc = {} as J): HTMLElem
153
205
  export type Command = Selector | J | number
154
206
 
155
207
  export function updateview(...cmds: Command[]): void {
156
-
157
208
  if (!cmds.length) cmds.push('body')
158
209
  let uc = {}
159
210
  let depth = 999
@@ -178,5 +229,5 @@ export function updateview(...cmds: Command[]): void {
178
229
 
179
230
  f(cmds)
180
231
 
181
- if (!patchcount) console.warn("updateview did not update anything. cmds=", ...cmds)
232
+ // if (!patchcount) console.warn("updateview did not update anything. cmds=", ...cmds)
182
233
  }