@pie-qti/item-player 0.1.8 → 0.1.10

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 (199) hide show
  1. package/README.md +17 -0
  2. package/dist/catalog/applyGlossaryTriggers.d.ts +13 -2
  3. package/dist/catalog/applyGlossaryTriggers.d.ts.map +1 -1
  4. package/dist/catalog/glossaryAction.d.ts +4 -2
  5. package/dist/catalog/glossaryAction.d.ts.map +1 -1
  6. package/dist/components/utils/index.d.ts +1 -2
  7. package/dist/components/utils/index.d.ts.map +1 -1
  8. package/dist/components/utils/index.js +1 -2
  9. package/dist/core/ComponentRegistry.d.ts +10 -1
  10. package/dist/core/ComponentRegistry.d.ts.map +1 -1
  11. package/dist/core/ComponentRegistry.js +42 -1
  12. package/dist/core/Player.d.ts +10 -2
  13. package/dist/core/Player.d.ts.map +1 -1
  14. package/dist/core/Player.js +54 -46
  15. package/dist/document/AssessmentItemDocument.d.ts +1 -1
  16. package/dist/element.d.ts +15 -0
  17. package/dist/element.js +1921 -1785
  18. package/dist/element.js.map +1 -1
  19. package/dist/extraction/ExtractionRegistry.d.ts +1 -1
  20. package/dist/extraction/createContext.d.ts +1 -1
  21. package/dist/extraction/index.d.ts +2 -2
  22. package/dist/extraction/index.d.ts.map +1 -1
  23. package/dist/extraction/index.js +1 -1
  24. package/dist/extraction/interactionExtractionPipeline.d.ts +1 -1
  25. package/dist/extraction/interactionSecurity.d.ts +1 -1
  26. package/dist/extraction/types.d.ts +1 -1
  27. package/dist/presentation/itemPresentationPlan.d.ts +54 -0
  28. package/dist/presentation/itemPresentationPlan.d.ts.map +1 -0
  29. package/dist/presentation/itemPresentationPlan.js +98 -0
  30. package/dist/types/index.d.ts +17 -2
  31. package/dist/types/index.d.ts.map +1 -1
  32. package/dist/types/index.js +0 -3
  33. package/dist/web-components/QTIInteractionBase.d.ts +1 -1
  34. package/dist/web-components/QTIInteractionElement.d.ts +1 -1
  35. package/package.json +10 -12
  36. package/src/__tests__/catalog.test.ts +0 -267
  37. package/src/__tests__/fixtures/pci/echo-pci.js +0 -49
  38. package/src/__tests__/pci.test.ts +0 -353
  39. package/src/__tests__/qti3-attribute-extraction.test.ts +0 -174
  40. package/src/__tests__/qti3-debug.test.ts +0 -140
  41. package/src/__tests__/qti3-extraction.test.ts +0 -309
  42. package/src/catalog/applyGlossaryTriggers.ts +0 -372
  43. package/src/catalog/catalogExtractor.ts +0 -130
  44. package/src/catalog/catalogLookup.ts +0 -53
  45. package/src/catalog/glossaryAction.ts +0 -38
  46. package/src/catalog/types.ts +0 -37
  47. package/src/components/ItemBody.svelte +0 -363
  48. package/src/components/ItemPlayer.svelte +0 -163
  49. package/src/components/ModalFeedbackDisplay.svelte +0 -97
  50. package/src/components/actions/typesetAction.ts +0 -74
  51. package/src/components/index.ts +0 -22
  52. package/src/components/utils/assignProps.ts +0 -2
  53. package/src/components/utils/feedbackUtils.ts +0 -140
  54. package/src/components/utils/index.ts +0 -13
  55. package/src/components/utils/stimulusRender.ts +0 -93
  56. package/src/components/utils/stylesheetRender.ts +0 -58
  57. package/src/core/ComponentRegistry.ts +0 -248
  58. package/src/core/Player.ts +0 -1618
  59. package/src/core/Plugin.ts +0 -123
  60. package/src/core/PluginManager.ts +0 -182
  61. package/src/core/constants.ts +0 -117
  62. package/src/core/declarations.ts +0 -171
  63. package/src/core/parsingLimits.ts +0 -62
  64. package/src/core/random.ts +0 -24
  65. package/src/core/rolePolicy.ts +0 -34
  66. package/src/core/sanitizer.ts +0 -305
  67. package/src/core/trustedTypes.ts +0 -63
  68. package/src/core/urlPolicy.ts +0 -98
  69. package/src/document/AssessmentItemDocument.ts +0 -247
  70. package/src/element.ts +0 -128
  71. package/src/extensions/MathLiveEditor.ts +0 -313
  72. package/src/extraction/ExtractionRegistry.ts +0 -335
  73. package/src/extraction/createContext.ts +0 -52
  74. package/src/extraction/extractors/associateExtractor.ts +0 -1
  75. package/src/extraction/extractors/choiceExtractor.ts +0 -1
  76. package/src/extraction/extractors/conformance-qti3-advanced.test.ts +0 -239
  77. package/src/extraction/extractors/conformance-qti3-basic.test.ts +0 -82
  78. package/src/extraction/extractors/conformance.test.ts +0 -208
  79. package/src/extraction/extractors/customExtractor.ts +0 -1
  80. package/src/extraction/extractors/drawingExtractor.ts +0 -1
  81. package/src/extraction/extractors/endAttemptExtractor.ts +0 -1
  82. package/src/extraction/extractors/extendedTextExtractor.ts +0 -1
  83. package/src/extraction/extractors/gapMatchExtractor.ts +0 -1
  84. package/src/extraction/extractors/graphicAssociateExtractor.ts +0 -1
  85. package/src/extraction/extractors/graphicGapMatchExtractor.ts +0 -1
  86. package/src/extraction/extractors/graphicOrderExtractor.ts +0 -1
  87. package/src/extraction/extractors/hotspotExtractor.ts +0 -1
  88. package/src/extraction/extractors/hottextExtractor.ts +0 -1
  89. package/src/extraction/extractors/index.ts +0 -9
  90. package/src/extraction/extractors/inlineChoiceExtractor.ts +0 -1
  91. package/src/extraction/extractors/matchExtractor.ts +0 -1
  92. package/src/extraction/extractors/mediaExtractor.ts +0 -1
  93. package/src/extraction/extractors/orderExtractor.ts +0 -1
  94. package/src/extraction/extractors/portableCustomExtractor.ts +0 -1
  95. package/src/extraction/extractors/positionObjectExtractor.ts +0 -1
  96. package/src/extraction/extractors/selectPointExtractor.ts +0 -1
  97. package/src/extraction/extractors/sliderExtractor.ts +0 -1
  98. package/src/extraction/extractors/textEntryExtractor.ts +0 -1
  99. package/src/extraction/extractors/uploadExtractor.ts +0 -1
  100. package/src/extraction/index.ts +0 -108
  101. package/src/extraction/interactionExtractionPipeline.ts +0 -70
  102. package/src/extraction/interactionSecurity.ts +0 -60
  103. package/src/extraction/types.ts +0 -246
  104. package/src/extraction/utils.ts +0 -243
  105. package/src/iframe/IFramePlayerHost.ts +0 -265
  106. package/src/iframe/index.ts +0 -11
  107. package/src/iframe/protocol.ts +0 -137
  108. package/src/index.ts +0 -101
  109. package/src/interactions/associate/extractor.ts +0 -133
  110. package/src/interactions/associate/index.ts +0 -2
  111. package/src/interactions/associate/types.ts +0 -1
  112. package/src/interactions/choice/extractor.ts +0 -121
  113. package/src/interactions/choice/index.ts +0 -2
  114. package/src/interactions/choice/types.ts +0 -1
  115. package/src/interactions/custom/extractor.ts +0 -72
  116. package/src/interactions/custom/index.ts +0 -2
  117. package/src/interactions/custom/types.ts +0 -1
  118. package/src/interactions/drawing/extractor.ts +0 -113
  119. package/src/interactions/drawing/index.ts +0 -2
  120. package/src/interactions/drawing/types.ts +0 -1
  121. package/src/interactions/end-attempt/extractor.ts +0 -74
  122. package/src/interactions/end-attempt/index.ts +0 -2
  123. package/src/interactions/end-attempt/types.ts +0 -1
  124. package/src/interactions/extended-text/extractor.ts +0 -92
  125. package/src/interactions/extended-text/index.ts +0 -2
  126. package/src/interactions/extended-text/types.ts +0 -1
  127. package/src/interactions/gap-match/extractor.ts +0 -193
  128. package/src/interactions/gap-match/index.ts +0 -2
  129. package/src/interactions/gap-match/types.ts +0 -1
  130. package/src/interactions/graphic-associate/extractor.ts +0 -192
  131. package/src/interactions/graphic-associate/index.ts +0 -2
  132. package/src/interactions/graphic-associate/types.ts +0 -1
  133. package/src/interactions/graphic-gap-match/extractor.ts +0 -257
  134. package/src/interactions/graphic-gap-match/index.ts +0 -2
  135. package/src/interactions/graphic-gap-match/types.ts +0 -1
  136. package/src/interactions/graphic-order/extractor.ts +0 -162
  137. package/src/interactions/graphic-order/index.ts +0 -2
  138. package/src/interactions/graphic-order/types.ts +0 -1
  139. package/src/interactions/hotspot/extractor.ts +0 -208
  140. package/src/interactions/hotspot/index.ts +0 -2
  141. package/src/interactions/hotspot/types.ts +0 -1
  142. package/src/interactions/hottext/extractor.ts +0 -163
  143. package/src/interactions/hottext/index.ts +0 -2
  144. package/src/interactions/hottext/types.ts +0 -1
  145. package/src/interactions/index.ts +0 -52
  146. package/src/interactions/inline/index.ts +0 -1
  147. package/src/interactions/inline/render-plan.ts +0 -105
  148. package/src/interactions/inline-choice/InlineChoice.svelte +0 -57
  149. package/src/interactions/inline-choice/extractor.ts +0 -105
  150. package/src/interactions/inline-choice/index.ts +0 -2
  151. package/src/interactions/inline-choice/types.ts +0 -1
  152. package/src/interactions/match/extractor.ts +0 -187
  153. package/src/interactions/match/index.ts +0 -2
  154. package/src/interactions/match/types.ts +0 -1
  155. package/src/interactions/media/extractor.ts +0 -168
  156. package/src/interactions/media/index.ts +0 -2
  157. package/src/interactions/media/types.ts +0 -1
  158. package/src/interactions/modules.ts +0 -111
  159. package/src/interactions/order/extractor.ts +0 -120
  160. package/src/interactions/order/index.ts +0 -2
  161. package/src/interactions/order/types.ts +0 -1
  162. package/src/interactions/portable-custom/extractor.ts +0 -122
  163. package/src/interactions/portable-custom/index.ts +0 -2
  164. package/src/interactions/portable-custom/types.ts +0 -1
  165. package/src/interactions/position-object/extractor.ts +0 -243
  166. package/src/interactions/position-object/index.ts +0 -2
  167. package/src/interactions/position-object/types.ts +0 -1
  168. package/src/interactions/select-point/extractor.ts +0 -143
  169. package/src/interactions/select-point/index.ts +0 -2
  170. package/src/interactions/select-point/types.ts +0 -1
  171. package/src/interactions/shared/index.ts +0 -1
  172. package/src/interactions/shared/types.ts +0 -430
  173. package/src/interactions/slider/extractor.ts +0 -102
  174. package/src/interactions/slider/index.ts +0 -2
  175. package/src/interactions/slider/types.ts +0 -1
  176. package/src/interactions/text-entry/InlineTextEntry.svelte +0 -75
  177. package/src/interactions/text-entry/extractor.ts +0 -79
  178. package/src/interactions/text-entry/index.ts +0 -2
  179. package/src/interactions/text-entry/types.ts +0 -1
  180. package/src/interactions/upload/extractor.ts +0 -81
  181. package/src/interactions/upload/index.ts +0 -2
  182. package/src/interactions/upload/types.ts +0 -1
  183. package/src/pci/PciHost.ts +0 -138
  184. package/src/pci/types.ts +0 -70
  185. package/src/pnp/applyPnp.ts +0 -29
  186. package/src/pnp/parsePnpXml.ts +0 -215
  187. package/src/pnp/types.ts +0 -75
  188. package/src/server.ts +0 -14
  189. package/src/types/index.ts +0 -388
  190. package/src/types/interactions.ts +0 -8
  191. package/src/types/responseValidation.ts +0 -29
  192. package/src/types/xmldom.d.ts +0 -12
  193. package/src/utils/PerformanceMonitor.ts +0 -335
  194. package/src/utils/drawingUtils.ts +0 -73
  195. package/src/utils/responseUtils.ts +0 -51
  196. package/src/utils/touchDragHelper.ts +0 -1
  197. package/src/web-components/QTIInteractionBase.ts +0 -303
  198. package/src/web-components/QTIInteractionElement.ts +0 -125
  199. package/src/web-components/index.ts +0 -22
@@ -1,246 +0,0 @@
1
- /**
2
- * Core types for the plugin-based element extraction system
3
- *
4
- * Provides interfaces for extracting data from QTI XML elements with
5
- * priority-based dispatch, validation, and type safety.
6
- */
7
-
8
- import type { PlayerConfig } from '../types/index.js';
9
- import type { QTIElement } from '../types/interactions.js';
10
-
11
- /**
12
- * Context provided to extractors during extraction
13
- * Contains DOM reference, utilities, and metadata
14
- */
15
- export interface ExtractionContext {
16
- /** The element being extracted */
17
- element: QTIElement;
18
-
19
- /** Response identifier for this interaction */
20
- responseId: string;
21
-
22
- /** Root DOM element for document-wide queries */
23
- dom: QTIElement;
24
-
25
- /** Variable declarations from responseDeclaration/outcomeDeclaration */
26
- declarations: Map<string, VariableDeclaration>;
27
-
28
- /** Utility functions for DOM manipulation and extraction */
29
- utils: ExtractionUtils;
30
-
31
- /** Player configuration (role, typesetting, etc.) */
32
- config: PlayerConfig;
33
- }
34
-
35
- /**
36
- * Variable declaration from QTI responseDeclaration/outcomeDeclaration
37
- */
38
- export interface VariableDeclaration {
39
- identifier: string;
40
- cardinality: 'single' | 'multiple' | 'ordered' | 'record';
41
- baseType?: string;
42
- }
43
-
44
- /**
45
- * Utility functions available to extractors
46
- * Provides safe, consistent DOM manipulation and content extraction
47
- */
48
- export interface ExtractionUtils {
49
- /**
50
- * Get direct children with a specific tag name
51
- * @example utils.getChildrenByTag(element, 'simpleChoice')
52
- */
53
- getChildrenByTag(element: QTIElement, tagName: string): QTIElement[];
54
-
55
- /**
56
- * Query all descendants matching a selector
57
- * @example utils.querySelectorAll(element, 'choiceInteraction simpleChoice')
58
- */
59
- querySelectorAll(element: QTIElement, selector: string): QTIElement[];
60
-
61
- /**
62
- * Query first descendant matching a selector
63
- * @example utils.querySelector(element, 'prompt')
64
- */
65
- querySelector(element: QTIElement, selector: string): QTIElement | null;
66
-
67
- /**
68
- * Check if element has a child with specific tag (fast boolean check)
69
- * @example utils.hasChildWithTag(element, 'ratingChoice')
70
- */
71
- hasChildWithTag(element: QTIElement, tagName: string): boolean;
72
-
73
- /**
74
- * Get sanitized HTML content from element
75
- * @example utils.getHtmlContent(element) // '<p>Question <math>...</math></p>'
76
- */
77
- getHtmlContent(element: QTIElement): string;
78
-
79
- /**
80
- * Get plain text content (strips HTML tags)
81
- * @example utils.getTextContent(element) // 'Question text'
82
- */
83
- getTextContent(element: QTIElement): string;
84
-
85
- /**
86
- * Get attribute value with optional default
87
- * @example utils.getAttribute(element, 'identifier', '')
88
- */
89
- getAttribute(element: QTIElement, name: string, defaultValue?: string): string;
90
-
91
- /**
92
- * Get boolean attribute value with optional default
93
- * @example utils.getBooleanAttribute(element, 'shuffle', false) // true
94
- */
95
- getBooleanAttribute(element: QTIElement, name: string, defaultValue?: boolean): boolean;
96
-
97
- /**
98
- * Get numeric attribute value with optional default
99
- * @example utils.getNumberAttribute(element, 'maxChoices', 1) // 1
100
- */
101
- getNumberAttribute(element: QTIElement, name: string, defaultValue?: number): number;
102
-
103
- /**
104
- * Get CSS classes from element
105
- * @example utils.getClasses(element) // ['rating-interaction', 'custom']
106
- */
107
- getClasses(element: QTIElement): string[];
108
-
109
- /**
110
- * Extract prompt element text from an interaction
111
- * @example utils.getPrompt(element) // '<p>Select the correct answer</p>'
112
- */
113
- getPrompt(element: QTIElement): string | null;
114
- }
115
-
116
- /**
117
- * Result of validation
118
- */
119
- export interface ValidationResult {
120
- /** Whether validation passed */
121
- valid: boolean;
122
-
123
- /** Error messages if validation failed */
124
- errors?: string[];
125
-
126
- /** Warning messages (non-fatal) */
127
- warnings?: string[];
128
- }
129
-
130
- /**
131
- * Error thrown during extraction
132
- */
133
- export class ExtractionError extends Error {
134
- constructor(
135
- message: string,
136
- public readonly extractorId: string,
137
- public readonly elementType: string,
138
- public readonly cause?: Error
139
- ) {
140
- super(message);
141
- this.name = 'ExtractionError';
142
- }
143
- }
144
-
145
- /**
146
- * Result of extraction operation
147
- */
148
- export type ExtractionResult<TData> =
149
- | { success: true; data: TData; warnings?: string[] }
150
- | { success: false; error: ExtractionError };
151
-
152
- /**
153
- * Element extractor with priority-based dispatch
154
- *
155
- * @template TData - The extracted data type (e.g., ChoiceData, TextEntryData)
156
- * @template TContext - The extraction context type (defaults to ExtractionContext)
157
- */
158
- export interface ElementExtractor<
159
- TData = unknown,
160
- TContext extends ExtractionContext = ExtractionContext,
161
- > {
162
- /**
163
- * Unique identifier for this extractor
164
- * Convention: 'namespace:element-type' (e.g., 'qti:choice', 'renaissance:rating-choice')
165
- */
166
- id: string;
167
-
168
- /**
169
- * Human-readable name for debugging/error messages
170
- * @example 'QTI Standard Choice Interaction'
171
- */
172
- name: string;
173
-
174
- /**
175
- * Priority for extractor evaluation (higher = checked first)
176
- * - 1000+: Critical system extractors (error handlers, etc.)
177
- * - 500-999: Vendor-specific extractors (Renaissance, custom content)
178
- * - 100-499: Third-party plugin extractors
179
- * - 1-99: Standard QTI extractors with variations
180
- * - 0: Fallback/default extractors
181
- */
182
- priority: number;
183
-
184
- /**
185
- * Element types this extractor handles (for type-based indexing)
186
- * Used for O(M) lookup instead of O(N) scanning
187
- * @example ['choiceInteraction', 'inlineChoiceInteraction']
188
- */
189
- elementTypes: string[];
190
-
191
- /**
192
- * Optional description for debugging/error messages
193
- * @example 'Handles Renaissance rating choice interactions with star/face icons'
194
- */
195
- description?: string;
196
-
197
- /**
198
- * Predicate to determine if this extractor can handle the element
199
- * Evaluated in priority order (highest first)
200
- *
201
- * IMPORTANT: Must be FAST - use quick checks first
202
- * @example
203
- * canHandle(element, context) {
204
- * // Fast checks first
205
- * if (!context.utils.hasChildWithTag(element, 'ratingChoice')) return false;
206
- * // Expensive checks only if fast checks pass
207
- * return validateRatingStructure(element);
208
- * }
209
- */
210
- canHandle(element: QTIElement, context: TContext): boolean;
211
-
212
- /**
213
- * Extract data from the element
214
- * Called only if canHandle() returns true
215
- *
216
- * @throws ExtractionError if extraction fails
217
- * @returns Extracted data structure
218
- */
219
- extract(element: QTIElement, context: TContext): TData;
220
-
221
- /**
222
- * Validate extracted data (optional)
223
- * Called after extract() succeeds
224
- *
225
- * @returns Validation result with errors/warnings
226
- */
227
- validate?(data: TData): ValidationResult;
228
- }
229
-
230
- /**
231
- * Type guard for successful extraction result
232
- */
233
- export function isSuccessResult<TData>(
234
- result: ExtractionResult<TData>
235
- ): result is { success: true; data: TData; warnings?: string[] } {
236
- return result.success === true;
237
- }
238
-
239
- /**
240
- * Type guard for failed extraction result
241
- */
242
- export function isErrorResult<TData>(
243
- result: ExtractionResult<TData>
244
- ): result is { success: false; error: ExtractionError } {
245
- return result.success === false;
246
- }
@@ -1,243 +0,0 @@
1
- /**
2
- * Extraction utilities implementation
3
- *
4
- * Provides DOM manipulation and content extraction helpers for extractors
5
- */
6
-
7
- import type { ElementNameMapper, AttributeNameMapper } from '@pie-qti/qti-common';
8
- import { Qti2xElementNameMapper, Qti2xAttributeNameMapper } from '@pie-qti/qti-common';
9
- import { sanitizeTextContent } from '../core/sanitizer.js';
10
- import type { PlayerSecurityConfig } from '../types/index.js';
11
- import type { QTIElement } from '../types/interactions.js';
12
- import type { ExtractionUtils } from './types.js';
13
-
14
- /**
15
- * Extract text content from QTI elements
16
- */
17
- function getTextContent(element: QTIElement | null | undefined): string {
18
- return element?.textContent?.trim() || '';
19
- }
20
-
21
- /**
22
- * Extract HTML content from QTI elements (preserves MathML and other markup)
23
- * Content is sanitized to prevent XSS attacks
24
- */
25
- function getHtmlContent(element: QTIElement | null | undefined): string {
26
- const html = element?.innerHTML?.trim() || '';
27
- return sanitizeTextContent(html);
28
- }
29
-
30
- /**
31
- * Get children by tag name from QTI elements
32
- * Note: This version does NOT handle QTI version mapping - use the one in ExtractionUtils instead
33
- */
34
- function getChildrenByTag(
35
- element: QTIElement | null | undefined,
36
- tagName: string
37
- ): QTIElement[] {
38
- return (element?.childNodes?.filter((n) => n.rawTagName === tagName) as QTIElement[]) || [];
39
- }
40
-
41
- /**
42
- * Get children by tag name with QTI version awareness
43
- */
44
- function getChildrenByTagWithMapper(
45
- element: QTIElement | null | undefined,
46
- tagName: string,
47
- mapper: ElementNameMapper
48
- ): QTIElement[] {
49
- if (!element?.childNodes) return [];
50
- return element.childNodes.filter((n) =>
51
- matchesTagName((n as QTIElement).rawTagName, tagName, mapper)
52
- ) as QTIElement[];
53
- }
54
-
55
- /**
56
- * Extract a boolean attribute from a QTI element
57
- * Supports both QTI 2.x (camelCase) and QTI 3.0 (kebab-case) attributes
58
- */
59
- function getBooleanAttribute(
60
- element: QTIElement,
61
- name: string,
62
- defaultValue = false,
63
- attributeMapper?: AttributeNameMapper
64
- ): boolean {
65
- let value = element.getAttribute(name);
66
-
67
- // If not found and we have a mapper, try the native format (QTI 3.0 kebab-case)
68
- if ((value === null || value === undefined) && attributeMapper) {
69
- const nativeName = attributeMapper.toNative(name);
70
- value = element.getAttribute(nativeName);
71
- }
72
-
73
- if (value === null || value === undefined) return defaultValue;
74
- return value === 'true';
75
- }
76
-
77
- /**
78
- * Extract a number attribute from a QTI element
79
- * Supports both QTI 2.x (camelCase) and QTI 3.0 (kebab-case) attributes
80
- */
81
- function getNumberAttribute(
82
- element: QTIElement,
83
- name: string,
84
- defaultValue: number,
85
- attributeMapper?: AttributeNameMapper
86
- ): number {
87
- let value = element.getAttribute(name);
88
-
89
- // If not found and we have a mapper, try the native format (QTI 3.0 kebab-case)
90
- if ((value === null || value === undefined) && attributeMapper) {
91
- const nativeName = attributeMapper.toNative(name);
92
- value = element.getAttribute(nativeName);
93
- }
94
-
95
- return value ? Number(value) : defaultValue;
96
- }
97
-
98
- /**
99
- * Extract a string attribute from a QTI element
100
- * Supports both QTI 2.x (camelCase) and QTI 3.0 (kebab-case) attributes
101
- */
102
- function getStringAttribute(
103
- element: QTIElement,
104
- name: string,
105
- defaultValue = '',
106
- attributeMapper?: AttributeNameMapper
107
- ): string {
108
- let value = element.getAttribute(name);
109
-
110
- // If not found and we have a mapper, try the native format (QTI 3.0 kebab-case)
111
- if ((value === null || value === undefined) && attributeMapper) {
112
- const nativeName = attributeMapper.toNative(name);
113
- value = element.getAttribute(nativeName);
114
- }
115
-
116
- return value || defaultValue;
117
- }
118
-
119
- /**
120
- * Extract CSS classes from an element
121
- */
122
- function getClasses(element: QTIElement): string[] {
123
- const classAttr = element.getAttribute('class');
124
- return classAttr ? classAttr.split(/\s+/).filter(Boolean) : [];
125
- }
126
-
127
- /**
128
- * Helper to check if an element's tag name matches a given QTI element name.
129
- * Handles both QTI 2.x and QTI 3.0 element naming conventions.
130
- *
131
- * @param rawTagName - The element's raw tag name from the HTML
132
- * @param expectedTagName - The tag name to check (in QTI 2.x format, e.g., 'simpleChoice')
133
- * @param mapper - Optional element name mapper for QTI version handling
134
- * @returns true if the tag name matches
135
- */
136
- function matchesTagName(
137
- rawTagName: string | undefined,
138
- expectedTagName: string,
139
- mapper: ElementNameMapper
140
- ): boolean {
141
- if (!rawTagName) return false;
142
-
143
- // Convert expected tag name to canonical form (lowercase)
144
- const canonical = expectedTagName.toLowerCase();
145
-
146
- // Get the native form for this QTI version
147
- const native = mapper.toNative(canonical);
148
-
149
- // Check if raw tag name matches the native form (case-insensitive)
150
- return rawTagName.toLowerCase() === native.toLowerCase();
151
- }
152
-
153
- /**
154
- * Create extraction utilities instance
155
- * Provides standard helpers for extracting data from QTI elements.
156
- * Automatically handles both QTI 2.x and QTI 3.0 element and attribute naming conventions.
157
- */
158
- export function createExtractionUtils(
159
- security?: PlayerSecurityConfig,
160
- mapper?: ElementNameMapper,
161
- attributeMapper?: AttributeNameMapper
162
- ): ExtractionUtils {
163
- // Default to QTI 2.x mappers (primary supported QTI format)
164
- const elementMapper = mapper || new Qti2xElementNameMapper();
165
- const attrMapper = attributeMapper || new Qti2xAttributeNameMapper();
166
- return {
167
- getChildrenByTag(element: QTIElement, tagName: string): QTIElement[] {
168
- return getChildrenByTagWithMapper(element, tagName, elementMapper);
169
- },
170
-
171
- querySelectorAll(element: QTIElement, selector: string): QTIElement[] {
172
- // Parse simple selectors like "choiceInteraction simpleChoice"
173
- const parts = selector.split(/\s+/);
174
- let results: QTIElement[] = [element];
175
-
176
- for (const part of parts) {
177
- const nextResults: QTIElement[] = [];
178
- for (const current of results) {
179
- if (matchesTagName(current.rawTagName, part, elementMapper)) {
180
- nextResults.push(current);
181
- }
182
- if (current.childNodes) {
183
- for (const child of current.childNodes) {
184
- if (typeof child !== 'string' && matchesTagName((child as QTIElement).rawTagName, part, elementMapper)) {
185
- nextResults.push(child as QTIElement);
186
- }
187
- }
188
- }
189
- }
190
- results = nextResults;
191
- }
192
-
193
- return results;
194
- },
195
-
196
- querySelector(element: QTIElement, selector: string): QTIElement | null {
197
- const results = this.querySelectorAll(element, selector);
198
- return results.length > 0 ? results[0] : null;
199
- },
200
-
201
- hasChildWithTag(element: QTIElement, tagName: string): boolean {
202
- // Fast boolean check without allocation
203
- if (!element.childNodes) return false;
204
-
205
- for (const child of element.childNodes) {
206
- if (typeof child !== 'string' && matchesTagName((child as QTIElement).rawTagName, tagName, elementMapper)) {
207
- return true;
208
- }
209
- }
210
- return false;
211
- },
212
-
213
- getHtmlContent(element: QTIElement): string {
214
- const html = element?.innerHTML?.trim() || '';
215
- return sanitizeTextContent(html, { security });
216
- },
217
-
218
- getTextContent(element: QTIElement): string {
219
- return getTextContent(element);
220
- },
221
-
222
- getAttribute(element: QTIElement, name: string, defaultValue = ''): string {
223
- return getStringAttribute(element, name, defaultValue, attrMapper);
224
- },
225
-
226
- getBooleanAttribute(element: QTIElement, name: string, defaultValue = false): boolean {
227
- return getBooleanAttribute(element, name, defaultValue, attrMapper);
228
- },
229
-
230
- getNumberAttribute(element: QTIElement, name: string, defaultValue = 0): number {
231
- return getNumberAttribute(element, name, defaultValue, attrMapper);
232
- },
233
-
234
- getPrompt(element: QTIElement): string | null {
235
- const promptElement = getChildrenByTagWithMapper(element, 'prompt', elementMapper)[0];
236
- return promptElement ? getHtmlContent(promptElement) : null;
237
- },
238
-
239
- getClasses(element: QTIElement): string[] {
240
- return getClasses(element);
241
- },
242
- };
243
- }