@pie-qti/item-player 0.1.19 → 0.1.20

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 (257) hide show
  1. package/README.md +137 -427
  2. package/dist/catalog/applyGlossaryTriggers.d.ts +2 -1
  3. package/dist/catalog/applyGlossaryTriggers.js +4 -2
  4. package/dist/catalog/applyGlossaryTriggers.js.map +1 -1
  5. package/dist/components/utils/stimulusRender.d.ts +1 -2
  6. package/dist/components/utils/stimulusRender.js +2 -5
  7. package/dist/components/utils/stimulusRender.js.map +1 -1
  8. package/dist/core/AssessmentItemDefinition.d.ts +69 -0
  9. package/dist/core/AssessmentItemDefinition.js +149 -0
  10. package/dist/core/AssessmentItemDefinition.js.map +1 -0
  11. package/dist/core/ComponentRegistry.d.ts +11 -5
  12. package/dist/core/ComponentRegistry.js +44 -17
  13. package/dist/core/ComponentRegistry.js.map +1 -1
  14. package/dist/core/ItemSession.d.ts +58 -0
  15. package/dist/core/ItemSession.js +286 -0
  16. package/dist/core/ItemSession.js.map +1 -0
  17. package/dist/core/Player.d.ts +9 -8
  18. package/dist/core/Player.js +182 -163
  19. package/dist/core/Player.js.map +1 -1
  20. package/dist/document/AssessmentItemDocument.d.ts +32 -10
  21. package/dist/document/AssessmentItemDocument.js +80 -23
  22. package/dist/document/AssessmentItemDocument.js.map +1 -1
  23. package/dist/{element-class-BEpwHdcP.js → element-class-CILJ4Ls7.js} +3572 -2783
  24. package/dist/element-class-CILJ4Ls7.js.map +1 -0
  25. package/dist/element-class.d.ts +17 -13
  26. package/dist/element-class.js +1 -1
  27. package/dist/element.js +1 -1
  28. package/dist/extraction/ExtractionRegistry.d.ts +34 -11
  29. package/dist/extraction/ExtractionRegistry.js +122 -19
  30. package/dist/extraction/ExtractionRegistry.js.map +1 -1
  31. package/dist/extraction/createContext.d.ts +5 -6
  32. package/dist/extraction/createContext.js +2 -2
  33. package/dist/extraction/createContext.js.map +1 -1
  34. package/dist/extraction/deliveryTypes.d.ts +26 -0
  35. package/dist/extraction/deliveryTypes.js +7 -0
  36. package/dist/extraction/deliveryTypes.js.map +1 -0
  37. package/dist/extraction/index.d.ts +3 -3
  38. package/dist/extraction/index.js +1 -2
  39. package/dist/extraction/index.js.map +1 -1
  40. package/dist/extraction/interactionExtractionPipeline.d.ts +4 -4
  41. package/dist/extraction/interactionExtractionPipeline.js +27 -10
  42. package/dist/extraction/interactionExtractionPipeline.js.map +1 -1
  43. package/dist/extraction/interactionSecurity.d.ts +11 -2
  44. package/dist/extraction/interactionSecurity.js +105 -40
  45. package/dist/extraction/interactionSecurity.js.map +1 -1
  46. package/dist/extraction/types.d.ts +44 -7
  47. package/dist/extraction/types.js.map +1 -1
  48. package/dist/extraction/utils.js +4 -16
  49. package/dist/extraction/utils.js.map +1 -1
  50. package/dist/index.d.ts +9 -8
  51. package/dist/index.js +5 -7
  52. package/dist/index.js.map +1 -1
  53. package/dist/interactions/associate/extractor.js +1 -1
  54. package/dist/interactions/associate/extractor.js.map +1 -1
  55. package/dist/interactions/drawing/extractor.js +5 -4
  56. package/dist/interactions/drawing/extractor.js.map +1 -1
  57. package/dist/interactions/end-attempt/extractor.js +2 -2
  58. package/dist/interactions/end-attempt/extractor.js.map +1 -1
  59. package/dist/interactions/gap-match/extractor.js +4 -3
  60. package/dist/interactions/gap-match/extractor.js.map +1 -1
  61. package/dist/interactions/graphic-associate/extractor.js +5 -4
  62. package/dist/interactions/graphic-associate/extractor.js.map +1 -1
  63. package/dist/interactions/graphic-gap-match/extractor.js +20 -17
  64. package/dist/interactions/graphic-gap-match/extractor.js.map +1 -1
  65. package/dist/interactions/graphic-order/extractor.js +5 -4
  66. package/dist/interactions/graphic-order/extractor.js.map +1 -1
  67. package/dist/interactions/hotspot/extractor.js +11 -10
  68. package/dist/interactions/hotspot/extractor.js.map +1 -1
  69. package/dist/interactions/inline/render-plan.d.ts +3 -5
  70. package/dist/interactions/inline/render-plan.js.map +1 -1
  71. package/dist/interactions/media/extractor.js +2 -2
  72. package/dist/interactions/media/extractor.js.map +1 -1
  73. package/dist/interactions/modules.d.ts +11 -91
  74. package/dist/interactions/modules.js +185 -45
  75. package/dist/interactions/modules.js.map +1 -1
  76. package/dist/interactions/portable-custom/extractor.d.ts +1 -1
  77. package/dist/interactions/portable-custom/extractor.js +1 -1
  78. package/dist/interactions/portable-custom/extractor.js.map +1 -1
  79. package/dist/interactions/position-object/extractor.js +3 -2
  80. package/dist/interactions/position-object/extractor.js.map +1 -1
  81. package/dist/interactions/select-point/extractor.js +7 -6
  82. package/dist/interactions/select-point/extractor.js.map +1 -1
  83. package/dist/interactions/shared/types.d.ts +59 -70
  84. package/dist/interactions/shared/types.js.map +1 -1
  85. package/dist/interactions/slider/extractor.js +2 -2
  86. package/dist/interactions/slider/extractor.js.map +1 -1
  87. package/dist/interactions/upload/extractor.js +2 -2
  88. package/dist/interactions/upload/extractor.js.map +1 -1
  89. package/dist/pci/types.d.ts +2 -3
  90. package/dist/pci/types.js.map +1 -1
  91. package/dist/presentation/itemPresentationPlan.d.ts +71 -38
  92. package/dist/presentation/itemPresentationPlan.js +78 -34
  93. package/dist/presentation/itemPresentationPlan.js.map +1 -1
  94. package/dist/security/index.d.ts +1 -1
  95. package/dist/security/index.js +1 -1
  96. package/dist/security/index.js.map +1 -1
  97. package/dist/security/styleValues.d.ts +20 -0
  98. package/dist/security/styleValues.js +49 -0
  99. package/dist/security/styleValues.js.map +1 -0
  100. package/dist/server.d.ts +188 -9
  101. package/dist/server.js +32 -7
  102. package/dist/server.js.map +1 -1
  103. package/dist/types/index.d.ts +1 -110
  104. package/dist/types/index.js.map +1 -1
  105. package/dist/web-components/QTIInteractionBase.d.ts +2 -2
  106. package/dist/web-components/QTIInteractionBase.js.map +1 -1
  107. package/dist/web-components/QTIInteractionElement.d.ts +3 -3
  108. package/dist/web-components/QTIInteractionElement.js.map +1 -1
  109. package/package.json +11 -11
  110. package/dist/core/Plugin.d.ts +0 -104
  111. package/dist/core/Plugin.js +0 -8
  112. package/dist/core/Plugin.js.map +0 -1
  113. package/dist/core/PluginManager.d.ts +0 -55
  114. package/dist/core/PluginManager.js +0 -148
  115. package/dist/core/PluginManager.js.map +0 -1
  116. package/dist/element-class-BEpwHdcP.js.map +0 -1
  117. package/dist/interactions/associate/index.d.ts +0 -2
  118. package/dist/interactions/associate/index.js +0 -2
  119. package/dist/interactions/associate/index.js.map +0 -1
  120. package/dist/interactions/associate/types.d.ts +0 -1
  121. package/dist/interactions/associate/types.js +0 -2
  122. package/dist/interactions/associate/types.js.map +0 -1
  123. package/dist/interactions/choice/index.d.ts +0 -2
  124. package/dist/interactions/choice/index.js +0 -2
  125. package/dist/interactions/choice/index.js.map +0 -1
  126. package/dist/interactions/choice/types.d.ts +0 -1
  127. package/dist/interactions/choice/types.js +0 -2
  128. package/dist/interactions/choice/types.js.map +0 -1
  129. package/dist/interactions/custom/index.d.ts +0 -2
  130. package/dist/interactions/custom/index.js +0 -2
  131. package/dist/interactions/custom/index.js.map +0 -1
  132. package/dist/interactions/custom/types.d.ts +0 -1
  133. package/dist/interactions/custom/types.js +0 -2
  134. package/dist/interactions/custom/types.js.map +0 -1
  135. package/dist/interactions/drawing/index.d.ts +0 -2
  136. package/dist/interactions/drawing/index.js +0 -2
  137. package/dist/interactions/drawing/index.js.map +0 -1
  138. package/dist/interactions/drawing/types.d.ts +0 -1
  139. package/dist/interactions/drawing/types.js +0 -2
  140. package/dist/interactions/drawing/types.js.map +0 -1
  141. package/dist/interactions/end-attempt/index.d.ts +0 -2
  142. package/dist/interactions/end-attempt/index.js +0 -2
  143. package/dist/interactions/end-attempt/index.js.map +0 -1
  144. package/dist/interactions/end-attempt/types.d.ts +0 -1
  145. package/dist/interactions/end-attempt/types.js +0 -2
  146. package/dist/interactions/end-attempt/types.js.map +0 -1
  147. package/dist/interactions/extended-text/index.d.ts +0 -3
  148. package/dist/interactions/extended-text/index.js +0 -3
  149. package/dist/interactions/extended-text/index.js.map +0 -1
  150. package/dist/interactions/extended-text/types.d.ts +0 -1
  151. package/dist/interactions/extended-text/types.js +0 -2
  152. package/dist/interactions/extended-text/types.js.map +0 -1
  153. package/dist/interactions/gap-match/index.d.ts +0 -2
  154. package/dist/interactions/gap-match/index.js +0 -2
  155. package/dist/interactions/gap-match/index.js.map +0 -1
  156. package/dist/interactions/gap-match/types.d.ts +0 -1
  157. package/dist/interactions/gap-match/types.js +0 -2
  158. package/dist/interactions/gap-match/types.js.map +0 -1
  159. package/dist/interactions/graphic-associate/index.d.ts +0 -2
  160. package/dist/interactions/graphic-associate/index.js +0 -2
  161. package/dist/interactions/graphic-associate/index.js.map +0 -1
  162. package/dist/interactions/graphic-associate/types.d.ts +0 -1
  163. package/dist/interactions/graphic-associate/types.js +0 -2
  164. package/dist/interactions/graphic-associate/types.js.map +0 -1
  165. package/dist/interactions/graphic-gap-match/index.d.ts +0 -2
  166. package/dist/interactions/graphic-gap-match/index.js +0 -2
  167. package/dist/interactions/graphic-gap-match/index.js.map +0 -1
  168. package/dist/interactions/graphic-gap-match/types.d.ts +0 -1
  169. package/dist/interactions/graphic-gap-match/types.js +0 -2
  170. package/dist/interactions/graphic-gap-match/types.js.map +0 -1
  171. package/dist/interactions/graphic-order/index.d.ts +0 -2
  172. package/dist/interactions/graphic-order/index.js +0 -2
  173. package/dist/interactions/graphic-order/index.js.map +0 -1
  174. package/dist/interactions/graphic-order/types.d.ts +0 -1
  175. package/dist/interactions/graphic-order/types.js +0 -2
  176. package/dist/interactions/graphic-order/types.js.map +0 -1
  177. package/dist/interactions/hotspot/index.d.ts +0 -2
  178. package/dist/interactions/hotspot/index.js +0 -2
  179. package/dist/interactions/hotspot/index.js.map +0 -1
  180. package/dist/interactions/hotspot/types.d.ts +0 -1
  181. package/dist/interactions/hotspot/types.js +0 -2
  182. package/dist/interactions/hotspot/types.js.map +0 -1
  183. package/dist/interactions/hottext/index.d.ts +0 -2
  184. package/dist/interactions/hottext/index.js +0 -2
  185. package/dist/interactions/hottext/index.js.map +0 -1
  186. package/dist/interactions/hottext/types.d.ts +0 -1
  187. package/dist/interactions/hottext/types.js +0 -2
  188. package/dist/interactions/hottext/types.js.map +0 -1
  189. package/dist/interactions/index.d.ts +0 -32
  190. package/dist/interactions/index.js +0 -31
  191. package/dist/interactions/index.js.map +0 -1
  192. package/dist/interactions/inline/index.d.ts +0 -1
  193. package/dist/interactions/inline/index.js +0 -2
  194. package/dist/interactions/inline/index.js.map +0 -1
  195. package/dist/interactions/inline-choice/index.d.ts +0 -2
  196. package/dist/interactions/inline-choice/index.js +0 -2
  197. package/dist/interactions/inline-choice/index.js.map +0 -1
  198. package/dist/interactions/inline-choice/types.d.ts +0 -1
  199. package/dist/interactions/inline-choice/types.js +0 -2
  200. package/dist/interactions/inline-choice/types.js.map +0 -1
  201. package/dist/interactions/match/index.d.ts +0 -2
  202. package/dist/interactions/match/index.js +0 -2
  203. package/dist/interactions/match/index.js.map +0 -1
  204. package/dist/interactions/match/types.d.ts +0 -1
  205. package/dist/interactions/match/types.js +0 -2
  206. package/dist/interactions/match/types.js.map +0 -1
  207. package/dist/interactions/media/index.d.ts +0 -2
  208. package/dist/interactions/media/index.js +0 -2
  209. package/dist/interactions/media/index.js.map +0 -1
  210. package/dist/interactions/media/types.d.ts +0 -1
  211. package/dist/interactions/media/types.js +0 -2
  212. package/dist/interactions/media/types.js.map +0 -1
  213. package/dist/interactions/order/index.d.ts +0 -2
  214. package/dist/interactions/order/index.js +0 -2
  215. package/dist/interactions/order/index.js.map +0 -1
  216. package/dist/interactions/order/types.d.ts +0 -1
  217. package/dist/interactions/order/types.js +0 -2
  218. package/dist/interactions/order/types.js.map +0 -1
  219. package/dist/interactions/portable-custom/index.d.ts +0 -2
  220. package/dist/interactions/portable-custom/index.js +0 -2
  221. package/dist/interactions/portable-custom/index.js.map +0 -1
  222. package/dist/interactions/portable-custom/types.d.ts +0 -1
  223. package/dist/interactions/portable-custom/types.js +0 -2
  224. package/dist/interactions/portable-custom/types.js.map +0 -1
  225. package/dist/interactions/position-object/index.d.ts +0 -2
  226. package/dist/interactions/position-object/index.js +0 -2
  227. package/dist/interactions/position-object/index.js.map +0 -1
  228. package/dist/interactions/position-object/types.d.ts +0 -1
  229. package/dist/interactions/position-object/types.js +0 -2
  230. package/dist/interactions/position-object/types.js.map +0 -1
  231. package/dist/interactions/select-point/index.d.ts +0 -2
  232. package/dist/interactions/select-point/index.js +0 -2
  233. package/dist/interactions/select-point/index.js.map +0 -1
  234. package/dist/interactions/select-point/types.d.ts +0 -1
  235. package/dist/interactions/select-point/types.js +0 -2
  236. package/dist/interactions/select-point/types.js.map +0 -1
  237. package/dist/interactions/shared/index.d.ts +0 -1
  238. package/dist/interactions/shared/index.js +0 -2
  239. package/dist/interactions/shared/index.js.map +0 -1
  240. package/dist/interactions/slider/index.d.ts +0 -2
  241. package/dist/interactions/slider/index.js +0 -2
  242. package/dist/interactions/slider/index.js.map +0 -1
  243. package/dist/interactions/slider/types.d.ts +0 -1
  244. package/dist/interactions/slider/types.js +0 -2
  245. package/dist/interactions/slider/types.js.map +0 -1
  246. package/dist/interactions/text-entry/index.d.ts +0 -2
  247. package/dist/interactions/text-entry/index.js +0 -2
  248. package/dist/interactions/text-entry/index.js.map +0 -1
  249. package/dist/interactions/text-entry/types.d.ts +0 -1
  250. package/dist/interactions/text-entry/types.js +0 -2
  251. package/dist/interactions/text-entry/types.js.map +0 -1
  252. package/dist/interactions/upload/index.d.ts +0 -2
  253. package/dist/interactions/upload/index.js +0 -2
  254. package/dist/interactions/upload/index.js.map +0 -1
  255. package/dist/interactions/upload/types.d.ts +0 -1
  256. package/dist/interactions/upload/types.js +0 -2
  257. package/dist/interactions/upload/types.js.map +0 -1
@@ -1,9 +1,9 @@
1
- import { buildOutcomeProcessingAst, buildResponseProcessingAst, buildTemplateProcessingAst, childElements, coerceBaseValue, DeclarationContext, evalExpr, execProgram, findDescendants, findFirstDescendant, getAttr, OperatorRegistry, qtiNull, qtiValue, toBoolean, toNumber, toStringValue, } from '@pie-qti/qti-processing';
1
+ import { buildOutcomeProcessingAst, buildResponseProcessingAst, buildTemplateProcessingAst, coerceBaseValue, DeclarationContext, evalExpr, execProgram, findDescendants, findFirstDescendant, getAttr, OperatorRegistry, qtiNull, qtiValue, toBoolean, toNumber, toStringValue, } from '@pie-qti/qti-processing';
2
2
  import { Qti2xElementNameMapper, Qti2xAttributeNameMapper, Qti3ElementNameMapper, Qti3AttributeNameMapper, detectQtiVersion, } from '@pie-qti/qti-common';
3
3
  import { parseAssessmentItemDocument } from '../document/AssessmentItemDocument.js';
4
- import { createExtractionRegistry } from '../extraction/ExtractionRegistry.js';
4
+ import { createExtractionRegistry, registerFrameworkDeliveryFields, } from '../extraction/ExtractionRegistry.js';
5
5
  import { extractInteractionData } from '../extraction/interactionExtractionPipeline.js';
6
- import { getStandardInteractionExtractors } from '../interactions/modules.js';
6
+ import { getStandardInteractionModules } from '../interactions/modules.js';
7
7
  import { createComponentRegistry } from './ComponentRegistry.js';
8
8
  import { createSeededRng } from './random.js';
9
9
  import { sanitizeHtml } from './sanitizer.js';
@@ -14,8 +14,25 @@ import { extractCatalog, extractCatalogFromItemXml, mergeCatalogs } from '../cat
14
14
  import { getCatalogEntry } from '../catalog/catalogLookup.js';
15
15
  import { PciHost } from '../pci/PciHost.js';
16
16
  import { createExtendedTextNumericRecord, EXTENDED_TEXT_RECORD_FIELD_TYPES, } from '../interactions/extended-text/response.js';
17
+ /** @internal Used by AssessmentItemDefinition; deliberately absent from the package root API. */
18
+ export function preparePlayerDefinitionInternal(config) {
19
+ const compiler = new Player(config, undefined, { compileOnly: true });
20
+ try {
21
+ return compiler.capturePreparedDefinition();
22
+ }
23
+ finally {
24
+ compiler.destroy();
25
+ }
26
+ }
27
+ /** @internal Used by ItemSession; deliberately absent from the package root API. */
28
+ export function createPlayerFromPreparedInternal(config, prepared, skipTemplateProcessing = false) {
29
+ return new Player(config, prepared, {
30
+ skipTemplateProcessing,
31
+ });
32
+ }
17
33
  export class Player {
18
- constructor(config) {
34
+ constructor(config, prepared, construction = {}) {
35
+ this.interactionDataCache = null;
19
36
  this.responseProcessingProgram = null;
20
37
  this.templateProcessingProgram = null;
21
38
  this.outcomeProcessingProgram = null;
@@ -31,101 +48,100 @@ export class Player {
31
48
  this.sessionGuid = createSessionGuid();
32
49
  this.sessionStartedAt = Date.now();
33
50
  this.rng = config.rng ?? (typeof config.seed === 'number' ? createSeededRng(config.seed) : Math.random);
34
- // Auto-detect QTI version and create appropriate mappers if not provided
35
- if (!config.elementNameMapper) {
51
+ // Resolve QTI-version mappers without mutating caller-owned configuration.
52
+ if (prepared) {
53
+ this.mapper = prepared.mapper;
54
+ }
55
+ else if (!config.elementNameMapper) {
36
56
  const version = detectQtiVersion(this.itemXml);
37
57
  if (version === '3.0') {
38
58
  this.mapper = new Qti3ElementNameMapper();
39
- // Set in config so it's available to extraction utils
40
- config.elementNameMapper = this.mapper;
41
- // Also set attribute mapper if not provided
42
- if (!config.attributeNameMapper) {
43
- config.attributeNameMapper = new Qti3AttributeNameMapper();
44
- }
45
59
  }
46
60
  else {
47
61
  this.mapper = new Qti2xElementNameMapper();
48
- config.elementNameMapper = this.mapper;
49
- if (!config.attributeNameMapper) {
50
- config.attributeNameMapper = new Qti2xAttributeNameMapper();
51
- }
52
62
  }
53
63
  }
54
64
  else {
55
65
  this.mapper = config.elementNameMapper;
56
- if (!config.attributeNameMapper) {
57
- config.attributeNameMapper =
58
- this.mapper.version === '3.0'
59
- ? new Qti3AttributeNameMapper()
60
- : new Qti2xAttributeNameMapper();
61
- }
62
66
  }
63
- this.itemDocument = parseAssessmentItemDocument({
64
- itemXml: this.itemXml,
67
+ const attributeMapper = prepared?.attributeMapper ??
68
+ config.attributeNameMapper ??
69
+ (this.mapper.version === '3.0'
70
+ ? new Qti3AttributeNameMapper()
71
+ : new Qti2xAttributeNameMapper());
72
+ this.config = {
73
+ ...config,
65
74
  elementNameMapper: this.mapper,
66
- attributeNameMapper: config.attributeNameMapper,
67
- security: this.config.security,
68
- });
69
- this.assessmentItem = this.itemDocument.getAssessmentItem();
70
- this.decls = this.buildDeclarations();
71
- this.ensureBuiltinDeclarations(this.decls);
75
+ attributeNameMapper: attributeMapper,
76
+ };
77
+ if (prepared) {
78
+ this.itemDocument = prepared.itemDocument;
79
+ this.decls = cloneDeclarationMap(prepared.declarations);
80
+ }
81
+ else {
82
+ this.itemDocument = parseAssessmentItemDocument({
83
+ itemXml: this.itemXml,
84
+ elementNameMapper: this.mapper,
85
+ attributeNameMapper: attributeMapper,
86
+ security: this.config.security,
87
+ });
88
+ this.decls = this.buildDeclarations();
89
+ this.ensureBuiltinDeclarations(this.decls);
90
+ }
91
+ this.sourceDeclarations = cloneDeclarationMap(this.decls);
72
92
  this.ctx = new DeclarationContext(this.decls);
73
93
  this.ops = new OperatorRegistry();
74
- // Restore session state (outcomes + responses) before running templateProcessing.
75
- if (config.sessionState) {
76
- this.applySessionState(config.sessionState);
77
- }
78
94
  // Extraction + rendering registries (for interaction rendering)
79
95
  // Pass the element name mapper to the extraction registry for QTI version handling
80
- this.extractionRegistry = config.extractionRegistry ?? createExtractionRegistry(this.mapper);
81
- this.componentRegistry = config.componentRegistry ?? createComponentRegistry();
96
+ this.extractionRegistry =
97
+ this.config.extractionRegistry ??
98
+ createExtractionRegistry(this.mapper);
99
+ this.componentRegistry =
100
+ this.config.componentRegistry ??
101
+ createComponentRegistry();
82
102
  // I18n provider (defaults to a simple fallback if not provided)
83
- this.i18nProvider = config.i18nProvider ?? this.createDefaultI18nProvider();
84
- // Register standard interaction extractors (idempotent per-registry instance)
85
- for (const ex of getStandardInteractionExtractors()) {
86
- try {
87
- this.extractionRegistry.register(ex);
88
- }
89
- catch {
90
- // ignore duplicates if a caller pre-registered
91
- }
92
- }
93
- // Plugins can further extend registries
94
- for (const p of (config.plugins ?? [])) {
95
- try {
96
- p?.registerExtractors?.(this.extractionRegistry);
97
- p?.registerComponents?.(this.componentRegistry);
98
- }
99
- catch {
100
- // plugin errors shouldn't prevent core parsing
103
+ this.i18nProvider = this.config.i18nProvider ?? this.createDefaultI18nProvider();
104
+ if (!this.extractionRegistry.isSealed()) {
105
+ // Register standard interaction extractors.
106
+ for (const module of getStandardInteractionModules()) {
107
+ this.extractionRegistry.register(module.extractor);
108
+ registerFrameworkDeliveryFields(this.extractionRegistry, module.type, module.delivery);
101
109
  }
110
+ this.extractionRegistry.seal();
102
111
  }
103
112
  // Build ASTs once.
104
113
  const processingBuildOptions = {
105
114
  elementNameMapper: this.mapper,
106
- resolveProcessingFragment: config.resolveProcessingFragment,
107
- maxProcessingFragmentDepth: config.processingFragmentLimits?.maxDepth,
108
- maxProcessingFragmentCharacters: config.processingFragmentLimits?.maxCharacters,
115
+ resolveProcessingFragment: this.config.resolveProcessingFragment,
116
+ maxProcessingFragmentDepth: this.config.processingFragmentLimits?.maxDepth,
117
+ maxProcessingFragmentCharacters: this.config.processingFragmentLimits?.maxCharacters,
109
118
  };
110
- const templateProcessing = this.itemDocument.getProcessingElement('template');
111
- if (templateProcessing) {
112
- this.templateProcessingProgram = buildTemplateProcessingAst(templateProcessing, processingBuildOptions);
119
+ const templateProcessing = prepared ? null : this.itemDocument.getProcessingElement('template');
120
+ this.templateProcessingProgram =
121
+ prepared?.templateProcessingProgram ??
122
+ (templateProcessing
123
+ ? buildTemplateProcessingAst(templateProcessing, processingBuildOptions)
124
+ : null);
125
+ if (this.templateProcessingProgram &&
126
+ !construction.compileOnly &&
127
+ !construction.skipTemplateProcessing) {
113
128
  this.execTemplateProcessing();
114
129
  }
115
- const responseProcessing = this.itemDocument.getProcessingElement('response');
116
- if (responseProcessing) {
117
- this.responseProcessingProgram = buildResponseProcessingAst(responseProcessing, processingBuildOptions);
118
- }
119
- const outcomeProcessing = this.itemDocument.getProcessingElement('outcome');
120
- if (outcomeProcessing) {
121
- this.outcomeProcessingProgram = buildOutcomeProcessingAst(outcomeProcessing, processingBuildOptions);
122
- }
123
- // Apply initial responses after templateProcessing (so template vars are ready).
124
- if (config.responses) {
125
- this.setResponses(config.responses);
126
- }
130
+ const responseProcessing = prepared ? null : this.itemDocument.getProcessingElement('response');
131
+ this.responseProcessing = prepared?.responseProcessing ?? this.itemDocument.describeResponseProcessing();
132
+ this.responseProcessingProgram =
133
+ prepared?.responseProcessingProgram ??
134
+ (responseProcessing
135
+ ? buildResponseProcessingAst(responseProcessing, processingBuildOptions)
136
+ : null);
137
+ const outcomeProcessing = prepared ? null : this.itemDocument.getProcessingElement('outcome');
138
+ this.outcomeProcessingProgram =
139
+ prepared?.outcomeProcessingProgram ??
140
+ (outcomeProcessing
141
+ ? buildOutcomeProcessingAst(outcomeProcessing, processingBuildOptions)
142
+ : null);
127
143
  // Detect and log QTI version for compatibility awareness
128
- const detectedVersion = this.detectQTIVersion();
144
+ const detectedVersion = prepared ? null : this.detectQTIVersion();
129
145
  if (detectedVersion === 'unknown') {
130
146
  console.warn('[QTI Player] Could not detect QTI version. Assuming QTI 2.2 compatibility.');
131
147
  }
@@ -136,32 +152,58 @@ export class Player {
136
152
  console.info('[QTI Player] QTI 2.1 detected. Using QTI 2.2 compatibility mode with CC2 template support.');
137
153
  }
138
154
  // Store PNP profile for later use (applyPnpToRoot is called once a root element is available).
139
- this._pnp = config.pnp;
140
- // Build catalog index from legacy shared catalog XML, item XML, and resolved delivery context.
155
+ this._pnp = this.config.pnp;
156
+ // Build the catalog index from shared catalog XML, item XML, and resolved delivery context.
141
157
  // Sanitized item-level delivery context entries win over raw item XML on collisions.
142
158
  // Stimulus entries are kept separately so rendered stimulus terms can avoid ID collisions.
143
- let mergedCatalog = new Map();
144
- let deliveryItemCatalog = new Map();
145
- for (const source of config.deliveryContext?.catalogSources ?? []) {
146
- const sourceCatalog = extractCatalog(source.xml);
147
- if (source.scope === 'stimulus' && source.stimulusIdentifier) {
148
- const existing = this._stimulusCatalogIndexes.get(source.stimulusIdentifier) ?? new Map();
149
- this._stimulusCatalogIndexes.set(source.stimulusIdentifier, mergeCatalogs(existing, sourceCatalog));
159
+ if (prepared) {
160
+ this._catalogIndex = prepared.catalogIndex;
161
+ this._stimulusCatalogIndexes = prepared.stimulusCatalogIndexes;
162
+ }
163
+ else {
164
+ let mergedCatalog = new Map();
165
+ let deliveryItemCatalog = new Map();
166
+ for (const source of this.config.deliveryContext?.catalogSources ?? []) {
167
+ const sourceCatalog = extractCatalog(source.xml);
168
+ if (source.scope === 'stimulus' && source.stimulusIdentifier) {
169
+ const existing = this._stimulusCatalogIndexes.get(source.stimulusIdentifier) ?? new Map();
170
+ this._stimulusCatalogIndexes.set(source.stimulusIdentifier, mergeCatalogs(existing, sourceCatalog));
171
+ }
172
+ else {
173
+ deliveryItemCatalog = mergeCatalogs(deliveryItemCatalog, sourceCatalog);
174
+ }
150
175
  }
151
- else {
152
- deliveryItemCatalog = mergeCatalogs(deliveryItemCatalog, sourceCatalog);
176
+ if (this.config.catalogXml) {
177
+ mergedCatalog = mergeCatalogs(mergedCatalog, extractCatalogFromItemXml(this.config.catalogXml));
153
178
  }
179
+ mergedCatalog = mergeCatalogs(mergedCatalog, extractCatalogFromItemXml(this.itemXml));
180
+ this._catalogIndex = mergeCatalogs(mergedCatalog, deliveryItemCatalog);
154
181
  }
155
- if (config.catalogXml) {
156
- mergedCatalog = mergeCatalogs(mergedCatalog, extractCatalogFromItemXml(config.catalogXml));
157
- }
158
- mergedCatalog = mergeCatalogs(mergedCatalog, extractCatalogFromItemXml(this.itemXml));
159
- this._catalogIndex = mergeCatalogs(mergedCatalog, deliveryItemCatalog);
160
182
  // Check strict compliance if enabled
161
- if (this.config.strictQtiCompliance?.enabled) {
183
+ if (!prepared && this.config.strictQtiCompliance?.enabled) {
162
184
  this.validateStrictCompliance();
163
185
  }
164
186
  }
187
+ /** @internal Definition compiler capture; parser artifacts remain module-private. */
188
+ capturePreparedDefinition() {
189
+ const identifier = this.itemDocument.getAssessmentItemAttribute('identifier')?.trim();
190
+ if (!identifier) {
191
+ throw new Error('AssessmentItemDefinition requires an assessment-item identifier');
192
+ }
193
+ return Object.freeze({
194
+ identifier,
195
+ mapper: this.mapper,
196
+ attributeMapper: this.config.attributeNameMapper,
197
+ itemDocument: this.itemDocument,
198
+ declarations: cloneDeclarationMap(this.sourceDeclarations),
199
+ responseProcessing: this.responseProcessing,
200
+ responseProcessingProgram: this.responseProcessingProgram,
201
+ templateProcessingProgram: this.templateProcessingProgram,
202
+ outcomeProcessingProgram: this.outcomeProcessingProgram,
203
+ catalogIndex: this._catalogIndex,
204
+ stimulusCatalogIndexes: this._stimulusCatalogIndexes,
205
+ });
206
+ }
165
207
  /**
166
208
  * Apply the PNP profile to a player root element.
167
209
  * Call this once the root DOM element is available (e.g. after mounting).
@@ -212,7 +254,7 @@ export class Player {
212
254
  if (looksLikeCatalogUrl(html)) {
213
255
  return sanitizeResourceUrl(html.trim(), this.config.security?.urlPolicy, 'img');
214
256
  }
215
- return this.sanitizeHtmlContent(html);
257
+ return toTrustedHtml(this.sanitizeHtmlContent(html), this.config.security?.trustedTypesPolicyName);
216
258
  }
217
259
  /** Sanitize externally resolved item-adjacent HTML before it reaches a rendering sink. */
218
260
  sanitizeHtmlContent(html) {
@@ -225,11 +267,13 @@ export class Player {
225
267
  */
226
268
  createPciHost(data) {
227
269
  const host = new PciHost(data, {
228
- baseUrl: this.config.pci?.baseUrl ?? this.config.pciBaseUrl,
270
+ baseUrl: this.config.pci?.baseUrl,
229
271
  moduleResolver: this.config.pci?.moduleResolver,
230
272
  });
231
273
  // Wire response changes back into the player's declaration context
232
274
  host.onResponseChange((responseId, value) => {
275
+ if (this.lifecycleStatus !== 'initial' && this.lifecycleStatus !== 'interacting')
276
+ return;
233
277
  const d = this.decls[responseId];
234
278
  if (d) {
235
279
  d.value = this.coerceToDeclarationValue(d.baseType, d.cardinality, value, d.identifier);
@@ -278,7 +322,7 @@ export class Player {
278
322
  continue;
279
323
  d.value = this.coerceToDeclarationValue(d.baseType, d.cardinality, raw, d.identifier);
280
324
  // Push restored responses into any mounted PCI modules
281
- this.setPciResponse(id, raw);
325
+ this.setPciResponse(id, this.qtiValueToPublic(d.value));
282
326
  }
283
327
  }
284
328
  /**
@@ -297,6 +341,7 @@ export class Player {
297
341
  }
298
342
  restoreItemSession(state) {
299
343
  this.sessionGuid = state.sessionGuid;
344
+ this.interactionDataCache = null;
300
345
  this.lifecycleStatus = state.lifecycleStatus;
301
346
  this.accumulatedDurationMs = Math.max(0, Number(state.duration) || 0);
302
347
  this.sessionStartedAt = Date.now();
@@ -306,6 +351,15 @@ export class Player {
306
351
  this.applySerializedVariables(state.contextVariables);
307
352
  this.updateDuration();
308
353
  }
354
+ /** @internal ItemSession uses this to activate a restored delivery handoff. */
355
+ getLifecycleStatus() {
356
+ return this.lifecycleStatus;
357
+ }
358
+ /** @internal ItemSession uses this to resume a suspended delivery handoff. */
359
+ activateAttempt() {
360
+ if (this.lifecycleStatus === 'suspended')
361
+ this.lifecycleStatus = 'interacting';
362
+ }
309
363
  suspendAttempt() {
310
364
  return this.runItemSessionAction({ action: 'suspendAttempt' });
311
365
  }
@@ -451,30 +505,7 @@ export class Player {
451
505
  * @returns QTI version string ('2.0', '2.1', '2.2') or 'unknown'
452
506
  */
453
507
  detectQTIVersion() {
454
- const ns = this.assessmentItem.namespaceURI;
455
- if (ns?.includes('v3p0') || ns?.includes('imsqtiasi_v3p0'))
456
- return '3.0';
457
- if (ns?.includes('v2p2') || ns?.includes('imsqti_v2p2'))
458
- return '2.2';
459
- if (ns?.includes('v2p1') || ns?.includes('imsqti_v2p1'))
460
- return '2.1';
461
- if (ns?.includes('v2p0') || ns?.includes('imsqti_v2p0'))
462
- return '2.0';
463
- // Check element name for QTI 3.0
464
- const localName = this.assessmentItem.localName || this.assessmentItem.tagName;
465
- if (localName === 'qti-assessment-item' || localName === 'qti-assessment-test') {
466
- return '3.0';
467
- }
468
- const versionAttr = getAttr(this.assessmentItem, 'version');
469
- if (versionAttr?.startsWith('3.'))
470
- return '3.0';
471
- if (versionAttr === '2.0')
472
- return '2.0';
473
- if (versionAttr === '2.1')
474
- return '2.1';
475
- if (versionAttr === '2.2')
476
- return '2.2';
477
- return 'unknown';
508
+ return this.itemDocument.detectVersion();
478
509
  }
479
510
  /**
480
511
  * Validates strict QTI 2.2 compliance if enabled in configuration.
@@ -528,18 +559,14 @@ export class Player {
528
559
  // This prevents stale outcomes when response/outcome processing doesn't set every variable.
529
560
  this.resetOutcomesToDefault();
530
561
  // Execute response processing if present
531
- const rpEl = this.itemDocument.getProcessingElement('response');
532
- if (rpEl) {
562
+ if (this.responseProcessing.present) {
533
563
  // If responseProcessing has explicit statements, run the compiled program.
534
564
  // Otherwise, fall back to template-based processing (responseProcessing@template).
535
- const hasStatements = childElements(rpEl).length > 0;
536
- if (hasStatements) {
565
+ if (this.responseProcessing.hasStatements) {
537
566
  this.execResponseProcessingProgram();
538
567
  }
539
- else {
540
- const templateUrl = getAttr(rpEl, 'template');
541
- if (templateUrl)
542
- this.execResponseProcessingTemplate(templateUrl);
568
+ else if (this.responseProcessing.template) {
569
+ this.execResponseProcessingTemplate(this.responseProcessing.template);
543
570
  }
544
571
  }
545
572
  // Execute outcome processing if present (runs after responseProcessing)
@@ -582,25 +609,22 @@ export class Player {
582
609
  */
583
610
  getRubrics(options = {}) {
584
611
  const role = this.role;
585
- const rubricEls = this.itemDocument.findRubricElements();
586
- if (rubricEls.length === 0)
612
+ const rubrics = this.itemDocument.readRubricBlocks();
613
+ if (rubrics.length === 0)
587
614
  return [];
588
615
  const blocks = [];
589
- for (const el of rubricEls) {
590
- const scope = this.itemDocument.rubricElementScope(el);
616
+ for (const rubric of rubrics) {
617
+ const { scope } = rubric;
591
618
  if (options.scope && options.scope !== 'all' && scope !== options.scope)
592
619
  continue;
593
- const viewRaw = (getAttr(el, 'view') || '').trim();
594
- const view = viewRaw ? viewRaw.split(/[\s,]+/).filter(Boolean) : [];
595
- const use = (getAttr(el, 'use') || '').trim() || undefined;
620
+ const view = [...rubric.view];
596
621
  // If view is specified, show only when it includes the current role.
597
622
  if (view.length > 0 && role && !view.includes(role))
598
623
  continue;
599
- const contentRaw = this.itemDocument.serializeChildren(el) || (el.textContent || '');
600
- const printed = this.renderPrintedVariables(contentRaw);
624
+ const printed = this.renderPrintedVariables(rubric.content);
601
625
  const sanitized = sanitizeHtml(printed, { security: this.config.security });
602
626
  const html = toTrustedHtml(sanitized, this.config.security?.trustedTypesPolicyName);
603
- blocks.push({ view, html, scope, use });
627
+ blocks.push({ view, html, scope, use: rubric.use });
604
628
  }
605
629
  return blocks;
606
630
  }
@@ -614,7 +638,9 @@ export class Player {
614
638
  * Canonical interaction API (new): extracted, typed interaction data.
615
639
  */
616
640
  getInteractionData() {
617
- return extractInteractionData({
641
+ if (this.interactionDataCache)
642
+ return this.interactionDataCache;
643
+ this.interactionDataCache = extractInteractionData({
618
644
  document: this.itemDocument,
619
645
  extractionRegistry: this.extractionRegistry,
620
646
  declarations: this.decls,
@@ -624,6 +650,7 @@ export class Player {
624
650
  // different candidate or a new attempt gets a different one.
625
651
  sessionGuid: this.sessionGuid,
626
652
  });
653
+ return this.interactionDataCache;
627
654
  }
628
655
  /**
629
656
  * Convenience API (used by tests/bench): returns a minimal interaction list with QTI-ish field names.
@@ -662,10 +689,6 @@ export class Player {
662
689
  isCompleted() {
663
690
  return this.getCompletionStatus() === 'completed';
664
691
  }
665
- submitAttempt(countAttempt = true) {
666
- const result = this.runItemSessionAction({ action: 'submitAttempt', countAttempt });
667
- return result.scoring;
668
- }
669
692
  runAdaptiveSubmitAttempt(countAttempt = true) {
670
693
  if (this.isCompleted()) {
671
694
  throw new Error('Cannot submit: item is already completed');
@@ -1229,7 +1252,7 @@ export class Player {
1229
1252
  continue;
1230
1253
  d.value = this.coerceToDeclarationValue(d.baseType, d.cardinality, v.value, d.identifier);
1231
1254
  if (d.__kind === 'response') {
1232
- this.setPciResponse(d.identifier, v.value);
1255
+ this.setPciResponse(d.identifier, this.qtiValueToPublic(d.value));
1233
1256
  }
1234
1257
  }
1235
1258
  }
@@ -1284,7 +1307,7 @@ export class Player {
1284
1307
  if (!value || value.kind !== 'value')
1285
1308
  return null;
1286
1309
  if (value.cardinality !== 'record')
1287
- return value.value;
1310
+ return cloneRuntimeValue(value.value);
1288
1311
  const serialized = {};
1289
1312
  for (const [fieldIdentifier, fieldValue] of Object.entries(value.value)) {
1290
1313
  serialized[fieldIdentifier] = {
@@ -1299,7 +1322,7 @@ export class Player {
1299
1322
  if (!value || value.kind !== 'value')
1300
1323
  return null;
1301
1324
  if (value.cardinality !== 'record')
1302
- return value.value;
1325
+ return cloneRuntimeValue(value.value);
1303
1326
  const record = {};
1304
1327
  for (const [fieldIdentifier, fieldValue] of Object.entries(value.value)) {
1305
1328
  record[fieldIdentifier] = this.qtiValueToPublic(fieldValue);
@@ -1420,7 +1443,7 @@ export class Player {
1420
1443
  return qtiNull(baseType, cardinality);
1421
1444
  // Allow callers to pass fully-formed QtiValue (e.g. session restore).
1422
1445
  if (typeof raw === 'object' && raw && 'kind' in raw) {
1423
- return raw;
1446
+ return cloneRuntimeValue(raw);
1424
1447
  }
1425
1448
  const extendedTextBase = identifier ? this.getExtendedTextBase(identifier) : undefined;
1426
1449
  if (cardinality === 'record') {
@@ -1432,11 +1455,9 @@ export class Player {
1432
1455
  if (baseType === 'file') {
1433
1456
  if (cardinality === 'multiple' || cardinality === 'ordered') {
1434
1457
  const arr = Array.isArray(raw) ? raw : [raw];
1435
- // For file types, preserve objects (QTIFileResponse) directly
1436
- return qtiValue(baseType, cardinality, arr.filter((v) => v !== null && v !== undefined));
1458
+ return qtiValue(baseType, cardinality, cloneRuntimeValue(arr.filter((v) => v !== null && v !== undefined)));
1437
1459
  }
1438
- // For single file, preserve the object directly
1439
- return qtiValue(baseType, cardinality, raw);
1460
+ return qtiValue(baseType, cardinality, cloneRuntimeValue(raw));
1440
1461
  }
1441
1462
  if (cardinality === 'multiple' || cardinality === 'ordered') {
1442
1463
  const arr = Array.isArray(raw) ? raw : [raw];
@@ -1466,13 +1487,7 @@ export class Player {
1466
1487
  return coerceBaseValue(baseType, t);
1467
1488
  }
1468
1489
  getExtendedTextBase(responseIdentifier) {
1469
- const interaction = this.itemDocument
1470
- .findExtractionElements(['extendedTextInteraction'])
1471
- .find((candidate) => candidate.responseIdentifier === responseIdentifier);
1472
- if (!interaction)
1473
- return undefined;
1474
- const base = Number(interaction.element.getAttribute?.('base') ?? 10);
1475
- return Number.isInteger(base) && base >= 2 && base <= 36 ? base : 10;
1490
+ return this.itemDocument.getExtendedTextBase(responseIdentifier);
1476
1491
  }
1477
1492
  coerceRecordValue(raw, base) {
1478
1493
  let source = raw;
@@ -1573,15 +1588,10 @@ export class Player {
1573
1588
  return toTrustedHtml(sanitized, this.config.security?.trustedTypesPolicyName);
1574
1589
  }
1575
1590
  getModalFeedback(outcomes) {
1576
- const feedbackEls = this.itemDocument.findModalFeedbackElements();
1577
1591
  const active = [];
1578
- for (const el of feedbackEls) {
1579
- const identifier = this.getAttrMapped(el, 'identifier') || '';
1580
- const outcomeIdentifier = this.getAttrMapped(el, 'outcomeIdentifier') || '';
1581
- const showHide = (this.getAttrMapped(el, 'showHide') || 'show');
1582
- const title = this.getAttrMapped(el, 'title') || undefined;
1583
- const contentRaw = this.itemDocument.serializeChildren(el) || (el.textContent || '');
1584
- const sanitized = sanitizeHtml(contentRaw, { security: this.config.security });
1592
+ for (const feedback of this.itemDocument.readModalFeedback()) {
1593
+ const { identifier, outcomeIdentifier, showHide, title } = feedback;
1594
+ const sanitized = sanitizeHtml(feedback.content, { security: this.config.security });
1585
1595
  const content = toTrustedHtml(sanitized, this.config.security?.trustedTypesPolicyName);
1586
1596
  const outcomeValue = outcomes[outcomeIdentifier];
1587
1597
  const shouldShow = showHide === 'show' ? outcomeValue === identifier : outcomeValue !== identifier;
@@ -1625,6 +1635,15 @@ function createSessionGuid() {
1625
1635
  'requires crypto.randomUUID or crypto.getRandomValues (present in all supported ' +
1626
1636
  'browsers and in Node >= 20).');
1627
1637
  }
1638
+ function cloneDeclarationMap(declarations) {
1639
+ return structuredClone(declarations);
1640
+ }
1641
+ /** Detach serializable runtime values from caller- and declaration-owned references. */
1642
+ function cloneRuntimeValue(value) {
1643
+ if (!value || typeof value !== 'object')
1644
+ return value;
1645
+ return structuredClone(value);
1646
+ }
1628
1647
  function looksLikeCatalogUrl(value) {
1629
1648
  const trimmed = value.trim();
1630
1649
  return /^(https?:\/\/|\/\/|\/|\.{0,2}\/|data:|blob:)/i.test(trimmed) ||