@pie-qti/qti-common 1.0.0-beta.0 → 1.0.0-beta.2

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 (76) hide show
  1. package/dist/accessibility/apip.d.ts +66 -0
  2. package/dist/accessibility/apip.js +576 -0
  3. package/dist/accessibility/apip.js.map +1 -0
  4. package/dist/accessibility/catalog-info.d.ts +17 -0
  5. package/dist/accessibility/catalog-info.js +60 -0
  6. package/dist/accessibility/catalog-info.js.map +1 -0
  7. package/dist/accessibility/catalog.d.ts +53 -0
  8. package/dist/accessibility/catalog.js +68 -0
  9. package/dist/accessibility/catalog.js.map +1 -0
  10. package/dist/accessibility/index.d.ts +4 -0
  11. package/dist/accessibility/index.js +5 -0
  12. package/dist/accessibility/index.js.map +1 -0
  13. package/dist/accessibility/markup.d.ts +61 -0
  14. package/dist/accessibility/markup.js +484 -0
  15. package/dist/accessibility/markup.js.map +1 -0
  16. package/dist/dom/assignProps.d.ts +4 -0
  17. package/dist/dom/assignProps.js +9 -0
  18. package/dist/dom/assignProps.js.map +1 -0
  19. package/dist/dom/svelteMountController.d.ts +25 -0
  20. package/dist/dom/svelteMountController.js +131 -0
  21. package/dist/dom/svelteMountController.js.map +1 -0
  22. package/dist/dom/touchDrag.d.ts +10 -0
  23. package/dist/dom/touchDrag.js +141 -0
  24. package/dist/dom/touchDrag.js.map +1 -0
  25. package/dist/element-mapper/AttributeNameMapper.d.ts +66 -0
  26. package/dist/element-mapper/AttributeNameMapper.js +2 -0
  27. package/dist/element-mapper/AttributeNameMapper.js.map +1 -0
  28. package/dist/element-mapper/ElementNameMapper.d.ts +66 -0
  29. package/dist/element-mapper/ElementNameMapper.js +2 -0
  30. package/dist/element-mapper/ElementNameMapper.js.map +1 -0
  31. package/dist/element-mapper/Qti2xAttributeNameMapper.d.ts +27 -0
  32. package/dist/element-mapper/Qti2xAttributeNameMapper.js +38 -0
  33. package/dist/element-mapper/Qti2xAttributeNameMapper.js.map +1 -0
  34. package/dist/element-mapper/Qti2xElementNameMapper.d.ts +37 -0
  35. package/dist/element-mapper/Qti2xElementNameMapper.js +47 -0
  36. package/dist/element-mapper/Qti2xElementNameMapper.js.map +1 -0
  37. package/dist/element-mapper/Qti3AttributeNameMapper.d.ts +40 -0
  38. package/dist/element-mapper/Qti3AttributeNameMapper.js +87 -0
  39. package/dist/element-mapper/Qti3AttributeNameMapper.js.map +1 -0
  40. package/dist/element-mapper/Qti3ElementNameMapper.d.ts +14 -0
  41. package/dist/element-mapper/Qti3ElementNameMapper.js +39 -0
  42. package/dist/element-mapper/Qti3ElementNameMapper.js.map +1 -0
  43. package/dist/element-mapper/index.d.ts +8 -0
  44. package/dist/element-mapper/index.js +7 -0
  45. package/dist/element-mapper/index.js.map +1 -0
  46. package/dist/element-mapper/qti2-to-qti3.d.ts +36 -0
  47. package/dist/element-mapper/qti2-to-qti3.js +228 -0
  48. package/dist/element-mapper/qti2-to-qti3.js.map +1 -0
  49. package/dist/element-mapper/qti3-attribute-map.d.ts +8 -0
  50. package/dist/element-mapper/qti3-attribute-map.js +115 -0
  51. package/dist/element-mapper/qti3-attribute-map.js.map +1 -0
  52. package/dist/element-mapper/qti3-element-mappings.d.ts +538 -0
  53. package/dist/element-mapper/qti3-element-mappings.js +381 -0
  54. package/dist/element-mapper/qti3-element-mappings.js.map +1 -0
  55. package/dist/feedback/visibility.d.ts +12 -0
  56. package/dist/feedback/visibility.js +22 -0
  57. package/dist/feedback/visibility.js.map +1 -0
  58. package/dist/index.d.ts +17 -0
  59. package/dist/index.js +21 -0
  60. package/dist/index.js.map +1 -0
  61. package/dist/language/direction.d.ts +10 -0
  62. package/dist/language/direction.js +42 -0
  63. package/dist/language/direction.js.map +1 -0
  64. package/dist/parser-factory.d.ts +137 -0
  65. package/dist/parser-factory.js +146 -0
  66. package/dist/parser-factory.js.map +1 -0
  67. package/dist/version-detection/detectQtiVersion.d.ts +23 -0
  68. package/dist/version-detection/detectQtiVersion.js +129 -0
  69. package/dist/version-detection/detectQtiVersion.js.map +1 -0
  70. package/dist/version-detection/index.d.ts +2 -0
  71. package/dist/version-detection/index.js +2 -0
  72. package/dist/version-detection/index.js.map +1 -0
  73. package/dist/xml/attributes.d.ts +77 -0
  74. package/dist/xml/attributes.js +120 -0
  75. package/dist/xml/attributes.js.map +1 -0
  76. package/package.json +2 -2
@@ -0,0 +1,137 @@
1
+ /**
2
+ * Parser factory for creating version-appropriate QTI parsers.
3
+ *
4
+ * This module provides a unified entry point for detecting QTI version
5
+ * and creating appropriate element name mappers.
6
+ */
7
+ import { type QtiVersion } from './version-detection/detectQtiVersion.js';
8
+ import type { ElementNameMapper } from './element-mapper/ElementNameMapper.js';
9
+ import type { AttributeNameMapper } from './element-mapper/AttributeNameMapper.js';
10
+ /**
11
+ * Result of creating a QTI parser.
12
+ */
13
+ export interface QtiParserResult {
14
+ /**
15
+ * Detected or specified QTI version
16
+ */
17
+ version: QtiVersion;
18
+ /**
19
+ * Element name mapper appropriate for the detected version
20
+ */
21
+ mapper: ElementNameMapper;
22
+ /**
23
+ * Attribute name mapper appropriate for the detected version
24
+ */
25
+ attributeMapper: AttributeNameMapper;
26
+ }
27
+ /**
28
+ * Options for creating a QTI parser.
29
+ */
30
+ export interface CreateParserOptions {
31
+ /**
32
+ * Override automatic version detection.
33
+ * If not provided, version will be detected from XML content.
34
+ */
35
+ version?: QtiVersion;
36
+ /**
37
+ * Custom element name mapper to use.
38
+ * If provided, overrides automatic mapper selection.
39
+ */
40
+ mapper?: ElementNameMapper;
41
+ /**
42
+ * Custom attribute name mapper to use.
43
+ * If provided, overrides automatic mapper selection.
44
+ */
45
+ attributeMapper?: AttributeNameMapper;
46
+ }
47
+ /**
48
+ * Create a QTI parser with appropriate element name mapper for the given XML.
49
+ *
50
+ * This function automatically detects the QTI version and returns the appropriate
51
+ * element name mapper. It can be used as the primary entry point for parsing
52
+ * any QTI content.
53
+ *
54
+ * @param xml - QTI XML content as string
55
+ * @param options - Optional configuration
56
+ * @returns Parser result with version plus element and attribute mappers
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * // Auto-detect version and create appropriate parser
61
+ * const { version, mapper, attributeMapper } = createQtiParser(xml);
62
+ * console.log(`Detected QTI ${version}`);
63
+ *
64
+ * // Use with qti-processing
65
+ * const program = buildResponseProcessingAst(responseProcessing, { elementNameMapper: mapper });
66
+ *
67
+ * // Use with item-player
68
+ * const definition = createAssessmentItemDefinition({
69
+ * itemXml: xml,
70
+ * elementNameMapper: mapper,
71
+ * attributeNameMapper: attributeMapper
72
+ * });
73
+ * ```
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * // Force specific version
78
+ * const { mapper } = createQtiParser(xml, { version: '3.0' });
79
+ * ```
80
+ */
81
+ export declare function createQtiParser(xml: string, options?: CreateParserOptions): QtiParserResult;
82
+ /**
83
+ * Create element name mapper for a specific QTI version.
84
+ *
85
+ * @param version - QTI version
86
+ * @returns Element name mapper for the version
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * const qti2Mapper = createMapperForVersion('2.2');
91
+ * const qti3Mapper = createMapperForVersion('3.0');
92
+ * ```
93
+ */
94
+ export declare function createMapperForVersion(version: QtiVersion): ElementNameMapper;
95
+ /**
96
+ * Create attribute name mapper for a specific QTI version.
97
+ *
98
+ * @param version - QTI version
99
+ * @returns Attribute name mapper for the version
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * const qti2AttrMapper = createAttributeMapperForVersion('2.2');
104
+ * const qti3AttrMapper = createAttributeMapperForVersion('3.0');
105
+ * ```
106
+ */
107
+ export declare function createAttributeMapperForVersion(version: QtiVersion): AttributeNameMapper;
108
+ /**
109
+ * Check if XML content is QTI 3.0.
110
+ *
111
+ * Convenience function for quickly checking if content uses QTI 3.0.
112
+ *
113
+ * @param xml - QTI XML content
114
+ * @returns true if QTI 3.0, false otherwise
115
+ *
116
+ * @example
117
+ * ```typescript
118
+ * if (isQti3(xml)) {
119
+ * console.log('This is QTI 3.0 content');
120
+ * }
121
+ * ```
122
+ */
123
+ export declare function isQti3(xml: string): boolean;
124
+ /**
125
+ * Check if XML content is QTI 2.x (2.0, 2.1, or 2.2).
126
+ *
127
+ * @param xml - QTI XML content
128
+ * @returns true if QTI 2.x, false otherwise
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * if (isQti2(xml)) {
133
+ * console.log('This is QTI 2.x content');
134
+ * }
135
+ * ```
136
+ */
137
+ export declare function isQti2(xml: string): boolean;
@@ -0,0 +1,146 @@
1
+ /**
2
+ * Parser factory for creating version-appropriate QTI parsers.
3
+ *
4
+ * This module provides a unified entry point for detecting QTI version
5
+ * and creating appropriate element name mappers.
6
+ */
7
+ import { detectQtiVersion } from './version-detection/detectQtiVersion.js';
8
+ import { Qti2xElementNameMapper } from './element-mapper/Qti2xElementNameMapper.js';
9
+ import { Qti3ElementNameMapper } from './element-mapper/Qti3ElementNameMapper.js';
10
+ import { Qti2xAttributeNameMapper } from './element-mapper/Qti2xAttributeNameMapper.js';
11
+ import { Qti3AttributeNameMapper } from './element-mapper/Qti3AttributeNameMapper.js';
12
+ /**
13
+ * Create a QTI parser with appropriate element name mapper for the given XML.
14
+ *
15
+ * This function automatically detects the QTI version and returns the appropriate
16
+ * element name mapper. It can be used as the primary entry point for parsing
17
+ * any QTI content.
18
+ *
19
+ * @param xml - QTI XML content as string
20
+ * @param options - Optional configuration
21
+ * @returns Parser result with version plus element and attribute mappers
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * // Auto-detect version and create appropriate parser
26
+ * const { version, mapper, attributeMapper } = createQtiParser(xml);
27
+ * console.log(`Detected QTI ${version}`);
28
+ *
29
+ * // Use with qti-processing
30
+ * const program = buildResponseProcessingAst(responseProcessing, { elementNameMapper: mapper });
31
+ *
32
+ * // Use with item-player
33
+ * const definition = createAssessmentItemDefinition({
34
+ * itemXml: xml,
35
+ * elementNameMapper: mapper,
36
+ * attributeNameMapper: attributeMapper
37
+ * });
38
+ * ```
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * // Force specific version
43
+ * const { mapper } = createQtiParser(xml, { version: '3.0' });
44
+ * ```
45
+ */
46
+ export function createQtiParser(xml, options) {
47
+ // Detect or use specified version
48
+ const version = options?.version ?? detectQtiVersion(xml);
49
+ // Use custom mappers if provided, otherwise create appropriate ones
50
+ const mapper = options?.mapper ?? createMapperForVersion(version);
51
+ const attributeMapper = options?.attributeMapper ?? createAttributeMapperForVersion(version);
52
+ return {
53
+ version,
54
+ mapper,
55
+ attributeMapper,
56
+ };
57
+ }
58
+ /**
59
+ * Create element name mapper for a specific QTI version.
60
+ *
61
+ * @param version - QTI version
62
+ * @returns Element name mapper for the version
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * const qti2Mapper = createMapperForVersion('2.2');
67
+ * const qti3Mapper = createMapperForVersion('3.0');
68
+ * ```
69
+ */
70
+ export function createMapperForVersion(version) {
71
+ switch (version) {
72
+ case '2.0':
73
+ case '2.1':
74
+ case '2.2':
75
+ return new Qti2xElementNameMapper();
76
+ case '3.0':
77
+ return new Qti3ElementNameMapper();
78
+ case 'unknown':
79
+ default:
80
+ // Default to QTI 2.x mapper for unknown versions
81
+ // (most existing content is QTI 2.x)
82
+ return new Qti2xElementNameMapper();
83
+ }
84
+ }
85
+ /**
86
+ * Create attribute name mapper for a specific QTI version.
87
+ *
88
+ * @param version - QTI version
89
+ * @returns Attribute name mapper for the version
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * const qti2AttrMapper = createAttributeMapperForVersion('2.2');
94
+ * const qti3AttrMapper = createAttributeMapperForVersion('3.0');
95
+ * ```
96
+ */
97
+ export function createAttributeMapperForVersion(version) {
98
+ switch (version) {
99
+ case '2.0':
100
+ case '2.1':
101
+ case '2.2':
102
+ return new Qti2xAttributeNameMapper();
103
+ case '3.0':
104
+ return new Qti3AttributeNameMapper();
105
+ case 'unknown':
106
+ default:
107
+ // Default to QTI 2.x mapper for unknown versions
108
+ return new Qti2xAttributeNameMapper();
109
+ }
110
+ }
111
+ /**
112
+ * Check if XML content is QTI 3.0.
113
+ *
114
+ * Convenience function for quickly checking if content uses QTI 3.0.
115
+ *
116
+ * @param xml - QTI XML content
117
+ * @returns true if QTI 3.0, false otherwise
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * if (isQti3(xml)) {
122
+ * console.log('This is QTI 3.0 content');
123
+ * }
124
+ * ```
125
+ */
126
+ export function isQti3(xml) {
127
+ return detectQtiVersion(xml) === '3.0';
128
+ }
129
+ /**
130
+ * Check if XML content is QTI 2.x (2.0, 2.1, or 2.2).
131
+ *
132
+ * @param xml - QTI XML content
133
+ * @returns true if QTI 2.x, false otherwise
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * if (isQti2(xml)) {
138
+ * console.log('This is QTI 2.x content');
139
+ * }
140
+ * ```
141
+ */
142
+ export function isQti2(xml) {
143
+ const version = detectQtiVersion(xml);
144
+ return version === '2.0' || version === '2.1' || version === '2.2';
145
+ }
146
+ //# sourceMappingURL=parser-factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parser-factory.js","sourceRoot":"","sources":["../src/parser-factory.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,gBAAgB,EAAmB,MAAM,yCAAyC,CAAC;AAC5F,OAAO,EAAE,sBAAsB,EAAE,MAAM,4CAA4C,CAAC;AACpF,OAAO,EAAE,qBAAqB,EAAE,MAAM,2CAA2C,CAAC;AAElF,OAAO,EAAE,wBAAwB,EAAE,MAAM,8CAA8C,CAAC;AACxF,OAAO,EAAE,uBAAuB,EAAE,MAAM,6CAA6C,CAAC;AA8CtF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,UAAU,eAAe,CAAC,GAAW,EAAE,OAA6B;IACzE,kCAAkC;IAClC,MAAM,OAAO,GAAG,OAAO,EAAE,OAAO,IAAI,gBAAgB,CAAC,GAAG,CAAC,CAAC;IAE1D,oEAAoE;IACpE,MAAM,MAAM,GAAG,OAAO,EAAE,MAAM,IAAI,s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AACpE,CAAC","sourcesContent":["/**\n * Parser factory for creating version-appropriate QTI parsers.\n *\n * This module provides a unified entry point for detecting QTI version\n * and creating appropriate element name mappers.\n */\n\nimport { detectQtiVersion, type QtiVersion } from './version-detection/detectQtiVersion.js';\nimport { Qti2xElementNameMapper } from './element-mapper/Qti2xElementNameMapper.js';\nimport { Qti3ElementNameMapper } from './element-mapper/Qti3ElementNameMapper.js';\nimport type { ElementNameMapper } from './element-mapper/ElementNameMapper.js';\nimport { Qti2xAttributeNameMapper } from './element-mapper/Qti2xAttributeNameMapper.js';\nimport { Qti3AttributeNameMapper } from './element-mapper/Qti3AttributeNameMapper.js';\nimport type { AttributeNameMapper } from './element-mapper/AttributeNameMapper.js';\n\n/**\n * Result of creating a QTI parser.\n */\nexport interface QtiParserResult {\n\t/**\n\t * Detected or specified QTI version\n\t */\n\tversion: QtiVersion;\n\n\t/**\n\t * Element name mapper appropriate for the detected version\n\t */\n\tmapper: ElementNameMapper;\n\n\t/**\n\t * Attribute name mapper appropriate for the detected version\n\t */\n\tattributeMapper: AttributeNameMapper;\n}\n\n/**\n * Options for creating a QTI parser.\n */\nexport interface CreateParserOptions {\n\t/**\n\t * Override automatic version detection.\n\t * If not provided, version will be detected from XML content.\n\t */\n\tversion?: QtiVersion;\n\n\t/**\n\t * Custom element name mapper to use.\n\t * If provided, overrides automatic mapper selection.\n\t */\n\tmapper?: ElementNameMapper;\n\n\t/**\n\t * Custom attribute name mapper to use.\n\t * If provided, overrides automatic mapper selection.\n\t */\n\tattributeMapper?: AttributeNameMapper;\n}\n\n/**\n * Create a QTI parser with appropriate element name mapper for the given XML.\n *\n * This function automatically detects the QTI version and returns the appropriate\n * element name mapper. It can be used as the primary entry point for parsing\n * any QTI content.\n *\n * @param xml - QTI XML content as string\n * @param options - Optional configuration\n * @returns Parser result with version plus element and attribute mappers\n *\n * @example\n * ```typescript\n * // Auto-detect version and create appropriate parser\n * const { version, mapper, attributeMapper } = createQtiParser(xml);\n * console.log(`Detected QTI ${version}`);\n *\n * // Use with qti-processing\n * const program = buildResponseProcessingAst(responseProcessing, { elementNameMapper: mapper });\n *\n * // Use with item-player\n * const definition = createAssessmentItemDefinition({\n * itemXml: xml,\n * elementNameMapper: mapper,\n * attributeNameMapper: attributeMapper\n * });\n * ```\n *\n * @example\n * ```typescript\n * // Force specific version\n * const { mapper } = createQtiParser(xml, { version: '3.0' });\n * ```\n */\nexport function createQtiParser(xml: string, options?: CreateParserOptions): QtiParserResult {\n\t// Detect or use specified version\n\tconst version = options?.version ?? detectQtiVersion(xml);\n\n\t// Use custom mappers if provided, otherwise create appropriate ones\n\tconst mapper = options?.mapper ?? createMapperForVersion(version);\n\tconst attributeMapper = options?.attributeMapper ?? createAttributeMapperForVersion(version);\n\n\treturn {\n\t\tversion,\n\t\tmapper,\n\t\tattributeMapper,\n\t};\n}\n\n/**\n * Create element name mapper for a specific QTI version.\n *\n * @param version - QTI version\n * @returns Element name mapper for the version\n *\n * @example\n * ```typescript\n * const qti2Mapper = createMapperForVersion('2.2');\n * const qti3Mapper = createMapperForVersion('3.0');\n * ```\n */\nexport function createMapperForVersion(version: QtiVersion): ElementNameMapper {\n\tswitch (version) {\n\t\tcase '2.0':\n\t\tcase '2.1':\n\t\tcase '2.2':\n\t\t\treturn new Qti2xElementNameMapper();\n\n\t\tcase '3.0':\n\t\t\treturn new Qti3ElementNameMapper();\n\n\t\tcase 'unknown':\n\t\tdefault:\n\t\t\t// Default to QTI 2.x mapper for unknown versions\n\t\t\t// (most existing content is QTI 2.x)\n\t\t\treturn new Qti2xElementNameMapper();\n\t}\n}\n\n/**\n * Create attribute name mapper for a specific QTI version.\n *\n * @param version - QTI version\n * @returns Attribute name mapper for the version\n *\n * @example\n * ```typescript\n * const qti2AttrMapper = createAttributeMapperForVersion('2.2');\n * const qti3AttrMapper = createAttributeMapperForVersion('3.0');\n * ```\n */\nexport function createAttributeMapperForVersion(version: QtiVersion): AttributeNameMapper {\n\tswitch (version) {\n\t\tcase '2.0':\n\t\tcase '2.1':\n\t\tcase '2.2':\n\t\t\treturn new Qti2xAttributeNameMapper();\n\n\t\tcase '3.0':\n\t\t\treturn new Qti3AttributeNameMapper();\n\n\t\tcase 'unknown':\n\t\tdefault:\n\t\t\t// Default to QTI 2.x mapper for unknown versions\n\t\t\treturn new Qti2xAttributeNameMapper();\n\t}\n}\n\n/**\n * Check if XML content is QTI 3.0.\n *\n * Convenience function for quickly checking if content uses QTI 3.0.\n *\n * @param xml - QTI XML content\n * @returns true if QTI 3.0, false otherwise\n *\n * @example\n * ```typescript\n * if (isQti3(xml)) {\n * console.log('This is QTI 3.0 content');\n * }\n * ```\n */\nexport function isQti3(xml: string): boolean {\n\treturn detectQtiVersion(xml) === '3.0';\n}\n\n/**\n * Check if XML content is QTI 2.x (2.0, 2.1, or 2.2).\n *\n * @param xml - QTI XML content\n * @returns true if QTI 2.x, false otherwise\n *\n * @example\n * ```typescript\n * if (isQti2(xml)) {\n * console.log('This is QTI 2.x content');\n * }\n * ```\n */\nexport function isQti2(xml: string): boolean {\n\tconst version = detectQtiVersion(xml);\n\treturn version === '2.0' || version === '2.1' || version === '2.2';\n}\n"]}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * QTI version identifiers
3
+ */
4
+ export type QtiVersion = '2.0' | '2.1' | '2.2' | '3.0' | 'unknown';
5
+ /**
6
+ * Detect the QTI version from XML content.
7
+ *
8
+ * Detection strategy (in priority order):
9
+ * 1. Check XML namespace URI for version indicators (v2p0, v2p1, v2p2, v3p0)
10
+ * 2. Check root element name (qti-assessment-item => 3.0, assessmentItem => 2.x)
11
+ * 3. Check version attribute on root element
12
+ * 4. Fall back to 'unknown'
13
+ *
14
+ * @param xml - QTI XML content as string
15
+ * @returns Detected QTI version or 'unknown'
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * const xml = `<qti-assessment-item xmlns="http://www.imsglobal.org/xsd/imsqtiasi_v3p0">`;
20
+ * detectQtiVersion(xml); // => '3.0'
21
+ * ```
22
+ */
23
+ export declare function detectQtiVersion(xml: string): QtiVersion;
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Detect the QTI version from XML content.
3
+ *
4
+ * Detection strategy (in priority order):
5
+ * 1. Check XML namespace URI for version indicators (v2p0, v2p1, v2p2, v3p0)
6
+ * 2. Check root element name (qti-assessment-item => 3.0, assessmentItem => 2.x)
7
+ * 3. Check version attribute on root element
8
+ * 4. Fall back to 'unknown'
9
+ *
10
+ * @param xml - QTI XML content as string
11
+ * @returns Detected QTI version or 'unknown'
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const xml = `<qti-assessment-item xmlns="http://www.imsglobal.org/xsd/imsqtiasi_v3p0">`;
16
+ * detectQtiVersion(xml); // => '3.0'
17
+ * ```
18
+ */
19
+ export function detectQtiVersion(xml) {
20
+ try {
21
+ // Check if we're in a browser environment
22
+ if (typeof window !== 'undefined' && typeof window.DOMParser !== 'undefined') {
23
+ const parser = new window.DOMParser();
24
+ const doc = parser.parseFromString(xml, 'text/xml');
25
+ return detectFromDocument(doc);
26
+ }
27
+ // In Node.js environment, fall back to regex detection
28
+ // (xmldom can be optionally imported by consumers if needed)
29
+ return detectFromString(xml);
30
+ }
31
+ catch {
32
+ // If parsing fails, try string-based detection
33
+ return detectFromString(xml);
34
+ }
35
+ }
36
+ /**
37
+ * Detect QTI version from a parsed Document.
38
+ * @param doc - Parsed XML document
39
+ * @returns Detected QTI version
40
+ * @private
41
+ */
42
+ function detectFromDocument(doc) {
43
+ const root = doc.documentElement;
44
+ // Check for parse errors
45
+ if (root.tagName === 'parsererror') {
46
+ return 'unknown';
47
+ }
48
+ // Strategy 1: Check namespace URI
49
+ if (root.namespaceURI) {
50
+ if (root.namespaceURI.includes('v3p0') || root.namespaceURI.includes('imsqtiasi_v3p0')) {
51
+ return '3.0';
52
+ }
53
+ if (root.namespaceURI.includes('v2p2')) {
54
+ return '2.2';
55
+ }
56
+ if (root.namespaceURI.includes('v2p1')) {
57
+ return '2.1';
58
+ }
59
+ if (root.namespaceURI.includes('v2p0')) {
60
+ return '2.0';
61
+ }
62
+ }
63
+ // Strategy 2: Check root element name (any qti- prefixed element indicates QTI 3.0)
64
+ const localName = root.localName || root.tagName;
65
+ if (localName === 'qti-assessment-item' || localName === 'qti-assessment-test' || localName.startsWith('qti-')) {
66
+ return '3.0';
67
+ }
68
+ // Strategy 3: Check version attribute
69
+ if (localName === 'assessmentItem' || localName === 'assessmentTest') {
70
+ const version = root.getAttribute('version');
71
+ if (version) {
72
+ if (version.startsWith('3.'))
73
+ return '3.0';
74
+ if (version.startsWith('2.2'))
75
+ return '2.2';
76
+ if (version.startsWith('2.1'))
77
+ return '2.1';
78
+ if (version.startsWith('2.0'))
79
+ return '2.0';
80
+ }
81
+ // If no version attribute, assume 2.2 (most common)
82
+ return '2.2';
83
+ }
84
+ return 'unknown';
85
+ }
86
+ /**
87
+ * Detect QTI version using string matching (fallback method).
88
+ * @param xml - XML string
89
+ * @returns Detected QTI version
90
+ * @private
91
+ */
92
+ function detectFromString(xml) {
93
+ // Check namespace declarations
94
+ if (xml.includes('imsqtiasi_v3p0') || xml.includes('/v3p0/')) {
95
+ return '3.0';
96
+ }
97
+ if (xml.includes('imsqti_v2p2') || xml.includes('/v2p2/')) {
98
+ return '2.2';
99
+ }
100
+ if (xml.includes('imsqti_v2p1') || xml.includes('/v2p1/')) {
101
+ return '2.1';
102
+ }
103
+ if (xml.includes('imsqti_v2p0') || xml.includes('/v2p0/')) {
104
+ return '2.0';
105
+ }
106
+ // Check root element or any QTI 3.0 element (kebab-case with qti- prefix)
107
+ if (/<qti-assessment-(item|test)[\s>]/.test(xml) || /<qti-[a-z-]+[\s>]/.test(xml)) {
108
+ return '3.0';
109
+ }
110
+ if (/<assessment(Item|Test)[\s>]/.test(xml)) {
111
+ // Try to find version attribute
112
+ const versionMatch = xml.match(/version=["']([^"']+)["']/);
113
+ if (versionMatch) {
114
+ const version = versionMatch[1];
115
+ if (version.startsWith('3.'))
116
+ return '3.0';
117
+ if (version.startsWith('2.2'))
118
+ return '2.2';
119
+ if (version.startsWith('2.1'))
120
+ return '2.1';
121
+ if (version.startsWith('2.0'))
122
+ return '2.0';
123
+ }
124
+ // Assume 2.2 if no version specified
125
+ return '2.2';
126
+ }
127
+ return 'unknown';
128
+ }
129
+ //# sourceMappingURL=detectQtiVersion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"detectQtiVersion.js","sourceRoot":"","sources":["../../src/version-detection/detectQtiVersion.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAAW;IAC3C,IAAI,CAAC;QACJ,0CAA0C;QAC1C,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,MAAM,CAAC,SAAS,KAAK,WAAW,EAAE,CAAC;YAC9E,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;YACtC,MAAM,GAAG,GAAG,MAAM,CAAC,eAAe,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;YACpD,OAAO,kBAAkB,CAAC,GAAG,CAAC,CAAC;QAChC,CAAC;QAED,uDAAuD;QACvD,6DAA6D;QAC7D,OAAO,gBAAgB,CAAC,GAAG,CAAC,CAAC;IAC9B,CAAC;IAAC,MAAM,CAAC;QACR,+CAA+C;QAC/C,OAAO,gBAAgB,CAAC,GAAG,CAAC,CAAC;IAC9B,CAAC;AACF,CAAC;AAED;;;;;GAKG;AACH,SAAS,kBAAkB,CAAC,GAAQ;IACnC,MAAM,IAAI,GAAG,GAAG,CAAC,eAAe,CAAC;IAEjC,yBAAyB;IACzB,IAAI,IAAI,CAAC,OAAO,KAAK,aAAa,EAAE,CAAC;QACpC,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,kCAAkC;IAClC,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;QACvB,IAAI,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,YAAY,CAAC,QAAQ,CA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AAC,UAAU,CAAC,KAAK,CAAC;gBAAE,OAAO,KAAK,CAAC;QAC7C,CAAC;QAED,qCAAqC;QACrC,OAAO,KAAK,CAAC;IACd,CAAC;IAED,OAAO,SAAS,CAAC;AAClB,CAAC","sourcesContent":["/**\n * QTI version identifiers\n */\nexport type QtiVersion = '2.0' | '2.1' | '2.2' | '3.0' | 'unknown';\n\n/**\n * Detect the QTI version from XML content.\n *\n * Detection strategy (in priority order):\n * 1. Check XML namespace URI for version indicators (v2p0, v2p1, v2p2, v3p0)\n * 2. Check root element name (qti-assessment-item => 3.0, assessmentItem => 2.x)\n * 3. Check version attribute on root element\n * 4. Fall back to 'unknown'\n *\n * @param xml - QTI XML content as string\n * @returns Detected QTI version or 'unknown'\n *\n * @example\n * ```typescript\n * const xml = `<qti-assessment-item xmlns=\"http://www.imsglobal.org/xsd/imsqtiasi_v3p0\">`;\n * detectQtiVersion(xml); // => '3.0'\n * ```\n */\nexport function detectQtiVersion(xml: string): QtiVersion {\n\ttry {\n\t\t// Check if we're in a browser environment\n\t\tif (typeof window !== 'undefined' && typeof window.DOMParser !== 'undefined') {\n\t\t\tconst parser = new window.DOMParser();\n\t\t\tconst doc = parser.parseFromString(xml, 'text/xml');\n\t\t\treturn detectFromDocument(doc);\n\t\t}\n\n\t\t// In Node.js environment, fall back to regex detection\n\t\t// (xmldom can be optionally imported by consumers if needed)\n\t\treturn detectFromString(xml);\n\t} catch {\n\t\t// If parsing fails, try string-based detection\n\t\treturn detectFromString(xml);\n\t}\n}\n\n/**\n * Detect QTI version from a parsed Document.\n * @param doc - Parsed XML document\n * @returns Detected QTI version\n * @private\n */\nfunction detectFromDocument(doc: any): QtiVersion {\n\tconst root = doc.documentElement;\n\n\t// Check for parse errors\n\tif (root.tagName === 'parsererror') {\n\t\treturn 'unknown';\n\t}\n\n\t// Strategy 1: Check namespace URI\n\tif (root.namespaceURI) {\n\t\tif (root.namespaceURI.includes('v3p0') || root.namespaceURI.includes('imsqtiasi_v3p0')) {\n\t\t\treturn '3.0';\n\t\t}\n\t\tif (root.namespaceURI.includes('v2p2')) {\n\t\t\treturn '2.2';\n\t\t}\n\t\tif (root.namespaceURI.includes('v2p1')) {\n\t\t\treturn '2.1';\n\t\t}\n\t\tif (root.namespaceURI.includes('v2p0')) {\n\t\t\treturn '2.0';\n\t\t}\n\t}\n\n\t// Strategy 2: Check root element name (any qti- prefixed element indicates QTI 3.0)\n\tconst localName = root.localName || root.tagName;\n\tif (localName === 'qti-assessment-item' || localName === 'qti-assessment-test' || localName.startsWith('qti-')) {\n\t\treturn '3.0';\n\t}\n\n\t// Strategy 3: Check version attribute\n\tif (localName === 'assessmentItem' || localName === 'assessmentTest') {\n\t\tconst version = root.getAttribute('version');\n\t\tif (version) {\n\t\t\tif (version.startsWith('3.')) return '3.0';\n\t\t\tif (version.startsWith('2.2')) return '2.2';\n\t\t\tif (version.startsWith('2.1')) return '2.1';\n\t\t\tif (version.startsWith('2.0')) return '2.0';\n\t\t}\n\n\t\t// If no version attribute, assume 2.2 (most common)\n\t\treturn '2.2';\n\t}\n\n\treturn 'unknown';\n}\n\n/**\n * Detect QTI version using string matching (fallback method).\n * @param xml - XML string\n * @returns Detected QTI version\n * @private\n */\nfunction detectFromString(xml: string): QtiVersion {\n\t// Check namespace declarations\n\tif (xml.includes('imsqtiasi_v3p0') || xml.includes('/v3p0/')) {\n\t\treturn '3.0';\n\t}\n\tif (xml.includes('imsqti_v2p2') || xml.includes('/v2p2/')) {\n\t\treturn '2.2';\n\t}\n\tif (xml.includes('imsqti_v2p1') || xml.includes('/v2p1/')) {\n\t\treturn '2.1';\n\t}\n\tif (xml.includes('imsqti_v2p0') || xml.includes('/v2p0/')) {\n\t\treturn '2.0';\n\t}\n\n\t// Check root element or any QTI 3.0 element (kebab-case with qti- prefix)\n\tif (/<qti-assessment-(item|test)[\\s>]/.test(xml) || /<qti-[a-z-]+[\\s>]/.test(xml)) {\n\t\treturn '3.0';\n\t}\n\n\tif (/<assessment(Item|Test)[\\s>]/.test(xml)) {\n\t\t// Try to find version attribute\n\t\tconst versionMatch = xml.match(/version=[\"']([^\"']+)[\"']/);\n\t\tif (versionMatch) {\n\t\t\tconst version = versionMatch[1];\n\t\t\tif (version.startsWith('3.')) return '3.0';\n\t\t\tif (version.startsWith('2.2')) return '2.2';\n\t\t\tif (version.startsWith('2.1')) return '2.1';\n\t\t\tif (version.startsWith('2.0')) return '2.0';\n\t\t}\n\n\t\t// Assume 2.2 if no version specified\n\t\treturn '2.2';\n\t}\n\n\treturn 'unknown';\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export { detectQtiVersion } from './detectQtiVersion.js';
2
+ export type { QtiVersion } from './detectQtiVersion.js';
@@ -0,0 +1,2 @@
1
+ export { detectQtiVersion } from './detectQtiVersion.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/version-detection/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC","sourcesContent":["export { detectQtiVersion } from './detectQtiVersion.js';\nexport type { QtiVersion } from './detectQtiVersion.js';\n"]}
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Attribute name handling for QTI 2.x and QTI 3.0.
3
+ *
4
+ * QTI 2.x uses camelCase: identifier, baseType, responseIdentifier
5
+ * QTI 3.0 uses kebab-case: identifier, base-type, response-identifier
6
+ *
7
+ * This module provides utilities to access attributes regardless of version.
8
+ */
9
+ /**
10
+ * Convert camelCase to kebab-case.
11
+ * @param name - camelCase attribute name
12
+ * @returns kebab-case attribute name
13
+ *
14
+ * @example
15
+ * toKebabCase('baseType') // => 'base-type'
16
+ * toKebabCase('responseIdentifier') // => 'response-identifier'
17
+ */
18
+ export declare function toKebabCase(name: string): string;
19
+ /**
20
+ * Convert kebab-case to camelCase.
21
+ * @param name - kebab-case attribute name
22
+ * @returns camelCase attribute name
23
+ *
24
+ * @example
25
+ * toCamelCase('base-type') // => 'baseType'
26
+ * toCamelCase('response-identifier') // => 'responseIdentifier'
27
+ */
28
+ export declare function toCamelCase(name: string): string;
29
+ /**
30
+ * Get attribute value supporting both QTI 2.x (camelCase) and QTI 3.0 (kebab-case).
31
+ *
32
+ * This function tries to retrieve the attribute in both formats:
33
+ * 1. First tries the exact name as provided
34
+ * 2. If not found and name is camelCase, tries kebab-case version
35
+ * 3. If not found and name is kebab-case, tries camelCase version
36
+ *
37
+ * @param element - Element to get attribute from
38
+ * @param name - Attribute name (can be camelCase or kebab-case)
39
+ * @returns Attribute value or null if not found
40
+ *
41
+ * @example
42
+ * // Works with QTI 2.x:
43
+ * getAttribute(el, 'baseType') // => 'float' (from baseType="float")
44
+ *
45
+ * // Works with QTI 3.0:
46
+ * getAttribute(el, 'baseType') // => 'float' (from base-type="float")
47
+ * getAttribute(el, 'base-type') // => 'float' (from base-type="float")
48
+ */
49
+ export declare function getAttribute(element: Element, name: string): string | null;
50
+ /**
51
+ * Check if element has attribute (supports both camelCase and kebab-case).
52
+ *
53
+ * @param element - Element to check
54
+ * @param name - Attribute name
55
+ * @returns true if attribute exists
56
+ */
57
+ export declare function hasAttribute(element: Element, name: string): boolean;
58
+ /**
59
+ * Get attribute as number.
60
+ *
61
+ * @param element - Element to get attribute from
62
+ * @param name - Attribute name
63
+ * @param defaultValue - Default value if attribute is missing or invalid
64
+ * @returns Attribute value as number
65
+ */
66
+ export declare function getNumberAttribute(element: Element, name: string, defaultValue: number): number;
67
+ /**
68
+ * Get attribute as boolean.
69
+ *
70
+ * QTI uses 'true'/'false' or '1'/'0' for boolean attributes.
71
+ *
72
+ * @param element - Element to get attribute from
73
+ * @param name - Attribute name
74
+ * @param defaultValue - Default value if attribute is missing
75
+ * @returns Attribute value as boolean
76
+ */
77
+ export declare function getBooleanAttribute(element: Element, name: string, defaultValue?: boolean): boolean;
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Attribute name handling for QTI 2.x and QTI 3.0.
3
+ *
4
+ * QTI 2.x uses camelCase: identifier, baseType, responseIdentifier
5
+ * QTI 3.0 uses kebab-case: identifier, base-type, response-identifier
6
+ *
7
+ * This module provides utilities to access attributes regardless of version.
8
+ */
9
+ /**
10
+ * Convert camelCase to kebab-case.
11
+ * @param name - camelCase attribute name
12
+ * @returns kebab-case attribute name
13
+ *
14
+ * @example
15
+ * toKebabCase('baseType') // => 'base-type'
16
+ * toKebabCase('responseIdentifier') // => 'response-identifier'
17
+ */
18
+ export function toKebabCase(name) {
19
+ return name.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
20
+ }
21
+ /**
22
+ * Convert kebab-case to camelCase.
23
+ * @param name - kebab-case attribute name
24
+ * @returns camelCase attribute name
25
+ *
26
+ * @example
27
+ * toCamelCase('base-type') // => 'baseType'
28
+ * toCamelCase('response-identifier') // => 'responseIdentifier'
29
+ */
30
+ export function toCamelCase(name) {
31
+ return name.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
32
+ }
33
+ /**
34
+ * Get attribute value supporting both QTI 2.x (camelCase) and QTI 3.0 (kebab-case).
35
+ *
36
+ * This function tries to retrieve the attribute in both formats:
37
+ * 1. First tries the exact name as provided
38
+ * 2. If not found and name is camelCase, tries kebab-case version
39
+ * 3. If not found and name is kebab-case, tries camelCase version
40
+ *
41
+ * @param element - Element to get attribute from
42
+ * @param name - Attribute name (can be camelCase or kebab-case)
43
+ * @returns Attribute value or null if not found
44
+ *
45
+ * @example
46
+ * // Works with QTI 2.x:
47
+ * getAttribute(el, 'baseType') // => 'float' (from baseType="float")
48
+ *
49
+ * // Works with QTI 3.0:
50
+ * getAttribute(el, 'baseType') // => 'float' (from base-type="float")
51
+ * getAttribute(el, 'base-type') // => 'float' (from base-type="float")
52
+ */
53
+ export function getAttribute(element, name) {
54
+ // Try exact name first
55
+ let value = element.getAttribute(name);
56
+ if (value !== null && value !== '')
57
+ return value;
58
+ // If name contains uppercase (camelCase), try kebab-case
59
+ if (/[A-Z]/.test(name)) {
60
+ const kebab = toKebabCase(name);
61
+ value = element.getAttribute(kebab);
62
+ if (value !== null && value !== '')
63
+ return value;
64
+ }
65
+ // If name contains hyphens (kebab-case), try camelCase
66
+ if (name.includes('-')) {
67
+ const camel = toCamelCase(name);
68
+ value = element.getAttribute(camel);
69
+ if (value !== null && value !== '')
70
+ return value;
71
+ }
72
+ return null;
73
+ }
74
+ /**
75
+ * Check if element has attribute (supports both camelCase and kebab-case).
76
+ *
77
+ * @param element - Element to check
78
+ * @param name - Attribute name
79
+ * @returns true if attribute exists
80
+ */
81
+ export function hasAttribute(element, name) {
82
+ return getAttribute(element, name) !== null;
83
+ }
84
+ /**
85
+ * Get attribute as number.
86
+ *
87
+ * @param element - Element to get attribute from
88
+ * @param name - Attribute name
89
+ * @param defaultValue - Default value if attribute is missing or invalid
90
+ * @returns Attribute value as number
91
+ */
92
+ export function getNumberAttribute(element, name, defaultValue) {
93
+ const value = getAttribute(element, name);
94
+ if (value === null)
95
+ return defaultValue;
96
+ const num = Number(value);
97
+ return Number.isFinite(num) ? num : defaultValue;
98
+ }
99
+ /**
100
+ * Get attribute as boolean.
101
+ *
102
+ * QTI uses 'true'/'false' or '1'/'0' for boolean attributes.
103
+ *
104
+ * @param element - Element to get attribute from
105
+ * @param name - Attribute name
106
+ * @param defaultValue - Default value if attribute is missing
107
+ * @returns Attribute value as boolean
108
+ */
109
+ export function getBooleanAttribute(element, name, defaultValue = false) {
110
+ const value = getAttribute(element, name);
111
+ if (value === null)
112
+ return defaultValue;
113
+ const normalized = value.trim().toLowerCase();
114
+ if (normalized === 'true' || normalized === '1')
115
+ return true;
116
+ if (normalized === 'false' || normalized === '0')
117
+ return false;
118
+ return defaultValue;
119
+ }
120
+ //# sourceMappingURL=attributes.js.map