Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/proud-years-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@getodk/xforms-engine': patch
'@getodk/common': patch
---

Fixed handling of ranges with negative step values
16 changes: 16 additions & 0 deletions packages/common/test-utils/xform-dsl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ export const select1 = (ref: string, ...children: XFormsElement[]): XFormsElemen
return t(`select1 ref="${ref}"`, ...children);
};

interface RangeAttributes {
start: number;
end: number;
step: number;
}
export const range = (
ref: string,
attributes: RangeAttributes,
...children: XFormsElement[]
): XFormsElement => {
return t(
`range ref="${ref}" start="${attributes.start}" end="${attributes.end}" step="${attributes.step}"`,
...children
);
};

type Select1DynamicParameters =
// eslint-disable-next-line @typescript-eslint/sort-type-constituents
| readonly [ref: string, nodesetRef: string]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ const parseNumericStringAttribute = (element: Element, localName: string): Numer
return value;
};

const abs = (value: string | null) => (value?.startsWith('-') ? value.substring(1) : value);

const parseNumericStringAttributeAbs = (element: Element, localName: string): NumericString => {
const value = abs(element.getAttribute(localName));

assertNumericStringAttribute(localName, value);

return value;
};

/**
* Per
* {@link https://getodk.github.io/xforms-spec/#body-elements | ODK XForms spec},
Expand All @@ -69,15 +79,13 @@ const parseNumericStringAttribute = (element: Element, localName: string): Numer
* checking only that they appear to be numeric values. We also preserve the
* attributes' names here, for consistency with the spec.
*
* Downstream, we parse these to their appropriate numeric runtime types, and
* alias them to their more conventional names (i.e. "start" -> "min", "end" ->
* "max").
* Downstream, we parse these to their appropriate numeric runtime types.
*/
export class RangeControlBoundsDefinition {
static from(element: Element) {
const start = parseNumericStringAttribute(element, 'start');
const end = parseNumericStringAttribute(element, 'end');
const step = parseNumericStringAttribute(element, 'step');
const step = parseNumericStringAttributeAbs(element, 'step');

return new this(start, end, step);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
bind,
body,
head,
html,
mainInstance,
model,
range,
t,
title,
} from '@getodk/common/test-utils/xform-dsl/index.ts';
import { describe, expect, it } from 'vitest';
import { RangeControlDefinition } from '../../../../src/parse/body/control/RangeControlDefinition.ts';
import { XFormDefinition } from '../../../../src/parse/XFormDefinition.ts';
import { XFormDOM } from '../../../../src/parse/XFormDOM.ts';

describe('RangeControlDefinition', () => {
const create = (type: string, start: number, end: number, step: number) => {
const xform = html(
head(
title('Range definition'),
model(
mainInstance(t('root id="body-definition"', t('range'))),
bind('/root/range').type(type)
)
),
body(range('/root/range', { start, end, step }))
);

const xformDOM = XFormDOM.from(xform.asXml());
const xformDefinition = new XFormDefinition(xformDOM);
const rangeElement = xformDefinition.body.element.children[0];

return new RangeControlDefinition(xformDefinition, xformDefinition.body, rangeElement!);
};

describe('bounds', () => {
describe('int', () => {
it('parses', () => {
const definition = create('int', -2, 10, 2);
expect(definition.bounds.start).to.equal('-2');
expect(definition.bounds.step).to.equal('2');
expect(definition.bounds.end).to.equal('10');
});

it('takes the absolute value of step', () => {
const definition = create('int', 0, 10, -2);
expect(definition.bounds.start).to.equal('0');
expect(definition.bounds.step).to.equal('2');
expect(definition.bounds.end).to.equal('10');
});
});

describe('decimal', () => {
it('parses', () => {
const definition = create('decimal', -2.5, 10.5, 2.5);
expect(definition.bounds.start).to.equal('-2.5');
expect(definition.bounds.step).to.equal('2.5');
expect(definition.bounds.end).to.equal('10.5');
});

it('takes the absolute value of step', () => {
const definition = create('decimal', 0, 10, -2.5);
expect(definition.bounds.start).to.equal('0');
expect(definition.bounds.step).to.equal('2.5');
expect(definition.bounds.end).to.equal('10');
});
});
});
});