wrec 0.22.1 → 0.22.3

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.
@@ -1,4 +1,4 @@
1
- export declare type ChangeListener = {
1
+ declare type ChangeListener = {
2
2
  changed: (statePath: string, componentProperty: string, newValue: unknown, oldValue: unknown, state: WrecState) => void;
3
3
  };
4
4
 
@@ -8,13 +8,18 @@ export declare function css(strings: TemplateStringsArray, ...values: unknown[])
8
8
 
9
9
  export declare function html(strings: TemplateStringsArray, ...values: unknown[]): string;
10
10
 
11
+ declare const HTMLElementBase: {
12
+ new (): HTMLElement;
13
+ prototype: HTMLElement;
14
+ };
15
+
11
16
  declare type LooseObject = Record<string, unknown>;
12
17
 
13
18
  declare type StringToAny = Record<string, any>;
14
19
 
15
20
  declare type StringToString = Record<string, string>;
16
21
 
17
- export declare abstract class Wrec extends HTMLElement implements ChangeListener {
22
+ export declare abstract class Wrec extends HTMLElementBase implements ChangeListener {
18
23
  #private;
19
24
  private static attrToPropMap;
20
25
  private static propToAttrMap;
@@ -1,71 +1,60 @@
1
- import { HTMLElement as y, parse as H, NodeType as T, TextNode as M } from "node-html-parser";
2
- import { Wrec as R } from "./wrec.es.js";
3
- import { WrecState as I, createElement as C, css as D, html as F } from "./wrec.es.js";
4
- const b = "a-zA-Z_$", S = b + "0-9", h = `[${b}][${S}]*`, N = new RegExp(`this\\.${h}(\\.${h})*`), _ = 5, O = (c) => c.substring(_).split(".")[0];
5
- typeof window > "u" && !globalThis.HTMLElement && (globalThis.HTMLElement = y);
6
- typeof window > "u" && !globalThis.customElements && (globalThis.customElements = {
7
- get: (c) => {
8
- },
9
- getName: () => "",
10
- define: () => {
11
- },
12
- upgrade: () => {
13
- },
14
- whenDefined: () => Promise.reject(new Error("customElements is not available"))
15
- });
16
- R.ssr = function(o = {}) {
17
- let l = "";
18
- const m = Object.keys(o);
19
- m.sort();
20
- for (const t of m) {
21
- const n = this.getAttrName(t);
22
- l += ` ${n}="${o[t]}"`;
1
+ import { parse as S, NodeType as h, TextNode as O } from "node-html-parser";
2
+ import { Wrec as _ } from "./wrec.es.js";
3
+ import { WrecState as j, createElement as C, css as F, html as D } from "./wrec.es.js";
4
+ const $ = "a-zA-Z_$", w = $ + "0-9", T = `[${$}][${w}]*`, d = new RegExp(`this\\.${T}(\\.${T})*`), y = 5, A = (a) => a.substring(y).split(".")[0];
5
+ _.ssr = function(o = {}) {
6
+ let u = "";
7
+ const l = Object.keys(o);
8
+ l.sort();
9
+ for (const t of l) {
10
+ const s = this.getAttrName(t);
11
+ u += ` ${s}="${o[t]}"`;
23
12
  }
24
- const u = this.properties;
25
- for (const [t, n] of Object.entries(u))
13
+ const f = this.properties;
14
+ for (const [t, s] of Object.entries(f))
26
15
  if (o[t] === void 0) {
27
- const { value: s } = n;
28
- s !== void 0 && (o[t] = s);
16
+ const { value: n } = s;
17
+ n !== void 0 && (o[t] = n);
29
18
  }
30
- function f(t) {
19
+ function m(t) {
31
20
  return new Function("return " + t).call(o);
32
21
  }
33
- function E(t) {
34
- const { attributes: n } = t;
35
- for (const [e, i] of Object.entries(n))
36
- if (N.test(i)) {
37
- const r = f(i), a = O(e), v = u[a]?.value ?? "";
38
- r === v ? t.removeAttribute(e) : t.setAttribute(e, String(r));
22
+ function p(t) {
23
+ const { attributes: s } = t;
24
+ for (const [e, c] of Object.entries(s))
25
+ if (d.test(c)) {
26
+ const r = m(c), i = A(e), R = f[i]?.value ?? "";
27
+ r === R ? t.removeAttribute(e) : t.setAttribute(e, String(r));
39
28
  }
40
- const { childNodes: s } = t;
41
- s.forEach((e, i) => {
42
- if (e.nodeType === T.ELEMENT_NODE)
43
- E(e);
44
- else if (e.nodeType === T.COMMENT_NODE) {
29
+ const { childNodes: n } = t;
30
+ n.forEach((e, c) => {
31
+ if (e.nodeType === h.ELEMENT_NODE)
32
+ p(e);
33
+ else if (e.nodeType === h.COMMENT_NODE) {
45
34
  const r = e.textContent ?? "";
46
- if (N.test(r)) {
47
- const a = f(r);
48
- s[i] = new M(String(a));
35
+ if (d.test(r)) {
36
+ const i = m(r);
37
+ n[c] = new O(String(i));
49
38
  }
50
39
  }
51
40
  });
52
41
  }
53
- const g = this.buildHTML(), w = H(g, { comment: !0 }), { children: d } = w;
54
- d.forEach(E);
55
- const $ = d.map((t) => t.outerHTML).join(`
56
- `), p = this.elementName;
42
+ const b = this.buildHTML(), g = S(b, { comment: !0 }), { children: E } = g;
43
+ E.forEach(p);
44
+ const v = E.map((t) => t.outerHTML).join(`
45
+ `), N = this.elementName;
57
46
  return `
58
- <${p}${l}>
47
+ <${N}${u}>
59
48
  <template shadowrootmode="open">
60
- ${$}
49
+ ${v}
61
50
  </template>
62
- </${p}>
51
+ </${N}>
63
52
  `;
64
53
  };
65
54
  export {
66
- R as Wrec,
67
- I as WrecState,
55
+ _ as Wrec,
56
+ j as WrecState,
68
57
  C as createElement,
69
- D as css,
70
- F as html
58
+ F as css,
59
+ D as html
71
60
  };
package/dist/wrec.d.ts CHANGED
@@ -8,13 +8,18 @@ export declare function css(strings: TemplateStringsArray, ...values: unknown[])
8
8
 
9
9
  export declare function html(strings: TemplateStringsArray, ...values: unknown[]): string;
10
10
 
11
+ declare const HTMLElementBase: {
12
+ new (): HTMLElement;
13
+ prototype: HTMLElement;
14
+ };
15
+
11
16
  declare type LooseObject = Record<string, unknown>;
12
17
 
13
18
  declare type StringToAny = Record<string, any>;
14
19
 
15
20
  declare type StringToString = Record<string, string>;
16
21
 
17
- export declare abstract class Wrec extends HTMLElement implements ChangeListener {
22
+ export declare abstract class Wrec extends HTMLElementBase implements ChangeListener {
18
23
  #private;
19
24
  private static attrToPropMap;
20
25
  private static propToAttrMap;
package/dist/wrec.es.js CHANGED
@@ -206,7 +206,8 @@ const nt = /* @__PURE__ */ new Set([
206
206
  "id",
207
207
  "tabindex",
208
208
  "title"
209
- ]), F = globalThis.customElements ?? {
209
+ ]), rt = globalThis.HTMLElement ?? class {
210
+ }, F = globalThis.customElements ?? {
210
211
  get: (n) => {
211
212
  },
212
213
  getName: () => "",
@@ -220,18 +221,18 @@ const nt = /* @__PURE__ */ new Set([
220
221
  };
221
222
  class E extends Error {
222
223
  }
223
- const rt = /([a-zA-Z-]+)\s*:\s*([^;}]+)/g, Y = "a-zA-Z_$", ct = Y + "0-9", S = `[${Y}][${ct}]*`, at = /<!--\s*(.*?)\s*-->/, ft = /<(\w+)(?:\s[^>]*)?>((?:[^<]|<(?!\w))*?)<\/\1>/g, L = new RegExp(`^this\\.${S}$`), j = new RegExp(`this\\.${S}(\\.${S})*`, "g"), G = new RegExp(`this\\.${S}(\\.${S})*`), ht = 5;
224
- function lt(n) {
224
+ const ct = /([a-zA-Z-]+)\s*:\s*([^;}]+)/g, Y = "a-zA-Z_$", at = Y + "0-9", S = `[${Y}][${at}]*`, ft = /<!--\s*(.*?)\s*-->/, ht = /<(\w+)(?:\s[^>]*)?>((?:[^<]|<(?!\w))*?)<\/\1>/g, L = new RegExp(`^this\\.${S}$`), j = new RegExp(`this\\.${S}(\\.${S})*`, "g"), G = new RegExp(`this\\.${S}(\\.${S})*`), lt = 5;
225
+ function pt(n) {
225
226
  return n instanceof HTMLButtonElement || n instanceof HTMLFieldSetElement || n instanceof HTMLInputElement || n instanceof HTMLSelectElement || n instanceof HTMLTextAreaElement || n instanceof u;
226
227
  }
227
- function yt(n, t, e) {
228
+ function wt(n, t, e) {
228
229
  const s = document.createElement(n);
229
230
  if (t)
230
231
  for (const [o, i] of Object.entries(t))
231
232
  s.setAttribute(o, i);
232
233
  return e && (s.innerHTML = e), s;
233
234
  }
234
- const pt = (n) => n === String ? "" : n === Number ? 0 : n === Boolean ? !1 : n === Array ? [] : n === Object ? {} : void 0;
235
+ const ut = (n) => n === String ? "" : n === Number ? 0 : n === Boolean ? !1 : n === Array ? [] : n === Object ? {} : void 0;
235
236
  function R(n) {
236
237
  const t = [];
237
238
  let e = n.firstElementChild;
@@ -239,7 +240,7 @@ function R(n) {
239
240
  t.push(e), e.shadowRoot && t.push(...R(e.shadowRoot)), e.firstElementChild && t.push(...R(e)), e = e.nextElementSibling;
240
241
  return t;
241
242
  }
242
- const M = (n) => n.substring(ht).split(".")[0];
243
+ const M = (n) => n.substring(lt).split(".")[0];
243
244
  function Q(n, t) {
244
245
  let e = n[0];
245
246
  return t.forEach((s, o) => {
@@ -257,11 +258,11 @@ function D(n) {
257
258
  const { localName: t } = n;
258
259
  return t === "input" || t === "select";
259
260
  }
260
- const ut = (n) => n.replace(/<!--[\s\S]*?-->/g, "");
261
+ const dt = (n) => n.replace(/<!--[\s\S]*?-->/g, "");
261
262
  function tt(n, t, e, s) {
262
263
  return n.slice(0, t) + s + n.slice(t + e);
263
264
  }
264
- function k(n) {
265
+ function H(n) {
265
266
  const t = Number(n);
266
267
  if (isNaN(t)) throw new E(`can't convert "${n}" to a number`);
267
268
  return t;
@@ -282,11 +283,11 @@ function et(n, t, e) {
282
283
  n[i] = e;
283
284
  }
284
285
  }
285
- function H(n, t, e) {
286
+ function k(n, t, e) {
286
287
  const [s, o] = t.split(":");
287
288
  n instanceof CSSStyleRule ? n.style.setProperty(s, e) : (et(n, s, e), s === "value" && D(n) && (n.value = e));
288
289
  }
289
- async function dt(n) {
290
+ async function mt(n) {
290
291
  const t = /* @__PURE__ */ new Set();
291
292
  for (const s of R(n.content)) {
292
293
  const { localName: o } = s;
@@ -309,7 +310,7 @@ async function dt(n) {
309
310
  )
310
311
  );
311
312
  }
312
- class u extends HTMLElement {
313
+ class u extends rt {
313
314
  // There is one instance of `attrToPropMap`, `properties`, `propToAttrMap`,
314
315
  // `propToComputedMap`, and `propToExprsMap` per Wrec subclass,
315
316
  // not one for only the Wrec class.
@@ -435,7 +436,7 @@ class u extends HTMLElement {
435
436
  async #x() {
436
437
  const t = this.#t;
437
438
  let { template: e } = t;
438
- e || (e = t.template = document.createElement("template"), e.innerHTML = t.buildHTML()), await dt(e), this.shadowRoot.replaceChildren(e.content.cloneNode(!0));
439
+ e || (e = t.template = document.createElement("template"), e.innerHTML = t.buildHTML()), await mt(e), this.shadowRoot.replaceChildren(e.content.cloneNode(!0));
439
440
  }
440
441
  static buildHTML() {
441
442
  let t = `<style>
@@ -473,19 +474,19 @@ class u extends HTMLElement {
473
474
  e.required && !i && this.#e(this, o, "is a required attribute");
474
475
  let r = e.value;
475
476
  this.hasOwnProperty(t) && (r = this[t], delete this[t]);
476
- const { type: c } = e, a = c === Boolean ? r || i : s.includes(o) && i ? this.#A(t, o) : r || pt(c), h = "#" + t;
477
+ const { type: c } = e, a = c === Boolean ? r || i : s.includes(o) && i ? this.#A(t, o) : r || ut(c), h = "#" + t;
477
478
  this[h] = a, e.computed && this.#L(t, e), Object.defineProperty(this, t, {
478
479
  enumerable: !0,
479
480
  get() {
480
481
  return this[h];
481
482
  },
482
483
  set(f) {
483
- c === Number && typeof f == "string" && (f = k(f));
484
+ c === Number && typeof f == "string" && (f = H(f));
484
485
  const l = this[h];
485
486
  if (f === l) return;
486
487
  this.#I(t, c, f), this[h] = f;
487
488
  const m = this.#m.get(t);
488
- m && st(m.state, m.stateProp, f), this.#j(t, c, f, o), this.#f || (this.#k(t), this.#M(t)), this.#H(t, f);
489
+ m && st(m.state, m.stateProp, f), this.#j(t, c, f, o), this.#f || (this.#H(t), this.#M(t)), this.#k(t, f);
489
490
  const x = this.#h[t];
490
491
  x && this.setFormValue(x, String(f)), this.propertyChangedCallback(t, l, f), e.dispatch && this.dispatch("change", {
491
492
  tagName: this.localName,
@@ -499,7 +500,7 @@ class u extends HTMLElement {
499
500
  #g() {
500
501
  const t = this.hasAttribute("disabled"), e = R(this.shadowRoot);
501
502
  for (const s of e)
502
- lt(s) && (s.disabled = t);
503
+ pt(s) && (s.disabled = t);
503
504
  }
504
505
  disconnectedCallback() {
505
506
  this.#o.clear(), this.#l.clear(), this.#u.clear();
@@ -543,7 +544,7 @@ class u extends HTMLElement {
543
544
  this.#v(i, s);
544
545
  else if (!(i instanceof CSSStyleRule)) {
545
546
  const { element: r, attrName: c } = i;
546
- r instanceof CSSStyleRule ? r.style.setProperty(c, s) : H(r, c, s);
547
+ r instanceof CSSStyleRule ? r.style.setProperty(c, s) : k(r, c, s);
547
548
  }
548
549
  }
549
550
  }
@@ -571,7 +572,7 @@ class u extends HTMLElement {
571
572
  let s = "";
572
573
  if (A(t)) {
573
574
  this.#c(t.textContent, t);
574
- const o = t.textContent?.match(at);
575
+ const o = t.textContent?.match(ft);
575
576
  o && (s = o[1]);
576
577
  } else {
577
578
  const o = Array.from(t.childNodes).find(
@@ -639,7 +640,7 @@ class u extends HTMLElement {
639
640
  const { target: l } = f;
640
641
  if (!l) return;
641
642
  const m = l.value, { type: x } = this.#t.properties[h];
642
- this[h] = x === Number ? k(m) : m, this.#M(h);
643
+ this[h] = x === Number ? H(m) : m, this.#M(h);
643
644
  });
644
645
  }
645
646
  #n(t) {
@@ -703,7 +704,7 @@ class u extends HTMLElement {
703
704
  const o = this.#d(e, s, t);
704
705
  if (!o) {
705
706
  const a = t.replaceAll("this..", "this.");
706
- s ? H(e, s, a) : "textContent" in e && (e.textContent = a);
707
+ s ? k(e, s, a) : "textContent" in e && (e.textContent = a);
707
708
  return;
708
709
  }
709
710
  const i = this.#t;
@@ -725,7 +726,7 @@ class u extends HTMLElement {
725
726
  let r = this.#o.get(t);
726
727
  r || (r = [], this.#o.set(t, r)), r.push(s ? { element: e, attrName: s } : e), e instanceof HTMLElement && this.#O(e, s, o);
727
728
  const c = this.#s(t);
728
- s ? H(e, s, c) : this.#v(e, c);
729
+ s ? k(e, s, c) : this.#v(e, c);
729
730
  }
730
731
  // This follows the best practice
731
732
  // "Do not override author-set, global attributes."
@@ -754,7 +755,7 @@ class u extends HTMLElement {
754
755
  if (e?.match(j)) return e;
755
756
  const s = this.#t, { type: o } = s.properties[t];
756
757
  if (o || this.#e(null, t, "does not specify its type"), o === String) return e;
757
- if (o === Number) return k(e);
758
+ if (o === Number) return H(e);
758
759
  if (o === Boolean) {
759
760
  if (e === "true") return !0;
760
761
  if (e === "false" || e === "null") return !1;
@@ -776,7 +777,7 @@ class u extends HTMLElement {
776
777
  }
777
778
  // Updates all computed properties that reference this property.
778
779
  // VS Code thinks this is never called, but it is called by #defineProp.
779
- #k(t) {
780
+ #H(t) {
780
781
  const s = this.#t.propToComputedMap.get(t) || [];
781
782
  for (const [o, i] of s)
782
783
  this[o] = this.#s(i);
@@ -801,7 +802,7 @@ class u extends HTMLElement {
801
802
  }
802
803
  // Update corresponding parent web component property if bound to one.
803
804
  // VS Code thinks this is never called, but it is called by #defineProp.
804
- #H(t, e) {
805
+ #k(t, e) {
805
806
  const s = this.#u.get(t);
806
807
  if (!s) return;
807
808
  const o = this.getRootNode();
@@ -904,10 +905,10 @@ class u extends HTMLElement {
904
905
  }
905
906
  }
906
907
  }
907
- function wt(n, ...t) {
908
+ function Et(n, ...t) {
908
909
  let e = Q(n, t);
909
910
  for (; ; ) {
910
- const s = rt.exec(e);
911
+ const s = ct.exec(e);
911
912
  if (!s) break;
912
913
  const o = s[2];
913
914
  if (G.test(o)) {
@@ -921,12 +922,12 @@ function wt(n, ...t) {
921
922
  }
922
923
  return e;
923
924
  }
924
- function Et(n, ...t) {
925
+ function Tt(n, ...t) {
925
926
  let e = Q(n, t);
926
927
  for (; ; ) {
927
- const s = ft.exec(e);
928
+ const s = ht.exec(e);
928
929
  if (!s || s[1] === "style") break;
929
- const o = ut(s[2]);
930
+ const o = dt(s[2]);
930
931
  if (G.test(o)) {
931
932
  const i = `<!-- ${o.trim()} -->`, r = s.index + s[0].indexOf(">") + 1;
932
933
  e = tt(e, r, o.length, i);
@@ -937,7 +938,7 @@ function Et(n, ...t) {
937
938
  export {
938
939
  u as Wrec,
939
940
  _ as WrecState,
940
- yt as createElement,
941
- wt as css,
942
- Et as html
941
+ wt as createElement,
942
+ Et as css,
943
+ Tt as html
943
944
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "wrec",
3
3
  "description": "a small library that greatly simplifies building web components",
4
4
  "author": "R. Mark Volkmann",
5
- "version": "0.22.1",
5
+ "version": "0.22.3",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",