solely 0.0.30 → 0.1.1

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