@learnosity/lib-qti-converter 0.12.5 → 0.12.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (19) hide show
  1. package/package.json +1 -1
  2. package/packages/cli/dist/{learnosityToQti-D3Xxswaf.js → learnosityToQti-CXDJoJpl.js} +3 -3
  3. package/packages/cli/dist/{learnosityToQti-D3Xxswaf.js.map → learnosityToQti-CXDJoJpl.js.map} +1 -1
  4. package/packages/cli/dist/{learnosityToQtiPackage-D-hlZ9yI.js → learnosityToQtiPackage-7v-s4ybk.js} +2 -2
  5. package/packages/cli/dist/{learnosityToQtiPackage-D-hlZ9yI.js.map → learnosityToQtiPackage-7v-s4ybk.js.map} +1 -1
  6. package/packages/cli/dist/main.js +4 -4
  7. package/packages/cli/dist/{pathUtils-CwZnJ55s.js → pathUtils-CgoDQyJk.js} +2 -1
  8. package/packages/cli/dist/pathUtils-CgoDQyJk.js.map +1 -0
  9. package/packages/cli/dist/{qtiPackageToLearnosity-CHDga9lI.js → qtiPackageToLearnosity-hT4QZ7YX.js} +2 -2
  10. package/packages/cli/dist/{qtiPackageToLearnosity-CHDga9lI.js.map → qtiPackageToLearnosity-hT4QZ7YX.js.map} +1 -1
  11. package/packages/cli/dist/{qtiToLearnosity-BkYlsuH0.js → qtiToLearnosity-BU7mHhIu.js} +3 -3
  12. package/packages/cli/dist/{qtiToLearnosity-BkYlsuH0.js.map → qtiToLearnosity-BU7mHhIu.js.map} +1 -1
  13. package/packages/cli/dist/{resolveOptions-CZ4dw2gp.js → resolveOptions-ehwJSkQy.js} +25 -9
  14. package/packages/cli/dist/{resolveOptions-CZ4dw2gp.js.map → resolveOptions-ehwJSkQy.js.map} +1 -1
  15. package/packages/core/dist/index.d.ts +24 -0
  16. package/packages/core/dist/index.js +54 -9
  17. package/packages/core/dist/node.d.ts +24 -0
  18. package/packages/core/dist/node.js +246 -13
  19. package/packages/cli/dist/pathUtils-CwZnJ55s.js.map +0 -1
@@ -21,6 +21,28 @@ interface FileAccess {
21
21
  * LOM XML, or `undefined` if none is found (or without filesystem access).
22
22
  */
23
23
  findAndReadLomFile(itemPath: string): string | undefined;
24
+ /**
25
+ * Resolve an image referenced by an `<object data="...">` / `<img src="...">`
26
+ * tag and read its intrinsic pixel dimensions from the file header.
27
+ *
28
+ * Used when the QTI markup omits `width`/`height` attributes (which is valid
29
+ * QTI) but a downstream Learnosity question needs pixel dimensions (e.g.
30
+ * hotspot coordinate conversion). Returns `undefined` if the file cannot be
31
+ * resolved/read, the format is unsupported, or when running without
32
+ * filesystem access.
33
+ *
34
+ * `packageRoot`, when provided, is the directory that bounds every read: any
35
+ * asset inside the package resolves regardless of directory naming, while
36
+ * paths escaping the package are rejected. When omitted, reads are confined
37
+ * to the source item's own directory.
38
+ *
39
+ * Optional so in-memory/browser callers and existing implementations that
40
+ * predate it remain valid.
41
+ */
42
+ resolveImageDimensions?: (_objectDataPath: string, _sourceFile?: string, _packageRoot?: string) => {
43
+ width: number;
44
+ height: number;
45
+ } | undefined;
24
46
  }
25
47
  //#endregion
26
48
  //#region src/types.d.ts
@@ -166,6 +188,7 @@ declare const DiagnosticCode: {
166
188
  readonly QTI_PACKAGE_MULTIPLE_TESTS: "QTI_PACKAGE_MULTIPLE_TESTS";
167
189
  readonly QTI_PACKAGE_NO_CONTENT: "QTI_PACKAGE_NO_CONTENT";
168
190
  readonly QTI_PACKAGE_DUPLICATE_IDENTIFIER: "QTI_PACKAGE_DUPLICATE_IDENTIFIER";
191
+ readonly QTI_ITEM_CONVERSION_FAILED: "QTI_ITEM_CONVERSION_FAILED";
169
192
  readonly LRN_BUNDLE_WRITTEN: "LRN_BUNDLE_WRITTEN";
170
193
  readonly QTI_DOWNGRADE_NAMESPACE: "QTI_DOWNGRADE_NAMESPACE";
171
194
  readonly QTI_DOWNGRADE_SCHEMA_LOCATION: "QTI_DOWNGRADE_SCHEMA_LOCATION";
@@ -2541,6 +2564,7 @@ interface ConvertOptions {
2541
2564
  itemReferenceSource?: ItemReferenceSource;
2542
2565
  truncateTags?: boolean;
2543
2566
  fileAccess?: FileAccess;
2567
+ packageRoot?: string;
2544
2568
  }
2545
2569
  /**
2546
2570
  * Convert QTI assessmentItem XML to Learnosity Item JSON
@@ -173,6 +173,7 @@ const DiagnosticCode = {
173
173
  QTI_PACKAGE_MULTIPLE_TESTS: "QTI_PACKAGE_MULTIPLE_TESTS",
174
174
  QTI_PACKAGE_NO_CONTENT: "QTI_PACKAGE_NO_CONTENT",
175
175
  QTI_PACKAGE_DUPLICATE_IDENTIFIER: "QTI_PACKAGE_DUPLICATE_IDENTIFIER",
176
+ QTI_ITEM_CONVERSION_FAILED: "QTI_ITEM_CONVERSION_FAILED",
176
177
  LRN_BUNDLE_WRITTEN: "LRN_BUNDLE_WRITTEN",
177
178
  QTI_DOWNGRADE_NAMESPACE: "QTI_DOWNGRADE_NAMESPACE",
178
179
  QTI_DOWNGRADE_SCHEMA_LOCATION: "QTI_DOWNGRADE_SCHEMA_LOCATION",
@@ -24902,13 +24903,28 @@ function buildHotspotValidation(correctResponses, choiceIdToIndex) {
24902
24903
  /**
24903
24904
  * Convert hotspotInteraction to Learnosity hotspot
24904
24905
  */
24905
- function convertHotspotToLearnosity(inputXml, diagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource) {
24906
+ function convertHotspotToLearnosity(inputXml, diagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, fileAccess, packageRoot) {
24906
24907
  const parsed = readAssessmentItemHotspot(inputXml);
24907
24908
  const updatedDiagnostics = addFileContextIfProvided(parsed.diagnostics, sourceFile);
24908
24909
  diagnostics.push(...updatedDiagnostics);
24909
24910
  if (hasErrors(diagnostics)) return { diagnostics };
24910
- if (!parsed.identifier || !parsed.responseIdentifier || !parsed.imageSource || !parsed.imageWidth || !parsed.imageHeight) return { diagnostics };
24911
+ if (!parsed.identifier || !parsed.responseIdentifier || !parsed.imageSource) return { diagnostics };
24911
24912
  if (parsed.hotspotChoices.length === 0) return { diagnostics };
24913
+ if (!parsed.imageWidth || !parsed.imageHeight) {
24914
+ const resolved = fileAccess?.resolveImageDimensions?.(parsed.imageSource, sourceFile, packageRoot);
24915
+ if (resolved) {
24916
+ parsed.imageWidth = parsed.imageWidth || resolved.width;
24917
+ parsed.imageHeight = parsed.imageHeight || resolved.height;
24918
+ } else {
24919
+ diagnostics.push({
24920
+ code: DiagnosticCode.LRN_MISSING_REQUIRED_FIELD,
24921
+ level: "error",
24922
+ file: sourceFile ?? "hotspotInteraction",
24923
+ message: `hotspotInteraction image "${parsed.imageSource}" is missing width/height attributes and its dimensions could not be determined from the image file. Hotspot coordinate conversion requires image dimensions.`
24924
+ });
24925
+ return { diagnostics };
24926
+ }
24927
+ }
24912
24928
  const itemRef = generateItemReference(parsed.identifier, itemReferenceSource, sourceFile);
24913
24929
  const questionRef = md5hex(itemRef);
24914
24930
  const stimulus = buildHotspotStimulus(parsed, organisationId, mediaVault, customAssetUrl);
@@ -28013,6 +28029,7 @@ function computeClosingTags(html) {
28013
28029
  */
28014
28030
  function getConverterMap() {
28015
28031
  const wrapSourceFirst = (fn) => (xml, diag, tags, orgId, sf, mv, cau, irs, stimuli) => fn(xml, diag, tags, sf, orgId, mv, cau, irs, stimuli);
28032
+ const hotspotConverter = (xml, diag, tags, orgId, sf, mv, cau, irs, _stimuli, fileAccess, packageRoot) => convertHotspotToLearnosity(xml, diag, tags, orgId, sf, mv, cau, irs, fileAccess, packageRoot);
28016
28033
  return {
28017
28034
  choiceInteraction: wrapSourceFirst(convertMcqToLearnosity),
28018
28035
  textEntryInteraction: wrapSourceFirst(convertTextEntryToLearnosity),
@@ -28021,7 +28038,7 @@ function getConverterMap() {
28021
28038
  gapMatchInteraction: convertGapMatchToLearnosity,
28022
28039
  inlineChoiceInteraction: convertInlineChoiceToLearnosity,
28023
28040
  orderInteraction: convertOrderToLearnosity,
28024
- hotspotInteraction: convertHotspotToLearnosity,
28041
+ hotspotInteraction: hotspotConverter,
28025
28042
  hottextInteraction: convertHottextToLearnosity,
28026
28043
  graphicGapMatchInteraction: convertGraphicGapMatchToLearnosity,
28027
28044
  drawingInteraction: convertDrawingToLearnosity,
@@ -28035,7 +28052,7 @@ const CONVERTER_MAP = getConverterMap();
28035
28052
  * Strategy: Call each individual converter for its interaction type,
28036
28053
  * then combine all questions and widgets into a single composite item
28037
28054
  */
28038
- function convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, fileAccess) {
28055
+ function convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, fileAccess, packageRoot) {
28039
28056
  const metadata = extractItemMetadata(inputXml);
28040
28057
  const { segments } = segmentItemBodyByInteractions(inputXml);
28041
28058
  const stimuliByType = /* @__PURE__ */ new Map();
@@ -28252,7 +28269,7 @@ function convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, or
28252
28269
  continue;
28253
28270
  }
28254
28271
  const converter = CONVERTER_MAP[group.type];
28255
- if (converter) processResult(converter(inputXml, subDiagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, group.stimuli), subDiagnostics);
28272
+ if (converter) processResult(converter(inputXml, subDiagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, group.stimuli, fileAccess, packageRoot), subDiagnostics);
28256
28273
  }
28257
28274
  if (allQuestions.length === 0) {
28258
28275
  diagnostics.push({
@@ -28889,7 +28906,7 @@ const SINGLE_INTERACTION_CONVERTERS = /* @__PURE__ */ new Map([
28889
28906
  ["matchInteraction", (xml, diag, lom, opts) => convertMatchToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28890
28907
  ["gapMatchInteraction", (xml, diag, lom, opts) => convertGapMatchToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28891
28908
  ["orderInteraction", (xml, diag, lom, opts) => convertOrderToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28892
- ["hotspotInteraction", (xml, diag, lom, opts) => convertHotspotToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28909
+ ["hotspotInteraction", (xml, diag, lom, opts) => convertHotspotToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource, opts.fileAccess, opts.packageRoot)],
28893
28910
  ["hottextInteraction", (xml, diag, lom, opts) => convertHottextToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28894
28911
  ["graphicGapMatchInteraction", (xml, diag, lom, opts) => convertGraphicGapMatchToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28895
28912
  ["drawingInteraction", (xml, diag, lom, opts) => convertDrawingToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
@@ -29017,7 +29034,7 @@ function convertQtiAssessmentItemToLearnosity(inputXml, opts = {}) {
29017
29034
  const { counts, allInteractions } = countAllInteractions(inputXml);
29018
29035
  const presentTypes = INTERACTION_NAMES.filter((name) => counts[name] > 0);
29019
29036
  if (presentTypes.reduce((sum, name) => sum + counts[name], 0) === 0) return handleNoInteractions(inputXml, diagnostics, allInteractions, lomTags, opts);
29020
- if (presentTypes.length > 1) return convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource, opts.fileAccess);
29037
+ if (presentTypes.length > 1) return convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource, opts.fileAccess, opts.packageRoot);
29021
29038
  return dispatchSingleInteractionConverter(presentTypes[0], inputXml, counts[presentTypes[0]], diagnostics, lomTags, opts);
29022
29039
  } catch (error) {
29023
29040
  diagnostics.push({
@@ -30856,6 +30873,30 @@ function posixJoin(dir, rel) {
30856
30873
  return `${dir.replace(/\/+$/, "")}/${rel.replace(/^\.?\//, "")}`;
30857
30874
  }
30858
30875
  /**
30876
+ * Guarantee that a failed item conversion is represented by at least one
30877
+ * error-level diagnostic keyed to the item's href.
30878
+ *
30879
+ * Some converters can return without an item AND without pushing an error
30880
+ * (silent early-returns), which previously left the diagnostics report showing a
30881
+ * non-zero failed count with an empty errors array. When the per-item result
30882
+ * carried no error, this appends a synthetic {@link DiagnosticCode.QTI_ITEM_CONVERSION_FAILED}
30883
+ * so the failed count and the errors list can never diverge.
30884
+ *
30885
+ * @param itemDiagnostics - diagnostics produced for the single failed item
30886
+ * @param href - the item's manifest href (used as the diagnostic `file`)
30887
+ * @returns the diagnostics to append (either empty, when a real error already
30888
+ * exists, or a one-element array with the synthetic fallback)
30889
+ */
30890
+ function ensureFailedItemDiagnostic(itemDiagnostics, href) {
30891
+ if (itemDiagnostics.some((d) => d.level === "error")) return [];
30892
+ return [{
30893
+ code: DiagnosticCode.QTI_ITEM_CONVERSION_FAILED,
30894
+ level: "error",
30895
+ file: href,
30896
+ message: `Item conversion failed for "${href}" but produced no specific error diagnostic. The item may use an unsupported interaction or be missing required data.`
30897
+ }];
30898
+ }
30899
+ /**
30859
30900
  * Normalize a forward-slash relative path (collapse `.`/`//`, resolve `..`).
30860
30901
  * Pure replacement for `node:path`'s `normalize(...).split(sep).join('/')` on
30861
30902
  * the forward-slash paths used as entry keys.
@@ -31081,7 +31122,8 @@ function convertQtiPackageToLearnosityBundle(input, opts) {
31081
31122
  mediaVault: opts?.mediaVault,
31082
31123
  customAssetUrl: opts?.customAssetUrl,
31083
31124
  itemReferenceSource: opts?.itemReferenceSource,
31084
- fileAccess: opts?.fileAccess
31125
+ fileAccess: opts?.fileAccess,
31126
+ packageRoot: opts?.sourceDir
31085
31127
  });
31086
31128
  diagnostics.push(...itemResult.diagnostics);
31087
31129
  if (itemResult.item) {
@@ -31131,7 +31173,10 @@ function convertQtiPackageToLearnosityBundle(input, opts) {
31131
31173
  if (!qtiRefToMd5Refs.has(itemResource.identifier)) qtiRefToMd5Refs.set(itemResource.identifier, []);
31132
31174
  qtiRefToMd5Refs.get(itemResource.identifier)?.push(md5Ref);
31133
31175
  convertedSourceFiles.push(itemResource.href);
31134
- } else failedSourceFiles.push(itemResource.href);
31176
+ } else {
31177
+ failedSourceFiles.push(itemResource.href);
31178
+ diagnostics.push(...ensureFailedItemDiagnostic(itemResult.diagnostics, itemResource.href));
31179
+ }
31135
31180
  }
31136
31181
  if (items.size === 0 && itemResourcesToConvert.length > 0) {
31137
31182
  diagnostics.push({
@@ -21,6 +21,28 @@ interface FileAccess {
21
21
  * LOM XML, or `undefined` if none is found (or without filesystem access).
22
22
  */
23
23
  findAndReadLomFile(itemPath: string): string | undefined;
24
+ /**
25
+ * Resolve an image referenced by an `<object data="...">` / `<img src="...">`
26
+ * tag and read its intrinsic pixel dimensions from the file header.
27
+ *
28
+ * Used when the QTI markup omits `width`/`height` attributes (which is valid
29
+ * QTI) but a downstream Learnosity question needs pixel dimensions (e.g.
30
+ * hotspot coordinate conversion). Returns `undefined` if the file cannot be
31
+ * resolved/read, the format is unsupported, or when running without
32
+ * filesystem access.
33
+ *
34
+ * `packageRoot`, when provided, is the directory that bounds every read: any
35
+ * asset inside the package resolves regardless of directory naming, while
36
+ * paths escaping the package are rejected. When omitted, reads are confined
37
+ * to the source item's own directory.
38
+ *
39
+ * Optional so in-memory/browser callers and existing implementations that
40
+ * predate it remain valid.
41
+ */
42
+ resolveImageDimensions?: (_objectDataPath: string, _sourceFile?: string, _packageRoot?: string) => {
43
+ width: number;
44
+ height: number;
45
+ } | undefined;
24
46
  }
25
47
  //#endregion
26
48
  //#region src/types.d.ts
@@ -166,6 +188,7 @@ declare const DiagnosticCode: {
166
188
  readonly QTI_PACKAGE_MULTIPLE_TESTS: "QTI_PACKAGE_MULTIPLE_TESTS";
167
189
  readonly QTI_PACKAGE_NO_CONTENT: "QTI_PACKAGE_NO_CONTENT";
168
190
  readonly QTI_PACKAGE_DUPLICATE_IDENTIFIER: "QTI_PACKAGE_DUPLICATE_IDENTIFIER";
191
+ readonly QTI_ITEM_CONVERSION_FAILED: "QTI_ITEM_CONVERSION_FAILED";
169
192
  readonly LRN_BUNDLE_WRITTEN: "LRN_BUNDLE_WRITTEN";
170
193
  readonly QTI_DOWNGRADE_NAMESPACE: "QTI_DOWNGRADE_NAMESPACE";
171
194
  readonly QTI_DOWNGRADE_SCHEMA_LOCATION: "QTI_DOWNGRADE_SCHEMA_LOCATION";
@@ -2549,6 +2572,7 @@ interface ConvertOptions {
2549
2572
  itemReferenceSource?: ItemReferenceSource;
2550
2573
  truncateTags?: boolean;
2551
2574
  fileAccess?: FileAccess;
2575
+ packageRoot?: string;
2552
2576
  }
2553
2577
  /**
2554
2578
  * Convert QTI assessmentItem XML to Learnosity Item JSON
@@ -1,6 +1,6 @@
1
1
  import { createRequire } from "node:module";
2
2
  import { createRequire as createRequire$1 } from "module";
3
- import { existsSync, readFileSync } from "fs";
3
+ import { closeSync, existsSync, openSync, readFileSync, readSync, realpathSync } from "fs";
4
4
  import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "path";
5
5
  import * as fs from "node:fs";
6
6
  import { existsSync as existsSync$1, readFileSync as readFileSync$1, readdirSync, statSync, writeFileSync } from "node:fs";
@@ -180,6 +180,7 @@ const DiagnosticCode = {
180
180
  QTI_PACKAGE_MULTIPLE_TESTS: "QTI_PACKAGE_MULTIPLE_TESTS",
181
181
  QTI_PACKAGE_NO_CONTENT: "QTI_PACKAGE_NO_CONTENT",
182
182
  QTI_PACKAGE_DUPLICATE_IDENTIFIER: "QTI_PACKAGE_DUPLICATE_IDENTIFIER",
183
+ QTI_ITEM_CONVERSION_FAILED: "QTI_ITEM_CONVERSION_FAILED",
183
184
  LRN_BUNDLE_WRITTEN: "LRN_BUNDLE_WRITTEN",
184
185
  QTI_DOWNGRADE_NAMESPACE: "QTI_DOWNGRADE_NAMESPACE",
185
186
  QTI_DOWNGRADE_SCHEMA_LOCATION: "QTI_DOWNGRADE_SCHEMA_LOCATION",
@@ -24909,13 +24910,28 @@ function buildHotspotValidation(correctResponses, choiceIdToIndex) {
24909
24910
  /**
24910
24911
  * Convert hotspotInteraction to Learnosity hotspot
24911
24912
  */
24912
- function convertHotspotToLearnosity(inputXml, diagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource) {
24913
+ function convertHotspotToLearnosity(inputXml, diagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, fileAccess, packageRoot) {
24913
24914
  const parsed = readAssessmentItemHotspot(inputXml);
24914
24915
  const updatedDiagnostics = addFileContextIfProvided(parsed.diagnostics, sourceFile);
24915
24916
  diagnostics.push(...updatedDiagnostics);
24916
24917
  if (hasErrors(diagnostics)) return { diagnostics };
24917
- if (!parsed.identifier || !parsed.responseIdentifier || !parsed.imageSource || !parsed.imageWidth || !parsed.imageHeight) return { diagnostics };
24918
+ if (!parsed.identifier || !parsed.responseIdentifier || !parsed.imageSource) return { diagnostics };
24918
24919
  if (parsed.hotspotChoices.length === 0) return { diagnostics };
24920
+ if (!parsed.imageWidth || !parsed.imageHeight) {
24921
+ const resolved = fileAccess?.resolveImageDimensions?.(parsed.imageSource, sourceFile, packageRoot);
24922
+ if (resolved) {
24923
+ parsed.imageWidth = parsed.imageWidth || resolved.width;
24924
+ parsed.imageHeight = parsed.imageHeight || resolved.height;
24925
+ } else {
24926
+ diagnostics.push({
24927
+ code: DiagnosticCode.LRN_MISSING_REQUIRED_FIELD,
24928
+ level: "error",
24929
+ file: sourceFile ?? "hotspotInteraction",
24930
+ message: `hotspotInteraction image "${parsed.imageSource}" is missing width/height attributes and its dimensions could not be determined from the image file. Hotspot coordinate conversion requires image dimensions.`
24931
+ });
24932
+ return { diagnostics };
24933
+ }
24934
+ }
24919
24935
  const itemRef = generateItemReference(parsed.identifier, itemReferenceSource, sourceFile);
24920
24936
  const questionRef = md5hex(itemRef);
24921
24937
  const stimulus = buildHotspotStimulus(parsed, organisationId, mediaVault, customAssetUrl);
@@ -28020,6 +28036,7 @@ function computeClosingTags(html) {
28020
28036
  */
28021
28037
  function getConverterMap() {
28022
28038
  const wrapSourceFirst = (fn) => (xml, diag, tags, orgId, sf, mv, cau, irs, stimuli) => fn(xml, diag, tags, sf, orgId, mv, cau, irs, stimuli);
28039
+ const hotspotConverter = (xml, diag, tags, orgId, sf, mv, cau, irs, _stimuli, fileAccess, packageRoot) => convertHotspotToLearnosity(xml, diag, tags, orgId, sf, mv, cau, irs, fileAccess, packageRoot);
28023
28040
  return {
28024
28041
  choiceInteraction: wrapSourceFirst(convertMcqToLearnosity),
28025
28042
  textEntryInteraction: wrapSourceFirst(convertTextEntryToLearnosity),
@@ -28028,7 +28045,7 @@ function getConverterMap() {
28028
28045
  gapMatchInteraction: convertGapMatchToLearnosity,
28029
28046
  inlineChoiceInteraction: convertInlineChoiceToLearnosity,
28030
28047
  orderInteraction: convertOrderToLearnosity,
28031
- hotspotInteraction: convertHotspotToLearnosity,
28048
+ hotspotInteraction: hotspotConverter,
28032
28049
  hottextInteraction: convertHottextToLearnosity,
28033
28050
  graphicGapMatchInteraction: convertGraphicGapMatchToLearnosity,
28034
28051
  drawingInteraction: convertDrawingToLearnosity,
@@ -28042,7 +28059,7 @@ const CONVERTER_MAP = getConverterMap();
28042
28059
  * Strategy: Call each individual converter for its interaction type,
28043
28060
  * then combine all questions and widgets into a single composite item
28044
28061
  */
28045
- function convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, fileAccess) {
28062
+ function convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, fileAccess, packageRoot) {
28046
28063
  const metadata = extractItemMetadata(inputXml);
28047
28064
  const { segments } = segmentItemBodyByInteractions(inputXml);
28048
28065
  const stimuliByType = /* @__PURE__ */ new Map();
@@ -28259,7 +28276,7 @@ function convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, or
28259
28276
  continue;
28260
28277
  }
28261
28278
  const converter = CONVERTER_MAP[group.type];
28262
- if (converter) processResult(converter(inputXml, subDiagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, group.stimuli), subDiagnostics);
28279
+ if (converter) processResult(converter(inputXml, subDiagnostics, lomTags, organisationId, sourceFile, mediaVault, customAssetUrl, itemReferenceSource, group.stimuli, fileAccess, packageRoot), subDiagnostics);
28263
28280
  }
28264
28281
  if (allQuestions.length === 0) {
28265
28282
  diagnostics.push({
@@ -28896,7 +28913,7 @@ const SINGLE_INTERACTION_CONVERTERS = /* @__PURE__ */ new Map([
28896
28913
  ["matchInteraction", (xml, diag, lom, opts) => convertMatchToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28897
28914
  ["gapMatchInteraction", (xml, diag, lom, opts) => convertGapMatchToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28898
28915
  ["orderInteraction", (xml, diag, lom, opts) => convertOrderToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28899
- ["hotspotInteraction", (xml, diag, lom, opts) => convertHotspotToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28916
+ ["hotspotInteraction", (xml, diag, lom, opts) => convertHotspotToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource, opts.fileAccess, opts.packageRoot)],
28900
28917
  ["hottextInteraction", (xml, diag, lom, opts) => convertHottextToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28901
28918
  ["graphicGapMatchInteraction", (xml, diag, lom, opts) => convertGraphicGapMatchToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
28902
28919
  ["drawingInteraction", (xml, diag, lom, opts) => convertDrawingToLearnosity(xml, diag, lom, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource)],
@@ -29024,7 +29041,7 @@ function convertQtiAssessmentItemToLearnosity(inputXml, opts = {}) {
29024
29041
  const { counts, allInteractions } = countAllInteractions(inputXml);
29025
29042
  const presentTypes = INTERACTION_NAMES.filter((name) => counts[name] > 0);
29026
29043
  if (presentTypes.reduce((sum, name) => sum + counts[name], 0) === 0) return handleNoInteractions(inputXml, diagnostics, allInteractions, lomTags, opts);
29027
- if (presentTypes.length > 1) return convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource, opts.fileAccess);
29044
+ if (presentTypes.length > 1) return convertMixedInteractionsToLearnosity(inputXml, diagnostics, lomTags, opts.organisationId, opts.sourceFile, opts.mediaVault, opts.customAssetUrl, opts.itemReferenceSource, opts.fileAccess, opts.packageRoot);
29028
29045
  return dispatchSingleInteractionConverter(presentTypes[0], inputXml, counts[presentTypes[0]], diagnostics, lomTags, opts);
29029
29046
  } catch (error) {
29030
29047
  diagnostics.push({
@@ -31388,6 +31405,30 @@ function posixJoin(dir, rel) {
31388
31405
  return `${dir.replace(/\/+$/, "")}/${rel.replace(/^\.?\//, "")}`;
31389
31406
  }
31390
31407
  /**
31408
+ * Guarantee that a failed item conversion is represented by at least one
31409
+ * error-level diagnostic keyed to the item's href.
31410
+ *
31411
+ * Some converters can return without an item AND without pushing an error
31412
+ * (silent early-returns), which previously left the diagnostics report showing a
31413
+ * non-zero failed count with an empty errors array. When the per-item result
31414
+ * carried no error, this appends a synthetic {@link DiagnosticCode.QTI_ITEM_CONVERSION_FAILED}
31415
+ * so the failed count and the errors list can never diverge.
31416
+ *
31417
+ * @param itemDiagnostics - diagnostics produced for the single failed item
31418
+ * @param href - the item's manifest href (used as the diagnostic `file`)
31419
+ * @returns the diagnostics to append (either empty, when a real error already
31420
+ * exists, or a one-element array with the synthetic fallback)
31421
+ */
31422
+ function ensureFailedItemDiagnostic(itemDiagnostics, href) {
31423
+ if (itemDiagnostics.some((d) => d.level === "error")) return [];
31424
+ return [{
31425
+ code: DiagnosticCode.QTI_ITEM_CONVERSION_FAILED,
31426
+ level: "error",
31427
+ file: href,
31428
+ message: `Item conversion failed for "${href}" but produced no specific error diagnostic. The item may use an unsupported interaction or be missing required data.`
31429
+ }];
31430
+ }
31431
+ /**
31391
31432
  * Normalize a forward-slash relative path (collapse `.`/`//`, resolve `..`).
31392
31433
  * Pure replacement for `node:path`'s `normalize(...).split(sep).join('/')` on
31393
31434
  * the forward-slash paths used as entry keys.
@@ -31613,7 +31654,8 @@ function convertQtiPackageToLearnosityBundle(input, opts) {
31613
31654
  mediaVault: opts?.mediaVault,
31614
31655
  customAssetUrl: opts?.customAssetUrl,
31615
31656
  itemReferenceSource: opts?.itemReferenceSource,
31616
- fileAccess: opts?.fileAccess
31657
+ fileAccess: opts?.fileAccess,
31658
+ packageRoot: opts?.sourceDir
31617
31659
  });
31618
31660
  diagnostics.push(...itemResult.diagnostics);
31619
31661
  if (itemResult.item) {
@@ -31663,7 +31705,10 @@ function convertQtiPackageToLearnosityBundle(input, opts) {
31663
31705
  if (!qtiRefToMd5Refs.has(itemResource.identifier)) qtiRefToMd5Refs.set(itemResource.identifier, []);
31664
31706
  qtiRefToMd5Refs.get(itemResource.identifier)?.push(md5Ref);
31665
31707
  convertedSourceFiles.push(itemResource.href);
31666
- } else failedSourceFiles.push(itemResource.href);
31708
+ } else {
31709
+ failedSourceFiles.push(itemResource.href);
31710
+ diagnostics.push(...ensureFailedItemDiagnostic(itemResult.diagnostics, itemResource.href));
31711
+ }
31667
31712
  }
31668
31713
  if (items.size === 0 && itemResourcesToConvert.length > 0) {
31669
31714
  diagnostics.push({
@@ -31846,7 +31891,7 @@ function convertQtiPackageToLearnosityBundle(input, opts) {
31846
31891
  * @param root - Directory the path must stay within
31847
31892
  * @returns Absolute path if contained by root, undefined otherwise
31848
31893
  */
31849
- function containedPath(candidate, root) {
31894
+ function containedPath$1(candidate, root) {
31850
31895
  const resolvedRoot = resolve(root);
31851
31896
  const resolvedCandidate = resolve(candidate);
31852
31897
  const rel = relative(resolvedRoot, resolvedCandidate);
@@ -31871,7 +31916,7 @@ function resolvePassageFile(objectDataPath, sourceFile) {
31871
31916
  candidates.push(join(root, objectDataPath));
31872
31917
  }
31873
31918
  for (const candidate of candidates) {
31874
- const passagePath = containedPath(candidate, root);
31919
+ const passagePath = containedPath$1(candidate, root);
31875
31920
  if (!passagePath || !existsSync(passagePath)) continue;
31876
31921
  try {
31877
31922
  return readFileSync(passagePath, "utf-8").replace(/^\uFEFF/, "");
@@ -31922,10 +31967,198 @@ function findAndReadLomFile(itemPath) {
31922
31967
  }
31923
31968
  }
31924
31969
  //#endregion
31970
+ //#region src/qti-to-learnosity/utils/imageDimensions.ts
31971
+ /**
31972
+ * Image dimension resolver
31973
+ *
31974
+ * Resolves an image file referenced by a QTI `<object data="...">` /
31975
+ * `<img src="...">` tag and reads its intrinsic pixel dimensions directly from
31976
+ * the file header. This is a dependency-free reader for the formats commonly
31977
+ * found in QTI packages (GIF, PNG, JPEG). It reads only the small header region
31978
+ * needed to determine dimensions, not the whole file.
31979
+ *
31980
+ * QTI legitimately allows an `<object>` without width/height attributes, but
31981
+ * some Learnosity question types (e.g. hotspot) need pixel dimensions to convert
31982
+ * absolute coordinates into percentages. When the markup omits them, the
31983
+ * dimensions are recovered from the referenced image file.
31984
+ */
31985
+ /**
31986
+ * Only the leading bytes of an image are needed to read its dimensions: GIF and
31987
+ * PNG carry them in the first ~24 bytes, and a JPEG's first Start-Of-Frame
31988
+ * marker is virtually always within the first segments. Cap the read so a huge
31989
+ * asset can never be loaded whole into memory.
31990
+ */
31991
+ const MAX_HEADER_BYTES = 65536;
31992
+ /**
31993
+ * Check that `resolvedCandidate` stays lexically inside `resolvedRoot`.
31994
+ *
31995
+ * Both arguments must already be absolute/normalised. Returns true when the
31996
+ * candidate is contained (and is not the root itself).
31997
+ */
31998
+ function isContained(resolvedCandidate, resolvedRoot) {
31999
+ const rel = relative(resolvedRoot, resolvedCandidate);
32000
+ return rel !== "" && !isAbsolute(rel) && rel.split(sep)[0] !== "..";
32001
+ }
32002
+ /**
32003
+ * Resolve `candidate` and confirm it stays inside `root`, both lexically and
32004
+ * after dereferencing symlinks.
32005
+ *
32006
+ * The `data`/`src` attribute comes from the QTI file being converted, which is
32007
+ * untrusted input. A lexical check alone is insufficient: a package could place
32008
+ * a symlink under `root` that targets a file outside it (e.g. `/dev/zero`), so
32009
+ * the real path is re-verified against the real root before it is returned for
32010
+ * reading.
32011
+ *
32012
+ * @returns The real (symlink-dereferenced) absolute path if contained, else undefined.
32013
+ */
32014
+ function containedPath(candidate, root) {
32015
+ const resolvedRoot = resolve(root);
32016
+ const resolvedCandidate = resolve(candidate);
32017
+ if (!isContained(resolvedCandidate, resolvedRoot)) return;
32018
+ if (!existsSync(resolvedCandidate)) return;
32019
+ let realCandidate;
32020
+ let realRoot;
32021
+ try {
32022
+ realCandidate = realpathSync(resolvedCandidate);
32023
+ realRoot = realpathSync(resolvedRoot);
32024
+ } catch {
32025
+ return;
32026
+ }
32027
+ if (!isContained(realCandidate, realRoot)) return;
32028
+ return realCandidate;
32029
+ }
32030
+ /**
32031
+ * Read up to {@link MAX_HEADER_BYTES} from the start of a file without loading
32032
+ * the whole file into memory. Returns the bytes actually read.
32033
+ */
32034
+ function readHeaderBytes(path) {
32035
+ let fd;
32036
+ try {
32037
+ fd = openSync(path, "r");
32038
+ const buf = Buffer.alloc(MAX_HEADER_BYTES);
32039
+ const bytesRead = readSync(fd, buf, 0, MAX_HEADER_BYTES, 0);
32040
+ return buf.subarray(0, bytesRead);
32041
+ } catch {
32042
+ return;
32043
+ } finally {
32044
+ if (fd !== void 0) try {
32045
+ closeSync(fd);
32046
+ } catch {}
32047
+ }
32048
+ }
32049
+ /**
32050
+ * Read image dimensions from an in-memory file header buffer.
32051
+ * Exported for unit testing without touching the filesystem.
32052
+ *
32053
+ * Supports GIF (87a/89a), PNG, and JPEG (baseline and progressive).
32054
+ *
32055
+ * @param buf - The image file bytes (at least the header region)
32056
+ * @returns The pixel dimensions, or undefined if the format is unrecognised
32057
+ */
32058
+ function readImageDimensionsFromBuffer(buf) {
32059
+ if (buf.length >= 10 && buf[0] === 71 && buf[1] === 73 && buf[2] === 70) {
32060
+ const width = buf.readUInt16LE(6);
32061
+ const height = buf.readUInt16LE(8);
32062
+ if (width > 0 && height > 0) return {
32063
+ width,
32064
+ height
32065
+ };
32066
+ return;
32067
+ }
32068
+ if (buf.length >= 24 && buf[0] === 137 && buf[1] === 80 && buf[2] === 78 && buf[3] === 71) {
32069
+ const width = buf.readUInt32BE(16);
32070
+ const height = buf.readUInt32BE(20);
32071
+ if (width > 0 && height > 0) return {
32072
+ width,
32073
+ height
32074
+ };
32075
+ return;
32076
+ }
32077
+ if (buf.length >= 4 && buf[0] === 255 && buf[1] === 216) {
32078
+ let offset = 2;
32079
+ while (offset + 9 < buf.length) {
32080
+ if (buf[offset] !== 255) {
32081
+ offset++;
32082
+ continue;
32083
+ }
32084
+ let markerOffset = offset;
32085
+ while (markerOffset < buf.length && buf[markerOffset] === 255) markerOffset++;
32086
+ if (markerOffset >= buf.length) break;
32087
+ const marker = buf[markerOffset];
32088
+ const segmentStart = markerOffset + 1;
32089
+ if (marker === 0) {
32090
+ offset = segmentStart;
32091
+ continue;
32092
+ }
32093
+ if (marker === 216 || marker === 217 || marker >= 208 && marker <= 215) {
32094
+ offset = segmentStart;
32095
+ continue;
32096
+ }
32097
+ if (segmentStart + 1 >= buf.length) break;
32098
+ const segmentLength = buf.readUInt16BE(segmentStart);
32099
+ if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
32100
+ if (segmentStart + 6 >= buf.length) break;
32101
+ const height = buf.readUInt16BE(segmentStart + 3);
32102
+ const width = buf.readUInt16BE(segmentStart + 5);
32103
+ if (width > 0 && height > 0) return {
32104
+ width,
32105
+ height
32106
+ };
32107
+ return;
32108
+ }
32109
+ offset = segmentStart + segmentLength;
32110
+ }
32111
+ }
32112
+ }
32113
+ /**
32114
+ * Resolve and read the intrinsic dimensions of an image referenced by a QTI
32115
+ * `<object>`/`<img>` tag.
32116
+ *
32117
+ * Security model: the `objectDataPath` comes from untrusted QTI markup, so every
32118
+ * resolved path is confined to a containment root before it is read.
32119
+ *
32120
+ * - When `packageRoot` is supplied (the normal case for package conversion), any
32121
+ * asset located anywhere inside the package is allowed — including assets a
32122
+ * sibling directory up, e.g. `questions/item.xml` referencing
32123
+ * `../images/foo.png`. This does not rely on directory names.
32124
+ * - When `packageRoot` is omitted (in-memory callers with no package context),
32125
+ * the read is confined to the source file's own directory. This is the safe
32126
+ * default: no path is read from outside where the item lives.
32127
+ *
32128
+ * @param objectDataPath - Path from the data/src attribute (e.g. "754493.gif")
32129
+ * @param sourceFile - Absolute path to the source QTI XML file
32130
+ * @param packageRoot - Absolute path to the package root that bounds all reads
32131
+ * @returns The image dimensions, or undefined if it cannot be resolved/read.
32132
+ */
32133
+ function resolveImageDimensions(objectDataPath, sourceFile, packageRoot) {
32134
+ if (!sourceFile || !objectDataPath) return;
32135
+ const sourceDir = dirname(sourceFile);
32136
+ let root = sourceDir;
32137
+ if (packageRoot) {
32138
+ const resolvedRoot = resolve(packageRoot);
32139
+ const resolvedSourceDir = resolve(sourceDir);
32140
+ const rel = relative(resolvedRoot, resolvedSourceDir);
32141
+ root = rel === "" || !isAbsolute(rel) && rel.split(sep)[0] !== ".." ? resolvedRoot : resolvedSourceDir;
32142
+ }
32143
+ const candidates = [];
32144
+ if (isAbsolute(objectDataPath)) candidates.push(objectDataPath);
32145
+ else {
32146
+ candidates.push(join(sourceDir, objectDataPath));
32147
+ if (resolve(root) !== resolve(sourceDir)) candidates.push(join(root, objectDataPath));
32148
+ }
32149
+ for (const candidate of candidates) {
32150
+ const imagePath = containedPath(candidate, root);
32151
+ if (!imagePath) continue;
32152
+ const buf = readHeaderBytes(imagePath);
32153
+ if (buf) return readImageDimensionsFromBuffer(buf);
32154
+ }
32155
+ }
32156
+ //#endregion
31925
32157
  //#region src/qti-to-learnosity/nodeFileAccess.ts
31926
32158
  const nodeFileAccess = {
31927
32159
  resolvePassageFile,
31928
- findAndReadLomFile
32160
+ findAndReadLomFile,
32161
+ resolveImageDimensions
31929
32162
  };
31930
32163
  //#endregion
31931
32164
  //#region src/qti-package/readZip.ts