@getodk/xforms-engine 0.5.0 → 0.6.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.
- package/README.md +1 -1
- package/dist/client/BaseNode.d.ts +4 -0
- package/dist/client/BaseValueNode.d.ts +1 -1
- package/dist/client/GroupNode.d.ts +1 -0
- package/dist/client/InputNode.d.ts +32 -3
- package/dist/client/ModelValueNode.d.ts +1 -0
- package/dist/client/NoteNode.d.ts +24 -7
- package/dist/client/RangeNode.d.ts +36 -0
- package/dist/client/RankNode.d.ts +46 -0
- package/dist/client/RootNode.d.ts +6 -3
- package/dist/client/SelectNode.d.ts +47 -25
- package/dist/client/SubtreeNode.d.ts +1 -0
- package/dist/client/TriggerNode.d.ts +10 -6
- package/dist/client/hierarchy.d.ts +5 -5
- package/dist/client/node-types.d.ts +2 -2
- package/dist/client/unsupported/UnsupportedControlNode.d.ts +1 -3
- package/dist/error/RankMissingValueError.d.ts +3 -0
- package/dist/error/RankValueTypeError.d.ts +6 -0
- package/dist/error/SelectValueTypeError.d.ts +15 -0
- package/dist/error/XFormsSpecViolationError.d.ts +2 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.js +5604 -4666
- package/dist/index.js.map +1 -1
- package/dist/instance/Group.d.ts +1 -0
- package/dist/instance/InputControl.d.ts +5 -3
- package/dist/instance/ModelValue.d.ts +2 -0
- package/dist/instance/Note.d.ts +13 -25
- package/dist/instance/PrimaryInstance.d.ts +1 -0
- package/dist/instance/RangeControl.d.ts +34 -0
- package/dist/instance/RankControl.d.ts +40 -0
- package/dist/instance/Root.d.ts +1 -0
- package/dist/instance/SelectControl.d.ts +66 -0
- package/dist/instance/Subtree.d.ts +1 -0
- package/dist/instance/TriggerControl.d.ts +9 -22
- package/dist/instance/abstract/DescendantNode.d.ts +1 -2
- package/dist/instance/abstract/InstanceNode.d.ts +3 -1
- package/dist/instance/abstract/UnsupportedControl.d.ts +1 -0
- package/dist/instance/abstract/ValueNode.d.ts +0 -1
- package/dist/instance/hierarchy.d.ts +9 -9
- package/dist/instance/internal-api/InstanceValueContext.d.ts +2 -0
- package/dist/instance/internal-api/submission/ClientReactiveSubmittableLeafNode.d.ts +2 -1
- package/dist/instance/internal-api/submission/ClientReactiveSubmittableParentNode.d.ts +2 -1
- package/dist/instance/internal-api/submission/ClientReactiveSubmittableValueNode.d.ts +2 -1
- package/dist/instance/repeat/BaseRepeatRange.d.ts +1 -0
- package/dist/instance/repeat/RepeatInstance.d.ts +1 -0
- package/dist/integration/xpath/adapter/names.d.ts +1 -11
- package/dist/integration/xpath/adapter/traversal.d.ts +10 -9
- package/dist/integration/xpath/static-dom/StaticAttribute.d.ts +10 -3
- package/dist/integration/xpath/static-dom/StaticDocument.d.ts +0 -1
- package/dist/integration/xpath/static-dom/StaticElement.d.ts +12 -4
- package/dist/lib/client-reactivity/submission/createRootSubmissionState.d.ts +3 -0
- package/dist/lib/codecs/Geopoint/Geopoint.d.ts +48 -0
- package/dist/lib/codecs/Geopoint/GeopointValueCodec.d.ts +5 -0
- package/dist/lib/codecs/NoteCodec.d.ts +8 -0
- package/dist/lib/codecs/RangeCodec.d.ts +8 -0
- package/dist/lib/codecs/TriggerCodec.d.ts +7 -0
- package/dist/lib/codecs/ValueArrayCodec.d.ts +11 -0
- package/dist/lib/codecs/ValueCodec.d.ts +2 -2
- package/dist/lib/codecs/getNoteCodec.d.ts +3 -0
- package/dist/lib/codecs/getSelectCodec.d.ts +5 -0
- package/dist/lib/codecs/getSharedValueCodec.d.ts +3 -2
- package/dist/lib/codecs/items/BaseItemCodec.d.ts +9 -0
- package/dist/lib/codecs/items/MultipleValueItemCodec.d.ts +14 -0
- package/dist/lib/codecs/items/SingleValueItemCodec.d.ts +24 -0
- package/dist/lib/dom/query.d.ts +1 -2
- package/dist/lib/names/NamespaceDeclaration.d.ts +45 -0
- package/dist/lib/names/NamespaceDeclarationMap.d.ts +137 -0
- package/dist/lib/names/NamespaceURL.d.ts +30 -0
- package/dist/lib/names/QualifiedName.d.ts +113 -0
- package/dist/lib/number-parsers.d.ts +2 -0
- package/dist/lib/reactivity/createItemCollection.d.ts +21 -0
- package/dist/lib/xml-serialization.d.ts +11 -2
- package/dist/parse/XFormDOM.d.ts +1 -1
- package/dist/parse/body/BodyDefinition.d.ts +2 -2
- package/dist/parse/body/appearance/rangeAppearanceParser.d.ts +3 -0
- package/dist/parse/body/control/InputControlDefinition.d.ts +3 -0
- package/dist/parse/body/control/ItemDefinition.d.ts +14 -0
- package/dist/parse/body/control/ItemsetDefinition.d.ts +18 -0
- package/dist/parse/body/control/RangeControlDefinition.d.ts +31 -2
- package/dist/parse/body/control/RankControlDefinition.d.ts +7 -3
- package/dist/parse/body/control/{select/SelectDefinition.d.ts → SelectControlDefinition.d.ts} +9 -9
- package/dist/parse/expression/ItemsetNodesetExpression.d.ts +1 -1
- package/dist/parse/expression/ItemsetValueExpression.d.ts +1 -1
- package/dist/parse/model/BindDefinition.d.ts +3 -1
- package/dist/parse/model/BindPreloadDefinition.d.ts +42 -0
- package/dist/parse/model/DescendentNodeDefinition.d.ts +4 -13
- package/dist/parse/model/ItextTranslation/ItextTranslationRootDefinition.d.ts +2 -1
- package/dist/parse/model/LeafNodeDefinition.d.ts +7 -4
- package/dist/parse/model/ModelBindMap.d.ts +1 -1
- package/dist/parse/model/NodeDefinition.d.ts +16 -19
- package/dist/parse/model/NoteNodeDefinition.d.ts +6 -5
- package/dist/parse/model/RangeNodeDefinition.d.ts +41 -0
- package/dist/parse/model/RepeatInstanceDefinition.d.ts +7 -4
- package/dist/parse/model/RepeatRangeDefinition.d.ts +7 -4
- package/dist/parse/model/RepeatTemplateDefinition.d.ts +7 -4
- package/dist/parse/model/RootAttributeDefinition.d.ts +24 -0
- package/dist/parse/model/RootAttributeMap.d.ts +23 -0
- package/dist/parse/model/RootDefinition.d.ts +9 -7
- package/dist/parse/model/SubtreeDefinition.d.ts +7 -4
- package/dist/parse/shared/parseStaticDocumentFromDOMSubtree.d.ts +2 -3
- package/dist/parse/text/ItemLabelDefinition.d.ts +1 -1
- package/dist/parse/text/ItemsetLabelDefinition.d.ts +2 -2
- package/dist/parse/text/abstract/TextElementDefinition.d.ts +1 -1
- package/dist/parse/xpath/semantic-analysis.d.ts +1 -3
- package/dist/solid.js +5603 -4665
- package/dist/solid.js.map +1 -1
- package/package.json +15 -12
- package/src/client/BaseNode.ts +5 -0
- package/src/client/BaseValueNode.ts +1 -1
- package/src/client/GroupNode.ts +1 -0
- package/src/client/InputNode.ts +38 -2
- package/src/client/ModelValueNode.ts +1 -0
- package/src/client/NoteNode.ts +43 -7
- package/src/client/RangeNode.ts +51 -0
- package/src/client/RankNode.ts +54 -0
- package/src/client/RootNode.ts +11 -5
- package/src/client/SelectNode.ts +53 -26
- package/src/client/SubtreeNode.ts +1 -0
- package/src/client/TriggerNode.ts +12 -6
- package/src/client/hierarchy.ts +7 -8
- package/src/client/node-types.ts +1 -1
- package/src/client/unsupported/UnsupportedControlNode.ts +2 -6
- package/src/error/RankMissingValueError.ts +5 -0
- package/src/error/RankValueTypeError.ts +13 -0
- package/src/error/SelectValueTypeError.ts +22 -0
- package/src/error/XFormsSpecViolationError.ts +1 -0
- package/src/index.ts +2 -12
- package/src/instance/Group.ts +1 -0
- package/src/instance/InputControl.ts +42 -2
- package/src/instance/ModelValue.ts +2 -0
- package/src/instance/Note.ts +34 -59
- package/src/instance/PrimaryInstance.ts +1 -0
- package/src/instance/RangeControl.ts +113 -0
- package/src/instance/RankControl.ts +199 -0
- package/src/instance/Root.ts +3 -2
- package/src/instance/SelectControl.ts +219 -0
- package/src/instance/Subtree.ts +1 -0
- package/src/instance/TriggerControl.ts +36 -75
- package/src/instance/abstract/DescendantNode.ts +1 -6
- package/src/instance/abstract/InstanceNode.ts +10 -2
- package/src/instance/abstract/UnsupportedControl.ts +1 -0
- package/src/instance/abstract/ValueNode.ts +3 -2
- package/src/instance/children.ts +71 -30
- package/src/instance/hierarchy.ts +21 -16
- package/src/instance/internal-api/InstanceValueContext.ts +2 -0
- package/src/instance/internal-api/submission/ClientReactiveSubmittableLeafNode.ts +2 -1
- package/src/instance/internal-api/submission/ClientReactiveSubmittableParentNode.ts +2 -1
- package/src/instance/internal-api/submission/ClientReactiveSubmittableValueNode.ts +2 -1
- package/src/instance/repeat/BaseRepeatRange.ts +2 -0
- package/src/instance/repeat/RepeatInstance.ts +1 -0
- package/src/instance/resource.ts +4 -1
- package/src/integration/xpath/adapter/names.ts +66 -17
- package/src/integration/xpath/adapter/traversal.ts +10 -9
- package/src/integration/xpath/static-dom/StaticAttribute.ts +15 -7
- package/src/integration/xpath/static-dom/StaticDocument.ts +0 -2
- package/src/integration/xpath/static-dom/StaticElement.ts +21 -8
- package/src/lib/client-reactivity/submission/createLeafNodeSubmissionState.ts +1 -1
- package/src/lib/client-reactivity/submission/createParentNodeSubmissionState.ts +1 -1
- package/src/lib/client-reactivity/submission/createRootSubmissionState.ts +19 -0
- package/src/lib/client-reactivity/submission/createValueNodeSubmissionState.ts +2 -2
- package/src/lib/codecs/Geopoint/Geopoint.ts +150 -0
- package/src/lib/codecs/Geopoint/GeopointValueCodec.ts +20 -0
- package/src/lib/codecs/NoteCodec.ts +32 -0
- package/src/lib/codecs/RangeCodec.ts +65 -0
- package/src/lib/codecs/TriggerCodec.ts +64 -0
- package/src/lib/codecs/ValueArrayCodec.ts +42 -0
- package/src/lib/codecs/ValueCodec.ts +2 -2
- package/src/lib/codecs/getNoteCodec.ts +27 -0
- package/src/lib/codecs/getSelectCodec.ts +27 -0
- package/src/lib/codecs/getSharedValueCodec.ts +5 -3
- package/src/lib/codecs/items/BaseItemCodec.ts +20 -0
- package/src/lib/codecs/items/MultipleValueItemCodec.ts +28 -0
- package/src/lib/codecs/items/SingleValueItemCodec.ts +67 -0
- package/src/lib/dom/query.ts +1 -2
- package/src/lib/names/NamespaceDeclaration.ts +106 -0
- package/src/lib/names/NamespaceDeclarationMap.ts +228 -0
- package/src/lib/names/NamespaceURL.ts +44 -0
- package/src/lib/names/QualifiedName.ts +170 -0
- package/src/lib/number-parsers.ts +25 -0
- package/src/lib/reactivity/createInstanceValueState.ts +50 -0
- package/src/lib/reactivity/{createSelectItems.ts → createItemCollection.ts} +41 -36
- package/src/lib/xml-serialization.ts +76 -9
- package/src/parse/XFormDOM.ts +141 -21
- package/src/parse/XFormDefinition.ts +1 -4
- package/src/parse/body/BodyDefinition.ts +4 -4
- package/src/parse/body/appearance/rangeAppearanceParser.ts +11 -0
- package/src/parse/body/control/InputControlDefinition.ts +9 -0
- package/src/parse/body/control/{select/ItemDefinition.ts → ItemDefinition.ts} +8 -6
- package/src/parse/body/control/{select/ItemsetDefinition.ts → ItemsetDefinition.ts} +11 -9
- package/src/parse/body/control/RangeControlDefinition.ts +91 -6
- package/src/parse/body/control/RankControlDefinition.ts +25 -7
- package/src/parse/body/control/{select/SelectDefinition.ts → SelectControlDefinition.ts} +9 -9
- package/src/parse/expression/ItemsetNodesetExpression.ts +1 -1
- package/src/parse/expression/ItemsetValueExpression.ts +1 -1
- package/src/parse/model/BindDefinition.ts +4 -0
- package/src/parse/model/BindPreloadDefinition.ts +100 -0
- package/src/parse/model/DescendentNodeDefinition.ts +7 -25
- package/src/parse/model/ItextTranslation/ItextTranslationRootDefinition.ts +2 -1
- package/src/parse/model/LeafNodeDefinition.ts +11 -4
- package/src/parse/model/NodeDefinition.ts +24 -45
- package/src/parse/model/NoteNodeDefinition.ts +8 -7
- package/src/parse/model/RangeNodeDefinition.ts +118 -0
- package/src/parse/model/RepeatInstanceDefinition.ts +11 -7
- package/src/parse/model/RepeatRangeDefinition.ts +11 -7
- package/src/parse/model/RepeatTemplateDefinition.ts +11 -7
- package/src/parse/model/RootAttributeDefinition.ts +45 -0
- package/src/parse/model/RootAttributeMap.ts +44 -0
- package/src/parse/model/RootDefinition.ts +29 -28
- package/src/parse/model/SecondaryInstance/sources/GeoJSONExternalSecondaryInstance.ts +1 -0
- package/src/parse/model/SubtreeDefinition.ts +12 -12
- package/src/parse/shared/parseStaticDocumentFromDOMSubtree.ts +3 -3
- package/src/parse/text/ItemLabelDefinition.ts +1 -1
- package/src/parse/text/ItemsetLabelDefinition.ts +2 -2
- package/src/parse/text/abstract/TextElementDefinition.ts +1 -1
- package/src/parse/xpath/semantic-analysis.ts +4 -3
- package/dist/client/unsupported/RangeNode.d.ts +0 -9
- package/dist/client/unsupported/RankNode.d.ts +0 -9
- package/dist/instance/SelectField.d.ts +0 -58
- package/dist/instance/unsupported/RangeControl.d.ts +0 -6
- package/dist/instance/unsupported/RankControl.d.ts +0 -6
- package/dist/integration/xpath/static-dom/StaticNamedNode.d.ts +0 -17
- package/dist/lib/reactivity/createSelectItems.d.ts +0 -16
- package/dist/parse/body/control/select/ItemDefinition.d.ts +0 -13
- package/dist/parse/body/control/select/ItemsetDefinition.d.ts +0 -17
- package/dist/parse/body/control/select/ItemsetNodesetContext.d.ts +0 -9
- package/src/client/unsupported/RangeNode.ts +0 -14
- package/src/client/unsupported/RankNode.ts +0 -14
- package/src/instance/SelectField.ts +0 -263
- package/src/instance/unsupported/RangeControl.ts +0 -9
- package/src/instance/unsupported/RankControl.ts +0 -9
- package/src/integration/xpath/static-dom/StaticNamedNode.ts +0 -45
- package/src/parse/body/control/select/ItemsetNodesetContext.ts +0 -21
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { XMLNS_NAMESPACE_URI, XMLNS_PREFIX } from '@getodk/common/constants/xmlns.ts';
|
|
2
|
+
import { escapeXMLText } from '../xml-serialization.ts';
|
|
3
|
+
import type { NamespaceDeclarationMap } from './NamespaceDeclarationMap.ts';
|
|
4
|
+
import type { NamespaceURI } from './QualifiedName.ts';
|
|
5
|
+
import { QualifiedName } from './QualifiedName.ts';
|
|
6
|
+
|
|
7
|
+
interface NamespaceDeclarationXMLSerializationOptions {
|
|
8
|
+
readonly omitDefaultNamespace?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface NamespaceDeclarationOptions {
|
|
12
|
+
readonly declaredPrefix: string | null;
|
|
13
|
+
readonly declaredURI: NamespaceURI;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Provides a generalized representation of an XML namespace declaration, which
|
|
18
|
+
* can be used for:
|
|
19
|
+
*
|
|
20
|
+
* - Resolution of a declared namespace URI, by its declared prefix
|
|
21
|
+
* - Resolution of a declared namespace prefix associated with its namespace URI
|
|
22
|
+
* - Scoped resolution of same in an arbitrary DOM-like tree of nodes (or
|
|
23
|
+
* representations thereof)
|
|
24
|
+
* - Serialization of the namespace declaration as an XML representation, as
|
|
25
|
+
* part of broader XML serialization logic from an arbitrary DOM-like tree of
|
|
26
|
+
* nodes (or representations thereof)
|
|
27
|
+
*
|
|
28
|
+
* @see {@link NamespaceDeclarationMap} for details on scoped usage
|
|
29
|
+
*/
|
|
30
|
+
export class NamespaceDeclaration {
|
|
31
|
+
private readonly serializedXML: string;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A namespace is declared as either:
|
|
35
|
+
*
|
|
36
|
+
* - a "default" namespace (for which no prefix is declared, in which case
|
|
37
|
+
* this value will be `null`)
|
|
38
|
+
*
|
|
39
|
+
* - a namespace prefix (for which the prefix can be used to reference the
|
|
40
|
+
* declared namespace, in which case this value will be a `string`)
|
|
41
|
+
*/
|
|
42
|
+
readonly declaredPrefix: string | null;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A namespace is declared for a {@link NamespaceURI}, i.e. either a
|
|
46
|
+
* {@link URL} or `null`, where `null` corresponds to the "null namespace"
|
|
47
|
+
* (i.e. `xmlns=""` or `xmlns:prefix=""`, in serialized XML).
|
|
48
|
+
*/
|
|
49
|
+
readonly declaredURI: NamespaceURI;
|
|
50
|
+
|
|
51
|
+
constructor(options: NamespaceDeclarationOptions) {
|
|
52
|
+
const { declaredPrefix, declaredURI } = options;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Represents the {@link QualifiedName} **of the {@link NamespaceDeclaration} itself, used only for consistent XML serialization logic.
|
|
56
|
+
*/
|
|
57
|
+
let qualifiedName: QualifiedName;
|
|
58
|
+
|
|
59
|
+
switch (declaredPrefix) {
|
|
60
|
+
// Declaring a "null prefix" is equivalent to the following XML syntax:
|
|
61
|
+
// `xmlns="..."`
|
|
62
|
+
case null:
|
|
63
|
+
qualifiedName = new QualifiedName({
|
|
64
|
+
namespaceURI: XMLNS_NAMESPACE_URI,
|
|
65
|
+
prefix: null,
|
|
66
|
+
localName: XMLNS_PREFIX,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
break;
|
|
70
|
+
|
|
71
|
+
// Declaring a non-null prefix is equivalent to the following XML syntax:
|
|
72
|
+
// `xmlns:$declaredPrefix="..."
|
|
73
|
+
default:
|
|
74
|
+
qualifiedName = new QualifiedName({
|
|
75
|
+
namespaceURI: XMLNS_NAMESPACE_URI,
|
|
76
|
+
prefix: XMLNS_PREFIX,
|
|
77
|
+
localName: declaredPrefix,
|
|
78
|
+
});
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this.declaredPrefix = declaredPrefix;
|
|
83
|
+
this.declaredURI = declaredURI;
|
|
84
|
+
|
|
85
|
+
const serializedName = qualifiedName.getPrefixedName();
|
|
86
|
+
const serializedValue = escapeXMLText(declaredURI?.href ?? '');
|
|
87
|
+
|
|
88
|
+
this.serializedXML = ` ${serializedName}="${serializedValue}"`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
declaresNamespaceURI(namespaceURI: NamespaceURI) {
|
|
92
|
+
if (namespaceURI == null) {
|
|
93
|
+
return this.declaredURI === null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return this.declaredURI?.href === namespaceURI.href;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
serializeNamespaceDeclarationXML(options?: NamespaceDeclarationXMLSerializationOptions): string {
|
|
100
|
+
if (options?.omitDefaultNamespace && this.declaredPrefix == null) {
|
|
101
|
+
return '';
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return this.serializedXML;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { UnreachableError } from '@getodk/common/lib/error/UnreachableError.ts';
|
|
2
|
+
import { NamespaceDeclaration } from './NamespaceDeclaration.ts';
|
|
3
|
+
import type { NamespaceURI, QualifiedName } from './QualifiedName.ts';
|
|
4
|
+
|
|
5
|
+
export interface NamedNodeDefinition {
|
|
6
|
+
readonly qualifiedName: QualifiedName;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
type NamedNodeDefinitionMap = ReadonlyMap<QualifiedName, NamedNodeDefinition>;
|
|
10
|
+
|
|
11
|
+
export interface NamedSubtreeDefinition extends NamedNodeDefinition {
|
|
12
|
+
readonly namespaceDeclarations: NamespaceDeclarationMap;
|
|
13
|
+
readonly parent: NamedSubtreeDefinition | null;
|
|
14
|
+
readonly attributes?: NamedNodeDefinitionMap;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @todo This is a bit of a code style experiment! Responsive to
|
|
19
|
+
* {@link https://github.com/getodk/web-forms/issues/296 | How should we represent enumerated types?}.
|
|
20
|
+
* Observations to be considered for that issue...
|
|
21
|
+
*
|
|
22
|
+
* This more or less works as one would expect, with one really irritating
|
|
23
|
+
* downside: unlike a **TypeScript `enum`**, code completions (in VSCode, but
|
|
24
|
+
* I'd expect the same for any TypeScript language server/LSP implementation)
|
|
25
|
+
* automatically suggest the bare string values rather than the equivalent
|
|
26
|
+
* syntax referencing the enumerations as defined here. Without care, it would
|
|
27
|
+
* be fairly trivial to lose consistency between the source "enum" and consuming
|
|
28
|
+
* code which we presume to be an exhaustive check (such as the `switch`
|
|
29
|
+
* statement operating on it below). This is somewhat mitigated for now by
|
|
30
|
+
* habitual use of {@link UnreachableError} (and would be better mitigated by a
|
|
31
|
+
* lint rule to enforce exhaustiveness checks over similar enumerations). But
|
|
32
|
+
* there is an obvious _stylistic mismatch_ between how an editor treats "thing
|
|
33
|
+
* shaped like `enum` but not semantically an `enum`", whereas there's no such
|
|
34
|
+
* mismatch in how it treats a plain "union of strings". If nothing else, that
|
|
35
|
+
* mismatch would tend to exacerbate exhaustiveness drift as an enumeration
|
|
36
|
+
* evolves.
|
|
37
|
+
*/
|
|
38
|
+
const DECLARE_NAMESPACE_RESULTS = {
|
|
39
|
+
SUCCESS: 'SUCCESS',
|
|
40
|
+
HOISTED: 'HOISTED',
|
|
41
|
+
DEFERRED: 'DEFERRED',
|
|
42
|
+
REDUNDANT: 'REDUNDANT',
|
|
43
|
+
CONFLICT: 'CONFLICT',
|
|
44
|
+
} as const;
|
|
45
|
+
|
|
46
|
+
type DeclareNamespaceResultEnum = typeof DECLARE_NAMESPACE_RESULTS;
|
|
47
|
+
|
|
48
|
+
type DeclareNamespaceResult = DeclareNamespaceResultEnum[keyof DeclareNamespaceResultEnum];
|
|
49
|
+
|
|
50
|
+
export class NamespaceDeclarationMap extends Map<string | null, NamespaceDeclaration> {
|
|
51
|
+
constructor(readonly subtree: NamedSubtreeDefinition) {
|
|
52
|
+
super();
|
|
53
|
+
|
|
54
|
+
this.declareNamespace(subtree);
|
|
55
|
+
|
|
56
|
+
const { attributes } = subtree;
|
|
57
|
+
|
|
58
|
+
if (attributes != null) {
|
|
59
|
+
for (const attribute of attributes.values()) {
|
|
60
|
+
this.declareNamespace(attribute);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* For any {@link definition | named node definition}, we can _infer_ a
|
|
67
|
+
* namespace declaration (rather than parsing it directly, which is error
|
|
68
|
+
* prone depending on parsing context) from that definition's
|
|
69
|
+
* {@link QualifiedName.namespaceURI} and {@link QualifiedName.prefix} (if the
|
|
70
|
+
* latter is defined).
|
|
71
|
+
*
|
|
72
|
+
* If a namespace declaration can be inferred, we "declare" (set, in
|
|
73
|
+
* {@link Map} semantics) it in **EITHER**:
|
|
74
|
+
*
|
|
75
|
+
* - An ancestor {@link NamedSubtreeDefinition | named subtree definition}'s
|
|
76
|
+
* {@link NamespaceDeclarationMap}: if such an ancestor exists and has no
|
|
77
|
+
* conflicting declaration for the same prefix; **OR**
|
|
78
|
+
* - This {@link NamespaceDeclarationMap}, if no suitable ancestor exists
|
|
79
|
+
*
|
|
80
|
+
* This can be described as "hoisting" the declaration to the uppermost node
|
|
81
|
+
* (or definitional representation of same) where it would be valid to declare
|
|
82
|
+
* the namespace for its prefix.
|
|
83
|
+
*
|
|
84
|
+
* In the following example, note that this logic applies for arbitrary tree
|
|
85
|
+
* structures satisfying the {@link NamedNodeDefinition} and
|
|
86
|
+
* {@link NamedSubtreeDefinition} interfaces. XML syntax is used to provide a
|
|
87
|
+
* concise explanation, but it should not be inferred that this is operating
|
|
88
|
+
* directly on an XML value (or any platform-native DOM structure of the
|
|
89
|
+
* same).
|
|
90
|
+
*
|
|
91
|
+
* @example Given an input tree like:
|
|
92
|
+
*
|
|
93
|
+
* ```xml
|
|
94
|
+
* <foo xmlns="https://example.com/DEFAULT_ONE">
|
|
95
|
+
* <bar:bat xmlns:bar="https://example.com/bar"/>
|
|
96
|
+
* <baz xmlns="https://example.com/DEFAULT_TWO"/>
|
|
97
|
+
* </foo>
|
|
98
|
+
* ```
|
|
99
|
+
*
|
|
100
|
+
* The namespace declarations will be assigned as if they'd been declared
|
|
101
|
+
* like:
|
|
102
|
+
*
|
|
103
|
+
* ```xml
|
|
104
|
+
* <foo
|
|
105
|
+
* xmlns="https://example.com/DEFAULT_ONE"
|
|
106
|
+
* xmlns:bar="https://example.com/bar"
|
|
107
|
+
* >
|
|
108
|
+
* <!-- bar declaration has no conflict in foo, hoisted to parent -->
|
|
109
|
+
* <bar:bat/>
|
|
110
|
+
* <!-- default declaration conflicts with foo's default, not hoisted -->
|
|
111
|
+
* <baz xmlns="https://example.com/DEFAULT_TWO"/>
|
|
112
|
+
* </foo>
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* **IMPORTANT:** this behavior may seem overly complicated! It should be
|
|
116
|
+
* noted that the behavior:
|
|
117
|
+
*
|
|
118
|
+
* 1. ... is conceptually similar to behavior observable in a web standard
|
|
119
|
+
* WHAT Working Group DOM (as in browser DOM, XML DOM) implementation.
|
|
120
|
+
* There, serializing any subtree element will produce namespace
|
|
121
|
+
* declarations on the root element for any namespaces _referenced within
|
|
122
|
+
* its subtree but declared on an ancestor_. Note that in this case, the
|
|
123
|
+
* hierarchical behavior is inverted, but it demonstrates the same
|
|
124
|
+
* effective namespace scoping semantics.
|
|
125
|
+
*
|
|
126
|
+
* 2. ... vastly simplifies our ability to produce a compact XML
|
|
127
|
+
* representation from any arbitrary tree representation of its nodes.
|
|
128
|
+
* Hoisting namespace declarations to their uppermost scope, and
|
|
129
|
+
* deduplicating recursively up the ancestor tree, ensures that we only
|
|
130
|
+
* declare a given namespace once as it is referenced.
|
|
131
|
+
*
|
|
132
|
+
* @todo While this design is intended to help with producing compact
|
|
133
|
+
* serialized XML, at time of writing there is still an aspect which is
|
|
134
|
+
* unaddressed in the serialization logic: we assume namespace declarations
|
|
135
|
+
* are referenced if they've been parsed. This logic doesn't hold for nodes
|
|
136
|
+
* which are ultimately omitted from serialization, which would occur for
|
|
137
|
+
* non-relevant nodes, and repeat ranges with zero repeat instances (or any of
|
|
138
|
+
* their descendants). A future iteration of this same behavior could produce
|
|
139
|
+
* XML which is theoretically more compact, by performing the same declaration
|
|
140
|
+
* hoisting logic _dynamically at call time_ rather than at parse time.
|
|
141
|
+
*/
|
|
142
|
+
declareNamespace(definition: NamedNodeDefinition): DeclareNamespaceResult {
|
|
143
|
+
const { prefix, namespaceURI } = definition.qualifiedName;
|
|
144
|
+
|
|
145
|
+
if (typeof prefix === 'symbol') {
|
|
146
|
+
return DECLARE_NAMESPACE_RESULTS.DEFERRED;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const parentNamespaceDeclarations = this.subtree.parent?.namespaceDeclarations;
|
|
150
|
+
|
|
151
|
+
if (parentNamespaceDeclarations != null) {
|
|
152
|
+
const ancestorResult = parentNamespaceDeclarations.declareNamespace(definition);
|
|
153
|
+
|
|
154
|
+
switch (ancestorResult) {
|
|
155
|
+
case DECLARE_NAMESPACE_RESULTS.CONFLICT:
|
|
156
|
+
break;
|
|
157
|
+
|
|
158
|
+
case DECLARE_NAMESPACE_RESULTS.DEFERRED:
|
|
159
|
+
return ancestorResult;
|
|
160
|
+
|
|
161
|
+
case DECLARE_NAMESPACE_RESULTS.HOISTED:
|
|
162
|
+
return ancestorResult;
|
|
163
|
+
|
|
164
|
+
case DECLARE_NAMESPACE_RESULTS.SUCCESS:
|
|
165
|
+
case DECLARE_NAMESPACE_RESULTS.REDUNDANT:
|
|
166
|
+
return DECLARE_NAMESPACE_RESULTS.HOISTED;
|
|
167
|
+
|
|
168
|
+
default:
|
|
169
|
+
throw new UnreachableError(ancestorResult);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const currentDeclaration = this.get(prefix);
|
|
174
|
+
|
|
175
|
+
if (currentDeclaration == null) {
|
|
176
|
+
this.set(
|
|
177
|
+
prefix,
|
|
178
|
+
new NamespaceDeclaration({
|
|
179
|
+
declaredPrefix: prefix,
|
|
180
|
+
declaredURI: namespaceURI,
|
|
181
|
+
})
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
return DECLARE_NAMESPACE_RESULTS.SUCCESS;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (currentDeclaration.declaresNamespaceURI(namespaceURI)) {
|
|
188
|
+
return DECLARE_NAMESPACE_RESULTS.REDUNDANT;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return DECLARE_NAMESPACE_RESULTS.CONFLICT;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Given a {@link namespaceURI}, resolves a declared prefix (which may be
|
|
196
|
+
* `null`) for the {@link subtree} context **or any of its ancestors**. This
|
|
197
|
+
* is an important semantic detail:
|
|
198
|
+
*
|
|
199
|
+
* - Namespace declarations on a given subtree are effective for all of its
|
|
200
|
+
* descendants _until another declaration for the same prefix or namespace
|
|
201
|
+
* URI is encountered_
|
|
202
|
+
* - We "hoist" namespace declarations up to the uppermost {@link subtree}'s
|
|
203
|
+
* {@link NamespaceDeclarationMap} during parsing (as described in more
|
|
204
|
+
* detail on {@link declareNamespace}).
|
|
205
|
+
*/
|
|
206
|
+
lookupPrefix(namespaceURI: NamespaceURI): string | null {
|
|
207
|
+
const namespace = String(namespaceURI);
|
|
208
|
+
|
|
209
|
+
// Note: this is a dynamic lookup on the _very unlikely_ chance that a
|
|
210
|
+
// lookup occurs while parsing is still in progress. It's expected that we
|
|
211
|
+
// collect all namespace declarations by the time parsing is complete, at
|
|
212
|
+
// which point we could theoretically collect a companion map where the
|
|
213
|
+
// namespace URI is used as a key. This has been deferred for now, because
|
|
214
|
+
// we'd need:
|
|
215
|
+
//
|
|
216
|
+
// 1. To know _in this class_ when parsing is complete (which seems like a
|
|
217
|
+
// huge excess of mixed responsibilities!)
|
|
218
|
+
// 2. To resolve the "object-as-value-as-map-key" problem, which has also
|
|
219
|
+
// been deferred.
|
|
220
|
+
for (const namespaceDeclaration of this.values()) {
|
|
221
|
+
if (String(namespaceDeclaration.declaredURI) === namespace) {
|
|
222
|
+
return namespaceDeclaration.declaredPrefix;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return this.subtree.parent?.namespaceDeclarations.lookupPrefix(namespaceURI) ?? null;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { NamespaceDeclaration } from './NamespaceDeclaration.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Convenience wrapper to represent an XML namespace URI as a {@link URL}. This
|
|
5
|
+
* representation is used/responsible for:
|
|
6
|
+
*
|
|
7
|
+
* - normalized logic for XML semantics around special namespace URI values, in
|
|
8
|
+
* particular for consistent handling of the "null namespace" (input for such
|
|
9
|
+
* is accepted as either an empty string or `null`)
|
|
10
|
+
* - validation of input: a non-"null namespace" value will be rejected if it is
|
|
11
|
+
* not a valid URI string
|
|
12
|
+
* - type-level distinction between a namespace URI and a
|
|
13
|
+
* {@link NamespaceDeclaration.declaredPrefix | namespace declaration's prefix},
|
|
14
|
+
* as an aide to avoid using one in place of the other as e.g. a positional
|
|
15
|
+
* argument
|
|
16
|
+
*
|
|
17
|
+
* @todo Test the finer distinctions between "URL" and "URI"!
|
|
18
|
+
*
|
|
19
|
+
* @todo Probably not a huge deal in the scheme of things, but this is almost
|
|
20
|
+
* entirely pure overhead at runtime! The "validation" use case is kind of a
|
|
21
|
+
* stretch, and may well be wrong. The type-level distinction from a namespace
|
|
22
|
+
* prefix, however, has proved useful **quite a few times** during iteration of
|
|
23
|
+
* this change. If we can actually measure an impact, it might be worth instead
|
|
24
|
+
* considering "branded types" for the type-level distinct (in which case we
|
|
25
|
+
* could use a factory function to handle both the branding and special XML
|
|
26
|
+
* semantics).
|
|
27
|
+
*/
|
|
28
|
+
export class NamespaceURL extends URL {
|
|
29
|
+
static from(namespaceURI: NamespaceURL | string | null): NamespaceURL | null {
|
|
30
|
+
if (namespaceURI == null || namespaceURI === '') {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return new this(String(namespaceURI));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
override readonly href: string;
|
|
38
|
+
|
|
39
|
+
private constructor(href: string) {
|
|
40
|
+
super(href);
|
|
41
|
+
|
|
42
|
+
this.href = href;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import type { XPathDOMAdapter } from '@getodk/xpath';
|
|
2
|
+
import { NamespaceURL } from './NamespaceURL.ts';
|
|
3
|
+
|
|
4
|
+
export type NamespaceURI = NamespaceURL | null;
|
|
5
|
+
export type QualifiedNamePrefix = string | null;
|
|
6
|
+
|
|
7
|
+
export interface NamespaceQualifiedNameSource {
|
|
8
|
+
readonly namespaceURI: NamespaceURI | string;
|
|
9
|
+
readonly localName: string;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Note that this property is intentionally optional as one of the
|
|
13
|
+
* {@link QualifiedNameSource | QualifiedName source input}, and its absence
|
|
14
|
+
* is treated differently from an explicitly assigned `null` value.
|
|
15
|
+
*
|
|
16
|
+
* @see {@link SourcePrefixUnspecified}, {@link DeferredPrefix}
|
|
17
|
+
*/
|
|
18
|
+
readonly prefix?: QualifiedNamePrefix;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const SOURCE_PREFIX_UNSPECIFIED = Symbol('SOURCE_PREFIX_UNSPECIFIED');
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* May be used as a placeholder for a {@link QualifiedName.prefix}, where the
|
|
25
|
+
* actual prefix may not be known at definition time.
|
|
26
|
+
*
|
|
27
|
+
* Example: parsing non-XML sources into an XML-like tree, e.g. for XPath
|
|
28
|
+
* evaluation; in which case, we may not need to resolve a prefix for the name
|
|
29
|
+
* until such a node is serialized as XML, if it ever is.
|
|
30
|
+
*/
|
|
31
|
+
type SourcePrefixUnspecified = typeof SOURCE_PREFIX_UNSPECIFIED;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Represents a {@link QualifiedName.prefix} whose resolution may be deferred,
|
|
35
|
+
* e.g. until all requisite parsing is complete and/or until XML serialization
|
|
36
|
+
* requires use of a prefix to represent the corresponding
|
|
37
|
+
* {@link QualifiedName.namespaceURI}.
|
|
38
|
+
*/
|
|
39
|
+
// prettier-ignore
|
|
40
|
+
type DeferredPrefix =
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/sort-type-constituents
|
|
42
|
+
| string
|
|
43
|
+
| null
|
|
44
|
+
| SourcePrefixUnspecified;
|
|
45
|
+
|
|
46
|
+
interface DeferredPrefixedQualifiedNameSource {
|
|
47
|
+
readonly namespaceURI: NamespaceURI | string;
|
|
48
|
+
readonly prefix: DeferredPrefix;
|
|
49
|
+
readonly localName: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// prettier-ignore
|
|
53
|
+
export type QualifiedNameSource =
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/sort-type-constituents
|
|
55
|
+
| NamespaceQualifiedNameSource
|
|
56
|
+
| DeferredPrefixedQualifiedNameSource;
|
|
57
|
+
|
|
58
|
+
interface PrefixResolutionOptions {
|
|
59
|
+
lookupPrefix(namespaceURI: NamespaceURI | string): string | null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @todo This is in the `lib` directory because it's a cross-cutting concern,
|
|
64
|
+
* applicable to:
|
|
65
|
+
*
|
|
66
|
+
* - Parsing XML into a useful runtime data model (usage which motivated initial
|
|
67
|
+
* development of this class)
|
|
68
|
+
* - Serializing XML from a runtime data model (also motivated initial dev)
|
|
69
|
+
* - `@getodk/xpath` (internal) e.g. references to
|
|
70
|
+
* {@link https://www.w3.org/TR/REC-xml-names/#NT-QName | QName} in various
|
|
71
|
+
* parts of XPath expression _syntax_, as well as various parts of the package
|
|
72
|
+
* interpreting those parts of syntax
|
|
73
|
+
* - `@getodk/xpath` (cross-package), e.g. in aspects of the
|
|
74
|
+
* {@link XPathDOMAdapter} APIs, and implementations thereof
|
|
75
|
+
* - A zillion potential optimizations, e.g. where names are useful in a lookup
|
|
76
|
+
* table (or used in conjunction with other information to construct keys for
|
|
77
|
+
* same)
|
|
78
|
+
*
|
|
79
|
+
* @todo As a cross-cutting concern, there are subtle but important differences
|
|
80
|
+
* between certain XPath and XML semantics around expressions of a "null"
|
|
81
|
+
* {@link prefix}. E.g. in the expression `/foo`, **technically** the `foo` Step
|
|
82
|
+
* should select child elements _in the null namespace_, whereas in most other
|
|
83
|
+
* cases a null prefix (when explicitly assigned `null`, rather than
|
|
84
|
+
* {@link DeferredPrefix | deferred for later resolution}) is expected to
|
|
85
|
+
* correspond _to the default namespace_ (whatever that is in the context of the
|
|
86
|
+
* {@link QualifiedName | qualified-named thing}).
|
|
87
|
+
*
|
|
88
|
+
* @todo As a mechanism for many optimizations, an evolution of this class would
|
|
89
|
+
* be **BY FAR** most useful if it can be treated as a _value type_, despite
|
|
90
|
+
* challenges using non-primitives as such in a JS runtime. To be clear: it
|
|
91
|
+
* would be most useful if every instance of {@link QualifiedName} having the
|
|
92
|
+
* same property values (or in some cases, the same combined
|
|
93
|
+
* {@link namespaceURI}/{@link localName} or combined
|
|
94
|
+
* {@link prefix}/{@link localName}) would also have _reference equality_ with
|
|
95
|
+
* other instances having the same property values (or pertinent subset
|
|
96
|
+
* thereof). Making a somewhat obvious point explicit: this would be
|
|
97
|
+
* particularly useful in cases where a lookup table is implemented as a native
|
|
98
|
+
* {@link Map}, where using {@link QualifiedName} as a key would break
|
|
99
|
+
* expectations (and probably quite a lot of functionality!) if 2+ equivalent
|
|
100
|
+
* keys mapped to different values.
|
|
101
|
+
*
|
|
102
|
+
* @todo Where we would want to treat instances as a value type, it would be
|
|
103
|
+
* useful to look at prior art for representation of the same data as a string.
|
|
104
|
+
* One frame of reference worth looking at is
|
|
105
|
+
* {@link https://www.w3.org/TR/xpath-30/#prod-xpath30-URIQualifiedName | XPath 3.0's URIQualifiedName}
|
|
106
|
+
* (but note that this syntax is mutually exclusive with the prefixed `QName`).
|
|
107
|
+
*/
|
|
108
|
+
export class QualifiedName implements DeferredPrefixedQualifiedNameSource {
|
|
109
|
+
private readonly defaultPrefixResolutionOptions: PrefixResolutionOptions;
|
|
110
|
+
|
|
111
|
+
readonly namespaceURI: NamespaceURI;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @see {@link SourcePrefixUnspecified}, {@link DeferredPrefix}
|
|
115
|
+
*/
|
|
116
|
+
readonly prefix: DeferredPrefix;
|
|
117
|
+
|
|
118
|
+
readonly localName: string;
|
|
119
|
+
|
|
120
|
+
constructor(source: QualifiedNameSource) {
|
|
121
|
+
const { localName } = source;
|
|
122
|
+
|
|
123
|
+
let prefix = source.prefix;
|
|
124
|
+
|
|
125
|
+
if (typeof prefix === 'undefined') {
|
|
126
|
+
prefix = SOURCE_PREFIX_UNSPECIFIED;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const namespaceURI = NamespaceURL.from(source.namespaceURI);
|
|
130
|
+
|
|
131
|
+
this.namespaceURI = namespaceURI;
|
|
132
|
+
this.prefix = prefix;
|
|
133
|
+
this.localName = localName;
|
|
134
|
+
|
|
135
|
+
this.defaultPrefixResolutionOptions = {
|
|
136
|
+
lookupPrefix: () => {
|
|
137
|
+
if (prefix === SOURCE_PREFIX_UNSPECIFIED) {
|
|
138
|
+
throw new Error(`Failed to resolve prefix for namespace URI: ${String(namespaceURI)}`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return prefix;
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @todo at time of writing, it's not expected we will actually supply
|
|
148
|
+
* {@link options} in calls to this method! Current calls are from definitions
|
|
149
|
+
* whose prefixes are known at parse time (i.e. they are prefixed in the
|
|
150
|
+
* source XML from which they're parsed).
|
|
151
|
+
*
|
|
152
|
+
* The intent of accepting the options here now is to leave a fairly large
|
|
153
|
+
* breadcrumb, for any use case where we might want to serialize XML from
|
|
154
|
+
* artificially constructed DOM-like trees (e.g. `StaticNode` implementations
|
|
155
|
+
* defined by parsing non-XML external secondary instances such as CSV and
|
|
156
|
+
* GeoJSON). AFAIK this doesn't correspond to any known feature in the "like
|
|
157
|
+
* Collect" scope, but it could have implications for inspecting form details
|
|
158
|
+
* in e.g. "debug/form design/dev mode" scenarios.
|
|
159
|
+
*/
|
|
160
|
+
getPrefixedName(options: PrefixResolutionOptions = this.defaultPrefixResolutionOptions): string {
|
|
161
|
+
const { namespaceURI, localName } = this;
|
|
162
|
+
const prefix = options.lookupPrefix(namespaceURI);
|
|
163
|
+
|
|
164
|
+
if (prefix == null) {
|
|
165
|
+
return localName;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return `${prefix}:${localName}`;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const parseToInteger = (value: string | null): number | null => {
|
|
2
|
+
if (value === null) {
|
|
3
|
+
return null;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const parsed = Number(value);
|
|
7
|
+
if (typeof value !== 'string' || value.trim() === '' || !Number.isInteger(parsed)) {
|
|
8
|
+
throw new Error(`Expected an integer, but got: ${value}`);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return parsed;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const parseToFloat = (value: string | null): number | null => {
|
|
15
|
+
if (value === null) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const parsed = Number(value);
|
|
20
|
+
if (typeof value !== 'string' || value.trim() === '' || Number.isNaN(parsed)) {
|
|
21
|
+
throw new Error(`Expected a float, but got: ${value}`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return parsed;
|
|
25
|
+
};
|
|
@@ -8,6 +8,13 @@ import type { SimpleAtomicState, SimpleAtomicStateSetter } from './types.ts';
|
|
|
8
8
|
|
|
9
9
|
type InitialValueSource = 'FORM_DEFAULT' | 'PRIMARY_INSTANCE';
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* @todo {@link InitialValueSource} naming leaves a lot to be desired. As described in {@link InstanceValueStateOptions.initialValueSource}, this check (for now) will effectively answer the question: "are we **NOT** editing instance state (e.g. a submission)?". This answer, in turn, determines whether to {@link setPreloadUIDValue}
|
|
13
|
+
*/
|
|
14
|
+
const isInstanceFirstLoad = (valueSource?: InitialValueSource) => {
|
|
15
|
+
return valueSource === 'FORM_DEFAULT';
|
|
16
|
+
};
|
|
17
|
+
|
|
11
18
|
export interface InstanceValueStateOptions {
|
|
12
19
|
/**
|
|
13
20
|
* Specifies the source of a {@link createInstanceValueState} signal's initial
|
|
@@ -106,10 +113,47 @@ const guardDownstreamReadonlyWrites = (
|
|
|
106
113
|
return [getValue, setValue];
|
|
107
114
|
};
|
|
108
115
|
|
|
116
|
+
/**
|
|
117
|
+
* Per {@link https://getodk.github.io/xforms-spec/#preload-attributes:~:text=concatenation%20of%20%E2%80%98uuid%3A%E2%80%99%20and%20uuid()}
|
|
118
|
+
*/
|
|
119
|
+
const PRELOAD_UID_EXPRESSION = 'concat("uuid:", uuid())';
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @todo This is a temporary one-off, until we support the full range of
|
|
123
|
+
* {@link https://getodk.github.io/xforms-spec/#preload-attributes | preloads}.
|
|
124
|
+
*
|
|
125
|
+
* @todo ALSO, IMPORTANTLY(!): the **call site** for this function is
|
|
126
|
+
* semantically where we would expect to trigger a
|
|
127
|
+
* {@link https://getodk.github.io/xforms-spec/#event:odk-instance-first-load | odk-instance-first-load event},
|
|
128
|
+
* _and compute_ preloads semantically associated with that event.
|
|
129
|
+
*/
|
|
130
|
+
const setPreloadUIDValue = (
|
|
131
|
+
context: InstanceValueContext,
|
|
132
|
+
valueState: RelevantValueState,
|
|
133
|
+
options: InstanceValueStateOptions
|
|
134
|
+
): void => {
|
|
135
|
+
const { preload } = context.definition.bind;
|
|
136
|
+
|
|
137
|
+
if (preload?.type !== 'uid' || !isInstanceFirstLoad(options?.initialValueSource)) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const preloadUIDValue = context.evaluator.evaluateString(PRELOAD_UID_EXPRESSION, {
|
|
142
|
+
contextNode: context.contextNode,
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const [, setValue] = valueState;
|
|
146
|
+
|
|
147
|
+
setValue(preloadUIDValue);
|
|
148
|
+
};
|
|
149
|
+
|
|
109
150
|
/**
|
|
110
151
|
* Defines a reactive effect which writes the result of `calculate` bind
|
|
111
152
|
* computations to the provided value setter, on initialization and any
|
|
112
153
|
* subsequent reactive update.
|
|
154
|
+
*
|
|
155
|
+
* @see {@link setPreloadUIDValue} for important details about spec ordering of
|
|
156
|
+
* events and computations.
|
|
113
157
|
*/
|
|
114
158
|
const createCalculation = (
|
|
115
159
|
context: InstanceValueContext,
|
|
@@ -153,6 +197,12 @@ export const createInstanceValueState = (
|
|
|
153
197
|
const initialValue = getInitialValue(context, options);
|
|
154
198
|
const baseValueState = createSignal(initialValue);
|
|
155
199
|
const relevantValueState = createRelevantValueState(context, baseValueState);
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* @see {@link setPreloadUIDValue} for important details about spec ordering of events and computations.
|
|
203
|
+
*/
|
|
204
|
+
setPreloadUIDValue(context, relevantValueState, options);
|
|
205
|
+
|
|
156
206
|
const { calculate } = context.definition.bind;
|
|
157
207
|
|
|
158
208
|
if (calculate != null) {
|