solely 0.0.29 → 0.1.0

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.
Files changed (52) hide show
  1. package/README.md +817 -507
  2. package/dist/compiler/index.d.ts +2 -0
  3. package/dist/compiler/ir/buildIR.d.ts +12 -0
  4. package/dist/compiler/ir/index.d.ts +1 -0
  5. package/dist/compiler/parser/index.d.ts +1 -0
  6. package/dist/compiler/parser/parser.d.ts +5 -0
  7. package/dist/index.d.ts +4 -0
  8. package/dist/index.js +1756 -0
  9. package/dist/index.umd.cjs +47 -0
  10. package/dist/plugin.d.ts +1 -0
  11. package/dist/plugins/html-parse-precompile.d.ts +1 -0
  12. package/dist/plugins/html-precompile.d.ts +1 -0
  13. package/dist/plugins/index.d.ts +1 -0
  14. package/dist/runtime/component/base-element.d.ts +24 -0
  15. package/dist/{base → runtime/component}/decorators.d.ts +6 -4
  16. package/dist/runtime/component/index.d.ts +2 -0
  17. package/dist/runtime/index.d.ts +4 -0
  18. package/dist/runtime/reactivity/index.d.ts +2 -0
  19. package/dist/runtime/reactivity/observe.d.ts +47 -0
  20. package/dist/{utils/observe.d.ts → runtime/reactivity/types.d.ts} +0 -8
  21. package/dist/runtime/renderer/index.d.ts +1 -0
  22. package/dist/runtime/renderer/renderIR.d.ts +25 -0
  23. package/dist/runtime/router/core.d.ts +42 -0
  24. package/dist/runtime/router/index.d.ts +6 -0
  25. package/dist/runtime/router/router-link.d.ts +17 -0
  26. package/dist/runtime/router/router-view.d.ts +25 -0
  27. package/dist/runtime/router/types.d.ts +65 -0
  28. package/dist/shared/event-listeners.d.ts +2 -0
  29. package/dist/shared/gen-function.d.ts +3 -0
  30. package/dist/shared/index.d.ts +6 -0
  31. package/dist/shared/templateError.d.ts +9 -0
  32. package/dist/types/ast.d.ts +38 -0
  33. package/dist/types/component.d.ts +66 -0
  34. package/dist/types/index.d.ts +4 -0
  35. package/dist/types/ir.d.ts +67 -0
  36. package/dist/types/lifecycle.d.ts +4 -0
  37. package/dist/types/loop.d.ts +4 -0
  38. package/package.json +24 -6
  39. package/dist/base/base-element.d.ts +0 -56
  40. package/dist/base/index.d.ts +0 -5
  41. package/dist/base/router-view.d.ts +0 -43
  42. package/dist/solely.d.ts +0 -2
  43. package/dist/solely.js +0 -928
  44. package/dist/solely.umd.cjs +0 -3
  45. package/dist/utils/ast-processor.d.ts +0 -2
  46. package/dist/utils/async-queue.d.ts +0 -1
  47. package/dist/utils/event-listeners.d.ts +0 -2
  48. package/dist/utils/html-parse.d.ts +0 -45
  49. package/dist/utils/index.d.ts +0 -8
  50. /package/dist/{utils → shared}/functions.d.ts +0 -0
  51. /package/dist/{utils → shared}/is-object.d.ts +0 -0
  52. /package/dist/{utils → shared}/url.d.ts +0 -0
package/dist/solely.js DELETED
@@ -1,928 +0,0 @@
1
- var ft = (t) => {
2
- throw TypeError(t);
3
- };
4
- var st = (t, e, n) => e.has(t) || ft("Cannot " + n);
5
- var x = (t, e, n) => (st(t, e, "read from private field"), n ? n.call(t) : e.get(t)), M = (t, e, n) => e.has(t) ? ft("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, n), k = (t, e, n, s) => (st(t, e, "write to private field"), s ? s.call(t, n) : e.set(t, n), n), W = (t, e, n) => (st(t, e, "access private method"), n);
6
- const _t = /<!--(?:.|\s)*?-->|<[a-zA-Z0-9\-/](?:"[^"]*"|'[^']*'|[^'">])*>/g, St = /<\/?([^\s]+?)[/\s>]/, Wt = /\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g, It = ["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"], rt = /\{\{(.+?)\}\}/g;
7
- function Ot(t) {
8
- return t.replace(/>\s+</g, "><").replace(/\s+/g, " ");
9
- }
10
- function jt(t, e) {
11
- const n = (e.match(St) || [])[1] || "", s = {
12
- rootId: "0",
13
- tagName: n,
14
- attrs: {},
15
- props: {},
16
- on: {},
17
- children: []
18
- }, r = new RegExp(Wt);
19
- for (; ; ) {
20
- const o = r.exec(e);
21
- if (o === null) break;
22
- if (!o[0].trim()) continue;
23
- const [a, i] = o[1] ? o[1].trim().split("=") : [o[2], o[3].trim().slice(1, -1)];
24
- if (a === "onMounted" || a === "onUpdated")
25
- s[a] = Ht(t, i);
26
- else if (gt.includes(a) || a.startsWith("on-"))
27
- s.on[a.startsWith("on-") ? a.slice(3) : a.slice(2)] = H(t, i);
28
- else if (a.startsWith("s-")) {
29
- const f = a.slice(2);
30
- switch (f) {
31
- case "model":
32
- s.model = i;
33
- break;
34
- case "style":
35
- s.styles = (d) => O(t, i, d);
36
- break;
37
- case "class":
38
- s.classes = (d) => O(t, i, d);
39
- break;
40
- case "readonly":
41
- s.props.readOnly = (d) => O(t, i, d);
42
- break;
43
- default:
44
- s.props[f] = (d) => O(t, i, d);
45
- break;
46
- }
47
- } else
48
- s.attrs[a] = i;
49
- r.lastIndex--;
50
- }
51
- if (n === "For" ? s.fn = (o) => O(t, s.attrs.each, o) : (n === "If" || n === "ElseIf") && (s.fn = (o) => O(t, s.attrs.condition, o)), s.model) {
52
- const o = s.model.replace("this.", "").replace("$data.", ""), a = (f) => O(t, `$data.${o}`, f), i = (f, d) => {
53
- if (s.on[f]) {
54
- const l = s.on[f];
55
- s.on[f] = (c, u) => {
56
- d(c, u), l(c, u);
57
- };
58
- } else
59
- s.on[f] = d;
60
- };
61
- if (n === "input") {
62
- const f = s.attrs.type || "text";
63
- if (f === "checkbox")
64
- s.props.checked = a, i("change", H(t, `$data.${o}=checked`));
65
- else if (f === "radio") {
66
- const d = s.attrs.value;
67
- s.props.checked = (l) => a(l) == d, i("change", H(t, `if(checked){$data.${o}='${d}'}`));
68
- } else
69
- s.props.value = a, i("input", H(t, `$data.${o}=value`));
70
- } else if (n === "textarea")
71
- s.props.value = a, i("input", H(t, `$data.${o}=value`));
72
- else if (n === "select") {
73
- const f = "multiple" in s.attrs;
74
- f ? i(
75
- "change",
76
- H(t, `
77
- $data.${o} = Array.from(event.target.selectedOptions).map(o => o.value);
78
- `)
79
- ) : (i("change", H(t, `$data.${o}=value`)), s.props.value = a);
80
- const d = (u, p = []) => {
81
- const $ = a(p);
82
- if (f) {
83
- const A = Array.isArray($) ? $ : $ != null ? [$] : [];
84
- for (const R of u.options)
85
- R.selected = A.includes(R.value);
86
- } else {
87
- const A = $ ?? "";
88
- u.value !== A && (u.value = A);
89
- }
90
- }, l = s.onMounted, c = s.onUpdated;
91
- s.onMounted = (u, p) => {
92
- l == null || l(u, p), d(u, p || []);
93
- }, s.onUpdated = (u, p) => {
94
- c == null || c(u, p), d(u, p || []);
95
- };
96
- }
97
- }
98
- return s;
99
- }
100
- function Tt(t, e) {
101
- const n = [];
102
- if (!e) return n;
103
- e = Ot(e);
104
- const s = [], r = [], o = () => {
105
- var i;
106
- return s.length === 0 ? n : (i = s[s.length - 1]).children || (i.children = []);
107
- };
108
- let a = 0;
109
- return e.replace(_t, (i, f) => {
110
- const d = o();
111
- if (f > a) {
112
- const l = e.substring(a, f).trim();
113
- if (l) {
114
- rt.lastIndex = 0;
115
- const u = !rt.test(l) ? l : (p) => l.replace(rt, ($, A) => O(t, A, p));
116
- d.push({
117
- rootId: "0",
118
- tagName: "text",
119
- attrs: {},
120
- props: {},
121
- on: {},
122
- content: u,
123
- children: []
124
- });
125
- }
126
- }
127
- if (a = f + i.length, i.startsWith("<!--") && i.endsWith("-->"))
128
- d.push({
129
- rootId: "0",
130
- tagName: "comment",
131
- attrs: {},
132
- props: {},
133
- on: {},
134
- content: i.slice(4, -3),
135
- children: []
136
- });
137
- else if (i.startsWith("</")) {
138
- const l = r.pop();
139
- for (; s.length > 0 && s[s.length - 1].tagName !== l; )
140
- s.pop();
141
- s.pop();
142
- } else {
143
- const l = jt(t, i), c = i.endsWith("/>") || It.includes(l.tagName);
144
- d.push(l), c || (s.push(l), r.push(l.tagName));
145
- }
146
- return "";
147
- }), e.length > a && n.push({
148
- rootId: "0",
149
- tagName: "text",
150
- attrs: {},
151
- props: {},
152
- on: {},
153
- content: e.substring(a).trim(),
154
- children: []
155
- }), n;
156
- }
157
- const O = (t, e, n) => {
158
- try {
159
- return lt([
160
- "$data",
161
- ...n.map((r) => r.item),
162
- ...n.map((r) => r.index),
163
- // 表达式求值,返回结果
164
- `return (${e})`
165
- ]).bind(t)(
166
- t.$data,
167
- ...n.map((r) => r.value),
168
- ...n.map((r) => r.valueIndex)
169
- );
170
- } catch (s) {
171
- return console.error(
172
- `[BaseElement][${t.tagName}] Error evaluating expression: {{ ${e} }}`,
173
- t,
174
- s
175
- ), "";
176
- }
177
- }, H = (t, e) => (n, s) => {
178
- var r, o;
179
- try {
180
- return lt([
181
- "$data",
182
- "event",
183
- "value",
184
- "checked",
185
- ...s.map((i) => i.item),
186
- ...s.map((i) => i.index),
187
- e
188
- ]).bind(t)(
189
- t.$data,
190
- n,
191
- (r = n == null ? void 0 : n.target) == null ? void 0 : r.value,
192
- (o = n == null ? void 0 : n.target) == null ? void 0 : o.checked,
193
- ...s.map((i) => i.value),
194
- ...s.map((i) => i.valueIndex)
195
- );
196
- } catch (a) {
197
- console.error(
198
- `[BaseElement][${t.tagName}] Error executing event handler "${e}"`,
199
- t,
200
- a
201
- );
202
- }
203
- }, Ht = (t, e) => (n, s = []) => {
204
- try {
205
- lt([
206
- "$data",
207
- "elm",
208
- ...s.map((o) => o.item),
209
- ...s.map((o) => o.index),
210
- // 执行语句块(无 return)
211
- `(function(){ ${e} }).call(this)`
212
- ]).bind(t)(
213
- t.$data,
214
- n,
215
- ...s.map((o) => o.value),
216
- ...s.map((o) => o.valueIndex)
217
- );
218
- } catch (r) {
219
- console.error(
220
- `[BaseElement][${t.tagName}] Error executing lifecycle handler "${e}"`,
221
- t,
222
- r
223
- );
224
- }
225
- }, gt = [
226
- "onabort",
227
- "onautocomplete",
228
- "onautocompleteerror",
229
- "onblur",
230
- "oncancel",
231
- "oncanplay",
232
- "oncanplaythrough",
233
- "onchange",
234
- "onclick",
235
- "onclose",
236
- "oncontextmenu",
237
- "oncuechange",
238
- "ondblclick",
239
- "ondrag",
240
- "ondragend",
241
- "ondragenter",
242
- "ondragexit",
243
- "ondragleave",
244
- "ondragover",
245
- "ondragstart",
246
- "ondrop",
247
- "ondurationchange",
248
- "onemptied",
249
- "onended",
250
- "onerror",
251
- "onfocus",
252
- "oninput",
253
- "oninvalid",
254
- "onkeydown",
255
- "onkeypress",
256
- "onkeyup",
257
- "onload",
258
- "onloadeddata",
259
- "onloadedmetadata",
260
- "onloadstart",
261
- "onmousedown",
262
- "onmouseenter",
263
- "onmouseleave",
264
- "onmousemove",
265
- "onmouseout",
266
- "onmouseover",
267
- "onmouseup",
268
- "onwheel",
269
- "onpause",
270
- "onplay",
271
- "onplaying",
272
- "onprogress",
273
- "onratechange",
274
- "onreset",
275
- "onresize",
276
- "onscroll",
277
- "onseeked",
278
- "onseeking",
279
- "onselect",
280
- "onshow",
281
- "onstalled",
282
- "onsubmit",
283
- "onsuspend",
284
- "ontimeupdate",
285
- "ontoggle",
286
- "onvolumechange",
287
- "onwaiting"
288
- ], Lt = typeof requestAnimationFrame == "function" ? (t) => requestAnimationFrame(t) : typeof Promise < "u" ? (t) => Promise.resolve().then(t) : (t) => setTimeout(t, 0), Dt = (t, ...e) => {
289
- if (typeof t != "function")
290
- throw new Error("runInAsyncQueue expects a function as its first argument");
291
- Lt(() => t(...e));
292
- }, ot = {};
293
- function lt(t) {
294
- const e = t.map((n) => n.replace(/[',"]/g, "")).join(",");
295
- if (!ot[e]) {
296
- const n = t[t.length - 1];
297
- if (n.trim() === "")
298
- throw new Error("Function body cannot be empty");
299
- if (Ut(n))
300
- throw new Error("Input contains potentially dangerous code: " + n);
301
- ot[e] = new Function(...t);
302
- }
303
- return ot[e];
304
- }
305
- const Ft = ["eval", "Function", "process", "require"];
306
- function Ut(t) {
307
- return Ft.some((e) => new RegExp(`\\b${e}\\b`, "g").test(t));
308
- }
309
- const qt = "http://www.w3.org/1999/xlink", Vt = "http://www.w3.org/2000/xmlns/", Bt = "http://www.w3.org/XML/1998/namespace", zt = "http://www.w3.org/2000/svg", yt = (t) => typeof t.content == "function" ? t.content(t.loops || []) : t.content || "", Gt = (t) => {
310
- const e = yt(t), n = t.elm;
311
- n.textContent !== e && (n.textContent = e);
312
- }, Et = (t, e, n = []) => {
313
- Object.keys(e).forEach((s) => {
314
- const r = e[s](n);
315
- t[s] !== r && (t[s] = r);
316
- });
317
- }, vt = (t, e) => {
318
- const { styles: n, loops: s = [] } = e;
319
- if (!n) return;
320
- const r = n(s), o = at(r);
321
- Object.entries(o).forEach(([a, i]) => {
322
- t.style[a] = i;
323
- });
324
- }, $t = (t, e) => {
325
- const { classes: n, loops: s = [] } = e;
326
- if (!n) return;
327
- const r = n(s), o = bt(r);
328
- Object.entries(o).forEach(([a, i]) => {
329
- i ? t.classList.add(a) : t.classList.remove(a);
330
- });
331
- }, at = (t) => {
332
- let e = {};
333
- return typeof t == "object" && t !== null && !Array.isArray(t) ? Object.entries(t).forEach(([n, s]) => {
334
- if (typeof s == "object" && s !== null && !Array.isArray(s)) {
335
- const r = at(s);
336
- Object.entries(r).forEach(([o, a]) => {
337
- e[`${n}-${o}`] = a;
338
- });
339
- } else typeof s == "string" || typeof s == "number" ? e[n] = `${s}` : e[n] = String(s);
340
- }) : typeof t == "string" ? t.split(";").forEach((n) => {
341
- const [s, r] = n.split(":").map((o) => o.trim());
342
- s && r && (e[s] = r);
343
- }) : Array.isArray(t) && t.forEach((n) => {
344
- const s = at(n);
345
- Object.entries(s).forEach(([r, o]) => {
346
- e[r] = o;
347
- });
348
- }), e;
349
- }, bt = (t) => {
350
- let e = {};
351
- return typeof t == "object" && !Array.isArray(t) ? Object.entries(t).forEach(([n, s]) => {
352
- e[n] = !!s;
353
- }) : typeof t == "string" ? t.split(" ").forEach((n) => {
354
- n !== "" && (e[n] = !0);
355
- }) : Array.isArray(t) && t.forEach((n) => {
356
- const s = bt(n);
357
- Object.entries(s).forEach(([r, o]) => {
358
- e[r] = o;
359
- });
360
- }), e;
361
- }, kt = (t, e, n) => {
362
- const s = (r) => n.on[e](r, n.loops || []);
363
- t.addEventListener(e, s, !1), n.handlers = n.handlers || {}, n.handlers[e] = s;
364
- }, Kt = (t, e) => {
365
- const n = e.elm;
366
- Object.keys(e.on).forEach((s) => {
367
- t.handlers && t.handlers[s] && n.removeEventListener(s, t.handlers[s], !1), kt(n, s, e);
368
- }), Et(n, e.props, e.loops), $t(n, e), vt(n, e), typeof e.onUpdated == "function" && requestAnimationFrame(() => {
369
- e.onUpdated(n, e.loops || []);
370
- }), At(n, t.children, e.children);
371
- }, Qt = (t, e) => {
372
- switch (e.elm = t.elm, e.tagName) {
373
- case "comment":
374
- break;
375
- case "text":
376
- Gt(e);
377
- break;
378
- default:
379
- Kt(t, e);
380
- break;
381
- }
382
- }, ut = (t, e, n) => {
383
- n && n.parentNode === t ? t.insertBefore(e, n) : t.appendChild(e);
384
- }, Jt = (t, e, n) => {
385
- const s = yt(e), r = document.createTextNode(s);
386
- e.elm = r, ut(t, r, n);
387
- }, Nt = (t, e, n) => {
388
- const s = e.content, r = document.createComment(s);
389
- e.elm = r, ut(t, r, n);
390
- }, Xt = (t, e, n, s) => {
391
- e.tagName === "svg" && (s = zt);
392
- const r = s ? document.createElementNS(s, e.tagName) : document.createElement(e.tagName);
393
- Object.keys(e.attrs).forEach((o) => {
394
- const a = e.attrs[o] || "";
395
- o.charAt(0) !== "x" ? r.setAttribute(o, a) : o.startsWith("xml:") ? r.setAttributeNS(Bt, o, a) : o.startsWith("xlink:") ? r.setAttributeNS(qt, o, a) : o.startsWith("xmlns:") ? r.setAttributeNS(Vt, o, a) : r.setAttribute(o, a);
396
- }), Object.keys(e.on).forEach((o) => {
397
- kt(r, o, e);
398
- }), Et(r, e.props, e.loops), $t(r, e), vt(r, e), e.elm = r, ut(t, r, n), e.tagName === "foreignObject" && (s = void 0), e.children.forEach((o) => {
399
- N(r, o, void 0, s);
400
- }), typeof e.onMounted == "function" && requestAnimationFrame(() => {
401
- e.onMounted(r, e.loops || []);
402
- });
403
- }, N = (t, e, n, s) => {
404
- switch (e.tagName) {
405
- case "comment":
406
- Nt(t, e, n);
407
- break;
408
- case "text":
409
- Jt(t, e, n);
410
- break;
411
- default:
412
- Xt(t, e, n, s);
413
- break;
414
- }
415
- }, Zt = (t) => t && (t.tagName === "ElseIf" || t.tagName === "comment"), Yt = (t) => t && t.tagName === "Else", F = (t, e, n = [], s = "0", r) => {
416
- var i, f, d;
417
- let o = 0, a = 0;
418
- for (let l = 0; l < e.length; l++) {
419
- const c = e[l];
420
- if (c.tagName === "If") {
421
- let u = !1;
422
- o++;
423
- let p = 1;
424
- for (((i = c.fn) == null ? void 0 : i.call(c, n)) && (F(t, c.children, n, `${s}-if-${o}-${p}`, p), u = !0), p++; l + 1 < e.length && Zt(e[l + 1]); )
425
- ((d = (f = e[l + 1]).fn) == null ? void 0 : d.call(f, n)) && !u && (F(t, e[l + 1].children, n, `${s}-if-${o}-${p}`, p), u = !0), l++, p++;
426
- l + 1 < e.length && Yt(e[l + 1]) && (u || (F(t, e[l + 1].children, n, `${s}-if-${o}-${p}`, p), u = !0), l++);
427
- } else if (c.tagName === "For") {
428
- a++;
429
- const { item: u = "item", index: p = "index" } = c.attrs;
430
- (c.fn(n) || []).forEach((A, R) => {
431
- const Q = { item: u, value: A, index: p, valueIndex: R };
432
- F(t, c.children, [...n, Q], `${s}-for-${a}-${R}`, 1);
433
- });
434
- } else {
435
- const u = {
436
- ...c,
437
- loops: n,
438
- children: [],
439
- rootId: s,
440
- ifId: r,
441
- key: c.key || `${s}-${l}`
442
- // 添加 key 属性
443
- };
444
- F(u.children, c.children, n, `${s}-${l}`), t.push(u);
445
- }
446
- }
447
- }, At = (t, e, n) => {
448
- var a, i, f, d, l;
449
- const s = /* @__PURE__ */ new Map();
450
- e.forEach((c, u) => s.set(c.key, { node: c, index: u }));
451
- let r = 0, o = 0;
452
- for (; r < n.length; ) {
453
- const c = n[r], u = s.get(c.key);
454
- if (u) {
455
- const p = u.node;
456
- if (p.tagName !== c.tagName) {
457
- (a = p.elm) == null || a.remove();
458
- const $ = (i = e[o + 1]) == null ? void 0 : i.elm;
459
- N(t, c, $), s.delete(c.key), o++;
460
- } else {
461
- if (c.ifId && p.ifId !== c.ifId) {
462
- (f = p.elm) == null || f.remove();
463
- const $ = (d = e[o + 1]) == null ? void 0 : d.elm;
464
- N(t, c, $);
465
- } else
466
- Qt(p, c);
467
- s.delete(c.key), o++;
468
- }
469
- } else {
470
- const p = (l = e[o]) == null ? void 0 : l.elm;
471
- N(t, c, p);
472
- }
473
- r++;
474
- }
475
- s.forEach(({ node: c }) => {
476
- var u;
477
- return (u = c.elm) == null ? void 0 : u.remove();
478
- });
479
- }, Ct = (t, e, n = []) => {
480
- if (!e || e.length === 0) return [];
481
- const s = [];
482
- return F(s, e || [], [], "0"), At(t, n, s), s;
483
- }, j = (t) => {
484
- if (t === null || typeof t != "object") return !1;
485
- const e = Object.prototype.toString.call(t);
486
- return ![
487
- "[object Function]",
488
- "[object Date]",
489
- "[object RegExp]",
490
- "[object Map]",
491
- "[object Set]",
492
- "[object WeakMap]",
493
- "[object WeakSet]"
494
- ].includes(e);
495
- };
496
- function te(t = window.location) {
497
- let e;
498
- typeof t == "string" ? e = new URL(t).hash : e = t.hash;
499
- const n = e.split("?"), s = n[0].substring(2), r = n[1], o = {};
500
- return r && r.split("&").forEach((a) => {
501
- const [i, f] = a.split("=");
502
- i && f && (o[decodeURIComponent(i)] = decodeURIComponent(f));
503
- }), {
504
- path: s.split("/"),
505
- // 将路径分割成数组
506
- query: o
507
- // 查询参数对象
508
- };
509
- }
510
- function pt(t, e, n = {}) {
511
- const s = /* @__PURE__ */ new WeakMap(), r = /* @__PURE__ */ new WeakSet(), o = /* @__PURE__ */ new WeakMap(), { throttle: a = 0, deepCompare: i = !1, onBatch: f = null, immediate: d = !1 } = n, l = Array.isArray(n.filter) ? n.filter : n.filter ? [n.filter] : [];
512
- let c = [], u = null, p = !0;
513
- const $ = (h, m, v = /* @__PURE__ */ new WeakMap()) => {
514
- if (h === m) return !0;
515
- if (h == null || m == null || typeof h != "object" || typeof m != "object") return h === m;
516
- if (v.has(h)) return v.get(h) === m;
517
- if (v.set(h, m), Array.isArray(h) && Array.isArray(m)) {
518
- if (h.length !== m.length) return !1;
519
- for (let g = 0; g < h.length; g++)
520
- if (!$(h[g], m[g], v)) return !1;
521
- return !0;
522
- }
523
- if (Array.isArray(h) !== Array.isArray(m)) return !1;
524
- const b = Object.keys(h), y = Object.keys(m);
525
- if (b.length !== y.length) return !1;
526
- for (const g of b)
527
- if (!Object.prototype.hasOwnProperty.call(m, g) || !$(h[g], m[g], v)) return !1;
528
- return !0;
529
- }, A = (h) => {
530
- if (l.length === 0) return !0;
531
- const m = h.map(String).join(".");
532
- return l.some((v) => {
533
- if (!v) return !0;
534
- try {
535
- return new RegExp(
536
- "^" + v.replace(/\./g, "\\.").replace(/\[\*\]/g, "(\\.\\d+)?").replace(/\*\*/g, "(.+)").replace(/\*/g, "[^.]+") + "$"
537
- ).test(m);
538
- } catch {
539
- return !0;
540
- }
541
- });
542
- }, R = (h, m, v) => {
543
- p && A(h) && (a > 0 ? (c.push({ path: h, newValue: m, oldValue: v }), u || (u = setTimeout(() => {
544
- if (!p) {
545
- u = null;
546
- return;
547
- }
548
- const b = c;
549
- c = [], u = null, f && f(b);
550
- for (const y of b)
551
- e(y.path, y.newValue, y.oldValue);
552
- }, a))) : (e(h, m, v), f && f([{ path: h, newValue: m, oldValue: v }])));
553
- }, Q = (h, m) => {
554
- if (!j(h) || !(Array.isArray(h) || Object.prototype.toString.call(h) === "[object Object]")) return h;
555
- const v = {};
556
- if (["push", "pop", "shift", "unshift", "splice", "sort", "reverse"].forEach((y) => {
557
- const g = Array.prototype[y];
558
- v[y] = function(...E) {
559
- if (!p) return Reflect.apply(g, h, E);
560
- const C = [...h], S = g.apply(h, E);
561
- return $(h, C) || R(m, h, C), S;
562
- };
563
- }), s.has(h)) {
564
- const y = s.get(h);
565
- return o.set(y, m), y;
566
- }
567
- r.add(h);
568
- const b = new Proxy(h, {
569
- get(y, g, E) {
570
- if (Array.isArray(y) && v.hasOwnProperty(g))
571
- return Reflect.get(v, g, E);
572
- const C = Reflect.get(y, g, E);
573
- if (j(C)) {
574
- const J = [...o.get(b) || [], g], nt = Q(C, J);
575
- return o.set(nt, J), nt;
576
- }
577
- return C;
578
- },
579
- set(y, g, E, C) {
580
- const S = Reflect.get(y, g, C), J = j(E) && !(E instanceof Date || E instanceof RegExp || E instanceof Map || E instanceof Set);
581
- if (i && J ? $(S, E) : S === E) return !0;
582
- const Mt = Reflect.set(y, g, E, C), Pt = o.get(b) || m;
583
- return R([...Pt, g], E, S), Mt;
584
- },
585
- deleteProperty(y, g) {
586
- if (Reflect.has(y, g)) {
587
- const E = y[g], C = Reflect.deleteProperty(y, g), S = o.get(b) || [];
588
- return C && r.has(y) && R([...S, g], void 0, E), C;
589
- }
590
- return !1;
591
- }
592
- });
593
- return s.set(h, b), o.set(b, m), b;
594
- }, Rt = Q(t, []);
595
- if (d) {
596
- const h = (m, v, b = /* @__PURE__ */ new WeakSet()) => {
597
- if (!(!j(m) || !p || b.has(m))) {
598
- b.add(m);
599
- for (const y of Object.keys(m)) {
600
- const g = [...v, y], E = m[y];
601
- A(g) && e(g, E, void 0), j(E) && h(E, g, b);
602
- }
603
- }
604
- };
605
- h(t, []);
606
- }
607
- return { proxy: Rt, unobserve: () => {
608
- p && (p = !1, c = [], u && (clearTimeout(u), u = null));
609
- }, resume: () => {
610
- p = !0;
611
- } };
612
- }
613
- var V, z, G, L, K, D, _, w, U, it;
614
- class le extends HTMLElement {
615
- // 停止数据监听函数
616
- // -----------------------------------------------------
617
- /**
618
- * 构造函数
619
- * -------------------------------------------------------
620
- * 初始化模板、响应式数据和渲染根节点。
621
- * 样式注入延迟到 connectedCallback 阶段。
622
- */
623
- constructor(n = {}) {
624
- var a;
625
- super();
626
- M(this, w);
627
- // ---------------------- 私有属性 ----------------------
628
- M(this, V);
629
- // 响应式数据(Proxy)
630
- M(this, z, []);
631
- // 模板解析后的虚拟节点 AST
632
- M(this, G, []);
633
- // 上一次渲染的虚拟节点
634
- M(this, L, !1);
635
- // 节流刷新,防止重复 patch
636
- M(this, K, !1);
637
- // 是否已挂载
638
- M(this, D);
639
- // 渲染根节点(Shadow DOM 优先)
640
- M(this, _);
641
- const s = this._manifest || {};
642
- k(this, D, (a = s.shadowDOM) != null && a.use ? this.attachShadow({ mode: s.shadowDOM.mode || "open" }) : this);
643
- const { proxy: r, unobserve: o } = pt(n, () => W(this, w, U).call(this));
644
- k(this, V, r), k(this, _, o), s.template && typeof s.template == "string" && k(this, z, Tt(this, s.template)), Promise.resolve().then(() => this.created());
645
- }
646
- /** Web Components 标准方法:声明哪些属性变化会触发 attributeChangedCallback */
647
- static get observedAttributes() {
648
- const n = this._manifest || this.prototype._manifest;
649
- return n != null && n.props ? n.props.map((s) => typeof s == "string" ? s : s.name) : [];
650
- }
651
- // ---------------------- $data getter/setter ----------------------
652
- /** 获取当前组件数据 */
653
- get $data() {
654
- return x(this, V);
655
- }
656
- /** 更新组件数据 */
657
- set $data(n) {
658
- j(n) || (n = {}), x(this, _) && x(this, _).call(this);
659
- const { proxy: s, unobserve: r } = pt(n, () => W(this, w, U).call(this));
660
- k(this, V, s), k(this, _, r), W(this, w, U).call(this);
661
- }
662
- /**
663
- * 手动刷新组件
664
- * 外部可调用,用于数据未触发 observe 或强制刷新
665
- */
666
- refresh() {
667
- W(this, w, U).call(this);
668
- }
669
- // ---------------------- Web Components 生命周期 ----------------------
670
- connectedCallback() {
671
- const n = this._manifest || {};
672
- if ((n.className || n.tagName || "").split(" ").forEach((r) => r && this.classList.add(r)), n.styles && !x(this, D).querySelector("style[data-manifest-style]")) {
673
- const r = document.createElement("style");
674
- r.setAttribute("data-manifest-style", "true"), r.textContent = n.styles, x(this, D).appendChild(r);
675
- }
676
- if (n.props && j(this.$data))
677
- for (const r of n.props) {
678
- const o = typeof r == "string" ? r : r.name, a = typeof r == "object" && "type" in r ? r.type : void 0, i = o.replace(/-([a-z])/g, (d, l) => l.toUpperCase()), f = this.$data[i];
679
- if (this.hasAttribute(o)) {
680
- const d = this.getAttribute(o);
681
- this.$data[i] = W(this, w, it).call(this, d, f, a);
682
- }
683
- }
684
- W(this, w, U).call(this);
685
- }
686
- disconnectedCallback() {
687
- var n;
688
- this.isConnected && (this.unmounted(), (n = x(this, _)) == null || n.call(this), k(this, _, void 0));
689
- }
690
- attributeChangedCallback(n, s, r) {
691
- if (s === r || !j(this.$data)) return;
692
- const o = n.replace(/-([a-z])/g, (i, f) => f.toUpperCase()), a = this.$data[o];
693
- this.$data[o] = W(this, w, it).call(this, r, a);
694
- }
695
- // ---------------------- 自定义生命周期钩子 ----------------------
696
- /** 兼容旧组件的初始化钩子(首次 mounted 后调用) */
697
- onInit() {
698
- }
699
- /** 组件创建完成(构造函数结束时调用) */
700
- created() {
701
- }
702
- /** 组件首次挂载到 DOM 后调用 */
703
- mounted() {
704
- }
705
- /** 每次更新前调用 */
706
- beforeUpdate() {
707
- }
708
- /** 每次更新完成后调用 */
709
- updated() {
710
- }
711
- /** 组件从 DOM 中卸载时调用 */
712
- unmounted() {
713
- }
714
- }
715
- V = new WeakMap(), z = new WeakMap(), G = new WeakMap(), L = new WeakMap(), K = new WeakMap(), D = new WeakMap(), _ = new WeakMap(), w = new WeakSet(), // ---------------------- 渲染核心逻辑 ----------------------
716
- /** 内部刷新函数,由 observe() 或外部调用触发 */
717
- U = function() {
718
- x(this, L) || (k(this, L, !0), Dt(() => {
719
- var n;
720
- if (!this.isConnected) {
721
- k(this, L, !1);
722
- return;
723
- }
724
- this.beforeUpdate(), k(this, G, Ct(x(this, D), x(this, z), x(this, G))), k(this, L, !1), x(this, K) ? this.updated() : (k(this, K, !0), this.mounted(), (n = this.onInit) == null || n.call(this));
725
- }));
726
- }, // ---------------------- 属性类型转换工具 ----------------------
727
- it = function(n, s, r) {
728
- if (n === null) return s;
729
- switch (r || typeof s) {
730
- case "number":
731
- const a = Number(n);
732
- return isNaN(a) ? s : a;
733
- case "boolean":
734
- return n === "true";
735
- case "object":
736
- try {
737
- return JSON.parse(n);
738
- } catch {
739
- return s;
740
- }
741
- default:
742
- return n;
743
- }
744
- };
745
- const ee = (t) => (e) => {
746
- if (customElements.get(t.tagName)) {
747
- console.warn(`标签名称 "${t.tagName}" 已经被注册`);
748
- return;
749
- }
750
- return e.prototype._manifest = ne(t), customElements.define(t.tagName, e), e;
751
- }, ne = (t) => (t = Object.assign({
752
- hasConfig: !1
753
- }, t), t);
754
- var se = Object.getOwnPropertyDescriptor, xt = (t) => {
755
- throw TypeError(t);
756
- }, re = (t, e, n, s) => {
757
- for (var r = s > 1 ? void 0 : s ? se(e, n) : e, o = t.length - 1, a; o >= 0; o--)
758
- (a = t[o]) && (r = a(r) || r);
759
- return r;
760
- }, ht = (t, e, n) => e.has(t) || xt("Cannot " + n), P = (t, e, n) => (ht(t, e, "read from private field"), n ? n.call(t) : e.get(t)), I = (t, e, n) => e.has(t) ? xt("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, n), T = (t, e, n, s) => (ht(t, e, "write to private field"), e.set(t, n), n), oe = (t, e, n) => (ht(t, e, "access private method"), n), Y, tt, et, B, q, X, Z, ct, wt;
761
- function dt(t) {
762
- return t instanceof HTMLElement && t.isRouterView === !0;
763
- }
764
- let mt = class extends HTMLElement {
765
- constructor() {
766
- super(...arguments), I(this, ct), I(this, Y, []), I(this, tt, []), I(this, et, null), I(this, B), I(this, q, []), I(this, X, {}), I(this, Z, this.onHashChange.bind(this));
767
- }
768
- // #endregion
769
- // #region 公共属性和访问器
770
- get isRouterView() {
771
- return !0;
772
- }
773
- get $routes() {
774
- return P(this, q);
775
- }
776
- set $routes(t) {
777
- T(this, q, t), this.onHashChange();
778
- }
779
- get $remainingPath() {
780
- return P(this, tt);
781
- }
782
- get $matchedRoute() {
783
- return P(this, et);
784
- }
785
- set $pipe(t) {
786
- T(this, X, t || {}), this.onHashChange();
787
- }
788
- // #endregion
789
- // #region 生命周期回调
790
- connectedCallback() {
791
- window.addEventListener("hashchange", P(this, Z)), this.onHashChange();
792
- }
793
- disconnectedCallback() {
794
- window.removeEventListener("hashchange", P(this, Z));
795
- }
796
- // #endregion
797
- /**
798
- * Hash 变化时的核心处理函数
799
- */
800
- onHashChange() {
801
- var e;
802
- if (!this.isConnected) return;
803
- const t = this.findAncestorRouterView();
804
- if (t)
805
- T(this, B, t.$remainingPath), T(this, q, ((e = t.$matchedRoute) == null ? void 0 : e.children) || []);
806
- else {
807
- const { path: n } = te(window.location);
808
- T(this, B, n);
809
- }
810
- oe(this, ct, wt).call(this);
811
- }
812
- /**
813
- * 准备传递给子组件的 props 和事件监听
814
- * @param params 从路由中解析出的参数
815
- * @returns 包含 props 和 on 的对象
816
- */
817
- prepareComponentData(t) {
818
- const e = {}, n = {};
819
- for (const [s, r] of Object.entries(t))
820
- e[s] = () => r;
821
- for (const [s, r] of Object.entries(P(this, X))) {
822
- const o = s.toLowerCase();
823
- let a = null;
824
- o.startsWith("on-") ? a = o.slice(3) : o.startsWith("on") && gt.includes(o.slice(2)) && (a = o.slice(2)), a ? n[a] = r : e[s] = () => r;
825
- }
826
- return { props: e, on: n };
827
- }
828
- /**
829
- * 在路由表中查找与当前路径匹配的路由
830
- * @param routes 路由配置数组
831
- * @param pathSegments URL 路径分段数组
832
- * @returns 匹配结果
833
- */
834
- matchRoute(t, e) {
835
- for (const n of t) {
836
- const s = n.path.split("/").filter((a) => a), r = {};
837
- if (s.length > e.length && !s.includes("*"))
838
- continue;
839
- let o = !0;
840
- for (let a = 0; a < s.length; a++) {
841
- const i = s[a], f = e[a];
842
- if (i.startsWith(":"))
843
- r[i.substring(1)] = f;
844
- else {
845
- if (i === "*")
846
- return {
847
- matchedRoute: n,
848
- params: r,
849
- remainingPath: []
850
- // 通配符消耗所有剩余路径
851
- };
852
- if (i !== f) {
853
- o = !1;
854
- break;
855
- }
856
- }
857
- }
858
- if (o)
859
- return {
860
- matchedRoute: n,
861
- params: r,
862
- remainingPath: e.slice(s.length)
863
- };
864
- }
865
- return { matchedRoute: null, params: {}, remainingPath: e };
866
- }
867
- /**
868
- * 递归通知所有子节点中的 router-view 进行更新
869
- * @param node 当前遍历的起始节点
870
- */
871
- propagateToChildRouterViews(t = this) {
872
- for (const e of t.childNodes)
873
- dt(e) ? e.onHashChange() : this.propagateToChildRouterViews(e);
874
- }
875
- /**
876
- * 沿 DOM 树向上查找最近的父 router-view 实例
877
- * @returns 父 RouterViewElement 实例或 null
878
- */
879
- findAncestorRouterView() {
880
- let t = this.parentElement;
881
- for (; t; ) {
882
- if (dt(t))
883
- return t;
884
- t = t.parentElement;
885
- }
886
- return null;
887
- }
888
- };
889
- Y = /* @__PURE__ */ new WeakMap();
890
- tt = /* @__PURE__ */ new WeakMap();
891
- et = /* @__PURE__ */ new WeakMap();
892
- B = /* @__PURE__ */ new WeakMap();
893
- q = /* @__PURE__ */ new WeakMap();
894
- X = /* @__PURE__ */ new WeakMap();
895
- Z = /* @__PURE__ */ new WeakMap();
896
- ct = /* @__PURE__ */ new WeakSet();
897
- wt = function() {
898
- const t = P(this, B) || [], { matchedRoute: e, params: n, remainingPath: s } = this.matchRoute(P(this, q), t);
899
- T(this, tt, s), T(this, et, e);
900
- const r = (e == null ? void 0 : e.tagName) || "comment", { props: o, on: a } = this.prepareComponentData(n);
901
- T(this, Y, Ct(this, [{
902
- tagName: r,
903
- rootId: "0",
904
- // 假设 rootId 是固定的
905
- attrs: {},
906
- props: o,
907
- on: a,
908
- children: []
909
- }], P(this, Y))), this.propagateToChildRouterViews();
910
- };
911
- mt = re([
912
- ee({
913
- tagName: "router-view"
914
- })
915
- ], mt);
916
- export {
917
- le as BaseElement,
918
- ee as CustomElement,
919
- mt as RouterViewElement,
920
- lt as createFunction,
921
- j as isObject,
922
- gt as listeners,
923
- pt as observe,
924
- te as parseHashUrl,
925
- Tt as parseHtml,
926
- Ct as patch,
927
- Dt as runInAsyncQueue
928
- };