@littlepress/renderer-web 0.0.0-stage → 1.0.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 (187) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +20 -0
  3. package/README.md +229 -2
  4. package/dist/answer/answerComponents.d.ts +107 -0
  5. package/dist/answer/answerComponents.js +637 -0
  6. package/dist/answer/download.d.ts +63 -0
  7. package/dist/answer/download.js +109 -0
  8. package/dist/answer/downloadExport.d.ts +44 -0
  9. package/dist/answer/downloadExport.js +266 -0
  10. package/dist/answer/hostValues.d.ts +28 -0
  11. package/dist/answer/hostValues.js +379 -0
  12. package/dist/answer/mapComponent.d.ts +6 -0
  13. package/dist/answer/mapComponent.js +213 -0
  14. package/dist/answer/math.d.ts +57 -0
  15. package/dist/answer/math.js +168 -0
  16. package/dist/answer/surfaceStream.d.ts +51 -0
  17. package/dist/answer/surfaceStream.js +260 -0
  18. package/dist/answer/tableLayout.d.ts +42 -0
  19. package/dist/answer/tableLayout.js +89 -0
  20. package/dist/controls/ActionFeedback.d.ts +5 -0
  21. package/dist/controls/ActionFeedback.js +43 -0
  22. package/dist/controls/actionDispatch.d.ts +51 -0
  23. package/dist/controls/actionDispatch.js +211 -0
  24. package/dist/controls/actionOutcome.generated.d.ts +64 -0
  25. package/dist/controls/actionOutcome.generated.js +64 -0
  26. package/dist/controls/calculationPublisher.d.ts +14 -0
  27. package/dist/controls/calculationPublisher.js +6 -0
  28. package/dist/controls/calculations.d.ts +20 -0
  29. package/dist/controls/calculations.generated.d.ts +73 -0
  30. package/dist/controls/calculations.generated.js +85 -0
  31. package/dist/controls/calculations.js +208 -0
  32. package/dist/controls/controlState.d.ts +84 -0
  33. package/dist/controls/controlState.js +256 -0
  34. package/dist/controls/fieldMessages.d.ts +41 -0
  35. package/dist/controls/fieldMessages.js +81 -0
  36. package/dist/controls/fieldValidation.d.ts +19 -0
  37. package/dist/controls/fieldValidation.js +98 -0
  38. package/dist/controls/formComponents.d.ts +23 -0
  39. package/dist/controls/formComponents.js +738 -0
  40. package/dist/controls/formProgress.d.ts +63 -0
  41. package/dist/controls/formProgress.js +122 -0
  42. package/dist/controls/inputDrafts.d.ts +55 -0
  43. package/dist/controls/inputDrafts.js +183 -0
  44. package/dist/controls/inputValidation.d.ts +33 -0
  45. package/dist/controls/inputValidation.js +119 -0
  46. package/dist/controls/localWrites.d.ts +9 -0
  47. package/dist/controls/localWrites.js +29 -0
  48. package/dist/core/A2uiSurface.d.ts +13 -0
  49. package/dist/core/A2uiSurface.js +15 -0
  50. package/dist/core/AtlasIcon.d.ts +18 -0
  51. package/dist/core/AtlasIcon.js +22 -0
  52. package/dist/core/AtlasMedia.d.ts +22 -0
  53. package/dist/core/AtlasMedia.js +45 -0
  54. package/dist/core/Carousel.d.ts +61 -0
  55. package/dist/core/Carousel.js +183 -0
  56. package/dist/core/CoreMedia.d.ts +19 -0
  57. package/dist/core/CoreMedia.js +25 -0
  58. package/dist/core/FoundationA2uiSurface.d.ts +86 -0
  59. package/dist/core/FoundationA2uiSurface.js +440 -0
  60. package/dist/core/ImagePreview.d.ts +42 -0
  61. package/dist/core/ImagePreview.js +73 -0
  62. package/dist/core/LucideIcon.d.ts +19 -0
  63. package/dist/core/LucideIcon.js +42 -0
  64. package/dist/core/SurfaceLink.d.ts +35 -0
  65. package/dist/core/SurfaceLink.js +60 -0
  66. package/dist/core/appearance/aegis.d.ts +49 -0
  67. package/dist/core/appearance/aegis.js +34 -0
  68. package/dist/core/appearance/colour.d.ts +24 -0
  69. package/dist/core/appearance/colour.js +103 -0
  70. package/dist/core/appearance/css.d.ts +17 -0
  71. package/dist/core/appearance/css.js +153 -0
  72. package/dist/core/appearance/presets.generated.d.ts +4 -0
  73. package/dist/core/appearance/presets.generated.js +1184 -0
  74. package/dist/core/appearance/react.d.ts +29 -0
  75. package/dist/core/appearance/react.js +110 -0
  76. package/dist/core/appearance/resolve.d.ts +76 -0
  77. package/dist/core/appearance/resolve.js +127 -0
  78. package/dist/core/appearance/textScale.d.ts +11 -0
  79. package/dist/core/appearance/textScale.js +51 -0
  80. package/dist/core/appearance/theme.d.ts +138 -0
  81. package/dist/core/appearance/theme.js +274 -0
  82. package/dist/core/baseComponents.d.ts +53 -0
  83. package/dist/core/baseComponents.js +480 -0
  84. package/dist/core/binding.d.ts +46 -0
  85. package/dist/core/binding.js +118 -0
  86. package/dist/core/bundle.d.ts +23 -0
  87. package/dist/core/bundle.js +75 -0
  88. package/dist/core/catalog/catalog-index.generated.d.ts +44 -0
  89. package/dist/core/catalog/catalog-index.generated.js +79 -0
  90. package/dist/core/catalog/icons.generated.d.ts +6 -0
  91. package/dist/core/catalog/icons.generated.js +1788 -0
  92. package/dist/core/catalog.d.ts +6 -0
  93. package/dist/core/catalog.generated.d.ts +50 -0
  94. package/dist/core/catalog.generated.js +2174 -0
  95. package/dist/core/catalog.js +8 -0
  96. package/dist/core/chartRows.d.ts +7 -0
  97. package/dist/core/chartRows.js +38 -0
  98. package/dist/core/component-support.d.ts +39 -0
  99. package/dist/core/component-support.js +127 -0
  100. package/dist/core/components/actions.d.ts +4 -0
  101. package/dist/core/components/actions.js +21 -0
  102. package/dist/core/components/cards.d.ts +9 -0
  103. package/dist/core/components/cards.js +227 -0
  104. package/dist/core/components/charts.d.ts +18 -0
  105. package/dist/core/components/charts.js +412 -0
  106. package/dist/core/components/comparisonTable.d.ts +50 -0
  107. package/dist/core/components/comparisonTable.js +89 -0
  108. package/dist/core/components/content.d.ts +13 -0
  109. package/dist/core/components/content.js +108 -0
  110. package/dist/core/components/seriesCharts.d.ts +531 -0
  111. package/dist/core/components/seriesCharts.js +1053 -0
  112. package/dist/core/components/stepFlow.d.ts +7 -0
  113. package/dist/core/components/stepFlow.js +13 -0
  114. package/dist/core/components.full.d.ts +10 -0
  115. package/dist/core/components.full.js +41 -0
  116. package/dist/core/context.d.ts +207 -0
  117. package/dist/core/context.js +172 -0
  118. package/dist/core/coverage.d.ts +30 -0
  119. package/dist/core/coverage.js +93 -0
  120. package/dist/core/foundation.d.ts +21 -0
  121. package/dist/core/foundation.js +22 -0
  122. package/dist/core/fullTheme.d.ts +5 -0
  123. package/dist/core/fullTheme.js +9 -0
  124. package/dist/core/iconNodes.generated.d.ts +7 -0
  125. package/dist/core/iconNodes.generated.js +5 -0
  126. package/dist/core/lucideIcons.d.ts +5 -0
  127. package/dist/core/lucideIcons.js +7 -0
  128. package/dist/core/messages.d.ts +111 -0
  129. package/dist/core/messages.js +134 -0
  130. package/dist/core/pointer.d.ts +17 -0
  131. package/dist/core/pointer.js +104 -0
  132. package/dist/core/registry.d.ts +12 -0
  133. package/dist/core/registry.js +114 -0
  134. package/dist/core/runtime.d.ts +8 -0
  135. package/dist/core/runtime.js +6 -0
  136. package/dist/core/state.d.ts +73 -0
  137. package/dist/core/state.js +274 -0
  138. package/dist/core/surfaceMotion.d.ts +5 -0
  139. package/dist/core/surfaceMotion.js +46 -0
  140. package/dist/core/theme.d.ts +5 -0
  141. package/dist/core/theme.js +7 -0
  142. package/dist/core/useSurfaceImage.d.ts +20 -0
  143. package/dist/core/useSurfaceImage.js +65 -0
  144. package/dist/graph/RelationshipGraph.d.ts +14 -0
  145. package/dist/graph/RelationshipGraph.js +206 -0
  146. package/dist/graph/model.d.ts +20 -0
  147. package/dist/graph/model.js +36 -0
  148. package/dist/graph/program/assets.d.ts +3 -0
  149. package/dist/graph/program/assets.js +3 -0
  150. package/dist/graph/program/bridge.d.ts +6 -0
  151. package/dist/graph/program/bridge.generated.d.ts +136 -0
  152. package/dist/graph/program/bridge.generated.js +1 -0
  153. package/dist/graph/program/bridge.js +24 -0
  154. package/dist/graph/program/bridge.schema.json +717 -0
  155. package/dist/graph/program/bridgeValidator.cjs +2 -0
  156. package/dist/graph/program/bridgeValidator.d.cts +4 -0
  157. package/dist/graph/program/runner.d.ts +1 -0
  158. package/dist/graph/program/runner.html +2193 -0
  159. package/dist/graph/program/runner.js +157 -0
  160. package/dist/graph/program/runtime-manifest.json +1 -0
  161. package/dist/graph/program/view.d.ts +11 -0
  162. package/dist/graph/program/view.js +210 -0
  163. package/dist/graph/program/worker.d.ts +1 -0
  164. package/dist/graph/program/worker.js +2186 -0
  165. package/dist/index.d.ts +20 -0
  166. package/dist/index.js +45 -0
  167. package/dist/internal/dsh.d.ts +4 -0
  168. package/dist/internal/dsh.js +3 -0
  169. package/dist/internal/relationship-graph.d.ts +4 -0
  170. package/dist/internal/relationship-graph.js +7202 -0
  171. package/dist/page/layoutComponents.d.ts +6 -0
  172. package/dist/page/layoutComponents.js +123 -0
  173. package/dist/page/layoutGeometry.d.ts +68 -0
  174. package/dist/page/layoutGeometry.js +174 -0
  175. package/dist/page/pageFormat.d.ts +34 -0
  176. package/dist/page/pageFormat.js +119 -0
  177. package/dist/page/printFit.d.ts +81 -0
  178. package/dist/page/printFit.js +338 -0
  179. package/dist/react/index.d.ts +5 -0
  180. package/dist/react/index.js +2 -0
  181. package/dist/report/locate.d.ts +42 -0
  182. package/dist/report/locate.js +34 -0
  183. package/dist/styles/renderer.css +4580 -0
  184. package/licenses/LUCIDE-LICENSE.txt +43 -0
  185. package/licenses/QUICKJS-EMSCRIPTEN-LICENSE.txt +21 -0
  186. package/licenses/QUICKJS-VARIANT-LICENSE.txt +47 -0
  187. package/package.json +68 -3
@@ -0,0 +1,379 @@
1
+ /**
2
+ * Defensive Host-value validation helpers shared by Web answer renderers.
3
+ *
4
+ * Renderer layer: enforces ASCII-only hostnames (domain labels, IPv4, or bracketed IPv6; Unicode
5
+ * hostnames are normalized to ASCII punycode by the compiler) while allowing Unicode scalars in
6
+ * path, query, and fragment. Matches `ascii_https_url` in Python and the Compose/SwiftUI helpers,
7
+ * verified against `ui-foundation/surface/contracts/fixtures/renderer/host-values.json`.
8
+ */
9
+ export const MAX_HTTPS_URL_LENGTH = 2048;
10
+ export const MAX_STABLE_ID_LENGTH = 120;
11
+ const ASCII_LABEL_RE = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?$/;
12
+ const IPV4_OCTET_RE = /^(?:0|[1-9][0-9]{0,2})$/;
13
+ const ENDS_IN_NUMBER_RE = /^(?:[0-9]+|0[xX][0-9A-Fa-f]*)$/;
14
+ const PORT_RE = /^[0-9]{1,5}$/;
15
+ const HEX_GROUP_RE = /^[0-9A-Fa-f]{1,4}$/;
16
+ const FORMAT_CHAR_RE = /^\p{Cf}$/u;
17
+ const FORBIDDEN_HOST_CHARS = new Set([
18
+ " ", "\t", "\r", "\n", "\v", "\f", "|", "\\", "/", "?", "#", "@", "%",
19
+ "[", "]", "<", ">", "\"", "'", "`", "{", "}", "^", "!", "$", "&",
20
+ "(", ")", "*", "+", ",", ";", "=", "~", ":",
21
+ ]);
22
+ function isControl(cp) {
23
+ return cp <= 0x1f || (cp >= 0x7f && cp <= 0x9f);
24
+ }
25
+ function isDisallowedDisplayControl(cp) {
26
+ return isControl(cp) && cp !== 0x09 && cp !== 0x0a && cp !== 0x0d;
27
+ }
28
+ function isWhitespace(cp) {
29
+ return (cp === 0x09 ||
30
+ cp === 0x0a ||
31
+ cp === 0x0b ||
32
+ cp === 0x0c ||
33
+ cp === 0x0d ||
34
+ cp === 0x20 ||
35
+ cp === 0x85 ||
36
+ cp === 0xa0 ||
37
+ cp === 0x1680 ||
38
+ (cp >= 0x2000 && cp <= 0x200a) ||
39
+ cp === 0x2028 ||
40
+ cp === 0x2029 ||
41
+ cp === 0x202f ||
42
+ cp === 0x205f ||
43
+ cp === 0x3000 ||
44
+ cp === 0xfeff);
45
+ }
46
+ function isFormatChar(ch) {
47
+ return FORMAT_CHAR_RE.test(ch);
48
+ }
49
+ function scalarLength(value) {
50
+ let count = 0;
51
+ for (const _ of value)
52
+ count += 1;
53
+ return count;
54
+ }
55
+ function isValidIpv4Labels(labels) {
56
+ return (labels.length === 4 &&
57
+ labels.every((label) => IPV4_OCTET_RE.test(label) && Number(label) <= 255));
58
+ }
59
+ function isValidIpv6(host) {
60
+ if (!host || host.includes("%"))
61
+ return false;
62
+ const halves = host.split("::");
63
+ if (halves.length > 2)
64
+ return false;
65
+ const hasCompression = halves.length === 2;
66
+ const left = halves[0] ? halves[0].split(":") : [];
67
+ const right = hasCompression && halves[1] ? halves[1].split(":") : [];
68
+ let groups = 0;
69
+ const checkGroups = (items, allowEmbeddedIpv4) => {
70
+ for (let i = 0; i < items.length; i += 1) {
71
+ const part = items[i];
72
+ if (!part)
73
+ return false;
74
+ if (allowEmbeddedIpv4 && i === items.length - 1 && part.includes(".")) {
75
+ if (!isValidIpv4Labels(part.split(".")))
76
+ return false;
77
+ groups += 2;
78
+ }
79
+ else {
80
+ if (!HEX_GROUP_RE.test(part))
81
+ return false;
82
+ groups += 1;
83
+ }
84
+ }
85
+ return true;
86
+ };
87
+ if (!checkGroups(left, !hasCompression))
88
+ return false;
89
+ if (hasCompression && !checkGroups(right, true))
90
+ return false;
91
+ return hasCompression ? groups < 8 : groups === 8;
92
+ }
93
+ function isValidPort(port) {
94
+ return PORT_RE.test(port) && Number(port) <= 65535;
95
+ }
96
+ function isValidAsciiDomainOrIpv4(host) {
97
+ if (!host)
98
+ return false;
99
+ for (const ch of host) {
100
+ const cp = ch.codePointAt(0);
101
+ if (cp >= 0x80 || FORBIDDEN_HOST_CHARS.has(ch) || isControl(cp) || isWhitespace(cp))
102
+ return false;
103
+ }
104
+ const trimmed = host.endsWith(".") ? host.slice(0, -1) : host;
105
+ if (!trimmed || trimmed.length > 253)
106
+ return false;
107
+ const labels = trimmed.split(".");
108
+ if (labels.some((label) => label.length === 0))
109
+ return false;
110
+ if (ENDS_IN_NUMBER_RE.test(labels[labels.length - 1])) {
111
+ return isValidIpv4Labels(labels);
112
+ }
113
+ return labels.every((label) => ASCII_LABEL_RE.test(label));
114
+ }
115
+ const SCHEME_RE = /^[A-Za-z][A-Za-z0-9+.-]*:/;
116
+ const DATA_IMAGE_RE = /^data:image\/[A-Za-z0-9][A-Za-z0-9.+-]*(?:;[^,]*)?,.+/i;
117
+ const RASTER_LINK_DATA_RE = /^data:image\/(?:png|jpeg|gif|webp|avif|bmp)(?:;[^,]*)?,.+/i;
118
+ const EXECUTABLE_LINK_SCHEMES = new Set(["javascript", "vbscript"]);
119
+ const DEFAULT_OPENABLE_LINK_SCHEMES = new Set(["http", "https", "mailto", "tel"]);
120
+ function hasControlOrWhitespace(value) {
121
+ for (const ch of value) {
122
+ const cp = ch.codePointAt(0);
123
+ if (isControl(cp) || isWhitespace(cp))
124
+ return true;
125
+ }
126
+ return false;
127
+ }
128
+ function hasControl(value) {
129
+ for (const ch of value) {
130
+ if (isControl(ch.codePointAt(0)))
131
+ return true;
132
+ }
133
+ return false;
134
+ }
135
+ function validHttpOrHttpsUrl(value, maxLen) {
136
+ if (value.length === 0 || scalarLength(value) > maxLen)
137
+ return null;
138
+ const lower = value.toLowerCase();
139
+ const prefixLen = lower.startsWith("https://") ? 8 : lower.startsWith("http://") ? 7 : 0;
140
+ if (prefixLen === 0 || value.startsWith("/", prefixLen))
141
+ return null;
142
+ if (hasControlOrWhitespace(value))
143
+ return null;
144
+ const rest = value.slice(prefixLen);
145
+ let end = rest.length;
146
+ for (const sep of ["/", "?", "#"]) {
147
+ const idx = rest.indexOf(sep);
148
+ if (idx !== -1 && idx < end)
149
+ end = idx;
150
+ }
151
+ const authority = rest.slice(0, end);
152
+ if (!authority || authority.includes("@") || authority.includes("\\"))
153
+ return null;
154
+ if (authority.startsWith("[")) {
155
+ const close = authority.indexOf("]");
156
+ if (close === -1)
157
+ return null;
158
+ const ipv6Host = authority.slice(1, close);
159
+ if (!isValidIpv6(ipv6Host))
160
+ return null;
161
+ const tail = authority.slice(close + 1);
162
+ if (tail) {
163
+ if (!tail.startsWith(":") || !isValidPort(tail.slice(1)))
164
+ return null;
165
+ }
166
+ return value;
167
+ }
168
+ if (authority.includes("[") || authority.includes("]"))
169
+ return null;
170
+ const colon = authority.indexOf(":");
171
+ let host = authority;
172
+ if (colon !== -1) {
173
+ if (authority.indexOf(":", colon + 1) !== -1)
174
+ return null;
175
+ host = authority.slice(0, colon);
176
+ const port = authority.slice(colon + 1);
177
+ if (!isValidPort(port))
178
+ return null;
179
+ }
180
+ if (!isValidAsciiDomainOrIpv4(host))
181
+ return null;
182
+ return value;
183
+ }
184
+ export function isExecutableUrl(value) {
185
+ if (typeof value !== "string")
186
+ return false;
187
+ const stripped = value.replace(/[\u0000-\u0020]+/g, "").toLowerCase();
188
+ if (stripped.startsWith("javascript:") || stripped.startsWith("vbscript:"))
189
+ return true;
190
+ if (stripped.startsWith("data:")) {
191
+ return !DATA_IMAGE_RE.test(value.trim());
192
+ }
193
+ return false;
194
+ }
195
+ export function validImageUrl(value, maxLen = MAX_HTTPS_URL_LENGTH) {
196
+ if (typeof value !== "string")
197
+ return null;
198
+ const trimmed = value.trim();
199
+ if (!trimmed)
200
+ return null;
201
+ const lower = trimmed.toLowerCase();
202
+ if (lower.startsWith("http://") || lower.startsWith("https://")) {
203
+ return validHttpOrHttpsUrl(trimmed, maxLen);
204
+ }
205
+ if (lower.startsWith("data:image/")) {
206
+ if (hasControl(trimmed))
207
+ return null;
208
+ return DATA_IMAGE_RE.test(trimmed) ? trimmed : null;
209
+ }
210
+ return null;
211
+ }
212
+ /** Validates a Host-resolved `<img src>` value (e.g. from `images` or `imageLoader`). */
213
+ export function validResolvedImageSrc(value) {
214
+ if (typeof value !== "string")
215
+ return null;
216
+ const trimmed = value.trim();
217
+ if (!trimmed || hasControl(trimmed))
218
+ return null;
219
+ const lower = trimmed.toLowerCase();
220
+ if (lower.startsWith("javascript:") || lower.startsWith("vbscript:"))
221
+ return null;
222
+ if (lower.startsWith("data:")) {
223
+ return DATA_IMAGE_RE.test(trimmed) ? trimmed : null;
224
+ }
225
+ if (lower.startsWith("blob:"))
226
+ return trimmed;
227
+ if (lower.startsWith("http://") || lower.startsWith("https://")) {
228
+ return validHttpOrHttpsUrl(trimmed, MAX_HTTPS_URL_LENGTH);
229
+ }
230
+ if (!SCHEME_RE.test(trimmed))
231
+ return trimmed;
232
+ return null;
233
+ }
234
+ /**
235
+ * Validates any link URL that may be handed to the Host `actionHandler` or default opener.
236
+ * Execution-layer safety: rejects `javascript:`, `vbscript:`, and non-raster `data:` (including
237
+ * `data:text/html` and `data:image/svg+xml`) unconditionally. Safe raster `data:image/*` links
238
+ * are allowed through to the Host `actionHandler` (never opened by the built-in default opener).
239
+ */
240
+ export function validLinkUrl(value, maxLen = MAX_HTTPS_URL_LENGTH) {
241
+ if (typeof value !== "string")
242
+ return null;
243
+ const trimmed = value.trim();
244
+ if (!trimmed || hasControlOrWhitespace(trimmed))
245
+ return null;
246
+ if (!SCHEME_RE.test(trimmed))
247
+ return null;
248
+ const colon = trimmed.indexOf(":");
249
+ const scheme = trimmed.slice(0, colon).toLowerCase();
250
+ if (EXECUTABLE_LINK_SCHEMES.has(scheme))
251
+ return null;
252
+ if (scheme === "data") {
253
+ return RASTER_LINK_DATA_RE.test(trimmed) ? trimmed : null;
254
+ }
255
+ if (scalarLength(trimmed) > maxLen)
256
+ return null;
257
+ if (scheme === "http" || scheme === "https") {
258
+ return validHttpOrHttpsUrl(trimmed, maxLen);
259
+ }
260
+ const rest = trimmed.slice(colon + 1);
261
+ if (!rest)
262
+ return null;
263
+ return trimmed;
264
+ }
265
+ /** Whether the renderer's built-in default opener may open this URL when the Host declines. */
266
+ export function isDefaultOpenableLinkUrl(value, maxLen = MAX_HTTPS_URL_LENGTH) {
267
+ const safe = validLinkUrl(value, maxLen);
268
+ if (!safe)
269
+ return false;
270
+ const scheme = safe.slice(0, safe.indexOf(":")).toLowerCase();
271
+ return DEFAULT_OPENABLE_LINK_SCHEMES.has(scheme);
272
+ }
273
+ export function validHttpsUrl(value, maxLen = MAX_HTTPS_URL_LENGTH) {
274
+ if (typeof value !== "string" || value.length === 0 || scalarLength(value) > maxLen)
275
+ return null;
276
+ if (!value.startsWith("https://") || value.startsWith("https:///"))
277
+ return null;
278
+ for (const ch of value) {
279
+ const cp = ch.codePointAt(0);
280
+ if (isControl(cp) || isWhitespace(cp))
281
+ return null;
282
+ }
283
+ const rest = value.slice(8);
284
+ let end = rest.length;
285
+ for (const sep of ["/", "?", "#"]) {
286
+ const idx = rest.indexOf(sep);
287
+ if (idx !== -1 && idx < end)
288
+ end = idx;
289
+ }
290
+ const authority = rest.slice(0, end);
291
+ if (!authority || authority.includes("@") || authority.includes("\\"))
292
+ return null;
293
+ if (authority.startsWith("[")) {
294
+ const close = authority.indexOf("]");
295
+ if (close === -1)
296
+ return null;
297
+ const ipv6Host = authority.slice(1, close);
298
+ if (!isValidIpv6(ipv6Host))
299
+ return null;
300
+ const tail = authority.slice(close + 1);
301
+ if (tail) {
302
+ if (!tail.startsWith(":") || !isValidPort(tail.slice(1)))
303
+ return null;
304
+ }
305
+ return value;
306
+ }
307
+ if (authority.includes("[") || authority.includes("]"))
308
+ return null;
309
+ const colon = authority.indexOf(":");
310
+ let host = authority;
311
+ if (colon !== -1) {
312
+ if (authority.indexOf(":", colon + 1) !== -1)
313
+ return null;
314
+ host = authority.slice(0, colon);
315
+ const port = authority.slice(colon + 1);
316
+ if (!isValidPort(port))
317
+ return null;
318
+ }
319
+ if (!isValidAsciiDomainOrIpv4(host))
320
+ return null;
321
+ return value;
322
+ }
323
+ export function validDisplayText(value, maxLen) {
324
+ if (typeof value !== "string" || value.length === 0 || scalarLength(value) > maxLen)
325
+ return null;
326
+ let hasVisible = false;
327
+ for (const ch of value) {
328
+ const cp = ch.codePointAt(0);
329
+ if (isDisallowedDisplayControl(cp))
330
+ return null;
331
+ if (!isWhitespace(cp) && !isFormatChar(ch))
332
+ hasVisible = true;
333
+ }
334
+ return hasVisible ? value : null;
335
+ }
336
+ export function validStableId(value, maxLen = MAX_STABLE_ID_LENGTH) {
337
+ if (typeof value !== "string" || value.length === 0)
338
+ return null;
339
+ let count = 0;
340
+ let firstCp = -1;
341
+ let lastCp = -1;
342
+ let firstCh = "";
343
+ let lastCh = "";
344
+ for (const ch of value) {
345
+ const cp = ch.codePointAt(0);
346
+ if (isControl(cp))
347
+ return null;
348
+ if (firstCp === -1) {
349
+ firstCp = cp;
350
+ firstCh = ch;
351
+ }
352
+ lastCp = cp;
353
+ lastCh = ch;
354
+ count += 1;
355
+ if (count > maxLen)
356
+ return null;
357
+ }
358
+ if (firstCp === -1 ||
359
+ isWhitespace(firstCp) ||
360
+ isFormatChar(firstCh) ||
361
+ isWhitespace(lastCp) ||
362
+ isFormatChar(lastCh)) {
363
+ return null;
364
+ }
365
+ return value;
366
+ }
367
+ export function nfcKey(value) {
368
+ return value.normalize("NFC");
369
+ }
370
+ export function hasDuplicateNfcIds(ids) {
371
+ const seen = new Set();
372
+ for (const item of ids) {
373
+ const key = nfcKey(item);
374
+ if (seen.has(key))
375
+ return true;
376
+ seen.add(key);
377
+ }
378
+ return false;
379
+ }
@@ -0,0 +1,6 @@
1
+ import { type ComponentRenderer, type MapPlace, type MapPlaceGroup } from "../core/context.ts";
2
+ export declare const MAX_MAP_PLACES = 50;
3
+ export declare function isSafeMapUrl(value: unknown): value is string;
4
+ export declare function normalizeMapPlaces(raw: unknown): MapPlace[];
5
+ export declare function groupMapPlaces(places: readonly MapPlace[], chinese: boolean): MapPlaceGroup[];
6
+ export declare const renderMap: ComponentRenderer;
@@ -0,0 +1,213 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Component, useCallback, useEffect, useMemo, useState } from "react";
3
+ import { isRecord } from "../core/messages.js";
4
+ import { resolveValue } from "../core/binding.js";
5
+ import { isChineseLanguage, surfaceLanguage } from "../controls/fieldMessages.js";
6
+ import { surfaceIsStreaming } from "../controls/actionDispatch.js";
7
+ import { actionHandler, } from "../core/context.js";
8
+ import { withFallback } from "../core/component-support.js";
9
+ import { LucideIcon } from "../core/LucideIcon.js";
10
+ import { nfcKey, validDisplayText, validLinkUrl, validStableId } from "./hostValues.js";
11
+ export const MAX_MAP_PLACES = 50;
12
+ export function isSafeMapUrl(value) {
13
+ return validLinkUrl(value) !== null;
14
+ }
15
+ export function normalizeMapPlaces(raw) {
16
+ if (!Array.isArray(raw) || raw.length === 0 || raw.length > MAX_MAP_PLACES)
17
+ return [];
18
+ const seenNfcIds = new Set();
19
+ const places = [];
20
+ for (const item of raw) {
21
+ if (!isRecord(item))
22
+ return [];
23
+ const id = validStableId(item.id, 120);
24
+ if (id === null)
25
+ return [];
26
+ const nfcId = nfcKey(id);
27
+ if (seenNfcIds.has(nfcId))
28
+ return [];
29
+ const rawName = validDisplayText(item.name, 200);
30
+ if (rawName === null)
31
+ return [];
32
+ const name = rawName.trim();
33
+ const lat = item.lat;
34
+ const lng = item.lng;
35
+ if (typeof lat !== "number" || !Number.isFinite(lat) || lat < -90 || lat > 90)
36
+ return [];
37
+ if (typeof lng !== "number" || !Number.isFinite(lng) || lng < -180 || lng > 180)
38
+ return [];
39
+ seenNfcIds.add(nfcId);
40
+ const place = { id, name, lat, lng };
41
+ const rawAddress = validDisplayText(item.address, 300);
42
+ if (rawAddress !== null) {
43
+ place.address = rawAddress.trim();
44
+ }
45
+ if (typeof item.day === "number" && Number.isInteger(item.day) && item.day >= 1 && item.day <= 365) {
46
+ place.day = item.day;
47
+ }
48
+ else {
49
+ const rawDay = validDisplayText(item.day, 80);
50
+ if (rawDay !== null) {
51
+ place.day = rawDay.trim();
52
+ }
53
+ }
54
+ if (typeof item.order === "number" && Number.isInteger(item.order) && item.order >= 1 && item.order <= 1000) {
55
+ place.order = item.order;
56
+ }
57
+ const safeUrl = validLinkUrl(item.url);
58
+ if (safeUrl !== null) {
59
+ place.url = safeUrl;
60
+ }
61
+ places.push(place);
62
+ }
63
+ return places;
64
+ }
65
+ function sortByOrder(entries) {
66
+ return [...entries]
67
+ .sort((a, b) => {
68
+ const aHas = typeof a.place.order === "number";
69
+ const bHas = typeof b.place.order === "number";
70
+ if (aHas && bHas && a.place.order !== b.place.order)
71
+ return a.place.order - b.place.order;
72
+ if (aHas !== bHas)
73
+ return aHas ? -1 : 1;
74
+ return a.index - b.index;
75
+ })
76
+ .map((entry) => entry.place);
77
+ }
78
+ export function groupMapPlaces(places, chinese) {
79
+ const hasDay = places.some((place) => place.day !== undefined);
80
+ if (!hasDay) {
81
+ return [{
82
+ key: "all",
83
+ label: null,
84
+ places: sortByOrder(places.map((place, index) => ({ place, index }))),
85
+ }];
86
+ }
87
+ const buckets = new Map();
88
+ const ungrouped = [];
89
+ places.forEach((place, index) => {
90
+ if (place.day === undefined) {
91
+ ungrouped.push({ place, index });
92
+ return;
93
+ }
94
+ const key = typeof place.day === "number" ? `day:${place.day}` : `label:${place.day}`;
95
+ const label = typeof place.day === "number"
96
+ ? (chinese ? `第 ${place.day} 天` : `Day ${place.day}`)
97
+ : place.day;
98
+ const bucket = buckets.get(key);
99
+ if (bucket) {
100
+ bucket.items.push({ place, index });
101
+ }
102
+ else {
103
+ buckets.set(key, { label, items: [{ place, index }] });
104
+ }
105
+ });
106
+ const groups = [...buckets.entries()].map(([key, bucket]) => ({
107
+ key,
108
+ label: bucket.label,
109
+ places: sortByOrder(bucket.items),
110
+ }));
111
+ if (ungrouped.length > 0) {
112
+ groups.push({
113
+ key: "other",
114
+ label: chinese ? "其他地点" : "Other places",
115
+ places: sortByOrder(ungrouped),
116
+ });
117
+ }
118
+ return groups;
119
+ }
120
+ class MapAdapterBoundary extends Component {
121
+ state = { failed: false, resetKey: this.props.resetKey };
122
+ static getDerivedStateFromError() {
123
+ return { failed: true };
124
+ }
125
+ static getDerivedStateFromProps(props, state) {
126
+ if (props.resetKey !== state.resetKey) {
127
+ return { failed: false, resetKey: props.resetKey };
128
+ }
129
+ return null;
130
+ }
131
+ componentDidCatch(_error, _info) {
132
+ this.props.onError();
133
+ }
134
+ render() {
135
+ if (this.state.failed)
136
+ return null;
137
+ return this.props.children;
138
+ }
139
+ }
140
+ function MapAdapterHost({ renderMap, adapterProps, }) {
141
+ const rendered = renderMap(adapterProps);
142
+ if (rendered === null || rendered === undefined || rendered === false)
143
+ return null;
144
+ return (_jsx("div", { className: "lp-map-adapter", "data-map-adapter": "mounted", children: rendered }));
145
+ }
146
+ function MapBlock({ component, context, places, }) {
147
+ const zh = isChineseLanguage(surfaceLanguage(context.state?.rendererHints));
148
+ const isStreaming = surfaceIsStreaming(context.state) || component.streaming === true;
149
+ const [selectedPlaceId, setSelectedPlaceId] = useState(null);
150
+ const [adapterFailed, setAdapterFailed] = useState(false);
151
+ const title = typeof component.title === "string" && component.title.trim() ? component.title.trim() : undefined;
152
+ const caption = typeof component.caption === "string" && component.caption.trim() ? component.caption.trim() : undefined;
153
+ const connectRoute = component.connectRoute === true;
154
+ const groups = useMemo(() => groupMapPlaces(places, zh), [places, zh]);
155
+ useEffect(() => {
156
+ if (selectedPlaceId && !places.some((place) => place.id === selectedPlaceId)) {
157
+ setSelectedPlaceId(null);
158
+ }
159
+ }, [places, selectedPlaceId]);
160
+ useEffect(() => {
161
+ setAdapterFailed(false);
162
+ }, [component.id, context.renderMap]);
163
+ const handleFail = useCallback((_reason) => {
164
+ setAdapterFailed(true);
165
+ }, []);
166
+ const openPlaceUrl = useCallback((place) => {
167
+ if (!place.url || isStreaming)
168
+ return;
169
+ const handler = actionHandler({ functionCall: { call: "openUrl", args: { url: place.url } } }, component, context, {}, `places/${place.id}/open`);
170
+ if (handler && !handler.disabled)
171
+ handler();
172
+ }, [component, context, isStreaming]);
173
+ const shouldMountAdapter = Boolean(context.renderMap) && !isStreaming && !adapterFailed;
174
+ const mapMode = shouldMountAdapter
175
+ ? "adapter"
176
+ : isStreaming
177
+ ? "streaming"
178
+ : adapterFailed
179
+ ? "fallback"
180
+ : "list";
181
+ const adapterProps = {
182
+ componentId: component.id,
183
+ title,
184
+ places,
185
+ groups,
186
+ connectRoute,
187
+ selectedPlaceId,
188
+ onSelectPlace: setSelectedPlaceId,
189
+ onOpenUrl: openPlaceUrl,
190
+ onFail: handleFail,
191
+ };
192
+ return (_jsxs("section", { className: "lp-map", "data-a2ui-component": component.component, "data-map-mode": mapMode, "data-streaming": isStreaming ? "true" : undefined, "aria-label": title ?? (zh ? "地点列表" : "Places"), children: [title ? _jsx("h3", { className: "lp-map-title", children: title }) : null, shouldMountAdapter && context.renderMap ? (_jsx(MapAdapterBoundary, { resetKey: `${component.id}:${places.map((p) => p.id).join(",")}`, onError: handleFail, children: _jsx(MapAdapterHost, { renderMap: context.renderMap, adapterProps: adapterProps }) })) : null, _jsx("div", { className: "lp-map-groups", children: groups.map((group) => (_jsxs("div", { className: "lp-map-group", "data-map-group": group.key, children: [group.label ? _jsx("p", { className: "lp-map-day", children: group.label }) : null, _jsx("ol", { className: "lp-map-list", children: group.places.map((place, index) => {
193
+ const selected = selectedPlaceId === place.id;
194
+ const badgeNumber = place.order ?? index + 1;
195
+ const openHandler = place.url && !isStreaming
196
+ ? actionHandler({ functionCall: { call: "openUrl", args: { url: place.url } } }, component, context, {}, `places/${place.id}/open`)
197
+ : undefined;
198
+ const openDisabled = !openHandler || openHandler.disabled === true;
199
+ return (_jsxs("li", { className: "lp-map-item", "data-place-id": place.id, "data-selected": selected ? "true" : undefined, children: [_jsxs("button", { type: "button", className: "lp-map-row", "aria-pressed": selected, disabled: isStreaming, onClick: () => {
200
+ if (isStreaming)
201
+ return;
202
+ setSelectedPlaceId((current) => (current === place.id ? null : place.id));
203
+ }, children: [_jsx("span", { className: "lp-map-index", "aria-hidden": "true", children: badgeNumber }), _jsxs("span", { className: "lp-map-copy", children: [_jsx("span", { className: "lp-map-name", children: place.name }), place.address ? _jsx("span", { className: "lp-map-address", children: place.address }) : null] })] }), place.url ? (_jsxs("button", { type: "button", className: "lp-map-open", "data-action-key": `places/${place.id}/open`, disabled: openDisabled, "aria-label": `${zh ? "打开" : "Open"} ${place.name}`, onClick: () => {
204
+ if (!openDisabled)
205
+ openHandler?.();
206
+ }, children: [_jsx("span", { children: zh ? "打开" : "Open" }), _jsx(LucideIcon, { name: "external-link", size: "sm", decorative: true })] })) : null] }, place.id));
207
+ }) })] }, group.key))) }), caption ? _jsx("p", { className: "lp-map-caption", children: caption }) : null] }));
208
+ }
209
+ export const renderMap = (component, context) => {
210
+ const resolved = resolveValue(component.places, context.binding, context.valueFunctions);
211
+ const places = normalizeMapPlaces(resolved);
212
+ return withFallback(component, places.length > 0, (_jsx(MapBlock, { component: component, context: context, places: places })));
213
+ };
@@ -0,0 +1,57 @@
1
+ import { type ReactElement } from "react";
2
+ export type MathGeometry = {
3
+ width: number;
4
+ height: number;
5
+ depth: number;
6
+ paths: string;
7
+ glyphs?: {
8
+ path: string;
9
+ };
10
+ };
11
+ type Drawn = {
12
+ width: number;
13
+ height: number;
14
+ depth: number;
15
+ elements: {
16
+ d: string;
17
+ transform?: string;
18
+ }[];
19
+ };
20
+ /** Room kept above and below an inline formula for ink past its box (CJK glyphs, overshoot). */
21
+ export declare const MATH_INK_ALLOWANCE_EM = 0.12;
22
+ /** Formulas are drawn this much larger than the text around them, as KaTeX does. */
23
+ export declare const MATH_SCALE = 1.21;
24
+ /** The path data of one `f` element, or null when it is not exactly M/L/C/Z with numbers. */
25
+ export declare function fillPath(element: string): string | null;
26
+ /** The formula ready to draw, or null when the geometry or a glyph it names is missing or malformed. */
27
+ export declare function parseMathGeometry(value: unknown, dataModel: unknown): Drawn | null;
28
+ /** Room kept left and right for ink past the box (italic overhang), inside the scroll region. */
29
+ export declare const MATH_SIDE_ALLOWANCE_EM = 0.15;
30
+ /** Wider than its region: the formula scrolls, and a keyboard can reach it to scroll. */
31
+ export declare function syncScrollable(node: {
32
+ scrollWidth: number;
33
+ clientWidth: number;
34
+ tabIndex: number;
35
+ removeAttribute(name: string): void;
36
+ }): void;
37
+ /**
38
+ * A formula that is (part of) a link is underlined like the link's words, so the link does not rely
39
+ * on colour alone: a bar as thick as a text underline (1/15 of the text size), below the formula's
40
+ * depth or the text's descent, whichever is lower, with a small gap. All in the formula's em.
41
+ */
42
+ export declare const MATH_LINK_UNDERLINE_DESCENT_EM: number;
43
+ export declare const MATH_LINK_UNDERLINE_GAP_EM = 0.06;
44
+ export declare const MATH_LINK_UNDERLINE_THICKNESS_EM: number;
45
+ /** Where a linked formula's underline starts below its baseline, in its em. */
46
+ export declare function mathLinkUnderline(depth: number): number;
47
+ /**
48
+ * The formula as inline SVG with the TeX as its accessible name, or null to show the TeX instead.
49
+ * The formula is its own box, never wider than the line it sits in: a wider one scrolls sideways
50
+ * inside that box (focusable then, so a keyboard can scroll it) instead of widening the page.
51
+ * The box keeps `MATH_INK_ALLOWANCE_EM` free above and below, so an inline formula never runs into
52
+ * the next line, and `MATH_SIDE_ALLOWANCE_EM` at the sides inside negative margins, so the text
53
+ * around it is spaced as before. Its bottom edge sits that far below the depth, which puts the
54
+ * formula's baseline on the text's. `display` centres a block formula on its own line.
55
+ */
56
+ export declare function mathElement(tex: string, geometry: unknown, dataModel: unknown, display: boolean, link?: boolean): ReactElement | null;
57
+ export {};