@pie-qti/qti-common 0.1.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.
- package/README.md +356 -0
- package/dist/element-mapper/ElementNameMapper.d.ts +67 -0
- package/dist/element-mapper/ElementNameMapper.d.ts.map +1 -0
- package/dist/element-mapper/ElementNameMapper.js +2 -0
- package/dist/element-mapper/ElementNameMapper.js.map +1 -0
- package/dist/element-mapper/Qti2xElementNameMapper.d.ts +38 -0
- package/dist/element-mapper/Qti2xElementNameMapper.d.ts.map +1 -0
- package/dist/element-mapper/Qti2xElementNameMapper.js +47 -0
- package/dist/element-mapper/Qti2xElementNameMapper.js.map +1 -0
- package/dist/element-mapper/Qti3ElementNameMapper.d.ts +50 -0
- package/dist/element-mapper/Qti3ElementNameMapper.d.ts.map +1 -0
- package/dist/element-mapper/Qti3ElementNameMapper.js +224 -0
- package/dist/element-mapper/Qti3ElementNameMapper.js.map +1 -0
- package/dist/element-mapper/index.d.ts +4 -0
- package/dist/element-mapper/index.d.ts.map +1 -0
- package/dist/element-mapper/index.js +3 -0
- package/dist/element-mapper/index.js.map +1 -0
- package/dist/element-mapper/qti3-element-mappings.d.ts +448 -0
- package/dist/element-mapper/qti3-element-mappings.d.ts.map +1 -0
- package/dist/element-mapper/qti3-element-mappings.js +320 -0
- package/dist/element-mapper/qti3-element-mappings.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/parser-factory.d.ts +111 -0
- package/dist/parser-factory.d.ts.map +1 -0
- package/dist/parser-factory.js +120 -0
- package/dist/parser-factory.js.map +1 -0
- package/dist/version-detection/detectQtiVersion.d.ts +24 -0
- package/dist/version-detection/detectQtiVersion.d.ts.map +1 -0
- package/dist/version-detection/detectQtiVersion.js +129 -0
- package/dist/version-detection/detectQtiVersion.js.map +1 -0
- package/dist/version-detection/index.d.ts +3 -0
- package/dist/version-detection/index.d.ts.map +1 -0
- package/dist/version-detection/index.js +2 -0
- package/dist/version-detection/index.js.map +1 -0
- package/dist/xml/attributes.d.ts +78 -0
- package/dist/xml/attributes.d.ts.map +1 -0
- package/dist/xml/attributes.js +120 -0
- package/dist/xml/attributes.js.map +1 -0
- package/dist/xml/index.d.ts +2 -0
- package/dist/xml/index.d.ts.map +1 -0
- package/dist/xml/index.js +2 -0
- package/dist/xml/index.js.map +1 -0
- package/package.json +36 -0
|
@@ -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
|
|
64
|
+
const localName = root.localName || root.tagName;
|
|
65
|
+
if (localName === 'qti-assessment-item' || localName === 'qti-assessment-test') {
|
|
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
|
|
107
|
+
if (/<qti-assessment-(item|test)[\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,CAAC,gBAAgB,CAAC,EAAE,CAAC;YACxF,OAAO,KAAK,CAAC;QACd,CAAC;QACD,IAAI,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,MAAM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ts","sourceRoot":"","sources":["../../src/version-detection/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,YAAY,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC"}
|
|
@@ -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"}
|
|
@@ -0,0 +1,78 @@
|
|
|
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;
|
|
78
|
+
//# sourceMappingURL=attributes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attributes.d.ts","sourceRoot":"","sources":["../../src/xml/attributes.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAoB1E;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAEpE;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,MAAM,CAK/F;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,YAAY,UAAQ,GAAG,OAAO,CAOjG"}
|
|
@@ -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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attributes.js","sourceRoot":"","sources":["../../src/xml/attributes.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;;;;GAQG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY;IACvC,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;AACvE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY;IACvC,OAAO,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC;AACvE,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,YAAY,CAAC,OAAgB,EAAE,IAAY;IAC1D,uBAAuB;IACvB,IAAI,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IACvC,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,EAAE;QAAE,OAAO,KAAK,CAAC;IAEjD,yDAAyD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O,EAAE,IAAI,CAAC,CAAC;IAC1C,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,YAAY,CAAC;IACxC,MAAM,UAAU,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IAC9C,IAAI,UAAU,KAAK,MAAM,IAAI,UAAU,KAAK,GAAG;QAAE,OAAO,IAAI,CAAC;IAC7D,IAAI,UAAU,KAAK,OAAO,IAAI,UAAU,KAAK,GAAG;QAAE,OAAO,KAAK,CAAC;IAC/D,OAAO,YAAY,CAAC;AACrB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/xml/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,YAAY,EACZ,mBAAmB,EACnB,kBAAkB,EAClB,YAAY,EACZ,WAAW,EACX,WAAW,GACX,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/xml/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,YAAY,EACZ,mBAAmB,EACnB,kBAAkB,EAClB,YAAY,EACZ,WAAW,EACX,WAAW,GACX,MAAM,iBAAiB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pie-qti/qti-common",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared utilities and abstractions for QTI 2.x and QTI 3.0 support",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./element-mapper": {
|
|
14
|
+
"types": "./dist/element-mapper/index.d.ts",
|
|
15
|
+
"import": "./dist/element-mapper/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./version-detection": {
|
|
18
|
+
"types": "./dist/version-detection/index.d.ts",
|
|
19
|
+
"import": "./dist/version-detection/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": ["dist"],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc",
|
|
25
|
+
"dev": "tsc --watch",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"test": "bun test"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/bun": "^1.3.5",
|
|
32
|
+
"@xmldom/xmldom": "^0.8.11",
|
|
33
|
+
"typescript": "^5.9.3"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT"
|
|
36
|
+
}
|