@getodk/xforms-engine 0.9.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 (37) 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 +125 -133
  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/text/ItemsetLabelDefinition.d.ts +2 -4
  10. package/dist/parse/text/MessageDefinition.d.ts +3 -7
  11. package/dist/parse/text/abstract/TextElementDefinition.d.ts +2 -8
  12. package/dist/parse/text/abstract/TextRangeDefinition.d.ts +2 -2
  13. package/dist/solid.js +125 -133
  14. package/dist/solid.js.map +1 -1
  15. package/package.json +2 -2
  16. package/src/client/SelectNode.ts +2 -0
  17. package/src/client/TextRange.ts +5 -1
  18. package/src/instance/SelectControl.ts +6 -0
  19. package/src/instance/text/TextRange.ts +21 -1
  20. package/src/lib/reactivity/text/createTextRange.ts +56 -43
  21. package/src/lib/reactivity/validation/createValidation.ts +1 -3
  22. package/src/parse/expression/TextChunkExpression.ts +78 -0
  23. package/src/parse/text/ItemsetLabelDefinition.ts +8 -12
  24. package/src/parse/text/MessageDefinition.ts +9 -16
  25. package/src/parse/text/abstract/TextElementDefinition.ts +10 -26
  26. package/src/parse/text/abstract/TextRangeDefinition.ts +2 -2
  27. package/src/parse/xpath/semantic-analysis.ts +7 -2
  28. package/dist/parse/expression/TextLiteralExpression.d.ts +0 -9
  29. package/dist/parse/expression/TextOutputExpression.d.ts +0 -7
  30. package/dist/parse/expression/TextReferenceExpression.d.ts +0 -7
  31. package/dist/parse/expression/TextTranslationExpression.d.ts +0 -8
  32. package/dist/parse/expression/abstract/TextChunkExpression.d.ts +0 -17
  33. package/src/parse/expression/TextLiteralExpression.ts +0 -19
  34. package/src/parse/expression/TextOutputExpression.ts +0 -25
  35. package/src/parse/expression/TextReferenceExpression.ts +0 -14
  36. package/src/parse/expression/TextTranslationExpression.ts +0 -38
  37. 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.10.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
  }
@@ -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
+ }
@@ -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
- }
@@ -1,17 +0,0 @@
1
- import { TextChunkSource } from '../../../client/TextRange.ts';
2
- import { AnyTextRangeDefinition } from '../../text/abstract/TextRangeDefinition.ts';
3
- import { TextLiteralExpression } from '../TextLiteralExpression.ts';
4
- import { TextOutputExpression } from '../TextOutputExpression.ts';
5
- import { TextReferenceExpression } from '../TextReferenceExpression.ts';
6
- import { TextTranslationExpression } from '../TextTranslationExpression.ts';
7
- import { DependentExpression } from './DependentExpression.ts';
8
- interface TextChunkExpressionOptions {
9
- readonly isTranslated?: true;
10
- }
11
- export declare abstract class TextChunkExpression<Source extends TextChunkSource> extends DependentExpression<'string'> {
12
- abstract readonly source: Source;
13
- readonly stringValue?: string;
14
- constructor(context: AnyTextRangeDefinition, expression: string, options?: TextChunkExpressionOptions);
15
- }
16
- export type AnyTextChunkExpression = TextLiteralExpression | TextOutputExpression | TextReferenceExpression | TextTranslationExpression;
17
- export {};
@@ -1,19 +0,0 @@
1
- import type { AnyTextRangeDefinition } from '../text/abstract/TextRangeDefinition.ts';
2
- import { TextChunkExpression } from './abstract/TextChunkExpression.ts';
3
-
4
- export type TextLiteralSourceNode = Attr | Text;
5
-
6
- export class TextLiteralExpression extends TextChunkExpression<'literal'> {
7
- static from(context: AnyTextRangeDefinition, stringValue: string): TextLiteralExpression {
8
- return new this(context, stringValue);
9
- }
10
-
11
- readonly source = 'literal';
12
-
13
- private constructor(
14
- context: AnyTextRangeDefinition,
15
- override readonly stringValue: string
16
- ) {
17
- super(context, 'null');
18
- }
19
- }
@@ -1,25 +0,0 @@
1
- import type { KnownAttributeLocalNamedElement } from '@getodk/common/types/dom.ts';
2
- import type { AnyTextRangeDefinition } from '../text/abstract/TextRangeDefinition.ts';
3
- import { TextChunkExpression } from './abstract/TextChunkExpression.ts';
4
-
5
- interface OutputElement extends KnownAttributeLocalNamedElement<'output', 'value'> {}
6
-
7
- const isOutputElement = (element: Element): element is OutputElement => {
8
- return element.localName === 'output' && element.hasAttribute('value');
9
- };
10
-
11
- export class TextOutputExpression extends TextChunkExpression<'output'> {
12
- static from(context: AnyTextRangeDefinition, element: Element): TextOutputExpression | null {
13
- if (isOutputElement(element)) {
14
- return new this(context, element);
15
- }
16
-
17
- return null;
18
- }
19
-
20
- readonly source = 'output';
21
-
22
- private constructor(context: AnyTextRangeDefinition, element: OutputElement) {
23
- super(context, element.getAttribute('value'));
24
- }
25
- }
@@ -1,14 +0,0 @@
1
- import type { AnyTextRangeDefinition } from '../text/abstract/TextRangeDefinition.ts';
2
- import { TextChunkExpression } from './abstract/TextChunkExpression.ts';
3
-
4
- export class TextReferenceExpression extends TextChunkExpression<'reference'> {
5
- static from(context: AnyTextRangeDefinition, refExpression: string): TextReferenceExpression {
6
- return new this(context, refExpression);
7
- }
8
-
9
- readonly source = 'reference';
10
-
11
- private constructor(context: AnyTextRangeDefinition, refExpression: string) {
12
- super(context, refExpression);
13
- }
14
- }