@getodk/xforms-engine 0.6.0 → 0.7.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 (296) hide show
  1. package/dist/client/BaseNode.d.ts +4 -4
  2. package/dist/client/BaseValueNode.d.ts +7 -3
  3. package/dist/client/RootNode.d.ts +20 -16
  4. package/dist/client/constants.d.ts +11 -12
  5. package/dist/client/form/CreateFormInstance.d.ts +14 -0
  6. package/dist/client/form/EditFormInstance.d.ts +62 -0
  7. package/dist/client/form/FormInstance.d.ts +104 -0
  8. package/dist/client/form/FormInstanceConfig.d.ts +17 -0
  9. package/dist/client/form/FormResource.d.ts +1 -0
  10. package/dist/client/form/LoadForm.d.ts +79 -0
  11. package/dist/client/form/LoadFormResult.d.ts +67 -0
  12. package/dist/client/form/RestoreFormInstance.d.ts +8 -0
  13. package/dist/client/index.d.ts +37 -11
  14. package/dist/client/node-types.d.ts +2 -2
  15. package/dist/client/repeat/BaseRepeatRangeNode.d.ts +3 -4
  16. package/dist/client/repeat/RepeatInstanceNode.d.ts +4 -5
  17. package/dist/client/repeat/RepeatRangeControlledNode.d.ts +2 -2
  18. package/dist/client/repeat/RepeatRangeUncontrolledNode.d.ts +2 -2
  19. package/dist/client/resources.d.ts +1 -1
  20. package/dist/client/serialization/InstanceData.d.ts +12 -0
  21. package/dist/client/serialization/InstanceFile.d.ts +6 -0
  22. package/dist/client/serialization/InstancePayload.d.ts +93 -0
  23. package/dist/client/serialization/InstancePayloadOptions.d.ts +23 -0
  24. package/dist/client/serialization/InstanceState.d.ts +12 -0
  25. package/dist/client/submission/{SubmissionDefinition.d.ts → SubmissionMeta.d.ts} +1 -1
  26. package/dist/entrypoints/FormInstance.d.ts +20 -0
  27. package/dist/entrypoints/FormResult/BaseFormResult.d.ts +22 -0
  28. package/dist/entrypoints/FormResult/BaseInstantiableFormResult.d.ts +25 -0
  29. package/dist/entrypoints/FormResult/FormFailureResult.d.ts +17 -0
  30. package/dist/entrypoints/FormResult/FormSuccessResult.d.ts +15 -0
  31. package/dist/entrypoints/FormResult/FormWarningResult.d.ts +15 -0
  32. package/dist/entrypoints/createInstance.d.ts +9 -0
  33. package/dist/entrypoints/editInstance.d.ts +9 -0
  34. package/dist/entrypoints/index.d.ts +4 -0
  35. package/dist/entrypoints/loadForm.d.ts +4 -0
  36. package/dist/entrypoints/restoreInstance.d.ts +9 -0
  37. package/dist/error/LoadFormFailureError.d.ts +9 -0
  38. package/dist/error/MalformedInstanceDataError.d.ts +3 -0
  39. package/dist/error/TemplatedNodeAttributeSerializationError.d.ts +22 -0
  40. package/dist/index.d.ts +2 -33
  41. package/dist/index.js +1915 -1419
  42. package/dist/index.js.map +1 -1
  43. package/dist/instance/Group.d.ts +6 -5
  44. package/dist/instance/InputControl.d.ts +5 -4
  45. package/dist/instance/ModelValue.d.ts +5 -4
  46. package/dist/instance/Note.d.ts +4 -3
  47. package/dist/instance/PrimaryInstance.d.ts +35 -8
  48. package/dist/instance/RangeControl.d.ts +5 -4
  49. package/dist/instance/RankControl.d.ts +6 -5
  50. package/dist/instance/Root.d.ts +7 -9
  51. package/dist/instance/SelectControl.d.ts +4 -3
  52. package/dist/instance/Subtree.d.ts +6 -5
  53. package/dist/instance/TriggerControl.d.ts +4 -3
  54. package/dist/instance/abstract/DescendantNode.d.ts +10 -1
  55. package/dist/instance/abstract/InstanceNode.d.ts +7 -4
  56. package/dist/instance/abstract/ValueNode.d.ts +7 -5
  57. package/dist/instance/children/DescendantNodeInitOptions.d.ts +32 -0
  58. package/dist/instance/{children.d.ts → children/buildChildren.d.ts} +1 -1
  59. package/dist/instance/children/childrenInitOptions.d.ts +9 -0
  60. package/dist/instance/children/normalizeChildInitOptions.d.ts +2 -0
  61. package/dist/instance/input/InitialInstanceState.d.ts +13 -0
  62. package/dist/instance/input/InstanceAttachmentMap.d.ts +19 -0
  63. package/dist/instance/internal-api/InstanceConfig.d.ts +2 -9
  64. package/dist/instance/internal-api/InstanceValueContext.d.ts +8 -1
  65. package/dist/instance/internal-api/serialization/ClientReactiveSerializableInstance.d.ts +14 -0
  66. package/dist/instance/internal-api/serialization/ClientReactiveSerializableLeafNode.d.ts +32 -0
  67. package/dist/instance/internal-api/serialization/ClientReactiveSerializableParentNode.d.ts +18 -0
  68. package/dist/instance/internal-api/serialization/ClientReactiveSerializableTemplatedNode.d.ts +13 -0
  69. package/dist/instance/internal-api/serialization/ClientReactiveSerializableValueNode.d.ts +21 -0
  70. package/dist/instance/repeat/BaseRepeatRange.d.ts +10 -8
  71. package/dist/instance/repeat/RepeatInstance.d.ts +10 -7
  72. package/dist/instance/repeat/RepeatRangeControlled.d.ts +27 -3
  73. package/dist/instance/repeat/RepeatRangeUncontrolled.d.ts +4 -3
  74. package/dist/instance/resource.d.ts +5 -1
  75. package/dist/instance/unsupported/UploadControl.d.ts +32 -4
  76. package/dist/integration/xpath/EngineXPathEvaluator.d.ts +2 -2
  77. package/dist/integration/xpath/adapter/kind.d.ts +4 -10
  78. package/dist/integration/xpath/static-dom/StaticAttribute.d.ts +4 -5
  79. package/dist/integration/xpath/static-dom/StaticDocument.d.ts +13 -10
  80. package/dist/integration/xpath/static-dom/StaticElement.d.ts +21 -22
  81. package/dist/integration/xpath/static-dom/StaticNode.d.ts +1 -1
  82. package/dist/integration/xpath/static-dom/StaticParentNode.d.ts +13 -0
  83. package/dist/integration/xpath/static-dom/staticNodeName.d.ts +3 -0
  84. package/dist/lib/client-reactivity/instance-state/createNodeRangeInstanceState.d.ts +4 -0
  85. package/dist/lib/client-reactivity/instance-state/createParentNodeInstanceState.d.ts +4 -0
  86. package/dist/lib/client-reactivity/instance-state/createPrimaryInstanceState.d.ts +3 -0
  87. package/dist/lib/client-reactivity/instance-state/createRootInstanceState.d.ts +3 -0
  88. package/dist/lib/client-reactivity/instance-state/createTemplatedNodeInstanceState.d.ts +6 -0
  89. package/dist/lib/client-reactivity/instance-state/createValueNodeInstanceState.d.ts +3 -0
  90. package/dist/lib/client-reactivity/instance-state/prepareInstancePayload.d.ts +8 -0
  91. package/dist/lib/codecs/TempUnsupportedControlCodec.d.ts +7 -0
  92. package/dist/lib/names/UnprefixedXFormsName.d.ts +4 -0
  93. package/dist/lib/reactivity/createInstanceValueState.d.ts +2 -27
  94. package/dist/parse/XFormDOM.d.ts +1 -1
  95. package/dist/parse/model/BindDefinition.d.ts +1 -1
  96. package/dist/parse/model/ItextTranslationsDefinition.d.ts +18 -0
  97. package/dist/parse/model/LeafNodeDefinition.d.ts +4 -5
  98. package/dist/parse/model/ModelBindMap.d.ts +1 -1
  99. package/dist/parse/model/ModelDefinition.d.ts +9 -2
  100. package/dist/parse/model/NodeDefinition.d.ts +18 -30
  101. package/dist/parse/model/NoteNodeDefinition.d.ts +3 -2
  102. package/dist/parse/model/RangeNodeDefinition.d.ts +2 -1
  103. package/dist/parse/model/RepeatDefinition.d.ts +62 -0
  104. package/dist/parse/model/RootAttributeDefinition.d.ts +1 -4
  105. package/dist/parse/model/RootAttributeMap.d.ts +2 -1
  106. package/dist/parse/model/RootDefinition.d.ts +7 -8
  107. package/dist/parse/model/SecondaryInstance/SecondaryInstancesDefinition.d.ts +15 -2
  108. package/dist/parse/model/SecondaryInstance/assertSecondaryInstanceDefinition.d.ts +5 -0
  109. package/dist/parse/model/SecondaryInstance/defineSecondaryInstance.d.ts +5 -0
  110. package/dist/parse/model/SecondaryInstance/sources/BlankSecondaryInstanceSource.d.ts +1 -7
  111. package/dist/parse/model/SecondaryInstance/sources/CSVExternalSecondaryInstance.d.ts +1 -1
  112. package/dist/parse/model/SecondaryInstance/sources/GeoJSONExternalSecondaryInstance.d.ts +1 -1
  113. package/dist/parse/model/SecondaryInstance/sources/InternalSecondaryInstanceSource.d.ts +1 -1
  114. package/dist/parse/model/SecondaryInstance/sources/SecondaryInstanceSource.d.ts +1 -1
  115. package/dist/parse/model/SecondaryInstance/sources/XMLExternalSecondaryInstanceSource.d.ts +1 -1
  116. package/dist/parse/model/{FormSubmissionDefinition.d.ts → SubmissionDefinition.d.ts} +2 -2
  117. package/dist/parse/model/SubtreeDefinition.d.ts +4 -5
  118. package/dist/parse/model/nodeDefinitionMap.d.ts +5 -0
  119. package/dist/parse/shared/parseInstanceXML.d.ts +21 -0
  120. package/dist/parse/shared/parseStaticDocumentFromDOMSubtree.d.ts +4 -21
  121. package/dist/solid.js +1709 -1213
  122. package/dist/solid.js.map +1 -1
  123. package/package.json +1 -1
  124. package/src/client/BaseNode.ts +4 -4
  125. package/src/client/BaseValueNode.ts +7 -3
  126. package/src/client/RootNode.ts +27 -19
  127. package/src/client/constants.ts +12 -14
  128. package/src/client/form/CreateFormInstance.ts +19 -0
  129. package/src/client/form/EditFormInstance.ts +93 -0
  130. package/src/client/form/FormInstance.ts +114 -0
  131. package/src/client/form/FormInstanceConfig.ts +18 -0
  132. package/src/client/form/FormResource.ts +1 -0
  133. package/src/client/form/LoadForm.ts +92 -0
  134. package/src/client/form/LoadFormResult.ts +103 -0
  135. package/src/client/form/RestoreFormInstance.ts +14 -0
  136. package/src/client/index.ts +47 -29
  137. package/src/client/node-types.ts +2 -4
  138. package/src/client/repeat/BaseRepeatRangeNode.ts +3 -4
  139. package/src/client/repeat/RepeatInstanceNode.ts +4 -8
  140. package/src/client/repeat/RepeatRangeControlledNode.ts +2 -2
  141. package/src/client/repeat/RepeatRangeUncontrolledNode.ts +2 -2
  142. package/src/client/resources.ts +2 -2
  143. package/src/client/serialization/InstanceData.ts +16 -0
  144. package/src/client/serialization/InstanceFile.ts +9 -0
  145. package/src/client/serialization/InstancePayload.ts +126 -0
  146. package/src/client/serialization/InstancePayloadOptions.ts +28 -0
  147. package/src/client/serialization/InstanceState.ts +14 -0
  148. package/src/client/submission/{SubmissionDefinition.ts → SubmissionMeta.ts} +1 -1
  149. package/src/entrypoints/FormInstance.ts +55 -0
  150. package/src/entrypoints/FormResult/BaseFormResult.ts +40 -0
  151. package/src/entrypoints/FormResult/BaseInstantiableFormResult.ts +109 -0
  152. package/src/entrypoints/FormResult/FormFailureResult.ts +44 -0
  153. package/src/entrypoints/FormResult/FormSuccessResult.ts +25 -0
  154. package/src/entrypoints/FormResult/FormWarningResult.ts +25 -0
  155. package/src/entrypoints/createInstance.ts +23 -0
  156. package/src/entrypoints/editInstance.ts +24 -0
  157. package/src/entrypoints/index.ts +4 -0
  158. package/src/entrypoints/loadForm.ts +154 -0
  159. package/src/entrypoints/restoreInstance.ts +27 -0
  160. package/src/error/LoadFormFailureError.ts +114 -0
  161. package/src/error/MalformedInstanceDataError.ts +3 -0
  162. package/src/error/TemplatedNodeAttributeSerializationError.ts +24 -0
  163. package/src/index.ts +2 -46
  164. package/src/instance/Group.ts +16 -15
  165. package/src/instance/InputControl.ts +17 -11
  166. package/src/instance/ModelValue.ts +17 -11
  167. package/src/instance/Note.ts +10 -9
  168. package/src/instance/PrimaryInstance.ts +69 -31
  169. package/src/instance/RangeControl.ts +17 -11
  170. package/src/instance/RankControl.ts +28 -19
  171. package/src/instance/Root.ts +20 -31
  172. package/src/instance/SelectControl.ts +21 -12
  173. package/src/instance/Subtree.ts +16 -15
  174. package/src/instance/TriggerControl.ts +21 -12
  175. package/src/instance/abstract/DescendantNode.ts +12 -2
  176. package/src/instance/abstract/InstanceNode.ts +9 -5
  177. package/src/instance/abstract/ValueNode.ts +11 -13
  178. package/src/instance/children/DescendantNodeInitOptions.ts +35 -0
  179. package/src/instance/{children.ts → children/buildChildren.ts} +55 -48
  180. package/src/instance/children/childrenInitOptions.ts +117 -0
  181. package/src/instance/children/normalizeChildInitOptions.ts +332 -0
  182. package/src/instance/input/InitialInstanceState.ts +108 -0
  183. package/src/instance/input/InstanceAttachmentMap.ts +142 -0
  184. package/src/instance/internal-api/InstanceConfig.ts +3 -10
  185. package/src/instance/internal-api/InstanceValueContext.ts +9 -1
  186. package/src/instance/internal-api/serialization/ClientReactiveSerializableInstance.ts +20 -0
  187. package/src/instance/internal-api/serialization/ClientReactiveSerializableLeafNode.ts +43 -0
  188. package/src/instance/internal-api/serialization/ClientReactiveSerializableParentNode.ts +26 -0
  189. package/src/instance/internal-api/serialization/ClientReactiveSerializableTemplatedNode.ts +24 -0
  190. package/src/instance/internal-api/serialization/ClientReactiveSerializableValueNode.ts +28 -0
  191. package/src/instance/repeat/BaseRepeatRange.ts +19 -24
  192. package/src/instance/repeat/RepeatInstance.ts +26 -19
  193. package/src/instance/repeat/RepeatRangeControlled.ts +90 -17
  194. package/src/instance/repeat/RepeatRangeUncontrolled.ts +10 -9
  195. package/src/instance/resource.ts +14 -1
  196. package/src/instance/unsupported/UploadControl.ts +116 -5
  197. package/src/integration/xpath/EngineXPathEvaluator.ts +2 -2
  198. package/src/integration/xpath/adapter/kind.ts +1 -28
  199. package/src/integration/xpath/adapter/traversal.ts +2 -2
  200. package/src/integration/xpath/static-dom/StaticAttribute.ts +6 -5
  201. package/src/integration/xpath/static-dom/StaticDocument.ts +17 -16
  202. package/src/integration/xpath/static-dom/StaticElement.ts +196 -50
  203. package/src/integration/xpath/static-dom/StaticNode.ts +1 -1
  204. package/src/integration/xpath/static-dom/StaticParentNode.ts +22 -0
  205. package/src/integration/xpath/static-dom/staticNodeName.ts +20 -0
  206. package/src/lib/client-reactivity/instance-state/createNodeRangeInstanceState.ts +17 -0
  207. package/src/lib/client-reactivity/instance-state/createParentNodeInstanceState.ts +22 -0
  208. package/src/lib/client-reactivity/instance-state/createPrimaryInstanceState.ts +12 -0
  209. package/src/lib/client-reactivity/{submission/createRootSubmissionState.ts → instance-state/createRootInstanceState.ts} +4 -4
  210. package/src/lib/client-reactivity/instance-state/createTemplatedNodeInstanceState.ts +31 -0
  211. package/src/lib/client-reactivity/instance-state/createValueNodeInstanceState.ts +21 -0
  212. package/src/lib/client-reactivity/instance-state/prepareInstancePayload.ts +173 -0
  213. package/src/lib/codecs/TempUnsupportedControlCodec.ts +32 -0
  214. package/src/lib/names/UnprefixedXFormsName.ts +12 -0
  215. package/src/lib/reactivity/createInstanceValueState.ts +27 -51
  216. package/src/parse/model/ItextTranslationsDefinition.ts +79 -0
  217. package/src/parse/model/LeafNodeDefinition.ts +3 -5
  218. package/src/parse/model/ModelDefinition.ts +36 -3
  219. package/src/parse/model/NodeDefinition.ts +19 -45
  220. package/src/parse/model/NoteNodeDefinition.ts +4 -3
  221. package/src/parse/model/RangeNodeDefinition.ts +3 -2
  222. package/src/parse/model/RepeatDefinition.ts +382 -0
  223. package/src/parse/model/RootAttributeDefinition.ts +6 -7
  224. package/src/parse/model/RootAttributeMap.ts +15 -10
  225. package/src/parse/model/RootDefinition.ts +17 -19
  226. package/src/parse/model/SecondaryInstance/SecondaryInstancesDefinition.ts +23 -2
  227. package/src/parse/model/SecondaryInstance/assertSecondaryInstanceDefinition.ts +14 -0
  228. package/src/parse/model/SecondaryInstance/defineSecondaryInstance.ts +32 -0
  229. package/src/parse/model/SecondaryInstance/sources/BlankSecondaryInstanceSource.ts +3 -24
  230. package/src/parse/model/SecondaryInstance/sources/CSVExternalSecondaryInstance.ts +33 -86
  231. package/src/parse/model/SecondaryInstance/sources/GeoJSONExternalSecondaryInstance.ts +64 -137
  232. package/src/parse/model/SecondaryInstance/sources/InternalSecondaryInstanceSource.ts +9 -7
  233. package/src/parse/model/SecondaryInstance/sources/SecondaryInstanceSource.ts +1 -1
  234. package/src/parse/model/SecondaryInstance/sources/XMLExternalSecondaryInstanceSource.ts +7 -7
  235. package/src/parse/model/{FormSubmissionDefinition.ts → SubmissionDefinition.ts} +2 -2
  236. package/src/parse/model/SubtreeDefinition.ts +4 -5
  237. package/src/parse/model/nodeDefinitionMap.ts +34 -0
  238. package/src/parse/shared/parseInstanceXML.ts +79 -0
  239. package/src/parse/shared/parseStaticDocumentFromDOMSubtree.ts +45 -130
  240. package/dist/client/EngineConfig.d.ts +0 -79
  241. package/dist/client/submission/SubmissionData.d.ts +0 -7
  242. package/dist/client/submission/SubmissionInstanceFile.d.ts +0 -6
  243. package/dist/client/submission/SubmissionOptions.d.ts +0 -23
  244. package/dist/client/submission/SubmissionResult.d.ts +0 -91
  245. package/dist/client/submission/SubmissionState.d.ts +0 -12
  246. package/dist/instance/abstract/UnsupportedControl.d.ts +0 -54
  247. package/dist/instance/index.d.ts +0 -8
  248. package/dist/instance/internal-api/ValueContext.d.ts +0 -23
  249. package/dist/instance/internal-api/submission/ClientReactiveSubmittableInstance.d.ts +0 -14
  250. package/dist/instance/internal-api/submission/ClientReactiveSubmittableLeafNode.d.ts +0 -32
  251. package/dist/instance/internal-api/submission/ClientReactiveSubmittableParentNode.d.ts +0 -19
  252. package/dist/instance/internal-api/submission/ClientReactiveSubmittableValueNode.d.ts +0 -18
  253. package/dist/lib/client-reactivity/submission/createInstanceSubmissionState.d.ts +0 -3
  254. package/dist/lib/client-reactivity/submission/createLeafNodeSubmissionState.d.ts +0 -3
  255. package/dist/lib/client-reactivity/submission/createNodeRangeSubmissionState.d.ts +0 -4
  256. package/dist/lib/client-reactivity/submission/createParentNodeSubmissionState.d.ts +0 -4
  257. package/dist/lib/client-reactivity/submission/createRootSubmissionState.d.ts +0 -3
  258. package/dist/lib/client-reactivity/submission/createValueNodeSubmissionState.d.ts +0 -3
  259. package/dist/lib/client-reactivity/submission/prepareSubmission.d.ts +0 -8
  260. package/dist/lib/reactivity/createValueState.d.ts +0 -40
  261. package/dist/parse/model/ItextTranslation/ItextTranslationDefinition.d.ts +0 -4
  262. package/dist/parse/model/ItextTranslation/ItextTranslationRootDefinition.d.ts +0 -9
  263. package/dist/parse/model/ItextTranslation/ItextTranslationsDefinition.d.ts +0 -8
  264. package/dist/parse/model/RepeatInstanceDefinition.d.ts +0 -17
  265. package/dist/parse/model/RepeatRangeDefinition.d.ts +0 -32
  266. package/dist/parse/model/RepeatTemplateDefinition.d.ts +0 -31
  267. package/dist/parse/model/SecondaryInstance/SecondaryInstanceDefinition.d.ts +0 -4
  268. package/dist/parse/model/SecondaryInstance/SecondaryInstanceRootDefinition.d.ts +0 -7
  269. package/src/client/EngineConfig.ts +0 -84
  270. package/src/client/submission/SubmissionData.ts +0 -12
  271. package/src/client/submission/SubmissionInstanceFile.ts +0 -9
  272. package/src/client/submission/SubmissionOptions.ts +0 -28
  273. package/src/client/submission/SubmissionResult.ts +0 -124
  274. package/src/client/submission/SubmissionState.ts +0 -14
  275. package/src/instance/abstract/UnsupportedControl.ts +0 -175
  276. package/src/instance/index.ts +0 -55
  277. package/src/instance/internal-api/ValueContext.ts +0 -28
  278. package/src/instance/internal-api/submission/ClientReactiveSubmittableInstance.ts +0 -20
  279. package/src/instance/internal-api/submission/ClientReactiveSubmittableLeafNode.ts +0 -43
  280. package/src/instance/internal-api/submission/ClientReactiveSubmittableParentNode.ts +0 -26
  281. package/src/instance/internal-api/submission/ClientReactiveSubmittableValueNode.ts +0 -24
  282. package/src/lib/client-reactivity/submission/createInstanceSubmissionState.ts +0 -12
  283. package/src/lib/client-reactivity/submission/createLeafNodeSubmissionState.ts +0 -20
  284. package/src/lib/client-reactivity/submission/createNodeRangeSubmissionState.ts +0 -17
  285. package/src/lib/client-reactivity/submission/createParentNodeSubmissionState.ts +0 -22
  286. package/src/lib/client-reactivity/submission/createValueNodeSubmissionState.ts +0 -21
  287. package/src/lib/client-reactivity/submission/prepareSubmission.ts +0 -172
  288. package/src/lib/reactivity/createValueState.ts +0 -200
  289. package/src/parse/model/ItextTranslation/ItextTranslationDefinition.ts +0 -4
  290. package/src/parse/model/ItextTranslation/ItextTranslationRootDefinition.ts +0 -42
  291. package/src/parse/model/ItextTranslation/ItextTranslationsDefinition.ts +0 -31
  292. package/src/parse/model/RepeatInstanceDefinition.ts +0 -38
  293. package/src/parse/model/RepeatRangeDefinition.ts +0 -98
  294. package/src/parse/model/RepeatTemplateDefinition.ts +0 -149
  295. package/src/parse/model/SecondaryInstance/SecondaryInstanceDefinition.ts +0 -4
  296. package/src/parse/model/SecondaryInstance/SecondaryInstanceRootDefinition.ts +0 -12
package/dist/index.js CHANGED
@@ -1,4 +1,43 @@
1
- const identity$1 = (value) => value;
1
+ const MISSING_RESOURCE_BEHAVIOR = {
2
+ /**
3
+ * When this behavior is configured, {@link LoadForm | loading a form} which
4
+ * references any **missing** resources will fail, producing an error to the
5
+ * calling client.
6
+ *
7
+ * @see {@link MissingResourceBehavior}
8
+ */
9
+ ERROR: "ERROR",
10
+ /**
11
+ * When this behavior is configured, {@link LoadForm | loading a form} which
12
+ * references any **missing** resources will succeed (producing a warning).
13
+ *
14
+ * Such missing resources will be parsed as if they are blank, as appropriate
15
+ * for the resource's XForm semantic usage and/or format.
16
+ *
17
+ * @see {@link MissingResourceBehavior}
18
+ */
19
+ BLANK: "BLANK",
20
+ /**
21
+ * @see {@link MISSING_RESOURCE_BEHAVIOR.ERROR}
22
+ */
23
+ get DEFAULT() {
24
+ return MISSING_RESOURCE_BEHAVIOR.ERROR;
25
+ }
26
+ };
27
+ const VALIDATION_TEXT = {
28
+ constraintMsg: "Condition not satisfied: constraint",
29
+ requiredMsg: "Condition not satisfied: required"
30
+ };
31
+ const INSTANCE_FILE_NAME = "xml_submission_file";
32
+ const INSTANCE_FILE_TYPE = "text/xml";
33
+
34
+ const constants = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
35
+ __proto__: null,
36
+ INSTANCE_FILE_NAME,
37
+ INSTANCE_FILE_TYPE,
38
+ MISSING_RESOURCE_BEHAVIOR,
39
+ VALIDATION_TEXT
40
+ }, Symbol.toStringTag, { value: 'Module' }));
2
41
 
3
42
  const equalFn = (a, b) => a === b;
4
43
  const signalOptions = {
@@ -358,47 +397,65 @@ function createUniqueId$1() {
358
397
  return `cl-${counter++}`;
359
398
  }
360
399
 
361
- const MISSING_RESOURCE_BEHAVIOR = {
362
- /**
363
- * When this behavior is configured, {@link InitializeForm | initializing} a
364
- * {@link PrimaryInstance} for a form which references any **missing**
365
- * resources will fail, producing an error to the calling client.
366
- *
367
- * @see {@link MissingResourceBehavior}
368
- */
369
- ERROR: "ERROR",
370
- /**
371
- * When this behavior is configured, {@link InitializeForm | initializing} a
372
- * {@link PrimaryInstance} for a form which references any **missing**
373
- * resources will succeed (producing a warning).
374
- *
375
- * Such missing resources will be parsed as if they are blank, as appropriate
376
- * for the resource's XForm semantic usage and/or format.
377
- *
378
- * @see {@link MissingResourceBehavior}
379
- */
380
- BLANK: "BLANK",
381
- /**
382
- * @see {@link MISSING_RESOURCE_BEHAVIOR.ERROR}
383
- */
384
- get DEFAULT() {
385
- return MISSING_RESOURCE_BEHAVIOR.ERROR;
400
+ const blobDescription = (resource) => {
401
+ const { name } = resource;
402
+ if (typeof name === "string") {
403
+ return name;
386
404
  }
405
+ let commonConstructorName;
406
+ if (resource instanceof File) {
407
+ commonConstructorName = "File";
408
+ } else {
409
+ commonConstructorName = "Blob";
410
+ }
411
+ return `Unknown ${commonConstructorName} data`;
387
412
  };
388
- const VALIDATION_TEXT = {
389
- constraintMsg: "Condition not satisfied: constraint",
390
- requiredMsg: "Condition not satisfied: required"
413
+ const formResourceMetadata = (resource) => {
414
+ if (resource instanceof Blob) {
415
+ return {
416
+ description: blobDescription(resource),
417
+ rawData: null
418
+ };
419
+ }
420
+ if (resource instanceof URL) {
421
+ return {
422
+ description: resource.href,
423
+ rawData: null
424
+ };
425
+ }
426
+ return {
427
+ description: "Raw string data",
428
+ rawData: resource
429
+ };
391
430
  };
392
- const SUBMISSION_INSTANCE_FILE_NAME = "xml_submission_file";
393
- const SUBMISSION_INSTANCE_FILE_TYPE = "text/xml";
394
-
395
- const constants = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
396
- __proto__: null,
397
- MISSING_RESOURCE_BEHAVIOR,
398
- SUBMISSION_INSTANCE_FILE_NAME,
399
- SUBMISSION_INSTANCE_FILE_TYPE,
400
- VALIDATION_TEXT
401
- }, Symbol.toStringTag, { value: 'Module' }));
431
+ class LoadFormFailureError extends AggregateError {
432
+ constructor(resource, errors) {
433
+ const { description, rawData } = formResourceMetadata(resource);
434
+ const messageLines = [
435
+ `Failed to load form resource: ${description}`,
436
+ "\n",
437
+ ...errors.map((error) => {
438
+ const aggregatedMessage = error.message;
439
+ if (aggregatedMessage == null) {
440
+ return "- Unknown error";
441
+ }
442
+ return `- ${aggregatedMessage}`;
443
+ })
444
+ ];
445
+ if (rawData != null) {
446
+ messageLines.push("\n- - -\n", "Raw resource data:", rawData);
447
+ }
448
+ const message = messageLines.join("\n");
449
+ super(errors, message);
450
+ const [head, ...tail] = errors;
451
+ if (head != null && tail.length === 0) {
452
+ const { stack } = head;
453
+ if (typeof stack === "string") {
454
+ this.stack = stack;
455
+ }
456
+ }
457
+ }
458
+ }
402
459
 
403
460
  let UnreachableError$1 = class UnreachableError extends Error {
404
461
  constructor(unrechable, additionalDetail) {
@@ -497,6 +554,11 @@ const retrieveSourceXMLResource = async (resource, options) => {
497
554
  assertResourceTextIsXML(text);
498
555
  return text;
499
556
  };
557
+ const retrieveFormDefinition = async (resource, options) => {
558
+ return retrieveSourceXMLResource(resource, {
559
+ fetchResource: options.fetchFormDefinition
560
+ });
561
+ };
500
562
 
501
563
  const validateReactiveScopeAPI = (api) => {
502
564
  if (api.owner == null) {
@@ -538,11 +600,6 @@ const createReactiveScope = (options = {}) => {
538
600
  }, options.owner ?? getOwner());
539
601
  };
540
602
 
541
- const createUniqueId = (() => {
542
- const { crypto } = globalThis;
543
- return crypto?.randomUUID?.bind(crypto) ?? createUniqueId$1;
544
- })();
545
-
546
603
  const XHTML_NAMESPACE_URI$1 = "http://www.w3.org/1999/xhtml";
547
604
  const XMLNS_NAMESPACE_URI$1 = "http://www.w3.org/2000/xmlns/";
548
605
  const XSD_NAMESPACE_URI = "http://www.w3.org/2001/XMLSchema";
@@ -25460,7 +25517,7 @@ class UnreachableError extends Error {
25460
25517
  }
25461
25518
  }
25462
25519
 
25463
- const identity = (value) => value;
25520
+ const identity$1 = (value) => value;
25464
25521
 
25465
25522
  class Reiterable {
25466
25523
  constructor(source) {
@@ -25740,7 +25797,7 @@ const getNodeTypeFilter = (domProvider, step) => {
25740
25797
  case "comment":
25741
25798
  return domProvider.filterComments;
25742
25799
  case "node":
25743
- return identity;
25800
+ return identity$1;
25744
25801
  case "text":
25745
25802
  return domProvider.filterTextNodes;
25746
25803
  default:
@@ -29221,9 +29278,6 @@ class DefaultEvaluator extends Evaluator {
29221
29278
  }
29222
29279
  }
29223
29280
 
29224
- const XFORMS_LOCAL_NAME = Symbol("XFORMS_LOCAL_NAME");
29225
- const XFORMS_KNOWN_ATTRIBUTE = Symbol("XFORMS_KNOWN_ATTRIBUTE");
29226
-
29227
29281
  const DAY_MILLISECONDS = 1e3 * 60 * 60 * 24;
29228
29282
  const MILLISECOND_NANOSECONDS = BigInt(1e6);
29229
29283
  const ISO_DATE_LIKE_SUBPATTERN = "\\d{4}-\\d{2}-\\d{2}";
@@ -39014,38 +39068,6 @@ class BodyDefinition extends DependencyContext {
39014
39068
  }
39015
39069
  }
39016
39070
 
39017
- class FormSubmissionDefinition {
39018
- submissionAction;
39019
- submissionMethod = "post";
39020
- encryptionKey;
39021
- constructor(xformDOM) {
39022
- const submissionElement = getSubmissionElement(xformDOM.model);
39023
- let submissionAction = null;
39024
- let submissionMethod;
39025
- let encryptionKey = null;
39026
- if (submissionElement == null) {
39027
- submissionAction = null;
39028
- submissionMethod = "post";
39029
- encryptionKey = null;
39030
- } else {
39031
- const method = submissionElement.getAttribute("method")?.trim();
39032
- if (method == null || method === "post" || method === "form-data-post") {
39033
- submissionMethod = "post";
39034
- } else {
39035
- throw new Error(`Unexpected <submission method>: ${method}`);
39036
- }
39037
- const action = submissionElement.getAttribute("action");
39038
- if (action != null) {
39039
- submissionAction = new URL(action.trim());
39040
- }
39041
- encryptionKey = submissionElement.getAttribute("base64RsaPublicKey");
39042
- }
39043
- this.submissionAction = submissionAction;
39044
- this.submissionMethod = submissionMethod;
39045
- this.encryptionKey = encryptionKey;
39046
- }
39047
- }
39048
-
39049
39071
  class NamespaceURL extends URL {
39050
39072
  static from(namespaceURI) {
39051
39073
  if (namespaceURI == null || namespaceURI === "") {
@@ -39119,13 +39141,34 @@ class StaticNode {
39119
39141
  }
39120
39142
  }
39121
39143
 
39144
+ class UnprefixedXFormsName extends QualifiedName {
39145
+ constructor(localName) {
39146
+ super({
39147
+ namespaceURI: XFORMS_NAMESPACE_URI$1,
39148
+ prefix: null,
39149
+ localName
39150
+ });
39151
+ }
39152
+ }
39153
+
39154
+ const staticNodeName = (source) => {
39155
+ if (source instanceof QualifiedName) {
39156
+ return source;
39157
+ }
39158
+ if (typeof source === "string") {
39159
+ return new UnprefixedXFormsName(source);
39160
+ }
39161
+ return new QualifiedName(source);
39162
+ };
39163
+
39122
39164
  class StaticAttribute extends StaticNode {
39123
39165
  constructor(parent, options) {
39124
39166
  super();
39125
39167
  this.parent = parent;
39126
39168
  this.rootDocument = parent.rootDocument;
39127
39169
  this.root = parent.root;
39128
- this.qualifiedName = new QualifiedName(options);
39170
+ this.qualifiedName = staticNodeName(options.name);
39171
+ this.nodeset = `${parent.nodeset}/@${this.qualifiedName.getPrefixedName()}`;
39129
39172
  this.value = options.value;
39130
39173
  }
39131
39174
  [XPathNodeKindKey] = "attribute";
@@ -39133,6 +39176,7 @@ class StaticAttribute extends StaticNode {
39133
39176
  rootDocument;
39134
39177
  root;
39135
39178
  qualifiedName;
39179
+ nodeset;
39136
39180
  attributes = [];
39137
39181
  children = null;
39138
39182
  value;
@@ -39142,36 +39186,140 @@ class StaticAttribute extends StaticNode {
39142
39186
  }
39143
39187
  }
39144
39188
 
39145
- const assertStaticElementKnownAttributeValue = (element, localName, value) => {
39146
- if (localName === element[XFORMS_KNOWN_ATTRIBUTE] && value == null) {
39147
- throw new Error(`Expected attribute: ${element[XFORMS_KNOWN_ATTRIBUTE]}`);
39189
+ class StaticParentNode extends StaticNode {
39190
+ [XPathNodeKindKey];
39191
+ nodeType;
39192
+ constructor(kind) {
39193
+ super();
39194
+ this[XPathNodeKindKey] = kind;
39195
+ this.nodeType = `static-${kind}`;
39148
39196
  }
39149
- };
39150
- class StaticElement extends StaticNode {
39151
- constructor(parent, attributesFactory, childNodesFactory, options) {
39197
+ }
39198
+
39199
+ class StaticText extends StaticNode {
39200
+ constructor(parent, value) {
39152
39201
  super();
39153
39202
  this.parent = parent;
39203
+ this.value = value;
39204
+ this.rootDocument = parent.rootDocument;
39205
+ this.root = parent.root;
39206
+ }
39207
+ [XPathNodeKindKey] = "text";
39208
+ nodeType = "static-text";
39209
+ rootDocument;
39210
+ root;
39211
+ children = null;
39212
+ // XFormsXPathText
39213
+ getXPathValue() {
39214
+ return this.value;
39215
+ }
39216
+ }
39217
+
39218
+ const leafElementChildrenResult = (parent, leafValue) => {
39219
+ const child = new StaticText(parent, leafValue);
39220
+ return {
39221
+ children: [child],
39222
+ childElements: [],
39223
+ leafValue
39224
+ };
39225
+ };
39226
+ const blankLeafElementChildrenResult = (parent) => {
39227
+ return leafElementChildrenResult(parent, "");
39228
+ };
39229
+ const buildStaticElementChildren = (parent, options) => {
39230
+ const children = Array();
39231
+ const childElements = Array();
39232
+ const childOptions = options.children ?? [""];
39233
+ if (childOptions.length === 0) {
39234
+ return {
39235
+ children,
39236
+ childElements,
39237
+ leafValue: ""
39238
+ };
39239
+ }
39240
+ const [head, ...tail] = childOptions;
39241
+ if (tail.length === 0) {
39242
+ if (head == null) {
39243
+ return blankLeafElementChildrenResult(parent);
39244
+ }
39245
+ if (typeof head === "string") {
39246
+ return leafElementChildrenResult(parent, head);
39247
+ }
39248
+ }
39249
+ if (typeof head === "string" && tail.length === 0) {
39250
+ const child = new StaticText(parent, head);
39251
+ children.push(child);
39252
+ return {
39253
+ children,
39254
+ childElements,
39255
+ leafValue: child.value
39256
+ };
39257
+ }
39258
+ for (const item of childOptions) {
39259
+ switch (typeof item) {
39260
+ case "string":
39261
+ children.push(new StaticText(parent, item));
39262
+ break;
39263
+ case "object": {
39264
+ const childElement = new StaticElement(parent, item);
39265
+ children.push(childElement);
39266
+ childElements.push(childElement);
39267
+ break;
39268
+ }
39269
+ default:
39270
+ throw new UnreachableError$1(item);
39271
+ }
39272
+ }
39273
+ return {
39274
+ children,
39275
+ childElements,
39276
+ leafValue: null
39277
+ };
39278
+ };
39279
+ class StaticElement extends StaticParentNode {
39280
+ constructor(parent, options) {
39281
+ super("element");
39282
+ this.parent = parent;
39154
39283
  const { rootDocument } = parent;
39284
+ let nodesetPrefix = parent.nodeset;
39155
39285
  this.rootDocument = rootDocument;
39156
39286
  if (parent === rootDocument) {
39157
39287
  this.root = this;
39288
+ if (nodesetPrefix === "/") {
39289
+ nodesetPrefix = "";
39290
+ }
39158
39291
  } else {
39159
39292
  this.root = parent.root;
39160
39293
  }
39161
- this.qualifiedName = new QualifiedName(options);
39162
- this.attributes = attributesFactory(this);
39163
- this.children = childNodesFactory(this);
39294
+ const { name, attributes = [] } = options;
39295
+ this.qualifiedName = staticNodeName(name);
39296
+ this.nodeset = `${nodesetPrefix}/${this.qualifiedName.getPrefixedName()}`;
39297
+ this.attributes = attributes.map((attrOptions) => {
39298
+ return new StaticAttribute(this, attrOptions);
39299
+ });
39300
+ const { children, childElements, leafValue } = buildStaticElementChildren(this, options);
39301
+ this.children = children;
39302
+ this.childElements = childElements;
39303
+ this.computedValue = leafValue;
39304
+ this.value = leafValue;
39164
39305
  }
39165
- [XFORMS_LOCAL_NAME];
39166
- [XFORMS_KNOWN_ATTRIBUTE];
39167
- [XPathNodeKindKey] = "element";
39168
- nodeType = "static-element";
39306
+ computedValue;
39169
39307
  rootDocument;
39170
39308
  root;
39171
39309
  qualifiedName;
39310
+ nodeset;
39172
39311
  attributes;
39173
39312
  children;
39174
- value = null;
39313
+ childElements;
39314
+ value;
39315
+ isLeafElement() {
39316
+ return this.value != null;
39317
+ }
39318
+ assertLeafElement() {
39319
+ if (!this.isLeafElement()) {
39320
+ throw new ErrorProductionDesignPendingError();
39321
+ }
39322
+ }
39175
39323
  /**
39176
39324
  * @todo Generalize this, incorporate into {@link EngineDOMAdapter}
39177
39325
  * @todo Namespaced lookup
@@ -39194,109 +39342,36 @@ class StaticElement extends StaticNode {
39194
39342
  getAttributeValue(localName) {
39195
39343
  const attribute = this.getAttributeNode(localName);
39196
39344
  const value = attribute?.value ?? null;
39197
- assertStaticElementKnownAttributeValue(this, localName, value);
39198
39345
  return value;
39199
39346
  }
39200
39347
  // XFormsXPathElement
39201
39348
  getXPathValue() {
39202
- return this.children.map((child) => child.getXPathValue()).join("");
39203
- }
39204
- }
39205
-
39206
- class StaticText extends StaticNode {
39207
- constructor(parent, value) {
39208
- super();
39209
- this.parent = parent;
39210
- this.value = value;
39211
- this.rootDocument = parent.rootDocument;
39212
- this.root = parent.root;
39213
- }
39214
- [XPathNodeKindKey] = "text";
39215
- nodeType = "static-text";
39216
- rootDocument;
39217
- root;
39218
- children = null;
39219
- // XFormsXPathText
39220
- getXPathValue() {
39221
- return this.value;
39349
+ const { computedValue } = this;
39350
+ if (computedValue != null) {
39351
+ return computedValue;
39352
+ }
39353
+ const result = this.children.map((child) => child.getXPathValue()).join("");
39354
+ this.computedValue = result;
39355
+ return result;
39222
39356
  }
39223
39357
  }
39224
39358
 
39225
- const domElementAttributesFactory = (domElement) => {
39226
- const attributes = Array.from(domElement.attributes);
39227
- return (element) => {
39228
- return attributes.map((attr) => {
39229
- return new StaticAttribute(element, attr);
39230
- });
39231
- };
39232
- };
39233
- const { ELEMENT_NODE, CDATA_SECTION_NODE, TEXT_NODE } = Node;
39234
- const toStaticChildNodeSource = (childNode) => {
39235
- const domType = childNode.nodeType;
39236
- switch (domType) {
39237
- case ELEMENT_NODE:
39238
- return {
39239
- domType,
39240
- domNode: childNode
39241
- };
39242
- case TEXT_NODE:
39243
- return {
39244
- domType,
39245
- domNode: childNode
39246
- };
39247
- case CDATA_SECTION_NODE:
39248
- return {
39249
- domType,
39250
- domNode: childNode
39251
- };
39252
- default:
39253
- return null;
39254
- }
39255
- };
39256
- const toStaticChildNodeSources = (childNode) => {
39257
- return toStaticChildNodeSource(childNode) ?? [];
39258
- };
39259
- const domElementChildNodesFactory = (domElement) => {
39260
- const childNodeSources = Array.from(domElement.childNodes).flatMap(toStaticChildNodeSources);
39261
- return (parent) => {
39262
- return childNodeSources.map((domSource) => {
39263
- switch (domSource.domType) {
39264
- case ELEMENT_NODE:
39265
- return parseStaticElementFromDOMElement(parent, StaticElement, domSource.domNode);
39266
- case TEXT_NODE:
39267
- case CDATA_SECTION_NODE:
39268
- return new StaticText(parent, domSource.domNode.data);
39269
- default:
39270
- throw new UnreachableError$1(domSource);
39271
- }
39272
- });
39273
- };
39274
- };
39275
- const parseStaticElementFromDOMElement = (parent, ElementConstructor, domElement) => {
39276
- const attributesFactory = domElementAttributesFactory(domElement);
39277
- const childNodesFactory = domElementChildNodesFactory(domElement);
39278
- return new ElementConstructor(parent, attributesFactory, childNodesFactory, domElement);
39279
- };
39280
- const parseStaticDocumentFromDOMSubtree = (DocumentConstructor, DocumentRootConstructor, subtreeRootElement) => {
39281
- const rootFactory = (parent) => {
39282
- return parseStaticElementFromDOMElement(parent, DocumentRootConstructor, subtreeRootElement);
39283
- };
39284
- return new DocumentConstructor(rootFactory);
39285
- };
39286
-
39287
- class StaticDocument extends StaticNode {
39288
- [XPathNodeKindKey] = "document";
39289
- nodeType = "static-document";
39359
+ class StaticDocument extends StaticParentNode {
39290
39360
  rootDocument;
39291
39361
  root;
39292
39362
  parent = null;
39363
+ nodeset;
39293
39364
  children;
39294
- constructor(rootFactory) {
39295
- super();
39365
+ childElements;
39366
+ constructor(options) {
39367
+ super("document");
39368
+ this.nodeset = options.nodesetPrefix ?? "/";
39296
39369
  this.rootDocument = this;
39297
- const root = rootFactory(this);
39370
+ const { documentRoot } = options;
39371
+ const root = new StaticElement(this, documentRoot);
39298
39372
  this.root = root;
39299
39373
  this.children = [root];
39374
+ this.childElements = this.children;
39300
39375
  }
39301
39376
  // XFormsXPathDocument
39302
39377
  getXPathValue() {
@@ -39304,41 +39379,74 @@ class StaticDocument extends StaticNode {
39304
39379
  }
39305
39380
  }
39306
39381
 
39307
- const assertItextTranslationRootKnownAttributeValue = (localName, value) => {
39308
- if (localName === "lang" && value == null) {
39309
- throw new Error("Invalid itext <translation> element: missing `lang` attribute");
39310
- }
39382
+ const parseNodeName = (domNode) => {
39383
+ return {
39384
+ namespaceURI: domNode.namespaceURI,
39385
+ prefix: domNode.prefix,
39386
+ localName: domNode.localName
39387
+ };
39388
+ };
39389
+ const parseStaticElementAttributes = (domElement) => {
39390
+ return Array.from(domElement.attributes).map((attr) => ({
39391
+ name: attr,
39392
+ value: attr.value
39393
+ }));
39394
+ };
39395
+ const { ELEMENT_NODE, CDATA_SECTION_NODE, TEXT_NODE } = Node;
39396
+ const parseStaticElementChildren = (domElement) => {
39397
+ return Array.from(domElement.childNodes).flatMap((child) => {
39398
+ switch (child.nodeType) {
39399
+ case ELEMENT_NODE:
39400
+ return parseStaticElementOptions(child);
39401
+ case TEXT_NODE:
39402
+ case CDATA_SECTION_NODE:
39403
+ return child.data;
39404
+ default:
39405
+ return [];
39406
+ }
39407
+ });
39408
+ };
39409
+ const parseStaticElementOptions = (domElement) => {
39410
+ return {
39411
+ name: parseNodeName(domElement),
39412
+ attributes: parseStaticElementAttributes(domElement),
39413
+ children: parseStaticElementChildren(domElement)
39414
+ };
39415
+ };
39416
+ const parseStaticDocumentFromDOMSubtree = (subtreeRootElement, options = {}) => {
39417
+ const documentRoot = parseStaticElementOptions(subtreeRootElement);
39418
+ return new StaticDocument({
39419
+ ...options,
39420
+ documentRoot
39421
+ });
39311
39422
  };
39312
- class ItextTranslationRootDefinition extends StaticElement {
39313
- [XFORMS_LOCAL_NAME] = "translation";
39314
- [XFORMS_KNOWN_ATTRIBUTE] = "lang";
39315
- getAttributeValue(localName) {
39316
- const value = super.getAttributeValue(localName);
39317
- assertItextTranslationRootKnownAttributeValue(localName, value);
39318
- return value;
39319
- }
39320
- }
39321
-
39322
- class ItextTranslationDefinition extends StaticDocument {
39323
- }
39324
39423
 
39424
+ const assertItextTranslationsDefinitionEntry = ([
39425
+ key,
39426
+ root
39427
+ ]) => {
39428
+ if (key == null || root.qualifiedName.localName !== "translation") {
39429
+ throw new Error();
39430
+ }
39431
+ };
39432
+ const itextTranslationNodesetPrefix = (lang) => {
39433
+ return `wf:translation('${lang}')`;
39434
+ };
39325
39435
  class ItextTranslationsDefinition extends Map {
39326
39436
  static from(xformDOM) {
39327
- const translationDefinitions = xformDOM.itextTranslationElements.map((element) => {
39328
- return parseStaticDocumentFromDOMSubtree(
39329
- ItextTranslationDefinition,
39330
- ItextTranslationRootDefinition,
39331
- element
39332
- );
39437
+ const entries = xformDOM.itextTranslationElements.map((element) => {
39438
+ const lang = element.getAttribute("lang");
39439
+ const { root } = parseStaticDocumentFromDOMSubtree(element, {
39440
+ nodesetPrefix: itextTranslationNodesetPrefix(lang)
39441
+ });
39442
+ const entry = [lang, root];
39443
+ assertItextTranslationsDefinitionEntry(entry);
39444
+ return entry;
39333
39445
  });
39334
- return new this(translationDefinitions);
39446
+ return new this(entries);
39335
39447
  }
39336
- constructor(translations) {
39337
- super(
39338
- translations.map(({ root }) => {
39339
- return [root.getAttributeValue("lang"), root];
39340
- })
39341
- );
39448
+ constructor(entries) {
39449
+ super(entries);
39342
39450
  }
39343
39451
  }
39344
39452
 
@@ -39644,6 +39752,24 @@ class ModelBindMap extends Map {
39644
39752
  }
39645
39753
  }
39646
39754
 
39755
+ const collectDefinitions = (result, definition) => {
39756
+ const { nodeset } = definition;
39757
+ if (result.has(nodeset)) {
39758
+ throw new ErrorProductionDesignPendingError();
39759
+ }
39760
+ result.set(nodeset, definition);
39761
+ if (definition.type === "leaf-node") {
39762
+ return result;
39763
+ }
39764
+ for (const child of definition.children) {
39765
+ collectDefinitions(result, child);
39766
+ }
39767
+ return result;
39768
+ };
39769
+ const nodeDefinitionMap = (root) => {
39770
+ return collectDefinitions(/* @__PURE__ */ new Map(), root);
39771
+ };
39772
+
39647
39773
  const ATTR_REGEX = /[&<>"]/;
39648
39774
  const CONTENT_REGEX = /[&<>]/;
39649
39775
  const escapeXMLText = (text, attr) => {
@@ -39953,24 +40079,21 @@ class DescendentNodeDefinition extends NodeDefinition {
39953
40079
  }
39954
40080
 
39955
40081
  class LeafNodeDefinition extends DescendentNodeDefinition {
39956
- constructor(parent, bind, bodyElement, node) {
40082
+ constructor(parent, bind, bodyElement, template) {
39957
40083
  if (bodyElement != null && bodyElement.category !== "control") {
39958
40084
  throw new Error(`Unexpected body element for nodeset ${bind.nodeset}`);
39959
40085
  }
39960
40086
  super(parent, bind, bodyElement);
39961
- this.node = node;
40087
+ this.template = template;
39962
40088
  this.valueType = bind.type.resolved;
39963
- this.qualifiedName = new QualifiedName(node);
40089
+ this.qualifiedName = template.qualifiedName;
39964
40090
  this.namespaceDeclarations = new NamespaceDeclarationMap(this);
39965
- this.defaultValue = node.textContent ?? "";
39966
40091
  }
39967
40092
  type = "leaf-node";
39968
40093
  valueType;
39969
40094
  namespaceDeclarations;
39970
40095
  qualifiedName;
39971
40096
  children = null;
39972
- instances = null;
39973
- defaultValue;
39974
40097
  toJSON() {
39975
40098
  const { bind, bodyElement, parent, root, ...rest } = this;
39976
40099
  return rest;
@@ -39981,8 +40104,8 @@ const isNoteBindDefinition = (bind) => {
39981
40104
  return bind.readonly.isConstantTruthyExpression();
39982
40105
  };
39983
40106
  class NoteNodeDefinition extends LeafNodeDefinition {
39984
- constructor(parent, bind, bodyElement, noteTextDefinition, node) {
39985
- super(parent, bind, bodyElement, node);
40107
+ constructor(parent, bind, bodyElement, noteTextDefinition, template) {
40108
+ super(parent, bind, bodyElement, template);
39986
40109
  this.bind = bind;
39987
40110
  this.bodyElement = bodyElement;
39988
40111
  this.noteTextDefinition = noteTextDefinition;
@@ -40223,20 +40346,22 @@ class IntValueCodec extends ValueCodec {
40223
40346
  }
40224
40347
  }
40225
40348
 
40349
+ const identity = (value) => value;
40350
+
40226
40351
  class StringValueCodec extends ValueCodec {
40227
40352
  constructor() {
40228
- super("string", identity$1, identity$1, {
40229
- decodeInstanceValueFactory: () => identity$1,
40230
- runtimeValueStateFactory: () => identity$1
40353
+ super("string", identity, identity, {
40354
+ decodeInstanceValueFactory: () => identity,
40355
+ runtimeValueStateFactory: () => identity
40231
40356
  });
40232
40357
  }
40233
40358
  }
40234
40359
 
40235
40360
  class ValueTypePlaceholderCodec extends ValueCodec {
40236
40361
  constructor(valueType) {
40237
- super(valueType, identity$1, identity$1, {
40238
- decodeInstanceValueFactory: () => identity$1,
40239
- runtimeValueStateFactory: () => identity$1
40362
+ super(valueType, identity, identity, {
40363
+ decodeInstanceValueFactory: () => identity,
40364
+ runtimeValueStateFactory: () => identity
40240
40365
  });
40241
40366
  }
40242
40367
  }
@@ -40323,131 +40448,70 @@ class RepeatCountControlExpression extends DependentExpression {
40323
40448
  }
40324
40449
  }
40325
40450
 
40326
- class RepeatInstanceDefinition extends DescendentNodeDefinition {
40327
- constructor(range, node) {
40328
- const { bind, bodyElement, parent, root } = range;
40329
- super(parent, bind, bodyElement);
40330
- this.node = node;
40331
- this.qualifiedName = new QualifiedName(node);
40332
- this.namespaceDeclarations = new NamespaceDeclarationMap(this);
40333
- this.children = root.buildSubtree(this);
40334
- }
40335
- type = "repeat-instance";
40336
- namespaceDeclarations;
40337
- qualifiedName;
40338
- children;
40339
- instances = null;
40340
- defaultValue = null;
40341
- toJSON() {
40342
- const { bind, bodyElement, parent, root, ...rest } = this;
40343
- return rest;
40451
+ const isJRTemplateAttributeName = (name) => {
40452
+ if (name.localName !== "template") {
40453
+ return false;
40344
40454
  }
40345
- }
40346
-
40347
- const repeatTemplates = /* @__PURE__ */ new WeakMap();
40348
- const isExplicitRepeatTemplateElement = (element) => {
40349
- return element.hasAttributeNS(JAVAROSA_NAMESPACE_URI$1, "template");
40455
+ const namespaceURI = name.namespaceURI?.href ?? name.namespaceURI;
40456
+ return namespaceURI === JAVAROSA_NAMESPACE_URI$1;
40350
40457
  };
40351
- const isLeafNode = (element) => {
40352
- return element.childElementCount === 0;
40458
+ const isJRTemplateAttribute = (attribute) => {
40459
+ return isJRTemplateAttributeName(attribute.qualifiedName);
40353
40460
  };
40354
- const clearLeafNodes = (element) => {
40355
- if (isLeafNode(element)) {
40356
- element.textContent = "";
40357
- } else {
40358
- for (const child of element.children) {
40359
- clearLeafNodes(child);
40360
- }
40361
- }
40362
- return element;
40461
+ const isExplicitRepeatTemplateElement = (sourceElement) => {
40462
+ return sourceElement.attributes.some(isJRTemplateAttribute);
40363
40463
  };
40364
- const getOrCreateTemplateElement = (element) => {
40365
- if (isExplicitRepeatTemplateElement(element)) {
40366
- return element;
40367
- }
40368
- const clone = element.cloneNode(true);
40369
- clone.setAttributeNS(JAVAROSA_NAMESPACE_URI$1, "template", "");
40370
- return clearLeafNodes(clone);
40464
+ const cloneStaticAttributeOptions = (attribute) => {
40465
+ return {
40466
+ name: attribute.qualifiedName,
40467
+ value: attribute.value
40468
+ };
40469
+ };
40470
+ const cloneStaticSubtreeStructure = (sourceElement) => {
40471
+ const name = sourceElement.qualifiedName;
40472
+ const attributes = sourceElement.attributes.map(cloneStaticAttributeOptions);
40473
+ const children = sourceElement.childElements.map(cloneStaticSubtreeStructure);
40474
+ return {
40475
+ name,
40476
+ attributes,
40477
+ children
40478
+ };
40479
+ };
40480
+ const cloneStaticElementStructure = (sourceElement) => {
40481
+ const { root: clone } = new StaticDocument({
40482
+ documentRoot: cloneStaticSubtreeStructure(sourceElement),
40483
+ nodesetPrefix: sourceElement.parent.nodeset
40484
+ });
40485
+ return clone;
40371
40486
  };
40372
- const splitInstanceNodes = (modelNodes) => {
40373
- const [first, ...rest] = modelNodes;
40374
- const template = getOrCreateTemplateElement(first);
40375
- if (template === first) {
40376
- return [template, ...rest];
40487
+ const parseRepeatTemplateElement = (firstRepeatInstanceNode) => {
40488
+ if (isExplicitRepeatTemplateElement(firstRepeatInstanceNode)) {
40489
+ return firstRepeatInstanceNode;
40377
40490
  }
40378
- return [template, ...modelNodes];
40491
+ return cloneStaticElementStructure(firstRepeatInstanceNode);
40379
40492
  };
40380
- class RepeatTemplateDefinition extends DescendentNodeDefinition {
40381
- constructor(range, templateNode) {
40382
- const { bind, bodyElement, parent, root } = range;
40383
- super(parent, bind, bodyElement);
40384
- this.templateNode = templateNode;
40385
- const node = templateNode.cloneNode(true);
40386
- node.removeAttributeNS(JAVAROSA_NAMESPACE_URI$1, "template");
40387
- this.node = node;
40388
- this.qualifiedName = new QualifiedName(node);
40389
- this.namespaceDeclarations = new NamespaceDeclarationMap(this);
40390
- this.children = root.buildSubtree(this);
40391
- }
40392
- static parseModelNodes(range, modelNodes) {
40393
- const { bind } = range;
40394
- let template = repeatTemplates.get(bind);
40395
- let instanceNodes;
40396
- if (template == null) {
40397
- const [templateNode, ...rest] = splitInstanceNodes(modelNodes);
40398
- instanceNodes = rest;
40399
- template = new this(range, templateNode);
40400
- } else {
40401
- const duplicateTemplate = modelNodes.find(
40402
- (node) => node.hasAttributeNS(JAVAROSA_NAMESPACE_URI$1, "template")
40403
- );
40404
- if (duplicateTemplate != null) {
40405
- throw new Error(`Multiple explicit templates defined for ${bind.nodeset}`);
40406
- }
40407
- instanceNodes = modelNodes;
40408
- }
40409
- return {
40410
- template,
40411
- instanceNodes
40412
- };
40493
+ class RepeatDefinition extends DescendentNodeDefinition {
40494
+ static from(parent, bind, bodyElement, instanceNodes) {
40495
+ return new this(parent, bind, bodyElement, instanceNodes);
40413
40496
  }
40414
- type = "repeat-template";
40415
- node;
40416
- namespaceDeclarations;
40417
- qualifiedName;
40497
+ type = "repeat";
40418
40498
  children;
40419
- instances = null;
40420
- defaultValue = null;
40421
- toJSON() {
40422
- const { bind, bodyElement, parent, root, ...rest } = this;
40423
- return rest;
40424
- }
40425
- }
40426
-
40427
- class RepeatRangeDefinition extends DescendentNodeDefinition {
40428
- static from(parent, bind, bodyElement, modelNodes) {
40429
- const definition = new this(parent, bind, bodyElement, modelNodes);
40430
- return definition;
40431
- }
40432
- type = "repeat-range";
40433
- template;
40434
- children = null;
40435
- instances;
40436
40499
  count;
40437
- node = null;
40500
+ template;
40438
40501
  namespaceDeclarations;
40439
40502
  qualifiedName;
40440
- defaultValue = null;
40441
- constructor(parent, bind, bodyElement, modelNodes) {
40503
+ constructor(parent, bind, bodyElement, instanceNodes) {
40442
40504
  super(parent, bind, bodyElement);
40443
- const { template, instanceNodes } = RepeatTemplateDefinition.parseModelNodes(this, modelNodes);
40505
+ const { root } = parent;
40506
+ const [instanceNode] = instanceNodes;
40507
+ const template = parseRepeatTemplateElement(instanceNode);
40508
+ const self = this;
40444
40509
  this.template = template;
40445
40510
  this.qualifiedName = template.qualifiedName;
40446
40511
  this.namespaceDeclarations = new NamespaceDeclarationMap(this);
40447
- this.count = RepeatCountControlExpression.from(bodyElement, instanceNodes.length);
40448
- this.instances = instanceNodes.map((element) => {
40449
- return new RepeatInstanceDefinition(this, element);
40450
- });
40512
+ this.children = root.buildSubtree(self, template);
40513
+ const initialCount = this.omitTemplate(instanceNodes).length;
40514
+ this.count = RepeatCountControlExpression.from(bodyElement, initialCount);
40451
40515
  }
40452
40516
  isControlled() {
40453
40517
  return this.count != null;
@@ -40455,9 +40519,13 @@ class RepeatRangeDefinition extends DescendentNodeDefinition {
40455
40519
  isUncontrolled() {
40456
40520
  return this.count == null;
40457
40521
  }
40522
+ omitTemplate(instanceNodes) {
40523
+ return instanceNodes.filter((instanceNode) => {
40524
+ return instanceNode !== this.template;
40525
+ });
40526
+ }
40458
40527
  toJSON() {
40459
- const { bind, bodyElement: groupDefinition, parent, root, ...rest } = this;
40460
- return rest;
40528
+ return {};
40461
40529
  }
40462
40530
  }
40463
40531
 
@@ -40467,13 +40535,14 @@ class RootAttributeDefinition {
40467
40535
  qualifiedName;
40468
40536
  value;
40469
40537
  constructor(root, source) {
40470
- const { namespaceURI, nodeName, value } = source;
40538
+ const { qualifiedName, value } = source;
40471
40539
  this.parent = root;
40472
- this.qualifiedName = new QualifiedName(source);
40540
+ this.qualifiedName = qualifiedName;
40473
40541
  this.value = value;
40474
- if (namespaceURI === XMLNS_NAMESPACE_URI$1) {
40542
+ if (qualifiedName.namespaceURI?.href === XMLNS_NAMESPACE_URI$1) {
40475
40543
  this.serializedXML = "";
40476
40544
  } else {
40545
+ const nodeName = qualifiedName.getPrefixedName();
40477
40546
  this.serializedXML = ` ${nodeName}="${escapeXMLText(value, true)}"`;
40478
40547
  }
40479
40548
  }
@@ -40482,21 +40551,20 @@ class RootAttributeDefinition {
40482
40551
  }
40483
40552
  }
40484
40553
 
40485
- const isNonNamespaceDeclarationDOMAttr = (domAttr) => {
40486
- return domAttr.namespaceURI !== XMLNS_NAMESPACE_URI$1;
40554
+ const isNonNamespaceAttribute = (attribute) => {
40555
+ return attribute.qualifiedName.namespaceURI?.href !== XMLNS_NAMESPACE_URI$1;
40487
40556
  };
40488
40557
  class RootAttributeMap extends Map {
40489
- static from(root, domRootSourceElement) {
40490
- const domAttrs = Array.from(domRootSourceElement.attributes);
40491
- const nonNamespaceDeclarationDOMAttrs = domAttrs.filter(isNonNamespaceDeclarationDOMAttr);
40492
- const attributes = nonNamespaceDeclarationDOMAttrs.map((source) => {
40493
- return new RootAttributeDefinition(root, source);
40558
+ static from(root, instanceNode) {
40559
+ const nonNamespaceAttributes = instanceNode.attributes.filter(isNonNamespaceAttribute);
40560
+ const definitions = nonNamespaceAttributes.map((attribute) => {
40561
+ return new RootAttributeDefinition(root, attribute);
40494
40562
  });
40495
- return new this(attributes);
40563
+ return new this(definitions);
40496
40564
  }
40497
- constructor(attributes) {
40565
+ constructor(definitions) {
40498
40566
  super(
40499
- attributes.map((attribute) => {
40567
+ definitions.map((attribute) => {
40500
40568
  return [attribute.qualifiedName, attribute];
40501
40569
  })
40502
40570
  );
@@ -40504,23 +40572,21 @@ class RootAttributeMap extends Map {
40504
40572
  }
40505
40573
 
40506
40574
  class SubtreeDefinition extends DescendentNodeDefinition {
40507
- constructor(parent, bind, bodyElement, node) {
40575
+ constructor(parent, bind, bodyElement, template) {
40508
40576
  if (bodyElement != null && (bodyElement.category !== "structure" || bodyElement.type === "repeat")) {
40509
40577
  throw new Error(`Unexpected body element for nodeset ${bind.nodeset}`);
40510
40578
  }
40511
40579
  super(parent, bind, bodyElement);
40512
- this.node = node;
40580
+ this.template = template;
40513
40581
  const { root } = parent;
40514
- this.qualifiedName = new QualifiedName(node);
40582
+ this.qualifiedName = template.qualifiedName;
40515
40583
  this.namespaceDeclarations = new NamespaceDeclarationMap(this);
40516
- this.children = root.buildSubtree(this);
40584
+ this.children = root.buildSubtree(this, template);
40517
40585
  }
40518
40586
  type = "subtree";
40519
40587
  namespaceDeclarations;
40520
40588
  qualifiedName;
40521
40589
  children;
40522
- instances = null;
40523
- defaultValue = null;
40524
40590
  toJSON() {
40525
40591
  const { parent, bodyElement, bind, root, ...rest } = this;
40526
40592
  return rest;
@@ -40529,8 +40595,8 @@ class SubtreeDefinition extends DescendentNodeDefinition {
40529
40595
 
40530
40596
  class RootDefinition extends NodeDefinition {
40531
40597
  constructor(form, model, submission, classes) {
40532
- const { primaryInstanceRoot } = form.xformDOM;
40533
- const qualifiedName = new QualifiedName(primaryInstanceRoot);
40598
+ const template = model.instance.root;
40599
+ const qualifiedName = template.qualifiedName;
40534
40600
  const nodeName = qualifiedName.getPrefixedName();
40535
40601
  const nodeset = `/${nodeName}`;
40536
40602
  const bind = model.binds.get(nodeset);
@@ -40543,32 +40609,30 @@ class RootDefinition extends NodeDefinition {
40543
40609
  this.submission = submission;
40544
40610
  this.classes = classes;
40545
40611
  this.qualifiedName = qualifiedName;
40546
- this.node = primaryInstanceRoot;
40547
- this.attributes = RootAttributeMap.from(this, primaryInstanceRoot);
40612
+ this.template = template;
40613
+ this.attributes = RootAttributeMap.from(this, template);
40548
40614
  this.namespaceDeclarations = new NamespaceDeclarationMap(this);
40549
- this.children = this.buildSubtree(this);
40615
+ this.children = this.buildSubtree(this, template);
40550
40616
  }
40551
40617
  type = "root";
40552
40618
  qualifiedName;
40553
40619
  bodyElement = null;
40554
40620
  root = this;
40555
40621
  parent = null;
40622
+ template;
40556
40623
  namespaceDeclarations;
40557
40624
  attributes;
40558
40625
  children;
40559
- instances = null;
40560
- node;
40561
- defaultValue = null;
40562
40626
  isTranslated = false;
40563
- buildSubtree(parent) {
40627
+ buildSubtree(parent, node) {
40564
40628
  const { form, model } = this;
40565
40629
  const { body } = form;
40566
40630
  const { binds } = model;
40567
- const { bind: parentBind, node } = parent;
40631
+ const { bind: parentBind } = parent;
40568
40632
  const { nodeset: parentNodeset } = parentBind;
40569
40633
  const childrenByName = /* @__PURE__ */ new Map();
40570
- for (const child of node.children) {
40571
- const { nodeName } = child;
40634
+ for (const child of node.childElements) {
40635
+ const nodeName = child.qualifiedName.getPrefixedName();
40572
40636
  let elements = childrenByName.get(nodeName);
40573
40637
  if (elements == null) {
40574
40638
  elements = [child];
@@ -40583,14 +40647,13 @@ class RootDefinition extends NodeDefinition {
40583
40647
  const bodyElement = body.getBodyElement(nodeset);
40584
40648
  const [firstChild, ...restChildren] = children;
40585
40649
  if (bodyElement?.type === "repeat") {
40586
- return RepeatRangeDefinition.from(parent, bind, bodyElement, children);
40650
+ return RepeatDefinition.from(parent, bind, bodyElement, children);
40587
40651
  }
40588
40652
  if (restChildren.length) {
40589
40653
  throw new Error(`Unexpected: multiple elements for non-repeat nodeset: ${nodeset}`);
40590
40654
  }
40591
40655
  const element = firstChild;
40592
- const isLeafNode = element.childElementCount === 0;
40593
- if (isLeafNode) {
40656
+ if (element.isLeafElement()) {
40594
40657
  if (bodyElement?.type === "range") {
40595
40658
  return RangeNodeDefinition.from(parent, bind, bodyElement, element);
40596
40659
  }
@@ -40605,17 +40668,69 @@ class RootDefinition extends NodeDefinition {
40605
40668
  }
40606
40669
  }
40607
40670
 
40671
+ class SubmissionDefinition {
40672
+ submissionAction;
40673
+ submissionMethod = "post";
40674
+ encryptionKey;
40675
+ constructor(xformDOM) {
40676
+ const submissionElement = getSubmissionElement(xformDOM.model);
40677
+ let submissionAction = null;
40678
+ let submissionMethod;
40679
+ let encryptionKey = null;
40680
+ if (submissionElement == null) {
40681
+ submissionAction = null;
40682
+ submissionMethod = "post";
40683
+ encryptionKey = null;
40684
+ } else {
40685
+ const method = submissionElement.getAttribute("method")?.trim();
40686
+ if (method == null || method === "post" || method === "form-data-post") {
40687
+ submissionMethod = "post";
40688
+ } else {
40689
+ throw new Error(`Unexpected <submission method>: ${method}`);
40690
+ }
40691
+ const action = submissionElement.getAttribute("action");
40692
+ if (action != null) {
40693
+ submissionAction = new URL(action.trim());
40694
+ }
40695
+ encryptionKey = submissionElement.getAttribute("base64RsaPublicKey");
40696
+ }
40697
+ this.submissionAction = submissionAction;
40698
+ this.submissionMethod = submissionMethod;
40699
+ this.encryptionKey = encryptionKey;
40700
+ }
40701
+ }
40702
+
40608
40703
  class ModelDefinition {
40609
40704
  constructor(form) {
40610
40705
  this.form = form;
40611
- const submission = new FormSubmissionDefinition(form.xformDOM);
40706
+ const submission = new SubmissionDefinition(form.xformDOM);
40612
40707
  this.binds = ModelBindMap.fromModel(this);
40708
+ this.instance = parseStaticDocumentFromDOMSubtree(form.xformDOM.primaryInstanceRoot, {
40709
+ nodesetPrefix: "/"
40710
+ });
40613
40711
  this.root = new RootDefinition(form, this, submission, form.body.classes);
40712
+ this.nodes = nodeDefinitionMap(this.root);
40614
40713
  this.itextTranslations = ItextTranslationsDefinition.from(form.xformDOM);
40615
40714
  }
40616
40715
  binds;
40617
40716
  root;
40717
+ nodes;
40718
+ instance;
40618
40719
  itextTranslations;
40720
+ getNodeDefinition(nodeset) {
40721
+ const definition = this.nodes.get(nodeset);
40722
+ if (definition == null) {
40723
+ throw new ErrorProductionDesignPendingError(`No definition for nodeset: ${nodeset}`);
40724
+ }
40725
+ return definition;
40726
+ }
40727
+ getRootDefinition(instance) {
40728
+ const definition = this.getNodeDefinition(instance.root.nodeset);
40729
+ if (definition !== this.root) {
40730
+ throw new ErrorProductionDesignPendingError();
40731
+ }
40732
+ return definition;
40733
+ }
40619
40734
  toJSON() {
40620
40735
  const { form, ...rest } = this;
40621
40736
  return rest;
@@ -40661,13 +40776,30 @@ class JRResourceURL extends URL {
40661
40776
  }
40662
40777
  }
40663
40778
 
40664
- class SecondaryInstanceRootDefinition extends StaticElement {
40665
- [XFORMS_LOCAL_NAME] = "instance";
40666
- [XFORMS_KNOWN_ATTRIBUTE] = "id";
40667
- }
40779
+ const assertSecondaryInstanceDefinition = ({ root }) => {
40780
+ const id = root.getAttributeValue("id");
40781
+ if (id == null) {
40782
+ throw new Error();
40783
+ }
40784
+ };
40668
40785
 
40669
- class SecondaryInstanceDefinition extends StaticDocument {
40670
- }
40786
+ const defineSecondaryInstance = (instanceId, secondaryInstanceRoot) => {
40787
+ const doc = new StaticDocument({
40788
+ documentRoot: {
40789
+ name: "instance",
40790
+ attributes: [
40791
+ {
40792
+ name: "id",
40793
+ value: instanceId
40794
+ }
40795
+ ],
40796
+ children: [secondaryInstanceRoot]
40797
+ },
40798
+ nodesetPrefix: `instance('${instanceId}')`
40799
+ });
40800
+ assertSecondaryInstanceDefinition(doc);
40801
+ return doc;
40802
+ };
40671
40803
 
40672
40804
  class SecondaryInstanceSource {
40673
40805
  constructor(format, instanceId, resourceURL, domElement) {
@@ -40682,24 +40814,8 @@ class BlankSecondaryInstanceSource extends SecondaryInstanceSource {
40682
40814
  constructor(instanceId, resourceURL, domElement) {
40683
40815
  super("blank", instanceId, resourceURL, domElement);
40684
40816
  }
40685
- /**
40686
- * @todo there is really no sense in using the DOM for this, other than it was
40687
- * quicker/more ergonomic than constructing the requisite {@link StaticNode}
40688
- * structures directly. Pretty good sign those constructor signatures could
40689
- * use some TLC!
40690
- */
40691
40817
  parseDefinition() {
40692
- const xmlDocument = this.domElement.ownerDocument.implementation.createDocument(
40693
- XFORMS_NAMESPACE_URI$1,
40694
- "instance"
40695
- );
40696
- const instanceElement = xmlDocument.documentElement;
40697
- instanceElement.setAttribute("id", this.instanceId);
40698
- return parseStaticDocumentFromDOMSubtree(
40699
- SecondaryInstanceDefinition,
40700
- SecondaryInstanceRootDefinition,
40701
- instanceElement
40702
- );
40818
+ return defineSecondaryInstance(this.instanceId, "");
40703
40819
  }
40704
40820
  }
40705
40821
 
@@ -40762,80 +40878,28 @@ const stripTrailingEmptyCells = (columns, row) => {
40762
40878
  }
40763
40879
  return result;
40764
40880
  };
40765
- class CSVExternalSecondaryInstanceColumnElement extends StaticElement {
40766
- constructor(parent, column) {
40767
- const { columnName, cellValue } = column;
40768
- super(
40769
- parent,
40770
- () => [],
40771
- (self) => [new StaticText(self, cellValue)],
40772
- {
40773
- namespaceURI: XFORMS_NAMESPACE_URI$1,
40774
- localName: columnName
40775
- }
40776
- );
40777
- }
40778
- }
40779
- class CSVExternalSecondaryInstanceItemElement extends StaticElement {
40780
- constructor(parent, item) {
40781
- super(
40782
- parent,
40783
- () => [],
40784
- (self) => {
40785
- return item.map((column) => {
40786
- return new CSVExternalSecondaryInstanceColumnElement(self, column);
40787
- });
40788
- },
40789
- {
40790
- namespaceURI: XFORMS_NAMESPACE_URI$1,
40791
- localName: "item"
40792
- }
40793
- );
40794
- }
40795
- }
40796
- class CSVExternalSecondaryInstanceRootElement extends StaticElement {
40797
- constructor(parent, items) {
40798
- super(
40799
- parent,
40800
- () => [],
40801
- (self) => {
40802
- return items.map((item) => {
40803
- return new CSVExternalSecondaryInstanceItemElement(self, item);
40804
- });
40805
- },
40806
- {
40807
- namespaceURI: XFORMS_NAMESPACE_URI$1,
40808
- localName: "root"
40809
- }
40810
- );
40811
- }
40812
- }
40813
- class CSVExternalSecondaryInstanceDocumentElement extends SecondaryInstanceRootDefinition {
40814
- constructor(instanceId, parent, items) {
40815
- super(
40816
- parent,
40817
- (self) => [
40818
- new StaticAttribute(self, {
40819
- namespaceURI: XFORMS_NAMESPACE_URI$1,
40820
- localName: "id",
40821
- value: instanceId
40822
- })
40823
- ],
40824
- (self) => [new CSVExternalSecondaryInstanceRootElement(self, items)],
40825
- {
40826
- namespaceURI: XFORMS_NAMESPACE_URI$1,
40827
- localName: "instance"
40828
- }
40829
- );
40830
- }
40831
- }
40832
- class CSVExternalSecondaryInstanceDefinition extends SecondaryInstanceDefinition {
40833
- constructor(instanceId, items) {
40834
- super((self) => {
40835
- return new CSVExternalSecondaryInstanceDocumentElement(instanceId, self, items);
40836
- });
40837
- }
40838
- }
40881
+ const columnChildOption = (column) => {
40882
+ const { columnName, cellValue } = column;
40883
+ return {
40884
+ name: columnName,
40885
+ children: [cellValue]
40886
+ };
40887
+ };
40888
+ const itemChildOption$1 = (item) => {
40889
+ return {
40890
+ name: "item",
40891
+ children: item.map(columnChildOption)
40892
+ };
40893
+ };
40894
+ const rootChildOption$1 = (items) => {
40895
+ return {
40896
+ name: "root",
40897
+ children: items.map(itemChildOption$1)
40898
+ };
40899
+ };
40900
+ const csvExternalSecondaryInstanceDefinition = (instanceId, items) => {
40901
+ return defineSecondaryInstance(instanceId, rootChildOption$1(items));
40902
+ };
40839
40903
  class CSVExternalSecondaryInstanceSource extends ExternalSecondaryInstanceSource {
40840
40904
  /**
40841
40905
  * Based on
@@ -40923,7 +40987,7 @@ class CSVExternalSecondaryInstanceSource extends ExternalSecondaryInstanceSource
40923
40987
  delimiter: meta.delimiter
40924
40988
  });
40925
40989
  const items = this.toItems(columns, rows);
40926
- return new CSVExternalSecondaryInstanceDefinition(this.instanceId, items);
40990
+ return csvExternalSecondaryInstanceDefinition(this.instanceId, items);
40927
40991
  }
40928
40992
  }
40929
40993
 
@@ -41172,134 +41236,72 @@ const serializeCoordinates = (coordinates) => {
41172
41236
  const [longitude, latitude] = coordinates;
41173
41237
  return `${latitude} ${longitude} 0 0`;
41174
41238
  };
41175
- class GeoJSONSecondaryInstanceFeatureGeometryElement extends StaticElement {
41176
- static from(parent, feature) {
41177
- const { geometry } = feature;
41178
- switch (geometry.type) {
41179
- case "LineString":
41180
- return new this(parent, geometry.coordinates);
41181
- case "Point":
41182
- return new this(parent, [geometry.coordinates]);
41183
- case "Polygon":
41184
- return new this(parent, geometry.coordinates[0] ?? []);
41185
- default:
41186
- throw new UnreachableError$1(geometry);
41239
+ const geometryValues = (geometry) => {
41240
+ switch (geometry.type) {
41241
+ case "LineString":
41242
+ return geometry.coordinates.map(serializeCoordinates);
41243
+ case "Point":
41244
+ return [serializeCoordinates(geometry.coordinates)];
41245
+ case "Polygon": {
41246
+ const [coordinates = []] = geometry.coordinates;
41247
+ return coordinates.map(serializeCoordinates);
41187
41248
  }
41249
+ default:
41250
+ throw new UnreachableError$1(geometry);
41188
41251
  }
41189
- constructor(parent, points) {
41190
- const values = points.map(serializeCoordinates);
41191
- const value = values.join("; ");
41192
- super(
41193
- parent,
41194
- () => [],
41195
- (self) => [new StaticText(self, value)],
41196
- {
41197
- namespaceURI: XFORMS_NAMESPACE_URI$1,
41198
- localName: "geometry"
41199
- }
41200
- );
41201
- }
41202
- }
41203
- class GeoJSONSecondaryInstanceFeaturePropertyElement extends StaticElement {
41204
- static *buildPropertyElements(parent, feature) {
41205
- const { properties } = feature;
41206
- if (properties == null) {
41207
- return [];
41208
- }
41209
- const { id: propertiesId, ...nonIdProperties } = properties;
41210
- const { id = propertiesId } = feature;
41211
- if (id !== undefined) {
41212
- yield new this(parent, "id", String(id));
41213
- }
41214
- for (const [propertyName, propertyValue] of Object.entries(nonIdProperties)) {
41215
- yield new this(parent, propertyName, String(propertyValue));
41216
- }
41217
- }
41218
- constructor(parent, propertyName, propertyValue) {
41219
- super(
41220
- parent,
41221
- () => [],
41222
- (self) => [new StaticText(self, propertyValue)],
41223
- {
41224
- namespaceURI: XFORMS_NAMESPACE_URI$1,
41225
- localName: propertyName
41226
- }
41227
- );
41228
- }
41229
- }
41230
- class GeoJSONSecondaryInstanceFeatureItemElement extends StaticElement {
41231
- constructor(parent, feature) {
41232
- super(
41233
- parent,
41234
- () => [],
41235
- (self) => {
41236
- const geometry = GeoJSONSecondaryInstanceFeatureGeometryElement.from(self, feature);
41237
- const properties = GeoJSONSecondaryInstanceFeaturePropertyElement.buildPropertyElements(
41238
- self,
41239
- feature
41240
- );
41241
- return [geometry, ...properties];
41242
- },
41243
- {
41244
- namespaceURI: XFORMS_NAMESPACE_URI$1,
41245
- localName: "item"
41246
- }
41247
- );
41248
- }
41249
- }
41250
- class GeoJSONSecondaryInstanceRootElement extends StaticElement {
41251
- constructor(parent, featureCollection) {
41252
- super(
41253
- parent,
41254
- () => [],
41255
- (self) => {
41256
- return featureCollection.features.map((feature) => {
41257
- return new GeoJSONSecondaryInstanceFeatureItemElement(self, feature);
41258
- });
41259
- },
41260
- {
41261
- namespaceURI: XFORMS_NAMESPACE_URI$1,
41262
- localName: "root"
41263
- }
41264
- );
41252
+ };
41253
+ const geometryChildElementOption = (feature) => {
41254
+ const { geometry } = feature;
41255
+ const values = geometryValues(geometry);
41256
+ const value = values.join("; ");
41257
+ return {
41258
+ name: "geometry",
41259
+ children: [value]
41260
+ };
41261
+ };
41262
+ const propertyChildOption = (propertyName, propertyValue) => {
41263
+ return {
41264
+ name: propertyName,
41265
+ children: [propertyValue]
41266
+ };
41267
+ };
41268
+ function* propertyChildOptions(feature) {
41269
+ const { properties } = feature;
41270
+ if (properties == null) {
41271
+ return [];
41265
41272
  }
41266
- }
41267
- class GeoJSONExternalSecondaryInstanceDocumentElement extends SecondaryInstanceRootDefinition {
41268
- constructor(instanceId, parent, featureCollection) {
41269
- super(
41270
- parent,
41271
- (self) => [
41272
- new StaticAttribute(self, {
41273
- namespaceURI: XFORMS_NAMESPACE_URI$1,
41274
- localName: "id",
41275
- value: instanceId
41276
- })
41277
- ],
41278
- (self) => [new GeoJSONSecondaryInstanceRootElement(self, featureCollection)],
41279
- {
41280
- namespaceURI: XFORMS_NAMESPACE_URI$1,
41281
- localName: "instance"
41282
- }
41283
- );
41273
+ const { id: propertiesId, ...nonIdProperties } = properties;
41274
+ const { id = propertiesId } = feature;
41275
+ if (id !== undefined) {
41276
+ yield propertyChildOption("id", String(id));
41284
41277
  }
41285
- }
41286
- class GeoJSONExternalSecondaryInstanceDefinition extends SecondaryInstanceDefinition {
41287
- constructor(instanceId, featureCollection) {
41288
- super((self) => {
41289
- return new GeoJSONExternalSecondaryInstanceDocumentElement(
41290
- instanceId,
41291
- self,
41292
- featureCollection
41293
- );
41294
- });
41278
+ for (const [propertyName, propertyValue] of Object.entries(nonIdProperties)) {
41279
+ yield propertyChildOption(propertyName, String(propertyValue));
41295
41280
  }
41296
41281
  }
41282
+ const itemChildOption = (feature) => {
41283
+ const geometry = geometryChildElementOption(feature);
41284
+ const properties = propertyChildOptions(feature);
41285
+ return {
41286
+ name: "item",
41287
+ children: [geometry, ...properties]
41288
+ };
41289
+ };
41290
+ const rootChildOption = (featureCollection) => {
41291
+ return {
41292
+ name: "root",
41293
+ children: featureCollection.features.map(itemChildOption)
41294
+ };
41295
+ };
41296
+ const geoJSONExternalSecondaryInstanceDefinition = (instanceId, featureCollection) => {
41297
+ return defineSecondaryInstance(instanceId, rootChildOption(featureCollection));
41298
+ };
41297
41299
  class GeoJSONExternalSecondaryInstanceSource extends ExternalSecondaryInstanceSource {
41298
41300
  parseDefinition() {
41299
41301
  const { data } = this.resource;
41300
41302
  const value = JSON.parse(data);
41301
41303
  assertFeatureCollection(value);
41302
- return new GeoJSONExternalSecondaryInstanceDefinition(this.instanceId, value);
41304
+ return geoJSONExternalSecondaryInstanceDefinition(this.instanceId, value);
41303
41305
  }
41304
41306
  }
41305
41307
 
@@ -41308,11 +41310,11 @@ class InternalSecondaryInstanceSource extends SecondaryInstanceSource {
41308
41310
  super("internal", instanceId, resourceURL, domElement);
41309
41311
  }
41310
41312
  parseDefinition() {
41311
- return parseStaticDocumentFromDOMSubtree(
41312
- SecondaryInstanceDefinition,
41313
- SecondaryInstanceRootDefinition,
41314
- this.domElement
41315
- );
41313
+ const doc = parseStaticDocumentFromDOMSubtree(this.domElement, {
41314
+ nodesetPrefix: `instance('${this.instanceId}')`
41315
+ });
41316
+ assertSecondaryInstanceDefinition(doc);
41317
+ return doc;
41316
41318
  }
41317
41319
  }
41318
41320
 
@@ -41332,11 +41334,9 @@ class XMLExternalSecondaryInstanceSource extends ExternalSecondaryInstanceSource
41332
41334
  const instanceElement = xmlDocument.documentElement;
41333
41335
  instanceElement.setAttribute("id", this.instanceId);
41334
41336
  instanceElement.innerHTML = this.resource.data;
41335
- return parseStaticDocumentFromDOMSubtree(
41336
- SecondaryInstanceDefinition,
41337
- SecondaryInstanceRootDefinition,
41338
- instanceElement
41339
- );
41337
+ const doc = parseStaticDocumentFromDOMSubtree(instanceElement);
41338
+ assertSecondaryInstanceDefinition(doc);
41339
+ return doc;
41340
41340
  }
41341
41341
  }
41342
41342
 
@@ -41405,6 +41405,208 @@ class SecondaryInstancesDefinition extends Map {
41405
41405
  }
41406
41406
  }
41407
41407
 
41408
+ class BaseFormResult {
41409
+ status;
41410
+ warnings;
41411
+ error;
41412
+ constructor(options) {
41413
+ this.status = options.status;
41414
+ this.warnings = options.warnings;
41415
+ this.error = options.error;
41416
+ }
41417
+ }
41418
+
41419
+ const failedFormResultMethodFactory = (cause) => {
41420
+ return () => {
41421
+ throw new Error(cause.message, { cause });
41422
+ };
41423
+ };
41424
+ class FormFailureResult extends BaseFormResult {
41425
+ createInstance;
41426
+ editInstance;
41427
+ restoreInstance;
41428
+ constructor(options) {
41429
+ const { error, warnings } = options;
41430
+ super({
41431
+ status: "failure",
41432
+ warnings,
41433
+ error
41434
+ });
41435
+ this.createInstance = failedFormResultMethodFactory(error);
41436
+ this.editInstance = failedFormResultMethodFactory(error);
41437
+ this.restoreInstance = failedFormResultMethodFactory(error);
41438
+ }
41439
+ }
41440
+
41441
+ const wrapInstanceXML = (model, instanceXML) => {
41442
+ const defaultNamespace = model.root.namespaceDeclarations.get(null);
41443
+ const defaultNamespaceURI = defaultNamespace?.declaredURI?.href;
41444
+ if (defaultNamespaceURI == null) {
41445
+ return `<wrapped>${instanceXML}</wrapped>`;
41446
+ }
41447
+ return (
41448
+ /* xml */
41449
+ `<wrapped xmlns="${escapeXMLText(defaultNamespaceURI, true)}">
41450
+ ${instanceXML}
41451
+ </wrapped>`
41452
+ );
41453
+ };
41454
+ const assertSoleChildElement = (element) => {
41455
+ if (element == null) {
41456
+ throw new ErrorProductionDesignPendingError("Expected a child element");
41457
+ }
41458
+ const { parentNode } = element;
41459
+ if (parentNode == null) {
41460
+ throw new ErrorProductionDesignPendingError("Expected element to be a child of a parent node");
41461
+ }
41462
+ if (element !== parentNode.firstElementChild || element !== parentNode.lastElementChild) {
41463
+ throw new ErrorProductionDesignPendingError(
41464
+ "Expected child element to be parent node's only element child."
41465
+ );
41466
+ }
41467
+ };
41468
+ const getWrappedInstanceRootElement = (xml) => {
41469
+ const domParser = new DOMParser();
41470
+ const doc = domParser.parseFromString(xml, "text/xml");
41471
+ const { documentElement } = doc;
41472
+ const [root] = documentElement.children;
41473
+ assertSoleChildElement(root);
41474
+ return root;
41475
+ };
41476
+ const parseInstanceXML = (model, instanceXML) => {
41477
+ const wrappedXML = wrapInstanceXML(model, instanceXML);
41478
+ const root = getWrappedInstanceRootElement(wrappedXML);
41479
+ return parseStaticDocumentFromDOMSubtree(root);
41480
+ };
41481
+
41482
+ class MalformedInstanceDataError extends ErrorProductionDesignPendingError {
41483
+ }
41484
+
41485
+ const resolveInstanceAttachmentMapSource = async (input) => {
41486
+ const inputEntries = Array.from(input.entries());
41487
+ if (inputEntries.length > 0) {
41488
+ const fileNames = Array.from(input.keys());
41489
+ const errors = fileNames.map((fileName) => {
41490
+ return new Error(`Failed to resolve instance attachment with file name "${fileName}"`);
41491
+ });
41492
+ throw new AggregateError(errors, "Not implemented: instance attachment resource resolution");
41493
+ }
41494
+ const entries = await Promise.all(
41495
+ inputEntries.map(async ([fileName, resolveAttachment]) => {
41496
+ const response = await resolveAttachment();
41497
+ const blob = await response.blob();
41498
+ const value = new File([blob], fileName);
41499
+ return [fileName, value];
41500
+ })
41501
+ );
41502
+ return { entries: () => entries };
41503
+ };
41504
+ const assertKeyedInstanceDataFile = (key, file) => {
41505
+ if (file.name !== key) {
41506
+ throw new MalformedInstanceDataError(
41507
+ `Unexpected InstanceData file. Expected file name to match key, got key: ${JSON.stringify(key)} and file name: ${JSON.stringify(file.name)}`
41508
+ );
41509
+ }
41510
+ };
41511
+ const assertInstanceDataEntry = (entry) => {
41512
+ const [key, value] = entry;
41513
+ if (!(value instanceof File)) {
41514
+ throw new MalformedInstanceDataError(
41515
+ `Unexpected non-file attachment in InstanceData for key: ${key}`
41516
+ );
41517
+ }
41518
+ assertKeyedInstanceDataFile(key, value);
41519
+ };
41520
+ class InstanceAttachmentMap extends Map {
41521
+ static from(sources) {
41522
+ return new this(sources);
41523
+ }
41524
+ /**
41525
+ * @todo
41526
+ * @see {@link resolveInstanceAttachmentMapSource}
41527
+ */
41528
+ static async resolve(input) {
41529
+ const source = await resolveInstanceAttachmentMapSource(input);
41530
+ return new this([source]);
41531
+ }
41532
+ constructor(sources) {
41533
+ super();
41534
+ for (const source of sources) {
41535
+ for (const entry of source.entries()) {
41536
+ const [key] = entry;
41537
+ if (key === INSTANCE_FILE_NAME) {
41538
+ continue;
41539
+ }
41540
+ if (this.has(key)) {
41541
+ throw new MalformedInstanceDataError(
41542
+ `Unexpected InstanceData entry. Duplicate instance attachment for key: ${JSON.stringify(key)}`
41543
+ );
41544
+ }
41545
+ assertInstanceDataEntry(entry);
41546
+ const [, value] = entry;
41547
+ this.set(key, value);
41548
+ }
41549
+ }
41550
+ }
41551
+ }
41552
+
41553
+ const resolveInstanceXML = async (input) => {
41554
+ const instanceResult = await input.resolveInstance();
41555
+ if (typeof instanceResult === "string") {
41556
+ return instanceResult;
41557
+ }
41558
+ if (instanceResult instanceof Blob) {
41559
+ return getBlobText(instanceResult);
41560
+ }
41561
+ return instanceResult.text();
41562
+ };
41563
+ const parseInstanceDocument = (model, instanceXML) => {
41564
+ const doc = parseInstanceXML(model, instanceXML);
41565
+ const expectedId = model.instance.root.getAttributeValue("id");
41566
+ const actualId = doc.root.getAttributeValue("id");
41567
+ if (expectedId !== actualId) {
41568
+ throw new ErrorProductionDesignPendingError(
41569
+ `Invalid instance input. Expected instance id to be "${expectedId}", got: "${actualId}"`
41570
+ );
41571
+ }
41572
+ return doc;
41573
+ };
41574
+ class InitialInstanceState {
41575
+ static async from(model, data) {
41576
+ return this.resolve(model, {
41577
+ inputType: "FORM_INSTANCE_INPUT_RESOLVED",
41578
+ data
41579
+ });
41580
+ }
41581
+ static async resolve(model, input) {
41582
+ if (input.inputType === "FORM_INSTANCE_INPUT_RESOLVED") {
41583
+ const { data } = input;
41584
+ const [instanceData] = data;
41585
+ const instanceFile = instanceData.get(INSTANCE_FILE_NAME);
41586
+ const instanceXML2 = await getBlobText(instanceFile);
41587
+ const attachments2 = InstanceAttachmentMap.from(data);
41588
+ return new this(model, {
41589
+ instanceXML: instanceXML2,
41590
+ attachments: attachments2
41591
+ });
41592
+ }
41593
+ const [instanceXML, attachments] = await Promise.all([
41594
+ resolveInstanceXML(input),
41595
+ InstanceAttachmentMap.resolve(input.attachments)
41596
+ ]);
41597
+ return new this(model, {
41598
+ instanceXML,
41599
+ attachments
41600
+ });
41601
+ }
41602
+ document;
41603
+ attachments;
41604
+ constructor(model, options) {
41605
+ this.document = parseInstanceDocument(model, options.instanceXML);
41606
+ this.attachments = options.attachments;
41607
+ }
41608
+ }
41609
+
41408
41610
  const isEngineXPathNode = (value) => {
41409
41611
  return typeof value === "object" && value != null && XPathNodeKindKey in value;
41410
41612
  };
@@ -41716,53 +41918,51 @@ class EngineXPathEvaluator extends XFormsXPathEvaluator {
41716
41918
  }
41717
41919
  }
41718
41920
 
41719
- const createInstanceSubmissionState = (node) => {
41921
+ const createPrimaryInstanceState = (node) => {
41720
41922
  return {
41721
- get submissionXML() {
41722
- return node.root.submissionState.submissionXML;
41923
+ get instanceXML() {
41924
+ return node.root.instanceState.instanceXML;
41723
41925
  }
41724
41926
  };
41725
41927
  };
41726
41928
 
41727
41929
  class InstanceFile extends File {
41728
- name = SUBMISSION_INSTANCE_FILE_NAME;
41729
- type = SUBMISSION_INSTANCE_FILE_TYPE;
41930
+ name = INSTANCE_FILE_NAME;
41931
+ type = INSTANCE_FILE_TYPE;
41730
41932
  constructor(instanceRoot) {
41731
- const { submissionXML } = instanceRoot.submissionState;
41732
- super([submissionXML], SUBMISSION_INSTANCE_FILE_NAME, {
41733
- type: SUBMISSION_INSTANCE_FILE_TYPE
41933
+ const { instanceXML } = instanceRoot.instanceState;
41934
+ super([instanceXML], INSTANCE_FILE_NAME, {
41935
+ type: INSTANCE_FILE_TYPE
41734
41936
  });
41735
41937
  }
41736
41938
  }
41737
- const assertSubmissionData = (data) => {
41738
- const instanceFile = data.get(SUBMISSION_INSTANCE_FILE_NAME);
41939
+ const assertInstanceData = (data) => {
41940
+ const instanceFile = data.get(INSTANCE_FILE_NAME);
41739
41941
  if (!(instanceFile instanceof InstanceFile)) {
41740
- throw new Error(`Invalid SubmissionData`);
41942
+ throw new Error(`Invalid InstanceData`);
41741
41943
  }
41742
41944
  };
41743
- class InstanceSubmissionData extends FormData {
41945
+ class InstanceData extends FormData {
41744
41946
  constructor(instanceFile, attachments) {
41745
41947
  super();
41746
41948
  this.instanceFile = instanceFile;
41747
41949
  this.attachments = attachments;
41748
- this.set(SUBMISSION_INSTANCE_FILE_NAME, instanceFile);
41950
+ this.set(INSTANCE_FILE_NAME, instanceFile);
41749
41951
  attachments.forEach((attachment) => {
41750
41952
  const { name } = attachment;
41751
- if (name === SUBMISSION_INSTANCE_FILE_NAME && attachment !== instanceFile) {
41752
- throw new Error(
41753
- `Failed to add conflicting attachment with name ${SUBMISSION_INSTANCE_FILE_NAME}`
41754
- );
41953
+ if (name === INSTANCE_FILE_NAME && attachment !== instanceFile) {
41954
+ throw new Error(`Failed to add conflicting attachment with name ${INSTANCE_FILE_NAME}`);
41755
41955
  }
41756
41956
  this.set(name, attachment);
41757
41957
  });
41758
41958
  }
41759
41959
  static from(instanceFile, attachments) {
41760
41960
  const data = new this(instanceFile, attachments);
41761
- assertSubmissionData(data);
41961
+ assertInstanceData(data);
41762
41962
  return data;
41763
41963
  }
41764
41964
  }
41765
- const validateSubmission = (instanceRoot) => {
41965
+ const validateInstance = (instanceRoot) => {
41766
41966
  const { violations } = instanceRoot.validationState;
41767
41967
  if (violations.length === 0) {
41768
41968
  return {
@@ -41775,48 +41975,50 @@ const validateSubmission = (instanceRoot) => {
41775
41975
  violations
41776
41976
  };
41777
41977
  };
41778
- const monolithicSubmissionResult = (validation, definition, instanceFile, attachments) => {
41779
- const data = InstanceSubmissionData.from(instanceFile, attachments);
41978
+ const monolithicInstancePayload = (validation, submissionMeta, instanceFile, attachments) => {
41979
+ const data = InstanceData.from(instanceFile, attachments);
41780
41980
  return {
41981
+ payloadType: "monolithic",
41781
41982
  ...validation,
41782
- definition,
41783
- data
41983
+ submissionMeta,
41984
+ data: [data]
41784
41985
  };
41785
41986
  };
41786
- const chunkedSubmissionResult = (validation, definition, instanceFile, attachments, options) => {
41987
+ const chunkedInstancePayload = (validation, submissionMeta, instanceFile, attachments, options) => {
41787
41988
  if (attachments.length > 0 || options.maxSize !== Infinity) {
41788
- throw new Error("Submission chunking pending implementation");
41989
+ throw new Error("InstancePayload chunking pending implementation");
41789
41990
  }
41790
- const data = InstanceSubmissionData.from(instanceFile, attachments);
41991
+ const data = InstanceData.from(instanceFile, attachments);
41791
41992
  return {
41993
+ payloadType: "chunked",
41792
41994
  ...validation,
41793
- definition,
41995
+ submissionMeta,
41794
41996
  data: [data]
41795
41997
  };
41796
41998
  };
41797
- const prepareSubmission = (instanceRoot, options) => {
41798
- const validation = validateSubmission(instanceRoot);
41799
- const definition = instanceRoot.definition.submission;
41999
+ const prepareInstancePayload = (instanceRoot, options) => {
42000
+ const validation = validateInstance(instanceRoot);
42001
+ const submissionMeta = instanceRoot.definition.submission;
41800
42002
  const instanceFile = new InstanceFile(instanceRoot);
41801
42003
  const attachments = [];
41802
- switch (options.chunked) {
42004
+ switch (options.payloadType) {
41803
42005
  case "chunked":
41804
- return chunkedSubmissionResult(
42006
+ return chunkedInstancePayload(
41805
42007
  validation,
41806
- definition,
42008
+ submissionMeta,
41807
42009
  instanceFile,
41808
42010
  attachments,
41809
42011
  options
41810
42012
  );
41811
42013
  case "monolithic":
41812
- return monolithicSubmissionResult(
42014
+ return monolithicInstancePayload(
41813
42015
  validation,
41814
- definition,
42016
+ submissionMeta,
41815
42017
  instanceFile,
41816
42018
  attachments
41817
42019
  );
41818
42020
  default:
41819
- throw new UnreachableError$1(options.chunked);
42021
+ throw new UnreachableError$1(options.payloadType);
41820
42022
  }
41821
42023
  };
41822
42024
 
@@ -42044,14 +42246,20 @@ const createSharedNodeState = (scope, spec, options) => {
42044
42246
  };
42045
42247
  };
42046
42248
 
42249
+ const createUniqueId = (() => {
42250
+ const { crypto } = globalThis;
42251
+ return crypto?.randomUUID?.bind(crypto) ?? createUniqueId$1;
42252
+ })();
42253
+
42047
42254
  const nodeID = (id) => {
42048
42255
  return `node:${id}`;
42049
42256
  };
42050
42257
 
42051
42258
  class InstanceNode {
42052
- constructor(engineConfig, parent, definition, options) {
42053
- this.engineConfig = engineConfig;
42259
+ constructor(instanceConfig, parent, instanceNode, definition, options) {
42260
+ this.instanceConfig = instanceConfig;
42054
42261
  this.parent = parent;
42262
+ this.instanceNode = instanceNode;
42055
42263
  this.definition = definition;
42056
42264
  const self = this;
42057
42265
  if (parent == null) {
@@ -42066,8 +42274,8 @@ class InstanceNode {
42066
42274
  }
42067
42275
  this.computeReference = options?.computeReference ?? this.computeChildStepReference;
42068
42276
  this.scope = options?.scope ?? createReactiveScope();
42069
- this.engineConfig = engineConfig;
42070
- this.nodeId = nodeID(engineConfig.createUniqueId());
42277
+ this.instanceConfig = instanceConfig;
42278
+ this.nodeId = nodeID(createUniqueId());
42071
42279
  this.definition = definition;
42072
42280
  }
42073
42281
  rootDocument;
@@ -42126,12 +42334,12 @@ class InstanceNode {
42126
42334
  }
42127
42335
  }
42128
42336
 
42129
- const createRootSubmissionState = (node) => {
42337
+ const createRootInstanceState = (node) => {
42130
42338
  return {
42131
- get submissionXML() {
42339
+ get instanceXML() {
42132
42340
  const { namespaceDeclarations, attributes } = node.definition;
42133
42341
  const serializedChildren = node.currentState.children.map((child) => {
42134
- return child.submissionState.submissionXML;
42342
+ return child.instanceState.instanceXML;
42135
42343
  });
42136
42344
  return serializeParentElementXML(node.definition.qualifiedName, serializedChildren, {
42137
42345
  namespaceDeclarations,
@@ -42248,9 +42456,10 @@ const createComputedExpression = (context, dependentExpression, options = {}) =>
42248
42456
  };
42249
42457
 
42250
42458
  class DescendantNode extends InstanceNode {
42251
- constructor(parent, definition, options) {
42252
- super(parent.engineConfig, parent, definition, options);
42459
+ constructor(parent, instanceNode, definition, options) {
42460
+ super(parent.instanceConfig, parent, instanceNode, definition, options);
42253
42461
  this.parent = parent;
42462
+ this.instanceNode = instanceNode;
42254
42463
  this.definition = definition;
42255
42464
  if (this.isRoot()) {
42256
42465
  this.root = this;
@@ -42258,7 +42467,7 @@ class DescendantNode extends InstanceNode {
42258
42467
  this.root = parent.root;
42259
42468
  }
42260
42469
  const { evaluator } = parent;
42261
- if (definition.type === "repeat-range") {
42470
+ if (definition.type === "repeat") {
42262
42471
  this[XPathNodeKindKey] = XFORMS_XPATH_NODE_RANGE_KIND;
42263
42472
  } else {
42264
42473
  this[XPathNodeKindKey] = "element";
@@ -42376,14 +42585,14 @@ class DescendantNode extends InstanceNode {
42376
42585
  }
42377
42586
  }
42378
42587
 
42379
- const createParentNodeSubmissionState = (node) => {
42588
+ const createParentNodeInstanceState = (node) => {
42380
42589
  return {
42381
- get submissionXML() {
42590
+ get instanceXML() {
42382
42591
  if (!node.currentState.relevant) {
42383
42592
  return "";
42384
42593
  }
42385
42594
  const serializedChildren = node.currentState.children.map((child) => {
42386
- return child.submissionState.submissionXML;
42595
+ return child.instanceState.instanceXML;
42387
42596
  });
42388
42597
  return serializeParentElementXML(node.definition.qualifiedName, serializedChildren);
42389
42598
  }
@@ -42490,15 +42699,12 @@ class Group extends DescendantNode {
42490
42699
  nodeOptions = null;
42491
42700
  currentState;
42492
42701
  validationState;
42493
- submissionState;
42494
- constructor(parent, definition) {
42495
- super(parent, definition);
42702
+ instanceState;
42703
+ constructor(parent, instanceNode, definition) {
42704
+ super(parent, instanceNode, definition);
42496
42705
  this.appearances = definition.bodyElement.appearances;
42497
42706
  const childrenState = createChildrenState(this);
42498
42707
  this.childrenState = childrenState;
42499
- const sharedStateOptions = {
42500
- clientStateFactory: this.engineConfig.stateFactory
42501
- };
42502
42708
  const state = createSharedNodeState(
42503
42709
  this.scope,
42504
42710
  {
@@ -42512,7 +42718,7 @@ class Group extends DescendantNode {
42512
42718
  valueOptions: null,
42513
42719
  value: null
42514
42720
  },
42515
- sharedStateOptions
42721
+ this.instanceConfig
42516
42722
  );
42517
42723
  this.state = state;
42518
42724
  this.engineState = state.engineState;
@@ -42522,8 +42728,8 @@ class Group extends DescendantNode {
42522
42728
  childrenState
42523
42729
  );
42524
42730
  childrenState.setChildren(buildChildren(this));
42525
- this.validationState = createAggregatedViolations(this, sharedStateOptions);
42526
- this.submissionState = createParentNodeSubmissionState(this);
42731
+ this.validationState = createAggregatedViolations(this, this.instanceConfig);
42732
+ this.instanceState = createParentNodeInstanceState(this);
42527
42733
  }
42528
42734
  getChildren() {
42529
42735
  return this.childrenState.getChildren();
@@ -42538,10 +42744,10 @@ const createFieldHint = (context, definition) => {
42538
42744
  return createTextRange(context, "hint", hintDefinition);
42539
42745
  };
42540
42746
 
42541
- const createValueNodeSubmissionState = (node) => {
42747
+ const createValueNodeInstanceState = (node) => {
42542
42748
  const { qualifiedName } = node.definition;
42543
42749
  return {
42544
- get submissionXML() {
42750
+ get instanceXML() {
42545
42751
  if (!node.currentState.relevant) {
42546
42752
  return "";
42547
42753
  }
@@ -42551,18 +42757,17 @@ const createValueNodeSubmissionState = (node) => {
42551
42757
  };
42552
42758
  };
42553
42759
 
42554
- const isInstanceFirstLoad = (valueSource) => {
42555
- return valueSource === "FORM_DEFAULT";
42760
+ const isInstanceFirstLoad = (context) => {
42761
+ return context.rootDocument.initializationMode === "create";
42556
42762
  };
42557
- const getInitialValue$1 = (context, options) => {
42558
- const { initialValueSource = "FORM_DEFAULT" } = options;
42559
- if (initialValueSource === "FORM_DEFAULT") {
42560
- const { defaultValue } = context.definition;
42561
- return context.decodeInstanceValue(defaultValue);
42562
- }
42563
- throw new ErrorProductionDesignPendingError("Edit implementation pending");
42763
+ const isEditInitialLoad = (context) => {
42764
+ return context.rootDocument.initializationMode === "edit";
42564
42765
  };
42565
- const createRelevantValueState$1 = (context, baseValueState) => {
42766
+ const getInitialValue = (context) => {
42767
+ const sourceNode = context.instanceNode ?? context.definition.template;
42768
+ return context.decodeInstanceValue(sourceNode.value);
42769
+ };
42770
+ const createRelevantValueState = (context, baseValueState) => {
42566
42771
  return context.scope.runTask(() => {
42567
42772
  const [getRelevantValue, setValue] = baseValueState;
42568
42773
  const getValue = createMemo(() => {
@@ -42574,7 +42779,7 @@ const createRelevantValueState$1 = (context, baseValueState) => {
42574
42779
  return [getValue, setValue];
42575
42780
  });
42576
42781
  };
42577
- const guardDownstreamReadonlyWrites$1 = (context, baseState) => {
42782
+ const guardDownstreamReadonlyWrites = (context, baseState) => {
42578
42783
  const { readonly } = context.definition.bind;
42579
42784
  if (readonly.isDefaultExpression) {
42580
42785
  return baseState;
@@ -42590,9 +42795,12 @@ const guardDownstreamReadonlyWrites$1 = (context, baseState) => {
42590
42795
  return [getValue, setValue];
42591
42796
  };
42592
42797
  const PRELOAD_UID_EXPRESSION = 'concat("uuid:", uuid())';
42593
- const setPreloadUIDValue = (context, valueState, options) => {
42798
+ const shouldPreloadUID = (context) => {
42799
+ return isInstanceFirstLoad(context) || isEditInitialLoad(context);
42800
+ };
42801
+ const setPreloadUIDValue = (context, valueState) => {
42594
42802
  const { preload } = context.definition.bind;
42595
- if (preload?.type !== "uid" || !isInstanceFirstLoad(options?.initialValueSource)) {
42803
+ if (preload?.type !== "uid" || !shouldPreloadUID(context)) {
42596
42804
  return;
42597
42805
  }
42598
42806
  const preloadUIDValue = context.evaluator.evaluateString(PRELOAD_UID_EXPRESSION, {
@@ -42601,7 +42809,7 @@ const setPreloadUIDValue = (context, valueState, options) => {
42601
42809
  const [, setValue] = valueState;
42602
42810
  setValue(preloadUIDValue);
42603
42811
  };
42604
- const createCalculation$1 = (context, setRelevantValue, calculateDefinition) => {
42812
+ const createCalculation = (context, setRelevantValue, calculateDefinition) => {
42605
42813
  context.scope.runTask(() => {
42606
42814
  const calculate = createComputedExpression(context, calculateDefinition, {
42607
42815
  defaultValue: ""
@@ -42615,18 +42823,18 @@ const createCalculation$1 = (context, setRelevantValue, calculateDefinition) =>
42615
42823
  });
42616
42824
  });
42617
42825
  };
42618
- const createInstanceValueState = (context, options = {}) => {
42826
+ const createInstanceValueState = (context) => {
42619
42827
  return context.scope.runTask(() => {
42620
- const initialValue = getInitialValue$1(context, options);
42828
+ const initialValue = getInitialValue(context);
42621
42829
  const baseValueState = createSignal(initialValue);
42622
- const relevantValueState = createRelevantValueState$1(context, baseValueState);
42623
- setPreloadUIDValue(context, relevantValueState, options);
42830
+ const relevantValueState = createRelevantValueState(context, baseValueState);
42831
+ setPreloadUIDValue(context, relevantValueState);
42624
42832
  const { calculate } = context.definition.bind;
42625
42833
  if (calculate != null) {
42626
42834
  const [, setValue] = relevantValueState;
42627
- createCalculation$1(context, setValue, calculate);
42835
+ createCalculation(context, setValue, calculate);
42628
42836
  }
42629
- return guardDownstreamReadonlyWrites$1(context, relevantValueState);
42837
+ return guardDownstreamReadonlyWrites(context, relevantValueState);
42630
42838
  });
42631
42839
  };
42632
42840
 
@@ -42734,6 +42942,24 @@ const createValidationState = (context, options) => {
42734
42942
  };
42735
42943
 
42736
42944
  class ValueNode extends DescendantNode {
42945
+ constructor(parent, instanceNode, definition, codec) {
42946
+ super(parent, instanceNode, definition);
42947
+ this.instanceNode = instanceNode;
42948
+ this.valueType = definition.valueType;
42949
+ this.decodeInstanceValue = codec.decodeInstanceValue;
42950
+ const instanceValueState = createInstanceValueState(this);
42951
+ const valueState = codec.createRuntimeValueState(instanceValueState);
42952
+ const [getInstanceValue] = instanceValueState;
42953
+ const [, setValueState] = valueState;
42954
+ this.getInstanceValue = getInstanceValue;
42955
+ this.setValueState = setValueState;
42956
+ this.getXPathValue = () => {
42957
+ return this.getInstanceValue();
42958
+ };
42959
+ this.valueState = valueState;
42960
+ this.validation = createValidationState(this, this.instanceConfig);
42961
+ this.instanceState = createValueNodeInstanceState(this);
42962
+ }
42737
42963
  validation;
42738
42964
  getInstanceValue;
42739
42965
  valueState;
@@ -42747,28 +42973,7 @@ class ValueNode extends DescendantNode {
42747
42973
  get validationState() {
42748
42974
  return this.validation.currentState;
42749
42975
  }
42750
- submissionState;
42751
- constructor(parent, definition, codec) {
42752
- super(parent, definition);
42753
- this.valueType = definition.valueType;
42754
- this.decodeInstanceValue = codec.decodeInstanceValue;
42755
- const instanceValueState = createInstanceValueState(this, {
42756
- initialValueSource: "FORM_DEFAULT"
42757
- });
42758
- const valueState = codec.createRuntimeValueState(instanceValueState);
42759
- const [getInstanceValue] = instanceValueState;
42760
- const [, setValueState] = valueState;
42761
- this.getInstanceValue = getInstanceValue;
42762
- this.setValueState = setValueState;
42763
- this.getXPathValue = () => {
42764
- return this.getInstanceValue();
42765
- };
42766
- this.valueState = valueState;
42767
- this.validation = createValidationState(this, {
42768
- clientStateFactory: this.engineConfig.stateFactory
42769
- });
42770
- this.submissionState = createValueNodeSubmissionState(this);
42771
- }
42976
+ instanceState;
42772
42977
  // ValidationContext
42773
42978
  getViolation() {
42774
42979
  return this.validation.engineState.violation;
@@ -42805,8 +43010,8 @@ const nodeOptionsFactoryByType = {
42805
43010
  intent: () => null
42806
43011
  };
42807
43012
  class InputControl extends ValueNode {
42808
- static from(parent, definition) {
42809
- return new this(parent, definition);
43013
+ static from(parent, instanceNode, definition) {
43014
+ return new this(parent, instanceNode, definition);
42810
43015
  }
42811
43016
  // XFormsXPathElement
42812
43017
  [XPathNodeKindKey] = "element";
@@ -42818,14 +43023,260 @@ class InputControl extends ValueNode {
42818
43023
  appearances;
42819
43024
  nodeOptions;
42820
43025
  currentState;
42821
- constructor(parent, definition) {
43026
+ constructor(parent, instanceNode, definition) {
42822
43027
  const codec = getSharedValueCodec(definition.valueType);
42823
- super(parent, definition, codec);
43028
+ super(parent, instanceNode, definition, codec);
42824
43029
  this.appearances = definition.bodyElement.appearances;
42825
43030
  this.nodeOptions = nodeOptionsFactoryByType[definition.valueType](definition.bodyElement);
42826
- const sharedStateOptions = {
42827
- clientStateFactory: this.engineConfig.stateFactory
43031
+ const state = createSharedNodeState(
43032
+ this.scope,
43033
+ {
43034
+ reference: this.contextReference,
43035
+ readonly: this.isReadonly,
43036
+ relevant: this.isRelevant,
43037
+ required: this.isRequired,
43038
+ label: createNodeLabel(this, definition),
43039
+ hint: createFieldHint(this, definition),
43040
+ children: null,
43041
+ valueOptions: null,
43042
+ value: this.valueState,
43043
+ instanceValue: this.getInstanceValue
43044
+ },
43045
+ this.instanceConfig
43046
+ );
43047
+ this.state = state;
43048
+ this.engineState = state.engineState;
43049
+ this.currentState = state.currentState;
43050
+ }
43051
+ setValue(value) {
43052
+ this.setValueState(value);
43053
+ return this.root;
43054
+ }
43055
+ }
43056
+
43057
+ class ModelValue extends ValueNode {
43058
+ static from(parent, instanceNode, definition) {
43059
+ return new this(parent, instanceNode, definition);
43060
+ }
43061
+ // XFormsXPathElement
43062
+ [XPathNodeKindKey] = "element";
43063
+ // InstanceNode
43064
+ state;
43065
+ engineState;
43066
+ // ModelValueNode
43067
+ nodeType = "model-value";
43068
+ appearances = null;
43069
+ nodeOptions = null;
43070
+ currentState;
43071
+ constructor(parent, instanceNode, definition) {
43072
+ const codec = getSharedValueCodec(definition.valueType);
43073
+ super(parent, instanceNode, definition, codec);
43074
+ const state = createSharedNodeState(
43075
+ this.scope,
43076
+ {
43077
+ reference: this.contextReference,
43078
+ readonly: this.isReadonly,
43079
+ relevant: this.isRelevant,
43080
+ required: this.isRequired,
43081
+ label: null,
43082
+ hint: null,
43083
+ children: null,
43084
+ valueOptions: null,
43085
+ value: this.valueState,
43086
+ instanceValue: this.getInstanceValue
43087
+ },
43088
+ this.instanceConfig
43089
+ );
43090
+ this.state = state;
43091
+ this.engineState = state.engineState;
43092
+ this.currentState = state.currentState;
43093
+ }
43094
+ }
43095
+
43096
+ class NoteCodec extends ValueCodec {
43097
+ constructor(baseCodec) {
43098
+ const encodeValue = (value) => {
43099
+ return baseCodec.encodeValue(value ?? "");
43100
+ };
43101
+ const decodeValue = (value) => {
43102
+ return value === "" ? null : baseCodec.decodeValue(value);
43103
+ };
43104
+ super(baseCodec.valueType, encodeValue, decodeValue);
43105
+ }
43106
+ }
43107
+
43108
+ const noteCodecs = {
43109
+ string: new NoteCodec(sharedValueCodecs.string),
43110
+ int: new NoteCodec(sharedValueCodecs.int),
43111
+ decimal: new NoteCodec(sharedValueCodecs.decimal),
43112
+ boolean: new NoteCodec(sharedValueCodecs.boolean),
43113
+ date: new NoteCodec(sharedValueCodecs.date),
43114
+ time: new NoteCodec(sharedValueCodecs.time),
43115
+ dateTime: new NoteCodec(sharedValueCodecs.dateTime),
43116
+ geopoint: new NoteCodec(sharedValueCodecs.geopoint),
43117
+ geotrace: new NoteCodec(sharedValueCodecs.geotrace),
43118
+ geoshape: new NoteCodec(sharedValueCodecs.geoshape),
43119
+ binary: new NoteCodec(sharedValueCodecs.binary),
43120
+ barcode: new NoteCodec(sharedValueCodecs.barcode),
43121
+ intent: new NoteCodec(sharedValueCodecs.intent)
43122
+ };
43123
+ const getNoteCodec = (valueType) => {
43124
+ return noteCodecs[valueType];
43125
+ };
43126
+
43127
+ const createNoteReadonlyThunk = (context, definition) => {
43128
+ definition.bodyElement;
43129
+ const { readonly } = definition.bind;
43130
+ if (!readonly.isConstantTruthyExpression()) {
43131
+ throw new Error("Expected a static readonly expression");
43132
+ }
43133
+ let result = true;
43134
+ return () => result;
43135
+ };
43136
+
43137
+ const createNoteText = (context, noteTextDefinition) => {
43138
+ const { scope } = context;
43139
+ const { role } = noteTextDefinition;
43140
+ return scope.runTask(() => {
43141
+ switch (role) {
43142
+ case "label": {
43143
+ const label = createTextRange(context, role, noteTextDefinition);
43144
+ return {
43145
+ role,
43146
+ label,
43147
+ hint: null
43148
+ };
43149
+ }
43150
+ case "hint": {
43151
+ const hint = createTextRange(context, role, noteTextDefinition);
43152
+ return {
43153
+ role,
43154
+ label: null,
43155
+ hint
43156
+ };
43157
+ }
43158
+ default:
43159
+ throw new UnreachableError$1(noteTextDefinition);
43160
+ }
43161
+ });
43162
+ };
43163
+
43164
+ class Note extends ValueNode {
43165
+ // XFormsXPathElement
43166
+ [XPathNodeKindKey] = "element";
43167
+ // InstanceNode
43168
+ state;
43169
+ engineState;
43170
+ // NoteNode
43171
+ nodeType = "note";
43172
+ appearances;
43173
+ nodeOptions = null;
43174
+ currentState;
43175
+ constructor(parent, instanceNode, definition) {
43176
+ const codec = getNoteCodec(definition.valueType);
43177
+ super(parent, instanceNode, definition, codec);
43178
+ this.appearances = definition.bodyElement.appearances;
43179
+ const isReadonly = createNoteReadonlyThunk(this, definition);
43180
+ const noteTextComputation = createNoteText(this, definition.noteTextDefinition);
43181
+ let noteText;
43182
+ let label;
43183
+ let hint;
43184
+ switch (noteTextComputation.role) {
43185
+ case "label": {
43186
+ noteText = noteTextComputation.label;
43187
+ label = noteTextComputation.label;
43188
+ hint = createFieldHint(this, definition);
43189
+ break;
43190
+ }
43191
+ case "hint": {
43192
+ noteText = noteTextComputation.hint;
43193
+ label = createNodeLabel(this, definition);
43194
+ hint = noteTextComputation.hint;
43195
+ break;
43196
+ }
43197
+ default:
43198
+ throw new UnreachableError$1(noteTextComputation);
43199
+ }
43200
+ const state = createSharedNodeState(
43201
+ this.scope,
43202
+ {
43203
+ reference: this.contextReference,
43204
+ readonly: isReadonly,
43205
+ relevant: this.isRelevant,
43206
+ required: this.isRequired,
43207
+ label,
43208
+ hint,
43209
+ noteText,
43210
+ children: null,
43211
+ valueOptions: null,
43212
+ value: this.valueState,
43213
+ instanceValue: this.getInstanceValue
43214
+ },
43215
+ this.instanceConfig
43216
+ );
43217
+ this.state = state;
43218
+ this.engineState = state.engineState;
43219
+ this.currentState = state.currentState;
43220
+ }
43221
+ }
43222
+
43223
+ class RangeCodec extends ValueCodec {
43224
+ constructor(baseCodec, definition) {
43225
+ const { valueType, bounds } = definition;
43226
+ const { min, max } = bounds;
43227
+ const toComparableValue = (value) => {
43228
+ if (value == null) {
43229
+ return value;
43230
+ }
43231
+ if (typeof value === "string") {
43232
+ return baseCodec.decodeValue(value);
43233
+ }
43234
+ return value;
43235
+ };
43236
+ const assertBounds = (value) => {
43237
+ const comparableValue = toComparableValue(value);
43238
+ if (comparableValue == null) {
43239
+ return;
43240
+ }
43241
+ if (comparableValue < min || comparableValue > max) {
43242
+ throw new ValueTypeInvariantError(
43243
+ valueType,
43244
+ `Expected value to be within bounds [${min}, ${max}]. Got: ${value}`
43245
+ );
43246
+ }
43247
+ };
43248
+ const encodeValue = (value) => {
43249
+ assertBounds(value);
43250
+ return baseCodec.encodeValue(value);
42828
43251
  };
43252
+ const decodeValue = (value) => {
43253
+ const decoded = baseCodec.decodeValue(value);
43254
+ assertBounds(value);
43255
+ return decoded;
43256
+ };
43257
+ super(valueType, encodeValue, decodeValue);
43258
+ }
43259
+ }
43260
+
43261
+ class RangeControl extends ValueNode {
43262
+ static from(parent, instanceNode, definition) {
43263
+ return new this(parent, instanceNode, definition);
43264
+ }
43265
+ // XFormsXPathElement
43266
+ [XPathNodeKindKey] = "element";
43267
+ // InstanceNode
43268
+ state;
43269
+ engineState;
43270
+ // RangeNode
43271
+ nodeType = "range";
43272
+ appearances;
43273
+ nodeOptions = null;
43274
+ currentState;
43275
+ constructor(parent, instanceNode, definition) {
43276
+ const baseCodec = getSharedValueCodec(definition.valueType);
43277
+ const codec = new RangeCodec(baseCodec, definition);
43278
+ super(parent, instanceNode, definition, codec);
43279
+ this.appearances = definition.bodyElement.appearances;
42829
43280
  const state = createSharedNodeState(
42830
43281
  this.scope,
42831
43282
  {
@@ -42840,7 +43291,7 @@ class InputControl extends ValueNode {
42840
43291
  value: this.valueState,
42841
43292
  instanceValue: this.getInstanceValue
42842
43293
  },
42843
- sharedStateOptions
43294
+ this.instanceConfig
42844
43295
  );
42845
43296
  this.state = state;
42846
43297
  this.engineState = state.engineState;
@@ -42852,238 +43303,217 @@ class InputControl extends ValueNode {
42852
43303
  }
42853
43304
  }
42854
43305
 
42855
- class ModelValue extends ValueNode {
42856
- static from(parent, definition) {
42857
- return new this(parent, definition);
43306
+ class RankMissingValueError extends Error {
43307
+ constructor(message) {
43308
+ super(message);
42858
43309
  }
42859
- // XFormsXPathElement
42860
- [XPathNodeKindKey] = "element";
42861
- // InstanceNode
42862
- state;
42863
- engineState;
42864
- // ModelValueNode
42865
- nodeType = "model-value";
42866
- appearances = null;
42867
- nodeOptions = null;
42868
- currentState;
42869
- constructor(parent, definition) {
42870
- const codec = getSharedValueCodec(definition.valueType);
42871
- super(parent, definition, codec);
42872
- const sharedStateOptions = {
42873
- clientStateFactory: this.engineConfig.stateFactory
42874
- };
42875
- const state = createSharedNodeState(
42876
- this.scope,
42877
- {
42878
- reference: this.contextReference,
42879
- readonly: this.isReadonly,
42880
- relevant: this.isRelevant,
42881
- required: this.isRequired,
42882
- label: null,
42883
- hint: null,
42884
- children: null,
42885
- valueOptions: null,
42886
- value: this.valueState,
42887
- instanceValue: this.getInstanceValue
42888
- },
42889
- sharedStateOptions
43310
+ }
43311
+
43312
+ class XFormsSpecViolationError extends Error {
43313
+ }
43314
+
43315
+ class RankValueTypeError extends XFormsSpecViolationError {
43316
+ constructor(definition) {
43317
+ const { valueType } = definition;
43318
+ super(
43319
+ `Ranks of type ${valueType} are not currently supported. If this functionality would be useful for your form, your feedback is welcome!`
42890
43320
  );
42891
- this.state = state;
42892
- this.engineState = state.engineState;
42893
- this.currentState = state.currentState;
42894
43321
  }
42895
43322
  }
42896
43323
 
42897
- class NoteCodec extends ValueCodec {
43324
+ class ValueArrayCodec extends ValueCodec {
43325
+ decodeItemValue;
43326
+ constructor(baseCodec, encodeValue, decodeValue) {
43327
+ const decodeItemValue = (value) => {
43328
+ const decoded = baseCodec.decodeValue(value);
43329
+ if (decoded == null) {
43330
+ throw new ErrorProductionDesignPendingError(
43331
+ `Failed to decode item value: ${JSON.stringify(value)}`
43332
+ );
43333
+ }
43334
+ return decoded;
43335
+ };
43336
+ super(baseCodec.valueType, encodeValue, decodeValue);
43337
+ this.decodeItemValue = decodeItemValue;
43338
+ }
43339
+ }
43340
+
43341
+ class BaseItemCodec extends ValueArrayCodec {
43342
+ constructor(baseCodec, encodeValue, decodeValue) {
43343
+ super(baseCodec, encodeValue, decodeValue);
43344
+ }
43345
+ }
43346
+
43347
+ class MultipleValueItemCodec extends BaseItemCodec {
42898
43348
  constructor(baseCodec) {
42899
43349
  const encodeValue = (value) => {
42900
- return baseCodec.encodeValue(value ?? "");
43350
+ return value.join(" ");
42901
43351
  };
42902
43352
  const decodeValue = (value) => {
42903
- return value === "" ? null : baseCodec.decodeValue(value);
43353
+ return xmlXPathWhitespaceSeparatedList(value, {
43354
+ ignoreEmpty: true
43355
+ });
42904
43356
  };
42905
- super(baseCodec.valueType, encodeValue, decodeValue);
43357
+ super(baseCodec, encodeValue, decodeValue);
42906
43358
  }
42907
43359
  }
42908
43360
 
42909
- const noteCodecs = {
42910
- string: new NoteCodec(sharedValueCodecs.string),
42911
- int: new NoteCodec(sharedValueCodecs.int),
42912
- decimal: new NoteCodec(sharedValueCodecs.decimal),
42913
- boolean: new NoteCodec(sharedValueCodecs.boolean),
42914
- date: new NoteCodec(sharedValueCodecs.date),
42915
- time: new NoteCodec(sharedValueCodecs.time),
42916
- dateTime: new NoteCodec(sharedValueCodecs.dateTime),
42917
- geopoint: new NoteCodec(sharedValueCodecs.geopoint),
42918
- geotrace: new NoteCodec(sharedValueCodecs.geotrace),
42919
- geoshape: new NoteCodec(sharedValueCodecs.geoshape),
42920
- binary: new NoteCodec(sharedValueCodecs.binary),
42921
- barcode: new NoteCodec(sharedValueCodecs.barcode),
42922
- intent: new NoteCodec(sharedValueCodecs.intent)
43361
+ const derivedItemLabel = (context, value) => {
43362
+ const chunk = new TextChunk(context, "literal", value);
43363
+ return new TextRange("form-derived", "item-label", [chunk]);
42923
43364
  };
42924
- const getNoteCodec = (valueType) => {
42925
- return noteCodecs[valueType];
43365
+ const createItemLabel = (context, definition) => {
43366
+ const { label, value } = definition;
43367
+ if (label == null) {
43368
+ return () => derivedItemLabel(context, value);
43369
+ }
43370
+ return createTextRange(context, "item-label", label);
42926
43371
  };
42927
-
42928
- const createNoteReadonlyThunk = (context, definition) => {
42929
- definition.bodyElement;
42930
- const { readonly } = definition.bind;
42931
- if (!readonly.isConstantTruthyExpression()) {
42932
- throw new Error("Expected a static readonly expression");
43372
+ const createTranslatedStaticItems = (control, items) => {
43373
+ return control.scope.runTask(() => {
43374
+ const labeledItems = items.map((item) => {
43375
+ const { value } = item;
43376
+ const label = createItemLabel(control, item);
43377
+ return () => ({
43378
+ value,
43379
+ label: label()
43380
+ });
43381
+ });
43382
+ return createMemo(() => {
43383
+ return labeledItems.map((item) => item());
43384
+ });
43385
+ });
43386
+ };
43387
+ class ItemsetItemEvaluationContext {
43388
+ constructor(control, contextNode) {
43389
+ this.contextNode = contextNode;
43390
+ this.isAttached = control.isAttached;
43391
+ this.scope = control.scope;
43392
+ this.evaluator = control.evaluator;
43393
+ this.contextReference = control.contextReference;
43394
+ this.getActiveLanguage = control.getActiveLanguage;
42933
43395
  }
42934
- let result = true;
42935
- return () => result;
43396
+ isAttached;
43397
+ scope;
43398
+ evaluator;
43399
+ contextReference;
43400
+ getActiveLanguage;
43401
+ }
43402
+ const createItemsetItemLabel = (context, definition, itemValue) => {
43403
+ const { label } = definition;
43404
+ if (label == null) {
43405
+ return createMemo(() => {
43406
+ return derivedItemLabel(context, itemValue());
43407
+ });
43408
+ }
43409
+ return createTextRange(context, "item-label", label);
42936
43410
  };
42937
-
42938
- const createNoteText = (context, noteTextDefinition) => {
42939
- const { scope } = context;
42940
- const { role } = noteTextDefinition;
42941
- return scope.runTask(() => {
42942
- switch (role) {
42943
- case "label": {
42944
- const label = createTextRange(context, role, noteTextDefinition);
42945
- return {
42946
- role,
42947
- label,
42948
- hint: null
42949
- };
42950
- }
42951
- case "hint": {
42952
- const hint = createTextRange(context, role, noteTextDefinition);
43411
+ const createItemsetItems = (control, itemset) => {
43412
+ return control.scope.runTask(() => {
43413
+ const itemNodes = createComputedExpression(control, itemset.nodes, {
43414
+ defaultValue: []
43415
+ });
43416
+ const itemsCache = new UpsertableMap();
43417
+ return createMemo(() => {
43418
+ return itemNodes().map((itemNode) => {
43419
+ return itemsCache.upsert(itemNode, () => {
43420
+ const context = new ItemsetItemEvaluationContext(control, itemNode);
43421
+ const value = createComputedExpression(context, itemset.value, {
43422
+ defaultValue: ""
43423
+ });
43424
+ const label = createItemsetItemLabel(context, itemset, value);
43425
+ return {
43426
+ label,
43427
+ value
43428
+ };
43429
+ });
43430
+ });
43431
+ });
43432
+ });
43433
+ };
43434
+ const createItemset = (control, itemset) => {
43435
+ return control.scope.runTask(() => {
43436
+ const itemsetItems = createItemsetItems(control, itemset);
43437
+ return createMemo(() => {
43438
+ return itemsetItems().map((item) => {
42953
43439
  return {
42954
- role,
42955
- label: null,
42956
- hint
43440
+ label: item.label(),
43441
+ value: item.value()
42957
43442
  };
42958
- }
42959
- default:
42960
- throw new UnreachableError$1(noteTextDefinition);
42961
- }
43443
+ });
43444
+ });
42962
43445
  });
42963
43446
  };
42964
-
42965
- class Note extends ValueNode {
42966
- // XFormsXPathElement
42967
- [XPathNodeKindKey] = "element";
42968
- // InstanceNode
42969
- state;
42970
- engineState;
42971
- // NoteNode
42972
- nodeType = "note";
42973
- appearances;
42974
- nodeOptions = null;
42975
- currentState;
42976
- constructor(parent, definition) {
42977
- const codec = getNoteCodec(definition.valueType);
42978
- super(parent, definition, codec);
42979
- this.appearances = definition.bodyElement.appearances;
42980
- const sharedStateOptions = {
42981
- clientStateFactory: this.engineConfig.stateFactory
42982
- };
42983
- const isReadonly = createNoteReadonlyThunk(this, definition);
42984
- const noteTextComputation = createNoteText(this, definition.noteTextDefinition);
42985
- let noteText;
42986
- let label;
42987
- let hint;
42988
- switch (noteTextComputation.role) {
42989
- case "label": {
42990
- noteText = noteTextComputation.label;
42991
- label = noteTextComputation.label;
42992
- hint = createFieldHint(this, definition);
42993
- break;
42994
- }
42995
- case "hint": {
42996
- noteText = noteTextComputation.hint;
42997
- label = createNodeLabel(this, definition);
42998
- hint = noteTextComputation.hint;
42999
- break;
43000
- }
43001
- default:
43002
- throw new UnreachableError$1(noteTextComputation);
43003
- }
43004
- const state = createSharedNodeState(
43005
- this.scope,
43006
- {
43007
- reference: this.contextReference,
43008
- readonly: isReadonly,
43009
- relevant: this.isRelevant,
43010
- required: this.isRequired,
43011
- label,
43012
- hint,
43013
- noteText,
43014
- children: null,
43015
- valueOptions: null,
43016
- value: this.valueState,
43017
- instanceValue: this.getInstanceValue
43018
- },
43019
- sharedStateOptions
43020
- );
43021
- this.state = state;
43022
- this.engineState = state.engineState;
43023
- this.currentState = state.currentState;
43447
+ const createItemCollection = (control) => {
43448
+ const { items, itemset } = control.definition.bodyElement;
43449
+ if (itemset != null) {
43450
+ return createItemset(control, itemset);
43024
43451
  }
43025
- }
43452
+ return createTranslatedStaticItems(control, items);
43453
+ };
43026
43454
 
43027
- class RangeCodec extends ValueCodec {
43028
- constructor(baseCodec, definition) {
43029
- const { valueType, bounds } = definition;
43030
- const { min, max } = bounds;
43031
- const toComparableValue = (value) => {
43032
- if (value == null) {
43033
- return value;
43034
- }
43035
- if (typeof value === "string") {
43036
- return baseCodec.decodeValue(value);
43037
- }
43038
- return value;
43039
- };
43040
- const assertBounds = (value) => {
43041
- const comparableValue = toComparableValue(value);
43042
- if (comparableValue == null) {
43043
- return;
43044
- }
43045
- if (comparableValue < min || comparableValue > max) {
43046
- throw new ValueTypeInvariantError(
43047
- valueType,
43048
- `Expected value to be within bounds [${min}, ${max}]. Got: ${value}`
43049
- );
43050
- }
43051
- };
43052
- const encodeValue = (value) => {
43053
- assertBounds(value);
43054
- return baseCodec.encodeValue(value);
43055
- };
43056
- const decodeValue = (value) => {
43057
- const decoded = baseCodec.decodeValue(value);
43058
- assertBounds(value);
43059
- return decoded;
43060
- };
43061
- super(valueType, encodeValue, decodeValue);
43455
+ const assertRankNodeDefinition = (definition) => {
43456
+ if (definition.valueType !== "string") {
43457
+ throw new RankValueTypeError(definition);
43062
43458
  }
43063
- }
43064
-
43065
- class RangeControl extends ValueNode {
43066
- static from(parent, definition) {
43067
- return new this(parent, definition);
43459
+ };
43460
+ const isBlankValueState = (values) => {
43461
+ return values.length === 0;
43462
+ };
43463
+ class RankControl extends ValueNode {
43464
+ static from(parent, instanceNode, definition) {
43465
+ assertRankNodeDefinition(definition);
43466
+ return new this(parent, instanceNode, definition);
43068
43467
  }
43468
+ mapOptionsByValue;
43469
+ getInstanceValue;
43069
43470
  // XFormsXPathElement
43070
43471
  [XPathNodeKindKey] = "element";
43071
43472
  // InstanceNode
43072
43473
  state;
43073
43474
  engineState;
43074
- // RangeNode
43075
- nodeType = "range";
43475
+ // RankNode
43476
+ nodeType = "rank";
43076
43477
  appearances;
43077
43478
  nodeOptions = null;
43078
43479
  currentState;
43079
- constructor(parent, definition) {
43080
- const baseCodec = getSharedValueCodec(definition.valueType);
43081
- const codec = new RangeCodec(baseCodec, definition);
43082
- super(parent, definition, codec);
43480
+ constructor(parent, instanceNode, definition) {
43481
+ const codec = new MultipleValueItemCodec(sharedValueCodecs.string);
43482
+ super(parent, instanceNode, definition, codec);
43083
43483
  this.appearances = definition.bodyElement.appearances;
43084
- const sharedStateOptions = {
43085
- clientStateFactory: this.engineConfig.stateFactory
43086
- };
43484
+ const valueOptions = createItemCollection(this);
43485
+ const mapOptionsByValue = this.scope.runTask(() => {
43486
+ return createMemo(() => {
43487
+ return new Map(valueOptions().map((item) => [item.value, item]));
43488
+ });
43489
+ });
43490
+ this.mapOptionsByValue = mapOptionsByValue;
43491
+ const baseValueState = this.valueState;
43492
+ const [baseGetValue, setValue] = baseValueState;
43493
+ const getValue = this.scope.runTask(() => {
43494
+ return createMemo(() => {
43495
+ const options = valueOptions();
43496
+ const values = baseGetValue();
43497
+ if (isBlankValueState(values)) {
43498
+ return values;
43499
+ }
43500
+ const optionValues = new Set(options.map((option) => option.value));
43501
+ const currentValues = values.filter((value) => optionValues.has(value));
43502
+ return Array.from(
43503
+ /* @__PURE__ */ new Set([
43504
+ ...currentValues,
43505
+ /**
43506
+ * @see {@link getValue} ToDo paragraph 1.
43507
+ */
43508
+ ...optionValues
43509
+ ])
43510
+ );
43511
+ });
43512
+ });
43513
+ const valueState = [getValue, setValue];
43514
+ this.getInstanceValue = this.scope.runTask(() => {
43515
+ return createMemo(() => codec.encodeValue(getValue()));
43516
+ });
43087
43517
  const state = createSharedNodeState(
43088
43518
  this.scope,
43089
43519
  {
@@ -43094,18 +43524,31 @@ class RangeControl extends ValueNode {
43094
43524
  label: createNodeLabel(this, definition),
43095
43525
  hint: createFieldHint(this, definition),
43096
43526
  children: null,
43097
- valueOptions: null,
43098
- value: this.valueState,
43527
+ valueOptions,
43528
+ value: valueState,
43099
43529
  instanceValue: this.getInstanceValue
43100
43530
  },
43101
- sharedStateOptions
43531
+ this.instanceConfig
43102
43532
  );
43103
43533
  this.state = state;
43104
43534
  this.engineState = state.engineState;
43105
43535
  this.currentState = state.currentState;
43106
43536
  }
43107
- setValue(value) {
43108
- this.setValueState(value);
43537
+ getValueLabel(value) {
43538
+ const valueOption = this.currentState.valueOptions.find((item) => item.value === value);
43539
+ return valueOption?.label ?? null;
43540
+ }
43541
+ setValues(valuesInOrder) {
43542
+ if (isBlankValueState(valuesInOrder)) {
43543
+ this.setValueState(valuesInOrder);
43544
+ return this.root;
43545
+ }
43546
+ const sourceValues = Array.from(this.mapOptionsByValue().keys());
43547
+ const hasAllValues = sourceValues.every((sourceValue) => valuesInOrder.includes(sourceValue));
43548
+ if (!hasAllValues) {
43549
+ throw new RankMissingValueError("There are missing options. Rank should have all options.");
43550
+ }
43551
+ this.setValueState(valuesInOrder);
43109
43552
  return this.root;
43110
43553
  }
43111
43554
  }
@@ -43127,24 +43570,49 @@ const insertAtIndex = (currentValues, insertionIndex, newValues) => {
43127
43570
  ];
43128
43571
  };
43129
43572
 
43130
- const createNodeRangeSubmissionState = (node) => {
43573
+ const createNodeRangeInstanceState = (node) => {
43131
43574
  return {
43132
- get submissionXML() {
43575
+ get instanceXML() {
43133
43576
  const serializedChildren = node.currentState.children.map((child) => {
43134
- return child.submissionState.submissionXML;
43577
+ return child.instanceState.instanceXML;
43135
43578
  });
43136
43579
  return serializedChildren.join("");
43137
43580
  }
43138
43581
  };
43139
43582
  };
43140
43583
 
43584
+ class TemplatedNodeAttributeSerializationError extends Error {
43585
+ constructor() {
43586
+ super("Template attribute omission not implemented");
43587
+ }
43588
+ }
43589
+
43590
+ const createTemplatedNodeInstanceState = (node) => {
43591
+ return {
43592
+ get instanceXML() {
43593
+ if (!node.currentState.relevant) {
43594
+ return "";
43595
+ }
43596
+ const serializedChildren = node.currentState.children.map((child) => {
43597
+ return child.instanceState.instanceXML;
43598
+ });
43599
+ const { attributes } = node.currentState;
43600
+ if (attributes != null) {
43601
+ throw new TemplatedNodeAttributeSerializationError();
43602
+ }
43603
+ return serializeParentElementXML(node.definition.qualifiedName, serializedChildren);
43604
+ }
43605
+ };
43606
+ };
43607
+
43141
43608
  class RepeatInstance extends DescendantNode {
43142
- constructor(parent, definition, options) {
43609
+ constructor(parent, instanceNode, options) {
43610
+ const { definition } = parent;
43143
43611
  const { precedingInstance } = options;
43144
43612
  const precedingIndex = precedingInstance?.currentIndex ?? (() => -1);
43145
43613
  const initialIndex = precedingIndex() + 1;
43146
43614
  const [currentIndex, setCurrentIndex] = createSignal(initialIndex);
43147
- super(parent, definition, {
43615
+ super(parent, instanceNode, definition, {
43148
43616
  computeReference: () => {
43149
43617
  const currentPosition = currentIndex() + 1;
43150
43618
  return `${parent.contextReference()}[${currentPosition}]`;
@@ -43155,9 +43623,6 @@ class RepeatInstance extends DescendantNode {
43155
43623
  const childrenState = createChildrenState(this);
43156
43624
  this.childrenState = childrenState;
43157
43625
  this.currentIndex = currentIndex;
43158
- const sharedStateOptions = {
43159
- clientStateFactory: this.engineConfig.stateFactory
43160
- };
43161
43626
  const state = createSharedNodeState(
43162
43627
  this.scope,
43163
43628
  {
@@ -43168,11 +43633,12 @@ class RepeatInstance extends DescendantNode {
43168
43633
  // TODO: only-child <group><label>
43169
43634
  label: createNodeLabel(this, definition),
43170
43635
  hint: null,
43636
+ attributes: null,
43171
43637
  children: childrenState.childIds,
43172
43638
  valueOptions: null,
43173
43639
  value: null
43174
43640
  },
43175
- sharedStateOptions
43641
+ this.instanceConfig
43176
43642
  );
43177
43643
  this.state = state;
43178
43644
  this.engineState = state.engineState;
@@ -43186,8 +43652,8 @@ class RepeatInstance extends DescendantNode {
43186
43652
  createComputed(on(computeCurrentIndex, setCurrentIndex, { defer: true }));
43187
43653
  });
43188
43654
  childrenState.setChildren(buildChildren(this));
43189
- this.validationState = createAggregatedViolations(this, sharedStateOptions);
43190
- this.submissionState = createParentNodeSubmissionState(this);
43655
+ this.validationState = createAggregatedViolations(this, this.instanceConfig);
43656
+ this.instanceState = createTemplatedNodeInstanceState(this);
43191
43657
  }
43192
43658
  childrenState;
43193
43659
  currentIndex;
@@ -43220,7 +43686,7 @@ class RepeatInstance extends DescendantNode {
43220
43686
  nodeOptions = null;
43221
43687
  currentState;
43222
43688
  validationState;
43223
- submissionState;
43689
+ instanceState;
43224
43690
  getChildren() {
43225
43691
  return this.childrenState.getChildren();
43226
43692
  }
@@ -43262,15 +43728,12 @@ class BaseRepeatRange extends DescendantNode {
43262
43728
  engineState;
43263
43729
  nodeOptions = null;
43264
43730
  currentState;
43265
- submissionState;
43731
+ instanceState;
43266
43732
  constructor(parent, definition) {
43267
- super(parent, definition);
43733
+ super(parent, definition.template, definition);
43268
43734
  const repeatRange = this;
43269
43735
  const childrenState = createChildrenState(repeatRange);
43270
43736
  this.childrenState = childrenState;
43271
- const sharedStateOptions = {
43272
- clientStateFactory: this.engineConfig.stateFactory
43273
- };
43274
43737
  const state = createSharedNodeState(
43275
43738
  this.scope,
43276
43739
  {
@@ -43284,7 +43747,7 @@ class BaseRepeatRange extends DescendantNode {
43284
43747
  valueOptions: null,
43285
43748
  value: null
43286
43749
  },
43287
- sharedStateOptions
43750
+ this.instanceConfig
43288
43751
  );
43289
43752
  this.state = state;
43290
43753
  this.engineState = state.engineState;
@@ -43293,7 +43756,7 @@ class BaseRepeatRange extends DescendantNode {
43293
43756
  state.currentState,
43294
43757
  childrenState
43295
43758
  );
43296
- this.submissionState = createNodeRangeSubmissionState(this);
43759
+ this.instanceState = createNodeRangeInstanceState(this);
43297
43760
  }
43298
43761
  getLastIndex() {
43299
43762
  return this.engineState.children.length - 1;
@@ -43301,7 +43764,7 @@ class BaseRepeatRange extends DescendantNode {
43301
43764
  getInstanceIndex(instance) {
43302
43765
  return this.engineState.children.indexOf(instance.nodeId);
43303
43766
  }
43304
- createChildren(afterIndex, definitions) {
43767
+ createChildren(afterIndex, instanceNodes) {
43305
43768
  return this.scope.runTask(() => {
43306
43769
  let initialPrecedingInstance;
43307
43770
  if (afterIndex === -1) {
@@ -43314,7 +43777,7 @@ class BaseRepeatRange extends DescendantNode {
43314
43777
  initialPrecedingInstance = instance;
43315
43778
  }
43316
43779
  const repeatRange = this;
43317
- return definitions.reduce((acc, definition) => {
43780
+ return instanceNodes.reduce((acc, definition) => {
43318
43781
  const precedingInstance = acc[acc.length - 1] ?? initialPrecedingInstance;
43319
43782
  const newInstance = new RepeatInstance(repeatRange, definition, {
43320
43783
  precedingInstance
@@ -43324,10 +43787,10 @@ class BaseRepeatRange extends DescendantNode {
43324
43787
  }, []);
43325
43788
  });
43326
43789
  }
43327
- addChildren(afterIndex, definitions) {
43790
+ addChildren(afterIndex, instanceNodes) {
43328
43791
  return this.scope.runTask(() => {
43329
43792
  const initialIndex = afterIndex + 1;
43330
- const newInstances = this.createChildren(afterIndex, definitions);
43793
+ const newInstances = this.createChildren(afterIndex, instanceNodes);
43331
43794
  return this.childrenState.setChildren((currentInstances) => {
43332
43795
  return insertAtIndex(currentInstances, initialIndex, newInstances);
43333
43796
  });
@@ -43351,25 +43814,33 @@ class BaseRepeatRange extends DescendantNode {
43351
43814
  }
43352
43815
 
43353
43816
  class RepeatRangeControlled extends BaseRepeatRange {
43817
+ isInstanceCreation;
43818
+ countControlType;
43354
43819
  // RepeatRangeControlledNode
43355
43820
  nodeType = "repeat-range:controlled";
43356
43821
  appearances;
43357
43822
  validationState;
43358
- constructor(parent, definition) {
43823
+ constructor(parent, instanceNodes, definition) {
43359
43824
  super(parent, definition);
43825
+ this.isInstanceCreation = parent.rootDocument.initializationMode === "create";
43360
43826
  this.appearances = definition.bodyElement.appearances;
43361
- this.initializeControlledChildrenState(definition);
43362
- this.validationState = createAggregatedViolations(this, {
43363
- clientStateFactory: this.engineConfig.stateFactory
43364
- });
43827
+ if (definition.count.isConstantExpression()) {
43828
+ this.countControlType = "REPEAT_COUNT_CONTROL_FIXED";
43829
+ } else {
43830
+ this.countControlType = "REPEAT_COUNT_CONTROL_DYNAMIC";
43831
+ }
43832
+ this.initializeControlledChildrenState(definition, instanceNodes);
43833
+ this.validationState = createAggregatedViolations(this, this.instanceConfig);
43365
43834
  }
43366
- initializeControlledChildrenState(definition) {
43835
+ initializeControlledChildrenState(definition, instanceNodes) {
43367
43836
  this.scope.runTask(() => {
43368
- const { count, instances, template } = definition;
43837
+ const { count, template } = definition;
43838
+ const repeatInstanceNodes = definition.omitTemplate(instanceNodes);
43369
43839
  const computeCount = createComputedExpression(this, count, {
43370
43840
  defaultValue: 0
43371
43841
  });
43372
- createComputed((previousCount) => {
43842
+ createComputed((previousCountComputation = null) => {
43843
+ const previousCount = previousCountComputation ?? 0;
43373
43844
  let currentCount = computeCount();
43374
43845
  if (Number.isFinite(currentCount) && currentCount < 0) {
43375
43846
  currentCount = 0;
@@ -43382,21 +43853,57 @@ class RepeatRangeControlled extends BaseRepeatRange {
43382
43853
  Number.isNaN(currentCount)) {
43383
43854
  return previousCount;
43384
43855
  }
43856
+ this.warnDroppedInstanceNodes(repeatInstanceNodes, previousCountComputation, currentCount);
43385
43857
  if (currentCount > previousCount) {
43386
43858
  const delta = currentCount - previousCount;
43387
- const definitions = Array(delta).fill(template).map((baseDefinition, index) => {
43859
+ const inputNodes = Array(delta).fill(null).map((_, index) => {
43388
43860
  const instanceIndex = previousCount + index;
43389
- return instances[instanceIndex] ?? baseDefinition;
43861
+ return repeatInstanceNodes[instanceIndex] ?? template;
43390
43862
  });
43391
- this.addChildren(previousCount - 1, definitions);
43863
+ this.addChildren(previousCount - 1, inputNodes);
43392
43864
  } else {
43393
43865
  const delta = previousCount - currentCount;
43394
43866
  this.removeChildren(currentCount, delta);
43395
43867
  }
43396
43868
  return currentCount;
43397
- }, 0);
43869
+ });
43398
43870
  });
43399
43871
  }
43872
+ /**
43873
+ * Note: we do not warn about dropping "excess" repeat instances...
43874
+ *
43875
+ * - ... after the instance is fully loaded: dropping repeat instances after
43876
+ * that point is an expected behavior of _dynamically-controlled repeats_
43877
+ * (i.e. those defined with `jr:count`), and it does not occur for
43878
+ * _fixed-controlled repeats_ (i.e. defined with `jr:noAddRemove`);
43879
+ *
43880
+ * - ... for dynamic (`jr:count`) repeats _in newly created form instances_, i.e. instances whose input instance
43881
+ * nodes are derived from the _form definition_: those form-defined repeat
43882
+ * instances are not "dropped", instead they're retained in memory to be
43883
+ * added as count is recomputed to a larger value.
43884
+ *
43885
+ * @todo ^ it is pretty likely that retain-in-memory behavior also applies to
43886
+ * instance state which has been serialized and restored as well! In this way,
43887
+ * it's possible we're not really "dropping" those nodes, only deferring their
43888
+ * restoration. Is this what we want?
43889
+ *
43890
+ * @todo Design and produce {@link LoadFormWarnings | warnings} as values!
43891
+ */
43892
+ warnDroppedInstanceNodes(repeatInstanceNodes, previousCountComputation, initialCount) {
43893
+ if (this.isInstanceCreation) {
43894
+ return;
43895
+ }
43896
+ const isInstanceLoad = previousCountComputation == null;
43897
+ if (isInstanceLoad && this.countControlType === "REPEAT_COUNT_CONTROL_DYNAMIC") {
43898
+ return;
43899
+ }
43900
+ const droppedCount = repeatInstanceNodes.length - initialCount;
43901
+ if (droppedCount > 0) {
43902
+ console.warn(
43903
+ `Dropped ${droppedCount} repeat instances for repeat range ${this.contextReference()}`
43904
+ );
43905
+ }
43906
+ }
43400
43907
  }
43401
43908
 
43402
43909
  class RepeatRangeUncontrolled extends BaseRepeatRange {
@@ -43404,13 +43911,11 @@ class RepeatRangeUncontrolled extends BaseRepeatRange {
43404
43911
  nodeType = "repeat-range:uncontrolled";
43405
43912
  appearances;
43406
43913
  validationState;
43407
- constructor(parent, definition) {
43914
+ constructor(parent, instanceNodes, definition) {
43408
43915
  super(parent, definition);
43409
43916
  this.appearances = definition.bodyElement.appearances;
43410
- this.addChildren(-1, definition.instances);
43411
- this.validationState = createAggregatedViolations(this, {
43412
- clientStateFactory: this.engineConfig.stateFactory
43413
- });
43917
+ this.addChildren(-1, definition.omitTemplate(instanceNodes));
43918
+ this.validationState = createAggregatedViolations(this, this.instanceConfig);
43414
43919
  }
43415
43920
  // RepeatRangeUncontrolledNode
43416
43921
  addInstances(afterIndex = this.getLastIndex(), count = 1) {
@@ -43441,9 +43946,6 @@ class RepeatRangeUncontrolled extends BaseRepeatRange {
43441
43946
  }
43442
43947
  }
43443
43948
 
43444
- class XFormsSpecViolationError extends Error {
43445
- }
43446
-
43447
43949
  class SelectValueTypeError extends XFormsSpecViolationError {
43448
43950
  constructor(definition) {
43449
43951
  const { valueType } = definition;
@@ -43453,43 +43955,6 @@ class SelectValueTypeError extends XFormsSpecViolationError {
43453
43955
  }
43454
43956
  }
43455
43957
 
43456
- class ValueArrayCodec extends ValueCodec {
43457
- decodeItemValue;
43458
- constructor(baseCodec, encodeValue, decodeValue) {
43459
- const decodeItemValue = (value) => {
43460
- const decoded = baseCodec.decodeValue(value);
43461
- if (decoded == null) {
43462
- throw new ErrorProductionDesignPendingError(
43463
- `Failed to decode item value: ${JSON.stringify(value)}`
43464
- );
43465
- }
43466
- return decoded;
43467
- };
43468
- super(baseCodec.valueType, encodeValue, decodeValue);
43469
- this.decodeItemValue = decodeItemValue;
43470
- }
43471
- }
43472
-
43473
- class BaseItemCodec extends ValueArrayCodec {
43474
- constructor(baseCodec, encodeValue, decodeValue) {
43475
- super(baseCodec, encodeValue, decodeValue);
43476
- }
43477
- }
43478
-
43479
- class MultipleValueItemCodec extends BaseItemCodec {
43480
- constructor(baseCodec) {
43481
- const encodeValue = (value) => {
43482
- return value.join(" ");
43483
- };
43484
- const decodeValue = (value) => {
43485
- return xmlXPathWhitespaceSeparatedList(value, {
43486
- ignoreEmpty: true
43487
- });
43488
- };
43489
- super(baseCodec, encodeValue, decodeValue);
43490
- }
43491
- }
43492
-
43493
43958
  const encodeValueFactory = (baseCodec) => {
43494
43959
  return (values) => {
43495
43960
  const [value] = values;
@@ -43525,109 +43990,15 @@ const getSelectCodec = (definition) => {
43525
43990
  }
43526
43991
  };
43527
43992
 
43528
- const derivedItemLabel = (context, value) => {
43529
- const chunk = new TextChunk(context, "literal", value);
43530
- return new TextRange("form-derived", "item-label", [chunk]);
43531
- };
43532
- const createItemLabel = (context, definition) => {
43533
- const { label, value } = definition;
43534
- if (label == null) {
43535
- return () => derivedItemLabel(context, value);
43536
- }
43537
- return createTextRange(context, "item-label", label);
43538
- };
43539
- const createTranslatedStaticItems = (control, items) => {
43540
- return control.scope.runTask(() => {
43541
- const labeledItems = items.map((item) => {
43542
- const { value } = item;
43543
- const label = createItemLabel(control, item);
43544
- return () => ({
43545
- value,
43546
- label: label()
43547
- });
43548
- });
43549
- return createMemo(() => {
43550
- return labeledItems.map((item) => item());
43551
- });
43552
- });
43553
- };
43554
- class ItemsetItemEvaluationContext {
43555
- constructor(control, contextNode) {
43556
- this.contextNode = contextNode;
43557
- this.isAttached = control.isAttached;
43558
- this.scope = control.scope;
43559
- this.evaluator = control.evaluator;
43560
- this.contextReference = control.contextReference;
43561
- this.getActiveLanguage = control.getActiveLanguage;
43562
- }
43563
- isAttached;
43564
- scope;
43565
- evaluator;
43566
- contextReference;
43567
- getActiveLanguage;
43568
- }
43569
- const createItemsetItemLabel = (context, definition, itemValue) => {
43570
- const { label } = definition;
43571
- if (label == null) {
43572
- return createMemo(() => {
43573
- return derivedItemLabel(context, itemValue());
43574
- });
43575
- }
43576
- return createTextRange(context, "item-label", label);
43577
- };
43578
- const createItemsetItems = (control, itemset) => {
43579
- return control.scope.runTask(() => {
43580
- const itemNodes = createComputedExpression(control, itemset.nodes, {
43581
- defaultValue: []
43582
- });
43583
- const itemsCache = new UpsertableMap();
43584
- return createMemo(() => {
43585
- return itemNodes().map((itemNode) => {
43586
- return itemsCache.upsert(itemNode, () => {
43587
- const context = new ItemsetItemEvaluationContext(control, itemNode);
43588
- const value = createComputedExpression(context, itemset.value, {
43589
- defaultValue: ""
43590
- });
43591
- const label = createItemsetItemLabel(context, itemset, value);
43592
- return {
43593
- label,
43594
- value
43595
- };
43596
- });
43597
- });
43598
- });
43599
- });
43600
- };
43601
- const createItemset = (control, itemset) => {
43602
- return control.scope.runTask(() => {
43603
- const itemsetItems = createItemsetItems(control, itemset);
43604
- return createMemo(() => {
43605
- return itemsetItems().map((item) => {
43606
- return {
43607
- label: item.label(),
43608
- value: item.value()
43609
- };
43610
- });
43611
- });
43612
- });
43613
- };
43614
- const createItemCollection = (control) => {
43615
- const { items, itemset } = control.definition.bodyElement;
43616
- if (itemset != null) {
43617
- return createItemset(control, itemset);
43618
- }
43619
- return createTranslatedStaticItems(control, items);
43620
- };
43621
-
43622
43993
  const assertSupportedSelectValueType = (definition) => {
43623
43994
  if (definition.valueType !== "string") {
43624
43995
  throw new SelectValueTypeError(definition);
43625
43996
  }
43626
43997
  };
43627
43998
  class SelectControl extends ValueNode {
43628
- static from(parent, definition) {
43999
+ static from(parent, instanceNode, definition) {
43629
44000
  assertSupportedSelectValueType(definition);
43630
- return new this(parent, definition);
44001
+ return new this(parent, instanceNode, definition);
43631
44002
  }
43632
44003
  mapOptionsByValue;
43633
44004
  getInstanceValue;
@@ -43642,9 +44013,9 @@ class SelectControl extends ValueNode {
43642
44013
  appearances;
43643
44014
  nodeOptions = null;
43644
44015
  currentState;
43645
- constructor(parent, definition) {
44016
+ constructor(parent, instanceNode, definition) {
43646
44017
  const codec = getSelectCodec(definition);
43647
- super(parent, definition, codec);
44018
+ super(parent, instanceNode, definition, codec);
43648
44019
  this.appearances = definition.bodyElement.appearances;
43649
44020
  this.selectType = definition.bodyElement.type;
43650
44021
  const valueOptions = createItemCollection(this);
@@ -43670,9 +44041,6 @@ class SelectControl extends ValueNode {
43670
44041
  return codec.encodeValue(getValue());
43671
44042
  });
43672
44043
  });
43673
- const sharedStateOptions = {
43674
- clientStateFactory: this.engineConfig.stateFactory
43675
- };
43676
44044
  const state = createSharedNodeState(
43677
44045
  this.scope,
43678
44046
  {
@@ -43687,7 +44055,7 @@ class SelectControl extends ValueNode {
43687
44055
  value: valueState,
43688
44056
  instanceValue: this.getInstanceValue
43689
44057
  },
43690
- sharedStateOptions
44058
+ this.instanceConfig
43691
44059
  );
43692
44060
  this.state = state;
43693
44061
  this.engineState = state.engineState;
@@ -43757,14 +44125,11 @@ class Subtree extends DescendantNode {
43757
44125
  nodeOptions = null;
43758
44126
  currentState;
43759
44127
  validationState;
43760
- submissionState;
43761
- constructor(parent, definition) {
43762
- super(parent, definition);
44128
+ instanceState;
44129
+ constructor(parent, instanceNode, definition) {
44130
+ super(parent, instanceNode, definition);
43763
44131
  const childrenState = createChildrenState(this);
43764
44132
  this.childrenState = childrenState;
43765
- const sharedStateOptions = {
43766
- clientStateFactory: this.engineConfig.stateFactory
43767
- };
43768
44133
  const state = createSharedNodeState(
43769
44134
  this.scope,
43770
44135
  {
@@ -43778,7 +44143,7 @@ class Subtree extends DescendantNode {
43778
44143
  valueOptions: null,
43779
44144
  value: null
43780
44145
  },
43781
- sharedStateOptions
44146
+ this.instanceConfig
43782
44147
  );
43783
44148
  this.state = state;
43784
44149
  this.engineState = state.engineState;
@@ -43788,8 +44153,8 @@ class Subtree extends DescendantNode {
43788
44153
  childrenState
43789
44154
  );
43790
44155
  childrenState.setChildren(buildChildren(this));
43791
- this.validationState = createAggregatedViolations(this, sharedStateOptions);
43792
- this.submissionState = createParentNodeSubmissionState(this);
44156
+ this.validationState = createAggregatedViolations(this, this.instanceConfig);
44157
+ this.instanceState = createParentNodeInstanceState(this);
43793
44158
  }
43794
44159
  getChildren() {
43795
44160
  return this.childrenState.getChildren();
@@ -43833,13 +44198,13 @@ class TriggerCodec extends ValueCodec {
43833
44198
 
43834
44199
  const codec = new TriggerCodec();
43835
44200
  class TriggerControl extends ValueNode {
43836
- static from(parent, definition) {
44201
+ static from(parent, instanceNode, definition) {
43837
44202
  if (definition.valueType !== "string") {
43838
44203
  throw new ErrorProductionDesignPendingError(
43839
44204
  `Unsupported trigger value type: ${definition.valueType}`
43840
44205
  );
43841
44206
  }
43842
- return new this(parent, definition);
44207
+ return new this(parent, instanceNode, definition);
43843
44208
  }
43844
44209
  // XFormsXPathElement
43845
44210
  [XPathNodeKindKey] = "element";
@@ -43851,12 +44216,9 @@ class TriggerControl extends ValueNode {
43851
44216
  appearances;
43852
44217
  nodeOptions = null;
43853
44218
  currentState;
43854
- constructor(parent, definition) {
43855
- super(parent, definition, codec);
44219
+ constructor(parent, instanceNode, definition) {
44220
+ super(parent, instanceNode, definition, codec);
43856
44221
  this.appearances = definition.bodyElement.appearances;
43857
- const sharedStateOptions = {
43858
- clientStateFactory: this.engineConfig.stateFactory
43859
- };
43860
44222
  const state = createSharedNodeState(
43861
44223
  this.scope,
43862
44224
  {
@@ -43871,7 +44233,7 @@ class TriggerControl extends ValueNode {
43871
44233
  value: this.valueState,
43872
44234
  instanceValue: this.getInstanceValue
43873
44235
  },
43874
- sharedStateOptions
44236
+ this.instanceConfig
43875
44237
  );
43876
44238
  this.state = state;
43877
44239
  this.engineState = state.engineState;
@@ -43884,86 +44246,59 @@ class TriggerControl extends ValueNode {
43884
44246
  }
43885
44247
  }
43886
44248
 
43887
- class RankMissingValueError extends Error {
43888
- constructor(message) {
43889
- super(message);
44249
+ class TempUnsupportedControlEncodeError extends Error {
44250
+ constructor() {
44251
+ super(`Cannot encode state: not implemented`);
43890
44252
  }
43891
44253
  }
43892
-
43893
- class RankValueTypeError extends XFormsSpecViolationError {
43894
- constructor(definition) {
43895
- const { valueType } = definition;
43896
- super(
43897
- `Ranks of type ${valueType} are not currently supported. If this functionality would be useful for your form, your feedback is welcome!`
43898
- );
44254
+ class TempUnsupportedControlCodec extends ValueCodec {
44255
+ constructor(valueType) {
44256
+ const encodeValue = (input) => {
44257
+ if (typeof input === "string") {
44258
+ return input;
44259
+ }
44260
+ throw new TempUnsupportedControlEncodeError();
44261
+ };
44262
+ const decodeValue = identity;
44263
+ super(valueType, encodeValue, decodeValue);
43899
44264
  }
43900
44265
  }
43901
44266
 
43902
- const assertRankNodeDefinition = (definition) => {
43903
- if (definition.valueType !== "string") {
43904
- throw new RankValueTypeError(definition);
43905
- }
44267
+ const codecs = {
44268
+ string: new TempUnsupportedControlCodec("string"),
44269
+ int: new TempUnsupportedControlCodec("int"),
44270
+ decimal: new TempUnsupportedControlCodec("decimal"),
44271
+ boolean: new TempUnsupportedControlCodec("boolean"),
44272
+ date: new TempUnsupportedControlCodec("date"),
44273
+ time: new TempUnsupportedControlCodec("time"),
44274
+ dateTime: new TempUnsupportedControlCodec("dateTime"),
44275
+ geopoint: new TempUnsupportedControlCodec("geopoint"),
44276
+ geotrace: new TempUnsupportedControlCodec("geotrace"),
44277
+ geoshape: new TempUnsupportedControlCodec("geoshape"),
44278
+ binary: new TempUnsupportedControlCodec("binary"),
44279
+ barcode: new TempUnsupportedControlCodec("barcode"),
44280
+ intent: new TempUnsupportedControlCodec("intent")
43906
44281
  };
43907
- const isBlankValueState = (values) => {
43908
- return values.length === 0;
43909
- };
43910
- class RankControl extends ValueNode {
43911
- static from(parent, definition) {
43912
- assertRankNodeDefinition(definition);
43913
- return new this(parent, definition);
44282
+ class UnsupportedControlWriteError extends Error {
44283
+ constructor(type) {
44284
+ super(`Cannot write state for node (type: ${type}) - not implemented`);
43914
44285
  }
43915
- mapOptionsByValue;
43916
- getInstanceValue;
44286
+ }
44287
+ class UploadControl extends ValueNode {
43917
44288
  // XFormsXPathElement
43918
44289
  [XPathNodeKindKey] = "element";
43919
44290
  // InstanceNode
43920
44291
  state;
43921
44292
  engineState;
43922
- // RankNode
43923
- nodeType = "rank";
44293
+ // UploadNode
44294
+ nodeType = "upload";
43924
44295
  appearances;
43925
44296
  nodeOptions = null;
43926
44297
  currentState;
43927
- constructor(parent, definition) {
43928
- const codec = new MultipleValueItemCodec(sharedValueCodecs.string);
43929
- super(parent, definition, codec);
44298
+ constructor(parent, instanceNode, definition) {
44299
+ const codec = codecs[definition.valueType];
44300
+ super(parent, instanceNode, definition, codec);
43930
44301
  this.appearances = definition.bodyElement.appearances;
43931
- const valueOptions = createItemCollection(this);
43932
- const mapOptionsByValue = this.scope.runTask(() => {
43933
- return createMemo(() => {
43934
- return new Map(valueOptions().map((item) => [item.value, item]));
43935
- });
43936
- });
43937
- this.mapOptionsByValue = mapOptionsByValue;
43938
- const baseValueState = this.valueState;
43939
- const [baseGetValue, setValue] = baseValueState;
43940
- const getValue = this.scope.runTask(() => {
43941
- return createMemo(() => {
43942
- const options = valueOptions();
43943
- const values = baseGetValue();
43944
- if (isBlankValueState(values)) {
43945
- return values;
43946
- }
43947
- const optionValues = new Set(options.map((option) => option.value));
43948
- const currentValues = values.filter((value) => optionValues.has(value));
43949
- return Array.from(
43950
- /* @__PURE__ */ new Set([
43951
- ...currentValues,
43952
- /**
43953
- * @see {@link getValue} ToDo paragraph 1.
43954
- */
43955
- ...optionValues
43956
- ])
43957
- );
43958
- });
43959
- });
43960
- const valueState = [getValue, setValue];
43961
- this.getInstanceValue = this.scope.runTask(() => {
43962
- return createMemo(() => codec.encodeValue(getValue()));
43963
- });
43964
- const sharedStateOptions = {
43965
- clientStateFactory: this.engineConfig.stateFactory
43966
- };
43967
44302
  const state = createSharedNodeState(
43968
44303
  this.scope,
43969
44304
  {
@@ -43974,209 +44309,218 @@ class RankControl extends ValueNode {
43974
44309
  label: createNodeLabel(this, definition),
43975
44310
  hint: createFieldHint(this, definition),
43976
44311
  children: null,
43977
- valueOptions,
43978
- value: valueState,
44312
+ valueOptions: null,
44313
+ value: this.valueState,
43979
44314
  instanceValue: this.getInstanceValue
43980
44315
  },
43981
- sharedStateOptions
44316
+ this.instanceConfig
43982
44317
  );
43983
44318
  this.state = state;
43984
44319
  this.engineState = state.engineState;
43985
44320
  this.currentState = state.currentState;
43986
44321
  }
43987
- getValueLabel(value) {
43988
- const valueOption = this.currentState.valueOptions.find((item) => item.value === value);
43989
- return valueOption?.label ?? null;
43990
- }
43991
- setValues(valuesInOrder) {
43992
- if (isBlankValueState(valuesInOrder)) {
43993
- this.setValueState(valuesInOrder);
43994
- return this.root;
43995
- }
43996
- const sourceValues = Array.from(this.mapOptionsByValue().keys());
43997
- const hasAllValues = sourceValues.every((sourceValue) => valuesInOrder.includes(sourceValue));
43998
- if (!hasAllValues) {
43999
- throw new RankMissingValueError("There are missing options. Rank should have all options.");
44000
- }
44001
- this.setValueState(valuesInOrder);
44002
- return this.root;
44322
+ // UnsupportedControlNode
44323
+ setValue(_) {
44324
+ throw new UnsupportedControlWriteError(this.nodeType);
44003
44325
  }
44004
44326
  }
44005
44327
 
44006
- const createLeafNodeSubmissionState = (node) => {
44007
- return {
44008
- get submissionXML() {
44009
- if (!node.currentState.relevant) {
44010
- return "";
44328
+ const META_LOCAL_NAME = "meta";
44329
+ const INSTANCE_ID_LOCAL_NAME = "instanceID";
44330
+ const DEPRECATED_ID_LOCAL_NAME = "deprecatedID";
44331
+ const META_NAMESPACE_URIS = [OPENROSA_XFORMS_NAMESPACE_URI$1, XFORMS_NAMESPACE_URI$1];
44332
+ const isMetaNamespacedName = (qualifiedName) => {
44333
+ const namespaceURI = qualifiedName.namespaceURI?.href;
44334
+ return namespaceURI != null && META_NAMESPACE_URIS.includes(namespaceURI);
44335
+ };
44336
+ const isMetaName = (qualifiedName) => {
44337
+ return isMetaNamespacedName(qualifiedName) && qualifiedName.localName === META_LOCAL_NAME;
44338
+ };
44339
+ const isDirectRootDescendant = (parent) => {
44340
+ return parent.parent === parent.root;
44341
+ };
44342
+ const isBaseMetaDefinition = (definition) => {
44343
+ return isMetaName(definition.qualifiedName);
44344
+ };
44345
+ const isMetaSubroot = (options) => {
44346
+ const { nodeType } = options.parent;
44347
+ return (nodeType === "subtree" || nodeType === "group") && isDirectRootDescendant(options.parent) && isBaseMetaDefinition(options.parent.definition);
44348
+ };
44349
+ const isEditModeInstance = (primaryInstance) => {
44350
+ return primaryInstance.initializationMode === "edit";
44351
+ };
44352
+ const isEditModeMetaSubroot = (subroot) => {
44353
+ return isEditModeInstance(subroot.parent.rootDocument);
44354
+ };
44355
+ const isLeafNodeInitOptions = (options) => {
44356
+ const { instanceNodes } = options;
44357
+ const [instanceNode, ...rest] = instanceNodes;
44358
+ return instanceNode != null && rest.length === 0 && instanceNode.isLeafElement();
44359
+ };
44360
+ const findMetaLeafChild = (subroot, localName) => {
44361
+ const metaName = subroot.parent.definition.qualifiedName;
44362
+ const namespaceURI = metaName.namespaceURI.href;
44363
+ const result = Array.from(subroot.children.entries()).find(
44364
+ (entry) => {
44365
+ const [, child] = entry;
44366
+ if (!isLeafNodeInitOptions(child)) {
44367
+ return false;
44011
44368
  }
44012
- const value = node.encodeValue(node.currentState.value);
44013
- const xmlValue = escapeXMLText(value);
44014
- return serializeLeafElementXML(node.definition.qualifiedName, xmlValue);
44369
+ const [instanceNode] = child.instanceNodes;
44370
+ const { qualifiedName } = instanceNode;
44371
+ return qualifiedName.localName === localName && qualifiedName.namespaceURI?.href === namespaceURI;
44015
44372
  }
44016
- };
44373
+ );
44374
+ return result ?? null;
44017
44375
  };
44018
-
44019
- const getInitialValue = (context, options) => {
44020
- const { initialValueSource = "FORM_DEFAULT" } = options;
44021
- if (initialValueSource === "FORM_DEFAULT") {
44022
- return context.definition.defaultValue;
44376
+ const getInstanceIDValue = (subroot) => {
44377
+ const [, child = null] = findMetaLeafChild(subroot, INSTANCE_ID_LOCAL_NAME) ?? [];
44378
+ if (child == null) {
44379
+ return null;
44023
44380
  }
44024
- throw new ErrorProductionDesignPendingError("Edit implementation pending");
44381
+ const [instanceIDNode] = child.instanceNodes;
44382
+ return instanceIDNode.value;
44025
44383
  };
44026
- const createRelevantValueState = (context, baseValueState) => {
44027
- return context.scope.runTask(() => {
44028
- const [getRelevantValue, setValue] = baseValueState;
44029
- const getValue = createMemo(() => {
44030
- if (context.isRelevant()) {
44031
- return getRelevantValue();
44032
- }
44033
- return "";
44034
- });
44035
- return [getValue, setValue];
44036
- });
44384
+ const assertStaticLeafElement = (element) => {
44385
+ if (!element.isLeafElement()) {
44386
+ throw new ErrorProductionDesignPendingError(
44387
+ `Expected a leaf element, got a non-leaf element at nodeset: ${element.nodeset}`
44388
+ );
44389
+ }
44037
44390
  };
44038
- const createRuntimeValueState = (context, relevantValueState) => {
44039
- const { decodeValue, encodeValue } = context;
44040
- return context.scope.runTask(() => {
44041
- const [primaryInstanceValue, setPrimaryInstanceValue] = relevantValueState;
44042
- const getRuntimeValue = createMemo(() => {
44043
- return decodeValue(primaryInstanceValue());
44044
- });
44045
- const setRuntimeValue = (value) => {
44046
- const encodedValue = encodeValue(value);
44047
- return decodeValue(setPrimaryInstanceValue(encodedValue));
44048
- };
44049
- return [getRuntimeValue, setRuntimeValue];
44391
+ const buildMetaValueElement = (subroot, localName, value) => {
44392
+ const { qualifiedName, nodeset } = subroot.parent.definition;
44393
+ const { namespaceURI, prefix } = qualifiedName;
44394
+ const { root } = new StaticDocument({
44395
+ documentRoot: {
44396
+ name: {
44397
+ namespaceURI,
44398
+ prefix,
44399
+ localName
44400
+ },
44401
+ children: [value]
44402
+ },
44403
+ nodesetPrefix: nodeset
44050
44404
  });
44405
+ assertStaticLeafElement(root);
44406
+ return root;
44051
44407
  };
44052
- const guardDownstreamReadonlyWrites = (context, baseState) => {
44053
- const { readonly } = context.definition.bind;
44054
- if (readonly.isDefaultExpression) {
44055
- return baseState;
44056
- }
44057
- const [getValue, baseSetValue] = baseState;
44058
- const setValue = (value) => {
44059
- if (context.isReadonly()) {
44060
- const reference = untrack(() => context.contextReference());
44061
- throw new Error(`Cannot write to readonly field: ${reference}`);
44062
- }
44063
- return baseSetValue(value);
44408
+ const buildDeprecatedIDDefinition = (subroot, instanceNode) => {
44409
+ const nodeset = instanceNode.nodeset;
44410
+ const bind = subroot.model.binds.getOrCreateBindDefinition(nodeset);
44411
+ return new LeafNodeDefinition(subroot.parent.definition, bind, null, instanceNode);
44412
+ };
44413
+ const buildDeprecatedID = (subroot, value) => {
44414
+ const instanceNode = buildMetaValueElement(subroot, DEPRECATED_ID_LOCAL_NAME, value);
44415
+ const definition = buildDeprecatedIDDefinition(subroot, instanceNode);
44416
+ return {
44417
+ childNodeset: instanceNode.nodeset,
44418
+ definition,
44419
+ instanceNodes: [instanceNode]
44064
44420
  };
44065
- return [getValue, setValue];
44066
44421
  };
44067
- const createCalculation = (context, setValue, calculateDefinition) => {
44068
- context.scope.runTask(() => {
44069
- const calculate = createComputedExpression(context, calculateDefinition, {
44070
- defaultValue: ""
44071
- });
44072
- createComputed(() => {
44073
- if (context.isAttached() && context.isRelevant()) {
44074
- const calculated = calculate();
44075
- const value = context.decodeValue(calculated);
44076
- setValue(value);
44077
- }
44078
- });
44079
- });
44422
+ const updateDeprecatedID = (subroot, child, value) => {
44423
+ const instanceNode = buildMetaValueElement(subroot, DEPRECATED_ID_LOCAL_NAME, value);
44424
+ return {
44425
+ childNodeset: child.childNodeset,
44426
+ definition: child.definition,
44427
+ instanceNodes: [instanceNode]
44428
+ };
44080
44429
  };
44081
- const createValueState = (context, options = {}) => {
44082
- return context.scope.runTask(() => {
44083
- const initialValue = getInitialValue(context, options);
44084
- const baseValueState = createSignal(initialValue);
44085
- const valueState = createRelevantValueState(context, baseValueState);
44086
- const runtimeState = createRuntimeValueState(context, valueState);
44087
- const { calculate } = context.definition.bind;
44088
- if (calculate != null) {
44089
- const [, setValue] = runtimeState;
44090
- createCalculation(context, setValue, calculate);
44091
- }
44092
- return guardDownstreamReadonlyWrites(context, runtimeState);
44093
- });
44430
+ const replace = (values, index, value) => {
44431
+ const results = values.slice();
44432
+ results.splice(index, 1, value);
44433
+ return results;
44094
44434
  };
44095
-
44096
- class UnsupportedControlValueEncodeError extends Error {
44097
- constructor(type) {
44098
- super(`Cannot encode state for node (type: ${type}) - not implemented`);
44435
+ const replaceOrConcat = (values, index, value) => {
44436
+ if (index == null) {
44437
+ return values.concat(value);
44099
44438
  }
44100
- }
44101
- class UnsupportedControlWriteError extends Error {
44102
- constructor(type) {
44103
- super(`Cannot write state for node (type: ${type}) - not implemented`);
44439
+ return replace(values, index, value);
44440
+ };
44441
+ const populateDeprecatedID = (subroot) => {
44442
+ const value = getInstanceIDValue(subroot);
44443
+ if (value == null) {
44444
+ return subroot;
44104
44445
  }
44105
- }
44106
- class UnsupportedControl extends DescendantNode {
44107
- validation;
44108
- state;
44109
- // XFormsXPathElement
44110
- [XPathNodeKindKey] = "element";
44111
- // InstanceNode
44112
- engineState;
44113
- appearances;
44114
- nodeOptions = null;
44115
- currentState;
44116
- get validationState() {
44117
- return this.validation.currentState;
44446
+ const [index, currentDeprecatedID] = findMetaLeafChild(subroot, DEPRECATED_ID_LOCAL_NAME) ?? [
44447
+ null
44448
+ ];
44449
+ let deprecatedID;
44450
+ if (currentDeprecatedID == null) {
44451
+ deprecatedID = buildDeprecatedID(subroot, value);
44452
+ } else {
44453
+ deprecatedID = updateDeprecatedID(subroot, currentDeprecatedID, value);
44118
44454
  }
44119
- submissionState;
44120
- encodeValue = (instanceValue) => {
44121
- const encoded = instanceValue;
44122
- if (typeof encoded === "string") {
44123
- return encoded;
44124
- }
44125
- throw new UnsupportedControlValueEncodeError(this.nodeType);
44126
- };
44127
- decodeValue = (instanceValue) => {
44128
- return identity$1(instanceValue);
44455
+ return {
44456
+ model: subroot.model,
44457
+ parent: subroot.parent,
44458
+ children: replaceOrConcat(subroot.children, index, deprecatedID)
44129
44459
  };
44130
- constructor(parent, definition) {
44131
- super(parent, definition);
44132
- this.appearances = definition.bodyElement.appearances;
44133
- const sharedStateOptions = {
44134
- clientStateFactory: this.engineConfig.stateFactory
44135
- };
44136
- const stateSpec = {
44137
- reference: this.contextReference,
44138
- readonly: this.isReadonly,
44139
- relevant: this.isRelevant,
44140
- required: this.isRequired,
44141
- label: createNodeLabel(this, definition),
44142
- hint: createFieldHint(this, definition),
44143
- children: null,
44144
- valueOptions: null,
44145
- value: createValueState(this)
44146
- };
44147
- const state = createSharedNodeState(this.scope, stateSpec, sharedStateOptions);
44148
- this.state = state;
44149
- this.engineState = state.engineState;
44150
- this.currentState = state.currentState;
44151
- this.validation = createValidationState(this, sharedStateOptions);
44152
- this.submissionState = createLeafNodeSubmissionState(this);
44153
- }
44154
- // XFormsXPathElement
44155
- getXPathValue() {
44156
- return this.encodeValue(this.engineState.value);
44460
+ };
44461
+ const normalizeMetaSubroot = (subroot) => {
44462
+ if (isEditModeMetaSubroot(subroot)) {
44463
+ return populateDeprecatedID(subroot);
44157
44464
  }
44158
- // ValidationContext
44159
- getViolation() {
44160
- return this.validation.engineState.violation;
44465
+ return subroot;
44466
+ };
44467
+ const normalizeChildInitOptions = (options) => {
44468
+ if (isMetaSubroot(options)) {
44469
+ return normalizeMetaSubroot(options);
44161
44470
  }
44162
- isBlank() {
44163
- return this.engineState.value === "";
44471
+ return options;
44472
+ };
44473
+
44474
+ const collectModelChildNodesets = (parentTemplate) => {
44475
+ const nodesets = parentTemplate.childElements.map(({ nodeset }) => {
44476
+ return nodeset;
44477
+ });
44478
+ return Array.from(new Set(nodesets));
44479
+ };
44480
+ const groupChildElementsByNodeset = (parent) => {
44481
+ const result = /* @__PURE__ */ new Map();
44482
+ for (const child of parent.childElements) {
44483
+ const { nodeset } = child;
44484
+ const group = result.get(nodeset);
44485
+ if (group == null) {
44486
+ result.set(nodeset, [child]);
44487
+ } else {
44488
+ group.push(child);
44489
+ }
44164
44490
  }
44165
- // InstanceNode
44166
- getChildren() {
44167
- return [];
44491
+ return result;
44492
+ };
44493
+ const assertChildNodeDefinition = (definition, childNodeset) => {
44494
+ if (definition.type === "root") {
44495
+ throw new Error(`Unexpected root definition for child nodeset: ${childNodeset}`);
44168
44496
  }
44169
- // UnsupportedControlNode
44170
- setValue(_) {
44171
- throw new UnsupportedControlWriteError(this.nodeType);
44497
+ };
44498
+ const childrenInitOptions = (parent) => {
44499
+ const { model } = parent.rootDocument;
44500
+ const childNodesets = collectModelChildNodesets(parent.definition.template);
44501
+ let instanceChildren;
44502
+ if (parent.instanceNode == null) {
44503
+ instanceChildren = null;
44504
+ } else {
44505
+ instanceChildren = groupChildElementsByNodeset(parent.instanceNode);
44172
44506
  }
44173
- }
44174
-
44175
- class UploadControl extends UnsupportedControl {
44176
- nodeType = "upload";
44177
- // ValueContext
44178
- contextNode = this;
44179
- }
44507
+ const children = childNodesets.map((childNodeset) => {
44508
+ const definition = model.getNodeDefinition(childNodeset);
44509
+ assertChildNodeDefinition(definition, childNodeset);
44510
+ const instanceNodes = instanceChildren?.get(childNodeset) ?? [];
44511
+ return {
44512
+ childNodeset,
44513
+ definition,
44514
+ instanceNodes
44515
+ };
44516
+ });
44517
+ const baseResult = {
44518
+ parent,
44519
+ model,
44520
+ children
44521
+ };
44522
+ return normalizeChildInitOptions(baseResult);
44523
+ };
44180
44524
 
44181
44525
  const isSubtreeDefinition = (definition) => {
44182
44526
  return definition.bodyElement == null;
@@ -44211,52 +44555,56 @@ const isUploadNodeDefinition = (definition) => {
44211
44555
  return definition.bodyElement.type === "upload";
44212
44556
  };
44213
44557
  const buildChildren = (parent) => {
44214
- const { children } = parent.definition;
44215
- return children.map((child) => {
44216
- switch (child.type) {
44558
+ const { children } = childrenInitOptions(parent);
44559
+ return children.map(({ instanceNodes, definition }) => {
44560
+ const [instanceNode = null] = instanceNodes;
44561
+ switch (definition.type) {
44217
44562
  case "subtree": {
44218
- if (isSubtreeDefinition(child)) {
44219
- return new Subtree(parent, child);
44563
+ if (isSubtreeDefinition(definition)) {
44564
+ return new Subtree(parent, instanceNode, definition);
44220
44565
  }
44221
- return new Group(parent, child);
44566
+ return new Group(parent, instanceNode, definition);
44222
44567
  }
44223
- case "repeat-range": {
44224
- if (child.isControlled()) {
44225
- return new RepeatRangeControlled(parent, child);
44568
+ case "repeat": {
44569
+ if (definition.isControlled()) {
44570
+ return new RepeatRangeControlled(parent, instanceNodes, definition);
44226
44571
  }
44227
- return new RepeatRangeUncontrolled(parent, child);
44572
+ return new RepeatRangeUncontrolled(parent, instanceNodes, definition);
44228
44573
  }
44229
44574
  case "leaf-node": {
44230
- if (child instanceof NoteNodeDefinition) {
44231
- return new Note(parent, child);
44575
+ if (instanceNode != null && !instanceNode.isLeafElement()) {
44576
+ throw new ErrorProductionDesignPendingError();
44577
+ }
44578
+ if (definition instanceof NoteNodeDefinition) {
44579
+ return new Note(parent, instanceNode, definition);
44232
44580
  }
44233
- const leafChild = child;
44581
+ const leafChild = definition;
44234
44582
  if (isModelValueDefinition(leafChild)) {
44235
- return ModelValue.from(parent, leafChild);
44583
+ return ModelValue.from(parent, instanceNode, leafChild);
44236
44584
  }
44237
44585
  if (isInputDefinition(leafChild)) {
44238
- return InputControl.from(parent, leafChild);
44586
+ return InputControl.from(parent, instanceNode, leafChild);
44239
44587
  }
44240
44588
  if (isSelectDefinition(leafChild)) {
44241
- return SelectControl.from(parent, leafChild);
44589
+ return SelectControl.from(parent, instanceNode, leafChild);
44242
44590
  }
44243
44591
  if (isRankDefinition(leafChild)) {
44244
- return RankControl.from(parent, leafChild);
44592
+ return RankControl.from(parent, instanceNode, leafChild);
44245
44593
  }
44246
44594
  if (isTriggerNodeDefinition(leafChild)) {
44247
- return TriggerControl.from(parent, leafChild);
44595
+ return TriggerControl.from(parent, instanceNode, leafChild);
44248
44596
  }
44249
44597
  if (isRangeLeafNodeDefinition(leafChild)) {
44250
44598
  assertRangeNodeDefinition(leafChild);
44251
- return RangeControl.from(parent, leafChild);
44599
+ return RangeControl.from(parent, instanceNode, leafChild);
44252
44600
  }
44253
44601
  if (isUploadNodeDefinition(leafChild)) {
44254
- return new UploadControl(parent, leafChild);
44602
+ return new UploadControl(parent, instanceNode, leafChild);
44255
44603
  }
44256
44604
  throw new UnreachableError$1(leafChild);
44257
44605
  }
44258
44606
  default: {
44259
- throw new UnreachableError$1(child);
44607
+ throw new UnreachableError$1(definition);
44260
44608
  }
44261
44609
  }
44262
44610
  });
@@ -44283,27 +44631,20 @@ class Root extends DescendantNode {
44283
44631
  classes;
44284
44632
  currentState;
44285
44633
  validationState;
44286
- submissionState;
44287
- // ClientReactiveSubmittableInstance
44288
- get submissionDefinition() {
44289
- return this.definition.submission;
44290
- }
44291
- // RootNode
44634
+ instanceState;
44292
44635
  languages;
44293
44636
  constructor(parent) {
44294
- const { definition } = parent;
44637
+ const { definition, instanceNode: instance } = parent;
44638
+ const instanceNode = instance.root;
44295
44639
  const { nodeset: reference } = definition;
44296
44640
  const computeReference = () => reference;
44297
- super(parent, definition, {
44641
+ super(parent, instanceNode, definition, {
44298
44642
  computeReference
44299
44643
  });
44300
44644
  this.classes = parent.classes;
44301
44645
  const childrenState = createChildrenState(this);
44302
44646
  this.childrenState = childrenState;
44303
44647
  this.languages = parent.languages;
44304
- const sharedStateOptions = {
44305
- clientStateFactory: this.engineConfig.stateFactory
44306
- };
44307
44648
  const state = createSharedNodeState(
44308
44649
  this.scope,
44309
44650
  {
@@ -44318,7 +44659,7 @@ class Root extends DescendantNode {
44318
44659
  value: null,
44319
44660
  children: childrenState.childIds
44320
44661
  },
44321
- sharedStateOptions
44662
+ this.instanceConfig
44322
44663
  );
44323
44664
  this.state = state;
44324
44665
  this.engineState = state.engineState;
@@ -44328,8 +44669,8 @@ class Root extends DescendantNode {
44328
44669
  childrenState
44329
44670
  );
44330
44671
  childrenState.setChildren(buildChildren(this));
44331
- this.validationState = createAggregatedViolations(this, sharedStateOptions);
44332
- this.submissionState = createRootSubmissionState(this);
44672
+ this.validationState = createAggregatedViolations(this, this.instanceConfig);
44673
+ this.instanceState = createRootInstanceState(this);
44333
44674
  }
44334
44675
  getChildren() {
44335
44676
  return this.childrenState.getChildren();
@@ -44339,16 +44680,22 @@ class Root extends DescendantNode {
44339
44680
  this.rootDocument.setLanguage(language);
44340
44681
  return this;
44341
44682
  }
44342
- prepareSubmission(options) {
44343
- return this.rootDocument.prepareSubmission(options);
44683
+ prepareInstancePayload(options) {
44684
+ return this.rootDocument.prepareInstancePayload(options);
44344
44685
  }
44345
44686
  }
44346
44687
 
44347
44688
  const PRIMARY_INSTANCE_REFERENCE = "/";
44348
44689
  class PrimaryInstance extends InstanceNode {
44690
+ /**
44691
+ * @todo this will be populated as we introduce other initialization modes!
44692
+ */
44693
+ initializationMode = "create";
44694
+ model;
44349
44695
  // InstanceNode
44350
44696
  state;
44351
44697
  engineState;
44698
+ instanceNode;
44352
44699
  getChildren;
44353
44700
  hasReadonlyAncestor = () => false;
44354
44701
  isReadonly = () => false;
@@ -44358,7 +44705,7 @@ class PrimaryInstance extends InstanceNode {
44358
44705
  setActiveLanguage;
44359
44706
  // XFormsXPathDocument
44360
44707
  [XPathNodeKindKey] = "document";
44361
- // PrimaryInstanceDocument, ClientReactiveSubmittableInstance
44708
+ // PrimaryInstanceDocument, ClientReactiveSerializableInstance
44362
44709
  nodeType = "primary-instance";
44363
44710
  appearances = null;
44364
44711
  nodeOptions = null;
@@ -44366,7 +44713,7 @@ class PrimaryInstance extends InstanceNode {
44366
44713
  root;
44367
44714
  currentState;
44368
44715
  validationState;
44369
- submissionState;
44716
+ instanceState;
44370
44717
  languages;
44371
44718
  // TranslationContext (+ EvaluationContext)
44372
44719
  getActiveLanguage;
@@ -44374,12 +44721,18 @@ class PrimaryInstance extends InstanceNode {
44374
44721
  isAttached;
44375
44722
  evaluator;
44376
44723
  contextNode = this;
44377
- constructor(scope, model, secondaryInstances, engineConfig) {
44378
- const { root: definition } = model;
44379
- super(engineConfig, null, definition, {
44724
+ constructor(options) {
44725
+ const { mode, initialState, scope, model, secondaryInstances, config } = options;
44726
+ const { instance: modelInstance } = model;
44727
+ const activeInstance = initialState?.document ?? modelInstance;
44728
+ const definition = model.getRootDefinition(activeInstance);
44729
+ super(config, null, activeInstance, definition, {
44380
44730
  scope,
44381
44731
  computeReference: () => PRIMARY_INSTANCE_REFERENCE
44382
44732
  });
44733
+ this.initializationMode = mode;
44734
+ this.model = model;
44735
+ this.instanceNode = activeInstance;
44383
44736
  const [isAttached, setIsAttached] = createSignal(false);
44384
44737
  this.isAttached = isAttached;
44385
44738
  const evaluator = new EngineXPathEvaluator({
@@ -44410,9 +44763,7 @@ class PrimaryInstance extends InstanceNode {
44410
44763
  value: null,
44411
44764
  children: childrenState.childIds
44412
44765
  };
44413
- const state = createSharedNodeState(scope, stateSpec, {
44414
- clientStateFactory: engineConfig.stateFactory
44415
- });
44766
+ const state = createSharedNodeState(scope, stateSpec, config);
44416
44767
  this.state = state;
44417
44768
  this.engineState = state.engineState;
44418
44769
  this.currentState = materializeCurrentStateChildren(scope, state.currentState, childrenState);
@@ -44423,7 +44774,7 @@ class PrimaryInstance extends InstanceNode {
44423
44774
  return root.validationState.violations;
44424
44775
  }
44425
44776
  };
44426
- this.submissionState = createInstanceSubmissionState(this);
44777
+ this.instanceState = createPrimaryInstanceState(this);
44427
44778
  childrenState.setChildren([root]);
44428
44779
  setIsAttached(true);
44429
44780
  }
@@ -44459,43 +44810,188 @@ class PrimaryInstance extends InstanceNode {
44459
44810
  this.evaluator.setActiveLanguage(availableFormLanguage.language);
44460
44811
  return this.setActiveLanguage(availableFormLanguage);
44461
44812
  }
44462
- // PrimaryInstanceDocument, ClientReactiveSubmittableInstance
44463
- prepareSubmission(options) {
44464
- const result = prepareSubmission(this, {
44465
- chunked: options?.chunked ?? "monolithic",
44813
+ // PrimaryInstanceDocument
44814
+ prepareInstancePayload(options) {
44815
+ const result = prepareInstancePayload(this, {
44816
+ payloadType: options?.payloadType ?? "monolithic",
44466
44817
  maxSize: options?.maxSize ?? Infinity
44467
44818
  });
44468
44819
  return Promise.resolve(result);
44469
44820
  }
44470
44821
  }
44471
44822
 
44472
- const buildInstanceConfig = (options = {}) => {
44823
+ class FormInstance {
44824
+ constructor(formResult, options) {
44825
+ this.formResult = formResult;
44826
+ const { mode, initialState } = options;
44827
+ const config = {
44828
+ clientStateFactory: options.instanceConfig.stateFactory ?? identity
44829
+ };
44830
+ const primaryInstanceOptions = {
44831
+ ...options.instanceOptions,
44832
+ mode,
44833
+ initialState,
44834
+ config
44835
+ };
44836
+ const { root } = new PrimaryInstance(primaryInstanceOptions);
44837
+ this.mode = mode;
44838
+ this.root = root;
44839
+ }
44840
+ mode;
44841
+ root;
44842
+ }
44843
+
44844
+ class BaseInstantiableFormResult extends BaseFormResult {
44845
+ createInstance;
44846
+ editInstance;
44847
+ restoreInstance;
44848
+ constructor(options) {
44849
+ const { status, warnings, error, instanceOptions } = options;
44850
+ super({
44851
+ status,
44852
+ warnings,
44853
+ error
44854
+ });
44855
+ this.createInstance = (instanceConfig = {}) => {
44856
+ this.assertInstantiable();
44857
+ return new FormInstance(this, {
44858
+ mode: "create",
44859
+ instanceOptions,
44860
+ initialState: null,
44861
+ instanceConfig
44862
+ });
44863
+ };
44864
+ this.editInstance = async (input, instanceConfig = {}) => {
44865
+ this.assertInstantiable();
44866
+ const initialState = await InitialInstanceState.resolve(instanceOptions.model, input);
44867
+ return new FormInstance(this, {
44868
+ mode: "edit",
44869
+ instanceOptions,
44870
+ initialState,
44871
+ instanceConfig
44872
+ });
44873
+ };
44874
+ this.restoreInstance = async (input, instanceConfig = {}) => {
44875
+ this.assertInstantiable();
44876
+ const initialState = await InitialInstanceState.from(instanceOptions.model, input.data);
44877
+ return new FormInstance(this, {
44878
+ mode: "restore",
44879
+ instanceOptions,
44880
+ initialState,
44881
+ instanceConfig
44882
+ });
44883
+ };
44884
+ }
44885
+ isInstantiable() {
44886
+ const self = this;
44887
+ return self.status !== "failure";
44888
+ }
44889
+ assertInstantiable() {
44890
+ if (!this.isInstantiable()) {
44891
+ throw new ErrorProductionDesignPendingError(
44892
+ 'Failed to instantiate from result with status: "failure"'
44893
+ );
44894
+ }
44895
+ }
44896
+ }
44897
+
44898
+ class FormSuccessResult extends BaseInstantiableFormResult {
44899
+ constructor(options) {
44900
+ super({
44901
+ status: "success",
44902
+ ...options
44903
+ });
44904
+ }
44905
+ }
44906
+
44907
+ const createPotentiallyClientOwnedReactiveScope = () => {
44908
+ const clientOwner = getOwner();
44909
+ return createReactiveScope({ owner: clientOwner });
44910
+ };
44911
+ let unboundGlobalFetch;
44912
+ if (typeof globalThis.fetch === "function") {
44913
+ unboundGlobalFetch = (...args) => {
44914
+ return globalThis.fetch(...args);
44915
+ };
44916
+ } else {
44917
+ unboundGlobalFetch = () => {
44918
+ throw new Error("fetch is not supported in this environment");
44919
+ };
44920
+ }
44921
+ const resolveOptions = (options) => {
44473
44922
  return {
44474
- createUniqueId,
44475
- fetchFormDefinition: options.fetchFormDefinition ?? fetch,
44476
- fetchFormAttachment: options.fetchFormAttachment ?? fetch,
44477
- missingResourceBehavior: options.missingResourceBehavior ?? MISSING_RESOURCE_BEHAVIOR.DEFAULT,
44478
- stateFactory: options.stateFactory ?? identity$1
44923
+ fetchFormDefinition: options?.fetchFormDefinition ?? unboundGlobalFetch,
44924
+ fetchFormAttachment: options?.fetchFormAttachment ?? unboundGlobalFetch,
44925
+ missingResourceBehavior: options?.missingResourceBehavior ?? MISSING_RESOURCE_BEHAVIOR.DEFAULT
44479
44926
  };
44480
44927
  };
44481
- const initializeForm$1 = async (input, options = {}) => {
44482
- const owner = getOwner();
44483
- const scope = createReactiveScope({ owner });
44484
- const engineConfig = buildInstanceConfig(options.config);
44485
- const sourceXML = await retrieveSourceXMLResource(input, {
44486
- fetchResource: engineConfig.fetchFormDefinition
44487
- });
44488
- const xformDOM = XFormDOM.from(sourceXML);
44489
- const secondaryInstances = await SecondaryInstancesDefinition.load(xformDOM, {
44490
- fetchResource: engineConfig.fetchFormAttachment,
44491
- missingResourceBehavior: engineConfig.missingResourceBehavior
44492
- });
44493
- const form = new XFormDefinition(xformDOM);
44494
- const primaryInstance = new PrimaryInstance(scope, form.model, secondaryInstances, engineConfig);
44495
- return primaryInstance.root;
44928
+ const loadFormResult = async (scope, formResource, options) => {
44929
+ const { fetchFormDefinition, fetchFormAttachment, missingResourceBehavior } = options;
44930
+ try {
44931
+ const sourceXML = await retrieveFormDefinition(formResource, {
44932
+ fetchFormDefinition
44933
+ });
44934
+ const xformDOM = XFormDOM.from(sourceXML);
44935
+ const { model } = new XFormDefinition(xformDOM);
44936
+ const secondaryInstances = await SecondaryInstancesDefinition.load(xformDOM, {
44937
+ fetchResource: fetchFormAttachment,
44938
+ missingResourceBehavior
44939
+ });
44940
+ const instanceOptions = {
44941
+ scope,
44942
+ model,
44943
+ secondaryInstances
44944
+ };
44945
+ return new FormSuccessResult({
44946
+ warnings: null,
44947
+ error: null,
44948
+ scope,
44949
+ formResource,
44950
+ instanceOptions
44951
+ });
44952
+ } catch (caught) {
44953
+ let cause;
44954
+ if (caught instanceof Error) {
44955
+ cause = caught;
44956
+ } else {
44957
+ cause = new Error("Unknown form load error", { cause: caught });
44958
+ }
44959
+ const error = new LoadFormFailureError(formResource, [cause]);
44960
+ return new FormFailureResult({
44961
+ warnings: null,
44962
+ error
44963
+ });
44964
+ }
44965
+ };
44966
+ const loadForm = (formResource, options) => {
44967
+ const scope = createPotentiallyClientOwnedReactiveScope();
44968
+ const resolvedOptions = resolveOptions(options);
44969
+ return loadFormResult(scope, formResource, resolvedOptions);
44496
44970
  };
44497
44971
 
44498
- const initializeForm = initializeForm$1;
44972
+ const createInstance = async (formResource, options) => {
44973
+ const form = await loadForm(formResource, options?.form);
44974
+ if (form.status === "failure") {
44975
+ throw form.error;
44976
+ }
44977
+ return form.createInstance(options?.instance);
44978
+ };
44979
+
44980
+ const editInstance = async (formResource, input, options) => {
44981
+ const form = await loadForm(formResource, options?.form);
44982
+ if (form.status === "failure") {
44983
+ throw form.error;
44984
+ }
44985
+ return form.editInstance(input, options?.instance);
44986
+ };
44987
+
44988
+ const restoreInstance = async (formResource, input, options) => {
44989
+ const form = await loadForm(formResource, options?.form);
44990
+ if (form.status === "failure") {
44991
+ throw form.error;
44992
+ }
44993
+ return form.restoreInstance(input, options?.instance);
44994
+ };
44499
44995
 
44500
- export { constants, initializeForm };
44996
+ export { constants, createInstance, editInstance, loadForm, restoreInstance };
44501
44997
  //# sourceMappingURL=index.js.map