@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.
@@ -18535,6 +18535,77 @@ function getMimeTypeFromExtension(src) {
18535
18535
  if (ext === "webm") return "video/webm";
18536
18536
  if (ext === "ogv") return "video/ogg";
18537
18537
  }
18538
+ /**
18539
+ * Generic section-extraction wrapper that eliminates boilerplate from individual
18540
+ * interaction builders. Calls the provided `buildXml` function to generate raw XML
18541
+ * fragments, then regex-extracts the standard QTI assessmentItem sections.
18542
+ *
18543
+ * Handles both self-closing (`<responseDeclaration ... />`) and paired
18544
+ * (`<responseDeclaration ...>...</responseDeclaration>`) element forms.
18545
+ *
18546
+ * @example
18547
+ * ```ts
18548
+ * export function buildAssociateInteractionSections(...) {
18549
+ * buildInteractionSections(
18550
+ * buildAssociateInteractionXml,
18551
+ * [interaction, diagnostics],
18552
+ * sections, indent, indent2, indent3, nl
18553
+ * );
18554
+ * }
18555
+ * ```
18556
+ */
18557
+ function buildInteractionSections(buildXml, buildArgs, _diagnostics, sections, indent, indent2, indent3, nl, opts) {
18558
+ const tempParts = [];
18559
+ buildXml(...buildArgs, tempParts, indent, indent2, indent3, nl);
18560
+ const joined = tempParts.join("");
18561
+ if (opts?.multiResponseDeclarations) {
18562
+ const matches = joined.matchAll(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/g);
18563
+ for (const m of matches) sections.responseDeclarations.push(m[1] + nl);
18564
+ } else if (opts?.selfClosingResponseDecl) {
18565
+ const match = joined.match(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/);
18566
+ if (match) sections.responseDeclarations.push(match[1] + nl);
18567
+ } else {
18568
+ const match = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
18569
+ if (match) sections.responseDeclarations.push(match[1] + nl);
18570
+ }
18571
+ if (opts?.multiOutcomeDeclarations) {
18572
+ const matches = joined.matchAll(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/g);
18573
+ for (const m of matches) sections.outcomeDeclarations.push(m[1] + nl);
18574
+ } else {
18575
+ const match = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
18576
+ if (match) sections.outcomeDeclarations.push(match[1] + nl);
18577
+ }
18578
+ const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
18579
+ if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
18580
+ const responseProcMatch = joined.match(/(<responseProcessing[\s\S]*?(?:\/>|<\/responseProcessing>))/);
18581
+ if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
18582
+ }
18583
+ /**
18584
+ * Emit the standard SCORE outcomeDeclaration block.
18585
+ *
18586
+ * Used by: associate, choice, gapMatch, inlineChoice, match, order, textEntry (×2)
18587
+ */
18588
+ function pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl) {
18589
+ parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
18590
+ parts.push(nl);
18591
+ parts.push(`${indent2}<defaultValue>`);
18592
+ parts.push(nl);
18593
+ parts.push(`${indent3}<value>0</value>`);
18594
+ parts.push(nl);
18595
+ parts.push(`${indent2}</defaultValue>`);
18596
+ parts.push(nl);
18597
+ parts.push(`${indent}</outcomeDeclaration>`);
18598
+ parts.push(nl);
18599
+ }
18600
+ /**
18601
+ * Emit the standard responseProcessing match_correct template reference.
18602
+ *
18603
+ * Used by: associate, choice, gapMatch, hotspot, hottext, match, order, textEntry (×2)
18604
+ */
18605
+ function pushMatchCorrectResponseProcessing(parts, indent, nl) {
18606
+ parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
18607
+ parts.push(nl);
18608
+ }
18538
18609
  //#endregion
18539
18610
  //#region src/qti-serializer/multiWidgetSerializer.ts
18540
18611
  /**
@@ -18835,16 +18906,7 @@ function buildMultiWidgetItemXml(ast, diagnostics, parts, indent, indent2, inden
18835
18906
  parts.push(nl);
18836
18907
  }
18837
18908
  }
18838
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
18839
- parts.push(nl);
18840
- parts.push(`${indent2}<defaultValue>`);
18841
- parts.push(nl);
18842
- parts.push(`${indent3}<value>0</value>`);
18843
- parts.push(nl);
18844
- parts.push(`${indent2}</defaultValue>`);
18845
- parts.push(nl);
18846
- parts.push(`${indent}</outcomeDeclaration>`);
18847
- parts.push(nl);
18909
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
18848
18910
  parts.push(`${indent}<itemBody>`);
18849
18911
  parts.push(nl);
18850
18912
  let contentHtml = layout.contentHtml;
@@ -18893,10 +18955,7 @@ function buildMultiWidgetItemXml(ast, diagnostics, parts, indent, indent2, inden
18893
18955
  else if (int.type === "textEntry") return (int.correctResponses?.length ?? 0) > 0;
18894
18956
  else if (int.type === "order") return (int.correctResponse?.length ?? 0) > 0;
18895
18957
  return false;
18896
- })) {
18897
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
18898
- parts.push(nl);
18899
- }
18958
+ })) pushMatchCorrectResponseProcessing(parts, indent, nl);
18900
18959
  }
18901
18960
  /**
18902
18961
  * Render an interaction as XML string (for multi-widget layouts)
@@ -19225,17 +19284,11 @@ function convertVideoplayerToQh5(data) {
19225
19284
  * Section-based wrapper for buildChoiceInteractionXml
19226
19285
  */
19227
19286
  function buildChoiceInteractionSections(interaction, feedbackBlock, diagnostics, sections, indent, indent2, indent3, nl) {
19228
- const tempParts = [];
19229
- buildChoiceInteractionXml(interaction, feedbackBlock, diagnostics, tempParts, indent, indent2, indent3, nl);
19230
- const joined = tempParts.join("");
19231
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
19232
- const outcomeDeclMatches = joined.matchAll(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/g);
19233
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19234
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
19235
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19236
- for (const match of outcomeDeclMatches) sections.outcomeDeclarations.push(match[1] + nl);
19237
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19238
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19287
+ buildInteractionSections(buildChoiceInteractionXml, [
19288
+ interaction,
19289
+ feedbackBlock,
19290
+ diagnostics
19291
+ ], diagnostics, sections, indent, indent2, indent3, nl, { multiOutcomeDeclarations: true });
19239
19292
  }
19240
19293
  /**
19241
19294
  * Build choice interaction XML (MCQ)
@@ -19265,16 +19318,7 @@ function buildChoiceInteractionXml(interaction, feedbackBlock, diagnostics, part
19265
19318
  parts.push(`${indent}</responseDeclaration>`);
19266
19319
  parts.push(nl);
19267
19320
  }
19268
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
19269
- parts.push(nl);
19270
- parts.push(`${indent2}<defaultValue>`);
19271
- parts.push(nl);
19272
- parts.push(`${indent3}<value>0</value>`);
19273
- parts.push(nl);
19274
- parts.push(`${indent2}</defaultValue>`);
19275
- parts.push(nl);
19276
- parts.push(`${indent}</outcomeDeclaration>`);
19277
- parts.push(nl);
19321
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
19278
19322
  if (feedbackBlock && feedbackBlock.length > 0) {
19279
19323
  parts.push(`${indent}<outcomeDeclaration identifier="FEEDBACK" cardinality="single" baseType="identifier">`);
19280
19324
  parts.push(nl);
@@ -19327,10 +19371,7 @@ function buildChoiceInteractionXml(interaction, feedbackBlock, diagnostics, part
19327
19371
  }
19328
19372
  parts.push(`${indent}</itemBody>`);
19329
19373
  parts.push(nl);
19330
- if (interaction.correctResponse) {
19331
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
19332
- parts.push(nl);
19333
- }
19374
+ if (interaction.correctResponse) pushMatchCorrectResponseProcessing(parts, indent, nl);
19334
19375
  }
19335
19376
  //#endregion
19336
19377
  //#region src/qti-serializer/interaction-builders/buildTextEntryInteraction.ts
@@ -19338,17 +19379,7 @@ function buildChoiceInteractionXml(interaction, feedbackBlock, diagnostics, part
19338
19379
  * Section-based wrapper for buildTextEntryInteractionXml
19339
19380
  */
19340
19381
  function buildTextEntryInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
19341
- const tempParts = [];
19342
- buildTextEntryInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
19343
- const joined = tempParts.join("");
19344
- const responseDeclMatches = joined.matchAll(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/g);
19345
- for (const match of responseDeclMatches) sections.responseDeclarations.push(match[1] + nl);
19346
- const outcomeDeclMatch = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
19347
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19348
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
19349
- if (outcomeDeclMatch) sections.outcomeDeclarations.push(outcomeDeclMatch[1] + nl);
19350
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19351
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19382
+ buildInteractionSections(buildTextEntryInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl, { multiResponseDeclarations: true });
19352
19383
  }
19353
19384
  /**
19354
19385
  * Build text entry interaction XML (clozetext)
@@ -19382,16 +19413,7 @@ function buildTextEntryInteractionXml(interaction, diagnostics, parts, indent, i
19382
19413
  parts.push(`${indent}<responseDeclaration identifier="${interaction.responseIdentifier}" cardinality="single" baseType="string"/>`);
19383
19414
  parts.push(nl);
19384
19415
  }
19385
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
19386
- parts.push(nl);
19387
- parts.push(`${indent2}<defaultValue>`);
19388
- parts.push(nl);
19389
- parts.push(`${indent3}<value>0</value>`);
19390
- parts.push(nl);
19391
- parts.push(`${indent2}</defaultValue>`);
19392
- parts.push(nl);
19393
- parts.push(`${indent}</outcomeDeclaration>`);
19394
- parts.push(nl);
19416
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
19395
19417
  parts.push(`${indent}<itemBody>`);
19396
19418
  parts.push(nl);
19397
19419
  diagnostics.push({
@@ -19421,10 +19443,7 @@ function buildTextEntryInteractionXml(interaction, diagnostics, parts, indent, i
19421
19443
  parts.push(nl);
19422
19444
  parts.push(`${indent}</itemBody>`);
19423
19445
  parts.push(nl);
19424
- if (interaction.correctResponses && interaction.correctResponses.length > 0) {
19425
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
19426
- parts.push(nl);
19427
- }
19446
+ if (interaction.correctResponses && interaction.correctResponses.length > 0) pushMatchCorrectResponseProcessing(parts, indent, nl);
19428
19447
  return;
19429
19448
  }
19430
19449
  if (!Array.isArray(interaction.textEntries) || interaction.textEntries.length === 0) {
@@ -19461,16 +19480,7 @@ function buildTextEntryInteractionXml(interaction, diagnostics, parts, indent, i
19461
19480
  parts.push(`${indent}<responseDeclaration identifier="${entry.responseIdentifier}" cardinality="single" baseType="string"/>`);
19462
19481
  parts.push(nl);
19463
19482
  }
19464
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
19465
- parts.push(nl);
19466
- parts.push(`${indent2}<defaultValue>`);
19467
- parts.push(nl);
19468
- parts.push(`${indent3}<value>0</value>`);
19469
- parts.push(nl);
19470
- parts.push(`${indent2}</defaultValue>`);
19471
- parts.push(nl);
19472
- parts.push(`${indent}</outcomeDeclaration>`);
19473
- parts.push(nl);
19483
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
19474
19484
  parts.push(`${indent}<itemBody>`);
19475
19485
  parts.push(nl);
19476
19486
  if (interaction.prompt) {
@@ -19520,10 +19530,7 @@ function buildTextEntryInteractionXml(interaction, diagnostics, parts, indent, i
19520
19530
  parts.push(nl);
19521
19531
  parts.push(`${indent}</itemBody>`);
19522
19532
  parts.push(nl);
19523
- if (interaction.textEntries.some((e) => e.correctResponse)) {
19524
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
19525
- parts.push(nl);
19526
- }
19533
+ if (interaction.textEntries.some((e) => e.correctResponse)) pushMatchCorrectResponseProcessing(parts, indent, nl);
19527
19534
  }
19528
19535
  //#endregion
19529
19536
  //#region src/qti-serializer/interaction-builders/buildExtendedTextInteraction.ts
@@ -19531,15 +19538,14 @@ function buildTextEntryInteractionXml(interaction, diagnostics, parts, indent, i
19531
19538
  * Section-based wrapper for buildExtendedTextInteractionXml
19532
19539
  */
19533
19540
  function buildExtendedTextInteractionSections(interaction, outcomeDeclaration, diagnostics, sections, indent, indent2, indent3, nl) {
19534
- const tempParts = [];
19535
- buildExtendedTextInteractionXml(interaction, outcomeDeclaration, diagnostics, tempParts, indent, indent2, indent3, nl);
19536
- const joined = tempParts.join("");
19537
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/);
19538
- const outcomeDeclMatches = joined.matchAll(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/g);
19539
- for (const match of outcomeDeclMatches) sections.outcomeDeclarations.push(match[1] + nl);
19540
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19541
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19542
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19541
+ buildInteractionSections(buildExtendedTextInteractionXml, [
19542
+ interaction,
19543
+ outcomeDeclaration,
19544
+ diagnostics
19545
+ ], diagnostics, sections, indent, indent2, indent3, nl, {
19546
+ multiOutcomeDeclarations: true,
19547
+ selfClosingResponseDecl: true
19548
+ });
19543
19549
  }
19544
19550
  /**
19545
19551
  * Build extended text interaction XML (essay/long answer)
@@ -19587,15 +19593,10 @@ function buildExtendedTextInteractionXml(interaction, outcomeDeclaration, diagno
19587
19593
  * Section-based wrapper for buildUploadInteractionXml
19588
19594
  */
19589
19595
  function buildUploadInteractionSections(interaction, diagnostics, sections, indent, indent2, nl) {
19590
- const tempParts = [];
19591
- buildUploadInteractionXml(interaction, diagnostics, tempParts, indent, indent2, nl);
19592
- const joined = tempParts.join("");
19593
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/);
19594
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19595
- const responseProcessingMatch = joined.match(/(<responseProcessing[\s\S]*?(?:\/>|<\/responseProcessing>))/);
19596
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19597
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19598
- if (responseProcessingMatch) sections.responseProcessing = responseProcessingMatch[1] + nl;
19596
+ const buildXmlAdapter = (interaction, diagnostics, parts, indent, indent2, _indent3, nl) => {
19597
+ buildUploadInteractionXml(interaction, diagnostics, parts, indent, indent2, nl);
19598
+ };
19599
+ buildInteractionSections(buildXmlAdapter, [interaction, diagnostics], diagnostics, sections, indent, indent2, "", nl, { selfClosingResponseDecl: true });
19599
19600
  }
19600
19601
  /**
19601
19602
  * Build upload interaction XML (file upload)
@@ -19634,15 +19635,7 @@ function buildUploadInteractionXml(interaction, diagnostics, parts, indent, inde
19634
19635
  * Build drawingInteraction sections
19635
19636
  */
19636
19637
  function buildDrawingInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
19637
- const tempParts = [];
19638
- buildDrawingInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
19639
- const joined = tempParts.join("");
19640
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/);
19641
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19642
- const responseProcessingMatch = joined.match(/(<responseProcessing[\s\S]*?(?:\/>|<\/responseProcessing>))/);
19643
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19644
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19645
- if (responseProcessingMatch) sections.responseProcessing = responseProcessingMatch[1] + nl;
19638
+ buildInteractionSections(buildDrawingInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl, { selfClosingResponseDecl: true });
19646
19639
  }
19647
19640
  /**
19648
19641
  * Build drawingInteraction XML
@@ -19700,17 +19693,7 @@ function buildDrawingInteractionXml(interaction, diagnostics, parts, indent, ind
19700
19693
  * Section-based wrapper for buildInlineChoiceInteractionXml
19701
19694
  */
19702
19695
  function buildInlineChoiceInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
19703
- const tempParts = [];
19704
- buildInlineChoiceInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
19705
- const joined = tempParts.join("");
19706
- const responseDeclMatches = joined.matchAll(/(<responseDeclaration[\s\S]*?(?:\/>|<\/responseDeclaration>))/g);
19707
- for (const match of responseDeclMatches) sections.responseDeclarations.push(match[1] + nl);
19708
- const outcomeDeclMatch = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
19709
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19710
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
19711
- if (outcomeDeclMatch) sections.outcomeDeclarations.push(outcomeDeclMatch[1] + nl);
19712
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19713
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19696
+ buildInteractionSections(buildInlineChoiceInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl, { multiResponseDeclarations: true });
19714
19697
  }
19715
19698
  /**
19716
19699
  * Build inline choice interaction XML (clozedropdown)
@@ -19740,16 +19723,7 @@ function buildInlineChoiceInteractionXml(interaction, diagnostics, parts, indent
19740
19723
  parts.push(nl);
19741
19724
  }
19742
19725
  }
19743
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
19744
- parts.push(nl);
19745
- parts.push(`${indent2}<defaultValue>`);
19746
- parts.push(nl);
19747
- parts.push(`${indent3}<value>0</value>`);
19748
- parts.push(nl);
19749
- parts.push(`${indent2}</defaultValue>`);
19750
- parts.push(nl);
19751
- parts.push(`${indent}</outcomeDeclaration>`);
19752
- parts.push(nl);
19726
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
19753
19727
  parts.push(`${indent}<itemBody>`);
19754
19728
  parts.push(nl);
19755
19729
  if (interaction.prompt) {
@@ -19788,17 +19762,7 @@ function buildInlineChoiceInteractionXml(interaction, diagnostics, parts, indent
19788
19762
  * Section-based wrapper for buildGapMatchInteractionXml
19789
19763
  */
19790
19764
  function buildGapMatchInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
19791
- const tempParts = [];
19792
- buildGapMatchInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
19793
- const joined = tempParts.join("");
19794
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
19795
- const outcomeDeclMatch = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
19796
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19797
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
19798
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19799
- if (outcomeDeclMatch) sections.outcomeDeclarations.push(outcomeDeclMatch[1] + nl);
19800
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19801
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19765
+ buildInteractionSections(buildGapMatchInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl);
19802
19766
  }
19803
19767
  /**
19804
19768
  * Build gapMatch interaction XML for clozeassociation
@@ -19832,16 +19796,7 @@ function buildGapMatchInteractionXml(interaction, diagnostics, parts, indent, in
19832
19796
  parts.push(nl);
19833
19797
  }
19834
19798
  }
19835
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
19836
- parts.push(nl);
19837
- parts.push(`${indent2}<defaultValue>`);
19838
- parts.push(nl);
19839
- parts.push(`${indent3}<value>0</value>`);
19840
- parts.push(nl);
19841
- parts.push(`${indent2}</defaultValue>`);
19842
- parts.push(nl);
19843
- parts.push(`${indent}</outcomeDeclaration>`);
19844
- parts.push(nl);
19799
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
19845
19800
  parts.push(`${indent}<itemBody>`);
19846
19801
  parts.push(nl);
19847
19802
  if (interaction.prompt) {
@@ -19888,17 +19843,7 @@ function buildGapMatchInteractionXml(interaction, diagnostics, parts, indent, in
19888
19843
  * Section-based wrapper for buildOrderInteractionXml
19889
19844
  */
19890
19845
  function buildOrderInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
19891
- const tempParts = [];
19892
- buildOrderInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
19893
- const joined = tempParts.join("");
19894
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
19895
- const outcomeDeclMatch = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
19896
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19897
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
19898
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19899
- if (outcomeDeclMatch) sections.outcomeDeclarations.push(outcomeDeclMatch[1] + nl);
19900
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19901
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19846
+ buildInteractionSections(buildOrderInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl);
19902
19847
  }
19903
19848
  /**
19904
19849
  * Build orderInteraction XML (for single-widget items)
@@ -19931,16 +19876,7 @@ function buildOrderInteractionXml(interaction, diagnostics, parts, indent, inden
19931
19876
  parts.push(`${indent}<responseDeclaration identifier="${interaction.responseIdentifier}" cardinality="ordered" baseType="identifier"/>`);
19932
19877
  parts.push(nl);
19933
19878
  }
19934
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
19935
- parts.push(nl);
19936
- parts.push(`${indent2}<defaultValue>`);
19937
- parts.push(nl);
19938
- parts.push(`${indent3}<value>0</value>`);
19939
- parts.push(nl);
19940
- parts.push(`${indent2}</defaultValue>`);
19941
- parts.push(nl);
19942
- parts.push(`${indent}</outcomeDeclaration>`);
19943
- parts.push(nl);
19879
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
19944
19880
  parts.push(`${indent}<itemBody>`);
19945
19881
  parts.push(nl);
19946
19882
  if (interaction.prompt) {
@@ -19973,10 +19909,7 @@ function buildOrderInteractionXml(interaction, diagnostics, parts, indent, inden
19973
19909
  parts.push(nl);
19974
19910
  parts.push(`${indent}</itemBody>`);
19975
19911
  parts.push(nl);
19976
- if (interaction.correctResponse && interaction.correctResponse.length > 0) {
19977
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
19978
- parts.push(nl);
19979
- }
19912
+ if (interaction.correctResponse && interaction.correctResponse.length > 0) pushMatchCorrectResponseProcessing(parts, indent, nl);
19980
19913
  }
19981
19914
  //#endregion
19982
19915
  //#region src/qti-serializer/interaction-builders/buildMatchInteraction.ts
@@ -19984,17 +19917,7 @@ function buildOrderInteractionXml(interaction, diagnostics, parts, indent, inden
19984
19917
  * Section-based wrapper for buildMatchInteractionXml
19985
19918
  */
19986
19919
  function buildMatchInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
19987
- const tempParts = [];
19988
- buildMatchInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
19989
- const joined = tempParts.join("");
19990
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
19991
- const outcomeDeclMatch = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
19992
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
19993
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
19994
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
19995
- if (outcomeDeclMatch) sections.outcomeDeclarations.push(outcomeDeclMatch[1] + nl);
19996
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
19997
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19920
+ buildInteractionSections(buildMatchInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl);
19998
19921
  }
19999
19922
  /**
20000
19923
  * Build matchInteraction XML (choicematrix)
@@ -20017,16 +19940,7 @@ function buildMatchInteractionXml(interaction, diagnostics, parts, indent, inden
20017
19940
  parts.push(`${indent}<responseDeclaration identifier="${interaction.responseIdentifier}" cardinality="multiple" baseType="directedPair"/>`);
20018
19941
  parts.push(nl);
20019
19942
  }
20020
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
20021
- parts.push(nl);
20022
- parts.push(`${indent2}<defaultValue>`);
20023
- parts.push(nl);
20024
- parts.push(`${indent3}<value>0</value>`);
20025
- parts.push(nl);
20026
- parts.push(`${indent2}</defaultValue>`);
20027
- parts.push(nl);
20028
- parts.push(`${indent}</outcomeDeclaration>`);
20029
- parts.push(nl);
19943
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
20030
19944
  parts.push(`${indent}<itemBody>`);
20031
19945
  parts.push(nl);
20032
19946
  if (interaction.prompt) {
@@ -20065,8 +19979,7 @@ function buildMatchInteractionXml(interaction, diagnostics, parts, indent, inden
20065
19979
  parts.push(nl);
20066
19980
  parts.push(`${indent}</itemBody>`);
20067
19981
  parts.push(nl);
20068
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
20069
- parts.push(nl);
19982
+ pushMatchCorrectResponseProcessing(parts, indent, nl);
20070
19983
  }
20071
19984
  //#endregion
20072
19985
  //#region src/qti-serializer/interaction-builders/buildHottextInteraction.ts
@@ -20074,15 +19987,7 @@ function buildMatchInteractionXml(interaction, diagnostics, parts, indent, inden
20074
19987
  * Build hottext interaction sections (tokenhighlight)
20075
19988
  */
20076
19989
  function buildHottextInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
20077
- const tempParts = [];
20078
- buildHottextInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
20079
- const joined = tempParts.join("");
20080
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
20081
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
20082
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
20083
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
20084
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
20085
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
19990
+ buildInteractionSections(buildHottextInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl);
20086
19991
  }
20087
19992
  /**
20088
19993
  * Build hottext interaction XML (tokenhighlight)
@@ -20137,10 +20042,7 @@ function buildHottextInteractionXml(interaction, diagnostics, parts, indent, ind
20137
20042
  parts.push(nl);
20138
20043
  parts.push(`${indent}</itemBody>`);
20139
20044
  parts.push(nl);
20140
- if (interaction.correctResponse && interaction.correctResponse.length > 0) {
20141
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
20142
- parts.push(nl);
20143
- }
20045
+ if (interaction.correctResponse && interaction.correctResponse.length > 0) pushMatchCorrectResponseProcessing(parts, indent, nl);
20144
20046
  }
20145
20047
  //#endregion
20146
20048
  //#region src/qti-serializer/interaction-builders/buildHotspotInteraction.ts
@@ -20148,15 +20050,7 @@ function buildHottextInteractionXml(interaction, diagnostics, parts, indent, ind
20148
20050
  * Build hotspotInteraction sections
20149
20051
  */
20150
20052
  function buildHotspotInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
20151
- const tempParts = [];
20152
- buildHotspotInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
20153
- const joined = tempParts.join("");
20154
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
20155
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
20156
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
20157
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
20158
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
20159
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
20053
+ buildInteractionSections(buildHotspotInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl, { selfClosingResponseDecl: true });
20160
20054
  }
20161
20055
  /**
20162
20056
  * Build hotspotInteraction XML
@@ -20203,10 +20097,7 @@ function buildHotspotInteractionXml(interaction, _diagnostics, parts, indent, in
20203
20097
  parts.push(nl);
20204
20098
  parts.push(`${indent}</itemBody>`);
20205
20099
  parts.push(nl);
20206
- if (interaction.correctResponse && interaction.correctResponse.length > 0) {
20207
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
20208
- parts.push(nl);
20209
- }
20100
+ if (interaction.correctResponse && interaction.correctResponse.length > 0) pushMatchCorrectResponseProcessing(parts, indent, nl);
20210
20101
  }
20211
20102
  //#endregion
20212
20103
  //#region src/qti-serializer/interaction-builders/buildGraphicGapMatchInteraction.ts
@@ -20214,15 +20105,7 @@ function buildHotspotInteractionXml(interaction, _diagnostics, parts, indent, in
20214
20105
  * Build graphicGapMatchInteraction sections
20215
20106
  */
20216
20107
  function buildGraphicGapMatchInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
20217
- const tempParts = [];
20218
- buildGraphicGapMatchInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
20219
- const joined = tempParts.join("");
20220
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
20221
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
20222
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
20223
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
20224
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
20225
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
20108
+ buildInteractionSections(buildGraphicGapMatchInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl, { selfClosingResponseDecl: true });
20226
20109
  }
20227
20110
  /**
20228
20111
  * Build graphicGapMatchInteraction XML (imageclozeassociationV2)
@@ -20301,17 +20184,7 @@ function buildGraphicGapMatchInteractionXml(interaction, diagnostics, parts, ind
20301
20184
  * Section-based wrapper for buildAssociateInteractionXml
20302
20185
  */
20303
20186
  function buildAssociateInteractionSections(interaction, diagnostics, sections, indent, indent2, indent3, nl) {
20304
- const tempParts = [];
20305
- buildAssociateInteractionXml(interaction, diagnostics, tempParts, indent, indent2, indent3, nl);
20306
- const joined = tempParts.join("");
20307
- const responseDeclMatch = joined.match(/(<responseDeclaration[\s\S]*?<\/responseDeclaration>)/);
20308
- const outcomeDeclMatch = joined.match(/(<outcomeDeclaration[\s\S]*?<\/outcomeDeclaration>)/);
20309
- const itemBodyMatch = joined.match(/(<itemBody>[\s\S]*?<\/itemBody>)/);
20310
- const responseProcMatch = joined.match(/(<responseProcessing[^>]*\/>)/);
20311
- if (responseDeclMatch) sections.responseDeclarations.push(responseDeclMatch[1] + nl);
20312
- if (outcomeDeclMatch) sections.outcomeDeclarations.push(outcomeDeclMatch[1] + nl);
20313
- if (itemBodyMatch) sections.itemBody = itemBodyMatch[1] + nl;
20314
- if (responseProcMatch) sections.responseProcessing = responseProcMatch[1] + nl;
20187
+ buildInteractionSections(buildAssociateInteractionXml, [interaction, diagnostics], diagnostics, sections, indent, indent2, indent3, nl);
20315
20188
  }
20316
20189
  /**
20317
20190
  * Build associateInteraction XML (for single-widget items, classification)
@@ -20334,16 +20207,7 @@ function buildAssociateInteractionXml(interaction, diagnostics, parts, indent, i
20334
20207
  parts.push(`${indent}<responseDeclaration identifier="${interaction.responseIdentifier}" cardinality="multiple" baseType="pair"/>`);
20335
20208
  parts.push(nl);
20336
20209
  }
20337
- parts.push(`${indent}<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">`);
20338
- parts.push(nl);
20339
- parts.push(`${indent2}<defaultValue>`);
20340
- parts.push(nl);
20341
- parts.push(`${indent3}<value>0</value>`);
20342
- parts.push(nl);
20343
- parts.push(`${indent2}</defaultValue>`);
20344
- parts.push(nl);
20345
- parts.push(`${indent}</outcomeDeclaration>`);
20346
- parts.push(nl);
20210
+ pushScoreOutcomeDeclaration(parts, indent, indent2, indent3, nl);
20347
20211
  parts.push(`${indent}<itemBody>`);
20348
20212
  parts.push(nl);
20349
20213
  if (interaction.prompt) {
@@ -20372,10 +20236,7 @@ function buildAssociateInteractionXml(interaction, diagnostics, parts, indent, i
20372
20236
  parts.push(nl);
20373
20237
  parts.push(`${indent}</itemBody>`);
20374
20238
  parts.push(nl);
20375
- if (interaction.correctResponse && interaction.correctResponse.length > 0) {
20376
- parts.push(`${indent}<responseProcessing template="http://www.imsglobal.org/question/qti_v2p2/rptemplates/match_correct"/>`);
20377
- parts.push(nl);
20378
- }
20239
+ if (interaction.correctResponse && interaction.correctResponse.length > 0) pushMatchCorrectResponseProcessing(parts, indent, nl);
20379
20240
  }
20380
20241
  //#endregion
20381
20242
  //#region src/qti-serializer/serializeAssessmentItem.ts
@@ -27100,104 +26961,267 @@ function convertGraphicGapMatchToLearnosity(inputXml, diagnostics, _lomTags, org
27100
26961
  };
27101
26962
  }
27102
26963
  //#endregion
27103
- //#region src/qti-parser/readAssessmentItemDrawing.ts
26964
+ //#region src/qti-parser/createAssessmentItemReader.ts
27104
26965
  /**
27105
- * Read QTI drawingInteraction using streaming parser
26966
+ * Shared SAX streaming scaffold for QTI assessmentItem readers.
26967
+ *
26968
+ * Every `readAssessmentItemXxx` parser repeats the same ~40 lines of boilerplate:
26969
+ * - Create a SaxesParser
26970
+ * - Handle assessmentItem opentag (extract identifier, title)
26971
+ * - Handle responseDeclaration + correctResponse + value extraction
26972
+ * - Handle itemBody detection
26973
+ * - text/cdata capture dispatch
26974
+ * - try/catch error handling with diagnostics
26975
+ * - Post-parse validations (foundAssessmentItem, foundItemBody, interaction count)
26976
+ *
26977
+ * This utility extracts the shared envelope, leaving each parser to focus only
26978
+ * on its interaction-specific logic.
27106
26979
  */
27107
- function readAssessmentItemDrawing(inputXml) {
27108
- const diagnostics = [];
27109
- const result = { diagnostics };
27110
- const parser = new import_saxes.SaxesParser();
27111
- const stimulusBuf = [];
27112
- const promptBuf = [];
27113
- let capture = null;
27114
- let inDrawingInteraction = false;
27115
- let foundAssessmentItem = false;
27116
- let foundItemBody = false;
27117
- let drawingInteractionCount = 0;
27118
- parser.on("opentag", (tag) => {
27119
- const localName = tag.local ?? tag.name;
27120
- if (localName === "assessmentItem") {
27121
- foundAssessmentItem = true;
27122
- result.identifier = getAttr$5(tag, "identifier");
27123
- result.title = getAttr$5(tag, "title") ?? null;
27124
- } else if (localName === "itemBody") {
27125
- foundItemBody = true;
27126
- capture = "stimulus";
27127
- stimulusBuf.length = 0;
27128
- } else if (localName === "drawingInteraction") {
27129
- drawingInteractionCount++;
27130
- inDrawingInteraction = true;
27131
- result.responseIdentifier = getAttr$5(tag, "responseIdentifier");
27132
- if (capture === "stimulus") {
27133
- result.stimulusText = normalizeWhitespace(stimulusBuf.join(""));
27134
- capture = null;
26980
+ /**
26981
+ * Create an assessmentItem reader with the shared envelope handled automatically.
26982
+ *
26983
+ * The returned function accepts XML input and returns the typed result, with all
26984
+ * the common scaffolding (parser creation, assessmentItem/responseDeclaration/itemBody
26985
+ * handling, error handling, validations) done for you.
26986
+ *
26987
+ * @example
26988
+ * ```ts
26989
+ * export const readAssessmentItemDrawing = createAssessmentItemReader<ReadDrawingResult>({
26990
+ * interactionName: 'drawingInteraction',
26991
+ * createResult: (diagnostics) => ({ diagnostics, ... }),
26992
+ * onInteractionOpen: (tag, result, ctx) => { ... },
26993
+ * });
26994
+ * ```
26995
+ */
26996
+ function createAssessmentItemReader(hooks) {
26997
+ return (inputXml) => {
26998
+ const diagnostics = [];
26999
+ const result = hooks.createResult(diagnostics);
27000
+ const parser = new import_saxes.SaxesParser(hooks.parserOptions);
27001
+ const buffers = /* @__PURE__ */ new Map();
27002
+ let foundAssessmentItem = false;
27003
+ let foundItemBody = false;
27004
+ let inItemBody = false;
27005
+ let inInteraction = false;
27006
+ let interactionCount = 0;
27007
+ let currentResponseDeclId;
27008
+ let inCorrectResponse = false;
27009
+ const correctResponsesByIdentifier = /* @__PURE__ */ new Map();
27010
+ const correctValueBuf = [];
27011
+ const ctx = {
27012
+ get capture() {
27013
+ return _capture;
27014
+ },
27015
+ set capture(val) {
27016
+ _capture = val;
27017
+ },
27018
+ get inItemBody() {
27019
+ return inItemBody;
27020
+ },
27021
+ diagnostics,
27022
+ correctResponsesByIdentifier,
27023
+ get currentResponseDeclId() {
27024
+ return currentResponseDeclId;
27025
+ },
27026
+ getAttr: getAttr$5,
27027
+ normalizeWhitespace,
27028
+ pushToBuffer(name, text) {
27029
+ if (!buffers.has(name)) buffers.set(name, []);
27030
+ buffers.get(name).push(text);
27031
+ },
27032
+ flushBuffer(name) {
27033
+ const buf = buffers.get(name);
27034
+ if (!buf) return "";
27035
+ const result = normalizeWhitespace(buf.join(""));
27036
+ buf.length = 0;
27037
+ return result;
27038
+ },
27039
+ peekBuffer(name) {
27040
+ const buf = buffers.get(name);
27041
+ return buf ? buf.join("") : "";
27042
+ },
27043
+ clearBuffer(name) {
27044
+ const buf = buffers.get(name);
27045
+ if (buf) buf.length = 0;
27135
27046
  }
27136
- } else if (localName === "prompt" && inDrawingInteraction) {
27137
- capture = "prompt";
27138
- promptBuf.length = 0;
27139
- } else if (localName === "img" && inDrawingInteraction) {
27140
- result.imageSource = getAttr$5(tag, "src");
27141
- result.imageAlt = getAttr$5(tag, "alt");
27142
- const widthAttr = getAttr$5(tag, "width");
27143
- const heightAttr = getAttr$5(tag, "height");
27144
- if (widthAttr) result.imageWidth = parseInt(widthAttr, 10);
27145
- if (heightAttr) result.imageHeight = parseInt(heightAttr, 10);
27146
- }
27147
- });
27148
- parser.on("closetag", (tag) => {
27149
- const localName = tag.local ?? tag.name;
27150
- if (localName === "itemBody") {} else if (localName === "drawingInteraction") inDrawingInteraction = false;
27151
- else if (localName === "prompt" && capture === "prompt") {
27152
- result.promptText = normalizeWhitespace(promptBuf.join(""));
27153
- capture = null;
27047
+ };
27048
+ let _capture = null;
27049
+ parser.on("opentag", (tag) => {
27050
+ const localName = tag.local ?? tag.name;
27051
+ if (localName === "assessmentItem") {
27052
+ foundAssessmentItem = true;
27053
+ result.identifier = getAttr$5(tag, "identifier");
27054
+ result.title = getAttr$5(tag, "title") ?? null;
27055
+ return;
27056
+ }
27057
+ if (localName === "itemBody") {
27058
+ foundItemBody = true;
27059
+ inItemBody = true;
27060
+ return;
27061
+ }
27062
+ if (localName === hooks.interactionName) {
27063
+ interactionCount++;
27064
+ inInteraction = true;
27065
+ hooks.onInteractionOpen(tag, result, ctx);
27066
+ return;
27067
+ }
27068
+ if (localName === "responseDeclaration") {
27069
+ const identifier = getAttr$5(tag, "identifier");
27070
+ if (identifier) {
27071
+ currentResponseDeclId = identifier;
27072
+ correctResponsesByIdentifier.set(identifier, []);
27073
+ }
27074
+ return;
27075
+ }
27076
+ if (localName === "correctResponse") {
27077
+ inCorrectResponse = true;
27078
+ correctValueBuf.length = 0;
27079
+ return;
27080
+ }
27081
+ if (localName === "value" && inCorrectResponse) {
27082
+ correctValueBuf.length = 0;
27083
+ return;
27084
+ }
27085
+ const location = inInteraction ? "in-interaction" : inItemBody ? "pre-interaction" : "post-interaction";
27086
+ hooks.onOpenTag?.(localName, tag, result, ctx, location);
27087
+ });
27088
+ parser.on("closetag", (tag) => {
27089
+ const localName = tag.local ?? tag.name;
27090
+ if (localName === "itemBody") {
27091
+ inItemBody = false;
27092
+ return;
27093
+ }
27094
+ if (localName === hooks.interactionName) {
27095
+ inInteraction = false;
27096
+ hooks.onInteractionClose?.(result, ctx);
27097
+ return;
27098
+ }
27099
+ if (localName === "responseDeclaration") {
27100
+ currentResponseDeclId = void 0;
27101
+ return;
27102
+ }
27103
+ if (localName === "correctResponse") {
27104
+ inCorrectResponse = false;
27105
+ return;
27106
+ }
27107
+ if (localName === "value" && inCorrectResponse) {
27108
+ const value = normalizeWhitespace(correctValueBuf.join(""));
27109
+ if (value && currentResponseDeclId) {
27110
+ const values = correctResponsesByIdentifier.get(currentResponseDeclId);
27111
+ if (values) values.push(value);
27112
+ }
27113
+ correctValueBuf.length = 0;
27114
+ return;
27115
+ }
27116
+ const location = inInteraction ? "in-interaction" : inItemBody ? "pre-interaction" : "post-interaction";
27117
+ hooks.onCloseTag?.(localName, tag, result, ctx, location);
27118
+ });
27119
+ parser.on("text", (text) => {
27120
+ if (inCorrectResponse) {
27121
+ correctValueBuf.push(text);
27122
+ return;
27123
+ }
27124
+ if (_capture) ctx.pushToBuffer(_capture, text);
27125
+ });
27126
+ parser.on("cdata", (text) => {
27127
+ if (inCorrectResponse) {
27128
+ correctValueBuf.push(text);
27129
+ return;
27130
+ }
27131
+ if (_capture) ctx.pushToBuffer(_capture, text);
27132
+ });
27133
+ parser.on("error", (error) => {
27134
+ diagnostics.push({
27135
+ code: DiagnosticCode.LRN_SCHEMA_INVALID,
27136
+ level: "error",
27137
+ file: "assessmentItem",
27138
+ message: `Failed to parse QTI XML: ${error.message}`
27139
+ });
27140
+ });
27141
+ try {
27142
+ parser.write(inputXml).close();
27143
+ } catch (error) {
27144
+ diagnostics.push({
27145
+ code: DiagnosticCode.LRN_SCHEMA_INVALID,
27146
+ level: "error",
27147
+ file: "assessmentItem",
27148
+ message: `Failed to parse QTI XML: ${error instanceof Error ? error.message : String(error)}`
27149
+ });
27150
+ return result;
27154
27151
  }
27155
- });
27156
- parser.on("text", (text) => {
27157
- if (capture === "stimulus") stimulusBuf.push(text);
27158
- else if (capture === "prompt") promptBuf.push(text);
27159
- });
27160
- parser.on("cdata", (text) => {
27161
- if (capture === "stimulus") stimulusBuf.push(text);
27162
- else if (capture === "prompt") promptBuf.push(text);
27163
- });
27164
- try {
27165
- parser.write(inputXml).close();
27166
- } catch (error) {
27167
- diagnostics.push({
27152
+ hooks.onParseComplete?.(result, ctx);
27153
+ if (!foundAssessmentItem) diagnostics.push({
27168
27154
  code: DiagnosticCode.LRN_SCHEMA_INVALID,
27169
27155
  level: "error",
27170
27156
  file: "assessmentItem",
27171
- message: `Failed to parse QTI XML: ${error instanceof Error ? error.message : String(error)}`
27157
+ message: "No assessmentItem element found"
27172
27158
  });
27173
- }
27174
- if (!foundAssessmentItem) diagnostics.push({
27175
- code: DiagnosticCode.LRN_SCHEMA_INVALID,
27176
- level: "error",
27177
- file: "assessmentItem",
27178
- message: "No assessmentItem element found"
27179
- });
27180
- if (!foundItemBody) diagnostics.push({
27181
- code: DiagnosticCode.LRN_SCHEMA_INVALID,
27182
- level: "error",
27183
- file: "assessmentItem",
27184
- message: "No itemBody element found"
27185
- });
27186
- if (foundItemBody && drawingInteractionCount === 0) diagnostics.push({
27187
- code: DiagnosticCode.QTI_UNSUPPORTED_INTERACTION,
27188
- level: "error",
27189
- file: "assessmentItem.itemBody",
27190
- message: "No drawingInteraction found in itemBody"
27191
- });
27192
- if (drawingInteractionCount > 1) diagnostics.push({
27193
- code: DiagnosticCode.QTI_MULTIPLE_INTERACTIONS,
27194
- level: "error",
27195
- file: "assessmentItem.itemBody",
27196
- message: `Multiple drawingInteraction elements found (${drawingInteractionCount}), only single interaction supported`
27197
- });
27198
- return result;
27159
+ if (!foundItemBody) diagnostics.push({
27160
+ code: DiagnosticCode.LRN_SCHEMA_INVALID,
27161
+ level: "error",
27162
+ file: "assessmentItem",
27163
+ message: "No itemBody element found"
27164
+ });
27165
+ if (foundItemBody && interactionCount === 0) diagnostics.push({
27166
+ code: DiagnosticCode.QTI_UNSUPPORTED_INTERACTION,
27167
+ level: "error",
27168
+ file: "assessmentItem.itemBody",
27169
+ message: `No ${hooks.interactionName} found in itemBody`
27170
+ });
27171
+ if (interactionCount > 1) diagnostics.push({
27172
+ code: DiagnosticCode.QTI_MULTIPLE_INTERACTIONS,
27173
+ level: "error",
27174
+ file: "assessmentItem.itemBody",
27175
+ message: `Multiple ${hooks.interactionName} elements found (${interactionCount}), only single interaction supported`
27176
+ });
27177
+ hooks.validate?.(result, ctx, interactionCount);
27178
+ return result;
27179
+ };
27199
27180
  }
27200
27181
  //#endregion
27182
+ //#region src/qti-parser/readAssessmentItemDrawing.ts
27183
+ const readAssessmentItemDrawing = createAssessmentItemReader({
27184
+ interactionName: "drawingInteraction",
27185
+ createResult: (diagnostics) => ({ diagnostics }),
27186
+ onInteractionOpen: (tag, result, ctx) => {
27187
+ result.responseIdentifier = ctx.getAttr(tag, "responseIdentifier");
27188
+ if (ctx.capture === "stimulus") {
27189
+ result.stimulusText = ctx.flushBuffer("stimulus");
27190
+ ctx.capture = null;
27191
+ }
27192
+ },
27193
+ onOpenTag: (localName, tag, result, ctx, location) => {
27194
+ if (location === "pre-interaction") {
27195
+ if (!ctx.capture) {
27196
+ ctx.capture = "stimulus";
27197
+ ctx.clearBuffer("stimulus");
27198
+ }
27199
+ } else if (location === "in-interaction") {
27200
+ if (localName === "prompt") {
27201
+ ctx.capture = "prompt";
27202
+ ctx.clearBuffer("prompt");
27203
+ return true;
27204
+ }
27205
+ if (localName === "img") {
27206
+ result.imageSource = ctx.getAttr(tag, "src");
27207
+ result.imageAlt = ctx.getAttr(tag, "alt");
27208
+ const widthAttr = ctx.getAttr(tag, "width");
27209
+ const heightAttr = ctx.getAttr(tag, "height");
27210
+ if (widthAttr) result.imageWidth = parseInt(widthAttr, 10);
27211
+ if (heightAttr) result.imageHeight = parseInt(heightAttr, 10);
27212
+ return true;
27213
+ }
27214
+ }
27215
+ },
27216
+ onCloseTag: (localName, _tag, result, ctx, location) => {
27217
+ if (location === "in-interaction" && localName === "prompt" && ctx.capture === "prompt") {
27218
+ result.promptText = ctx.flushBuffer("prompt");
27219
+ ctx.capture = null;
27220
+ return true;
27221
+ }
27222
+ }
27223
+ });
27224
+ //#endregion
27201
27225
  //#region src/qti-to-learnosity/convertDrawingToLearnosity.ts
27202
27226
  /**
27203
27227
  * Build drawing stimulus from parsed QTI data
@@ -28672,14 +28696,6 @@ function parseMetadataTags(metadataXml, options = {}) {
28672
28696
  }
28673
28697
  //#endregion
28674
28698
  //#region src/qti-parser/readAssessmentItemUpload.ts
28675
- /**
28676
- * Read QTI uploadInteraction using a streaming parser.
28677
- *
28678
- * The Learnosity -> QTI serializer carries the fileupload allow-list on the
28679
- * interaction's `class` attribute as `lrn-allow-<suffix>` tokens and the file
28680
- * limit as `maxChoices` (QTI has no standard field for either). This reader
28681
- * recovers both, plus the stimulus HTML, so a fileupload item round-trips.
28682
- */
28683
28699
  /** Parse `lrn-allow-<suffix>` tokens from a class attribute into allow_* suffixes. */
28684
28700
  function parseAllowedFileTypes(classAttr) {
28685
28701
  if (!classAttr) return [];
@@ -28697,69 +28713,29 @@ function extractStimulus(inputXml) {
28697
28713
  const stimulus = itemBodyMatch[1].replace(/<uploadInteraction[\s\S]*?(?:\/>|<\/uploadInteraction>)/gi, "").trim();
28698
28714
  return stimulus.length > 0 ? stimulus : void 0;
28699
28715
  }
28700
- function readAssessmentItemUpload(inputXml) {
28701
- const diagnostics = [];
28702
- const result = {
28716
+ const _baseReader = createAssessmentItemReader({
28717
+ interactionName: "uploadInteraction",
28718
+ createResult: (diagnostics) => ({
28703
28719
  allowedFileTypes: [],
28704
28720
  diagnostics
28705
- };
28706
- const parser = new import_saxes.SaxesParser();
28707
- let foundAssessmentItem = false;
28708
- let foundItemBody = false;
28709
- let uploadInteractionCount = 0;
28710
- parser.on("opentag", (tag) => {
28711
- const localName = tag.local ?? tag.name;
28712
- if (localName === "assessmentItem") {
28713
- foundAssessmentItem = true;
28714
- result.identifier = getAttr$5(tag, "identifier");
28715
- result.title = getAttr$5(tag, "title") ?? null;
28716
- } else if (localName === "itemBody") foundItemBody = true;
28717
- else if (localName === "uploadInteraction") {
28718
- uploadInteractionCount++;
28719
- result.responseIdentifier = getAttr$5(tag, "responseIdentifier");
28720
- result.allowedFileTypes = parseAllowedFileTypes(getAttr$5(tag, "class"));
28721
- const maxChoices = getAttr$5(tag, "maxChoices");
28722
- if (maxChoices) {
28723
- const parsed = parseInt(maxChoices, 10);
28724
- if (!Number.isNaN(parsed)) result.maxFiles = parsed;
28725
- }
28721
+ }),
28722
+ onInteractionOpen: (tag, result, ctx) => {
28723
+ result.responseIdentifier = ctx.getAttr(tag, "responseIdentifier");
28724
+ result.allowedFileTypes = parseAllowedFileTypes(getAttr$5(tag, "class"));
28725
+ const maxChoices = ctx.getAttr(tag, "maxChoices");
28726
+ if (maxChoices) {
28727
+ const parsed = parseInt(maxChoices, 10);
28728
+ if (!Number.isNaN(parsed)) result.maxFiles = parsed;
28726
28729
  }
28727
- });
28728
- try {
28729
- parser.write(inputXml).close();
28730
- } catch (error) {
28731
- diagnostics.push({
28732
- code: DiagnosticCode.LRN_SCHEMA_INVALID,
28733
- level: "error",
28734
- file: "assessmentItem",
28735
- message: `Failed to parse QTI XML: ${error instanceof Error ? error.message : String(error)}`
28736
- });
28737
28730
  }
28738
- if (!foundAssessmentItem) diagnostics.push({
28739
- code: DiagnosticCode.LRN_SCHEMA_INVALID,
28740
- level: "error",
28741
- file: "assessmentItem",
28742
- message: "No assessmentItem element found"
28743
- });
28744
- if (!foundItemBody) diagnostics.push({
28745
- code: DiagnosticCode.LRN_SCHEMA_INVALID,
28746
- level: "error",
28747
- file: "assessmentItem",
28748
- message: "No itemBody element found"
28749
- });
28750
- if (foundItemBody && uploadInteractionCount === 0) diagnostics.push({
28751
- code: DiagnosticCode.QTI_UNSUPPORTED_INTERACTION,
28752
- level: "error",
28753
- file: "assessmentItem.itemBody",
28754
- message: "No uploadInteraction found in itemBody"
28755
- });
28756
- if (uploadInteractionCount > 1) diagnostics.push({
28757
- code: DiagnosticCode.QTI_MULTIPLE_INTERACTIONS,
28758
- level: "error",
28759
- file: "assessmentItem.itemBody",
28760
- message: `Multiple uploadInteraction elements found (${uploadInteractionCount}), only single interaction supported`
28761
- });
28762
- if (diagnostics.every((d) => d.level !== "error")) result.stimulus = extractStimulus(inputXml);
28731
+ });
28732
+ /**
28733
+ * Read uploadInteraction data from QTI assessmentItem XML.
28734
+ * Wraps the base reader to add post-parse stimulus extraction from raw XML.
28735
+ */
28736
+ function readAssessmentItemUpload(inputXml) {
28737
+ const result = _baseReader(inputXml);
28738
+ if (result.diagnostics.every((d) => d.level !== "error")) result.stimulus = extractStimulus(inputXml);
28763
28739
  return result;
28764
28740
  }
28765
28741
  //#endregion