@getodk/xforms-engine 0.8.0 → 0.10.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 (47) hide show
  1. package/dist/client/InputNode.d.ts +4 -2
  2. package/dist/client/NoteNode.d.ts +4 -2
  3. package/dist/client/SelectNode.d.ts +1 -0
  4. package/dist/client/TextRange.d.ts +4 -0
  5. package/dist/index.js +14474 -28463
  6. package/dist/index.js.map +1 -1
  7. package/dist/instance/SelectControl.d.ts +1 -0
  8. package/dist/instance/text/TextRange.d.ts +11 -1
  9. package/dist/integration/xpath/adapter/traversal.d.ts +2 -2
  10. package/dist/lib/codecs/DateValueCodec.d.ts +7 -0
  11. package/dist/lib/codecs/getSharedValueCodec.d.ts +3 -2
  12. package/dist/lib/reactivity/text/createTextRange.d.ts +1 -2
  13. package/dist/parse/expression/TextChunkExpression.d.ts +16 -0
  14. package/dist/parse/text/ItemsetLabelDefinition.d.ts +2 -4
  15. package/dist/parse/text/MessageDefinition.d.ts +3 -7
  16. package/dist/parse/text/abstract/TextElementDefinition.d.ts +2 -8
  17. package/dist/parse/text/abstract/TextRangeDefinition.d.ts +2 -2
  18. package/dist/solid.js +14474 -28463
  19. package/dist/solid.js.map +1 -1
  20. package/package.json +4 -3
  21. package/src/client/InputNode.ts +4 -0
  22. package/src/client/NoteNode.ts +4 -0
  23. package/src/client/SelectNode.ts +2 -0
  24. package/src/client/TextRange.ts +5 -1
  25. package/src/instance/SelectControl.ts +6 -0
  26. package/src/instance/text/TextRange.ts +21 -1
  27. package/src/integration/xpath/adapter/traversal.ts +4 -2
  28. package/src/lib/codecs/DateValueCodec.ts +94 -0
  29. package/src/lib/codecs/getSharedValueCodec.ts +5 -3
  30. package/src/lib/reactivity/text/createTextRange.ts +56 -43
  31. package/src/lib/reactivity/validation/createValidation.ts +1 -3
  32. package/src/parse/expression/TextChunkExpression.ts +78 -0
  33. package/src/parse/text/ItemsetLabelDefinition.ts +8 -12
  34. package/src/parse/text/MessageDefinition.ts +9 -16
  35. package/src/parse/text/abstract/TextElementDefinition.ts +10 -26
  36. package/src/parse/text/abstract/TextRangeDefinition.ts +2 -2
  37. package/src/parse/xpath/semantic-analysis.ts +7 -2
  38. package/dist/parse/expression/TextLiteralExpression.d.ts +0 -9
  39. package/dist/parse/expression/TextOutputExpression.d.ts +0 -7
  40. package/dist/parse/expression/TextReferenceExpression.d.ts +0 -7
  41. package/dist/parse/expression/TextTranslationExpression.d.ts +0 -8
  42. package/dist/parse/expression/abstract/TextChunkExpression.d.ts +0 -17
  43. package/src/parse/expression/TextLiteralExpression.ts +0 -19
  44. package/src/parse/expression/TextOutputExpression.ts +0 -25
  45. package/src/parse/expression/TextReferenceExpression.ts +0 -14
  46. package/src/parse/expression/TextTranslationExpression.ts +0 -38
  47. package/src/parse/expression/abstract/TextChunkExpression.ts +0 -38
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getodk/xforms-engine",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "XForms engine for ODK Web Forms",
6
6
  "type": "module",
@@ -56,12 +56,13 @@
56
56
  "dependencies": {
57
57
  "bin-packer": "1.7.0",
58
58
  "papaparse": "^5.5.2",
59
- "solid-js": "^1.9.5"
59
+ "solid-js": "^1.9.5",
60
+ "temporal-polyfill": "^0.3.0"
60
61
  },
61
62
  "devDependencies": {
62
63
  "@babel/core": "^7.26.10",
63
64
  "@getodk/tree-sitter-xpath": "0.1.3",
64
- "@getodk/xpath": "0.4.1",
65
+ "@getodk/xpath": "0.6.0",
65
66
  "@playwright/test": "^1.49.1",
66
67
  "@types/papaparse": "^5.3.15",
67
68
  "@vitest/browser": "^3.0.9",
@@ -84,11 +84,13 @@ export interface InputNode<V extends ValueType = ValueType>
84
84
  export type StringInputValue = InputValue<'string'>;
85
85
  export type IntInputValue = InputValue<'int'>;
86
86
  export type DecimalInputValue = InputValue<'decimal'>;
87
+ export type DateInputValue = InputValue<'date'>;
87
88
  export type GeopointInputValue = InputValue<'geopoint'>;
88
89
 
89
90
  export type StringInputNode = InputNode<'string'>;
90
91
  export type IntInputNode = InputNode<'int'>;
91
92
  export type DecimalInputNode = InputNode<'decimal'>;
93
+ export type DateInputNode = InputNode<'date'>;
92
94
  export type GeopointInputNode = InputNode<'geopoint'>;
93
95
 
94
96
  // prettier-ignore
@@ -97,6 +99,7 @@ type SupportedInputValueType =
97
99
  | 'string'
98
100
  | 'int'
99
101
  | 'decimal'
102
+ | 'date'
100
103
  | 'geopoint';
101
104
 
102
105
  type TemporaryStringValueType = Exclude<ValueType, SupportedInputValueType>;
@@ -109,5 +112,6 @@ export type AnyInputNode =
109
112
  | StringInputNode
110
113
  | IntInputNode
111
114
  | DecimalInputNode
115
+ | DateInputNode
112
116
  | GeopointInputNode
113
117
  | TemporaryStringValueInputNode;
@@ -81,11 +81,13 @@ export interface NoteNode<V extends ValueType = ValueType> extends BaseValueNode
81
81
  export type StringNoteValue = NoteValue<'string'>;
82
82
  export type IntNoteValue = NoteValue<'int'>;
83
83
  export type DecimalNoteValue = NoteValue<'decimal'>;
84
+ export type DateNoteValue = NoteValue<'date'>;
84
85
  export type GeopointNoteValue = NoteValue<'geopoint'>;
85
86
 
86
87
  export type StringNoteNode = NoteNode<'string'>;
87
88
  export type IntNoteNode = NoteNode<'int'>;
88
89
  export type DecimalNoteNode = NoteNode<'decimal'>;
90
+ export type DateNoteNode = NoteNode<'date'>;
89
91
  export type GeopointNoteNode = NoteNode<'geopoint'>;
90
92
 
91
93
  // prettier-ignore
@@ -94,6 +96,7 @@ type SupportedNoteValueType =
94
96
  | 'string'
95
97
  | 'int'
96
98
  | 'decimal'
99
+ | 'date'
97
100
  | 'geopoint';
98
101
 
99
102
  type TemporaryStringValueType = Exclude<ValueType, SupportedNoteValueType>;
@@ -106,5 +109,6 @@ export type AnyNoteNode =
106
109
  | StringNoteNode
107
110
  | IntNoteNode
108
111
  | DecimalNoteNode
112
+ | DateNoteNode
109
113
  | GeopointNoteNode
110
114
  | TemporaryStringValueNoteNode;
@@ -19,6 +19,8 @@ export interface SelectItem {
19
19
  export type SelectValueOptions = readonly SelectItem[];
20
20
 
21
21
  export interface SelectNodeState extends BaseValueNodeState<readonly string[]> {
22
+ get isSelectWithImages(): boolean;
23
+
22
24
  get children(): null;
23
25
 
24
26
  get valueOptions(): readonly SelectItem[];
@@ -1,5 +1,5 @@
1
+ import { JRResourceURL } from '@getodk/common/jr-resources/JRResourceURL.ts';
1
2
  import type { ActiveLanguage } from './FormLanguage.ts';
2
- import type { RootNodeState } from './RootNode.ts';
3
3
 
4
4
  /**
5
5
  * **COMMENTARY**
@@ -156,4 +156,8 @@ export interface TextRange<Role extends TextRole, Origin extends TextOrigin = Te
156
156
 
157
157
  get asString(): string;
158
158
  get formatted(): unknown;
159
+
160
+ get imageSource(): JRResourceURL | undefined;
161
+ get audioSource(): JRResourceURL | undefined;
162
+ get videoSource(): JRResourceURL | undefined;
159
163
  }
@@ -51,6 +51,7 @@ interface SelectControlStateSpec extends ValueNodeStateSpec<readonly string[]> {
51
51
  readonly label: Accessor<TextRange<'label'> | null>;
52
52
  readonly hint: Accessor<TextRange<'hint'> | null>;
53
53
  readonly valueOptions: Accessor<SelectValueOptions>;
54
+ readonly isSelectWithImages: Accessor<boolean>;
54
55
  }
55
56
 
56
57
  export class SelectControl
@@ -109,6 +110,10 @@ export class SelectControl
109
110
 
110
111
  const valueOptions = createItemCollection(this);
111
112
 
113
+ const isSelectWithImages = this.scope.runTask(() => {
114
+ return createMemo(() => valueOptions().some((item) => !!item.label.imageSource));
115
+ });
116
+
112
117
  const mapOptionsByValue: Accessor<SelectItemMap> = this.scope.runTask(() => {
113
118
  return createMemo(() => {
114
119
  return new Map(valueOptions().map((item) => [item.value, item]));
@@ -150,6 +155,7 @@ export class SelectControl
150
155
  valueOptions,
151
156
  value: valueState,
152
157
  instanceValue: this.getInstanceValue,
158
+ isSelectWithImages,
153
159
  },
154
160
  this.instanceConfig
155
161
  );
@@ -1,3 +1,4 @@
1
+ import { JRResourceURL } from '@getodk/common/jr-resources/JRResourceURL.ts';
1
2
  import type {
2
3
  TextRange as ClientTextRange,
3
4
  TextChunk,
@@ -6,6 +7,12 @@ import type {
6
7
  } from '../../client/TextRange.ts';
7
8
  import { FormattedTextStub } from './FormattedTextStub.ts';
8
9
 
10
+ export interface MediaSources {
11
+ image?: JRResourceURL;
12
+ video?: JRResourceURL;
13
+ audio?: JRResourceURL;
14
+ }
15
+
9
16
  export class TextRange<Role extends TextRole, Origin extends TextOrigin>
10
17
  implements ClientTextRange<Role, Origin>
11
18
  {
@@ -21,9 +28,22 @@ export class TextRange<Role extends TextRole, Origin extends TextOrigin>
21
28
  return this.chunks.map((chunk) => chunk.asString).join('');
22
29
  }
23
30
 
31
+ get imageSource(): JRResourceURL | undefined {
32
+ return this.mediaSources?.image;
33
+ }
34
+
35
+ get audioSource(): JRResourceURL | undefined {
36
+ return this.mediaSources?.audio;
37
+ }
38
+
39
+ get videoSource(): JRResourceURL | undefined {
40
+ return this.mediaSources?.video;
41
+ }
42
+
24
43
  constructor(
25
44
  readonly origin: Origin,
26
45
  readonly role: Role,
27
- protected readonly chunks: readonly TextChunk[]
46
+ protected readonly chunks: readonly TextChunk[],
47
+ protected readonly mediaSources?: MediaSources
28
48
  ) {}
29
49
  }
@@ -18,7 +18,9 @@ export const getContainingEngineXPathDocument = (node: EngineXPathNode): EngineX
18
18
  return node.rootDocument;
19
19
  };
20
20
 
21
- export const getEngineXPathAttributes = (node: EngineXPathNode): Iterable<EngineXPathAttribute> => {
21
+ export const getEngineXPathAttributes = (
22
+ node: EngineXPathNode
23
+ ): readonly EngineXPathAttribute[] => {
22
24
  if (node.nodeType === 'static-element') {
23
25
  return node.attributes;
24
26
  }
@@ -39,7 +41,7 @@ export const getEngineXPathAttributes = (node: EngineXPathNode): Iterable<Engine
39
41
  * it throw? It might be nice to be alerted if the assumptions in point 2 above
40
42
  * are somehow wrong (or become wrong).
41
43
  */
42
- export const getNamespaceDeclarations = (): Iterable<never> => [];
44
+ export const getNamespaceDeclarations = (): readonly [] => [];
43
45
 
44
46
  export const getParentNode = (node: EngineXPathNode): EngineXPathParentNode | null => {
45
47
  if (node.nodeType === 'repeat-instance') {
@@ -0,0 +1,94 @@
1
+ import { ISO_DATE_OR_DATE_TIME_NO_OFFSET_PATTERN } from '@getodk/common/constants/datetime.ts';
2
+ import { Temporal } from 'temporal-polyfill';
3
+ import { type CodecDecoder, type CodecEncoder, ValueCodec } from './ValueCodec.ts';
4
+
5
+ export type DatetimeRuntimeValue = Temporal.PlainDate | null;
6
+
7
+ export type DatetimeInputValue =
8
+ | Date
9
+ | Temporal.PlainDate
10
+ | Temporal.PlainDateTime
11
+ | Temporal.ZonedDateTime
12
+ | string
13
+ | null;
14
+
15
+ /**
16
+ * Parses a string in the format 'YYYY-MM-DD' or 'YYYY-MM-DDTHH:MM:SS' (no offset)
17
+ * into a Temporal.PlainDate.
18
+ * TODO: Datetimes with a valid timezone offset are treated as errors.
19
+ * User research is needed to determine whether the date should honor
20
+ * the timezone or be truncated to the yyyy-mm-dd format only.
21
+ *
22
+ * @param value - The string to parse.
23
+ * @returns A {@link DatetimeRuntimeValue}
24
+ */
25
+ const parseString = (value: string): DatetimeRuntimeValue => {
26
+ if (
27
+ value == null ||
28
+ typeof value !== 'string' ||
29
+ !ISO_DATE_OR_DATE_TIME_NO_OFFSET_PATTERN.test(value)
30
+ ) {
31
+ return null;
32
+ }
33
+
34
+ try {
35
+ const dateOnly = value.split('T')[0]!;
36
+ return Temporal.PlainDate.from(dateOnly);
37
+ } catch {
38
+ // TODO: should we throw when codec cannot interpret the value?
39
+ return null;
40
+ }
41
+ };
42
+
43
+ /**
44
+ * Converts a date-like value ({@link DatetimeInputValue}) to a 'YYYY-MM-DD' string.
45
+ * TODO: Datetimes with a valid timezone offset are treated as errors.
46
+ * User research is needed to determine whether the date should honor
47
+ * the timezone or be truncated to the yyyy-mm-dd format only.
48
+ *
49
+ * @param value - The value to convert.
50
+ * @returns A date string or empty string if invalid.
51
+ */
52
+ const toDateString = (value: DatetimeInputValue): string => {
53
+ if (value == null || value instanceof Temporal.ZonedDateTime) {
54
+ return '';
55
+ }
56
+
57
+ try {
58
+ if (value instanceof Temporal.PlainDate) {
59
+ return value.toString();
60
+ }
61
+
62
+ if (value instanceof Temporal.PlainDateTime) {
63
+ return value.toPlainDate().toString();
64
+ }
65
+
66
+ if (value instanceof Date) {
67
+ return Temporal.PlainDate.from({
68
+ year: value.getFullYear(),
69
+ month: value.getMonth() + 1,
70
+ day: value.getDate(),
71
+ }).toString();
72
+ }
73
+
74
+ const parsed = parseString(String(value));
75
+ return parsed?.toString() ?? '';
76
+ } catch {
77
+ // TODO: should we throw when codec cannot interpret the value?
78
+ return '';
79
+ }
80
+ };
81
+
82
+ export class DateValueCodec extends ValueCodec<'date', DatetimeRuntimeValue, DatetimeInputValue> {
83
+ constructor() {
84
+ const encodeValue: CodecEncoder<DatetimeInputValue> = (value) => {
85
+ return toDateString(value);
86
+ };
87
+
88
+ const decodeValue: CodecDecoder<DatetimeRuntimeValue> = (value: string) => {
89
+ return parseString(value);
90
+ };
91
+
92
+ super('date', encodeValue, decodeValue);
93
+ }
94
+ }
@@ -1,4 +1,6 @@
1
1
  import type { ValueType } from '../../client/ValueType.ts';
2
+ import type { DatetimeInputValue, DatetimeRuntimeValue } from './DateValueCodec.ts';
3
+ import { DateValueCodec } from './DateValueCodec.ts';
2
4
  import {
3
5
  DecimalValueCodec,
4
6
  type DecimalInputValue,
@@ -16,7 +18,7 @@ interface RuntimeValuesByType {
16
18
  readonly int: IntRuntimeValue;
17
19
  readonly decimal: DecimalRuntimeValue;
18
20
  readonly boolean: string;
19
- readonly date: string;
21
+ readonly date: DatetimeRuntimeValue;
20
22
  readonly time: string;
21
23
  readonly dateTime: string;
22
24
  readonly geopoint: GeopointRuntimeValue;
@@ -34,7 +36,7 @@ interface RuntimeInputValuesByType {
34
36
  readonly int: IntInputValue;
35
37
  readonly decimal: DecimalInputValue;
36
38
  readonly boolean: string;
37
- readonly date: string;
39
+ readonly date: DatetimeInputValue;
38
40
  readonly time: string;
39
41
  readonly dateTime: string;
40
42
  readonly geopoint: GeopointInputValue;
@@ -63,7 +65,7 @@ export const sharedValueCodecs: SharedValueCodecs = {
63
65
  int: new IntValueCodec(),
64
66
  decimal: new DecimalValueCodec(),
65
67
  boolean: new ValueTypePlaceholderCodec('boolean'),
66
- date: new ValueTypePlaceholderCodec('date'),
68
+ date: new DateValueCodec(),
67
69
  time: new ValueTypePlaceholderCodec('time'),
68
70
  dateTime: new ValueTypePlaceholderCodec('dateTime'),
69
71
  geopoint: new GeopointValueCodec(),
@@ -1,67 +1,79 @@
1
+ import { JRResourceURL } from '@getodk/common/jr-resources/JRResourceURL.ts';
1
2
  import type { Accessor } from 'solid-js';
2
3
  import { createMemo } from 'solid-js';
3
- import type { TextChunkSource, TextRole } from '../../../client/TextRange.ts';
4
+ import type { TextRole } from '../../../client/TextRange.ts';
4
5
  import type { EvaluationContext } from '../../../instance/internal-api/EvaluationContext.ts';
5
6
  import { TextChunk } from '../../../instance/text/TextChunk.ts';
6
- import { TextRange } from '../../../instance/text/TextRange.ts';
7
- import type { AnyTextChunkExpression } from '../../../parse/expression/abstract/TextChunkExpression.ts';
7
+ import { TextRange, type MediaSources } from '../../../instance/text/TextRange.ts';
8
+ import { isEngineXPathElement } from '../../../integration/xpath/adapter/kind.ts';
9
+ import { StaticElement } from '../../../integration/xpath/static-dom/StaticElement.ts';
10
+ import { type TextChunkExpression } from '../../../parse/expression/TextChunkExpression.ts';
8
11
  import type { TextRangeDefinition } from '../../../parse/text/abstract/TextRangeDefinition.ts';
9
12
  import { createComputedExpression } from '../createComputedExpression.ts';
10
13
 
11
- interface TextChunkComputation {
12
- readonly source: TextChunkSource;
13
- readonly getText: Accessor<string>;
14
+ interface ChunksAndMedia {
15
+ chunks: readonly TextChunk[];
16
+ mediaSources: MediaSources;
14
17
  }
15
18
 
16
- const createComputedTextChunk = (
19
+ /**
20
+ * Creates a reactive accessor for text chunks and an optional image from text source expressions.
21
+ * - Combines chunks from literal and computed sources into a single array.
22
+ * - Captures the first image found with a 'from="image"' attribute.
23
+ *
24
+ * @param context - The evaluation context for reactive XPath computations.
25
+ * @param chunkExpressions - Array of text source expressions to process.
26
+ * @returns An accessor for an object with all chunks and the first image (if any).
27
+ */
28
+ const createTextChunks = (
17
29
  context: EvaluationContext,
18
- textSource: AnyTextChunkExpression
19
- ): TextChunkComputation => {
20
- const { source } = textSource;
30
+ chunkExpressions: ReadonlyArray<TextChunkExpression<'nodes' | 'string'>>
31
+ ): Accessor<ChunksAndMedia> => {
32
+ return createMemo(() => {
33
+ const chunks: TextChunk[] = [];
34
+ const mediaSources: MediaSources = {};
21
35
 
22
- if (source === 'literal') {
23
- const { stringValue } = textSource;
36
+ chunkExpressions.forEach((chunkExpression) => {
37
+ if (chunkExpression.source === 'literal') {
38
+ chunks.push(new TextChunk(context, chunkExpression.source, chunkExpression.stringValue));
39
+ return;
40
+ }
24
41
 
25
- return {
26
- source,
27
- getText: () => stringValue,
28
- };
29
- }
42
+ const computed = createComputedExpression(context, chunkExpression)();
30
43
 
31
- return context.scope.runTask(() => {
32
- const getText = createComputedExpression(context, textSource, {
33
- defaultValue: '',
34
- });
44
+ if (typeof computed === 'string') {
45
+ // not a translation expression
46
+ chunks.push(new TextChunk(context, chunkExpression.source, computed));
47
+ return;
48
+ } else {
49
+ // translation expression evaluates to an entire itext block, process forms separately
50
+ computed.forEach((itextForm) => {
51
+ if (isEngineXPathElement(itextForm) && itextForm instanceof StaticElement) {
52
+ const formAttribute = itextForm.getAttributeValue('form');
35
53
 
36
- return {
37
- source,
38
- getText,
39
- };
40
- });
41
- };
54
+ if (!formAttribute) {
55
+ const defaultFormValue = itextForm.getXPathValue();
56
+ chunks.push(new TextChunk(context, chunkExpression.source, defaultFormValue));
57
+ } else if (['image', 'video', 'audio'].includes(formAttribute)) {
58
+ const formValue = itextForm.getXPathValue();
42
59
 
43
- const createTextChunks = (
44
- context: EvaluationContext,
45
- textSources: readonly AnyTextChunkExpression[]
46
- ): Accessor<readonly TextChunk[]> => {
47
- return context.scope.runTask(() => {
48
- const chunkComputations = textSources.map((textSource) => {
49
- return createComputedTextChunk(context, textSource);
60
+ if (JRResourceURL.isJRResourceReference(formValue)) {
61
+ mediaSources[formAttribute as keyof MediaSources] = JRResourceURL.from(formValue);
62
+ }
63
+ }
64
+ }
65
+ });
66
+ }
50
67
  });
51
68
 
52
- return createMemo(() => {
53
- return chunkComputations.map(({ source, getText }) => {
54
- return new TextChunk(context, source, getText());
55
- });
56
- });
69
+ return { chunks, mediaSources };
57
70
  });
58
71
  };
59
72
 
60
73
  type ComputedFormTextRange<Role extends TextRole> = Accessor<TextRange<Role, 'form'>>;
61
74
 
62
75
  /**
63
- * Creates a text range (e.g. label or hint) from the provided definition,
64
- * reactive to:
76
+ * Creates a text range (e.g. label or hint) from the provided definition, reactive to:
65
77
  *
66
78
  * - The form's current language (e.g. `<label ref="jr:itext('text-id')" />`)
67
79
  * - Direct `<output>` references within the label's children
@@ -74,10 +86,11 @@ export const createTextRange = <Role extends TextRole>(
74
86
  definition: TextRangeDefinition<Role>
75
87
  ): ComputedFormTextRange<Role> => {
76
88
  return context.scope.runTask(() => {
77
- const getTextChunks = createTextChunks(context, definition.chunks);
89
+ const textChunks = createTextChunks(context, definition.chunks);
78
90
 
79
91
  return createMemo(() => {
80
- return new TextRange('form', role, getTextChunks());
92
+ const chunks = textChunks();
93
+ return new TextRange('form', role, chunks.chunks, chunks.mediaSources);
81
94
  });
82
95
  });
83
96
  };
@@ -34,9 +34,7 @@ const engineViolationMessage = <Role extends ValidationTextRole>(
34
34
  ): Accessor<EngineViolationMessage<Role>> => {
35
35
  const messageText = VALIDATION_TEXT[role];
36
36
  const chunk = new TextChunk(context, 'literal', messageText);
37
- const message = new TextRange('engine', role, [chunk]);
38
-
39
- return () => message;
37
+ return () => new TextRange('engine', role, [chunk]);
40
38
  };
41
39
 
42
40
  const createViolationMessage = <Role extends ValidationTextRole>(
@@ -0,0 +1,78 @@
1
+ import type { KnownAttributeLocalNamedElement } from '@getodk/common/types/dom.ts';
2
+ import type { TextChunkSource } from '../../client/TextRange.ts';
3
+ import type { AnyTextRangeDefinition } from '../text/abstract/TextRangeDefinition.ts';
4
+ import { isTranslationExpression } from '../xpath/semantic-analysis.ts';
5
+ import { DependentExpression } from './abstract/DependentExpression.ts';
6
+
7
+ interface TextChunkExpressionOptions {
8
+ readonly isTranslated?: true;
9
+ }
10
+
11
+ interface OutputElement extends KnownAttributeLocalNamedElement<'output', 'value'> {}
12
+
13
+ const isOutputElement = (element: Element): element is OutputElement => {
14
+ return element.localName === 'output' && element.hasAttribute('value');
15
+ };
16
+
17
+ export class TextChunkExpression<T extends 'nodes' | 'string'> extends DependentExpression<T> {
18
+ readonly source: TextChunkSource;
19
+ // Set for the literal source, blank otherwise
20
+ readonly stringValue: string;
21
+
22
+ constructor(
23
+ context: AnyTextRangeDefinition,
24
+ resultType: T,
25
+ expression: string,
26
+ source: TextChunkSource,
27
+ options: TextChunkExpressionOptions = {},
28
+ literalValue = ''
29
+ ) {
30
+ super(context, resultType, expression, {
31
+ semanticDependencies: {
32
+ translations: options.isTranslated,
33
+ },
34
+ ignoreContextReference: true,
35
+ });
36
+
37
+ this.source = source;
38
+ this.stringValue = literalValue;
39
+ }
40
+
41
+ static fromLiteral(
42
+ context: AnyTextRangeDefinition,
43
+ stringValue: string
44
+ ): TextChunkExpression<'string'> {
45
+ return new TextChunkExpression(context, 'string', 'null', 'literal', {}, stringValue);
46
+ }
47
+
48
+ static fromReference(
49
+ context: AnyTextRangeDefinition,
50
+ ref: string
51
+ ): TextChunkExpression<'string'> {
52
+ return new TextChunkExpression(context, 'string', ref, 'reference');
53
+ }
54
+
55
+ static fromOutput(
56
+ context: AnyTextRangeDefinition,
57
+ element: Element
58
+ ): TextChunkExpression<'string'> | null {
59
+ if (!isOutputElement(element)) {
60
+ return null;
61
+ }
62
+
63
+ return new TextChunkExpression(context, 'string', element.getAttribute('value'), 'output');
64
+ }
65
+
66
+ static fromTranslation(
67
+ context: AnyTextRangeDefinition,
68
+ maybeExpression: string
69
+ ): TextChunkExpression<'nodes'> | null {
70
+ if (isTranslationExpression(maybeExpression)) {
71
+ return new TextChunkExpression(context, 'nodes', maybeExpression, 'translation', {
72
+ isTranslated: true,
73
+ });
74
+ }
75
+
76
+ return null;
77
+ }
78
+ }
@@ -1,15 +1,10 @@
1
1
  import type { LocalNamedElement } from '@getodk/common/types/dom.ts';
2
2
  import { getLabelElement } from '../../lib/dom/query.ts';
3
3
  import type { XFormDefinition } from '../../parse/XFormDefinition.ts';
4
- import type { ItemDefinition } from '../body/control/ItemDefinition.ts';
5
4
  import type { ItemsetDefinition } from '../body/control/ItemsetDefinition.ts';
6
- import { TextReferenceExpression } from '../expression/TextReferenceExpression.ts';
7
- import { TextTranslationExpression } from '../expression/TextTranslationExpression.ts';
8
- import type { RefAttributeChunk } from './abstract/TextElementDefinition.ts';
5
+ import { TextChunkExpression } from '../expression/TextChunkExpression.ts';
9
6
  import { TextRangeDefinition } from './abstract/TextRangeDefinition.ts';
10
7
 
11
- export type ItemLabelOwner = ItemDefinition | ItemsetDefinition;
12
-
13
8
  interface LabelElement extends LocalNamedElement<'label'> {}
14
9
 
15
10
  export class ItemsetLabelDefinition extends TextRangeDefinition<'item-label'> {
@@ -24,7 +19,7 @@ export class ItemsetLabelDefinition extends TextRangeDefinition<'item-label'> {
24
19
  }
25
20
 
26
21
  readonly role = 'item-label';
27
- readonly chunks: readonly [RefAttributeChunk];
22
+ readonly chunks: ReadonlyArray<TextChunkExpression<'nodes' | 'string'>>;
28
23
 
29
24
  private constructor(form: XFormDefinition, owner: ItemsetDefinition, element: LabelElement) {
30
25
  super(form, owner, element);
@@ -35,10 +30,11 @@ export class ItemsetLabelDefinition extends TextRangeDefinition<'item-label'> {
35
30
  throw new Error('<itemset><label> missing ref attribute');
36
31
  }
37
32
 
38
- const refChunk =
39
- TextTranslationExpression.from(this, refExpression) ??
40
- TextReferenceExpression.from(this, refExpression);
41
-
42
- this.chunks = [refChunk];
33
+ const expression = TextChunkExpression.fromTranslation(this, refExpression);
34
+ if (expression != null) {
35
+ this.chunks = [expression];
36
+ } else {
37
+ this.chunks = [TextChunkExpression.fromReference(this, refExpression)];
38
+ }
43
39
  }
44
40
  }
@@ -1,17 +1,9 @@
1
1
  import { JAVAROSA_NAMESPACE_URI } from '@getodk/common/constants/xmlns.ts';
2
- import { TextLiteralExpression } from '../expression/TextLiteralExpression.ts';
3
- import { TextTranslationExpression } from '../expression/TextTranslationExpression.ts';
2
+ import { TextChunkExpression } from '../expression/TextChunkExpression.ts';
4
3
  import type { BindDefinition } from '../model/BindDefinition.ts';
5
- import type { TextBindAttributeLocalName, TextSourceNode } from './abstract/TextRangeDefinition.ts';
4
+ import type { TextBindAttributeLocalName } from './abstract/TextRangeDefinition.ts';
6
5
  import { TextRangeDefinition } from './abstract/TextRangeDefinition.ts';
7
6
 
8
- export type MessageSourceNode = TextSourceNode<TextBindAttributeLocalName>;
9
-
10
- // prettier-ignore
11
- type MessageChunk =
12
- | TextLiteralExpression
13
- | TextTranslationExpression;
14
-
15
7
  export class MessageDefinition<
16
8
  Type extends TextBindAttributeLocalName,
17
9
  > extends TextRangeDefinition<Type> {
@@ -28,7 +20,7 @@ export class MessageDefinition<
28
20
  return new this(bind, type, message);
29
21
  }
30
22
 
31
- readonly chunks: readonly [MessageChunk];
23
+ readonly chunks: ReadonlyArray<TextChunkExpression<'nodes' | 'string'>>;
32
24
 
33
25
  private constructor(
34
26
  bind: BindDefinition,
@@ -37,11 +29,12 @@ export class MessageDefinition<
37
29
  ) {
38
30
  super(bind.form, bind, null);
39
31
 
40
- const chunk: MessageChunk =
41
- TextTranslationExpression.fromMessage(this, message) ??
42
- TextLiteralExpression.from(this, message);
43
-
44
- this.chunks = [chunk];
32
+ const expression = TextChunkExpression.fromTranslation(this, message);
33
+ if (expression != null) {
34
+ this.chunks = [expression];
35
+ } else {
36
+ this.chunks = [TextChunkExpression.fromLiteral(this, message)];
37
+ }
45
38
  }
46
39
  }
47
40