@getodk/xforms-engine 0.9.0 → 0.11.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 (40) hide show
  1. package/dist/client/SelectNode.d.ts +1 -0
  2. package/dist/client/TextRange.d.ts +4 -0
  3. package/dist/index.js +133 -160
  4. package/dist/index.js.map +1 -1
  5. package/dist/instance/SelectControl.d.ts +1 -0
  6. package/dist/instance/text/TextRange.d.ts +11 -1
  7. package/dist/lib/reactivity/text/createTextRange.d.ts +1 -2
  8. package/dist/parse/expression/TextChunkExpression.d.ts +16 -0
  9. package/dist/parse/shared/parseInstanceXML.d.ts +1 -1
  10. package/dist/parse/text/ItemsetLabelDefinition.d.ts +2 -4
  11. package/dist/parse/text/MessageDefinition.d.ts +3 -7
  12. package/dist/parse/text/abstract/TextElementDefinition.d.ts +2 -8
  13. package/dist/parse/text/abstract/TextRangeDefinition.d.ts +2 -2
  14. package/dist/solid.js +133 -160
  15. package/dist/solid.js.map +1 -1
  16. package/package.json +2 -2
  17. package/src/client/SelectNode.ts +2 -0
  18. package/src/client/TextRange.ts +5 -1
  19. package/src/error/LoadFormFailureError.ts +9 -35
  20. package/src/instance/SelectControl.ts +6 -0
  21. package/src/instance/text/TextRange.ts +21 -1
  22. package/src/lib/reactivity/text/createTextRange.ts +56 -43
  23. package/src/lib/reactivity/validation/createValidation.ts +1 -3
  24. package/src/parse/expression/TextChunkExpression.ts +78 -0
  25. package/src/parse/shared/parseInstanceXML.ts +4 -2
  26. package/src/parse/text/ItemsetLabelDefinition.ts +8 -12
  27. package/src/parse/text/MessageDefinition.ts +9 -16
  28. package/src/parse/text/abstract/TextElementDefinition.ts +10 -26
  29. package/src/parse/text/abstract/TextRangeDefinition.ts +2 -2
  30. package/src/parse/xpath/semantic-analysis.ts +7 -2
  31. package/dist/parse/expression/TextLiteralExpression.d.ts +0 -9
  32. package/dist/parse/expression/TextOutputExpression.d.ts +0 -7
  33. package/dist/parse/expression/TextReferenceExpression.d.ts +0 -7
  34. package/dist/parse/expression/TextTranslationExpression.d.ts +0 -8
  35. package/dist/parse/expression/abstract/TextChunkExpression.d.ts +0 -17
  36. package/src/parse/expression/TextLiteralExpression.ts +0 -19
  37. package/src/parse/expression/TextOutputExpression.ts +0 -25
  38. package/src/parse/expression/TextReferenceExpression.ts +0 -14
  39. package/src/parse/expression/TextTranslationExpression.ts +0 -38
  40. 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.9.0",
3
+ "version": "0.11.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "XForms engine for ODK Web Forms",
6
6
  "type": "module",
@@ -62,7 +62,7 @@
62
62
  "devDependencies": {
63
63
  "@babel/core": "^7.26.10",
64
64
  "@getodk/tree-sitter-xpath": "0.1.3",
65
- "@getodk/xpath": "0.5.0",
65
+ "@getodk/xpath": "0.6.0",
66
66
  "@playwright/test": "^1.49.1",
67
67
  "@types/papaparse": "^5.3.15",
68
68
  "@vitest/browser": "^3.0.9",
@@ -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
  }
@@ -49,7 +49,7 @@ interface FormResourceMetadata {
49
49
  readonly rawData: string | null;
50
50
  }
51
51
 
52
- const formResourceMetadata = (resource: FormResource): FormResourceMetadata => {
52
+ const formResourceMetadata = (resource: FormResource): FormResourceMetadata | undefined => {
53
53
  if (resource instanceof Blob) {
54
54
  return {
55
55
  description: blobDescription(resource),
@@ -64,10 +64,7 @@ const formResourceMetadata = (resource: FormResource): FormResourceMetadata => {
64
64
  };
65
65
  }
66
66
 
67
- return {
68
- description: 'Raw string data',
69
- rawData: resource,
70
- };
67
+ return;
71
68
  };
72
69
 
73
70
  /**
@@ -77,38 +74,15 @@ const formResourceMetadata = (resource: FormResource): FormResourceMetadata => {
77
74
  */
78
75
  export class LoadFormFailureError extends AggregateError {
79
76
  constructor(resource: FormResource, errors: readonly Error[]) {
80
- const { description, rawData } = formResourceMetadata(resource);
81
- const messageLines: string[] = [
82
- `Failed to load form resource: ${description}`,
83
- '\n',
84
-
85
- ...errors.map((error) => {
86
- const aggregatedMessage = error.message;
87
-
88
- if (aggregatedMessage == null) {
89
- return '- Unknown error';
90
- }
91
-
92
- return `- ${aggregatedMessage}`;
93
- }),
94
- ];
95
-
96
- if (rawData != null) {
97
- messageLines.push('\n- - -\n', 'Raw resource data:', rawData);
98
- }
99
-
100
- const message = messageLines.join('\n');
101
-
77
+ const metadata = formResourceMetadata(resource);
78
+ const errorMessages = errors.map((error) => error.message || 'Unknown error').join('\n');
79
+ const message = metadata?.description
80
+ ? `Form source: ${metadata.description}\n${errorMessages}`
81
+ : errorMessages;
102
82
  super(errors, message);
103
83
 
104
84
  const [head, ...tail] = errors;
105
-
106
- if (head != null && tail.length === 0) {
107
- const { stack } = head;
108
-
109
- if (typeof stack === 'string') {
110
- this.stack = stack;
111
- }
112
- }
85
+ this.stack =
86
+ typeof head?.stack === 'string' && !tail.length ? head.stack : 'No error trace available.';
113
87
  }
114
88
  }
@@ -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
  }
@@ -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
+ }
@@ -68,11 +68,13 @@ const getWrappedInstanceRootElement = (xml: WrappedInstanceXML): Element => {
68
68
  * @todo Aside from this being a hack, it's not very robust because it makes
69
69
  * assumptions which are _likely but definitely not guaranteed_!
70
70
  *
71
- * - Instance XML (probably) doeesn't declare a default namespace
71
+ * - Instance XML (probably) doesn't declare a default namespace
72
72
  * - Instance XML **definitely** declares non-default namespaces
73
73
  */
74
74
  export const parseInstanceXML = (model: ModelDefinition, instanceXML: string): StaticDocument => {
75
- const wrappedXML = wrapInstanceXML(model, instanceXML);
75
+ // Remove XML declaration if present: xforms-engine defaults to UTF-8
76
+ const cleanedXML = instanceXML.replace(/<\?xml\s+[^?]*\?>\s*/, '');
77
+ const wrappedXML = wrapInstanceXML(model, cleanedXML);
76
78
  const root = getWrappedInstanceRootElement(wrappedXML);
77
79
 
78
80
  return parseStaticDocumentFromDOMSubtree(root);
@@ -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
 
@@ -2,10 +2,7 @@ import { isElementNode, isTextNode } from '@getodk/common/lib/dom/predicates.ts'
2
2
  import type { ElementTextRole } from '../../../client/TextRange.ts';
3
3
  import type { XFormDefinition } from '../../../parse/XFormDefinition.ts';
4
4
  import type { ItemDefinition } from '../../body/control/ItemDefinition.ts';
5
- import { TextLiteralExpression } from '../../expression/TextLiteralExpression.ts';
6
- import { TextOutputExpression } from '../../expression/TextOutputExpression.ts';
7
- import { TextReferenceExpression } from '../../expression/TextReferenceExpression.ts';
8
- import { TextTranslationExpression } from '../../expression/TextTranslationExpression.ts';
5
+ import { TextChunkExpression } from '../../expression/TextChunkExpression.ts';
9
6
  import { parseNodesetReference } from '../../xpath/reference-parsing.ts';
10
7
  import type { HintDefinition } from '../HintDefinition.ts';
11
8
  import type { ItemLabelDefinition } from '../ItemLabelDefinition.ts';
@@ -14,27 +11,12 @@ import type { LabelDefinition, LabelOwner } from '../LabelDefinition.ts';
14
11
  import type { TextSourceNode } from './TextRangeDefinition.ts';
15
12
  import { TextRangeDefinition } from './TextRangeDefinition.ts';
16
13
 
17
- // prettier-ignore
18
- export type RefAttributeChunk =
19
- | TextReferenceExpression
20
- | TextTranslationExpression;
21
-
22
- // prettier-ignore
23
- type TextElementChildChunk =
24
- | TextLiteralExpression
25
- | TextOutputExpression;
26
-
27
- // prettier-ignore
28
- type TextElementChunks =
29
- | readonly [RefAttributeChunk]
30
- | readonly TextElementChildChunk[];
31
-
32
14
  type TextElementOwner = ItemDefinition | LabelOwner;
33
15
 
34
16
  export abstract class TextElementDefinition<
35
17
  Role extends ElementTextRole,
36
18
  > extends TextRangeDefinition<Role> {
37
- readonly chunks: TextElementChunks;
19
+ readonly chunks: ReadonlyArray<TextChunkExpression<'nodes' | 'string'>>;
38
20
 
39
21
  constructor(form: XFormDefinition, owner: TextElementOwner, sourceNode: TextSourceNode<Role>) {
40
22
  super(form, owner, sourceNode);
@@ -45,20 +27,22 @@ export abstract class TextElementDefinition<
45
27
  if (refExpression == null) {
46
28
  this.chunks = Array.from(sourceNode.childNodes).flatMap((childNode) => {
47
29
  if (isElementNode(childNode)) {
48
- return TextOutputExpression.from(context, childNode) ?? [];
30
+ return TextChunkExpression.fromOutput(context, childNode) ?? [];
49
31
  }
50
32
 
51
33
  if (isTextNode(childNode)) {
52
- return TextLiteralExpression.from(context, childNode.data);
34
+ return TextChunkExpression.fromLiteral(context, childNode.data);
53
35
  }
54
36
 
55
37
  return [];
56
38
  });
57
39
  } else {
58
- const refChunk =
59
- TextTranslationExpression.from(context, refExpression) ??
60
- TextReferenceExpression.from(context, refExpression);
61
- this.chunks = [refChunk];
40
+ const expression = TextChunkExpression.fromTranslation(context, refExpression);
41
+ if (expression != null) {
42
+ this.chunks = [expression];
43
+ } else {
44
+ this.chunks = [TextChunkExpression.fromReference(context, refExpression)];
45
+ }
62
46
  }
63
47
  }
64
48
  }
@@ -2,7 +2,7 @@ import type { LocalNamedElement } from '@getodk/common/types/dom.ts';
2
2
  import type { TextRole } from '../../../client/TextRange.ts';
3
3
  import type { XFormDefinition } from '../../../parse/XFormDefinition.ts';
4
4
  import { DependencyContext } from '../../expression/abstract/DependencyContext.ts';
5
- import type { AnyTextChunkExpression } from '../../expression/abstract/TextChunkExpression.ts';
5
+ import type { TextChunkExpression } from '../../expression/TextChunkExpression.ts';
6
6
  import type { AnyMessageDefinition } from '../MessageDefinition.ts';
7
7
  import type { AnyTextElementDefinition } from './TextElementDefinition.ts';
8
8
 
@@ -24,7 +24,7 @@ export abstract class TextRangeDefinition<Role extends TextRole> extends Depende
24
24
  readonly parentReference: string | null;
25
25
  readonly reference: string | null;
26
26
 
27
- abstract readonly chunks: readonly AnyTextChunkExpression[];
27
+ abstract readonly chunks: ReadonlyArray<TextChunkExpression<'nodes' | 'string'>>;
28
28
 
29
29
  override get isTranslated(): boolean {
30
30
  return (
@@ -117,9 +117,14 @@ export type TranslationExpression = LocalNamedFunctionCallLiteral<'itext'>;
117
117
  export const isTranslationExpression = (
118
118
  expression: string
119
119
  ): expression is TranslationExpression => {
120
- const { rootNode } = expressionParser.parse(expression);
121
- const functionCallNode = findTypedPrincipalExpressionNode(['function_call'], rootNode);
120
+ let result;
121
+ try {
122
+ result = expressionParser.parse(expression);
123
+ } catch {
124
+ return false;
125
+ }
122
126
 
127
+ const functionCallNode = findTypedPrincipalExpressionNode(['function_call'], result.rootNode);
123
128
  if (functionCallNode == null) {
124
129
  return false;
125
130
  }
@@ -1,9 +0,0 @@
1
- import { AnyTextRangeDefinition } from '../text/abstract/TextRangeDefinition.ts';
2
- import { TextChunkExpression } from './abstract/TextChunkExpression.ts';
3
- export type TextLiteralSourceNode = Attr | Text;
4
- export declare class TextLiteralExpression extends TextChunkExpression<'literal'> {
5
- readonly stringValue: string;
6
- static from(context: AnyTextRangeDefinition, stringValue: string): TextLiteralExpression;
7
- readonly source = "literal";
8
- private constructor();
9
- }
@@ -1,7 +0,0 @@
1
- import { AnyTextRangeDefinition } from '../text/abstract/TextRangeDefinition.ts';
2
- import { TextChunkExpression } from './abstract/TextChunkExpression.ts';
3
- export declare class TextOutputExpression extends TextChunkExpression<'output'> {
4
- static from(context: AnyTextRangeDefinition, element: Element): TextOutputExpression | null;
5
- readonly source = "output";
6
- private constructor();
7
- }
@@ -1,7 +0,0 @@
1
- import { AnyTextRangeDefinition } from '../text/abstract/TextRangeDefinition.ts';
2
- import { TextChunkExpression } from './abstract/TextChunkExpression.ts';
3
- export declare class TextReferenceExpression extends TextChunkExpression<'reference'> {
4
- static from(context: AnyTextRangeDefinition, refExpression: string): TextReferenceExpression;
5
- readonly source = "reference";
6
- private constructor();
7
- }
@@ -1,8 +0,0 @@
1
- import { AnyTextRangeDefinition } from '../text/abstract/TextRangeDefinition.ts';
2
- import { TextChunkExpression } from './abstract/TextChunkExpression.ts';
3
- export declare class TextTranslationExpression extends TextChunkExpression<'translation'> {
4
- static fromMessage(context: AnyTextRangeDefinition, maybeExpression: string): TextTranslationExpression | null;
5
- static from(context: AnyTextRangeDefinition, maybeExpression: string): TextTranslationExpression | null;
6
- readonly source = "translation";
7
- private constructor();
8
- }