@qti-components/delivery 0.0.0 → 0.1.0-next.1

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/dist/index.d.ts CHANGED
@@ -1,3 +1,6 @@
1
+ export * from './model/test-model.js';
2
+ export * from './model/item-facts.js';
3
+ export type { XmlElement, XmlNode } from './model/xml-node.js';
1
4
  export * from './random/seeded-random.js';
2
5
  export * from './random/shuffle.js';
3
6
  export * from './state/clock.js';
package/dist/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ export * from './model/test-model.js';
2
+ export * from './model/item-facts.js';
1
3
  export * from './random/seeded-random.js';
2
4
  export * from './random/shuffle.js';
3
5
  export * from './state/clock.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kCAAkC,CAAC;AACjD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iBAAiB,CAAC","sourcesContent":["export * from './random/seeded-random.js';\nexport * from './random/shuffle.js';\nexport * from './state/clock.js';\nexport * from './state/commands.js';\nexport * from './state/create-delivery-state.js';\nexport * from './state/delivery-state.js';\nexport * from './processing.js';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AAEtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kCAAkC,CAAC;AACjD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iBAAiB,CAAC","sourcesContent":["export * from './model/test-model.js';\nexport * from './model/item-facts.js';\nexport type { XmlElement, XmlNode } from './model/xml-node.js';\nexport * from './random/seeded-random.js';\nexport * from './random/shuffle.js';\nexport * from './state/clock.js';\nexport * from './state/commands.js';\nexport * from './state/create-delivery-state.js';\nexport * from './state/delivery-state.js';\nexport * from './processing.js';\n"]}
@@ -0,0 +1,34 @@
1
+ import type { XmlElement } from './xml-node.js';
2
+ /**
3
+ * What a `qti-assessment-item` says about itself, read from its XML when it is fetched: no live
4
+ * state, nothing that needs the item rendered. Plain JSON, so a server can read the same file.
5
+ *
6
+ * Experimental (0.x): roadmap 1 phase T1; see `plans/test-model.md`.
7
+ */
8
+ export interface ItemFacts {
9
+ /** The item's own `identifier`, which may differ from the item-ref's. */
10
+ identifier: string;
11
+ /** `adaptive`; false when absent (XSD default). */
12
+ adaptive: boolean;
13
+ /** `time-dependent`; false when absent. */
14
+ timeDependent: boolean;
15
+ /**
16
+ * A response declaration has a correct response, or a mapping with a map entry, or an area
17
+ * mapping with an area-map entry: something to compare a response with.
18
+ */
19
+ hasCorrectResponse: boolean;
20
+ /** The item has `qti-response-processing`. */
21
+ hasResponseProcessing: boolean;
22
+ /** `external-scored` on the `SCORE` outcome declaration, or null when not externally scored. */
23
+ externalScored: 'human' | 'externalMachine' | null;
24
+ /** `label`, or null when absent. */
25
+ label: string | null;
26
+ /** `title`; empty when absent. */
27
+ title: string;
28
+ }
29
+ /**
30
+ * Read the facts of an item from its `qti-assessment-item` element: the XML document under Node,
31
+ * the fetched document in the browser. Reads attributes and child elements only, so no element
32
+ * needs to be registered.
33
+ */
34
+ export declare function readItemFacts(item: XmlElement): ItemFacts;
@@ -0,0 +1,37 @@
1
+ import { booleanAttribute, childElement, childElements } from './xml-node.js';
2
+ /**
3
+ * Read the facts of an item from its `qti-assessment-item` element: the XML document under Node,
4
+ * the fetched document in the browser. Reads attributes and child elements only, so no element
5
+ * needs to be registered.
6
+ */
7
+ export function readItemFacts(item) {
8
+ if (item.localName !== 'qti-assessment-item') {
9
+ throw new TypeError(`Expected a qti-assessment-item element, not ${String(item.localName)}.`);
10
+ }
11
+ const responseDeclarations = childElements(item, 'qti-response-declaration');
12
+ const score = childElements(item, 'qti-outcome-declaration').find(declaration => declaration.getAttribute('identifier') === 'SCORE');
13
+ return {
14
+ identifier: item.getAttribute('identifier') ?? '',
15
+ adaptive: booleanAttribute(item, 'adaptive') ?? false,
16
+ timeDependent: booleanAttribute(item, 'time-dependent') ?? false,
17
+ hasCorrectResponse: responseDeclarations.some(hasSomethingToCompareWith),
18
+ hasResponseProcessing: childElement(item, 'qti-response-processing') !== null,
19
+ externalScored: externalScored(score),
20
+ label: item.getAttribute('label'),
21
+ title: item.getAttribute('title') ?? ''
22
+ };
23
+ }
24
+ function hasSomethingToCompareWith(declaration) {
25
+ if (childElement(declaration, 'qti-correct-response'))
26
+ return true;
27
+ const mapping = childElement(declaration, 'qti-mapping');
28
+ if (mapping && childElement(mapping, 'qti-map-entry'))
29
+ return true;
30
+ const areaMapping = childElement(declaration, 'qti-area-mapping');
31
+ return areaMapping !== null && childElement(areaMapping, 'qti-area-map-entry') !== null;
32
+ }
33
+ function externalScored(declaration) {
34
+ const value = declaration?.getAttribute('external-scored')?.trim();
35
+ return value === 'human' || value === 'externalMachine' ? value : null;
36
+ }
37
+ //# sourceMappingURL=item-facts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"item-facts.js","sourceRoot":"","sources":["../../src/model/item-facts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAgC9E;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,IAAgB;IAC5C,IAAI,IAAI,CAAC,SAAS,KAAK,qBAAqB,EAAE,CAAC;QAC7C,MAAM,IAAI,SAAS,CAAC,+CAA+C,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAChG,CAAC;IACD,MAAM,oBAAoB,GAAG,aAAa,CAAC,IAAI,EAAE,0BAA0B,CAAC,C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,EAAE,IAAI,EAAE,CAAC;IACnE,OAAO,KAAK,KAAK,OAAO,IAAI,KAAK,KAAK,iBAAiB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;AACzE,CAAC","sourcesContent":["import { booleanAttribute, childElement, childElements } from './xml-node.js';\n\nimport type { XmlElement } from './xml-node.js';\n\n/**\n * What a `qti-assessment-item` says about itself, read from its XML when it is fetched: no live\n * state, nothing that needs the item rendered. Plain JSON, so a server can read the same file.\n *\n * Experimental (0.x): roadmap 1 phase T1; see `plans/test-model.md`.\n */\nexport interface ItemFacts {\n /** The item's own `identifier`, which may differ from the item-ref's. */\n identifier: string;\n /** `adaptive`; false when absent (XSD default). */\n adaptive: boolean;\n /** `time-dependent`; false when absent. */\n timeDependent: boolean;\n /**\n * A response declaration has a correct response, or a mapping with a map entry, or an area\n * mapping with an area-map entry: something to compare a response with.\n */\n hasCorrectResponse: boolean;\n /** The item has `qti-response-processing`. */\n hasResponseProcessing: boolean;\n /** `external-scored` on the `SCORE` outcome declaration, or null when not externally scored. */\n externalScored: 'human' | 'externalMachine' | null;\n /** `label`, or null when absent. */\n label: string | null;\n /** `title`; empty when absent. */\n title: string;\n}\n\n/**\n * Read the facts of an item from its `qti-assessment-item` element: the XML document under Node,\n * the fetched document in the browser. Reads attributes and child elements only, so no element\n * needs to be registered.\n */\nexport function readItemFacts(item: XmlElement): ItemFacts {\n if (item.localName !== 'qti-assessment-item') {\n throw new TypeError(`Expected a qti-assessment-item element, not ${String(item.localName)}.`);\n }\n const responseDeclarations = childElements(item, 'qti-response-declaration');\n const score = childElements(item, 'qti-outcome-declaration').find(\n declaration => declaration.getAttribute('identifier') === 'SCORE'\n );\n return {\n identifier: item.getAttribute('identifier') ?? '',\n adaptive: booleanAttribute(item, 'adaptive') ?? false,\n timeDependent: booleanAttribute(item, 'time-dependent') ?? false,\n hasCorrectResponse: responseDeclarations.some(hasSomethingToCompareWith),\n hasResponseProcessing: childElement(item, 'qti-response-processing') !== null,\n externalScored: externalScored(score),\n label: item.getAttribute('label'),\n title: item.getAttribute('title') ?? ''\n };\n}\n\nfunction hasSomethingToCompareWith(declaration: XmlElement): boolean {\n if (childElement(declaration, 'qti-correct-response')) return true;\n const mapping = childElement(declaration, 'qti-mapping');\n if (mapping && childElement(mapping, 'qti-map-entry')) return true;\n const areaMapping = childElement(declaration, 'qti-area-mapping');\n return areaMapping !== null && childElement(areaMapping, 'qti-area-map-entry') !== null;\n}\n\nfunction externalScored(declaration: XmlElement | undefined): ItemFacts['externalScored'] {\n const value = declaration?.getAttribute('external-scored')?.trim();\n return value === 'human' || value === 'externalMachine' ? value : null;\n}\n"]}
@@ -0,0 +1,74 @@
1
+ import type { XmlElement } from './xml-node.js';
2
+ /**
3
+ * The structure and the resolved rules of a `qti-assessment-test`, read from its XML once per load.
4
+ * Plain JSON, so a server can build it from the same file.
5
+ *
6
+ * Experimental (0.x): roadmap 1 phase T1; see `plans/test-model.md`.
7
+ */
8
+ export interface TestModel {
9
+ identifier: string;
10
+ title: string;
11
+ testParts: TestPartModel[];
12
+ }
13
+ export interface TestPartModel {
14
+ identifier: string;
15
+ /** Defined on the test part only (QTI 3 XSD); a section never changes it. */
16
+ navigationMode: 'linear' | 'nonlinear';
17
+ submissionMode: 'individual' | 'simultaneous';
18
+ sessionControl: SessionControl;
19
+ sections: SectionModel[];
20
+ }
21
+ export interface SectionModel {
22
+ kind: 'section';
23
+ identifier: string;
24
+ title: string;
25
+ visible: boolean;
26
+ sessionControl: SessionControl;
27
+ /** The item-refs and sections directly inside this one, in document order, as the XML has them. */
28
+ content: SectionContent[];
29
+ }
30
+ export interface ItemRefModel {
31
+ kind: 'itemRef';
32
+ identifier: string;
33
+ href: string;
34
+ categories: string[];
35
+ sessionControl: SessionControl;
36
+ /** What this item-ref's own `qti-item-session-control` declares, nothing inherited. */
37
+ declaredSessionControl: Partial<SessionControl>;
38
+ }
39
+ export type SectionContent = SectionModel | ItemRefModel;
40
+ /** `qti-item-session-control`, resolved for one level (QTI 3 info model §7.19). */
41
+ export interface SessionControl {
42
+ /** 0 is unlimited. Ignored for adaptive items, which end by `completionStatus`. */
43
+ maxAttempts: number;
44
+ showFeedback: boolean;
45
+ allowReview: boolean;
46
+ showSolution: boolean;
47
+ allowComment: boolean;
48
+ /** Applies only in a test part with individual submission. */
49
+ allowSkipping: boolean;
50
+ /** Applies only in a test part with individual submission. */
51
+ validateResponses: boolean;
52
+ }
53
+ /**
54
+ * The values when no level declares an attribute: the XSD defaults, and 1 for `max-attempts`,
55
+ * which the spec text gives ("if it is unspecified it is treated as 1 for non-adaptive items").
56
+ */
57
+ export declare const DEFAULT_SESSION_CONTROL: Readonly<SessionControl>;
58
+ /**
59
+ * Build the model of a test from its `qti-assessment-test` element: the XML document under Node, the
60
+ * rendered element in the browser. Reads attributes and child elements only, so no element needs
61
+ * to be registered.
62
+ *
63
+ * Session control inherits per attribute: each one comes from the closest level that declares it
64
+ * (item-ref, then each enclosing section, then the test part), and its default only when none does.
65
+ * The spec: a test-part setting applies "unless the setting is overridden", and the defaults "are
66
+ * used only in the absence of any applicable constraint" (implementation guide §4.4.2).
67
+ */
68
+ export declare function buildTestModel(test: XmlElement): TestModel;
69
+ /** The item-refs directly inside a section, in document order; not those of its subsections. */
70
+ export declare const ownItemRefs: (section: SectionModel) => ItemRefModel[];
71
+ /** Every section, depth first in document order: a section before its subsections. */
72
+ export declare function sectionsInOrder(sections: SectionModel[]): SectionModel[];
73
+ /** Every item-ref inside the given sections, at any depth, in document order. */
74
+ export declare function itemRefsInOrder(sections: SectionContent[]): ItemRefModel[];
@@ -0,0 +1,102 @@
1
+ import { booleanAttribute, childElement, childElements, integerAttribute } from './xml-node.js';
2
+ /**
3
+ * The values when no level declares an attribute: the XSD defaults, and 1 for `max-attempts`,
4
+ * which the spec text gives ("if it is unspecified it is treated as 1 for non-adaptive items").
5
+ */
6
+ export const DEFAULT_SESSION_CONTROL = Object.freeze({
7
+ maxAttempts: 1,
8
+ showFeedback: false,
9
+ allowReview: true,
10
+ showSolution: false,
11
+ allowComment: false,
12
+ allowSkipping: true,
13
+ validateResponses: false
14
+ });
15
+ /**
16
+ * Build the model of a test from its `qti-assessment-test` element: the XML document under Node, the
17
+ * rendered element in the browser. Reads attributes and child elements only, so no element needs
18
+ * to be registered.
19
+ *
20
+ * Session control inherits per attribute: each one comes from the closest level that declares it
21
+ * (item-ref, then each enclosing section, then the test part), and its default only when none does.
22
+ * The spec: a test-part setting applies "unless the setting is overridden", and the defaults "are
23
+ * used only in the absence of any applicable constraint" (implementation guide §4.4.2).
24
+ */
25
+ export function buildTestModel(test) {
26
+ if (test.localName !== 'qti-assessment-test') {
27
+ throw new TypeError(`Expected a qti-assessment-test element, not ${String(test.localName)}.`);
28
+ }
29
+ return {
30
+ identifier: test.getAttribute('identifier') ?? '',
31
+ title: test.getAttribute('title') ?? '',
32
+ testParts: childElements(test, 'qti-test-part').map(buildTestPart)
33
+ };
34
+ }
35
+ function buildTestPart(part) {
36
+ const sessionControl = resolveSessionControl(DEFAULT_SESSION_CONTROL, part);
37
+ return {
38
+ identifier: part.getAttribute('identifier') ?? '',
39
+ navigationMode: part.getAttribute('navigation-mode') === 'linear' ? 'linear' : 'nonlinear',
40
+ submissionMode: part.getAttribute('submission-mode') === 'simultaneous' ? 'simultaneous' : 'individual',
41
+ sessionControl,
42
+ sections: childElements(part, 'qti-assessment-section').map(section => buildSection(section, sessionControl))
43
+ };
44
+ }
45
+ function buildSection(section, inherited) {
46
+ const sessionControl = resolveSessionControl(inherited, section);
47
+ return {
48
+ kind: 'section',
49
+ identifier: section.getAttribute('identifier') ?? '',
50
+ title: section.getAttribute('title') ?? '',
51
+ visible: booleanAttribute(section, 'visible') ?? true,
52
+ sessionControl,
53
+ content: childElements(section, 'qti-assessment-section', 'qti-assessment-item-ref').map(child => child.localName === 'qti-assessment-section'
54
+ ? buildSection(child, sessionControl)
55
+ : buildItemRef(child, sessionControl))
56
+ };
57
+ }
58
+ function buildItemRef(itemRef, inherited) {
59
+ const category = itemRef.getAttribute('category')?.trim();
60
+ return {
61
+ kind: 'itemRef',
62
+ identifier: itemRef.getAttribute('identifier') ?? '',
63
+ href: itemRef.getAttribute('href') ?? '',
64
+ categories: category ? category.split(/\s+/) : [],
65
+ sessionControl: resolveSessionControl(inherited, itemRef),
66
+ declaredSessionControl: declaredSessionControl(itemRef)
67
+ };
68
+ }
69
+ /** `inherited`, with what `owner`'s own `qti-item-session-control` declares on top. */
70
+ function resolveSessionControl(inherited, owner) {
71
+ return { ...inherited, ...declaredSessionControl(owner) };
72
+ }
73
+ /** Only the attributes `owner`'s `qti-item-session-control` declares, so an absent one inherits. */
74
+ function declaredSessionControl(owner) {
75
+ const element = childElement(owner, 'qti-item-session-control');
76
+ if (!element)
77
+ return {};
78
+ const declared = {
79
+ maxAttempts: integerAttribute(element, 'max-attempts'),
80
+ showFeedback: booleanAttribute(element, 'show-feedback'),
81
+ allowReview: booleanAttribute(element, 'allow-review'),
82
+ showSolution: booleanAttribute(element, 'show-solution'),
83
+ allowComment: booleanAttribute(element, 'allow-comment'),
84
+ allowSkipping: booleanAttribute(element, 'allow-skipping'),
85
+ validateResponses: booleanAttribute(element, 'validate-responses')
86
+ };
87
+ return Object.fromEntries(Object.entries(declared).filter(([, value]) => value !== undefined));
88
+ }
89
+ /** The item-refs directly inside a section, in document order; not those of its subsections. */
90
+ export const ownItemRefs = (section) => section.content.filter((child) => child.kind === 'itemRef');
91
+ /** Every section, depth first in document order: a section before its subsections. */
92
+ export function sectionsInOrder(sections) {
93
+ return sections.flatMap(section => [
94
+ section,
95
+ ...sectionsInOrder(section.content.filter((child) => child.kind === 'section'))
96
+ ]);
97
+ }
98
+ /** Every item-ref inside the given sections, at any depth, in document order. */
99
+ export function itemRefsInOrder(sections) {
100
+ return sections.flatMap(child => (child.kind === 'itemRef' ? [child] : itemRefsInOrder(child.content)));
101
+ }
102
+ //# sourceMappingURL=test-model.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-model.js","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AM,CAAC,CAAC,KAAK,EAAyB,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC;AAErF,sFAAsF;AACtF,MAAM,UAAU,eAAe,CAAC,QAAwB;IACtD,OAAO,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACjC,OAAO;QACP,GAAG,eAAe,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,EAAyB,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC;KACvG,CAAC,CAAC;AACL,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,eAAe,CAAC,QAA0B;IACxD,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAC1G,CAAC","sourcesContent":["import { booleanAttribute, childElement, childElements, integerAttribute } from './xml-node.js';\n\nimport type { XmlElement } from './xml-node.js';\n\n/**\n * The structure and the resolved rules of a `qti-assessment-test`, read from its XML once per load.\n * Plain JSON, so a server can build it from the same file.\n *\n * Experimental (0.x): roadmap 1 phase T1; see `plans/test-model.md`.\n */\nexport interface TestModel {\n identifier: string;\n title: string;\n testParts: TestPartModel[];\n}\n\nexport interface TestPartModel {\n identifier: string;\n /** Defined on the test part only (QTI 3 XSD); a section never changes it. */\n navigationMode: 'linear' | 'nonlinear';\n submissionMode: 'individual' | 'simultaneous';\n sessionControl: SessionControl;\n sections: SectionModel[];\n}\n\nexport interface SectionModel {\n kind: 'section';\n identifier: string;\n title: string;\n visible: boolean;\n sessionControl: SessionControl;\n /** The item-refs and sections directly inside this one, in document order, as the XML has them. */\n content: SectionContent[];\n}\n\nexport interface ItemRefModel {\n kind: 'itemRef';\n identifier: string;\n href: string;\n categories: string[];\n sessionControl: SessionControl;\n /** What this item-ref's own `qti-item-session-control` declares, nothing inherited. */\n declaredSessionControl: Partial<SessionControl>;\n}\n\nexport type SectionContent = SectionModel | ItemRefModel;\n\n/** `qti-item-session-control`, resolved for one level (QTI 3 info model §7.19). */\nexport interface SessionControl {\n /** 0 is unlimited. Ignored for adaptive items, which end by `completionStatus`. */\n maxAttempts: number;\n showFeedback: boolean;\n allowReview: boolean;\n showSolution: boolean;\n allowComment: boolean;\n /** Applies only in a test part with individual submission. */\n allowSkipping: boolean;\n /** Applies only in a test part with individual submission. */\n validateResponses: boolean;\n}\n\n/**\n * The values when no level declares an attribute: the XSD defaults, and 1 for `max-attempts`,\n * which the spec text gives (\"if it is unspecified it is treated as 1 for non-adaptive items\").\n */\nexport const DEFAULT_SESSION_CONTROL: Readonly<SessionControl> = Object.freeze({\n maxAttempts: 1,\n showFeedback: false,\n allowReview: true,\n showSolution: false,\n allowComment: false,\n allowSkipping: true,\n validateResponses: false\n});\n\n/**\n * Build the model of a test from its `qti-assessment-test` element: the XML document under Node, the\n * rendered element in the browser. Reads attributes and child elements only, so no element needs\n * to be registered.\n *\n * Session control inherits per attribute: each one comes from the closest level that declares it\n * (item-ref, then each enclosing section, then the test part), and its default only when none does.\n * The spec: a test-part setting applies \"unless the setting is overridden\", and the defaults \"are\n * used only in the absence of any applicable constraint\" (implementation guide §4.4.2).\n */\nexport function buildTestModel(test: XmlElement): TestModel {\n if (test.localName !== 'qti-assessment-test') {\n throw new TypeError(`Expected a qti-assessment-test element, not ${String(test.localName)}.`);\n }\n return {\n identifier: test.getAttribute('identifier') ?? '',\n title: test.getAttribute('title') ?? '',\n testParts: childElements(test, 'qti-test-part').map(buildTestPart)\n };\n}\n\nfunction buildTestPart(part: XmlElement): TestPartModel {\n const sessionControl = resolveSessionControl(DEFAULT_SESSION_CONTROL, part);\n return {\n identifier: part.getAttribute('identifier') ?? '',\n navigationMode: part.getAttribute('navigation-mode') === 'linear' ? 'linear' : 'nonlinear',\n submissionMode: part.getAttribute('submission-mode') === 'simultaneous' ? 'simultaneous' : 'individual',\n sessionControl,\n sections: childElements(part, 'qti-assessment-section').map(section => buildSection(section, sessionControl))\n };\n}\n\nfunction buildSection(section: XmlElement, inherited: SessionControl): SectionModel {\n const sessionControl = resolveSessionControl(inherited, section);\n return {\n kind: 'section',\n identifier: section.getAttribute('identifier') ?? '',\n title: section.getAttribute('title') ?? '',\n visible: booleanAttribute(section, 'visible') ?? true,\n sessionControl,\n content: childElements(section, 'qti-assessment-section', 'qti-assessment-item-ref').map(child =>\n child.localName === 'qti-assessment-section'\n ? buildSection(child, sessionControl)\n : buildItemRef(child, sessionControl)\n )\n };\n}\n\nfunction buildItemRef(itemRef: XmlElement, inherited: SessionControl): ItemRefModel {\n const category = itemRef.getAttribute('category')?.trim();\n return {\n kind: 'itemRef',\n identifier: itemRef.getAttribute('identifier') ?? '',\n href: itemRef.getAttribute('href') ?? '',\n categories: category ? category.split(/\\s+/) : [],\n sessionControl: resolveSessionControl(inherited, itemRef),\n declaredSessionControl: declaredSessionControl(itemRef)\n };\n}\n\n/** `inherited`, with what `owner`'s own `qti-item-session-control` declares on top. */\nfunction resolveSessionControl(inherited: Readonly<SessionControl>, owner: XmlElement): SessionControl {\n return { ...inherited, ...declaredSessionControl(owner) };\n}\n\n/** Only the attributes `owner`'s `qti-item-session-control` declares, so an absent one inherits. */\nfunction declaredSessionControl(owner: XmlElement): Partial<SessionControl> {\n const element = childElement(owner, 'qti-item-session-control');\n if (!element) return {};\n const declared: Partial<SessionControl> = {\n maxAttempts: integerAttribute(element, 'max-attempts'),\n showFeedback: booleanAttribute(element, 'show-feedback'),\n allowReview: booleanAttribute(element, 'allow-review'),\n showSolution: booleanAttribute(element, 'show-solution'),\n allowComment: booleanAttribute(element, 'allow-comment'),\n allowSkipping: booleanAttribute(element, 'allow-skipping'),\n validateResponses: booleanAttribute(element, 'validate-responses')\n };\n return Object.fromEntries(Object.entries(declared).filter(([, value]) => value !== undefined));\n}\n\n/** The item-refs directly inside a section, in document order; not those of its subsections. */\nexport const ownItemRefs = (section: SectionModel): ItemRefModel[] =>\n section.content.filter((child): child is ItemRefModel => child.kind === 'itemRef');\n\n/** Every section, depth first in document order: a section before its subsections. */\nexport function sectionsInOrder(sections: SectionModel[]): SectionModel[] {\n return sections.flatMap(section => [\n section,\n ...sectionsInOrder(section.content.filter((child): child is SectionModel => child.kind === 'section'))\n ]);\n}\n\n/** Every item-ref inside the given sections, at any depth, in document order. */\nexport function itemRefsInOrder(sections: SectionContent[]): ItemRefModel[] {\n return sections.flatMap(child => (child.kind === 'itemRef' ? [child] : itemRefsInOrder(child.content)));\n}\n"]}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * The part of a DOM element that `delivery` reads: a browser `Element` and an `@xmldom/xmldom`
3
+ * element both satisfy it, so the same code builds the model in the browser and under Node. Only
4
+ * attributes and child elements, never upgraded properties, so nothing depends on a custom element
5
+ * being registered.
6
+ */
7
+ export interface XmlElement {
8
+ readonly nodeType: number;
9
+ readonly localName: string | null;
10
+ readonly childNodes: ArrayLike<XmlNode>;
11
+ /** Null when the attribute is absent, in the browser and in xmldom alike. */
12
+ getAttribute(name: string): string | null;
13
+ }
14
+ export interface XmlNode {
15
+ readonly nodeType: number;
16
+ }
17
+ /** The child elements of `parent` with one of the given local names, in document order. */
18
+ export declare function childElements(parent: XmlElement, ...localNames: string[]): XmlElement[];
19
+ /** The first child element with the given local name, or null. */
20
+ export declare const childElement: (parent: XmlElement, localName: string) => XmlElement | null;
21
+ /** An `xs:boolean` attribute: `true`/`1` and `false`/`0`; anything else, or absent, is undefined. */
22
+ export declare function booleanAttribute(element: XmlElement, name: string): boolean | undefined;
23
+ /** An `xs:int` attribute, or undefined when absent or not an integer. */
24
+ export declare function integerAttribute(element: XmlElement, name: string): number | undefined;
@@ -0,0 +1,30 @@
1
+ const ELEMENT_NODE = 1;
2
+ /** The child elements of `parent` with one of the given local names, in document order. */
3
+ export function childElements(parent, ...localNames) {
4
+ const nodes = parent.childNodes;
5
+ const children = [];
6
+ for (let index = 0; index < nodes.length; index++) {
7
+ const node = nodes[index];
8
+ if (node.nodeType === ELEMENT_NODE && localNames.includes(node.localName ?? '')) {
9
+ children.push(node);
10
+ }
11
+ }
12
+ return children;
13
+ }
14
+ /** The first child element with the given local name, or null. */
15
+ export const childElement = (parent, localName) => childElements(parent, localName)[0] ?? null;
16
+ /** An `xs:boolean` attribute: `true`/`1` and `false`/`0`; anything else, or absent, is undefined. */
17
+ export function booleanAttribute(element, name) {
18
+ const value = element.getAttribute(name)?.trim();
19
+ if (value === 'true' || value === '1')
20
+ return true;
21
+ if (value === 'false' || value === '0')
22
+ return false;
23
+ return undefined;
24
+ }
25
+ /** An `xs:int` attribute, or undefined when absent or not an integer. */
26
+ export function integerAttribute(element, name) {
27
+ const value = element.getAttribute(name)?.trim();
28
+ return value && /^[+-]?\d+$/.test(value) ? Number(value) : undefined;
29
+ }
30
+ //# sourceMappingURL=xml-node.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xml-node.js","sourceRoot":"","sources":["../../src/model/xml-node.ts"],"names":[],"mappings":"AAkBA,MAAM,YAAY,GAAG,CAAC,CAAC;AAEvB,2FAA2F;AAC3F,MAAM,UAAU,aAAa,CAAC,MAAkB,EAAE,GAAG,UAAoB;IACvE,MAAM,KAAK,GAAG,MAAM,CAAC,UAAU,CAAC;IAChC,MAAM,QAAQ,GAAiB,EAAE,CAAC;IAClC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAClD,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;QAC1B,IAAI,IAAI,CAAC,QAAQ,KAAK,YAAY,IAAI,UAAU,CAAC,QAAQ,CAAE,IAAmB,CAAC,SAAS,IAAI,EAAE,CAAC,EAAE,CAAC;YAChG,QAAQ,CAAC,IAAI,CAAC,IAAkB,CAAC,CAAC;QACpC,CAAC;IACH,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,kEAAkE;AAClE,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,MAAkB,EAAE,SAAiB,EAAqB,EAAE,CACvF,aAAa,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;AAE9C,qGAAqG;AACrG,MAAM,UAAU,gBAAgB,CAAC,OAAmB,EAAE,IAAY;IAChE,MAAM,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,CAAC;IACjD,IAAI,KAAK,KAAK,MAAM,IAAI,KAAK,KAAK,GAAG;QAAE,OAAO,IAAI,CAAC;IACnD,IAAI,KAAK,KAAK,OAAO,IAAI,KAAK,KAAK,GAAG;QAAE,OAAO,KAAK,CAAC;IACrD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,gBAAgB,CAAC,OAAmB,EAAE,IAAY;IAChE,MAAM,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,CAAC;IACjD,OAAO,KAAK,IAAI,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AACvE,CAAC","sourcesContent":["/**\n * The part of a DOM element that `delivery` reads: a browser `Element` and an `@xmldom/xmldom`\n * element both satisfy it, so the same code builds the model in the browser and under Node. Only\n * attributes and child elements, never upgraded properties, so nothing depends on a custom element\n * being registered.\n */\nexport interface XmlElement {\n readonly nodeType: number;\n readonly localName: string | null;\n readonly childNodes: ArrayLike<XmlNode>;\n /** Null when the attribute is absent, in the browser and in xmldom alike. */\n getAttribute(name: string): string | null;\n}\n\nexport interface XmlNode {\n readonly nodeType: number;\n}\n\nconst ELEMENT_NODE = 1;\n\n/** The child elements of `parent` with one of the given local names, in document order. */\nexport function childElements(parent: XmlElement, ...localNames: string[]): XmlElement[] {\n const nodes = parent.childNodes;\n const children: XmlElement[] = [];\n for (let index = 0; index < nodes.length; index++) {\n const node = nodes[index];\n if (node.nodeType === ELEMENT_NODE && localNames.includes((node as XmlElement).localName ?? '')) {\n children.push(node as XmlElement);\n }\n }\n return children;\n}\n\n/** The first child element with the given local name, or null. */\nexport const childElement = (parent: XmlElement, localName: string): XmlElement | null =>\n childElements(parent, localName)[0] ?? null;\n\n/** An `xs:boolean` attribute: `true`/`1` and `false`/`0`; anything else, or absent, is undefined. */\nexport function booleanAttribute(element: XmlElement, name: string): boolean | undefined {\n const value = element.getAttribute(name)?.trim();\n if (value === 'true' || value === '1') return true;\n if (value === 'false' || value === '0') return false;\n return undefined;\n}\n\n/** An `xs:int` attribute, or undefined when absent or not an integer. */\nexport function integerAttribute(element: XmlElement, name: string): number | undefined {\n const value = element.getAttribute(name)?.trim();\n return value && /^[+-]?\\d+$/.test(value) ? Number(value) : undefined;\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@qti-components/delivery",
3
3
  "description": "The QTI 3 delivery engine: test model, item session lifecycle, sequencing and session state, with no browser globals",
4
- "version": "0.0.0",
4
+ "version": "0.1.0-next.1",
5
5
  "author": "",
6
6
  "keywords": [
7
7
  "qti",
@@ -29,6 +29,9 @@
29
29
  "url": "git+https://github.com/Citolab/qti-components.git",
30
30
  "directory": "packages/qti-delivery"
31
31
  },
32
+ "devDependencies": {
33
+ "@xmldom/xmldom": "^0.9.12"
34
+ },
32
35
  "scripts": {
33
36
  "attw": "attw --profile esm-only --pack",
34
37
  "build": "pnpm run clean && tsc --paths null",