marko 6.0.0-next.3.22 → 6.0.0-next.3.23

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,11 +1,11 @@
1
+ import type { Accessor } from "../common/types";
1
2
  import type { ServerRenderer } from "./template";
2
- import { type PartialScope } from "./writer";
3
3
  interface BodyContentObject {
4
4
  [x: PropertyKey]: unknown;
5
5
  content: ServerRenderer;
6
6
  }
7
- export declare function dynamicTagInput(scope: PartialScope, tag: unknown | string | ServerRenderer | BodyContentObject, input: Record<string, unknown>, content?: () => void, tagVar?: unknown): unknown;
8
- export declare function dynamicTagArgs(scope: PartialScope, tag: unknown | string | ServerRenderer | BodyContentObject, args: unknown[]): unknown;
7
+ export declare function dynamicTagInput(scopeId: number, accessor: Accessor, tag: unknown | string | ServerRenderer | BodyContentObject, input: Record<string, unknown>, content?: () => void, tagVar?: unknown): undefined;
8
+ export declare function dynamicTagArgs(scopeId: number, accessor: Accessor, tag: unknown | string | ServerRenderer | BodyContentObject, args: unknown[]): undefined;
9
9
  declare let getDynamicRenderer: (value: any) => string | ServerRenderer | undefined;
10
10
  export declare let createRenderer: (fn: ServerRenderer) => ServerRenderer;
11
11
  export declare function patchDynamicTag(newGetDynamicRenderer: typeof getDynamicRenderer, newCreateRenderer: typeof createRenderer): void;
@@ -1,4 +1,4 @@
1
- import type { $Global, Accessor } from "../common/types";
1
+ import { type $Global, type Accessor, type Falsy, ResumeSymbol } from "../common/types";
2
2
  import { Serializer } from "./serializer";
3
3
  export type PartialScope = Record<Accessor, unknown>;
4
4
  type ScopeInternals = PartialScope & {
@@ -8,12 +8,7 @@ declare const K_SCOPE_ID: unique symbol;
8
8
  declare enum Mark {
9
9
  Placeholder = "!^",
10
10
  PlaceholderEnd = "!",
11
- ReorderMarker = "#",
12
- SectionStart = "[",
13
- SectionEnd = "]",
14
- SectionSingleNodesEnd = "|",
15
- Node = "*",
16
- Cleanup = "$"
11
+ ReorderMarker = "#"
17
12
  }
18
13
  export declare function getChunk(): Chunk | undefined;
19
14
  export declare function getScopeId(scope: unknown): number | undefined;
@@ -29,10 +24,15 @@ export declare function peekNextScope(): PartialScope;
29
24
  export declare function getScopeById(scopeId: number | undefined): PartialScope | undefined;
30
25
  export declare function markResumeNode(scopeId: number, accessor: Accessor): string;
31
26
  export declare function nodeRef(scopeId: number, id?: string): () => void;
32
- export declare function markResumeScopeStart(scopeId: number, index?: number): string;
33
- export declare function markResumeControlEnd(scopeId: number, accessor: Accessor): string;
34
- export declare function markResumeControlSingleNodeEnd(scopeId: number, accessor: Accessor, childScopeIds?: number | number[]): string;
35
- export declare function markResumeCleanup(scopeId: number): string;
27
+ export declare function resumeClosestBranch(scopeId: number): void;
28
+ export declare function resumeForOf(list: Falsy | Iterable<unknown>, cb: (item: unknown, index: number) => void, scopeId: number, accessor: Accessor): void;
29
+ export declare function resumeSingleNodeForOf(list: Falsy | Iterable<unknown>, cb: (item: unknown, index: number) => void, scopeId: number, accessor: Accessor): void;
30
+ export declare function resumeForIn(obj: Falsy | {}, cb: (key: string, value: unknown) => void, scopeId: number, accessor: Accessor): void;
31
+ export declare function resumeSingleNodeForIn(obj: Falsy | {}, cb: (key: string, value: unknown) => void, scopeId: number, accessor: Accessor): void;
32
+ export declare function resumeForTo(to: number, from: number | Falsy, step: number | Falsy, cb: (index: number) => void, scopeId: number, accessor: Accessor): void;
33
+ export declare function resumeSingleNodeForTo(to: number, from: number | Falsy, step: number | Falsy, cb: (index: number) => void, scopeId: number, accessor: Accessor): void;
34
+ export declare function resumeConditional(cb: () => void | number, scopeId: number, accessor: Accessor): void;
35
+ export declare function resumeSingleNodeConditional(cb: () => void | number, scopeId: number, accessor: Accessor): void;
36
36
  export declare function writeScope(scopeId: number, partialScope: PartialScope): ScopeInternals;
37
37
  export declare function writeExistingScope(scope: ScopeInternals): ScopeInternals;
38
38
  export declare function ensureScopeWithId(scopeId: number): PartialScope;
@@ -79,7 +79,7 @@ export declare class State {
79
79
  get commentPrefix(): string;
80
80
  reorder(chunk: Chunk): void;
81
81
  nextReorderId(): string;
82
- mark(code: Mark, str: string): string;
82
+ mark(code: ResumeSymbol | Mark, str: string): string;
83
83
  }
84
84
  export declare class Boundary extends AbortController {
85
85
  state: State;
package/dist/html.d.ts CHANGED
@@ -6,4 +6,4 @@ export { escapeScript, escapeStyle, escapeXML, toString } from "./html/content";
6
6
  export { createRenderer, dynamicTagArgs, dynamicTagInput, } from "./html/dynamic-tag";
7
7
  export { forIn, forInBy, forOf, forOfBy, forTo, forToBy } from "./html/for";
8
8
  export { createTemplate } from "./html/template";
9
- export { $global, ensureScopeWithId, fork, getScopeById, markResumeCleanup, markResumeControlEnd, markResumeControlSingleNodeEnd, markResumeNode, markResumeScopeStart, nextScopeId, nextTagId, nodeRef, peekNextScope, register, tryContent, write, writeEffect, writeExistingScope, writeScope, writeTrailers, } from "./html/writer";
9
+ export { $global, ensureScopeWithId, fork, getScopeById, markResumeNode, nextScopeId, nextTagId, nodeRef, peekNextScope, register, resumeClosestBranch, resumeConditional, resumeForIn, resumeForOf, resumeForTo, resumeSingleNodeConditional, resumeSingleNodeForIn, resumeSingleNodeForOf, resumeSingleNodeForTo, tryContent, write, writeEffect, writeExistingScope, writeScope, writeTrailers, } from "./html/writer";
package/dist/html.js CHANGED
@@ -46,11 +46,7 @@ __export(html_exports, {
46
46
  forToBy: () => forToBy,
47
47
  fork: () => fork,
48
48
  getScopeById: () => getScopeById,
49
- markResumeCleanup: () => markResumeCleanup,
50
- markResumeControlEnd: () => markResumeControlEnd,
51
- markResumeControlSingleNodeEnd: () => markResumeControlSingleNodeEnd,
52
49
  markResumeNode: () => markResumeNode,
53
- markResumeScopeStart: () => markResumeScopeStart,
54
50
  nextScopeId: () => nextScopeId,
55
51
  nextTagId: () => nextTagId,
56
52
  nodeRef: () => nodeRef,
@@ -59,6 +55,15 @@ __export(html_exports, {
59
55
  partialAttrs: () => partialAttrs,
60
56
  peekNextScope: () => peekNextScope,
61
57
  register: () => register2,
58
+ resumeClosestBranch: () => resumeClosestBranch,
59
+ resumeConditional: () => resumeConditional,
60
+ resumeForIn: () => resumeForIn,
61
+ resumeForOf: () => resumeForOf,
62
+ resumeForTo: () => resumeForTo,
63
+ resumeSingleNodeConditional: () => resumeSingleNodeConditional,
64
+ resumeSingleNodeForIn: () => resumeSingleNodeForIn,
65
+ resumeSingleNodeForOf: () => resumeSingleNodeForOf,
66
+ resumeSingleNodeForTo: () => resumeSingleNodeForTo,
62
67
  styleAttr: () => styleAttr,
63
68
  toString: () => toString,
64
69
  tryContent: () => tryContent,
@@ -151,6 +156,24 @@ function escapeStyle(val) {
151
156
  return val ? escapeStyleStr(val + "") : val === 0 ? "0" : "";
152
157
  }
153
158
 
159
+ // src/common/for.ts
160
+ function forIn(obj, cb) {
161
+ for (let key in obj)
162
+ cb(key, obj[key]);
163
+ }
164
+ function forOf(list, cb) {
165
+ if (list) {
166
+ let i = 0;
167
+ for (let item of list)
168
+ cb(item, i++);
169
+ }
170
+ }
171
+ function forTo(to, from, step, cb) {
172
+ let start = from || 0, delta = step || 1;
173
+ for (let steps = (to - start) / delta, i = 0; i <= steps; i++)
174
+ cb(start + i * delta);
175
+ }
176
+
154
177
  // src/html/inlined-runtimes.ts
155
178
  var WALKER_RUNTIME_CODE = '(e=>self[e]=self[e]||(l=>{let t,d={},s=[],f=document,i=f.createTreeWalker(f,129),n=self[e][l]={i:l=e+l,d:f,l:d,v:s,x(){},w(e){for(;e=i.nextNode();)this.x(n=(n=e.data)&&!n.indexOf(l)&&(d[t=n.slice(x+1)]=e,n[x]),t,e),n>"#"&&s.push(e)}},x=l.length}))', REORDER_RUNTIME_CODE = '(e=>{let i,t,r,l={},o=(e,i)=>{e.replaceWith(...i.childNodes),i.remove()};e.d.head.append(e.d.querySelector("style["+e.i+"]")||""),e.j={},e.x=(d,n,a,c,p)=>{"#"==d?(l[n]=t).i++:a==r&&i(),"T"==a.tagName&&(n=a.getAttribute(e.i))&&((c=e.l["^"+n])&&(l[n]={i:1,c(i=e.l[n]||a){for(;i.parentNode!==c.parentNode;)i=i.parentNode;for(;i!=r;(r=c.nextSibling).remove());o(c,a)}}),r=a.nextSibling,t=l[n],i=()=>{c||o(e.l[n],a),--t.i||t.c()},(d=e.j[n])&&(p=t.c,t.c=()=>p()+d(e)))}})';
156
179
 
@@ -1007,23 +1030,114 @@ function nodeRef(scopeId, id) {
1007
1030
  };
1008
1031
  return id ? register2(getter, id, scopeId) : getter;
1009
1032
  }
1010
- function markResumeScopeStart(scopeId, index) {
1011
- return $chunk.boundary.state.mark(
1012
- "[" /* SectionStart */,
1013
- scopeId + (index ? " " + index : "")
1033
+ function resumeClosestBranch(scopeId) {
1034
+ let branchId = $chunk.context?.[branchIdKey];
1035
+ branchId !== void 0 && branchId !== scopeId && $chunk.writeHTML(
1036
+ $chunk.boundary.state.mark("$" /* ClosestBranch */, "" + scopeId)
1037
+ );
1038
+ }
1039
+ var branchIdKey = Symbol();
1040
+ function resumeForOf(list, cb, scopeId, accessor) {
1041
+ forOf(list, (item, i) => {
1042
+ let branchId = peekNextScopeId();
1043
+ $chunk.writeHTML(
1044
+ $chunk.boundary.state.mark(
1045
+ "[" /* BranchStart */,
1046
+ branchId + (i ? " " : "")
1047
+ )
1048
+ ), withContext(branchIdKey, branchId, () => cb(item, i));
1049
+ }), $chunk.writeHTML(
1050
+ $chunk.boundary.state.mark(
1051
+ "]" /* BranchEnd */,
1052
+ scopeId + " " + accessor
1053
+ )
1054
+ );
1055
+ }
1056
+ function resumeSingleNodeForOf(list, cb, scopeId, accessor) {
1057
+ let branchIds = "";
1058
+ forOf(list, (item, index) => {
1059
+ let branchId = peekNextScopeId();
1060
+ branchIds = " " + branchId + branchIds, withContext(branchIdKey, branchId, () => cb(item, index));
1061
+ }), $chunk.writeHTML(
1062
+ $chunk.boundary.state.mark(
1063
+ "|" /* BranchSingleNode */,
1064
+ scopeId + " " + accessor + branchIds
1065
+ )
1066
+ );
1067
+ }
1068
+ function resumeForIn(obj, cb, scopeId, accessor) {
1069
+ let sep = "";
1070
+ forIn(obj, (key, value) => {
1071
+ let branchId = peekNextScopeId();
1072
+ $chunk.writeHTML(
1073
+ $chunk.boundary.state.mark("[" /* BranchStart */, branchId + sep)
1074
+ ), sep = " ", withContext(branchIdKey, branchId, () => cb(key, value));
1075
+ }), $chunk.writeHTML(
1076
+ $chunk.boundary.state.mark(
1077
+ "]" /* BranchEnd */,
1078
+ scopeId + " " + accessor
1079
+ )
1080
+ );
1081
+ }
1082
+ function resumeSingleNodeForIn(obj, cb, scopeId, accessor) {
1083
+ let branchIds = "";
1084
+ forIn(obj, (key, value) => {
1085
+ let branchId = peekNextScopeId();
1086
+ branchIds = " " + branchId + branchIds, withContext(branchIdKey, branchId, () => cb(key, value));
1087
+ }), $chunk.writeHTML(
1088
+ $chunk.boundary.state.mark(
1089
+ "|" /* BranchSingleNode */,
1090
+ scopeId + " " + accessor + branchIds
1091
+ )
1092
+ );
1093
+ }
1094
+ function resumeForTo(to, from, step, cb, scopeId, accessor) {
1095
+ let sep = "";
1096
+ forTo(to, from, step, (index) => {
1097
+ let branchId = peekNextScopeId();
1098
+ $chunk.writeHTML(
1099
+ $chunk.boundary.state.mark("[" /* BranchStart */, branchId + sep)
1100
+ ), sep = " ", withContext(branchIdKey, branchId, () => cb(index));
1101
+ }), $chunk.writeHTML(
1102
+ $chunk.boundary.state.mark(
1103
+ "]" /* BranchEnd */,
1104
+ scopeId + " " + accessor
1105
+ )
1014
1106
  );
1015
1107
  }
1016
- function markResumeControlEnd(scopeId, accessor) {
1017
- return $chunk.boundary.state.mark("]" /* SectionEnd */, scopeId + " " + accessor);
1108
+ function resumeSingleNodeForTo(to, from, step, cb, scopeId, accessor) {
1109
+ let branchIds = "";
1110
+ forTo(to, from, step, (index) => {
1111
+ let branchId = peekNextScopeId();
1112
+ branchIds = " " + branchId + branchIds, withContext(branchIdKey, branchId, () => cb(index));
1113
+ }), $chunk.writeHTML(
1114
+ $chunk.boundary.state.mark(
1115
+ "|" /* BranchSingleNode */,
1116
+ scopeId + " " + accessor + branchIds
1117
+ )
1118
+ );
1018
1119
  }
1019
- function markResumeControlSingleNodeEnd(scopeId, accessor, childScopeIds) {
1020
- return $chunk.boundary.state.mark(
1021
- "|" /* SectionSingleNodesEnd */,
1022
- scopeId + " " + accessor + " " + (childScopeIds ?? "")
1120
+ function resumeConditional(cb, scopeId, accessor) {
1121
+ let branchId = peekNextScopeId();
1122
+ $chunk.writeHTML(
1123
+ $chunk.boundary.state.mark("[" /* BranchStart */, branchId + "")
1124
+ ), withContext(branchIdKey, branchId, cb), peekNextScopeId() !== branchId ? writeScope(branchId, {}) : nextScopeId(), $chunk.writeHTML(
1125
+ $chunk.boundary.state.mark(
1126
+ "]" /* BranchEnd */,
1127
+ scopeId + " " + accessor
1128
+ )
1023
1129
  );
1024
1130
  }
1025
- function markResumeCleanup(scopeId) {
1026
- return $chunk.boundary.state.mark("$" /* Cleanup */, "" + scopeId);
1131
+ function resumeSingleNodeConditional(cb, scopeId, accessor) {
1132
+ let branchId = peekNextScopeId();
1133
+ withContext(branchIdKey, branchId, cb);
1134
+ let rendered = peekNextScopeId() !== branchId;
1135
+ rendered ? writeScope(branchId, {}) : nextScopeId(), $chunk.writeHTML(
1136
+ $chunk.boundary.state.mark(
1137
+ "|" /* BranchSingleNode */,
1138
+ scopeId + " " + accessor + (rendered ? " " + branchId : "")
1139
+ )
1140
+ );
1027
1141
  }
1028
1142
  function writeScope(scopeId, partialScope) {
1029
1143
  let { state } = $chunk.boundary, { scopes } = state, scope = scopes.get(scopeId);
@@ -1211,7 +1325,7 @@ var State2 = class {
1211
1325
  if (cur.next && !cur.async) {
1212
1326
  let html = "", effects = "", scripts = "";
1213
1327
  do
1214
- cur.flushPlaceholder(), html += cur.html, effects += cur.effects, scripts = concatScripts(scripts, cur.scripts), cur.consumed = !0, cur = cur.next;
1328
+ cur.flushPlaceholder(), html += cur.html, effects = concatEffects(effects, cur.effects), scripts = concatScripts(scripts, cur.scripts), cur.consumed = !0, cur = cur.next;
1215
1329
  while (cur.next && !cur.async);
1216
1330
  cur.html = html + cur.html, cur.effects = concatEffects(effects, cur.effects), cur.scripts = concatScripts(scripts, cur.scripts);
1217
1331
  }
@@ -1511,36 +1625,72 @@ var DEFAULT_RUNTIME_ID = "M", DEFAULT_RENDER_ID = "_";
1511
1625
 
1512
1626
  // src/html/dynamic-tag.ts
1513
1627
  var voidElementsReg = /^(?:area|b(?:ase|r)|col|embed|hr|i(?:mg|nput)|link|meta|param|source|track|wbr)$/;
1514
- function dynamicTagInput(scope, tag, input, content, tagVar) {
1515
- if (!tag && !content) return;
1516
- let scopeId = getScopeId(scope);
1517
- return write(`${markResumeScopeStart(scopeId)}`), writeScope(scopeId, scope), tag ? typeof tag == "string" ? (nextScopeId(), write(
1518
- `<${tag}${attrs(input, 0, scopeId, tag)}>`
1519
- ), voidElementsReg.test(tag) || (tag === "textarea" ? write(
1520
- controllable_textarea_value(
1628
+ function dynamicTagInput(scopeId, accessor, tag, input, content, tagVar) {
1629
+ if (!tag && !content) {
1630
+ nextScopeId();
1631
+ return;
1632
+ }
1633
+ if (!tag) {
1634
+ resumeConditional(content, scopeId, accessor);
1635
+ return;
1636
+ }
1637
+ if (typeof tag == "string") {
1638
+ resumeSingleNodeConditional(
1639
+ () => {
1640
+ nextScopeId(), write(`<${tag}${attrs(input, accessor, scopeId, tag)}>`), voidElementsReg.test(tag) || (tag === "textarea" ? write(
1641
+ controllable_textarea_value(
1642
+ scopeId,
1643
+ accessor,
1644
+ input.value,
1645
+ input.valueChange
1646
+ )
1647
+ ) : content && (tag === "select" && ("value" in input || "valueChange" in input) ? controllable_select_value(
1648
+ scopeId,
1649
+ accessor,
1650
+ input.value,
1651
+ input.valueChange,
1652
+ content
1653
+ ) : content()), write(`</${tag}>`));
1654
+ },
1521
1655
  scopeId,
1522
- 0,
1523
- input.value,
1524
- input.valueChange
1525
- )
1526
- ) : content && (tag === "select" && ("value" in input || "valueChange" in input) ? controllable_select_value(
1656
+ accessor
1657
+ );
1658
+ return;
1659
+ }
1660
+ let renderer = getDynamicRenderer(tag), result;
1661
+ return resumeConditional(
1662
+ () => {
1663
+ result = renderer(content ? { ...input, content } : input, tagVar);
1664
+ },
1527
1665
  scopeId,
1528
- 0,
1529
- input.value,
1530
- input.valueChange,
1531
- content
1532
- ) : content()), write(`</${tag}>`)), null) : getDynamicRenderer(tag)(content ? { ...input, content } : input, tagVar) : content();
1533
- }
1534
- function dynamicTagArgs(scope, tag, args) {
1535
- if (!tag) return;
1536
- let scopeId = getScopeId(scope);
1537
- if (write(`${markResumeScopeStart(scopeId)}`), writeScope(scopeId, scope), typeof tag == "string") {
1538
- nextScopeId(), write(
1539
- `<${tag}${attrs(args[0], 0, scopeId, tag)}>`
1540
- ), voidElementsReg.test(tag) || write(`</${tag}>`);
1666
+ accessor
1667
+ ), result;
1668
+ }
1669
+ function dynamicTagArgs(scopeId, accessor, tag, args) {
1670
+ if (!tag) {
1671
+ nextScopeId();
1672
+ return;
1673
+ }
1674
+ if (typeof tag == "string") {
1675
+ resumeSingleNodeConditional(
1676
+ () => {
1677
+ nextScopeId(), write(
1678
+ `<${tag}${attrs(args[0], accessor, scopeId, tag)}>`
1679
+ ), voidElementsReg.test(tag) || write(`</${tag}>`);
1680
+ },
1681
+ scopeId,
1682
+ accessor
1683
+ );
1541
1684
  return;
1542
1685
  }
1543
- return getDynamicRenderer(tag)(...args);
1686
+ let renderer = getDynamicRenderer(tag), result;
1687
+ return resumeConditional(
1688
+ () => {
1689
+ result = renderer(...args);
1690
+ },
1691
+ scopeId,
1692
+ accessor
1693
+ ), result;
1544
1694
  }
1545
1695
  var getDynamicRenderer = normalizeDynamicRenderer, createRenderer = (fn) => fn;
1546
1696
  function patchDynamicTag(newGetDynamicRenderer, newCreateRenderer) {
@@ -1610,24 +1760,6 @@ var K_TAGS_API_STATE = Symbol(), COMPAT_REGISTRY = /* @__PURE__ */ new WeakMap()
1610
1760
  }
1611
1761
  };
1612
1762
 
1613
- // src/common/for.ts
1614
- function forIn(obj, cb) {
1615
- for (let key in obj)
1616
- cb(key, obj[key]);
1617
- }
1618
- function forOf(list, cb) {
1619
- if (list) {
1620
- let i = 0;
1621
- for (let item of list)
1622
- cb(item, i++);
1623
- }
1624
- }
1625
- function forTo(to, from, step, cb) {
1626
- let start = from || 0, delta = step || 1;
1627
- for (let steps = (to - start) / delta, i = 0; i <= steps; i++)
1628
- cb(start + i * delta);
1629
- }
1630
-
1631
1763
  // src/html/for.ts
1632
1764
  function forOfBy(by, item, index) {
1633
1765
  return by ? typeof by == "string" ? item[by] : by(item, index) : index;
@@ -1812,11 +1944,7 @@ var ServerRenderResult = class {
1812
1944
  forToBy,
1813
1945
  fork,
1814
1946
  getScopeById,
1815
- markResumeCleanup,
1816
- markResumeControlEnd,
1817
- markResumeControlSingleNodeEnd,
1818
1947
  markResumeNode,
1819
- markResumeScopeStart,
1820
1948
  nextScopeId,
1821
1949
  nextTagId,
1822
1950
  nodeRef,
@@ -1825,6 +1953,15 @@ var ServerRenderResult = class {
1825
1953
  partialAttrs,
1826
1954
  peekNextScope,
1827
1955
  register,
1956
+ resumeClosestBranch,
1957
+ resumeConditional,
1958
+ resumeForIn,
1959
+ resumeForOf,
1960
+ resumeForTo,
1961
+ resumeSingleNodeConditional,
1962
+ resumeSingleNodeForIn,
1963
+ resumeSingleNodeForOf,
1964
+ resumeSingleNodeForTo,
1828
1965
  styleAttr,
1829
1966
  toString,
1830
1967
  tryContent,