@learnosity/lib-qti-converter 0.12.0 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18528,6 +18528,77 @@ function getMimeTypeFromExtension(src) {
18528
18528
  if (ext === "webm") return "video/webm";
18529
18529
  if (ext === "ogv") return "video/ogg";
18530
18530
  }
18531
+ /**
18532
+ * Generic section-extraction wrapper that eliminates boilerplate from individual
18533
+ * interaction builders. Calls the provided `buildXml` function to generate raw XML
18534
+ * fragments, then regex-extracts the standard QTI assessmentItem sections.
18535
+ *
18536
+ * Handles both self-closing (`<responseDeclaration ... />`) and paired
18537
+ * (`<responseDeclaration ...>...</responseDeclaration>`) element forms.
18538
+ *
18539
+ * @example
18540
+ * ```ts
18541
+ * export function buildAssociateInteractionSections(...) {
18542
+ * buildInteractionSections(
18543
+ * buildAssociateInteractionXml,
18544
+ * [interaction, diagnostics],
18545
+ * sections, indent, indent2, indent3, nl
18546
+ * );
18547
+ * }
18548
+ * ```
18549
+ */
18550
+ function buildInteractionSections(buildXml, buildArgs, _diagnostics, sections, indent, indent2, indent3, nl, opts) {
18551
+ const tempParts = [];
18552
+ buildXml(...buildArgs, tempParts, indent, indent2, indent3, nl);
18553
+ const joined = tempParts.join("");
18554
+ if (opts?.multiResponseDeclarations) {
18555
+ const matches = joined.matchAll(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/g);
18556
+ for (const m of matches) sections.responseDeclarations.push(m[1] + nl);
18557
+ } else if (opts?.selfClosingResponseDecl) {
18558
+ const match = joined.match(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/);
18559
+ if (match) sections.responseDeclarations.push(match[1] + nl);
18560
+ } else {
18561
+ const match = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
18562
+ if (match) sections.responseDeclarations.push(match[1] + nl);
18563
+ }
18564
+ if (opts?.multiOutcomeDeclarations) {
18565
+ const matches = joined.matchAll(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/g);
18566
+ for (const m of matches) sections.outcomeDeclarations.push(m[1] + nl);
18567
+ } else {
18568
+ const match = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
18569
+ if (match) sections.outcomeDeclarations.push(match[1] + nl);
18570
+ }
18571
+ const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
18572
+ if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
18573
+ const responseProcMatch = joined.match(/(<responseProcessing[\s\S]*?(?:\/>|<\/responseProcessing>))/);
18574
+ if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
18575
+ }
18576
+ /**
18577
+ * Emit the standard SCORE outcomeDeclaration block.
18578
+ *
18579
+ * Used by: associate, choice, gapMatch, inlineChoice, match, order, textEntry (×2)
18580
+ */
18581
+ function pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl) {
18582
+ parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
18583
+ parts.push(nl);
18584
+ parts.push(`${indent2}<defaultValue>`);
18585
+ parts.push(nl);
18586
+ parts.push(`${indent3}<value>0</value>`);
18587
+ parts.push(nl);
18588
+ parts.push(`${indent2}</defaultValue>`);
18589
+ parts.push(nl);
18590
+ parts.push(`${indent}</outcomeDeclaration>`);
18591
+ parts.push(nl);
18592
+ }
18593
+ /**
18594
+ * Emit the standard responseProcessing match_correct template reference.
18595
+ *
18596
+ * Used by: associate, choice, gapMatch, hotspot, hottext, match, order, textEntry (×2)
18597
+ */
18598
+ function pushMatchCorrectResponseProcessing(parts, indent, nl) {
18599
+ parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
18600
+ parts.push(nl);
18601
+ }
18531
18602
  //#endregion
18532
18603
  //#region src/qti-serializer/multiWidgetSerializer.ts
18533
18604
  /**
@@ -18828,16 +18899,7 @@ function buildMultiWidgetItemXml(ast, diagnostics, parts, indent, indent2, inden
18828
18899
  parts.push(nl);
18829
18900
  }
18830
18901
  }
18831
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
18832
- parts.push(nl);
18833
- parts.push(`${indent2}<defaultValue>`);
18834
- parts.push(nl);
18835
- parts.push(`${indent3}<value>0</value>`);
18836
- parts.push(nl);
18837
- parts.push(`${indent2}</defaultValue>`);
18838
- parts.push(nl);
18839
- parts.push(`${indent}</outcomeDeclaration>`);
18840
- parts.push(nl);
18902
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
18841
18903
  parts.push(`${indent}<itemBody>`);
18842
18904
  parts.push(nl);
18843
18905
  let contentHtml = layout.contentHtml;
@@ -18886,10 +18948,7 @@ function buildMultiWidgetItemXml(ast, diagnostics, parts, indent, indent2, inden
18886
18948
  else if (int.type === "textEntry") return (int.correctResponses?.length ?? 0) > 0;
18887
18949
  else if (int.type === "order") return (int.correctResponse?.length ?? 0) > 0;
18888
18950
  return false;
18889
- })) {
18890
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
18891
- parts.push(nl);
18892
- }
18951
+ })) pushMatchCorrectResponseProcessing(parts, indent, nl);
18893
18952
  }
18894
18953
  /**
18895
18954
  * Render an interaction as XML string (for multi-widget layouts)
@@ -19218,17 +19277,11 @@ function convertVideoplayerToQh5(data) {
19218
19277
  * Section-based wrapper for buildChoiceInteractionXml
19219
19278
  */
19220
19279
  function buildChoiceInteractionSections(interaction, feedbackBlock, diagnostics, sections, indent, indent2, indent3, nl) {
19221
- const tempParts = [];
19222
- buildChoiceInteractionXml(interaction, feedbackBlock, diagnostics, tempParts, indent, indent2, indent3, nl);
19223
- const joined = tempParts.join("");
19224
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
19225
- const outcomeDeclMatches = joined.matchAll(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/g);
19226
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19227
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
19228
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19229
- for (const match of outcomeDeclMatches) sections.outcomeDeclarations.push(match[1] + nl);
19230
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19231
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19280
+ buildInteractionSections(buildChoiceInteractionXml, [
19281
+ interaction,
19282
+ feedbackBlock,
19283
+ diagnostics
19284
+ ], diagnostics, sections, indent, indent2, indent3, nl, { multiOutcomeDeclarations: true });
19232
19285
  }
19233
19286
  /**
19234
19287
  * Build choice interaction XML (MCQ)
@@ -19258,16 +19311,7 @@ function buildChoiceInteractionXml(interaction, feedbackBlock, diagnostics, part
19258
19311
  parts.push(`${indent}</responseDeclaration>`);
19259
19312
  parts.push(nl);
19260
19313
  }
19261
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
19262
- parts.push(nl);
19263
- parts.push(`${indent2}<defaultValue>`);
19264
- parts.push(nl);
19265
- parts.push(`${indent3}<value>0</value>`);
19266
- parts.push(nl);
19267
- parts.push(`${indent2}</defaultValue>`);
19268
- parts.push(nl);
19269
- parts.push(`${indent}</outcomeDeclaration>`);
19270
- parts.push(nl);
19314
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
19271
19315
  if (feedbackBlock && feedbackBlock.length > 0) {
19272
19316
  parts.push(`${indent}<outcomeDeclaration identifier="FEEDBACK" cardinality="single" baseType="identifier">`);
19273
19317
  parts.push(nl);
@@ -19320,10 +19364,7 @@ function buildChoiceInteractionXml(interaction, feedbackBlock, diagnostics, part
19320
19364
  }
19321
19365
  parts.push(`${indent}</itemBody>`);
19322
19366
  parts.push(nl);
19323
- if (interaction.correctResponse) {
19324
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
19325
- parts.push(nl);
19326
- }
19367
+ if (interaction.correctResponse) pushMatchCorrectResponseProcessing(parts, indent, nl);
19327
19368
  }
19328
19369
  //#endregion
19329
19370
  //#region src/qti-serializer/interaction-builders/buildTextEntryInteraction.ts
@@ -19331,17 +19372,7 @@ function buildChoiceInteractionXml(interaction, feedbackBlock, diagnostics, part
19331
19372
  * Section-based wrapper for buildTextEntryInteractionXml
19332
19373
  */
19333
19374
  function buildTextEntryInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
19334
- const tempParts = [];
19335
- buildTextEntryInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
19336
- const joined = tempParts.join("");
19337
- const responseDeclMatches = joined.matchAll(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/g);
19338
- for (const match of responseDeclMatches) sections.responseDeclarations.push(match[1] + nl);
19339
- const outcomeDeclMatch = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
19340
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19341
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
19342
- if (outcomeDeclMatch) sections.outcomeDeclarations.push(outcomeDeclMatch[1] + nl);
19343
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19344
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19375
+ buildInteractionSections(buildTextEntryInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl, { multiResponseDeclarations: true });
19345
19376
  }
19346
19377
  /**
19347
19378
  * Build text entry interaction XML (clozetext)
@@ -19375,16 +19406,7 @@ function buildTextEntryInteractionXml(interaction, diagnostics, parts, indent, i
19375
19406
  parts.push(`${indent}<responseDeclaration identifier="${interaction.responseIdentifier}" cardinality="single" baseType="string"/>`);
19376
19407
  parts.push(nl);
19377
19408
  }
19378
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
19379
- parts.push(nl);
19380
- parts.push(`${indent2}<defaultValue>`);
19381
- parts.push(nl);
19382
- parts.push(`${indent3}<value>0</value>`);
19383
- parts.push(nl);
19384
- parts.push(`${indent2}</defaultValue>`);
19385
- parts.push(nl);
19386
- parts.push(`${indent}</outcomeDeclaration>`);
19387
- parts.push(nl);
19409
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
19388
19410
  parts.push(`${indent}<itemBody>`);
19389
19411
  parts.push(nl);
19390
19412
  diagnostics.push({
@@ -19414,10 +19436,7 @@ function buildTextEntryInteractionXml(interaction, diagnostics, parts, indent, i
19414
19436
  parts.push(nl);
19415
19437
  parts.push(`${indent}</itemBody>`);
19416
19438
  parts.push(nl);
19417
- if (interaction.correctResponses && interaction.correctResponses.length > 0) {
19418
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
19419
- parts.push(nl);
19420
- }
19439
+ if (interaction.correctResponses && interaction.correctResponses.length > 0) pushMatchCorrectResponseProcessing(parts, indent, nl);
19421
19440
  return;
19422
19441
  }
19423
19442
  if (!Array.isArray(interaction.textEntries) || interaction.textEntries.length === 0) {
@@ -19454,16 +19473,7 @@ function buildTextEntryInteractionXml(interaction, diagnostics, parts, indent, i
19454
19473
  parts.push(`${indent}<responseDeclaration identifier="${entry.responseIdentifier}" cardinality="single" baseType="string"/>`);
19455
19474
  parts.push(nl);
19456
19475
  }
19457
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
19458
- parts.push(nl);
19459
- parts.push(`${indent2}<defaultValue>`);
19460
- parts.push(nl);
19461
- parts.push(`${indent3}<value>0</value>`);
19462
- parts.push(nl);
19463
- parts.push(`${indent2}</defaultValue>`);
19464
- parts.push(nl);
19465
- parts.push(`${indent}</outcomeDeclaration>`);
19466
- parts.push(nl);
19476
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
19467
19477
  parts.push(`${indent}<itemBody>`);
19468
19478
  parts.push(nl);
19469
19479
  if (interaction.prompt) {
@@ -19513,10 +19523,7 @@ function buildTextEntryInteractionXml(interaction, diagnostics, parts, indent, i
19513
19523
  parts.push(nl);
19514
19524
  parts.push(`${indent}</itemBody>`);
19515
19525
  parts.push(nl);
19516
- if (interaction.textEntries.some((e) => e.correctResponse)) {
19517
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
19518
- parts.push(nl);
19519
- }
19526
+ if (interaction.textEntries.some((e) => e.correctResponse)) pushMatchCorrectResponseProcessing(parts, indent, nl);
19520
19527
  }
19521
19528
  //#endregion
19522
19529
  //#region src/qti-serializer/interaction-builders/buildExtendedTextInteraction.ts
@@ -19524,15 +19531,14 @@ function buildTextEntryInteractionXml(interaction, diagnostics, parts, indent, i
19524
19531
  * Section-based wrapper for buildExtendedTextInteractionXml
19525
19532
  */
19526
19533
  function buildExtendedTextInteractionSections(interaction, outcomeDeclaration, diagnostics, sections, indent, indent2, indent3, nl) {
19527
- const tempParts = [];
19528
- buildExtendedTextInteractionXml(interaction, outcomeDeclaration, diagnostics, tempParts, indent, indent2, indent3, nl);
19529
- const joined = tempParts.join("");
19530
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/);
19531
- const outcomeDeclMatches = joined.matchAll(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/g);
19532
- for (const match of outcomeDeclMatches) sections.outcomeDeclarations.push(match[1] + nl);
19533
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19534
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19535
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19534
+ buildInteractionSections(buildExtendedTextInteractionXml, [
19535
+ interaction,
19536
+ outcomeDeclaration,
19537
+ diagnostics
19538
+ ], diagnostics, sections, indent, indent2, indent3, nl, {
19539
+ multiOutcomeDeclarations: true,
19540
+ selfClosingResponseDecl: true
19541
+ });
19536
19542
  }
19537
19543
  /**
19538
19544
  * Build extended text interaction XML (essay/long answer)
@@ -19580,15 +19586,10 @@ function buildExtendedTextInteractionXml(interaction, outcomeDeclaration, diagno
19580
19586
  * Section-based wrapper for buildUploadInteractionXml
19581
19587
  */
19582
19588
  function buildUploadInteractionSections(interaction, diagnostics, sections, indent, indent2, nl) {
19583
- const tempParts = [];
19584
- buildUploadInteractionXml(interaction, diagnostics, tempParts, indent, indent2, nl);
19585
- const joined = tempParts.join("");
19586
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/);
19587
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19588
- const responseProcessingMatch = joined.match(/(<responseProcessing[\s\S]*?(?:\/>|<\/responseProcessing>))/);
19589
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19590
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19591
- if (responseProcessingMatch) sections.responseProcessing = responseProcessingMatch[1] + nl;
19589
+ const buildXmlAdapter = (interaction, diagnostics, parts, indent, indent2, _indent3, nl) => {
19590
+ buildUploadInteractionXml(interaction, diagnostics, parts, indent, indent2, nl);
19591
+ };
19592
+ buildInteractionSections(buildXmlAdapter, [interaction, diagnostics], diagnostics, sections, indent, indent2, "", nl, { selfClosingResponseDecl: true });
19592
19593
  }
19593
19594
  /**
19594
19595
  * Build upload interaction XML (file upload)
@@ -19627,15 +19628,7 @@ function buildUploadInteractionXml(interaction, diagnostics, parts, indent, inde
19627
19628
  * Build drawingInteraction sections
19628
19629
  */
19629
19630
  function buildDrawingInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
19630
- const tempParts = [];
19631
- buildDrawingInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
19632
- const joined = tempParts.join("");
19633
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/);
19634
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19635
- const responseProcessingMatch = joined.match(/(<responseProcessing[\s\S]*?(?:\/>|<\/responseProcessing>))/);
19636
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19637
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19638
- if (responseProcessingMatch) sections.responseProcessing = responseProcessingMatch[1] + nl;
19631
+ buildInteractionSections(buildDrawingInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl, { selfClosingResponseDecl: true });
19639
19632
  }
19640
19633
  /**
19641
19634
  * Build drawingInteraction XML
@@ -19693,17 +19686,7 @@ function buildDrawingInteractionXml(interaction, diagnostics, parts, indent, ind
19693
19686
  * Section-based wrapper for buildInlineChoiceInteractionXml
19694
19687
  */
19695
19688
  function buildInlineChoiceInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
19696
- const tempParts = [];
19697
- buildInlineChoiceInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
19698
- const joined = tempParts.join("");
19699
- const responseDeclMatches = joined.matchAll(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/g);
19700
- for (const match of responseDeclMatches) sections.responseDeclarations.push(match[1] + nl);
19701
- const outcomeDeclMatch = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
19702
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19703
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
19704
- if (outcomeDeclMatch) sections.outcomeDeclarations.push(outcomeDeclMatch[1] + nl);
19705
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19706
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19689
+ buildInteractionSections(buildInlineChoiceInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl, { multiResponseDeclarations: true });
19707
19690
  }
19708
19691
  /**
19709
19692
  * Build inline choice interaction XML (clozedropdown)
@@ -19733,16 +19716,7 @@ function buildInlineChoiceInteractionXml(interaction, diagnostics, parts, indent
19733
19716
  parts.push(nl);
19734
19717
  }
19735
19718
  }
19736
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
19737
- parts.push(nl);
19738
- parts.push(`${indent2}<defaultValue>`);
19739
- parts.push(nl);
19740
- parts.push(`${indent3}<value>0</value>`);
19741
- parts.push(nl);
19742
- parts.push(`${indent2}</defaultValue>`);
19743
- parts.push(nl);
19744
- parts.push(`${indent}</outcomeDeclaration>`);
19745
- parts.push(nl);
19719
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
19746
19720
  parts.push(`${indent}<itemBody>`);
19747
19721
  parts.push(nl);
19748
19722
  if (interaction.prompt) {
@@ -19781,17 +19755,7 @@ function buildInlineChoiceInteractionXml(interaction, diagnostics, parts, indent
19781
19755
  * Section-based wrapper for buildGapMatchInteractionXml
19782
19756
  */
19783
19757
  function buildGapMatchInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
19784
- const tempParts = [];
19785
- buildGapMatchInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
19786
- const joined = tempParts.join("");
19787
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
19788
- const outcomeDeclMatch = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
19789
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19790
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
19791
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19792
- if (outcomeDeclMatch) sections.outcomeDeclarations.push(outcomeDeclMatch[1] + nl);
19793
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19794
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19758
+ buildInteractionSections(buildGapMatchInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl);
19795
19759
  }
19796
19760
  /**
19797
19761
  * Build gapMatch interaction XML for clozeassociation
@@ -19825,16 +19789,7 @@ function buildGapMatchInteractionXml(interaction, diagnostics, parts, indent, in
19825
19789
  parts.push(nl);
19826
19790
  }
19827
19791
  }
19828
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
19829
- parts.push(nl);
19830
- parts.push(`${indent2}<defaultValue>`);
19831
- parts.push(nl);
19832
- parts.push(`${indent3}<value>0</value>`);
19833
- parts.push(nl);
19834
- parts.push(`${indent2}</defaultValue>`);
19835
- parts.push(nl);
19836
- parts.push(`${indent}</outcomeDeclaration>`);
19837
- parts.push(nl);
19792
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
19838
19793
  parts.push(`${indent}<itemBody>`);
19839
19794
  parts.push(nl);
19840
19795
  if (interaction.prompt) {
@@ -19881,17 +19836,7 @@ function buildGapMatchInteractionXml(interaction, diagnostics, parts, indent, in
19881
19836
  * Section-based wrapper for buildOrderInteractionXml
19882
19837
  */
19883
19838
  function buildOrderInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
19884
- const tempParts = [];
19885
- buildOrderInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
19886
- const joined = tempParts.join("");
19887
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
19888
- const outcomeDeclMatch = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
19889
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19890
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
19891
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19892
- if (outcomeDeclMatch) sections.outcomeDeclarations.push(outcomeDeclMatch[1] + nl);
19893
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19894
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19839
+ buildInteractionSections(buildOrderInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl);
19895
19840
  }
19896
19841
  /**
19897
19842
  * Build orderInteraction XML (for single-widget items)
@@ -19924,16 +19869,7 @@ function buildOrderInteractionXml(interaction, diagnostics, parts, indent, inden
19924
19869
  parts.push(`${indent}<responseDeclaration identifier="${interaction.responseIdentifier}" cardinality="ordered" baseType="identifier"/>`);
19925
19870
  parts.push(nl);
19926
19871
  }
19927
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
19928
- parts.push(nl);
19929
- parts.push(`${indent2}<defaultValue>`);
19930
- parts.push(nl);
19931
- parts.push(`${indent3}<value>0</value>`);
19932
- parts.push(nl);
19933
- parts.push(`${indent2}</defaultValue>`);
19934
- parts.push(nl);
19935
- parts.push(`${indent}</outcomeDeclaration>`);
19936
- parts.push(nl);
19872
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
19937
19873
  parts.push(`${indent}<itemBody>`);
19938
19874
  parts.push(nl);
19939
19875
  if (interaction.prompt) {
@@ -19966,10 +19902,7 @@ function buildOrderInteractionXml(interaction, diagnostics, parts, indent, inden
19966
19902
  parts.push(nl);
19967
19903
  parts.push(`${indent}</itemBody>`);
19968
19904
  parts.push(nl);
19969
- if (interaction.correctResponse && interaction.correctResponse.length > 0) {
19970
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
19971
- parts.push(nl);
19972
- }
19905
+ if (interaction.correctResponse && interaction.correctResponse.length > 0) pushMatchCorrectResponseProcessing(parts, indent, nl);
19973
19906
  }
19974
19907
  //#endregion
19975
19908
  //#region src/qti-serializer/interaction-builders/buildMatchInteraction.ts
@@ -19977,17 +19910,7 @@ function buildOrderInteractionXml(interaction, diagnostics, parts, indent, inden
19977
19910
  * Section-based wrapper for buildMatchInteractionXml
19978
19911
  */
19979
19912
  function buildMatchInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
19980
- const tempParts = [];
19981
- buildMatchInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
19982
- const joined = tempParts.join("");
19983
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
19984
- const outcomeDeclMatch = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
19985
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19986
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
19987
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19988
- if (outcomeDeclMatch) sections.outcomeDeclarations.push(outcomeDeclMatch[1] + nl);
19989
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19990
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19913
+ buildInteractionSections(buildMatchInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl);
19991
19914
  }
19992
19915
  /**
19993
19916
  * Build matchInteraction XML (choicematrix)
@@ -20010,16 +19933,7 @@ function buildMatchInteractionXml(interaction, diagnostics, parts, indent, inden
20010
19933
  parts.push(`${indent}<responseDeclaration identifier="${interaction.responseIdentifier}" cardinality="multiple" baseType="directedPair"/>`);
20011
19934
  parts.push(nl);
20012
19935
  }
20013
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
20014
- parts.push(nl);
20015
- parts.push(`${indent2}<defaultValue>`);
20016
- parts.push(nl);
20017
- parts.push(`${indent3}<value>0</value>`);
20018
- parts.push(nl);
20019
- parts.push(`${indent2}</defaultValue>`);
20020
- parts.push(nl);
20021
- parts.push(`${indent}</outcomeDeclaration>`);
20022
- parts.push(nl);
19936
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
20023
19937
  parts.push(`${indent}<itemBody>`);
20024
19938
  parts.push(nl);
20025
19939
  if (interaction.prompt) {
@@ -20058,8 +19972,7 @@ function buildMatchInteractionXml(interaction, diagnostics, parts, indent, inden
20058
19972
  parts.push(nl);
20059
19973
  parts.push(`${indent}</itemBody>`);
20060
19974
  parts.push(nl);
20061
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
20062
- parts.push(nl);
19975
+ pushMatchCorrectResponseProcessing(parts, indent, nl);
20063
19976
  }
20064
19977
  //#endregion
20065
19978
  //#region src/qti-serializer/interaction-builders/buildHottextInteraction.ts
@@ -20067,15 +19980,7 @@ function buildMatchInteractionXml(interaction, diagnostics, parts, indent, inden
20067
19980
  * Build hottext interaction sections (tokenhighlight)
20068
19981
  */
20069
19982
  function buildHottextInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
20070
- const tempParts = [];
20071
- buildHottextInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
20072
- const joined = tempParts.join("");
20073
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
20074
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
20075
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
20076
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
20077
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
20078
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19983
+ buildInteractionSections(buildHottextInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl);
20079
19984
  }
20080
19985
  /**
20081
19986
  * Build hottext interaction XML (tokenhighlight)
@@ -20130,10 +20035,7 @@ function buildHottextInteractionXml(interaction, diagnostics, parts, indent, ind
20130
20035
  parts.push(nl);
20131
20036
  parts.push(`${indent}</itemBody>`);
20132
20037
  parts.push(nl);
20133
- if (interaction.correctResponse && interaction.correctResponse.length > 0) {
20134
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
20135
- parts.push(nl);
20136
- }
20038
+ if (interaction.correctResponse && interaction.correctResponse.length > 0) pushMatchCorrectResponseProcessing(parts, indent, nl);
20137
20039
  }
20138
20040
  //#endregion
20139
20041
  //#region src/qti-serializer/interaction-builders/buildHotspotInteraction.ts
@@ -20141,15 +20043,7 @@ function buildHottextInteractionXml(interaction, diagnostics, parts, indent, ind
20141
20043
  * Build hotspotInteraction sections
20142
20044
  */
20143
20045
  function buildHotspotInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
20144
- const tempParts = [];
20145
- buildHotspotInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
20146
- const joined = tempParts.join("");
20147
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
20148
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
20149
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
20150
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
20151
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
20152
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
20046
+ buildInteractionSections(buildHotspotInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl, { selfClosingResponseDecl: true });
20153
20047
  }
20154
20048
  /**
20155
20049
  * Build hotspotInteraction XML
@@ -20196,10 +20090,7 @@ function buildHotspotInteractionXml(interaction, _diagnostics, parts, indent, in
20196
20090
  parts.push(nl);
20197
20091
  parts.push(`${indent}</itemBody>`);
20198
20092
  parts.push(nl);
20199
- if (interaction.correctResponse && interaction.correctResponse.length > 0) {
20200
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
20201
- parts.push(nl);
20202
- }
20093
+ if (interaction.correctResponse && interaction.correctResponse.length > 0) pushMatchCorrectResponseProcessing(parts, indent, nl);
20203
20094
  }
20204
20095
  //#endregion
20205
20096
  //#region src/qti-serializer/interaction-builders/buildGraphicGapMatchInteraction.ts
@@ -20207,15 +20098,7 @@ function buildHotspotInteractionXml(interaction, _diagnostics, parts, indent, in
20207
20098
  * Build graphicGapMatchInteraction sections
20208
20099
  */
20209
20100
  function buildGraphicGapMatchInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
20210
- const tempParts = [];
20211
- buildGraphicGapMatchInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
20212
- const joined = tempParts.join("");
20213
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
20214
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
20215
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
20216
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
20217
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
20218
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
20101
+ buildInteractionSections(buildGraphicGapMatchInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl, { selfClosingResponseDecl: true });
20219
20102
  }
20220
20103
  /**
20221
20104
  * Build graphicGapMatchInteraction XML (imageclozeassociationV2)
@@ -20294,17 +20177,7 @@ function buildGraphicGapMatchInteractionXml(interaction, diagnostics, parts, ind
20294
20177
  * Section-based wrapper for buildAssociateInteractionXml
20295
20178
  */
20296
20179
  function buildAssociateInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
20297
- const tempParts = [];
20298
- buildAssociateInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
20299
- const joined = tempParts.join("");
20300
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
20301
- const outcomeDeclMatch = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
20302
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
20303
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
20304
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
20305
- if (outcomeDeclMatch) sections.outcomeDeclarations.push(outcomeDeclMatch[1] + nl);
20306
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
20307
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
20180
+ buildInteractionSections(buildAssociateInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl);
20308
20181
  }
20309
20182
  /**
20310
20183
  * Build associateInteraction XML (for single-widget items, classification)
@@ -20327,16 +20200,7 @@ function buildAssociateInteractionXml(interaction, diagnostics, parts, indent, i
20327
20200
  parts.push(`${indent}<responseDeclaration identifier="${interaction.responseIdentifier}" cardinality="multiple" baseType="pair"/>`);
20328
20201
  parts.push(nl);
20329
20202
  }
20330
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
20331
- parts.push(nl);
20332
- parts.push(`${indent2}<defaultValue>`);
20333
- parts.push(nl);
20334
- parts.push(`${indent3}<value>0</value>`);
20335
- parts.push(nl);
20336
- parts.push(`${indent2}</defaultValue>`);
20337
- parts.push(nl);
20338
- parts.push(`${indent}</outcomeDeclaration>`);
20339
- parts.push(nl);
20203
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
20340
20204
  parts.push(`${indent}<itemBody>`);
20341
20205
  parts.push(nl);
20342
20206
  if (interaction.prompt) {
@@ -20365,10 +20229,7 @@ function buildAssociateInteractionXml(interaction, diagnostics, parts, indent, i
20365
20229
  parts.push(nl);
20366
20230
  parts.push(`${indent}</itemBody>`);
20367
20231
  parts.push(nl);
20368
- if (interaction.correctResponse && interaction.correctResponse.length > 0) {
20369
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
20370
- parts.push(nl);
20371
- }
20232
+ if (interaction.correctResponse && interaction.correctResponse.length > 0) pushMatchCorrectResponseProcessing(parts, indent, nl);
20372
20233
  }
20373
20234
  //#endregion
20374
20235
  //#region src/qti-serializer/serializeAssessmentItem.ts
@@ -27093,104 +26954,267 @@ function convertGraphicGapMatchToLearnosity(inputXml, diagnostics, _lomTags, org
27093
26954
  };
27094
26955
  }
27095
26956
  //#endregion
27096
- //#region src/qti-parser/readAssessmentItemDrawing.ts
26957
+ //#region src/qti-parser/createAssessmentItemReader.ts
27097
26958
  /**
27098
- * Read QTI drawingInteraction using streaming parser
26959
+ * Shared SAX streaming scaffold for QTI assessmentItem readers.
26960
+ *
26961
+ * Every `readAssessmentItemXxx` parser repeats the same ~40 lines of boilerplate:
26962
+ * - Create a SaxesParser
26963
+ * - Handle assessmentItem opentag (extract identifier, title)
26964
+ * - Handle responseDeclaration + correctResponse + value extraction
26965
+ * - Handle itemBody detection
26966
+ * - text/cdata capture dispatch
26967
+ * - try/catch error handling with diagnostics
26968
+ * - Post-parse validations (foundAssessmentItem, foundItemBody, interaction count)
26969
+ *
26970
+ * This utility extracts the shared envelope, leaving each parser to focus only
26971
+ * on its interaction-specific logic.
27099
26972
  */
27100
- function readAssessmentItemDrawing(inputXml) {
27101
- const diagnostics = [];
27102
- const result = { diagnostics };
27103
- const parser = new import_saxes.SaxesParser();
27104
- const stimulusBuf = [];
27105
- const promptBuf = [];
27106
- let capture = null;
27107
- let inDrawingInteraction = false;
27108
- let foundAssessmentItem = false;
27109
- let foundItemBody = false;
27110
- let drawingInteractionCount = 0;
27111
- parser.on("opentag", (tag) => {
27112
- const localName = tag.local ?? tag.name;
27113
- if (localName === "assessmentItem") {
27114
- foundAssessmentItem = true;
27115
- result.identifier = getAttr$5(tag, "identifier");
27116
- result.title = getAttr$5(tag, "title") ?? null;
27117
- } else if (localName === "itemBody") {
27118
- foundItemBody = true;
27119
- capture = "stimulus";
27120
- stimulusBuf.length = 0;
27121
- } else if (localName === "drawingInteraction") {
27122
- drawingInteractionCount++;
27123
- inDrawingInteraction = true;
27124
- result.responseIdentifier = getAttr$5(tag, "responseIdentifier");
27125
- if (capture === "stimulus") {
27126
- result.stimulusText = normalizeWhitespace(stimulusBuf.join(""));
27127
- capture = null;
26973
+ /**
26974
+ * Create an assessmentItem reader with the shared envelope handled automatically.
26975
+ *
26976
+ * The returned function accepts XML input and returns the typed result, with all
26977
+ * the common scaffolding (parser creation, assessmentItem/responseDeclaration/itemBody
26978
+ * handling, error handling, validations) done for you.
26979
+ *
26980
+ * @example
26981
+ * ```ts
26982
+ * export const readAssessmentItemDrawing = createAssessmentItemReader<ReadDrawingResult>({
26983
+ * interactionName: 'drawingInteraction',
26984
+ * createResult: (diagnostics) => ({ diagnostics, ... }),
26985
+ * onInteractionOpen: (tag, result, ctx) => { ... },
26986
+ * });
26987
+ * ```
26988
+ */
26989
+ function createAssessmentItemReader(hooks) {
26990
+ return (inputXml) => {
26991
+ const diagnostics = [];
26992
+ const result = hooks.createResult(diagnostics);
26993
+ const parser = new import_saxes.SaxesParser(hooks.parserOptions);
26994
+ const buffers = /* @__PURE__ */ new Map();
26995
+ let foundAssessmentItem = false;
26996
+ let foundItemBody = false;
26997
+ let inItemBody = false;
26998
+ let inInteraction = false;
26999
+ let interactionCount = 0;
27000
+ let currentResponseDeclId;
27001
+ let inCorrectResponse = false;
27002
+ const correctResponsesByIdentifier = /* @__PURE__ */ new Map();
27003
+ const correctValueBuf = [];
27004
+ const ctx = {
27005
+ get capture() {
27006
+ return _capture;
27007
+ },
27008
+ set capture(val) {
27009
+ _capture = val;
27010
+ },
27011
+ get inItemBody() {
27012
+ return inItemBody;
27013
+ },
27014
+ diagnostics,
27015
+ correctResponsesByIdentifier,
27016
+ get currentResponseDeclId() {
27017
+ return currentResponseDeclId;
27018
+ },
27019
+ getAttr: getAttr$5,
27020
+ normalizeWhitespace,
27021
+ pushToBuffer(name, text) {
27022
+ if (!buffers.has(name)) buffers.set(name, []);
27023
+ buffers.get(name).push(text);
27024
+ },
27025
+ flushBuffer(name) {
27026
+ const buf = buffers.get(name);
27027
+ if (!buf) return "";
27028
+ const result = normalizeWhitespace(buf.join(""));
27029
+ buf.length = 0;
27030
+ return result;
27031
+ },
27032
+ peekBuffer(name) {
27033
+ const buf = buffers.get(name);
27034
+ return buf ? buf.join("") : "";
27035
+ },
27036
+ clearBuffer(name) {
27037
+ const buf = buffers.get(name);
27038
+ if (buf) buf.length = 0;
27128
27039
  }
27129
- } else if (localName === "prompt" && inDrawingInteraction) {
27130
- capture = "prompt";
27131
- promptBuf.length = 0;
27132
- } else if (localName === "img" && inDrawingInteraction) {
27133
- result.imageSource = getAttr$5(tag, "src");
27134
- result.imageAlt = getAttr$5(tag, "alt");
27135
- const widthAttr = getAttr$5(tag, "width");
27136
- const heightAttr = getAttr$5(tag, "height");
27137
- if (widthAttr) result.imageWidth = parseInt(widthAttr, 10);
27138
- if (heightAttr) result.imageHeight = parseInt(heightAttr, 10);
27139
- }
27140
- });
27141
- parser.on("closetag", (tag) => {
27142
- const localName = tag.local ?? tag.name;
27143
- if (localName === "itemBody") {} else if (localName === "drawingInteraction") inDrawingInteraction = false;
27144
- else if (localName === "prompt" && capture === "prompt") {
27145
- result.promptText = normalizeWhitespace(promptBuf.join(""));
27146
- capture = null;
27040
+ };
27041
+ let _capture = null;
27042
+ parser.on("opentag", (tag) => {
27043
+ const localName = tag.local ?? tag.name;
27044
+ if (localName === "assessmentItem") {
27045
+ foundAssessmentItem = true;
27046
+ result.identifier = getAttr$5(tag, "identifier");
27047
+ result.title = getAttr$5(tag, "title") ?? null;
27048
+ return;
27049
+ }
27050
+ if (localName === "itemBody") {
27051
+ foundItemBody = true;
27052
+ inItemBody = true;
27053
+ return;
27054
+ }
27055
+ if (localName === hooks.interactionName) {
27056
+ interactionCount++;
27057
+ inInteraction = true;
27058
+ hooks.onInteractionOpen(tag, result, ctx);
27059
+ return;
27060
+ }
27061
+ if (localName === "responseDeclaration") {
27062
+ const identifier = getAttr$5(tag, "identifier");
27063
+ if (identifier) {
27064
+ currentResponseDeclId = identifier;
27065
+ correctResponsesByIdentifier.set(identifier, []);
27066
+ }
27067
+ return;
27068
+ }
27069
+ if (localName === "correctResponse") {
27070
+ inCorrectResponse = true;
27071
+ correctValueBuf.length = 0;
27072
+ return;
27073
+ }
27074
+ if (localName === "value" && inCorrectResponse) {
27075
+ correctValueBuf.length = 0;
27076
+ return;
27077
+ }
27078
+ const location = inInteraction ? "in-interaction" : inItemBody ? "pre-interaction" : "post-interaction";
27079
+ hooks.onOpenTag?.(localName, tag, result, ctx, location);
27080
+ });
27081
+ parser.on("closetag", (tag) => {
27082
+ const localName = tag.local ?? tag.name;
27083
+ if (localName === "itemBody") {
27084
+ inItemBody = false;
27085
+ return;
27086
+ }
27087
+ if (localName === hooks.interactionName) {
27088
+ inInteraction = false;
27089
+ hooks.onInteractionClose?.(result, ctx);
27090
+ return;
27091
+ }
27092
+ if (localName === "responseDeclaration") {
27093
+ currentResponseDeclId = void 0;
27094
+ return;
27095
+ }
27096
+ if (localName === "correctResponse") {
27097
+ inCorrectResponse = false;
27098
+ return;
27099
+ }
27100
+ if (localName === "value" && inCorrectResponse) {
27101
+ const value = normalizeWhitespace(correctValueBuf.join(""));
27102
+ if (value && currentResponseDeclId) {
27103
+ const values = correctResponsesByIdentifier.get(currentResponseDeclId);
27104
+ if (values) values.push(value);
27105
+ }
27106
+ correctValueBuf.length = 0;
27107
+ return;
27108
+ }
27109
+ const location = inInteraction ? "in-interaction" : inItemBody ? "pre-interaction" : "post-interaction";
27110
+ hooks.onCloseTag?.(localName, tag, result, ctx, location);
27111
+ });
27112
+ parser.on("text", (text) => {
27113
+ if (inCorrectResponse) {
27114
+ correctValueBuf.push(text);
27115
+ return;
27116
+ }
27117
+ if (_capture) ctx.pushToBuffer(_capture, text);
27118
+ });
27119
+ parser.on("cdata", (text) => {
27120
+ if (inCorrectResponse) {
27121
+ correctValueBuf.push(text);
27122
+ return;
27123
+ }
27124
+ if (_capture) ctx.pushToBuffer(_capture, text);
27125
+ });
27126
+ parser.on("error", (error) => {
27127
+ diagnostics.push({
27128
+ code: DiagnosticCode.LRN_SCHEMA_INVALID,
27129
+ level: "error",
27130
+ file: "assessmentItem",
27131
+ message: `Failed to parse QTI XML: ${error.message}`
27132
+ });
27133
+ });
27134
+ try {
27135
+ parser.write(inputXml).close();
27136
+ } catch (error) {
27137
+ diagnostics.push({
27138
+ code: DiagnosticCode.LRN_SCHEMA_INVALID,
27139
+ level: "error",
27140
+ file: "assessmentItem",
27141
+ message: `Failed to parse QTI XML: ${error instanceof Error ? error.message : String(error)}`
27142
+ });
27143
+ return result;
27147
27144
  }
27148
- });
27149
- parser.on("text", (text) => {
27150
- if (capture === "stimulus") stimulusBuf.push(text);
27151
- else if (capture === "prompt") promptBuf.push(text);
27152
- });
27153
- parser.on("cdata", (text) => {
27154
- if (capture === "stimulus") stimulusBuf.push(text);
27155
- else if (capture === "prompt") promptBuf.push(text);
27156
- });
27157
- try {
27158
- parser.write(inputXml).close();
27159
- } catch (error) {
27160
- diagnostics.push({
27145
+ hooks.onParseComplete?.(result, ctx);
27146
+ if (!foundAssessmentItem) diagnostics.push({
27161
27147
  code: DiagnosticCode.LRN_SCHEMA_INVALID,
27162
27148
  level: "error",
27163
27149
  file: "assessmentItem",
27164
- message: `Failed to parse QTI XML: ${error instanceof Error ? error.message : String(error)}`
27150
+ message: "No assessmentItem element found"
27165
27151
  });
27166
- }
27167
- if (!foundAssessmentItem) diagnostics.push({
27168
- code: DiagnosticCode.LRN_SCHEMA_INVALID,
27169
- level: "error",
27170
- file: "assessmentItem",
27171
- message: "No assessmentItem element found"
27172
- });
27173
- if (!foundItemBody) diagnostics.push({
27174
- code: DiagnosticCode.LRN_SCHEMA_INVALID,
27175
- level: "error",
27176
- file: "assessmentItem",
27177
- message: "No itemBody element found"
27178
- });
27179
- if (foundItemBody && drawingInteractionCount === 0) diagnostics.push({
27180
- code: DiagnosticCode.QTI_UNSUPPORTED_INTERACTION,
27181
- level: "error",
27182
- file: "assessmentItem.itemBody",
27183
- message: "No drawingInteraction found in itemBody"
27184
- });
27185
- if (drawingInteractionCount > 1) diagnostics.push({
27186
- code: DiagnosticCode.QTI_MULTIPLE_INTERACTIONS,
27187
- level: "error",
27188
- file: "assessmentItem.itemBody",
27189
- message: `Multiple drawingInteraction elements found (${drawingInteractionCount}), only single interaction supported`
27190
- });
27191
- return result;
27152
+ if (!foundItemBody) diagnostics.push({
27153
+ code: DiagnosticCode.LRN_SCHEMA_INVALID,
27154
+ level: "error",
27155
+ file: "assessmentItem",
27156
+ message: "No itemBody element found"
27157
+ });
27158
+ if (foundItemBody && interactionCount === 0) diagnostics.push({
27159
+ code: DiagnosticCode.QTI_UNSUPPORTED_INTERACTION,
27160
+ level: "error",
27161
+ file: "assessmentItem.itemBody",
27162
+ message: `No ${hooks.interactionName} found in itemBody`
27163
+ });
27164
+ if (interactionCount > 1) diagnostics.push({
27165
+ code: DiagnosticCode.QTI_MULTIPLE_INTERACTIONS,
27166
+ level: "error",
27167
+ file: "assessmentItem.itemBody",
27168
+ message: `Multiple ${hooks.interactionName} elements found (${interactionCount}), only single interaction supported`
27169
+ });
27170
+ hooks.validate?.(result, ctx, interactionCount);
27171
+ return result;
27172
+ };
27192
27173
  }
27193
27174
  //#endregion
27175
+ //#region src/qti-parser/readAssessmentItemDrawing.ts
27176
+ const readAssessmentItemDrawing = createAssessmentItemReader({
27177
+ interactionName: "drawingInteraction",
27178
+ createResult: (diagnostics) => ({ diagnostics }),
27179
+ onInteractionOpen: (tag, result, ctx) => {
27180
+ result.responseIdentifier = ctx.getAttr(tag, "responseIdentifier");
27181
+ if (ctx.capture === "stimulus") {
27182
+ result.stimulusText = ctx.flushBuffer("stimulus");
27183
+ ctx.capture = null;
27184
+ }
27185
+ },
27186
+ onOpenTag: (localName, tag, result, ctx, location) => {
27187
+ if (location === "pre-interaction") {
27188
+ if (!ctx.capture) {
27189
+ ctx.capture = "stimulus";
27190
+ ctx.clearBuffer("stimulus");
27191
+ }
27192
+ } else if (location === "in-interaction") {
27193
+ if (localName === "prompt") {
27194
+ ctx.capture = "prompt";
27195
+ ctx.clearBuffer("prompt");
27196
+ return true;
27197
+ }
27198
+ if (localName === "img") {
27199
+ result.imageSource = ctx.getAttr(tag, "src");
27200
+ result.imageAlt = ctx.getAttr(tag, "alt");
27201
+ const widthAttr = ctx.getAttr(tag, "width");
27202
+ const heightAttr = ctx.getAttr(tag, "height");
27203
+ if (widthAttr) result.imageWidth = parseInt(widthAttr, 10);
27204
+ if (heightAttr) result.imageHeight = parseInt(heightAttr, 10);
27205
+ return true;
27206
+ }
27207
+ }
27208
+ },
27209
+ onCloseTag: (localName, _tag, result, ctx, location) => {
27210
+ if (location === "in-interaction" && localName === "prompt" && ctx.capture === "prompt") {
27211
+ result.promptText = ctx.flushBuffer("prompt");
27212
+ ctx.capture = null;
27213
+ return true;
27214
+ }
27215
+ }
27216
+ });
27217
+ //#endregion
27194
27218
  //#region src/qti-to-learnosity/convertDrawingToLearnosity.ts
27195
27219
  /**
27196
27220
  * Build drawing stimulus from parsed QTI data
@@ -28665,14 +28689,6 @@ function parseMetadataTags(metadataXml, options = {}) {
28665
28689
  }
28666
28690
  //#endregion
28667
28691
  //#region src/qti-parser/readAssessmentItemUpload.ts
28668
- /**
28669
- * Read QTI uploadInteraction using a streaming parser.
28670
- *
28671
- * The Learnosity -> QTI serializer carries the fileupload allow-list on the
28672
- * interaction's `class` attribute as `lrn-allow-<suffix>` tokens and the file
28673
- * limit as `maxChoices` (QTI has no standard field for either). This reader
28674
- * recovers both, plus the stimulus HTML, so a fileupload item round-trips.
28675
- */
28676
28692
  /** Parse `lrn-allow-<suffix>` tokens from a class attribute into allow_* suffixes. */
28677
28693
  function parseAllowedFileTypes(classAttr) {
28678
28694
  if (!classAttr) return [];
@@ -28690,69 +28706,29 @@ function extractStimulus(inputXml) {
28690
28706
  const stimulus = itemBodyMatch[1].replace(/<uploadInteraction[\s\S]*?(?:\/>|<\/uploadInteraction>)/gi, "").trim();
28691
28707
  return stimulus.length > 0 ? stimulus : void 0;
28692
28708
  }
28693
- function readAssessmentItemUpload(inputXml) {
28694
- const diagnostics = [];
28695
- const result = {
28709
+ const _baseReader = createAssessmentItemReader({
28710
+ interactionName: "uploadInteraction",
28711
+ createResult: (diagnostics) => ({
28696
28712
  allowedFileTypes: [],
28697
28713
  diagnostics
28698
- };
28699
- const parser = new import_saxes.SaxesParser();
28700
- let foundAssessmentItem = false;
28701
- let foundItemBody = false;
28702
- let uploadInteractionCount = 0;
28703
- parser.on("opentag", (tag) => {
28704
- const localName = tag.local ?? tag.name;
28705
- if (localName === "assessmentItem") {
28706
- foundAssessmentItem = true;
28707
- result.identifier = getAttr$5(tag, "identifier");
28708
- result.title = getAttr$5(tag, "title") ?? null;
28709
- } else if (localName === "itemBody") foundItemBody = true;
28710
- else if (localName === "uploadInteraction") {
28711
- uploadInteractionCount++;
28712
- result.responseIdentifier = getAttr$5(tag, "responseIdentifier");
28713
- result.allowedFileTypes = parseAllowedFileTypes(getAttr$5(tag, "class"));
28714
- const maxChoices = getAttr$5(tag, "maxChoices");
28715
- if (maxChoices) {
28716
- const parsed = parseInt(maxChoices, 10);
28717
- if (!Number.isNaN(parsed)) result.maxFiles = parsed;
28718
- }
28714
+ }),
28715
+ onInteractionOpen: (tag, result, ctx) => {
28716
+ result.responseIdentifier = ctx.getAttr(tag, "responseIdentifier");
28717
+ result.allowedFileTypes = parseAllowedFileTypes(getAttr$5(tag, "class"));
28718
+ const maxChoices = ctx.getAttr(tag, "maxChoices");
28719
+ if (maxChoices) {
28720
+ const parsed = parseInt(maxChoices, 10);
28721
+ if (!Number.isNaN(parsed)) result.maxFiles = parsed;
28719
28722
  }
28720
- });
28721
- try {
28722
- parser.write(inputXml).close();
28723
- } catch (error) {
28724
- diagnostics.push({
28725
- code: DiagnosticCode.LRN_SCHEMA_INVALID,
28726
- level: "error",
28727
- file: "assessmentItem",
28728
- message: `Failed to parse QTI XML: ${error instanceof Error ? error.message : String(error)}`
28729
- });
28730
28723
  }
28731
- if (!foundAssessmentItem) diagnostics.push({
28732
- code: DiagnosticCode.LRN_SCHEMA_INVALID,
28733
- level: "error",
28734
- file: "assessmentItem",
28735
- message: "No assessmentItem element found"
28736
- });
28737
- if (!foundItemBody) diagnostics.push({
28738
- code: DiagnosticCode.LRN_SCHEMA_INVALID,
28739
- level: "error",
28740
- file: "assessmentItem",
28741
- message: "No itemBody element found"
28742
- });
28743
- if (foundItemBody && uploadInteractionCount === 0) diagnostics.push({
28744
- code: DiagnosticCode.QTI_UNSUPPORTED_INTERACTION,
28745
- level: "error",
28746
- file: "assessmentItem.itemBody",
28747
- message: "No uploadInteraction found in itemBody"
28748
- });
28749
- if (uploadInteractionCount > 1) diagnostics.push({
28750
- code: DiagnosticCode.QTI_MULTIPLE_INTERACTIONS,
28751
- level: "error",
28752
- file: "assessmentItem.itemBody",
28753
- message: `Multiple uploadInteraction elements found (${uploadInteractionCount}), only single interaction supported`
28754
- });
28755
- if (diagnostics.every((d) => d.level !== "error")) result.stimulus = extractStimulus(inputXml);
28724
+ });
28725
+ /**
28726
+ * Read uploadInteraction data from QTI assessmentItem XML.
28727
+ * Wraps the base reader to add post-parse stimulus extraction from raw XML.
28728
+ */
28729
+ function readAssessmentItemUpload(inputXml) {
28730
+ const result = _baseReader(inputXml);
28731
+ if (result.diagnostics.every((d) => d.level !== "error")) result.stimulus = extractStimulus(inputXml);
28756
28732
  return result;
28757
28733
  }
28758
28734
  //#endregion