@pie-qti/item-player 0.1.19 → 0.1.21
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.
- package/README.md +137 -427
- package/dist/catalog/applyGlossaryTriggers.d.ts +2 -1
- package/dist/catalog/applyGlossaryTriggers.js +4 -2
- package/dist/catalog/applyGlossaryTriggers.js.map +1 -1
- package/dist/components/utils/stimulusRender.d.ts +1 -2
- package/dist/components/utils/stimulusRender.js +2 -5
- package/dist/components/utils/stimulusRender.js.map +1 -1
- package/dist/core/AssessmentItemDefinition.d.ts +69 -0
- package/dist/core/AssessmentItemDefinition.js +149 -0
- package/dist/core/AssessmentItemDefinition.js.map +1 -0
- package/dist/core/ComponentRegistry.d.ts +11 -5
- package/dist/core/ComponentRegistry.js +44 -17
- package/dist/core/ComponentRegistry.js.map +1 -1
- package/dist/core/ItemSession.d.ts +58 -0
- package/dist/core/ItemSession.js +286 -0
- package/dist/core/ItemSession.js.map +1 -0
- package/dist/core/Player.d.ts +26 -8
- package/dist/core/Player.js +225 -165
- package/dist/core/Player.js.map +1 -1
- package/dist/core/constants.d.ts +0 -2
- package/dist/core/constants.js +0 -33
- package/dist/core/constants.js.map +1 -1
- package/dist/document/AssessmentItemDocument.d.ts +32 -10
- package/dist/document/AssessmentItemDocument.js +80 -23
- package/dist/document/AssessmentItemDocument.js.map +1 -1
- package/dist/{element-class-BEpwHdcP.js → element-class-iN9MRyjF.js} +3582 -2783
- package/dist/element-class-iN9MRyjF.js.map +1 -0
- package/dist/element-class.d.ts +17 -13
- package/dist/element-class.js +1 -1
- package/dist/element.js +1 -1
- package/dist/extraction/ExtractionRegistry.d.ts +34 -11
- package/dist/extraction/ExtractionRegistry.js +122 -19
- package/dist/extraction/ExtractionRegistry.js.map +1 -1
- package/dist/extraction/createContext.d.ts +5 -6
- package/dist/extraction/createContext.js +2 -2
- package/dist/extraction/createContext.js.map +1 -1
- package/dist/extraction/deliveryTypes.d.ts +26 -0
- package/dist/extraction/deliveryTypes.js +7 -0
- package/dist/extraction/deliveryTypes.js.map +1 -0
- package/dist/extraction/index.d.ts +3 -3
- package/dist/extraction/index.js +1 -2
- package/dist/extraction/index.js.map +1 -1
- package/dist/extraction/interactionExtractionPipeline.d.ts +4 -4
- package/dist/extraction/interactionExtractionPipeline.js +27 -10
- package/dist/extraction/interactionExtractionPipeline.js.map +1 -1
- package/dist/extraction/interactionSecurity.d.ts +11 -2
- package/dist/extraction/interactionSecurity.js +105 -40
- package/dist/extraction/interactionSecurity.js.map +1 -1
- package/dist/extraction/types.d.ts +44 -7
- package/dist/extraction/types.js.map +1 -1
- package/dist/extraction/utils.js +4 -16
- package/dist/extraction/utils.js.map +1 -1
- package/dist/index.d.ts +9 -9
- package/dist/index.js +5 -8
- package/dist/index.js.map +1 -1
- package/dist/interactions/associate/extractor.js +1 -1
- package/dist/interactions/associate/extractor.js.map +1 -1
- package/dist/interactions/drawing/extractor.js +5 -4
- package/dist/interactions/drawing/extractor.js.map +1 -1
- package/dist/interactions/end-attempt/extractor.js +2 -2
- package/dist/interactions/end-attempt/extractor.js.map +1 -1
- package/dist/interactions/gap-match/extractor.js +4 -3
- package/dist/interactions/gap-match/extractor.js.map +1 -1
- package/dist/interactions/graphic-associate/extractor.js +5 -4
- package/dist/interactions/graphic-associate/extractor.js.map +1 -1
- package/dist/interactions/graphic-gap-match/extractor.js +20 -17
- package/dist/interactions/graphic-gap-match/extractor.js.map +1 -1
- package/dist/interactions/graphic-order/extractor.js +5 -4
- package/dist/interactions/graphic-order/extractor.js.map +1 -1
- package/dist/interactions/hotspot/extractor.js +11 -10
- package/dist/interactions/hotspot/extractor.js.map +1 -1
- package/dist/interactions/inline/render-plan.d.ts +3 -5
- package/dist/interactions/inline/render-plan.js.map +1 -1
- package/dist/interactions/media/extractor.js +2 -2
- package/dist/interactions/media/extractor.js.map +1 -1
- package/dist/interactions/modules.d.ts +11 -91
- package/dist/interactions/modules.js +185 -45
- package/dist/interactions/modules.js.map +1 -1
- package/dist/interactions/portable-custom/extractor.d.ts +1 -1
- package/dist/interactions/portable-custom/extractor.js +1 -1
- package/dist/interactions/portable-custom/extractor.js.map +1 -1
- package/dist/interactions/position-object/extractor.js +3 -2
- package/dist/interactions/position-object/extractor.js.map +1 -1
- package/dist/interactions/select-point/extractor.js +7 -6
- package/dist/interactions/select-point/extractor.js.map +1 -1
- package/dist/interactions/shared/types.d.ts +59 -70
- package/dist/interactions/shared/types.js.map +1 -1
- package/dist/interactions/slider/extractor.js +2 -2
- package/dist/interactions/slider/extractor.js.map +1 -1
- package/dist/interactions/upload/extractor.js +2 -2
- package/dist/interactions/upload/extractor.js.map +1 -1
- package/dist/pci/types.d.ts +2 -3
- package/dist/pci/types.js.map +1 -1
- package/dist/presentation/itemPresentationPlan.d.ts +71 -38
- package/dist/presentation/itemPresentationPlan.js +82 -35
- package/dist/presentation/itemPresentationPlan.js.map +1 -1
- package/dist/security/index.d.ts +1 -1
- package/dist/security/index.js +1 -1
- package/dist/security/index.js.map +1 -1
- package/dist/security/styleValues.d.ts +20 -0
- package/dist/security/styleValues.js +49 -0
- package/dist/security/styleValues.js.map +1 -0
- package/dist/server.d.ts +188 -9
- package/dist/server.js +32 -7
- package/dist/server.js.map +1 -1
- package/dist/types/index.d.ts +1 -110
- package/dist/types/index.js.map +1 -1
- package/dist/web-components/QTIInteractionBase.d.ts +2 -2
- package/dist/web-components/QTIInteractionBase.js.map +1 -1
- package/dist/web-components/QTIInteractionElement.d.ts +3 -3
- package/dist/web-components/QTIInteractionElement.js.map +1 -1
- package/package.json +11 -11
- package/dist/core/Plugin.d.ts +0 -104
- package/dist/core/Plugin.js +0 -8
- package/dist/core/Plugin.js.map +0 -1
- package/dist/core/PluginManager.d.ts +0 -55
- package/dist/core/PluginManager.js +0 -148
- package/dist/core/PluginManager.js.map +0 -1
- package/dist/core/declarations.d.ts +0 -44
- package/dist/core/declarations.js +0 -141
- package/dist/core/declarations.js.map +0 -1
- package/dist/element-class-BEpwHdcP.js.map +0 -1
- package/dist/interactions/associate/index.d.ts +0 -2
- package/dist/interactions/associate/index.js +0 -2
- package/dist/interactions/associate/index.js.map +0 -1
- package/dist/interactions/associate/types.d.ts +0 -1
- package/dist/interactions/associate/types.js +0 -2
- package/dist/interactions/associate/types.js.map +0 -1
- package/dist/interactions/choice/index.d.ts +0 -2
- package/dist/interactions/choice/index.js +0 -2
- package/dist/interactions/choice/index.js.map +0 -1
- package/dist/interactions/choice/types.d.ts +0 -1
- package/dist/interactions/choice/types.js +0 -2
- package/dist/interactions/choice/types.js.map +0 -1
- package/dist/interactions/custom/index.d.ts +0 -2
- package/dist/interactions/custom/index.js +0 -2
- package/dist/interactions/custom/index.js.map +0 -1
- package/dist/interactions/custom/types.d.ts +0 -1
- package/dist/interactions/custom/types.js +0 -2
- package/dist/interactions/custom/types.js.map +0 -1
- package/dist/interactions/drawing/index.d.ts +0 -2
- package/dist/interactions/drawing/index.js +0 -2
- package/dist/interactions/drawing/index.js.map +0 -1
- package/dist/interactions/drawing/types.d.ts +0 -1
- package/dist/interactions/drawing/types.js +0 -2
- package/dist/interactions/drawing/types.js.map +0 -1
- package/dist/interactions/end-attempt/index.d.ts +0 -2
- package/dist/interactions/end-attempt/index.js +0 -2
- package/dist/interactions/end-attempt/index.js.map +0 -1
- package/dist/interactions/end-attempt/types.d.ts +0 -1
- package/dist/interactions/end-attempt/types.js +0 -2
- package/dist/interactions/end-attempt/types.js.map +0 -1
- package/dist/interactions/extended-text/index.d.ts +0 -3
- package/dist/interactions/extended-text/index.js +0 -3
- package/dist/interactions/extended-text/index.js.map +0 -1
- package/dist/interactions/extended-text/types.d.ts +0 -1
- package/dist/interactions/extended-text/types.js +0 -2
- package/dist/interactions/extended-text/types.js.map +0 -1
- package/dist/interactions/gap-match/index.d.ts +0 -2
- package/dist/interactions/gap-match/index.js +0 -2
- package/dist/interactions/gap-match/index.js.map +0 -1
- package/dist/interactions/gap-match/types.d.ts +0 -1
- package/dist/interactions/gap-match/types.js +0 -2
- package/dist/interactions/gap-match/types.js.map +0 -1
- package/dist/interactions/graphic-associate/index.d.ts +0 -2
- package/dist/interactions/graphic-associate/index.js +0 -2
- package/dist/interactions/graphic-associate/index.js.map +0 -1
- package/dist/interactions/graphic-associate/types.d.ts +0 -1
- package/dist/interactions/graphic-associate/types.js +0 -2
- package/dist/interactions/graphic-associate/types.js.map +0 -1
- package/dist/interactions/graphic-gap-match/index.d.ts +0 -2
- package/dist/interactions/graphic-gap-match/index.js +0 -2
- package/dist/interactions/graphic-gap-match/index.js.map +0 -1
- package/dist/interactions/graphic-gap-match/types.d.ts +0 -1
- package/dist/interactions/graphic-gap-match/types.js +0 -2
- package/dist/interactions/graphic-gap-match/types.js.map +0 -1
- package/dist/interactions/graphic-order/index.d.ts +0 -2
- package/dist/interactions/graphic-order/index.js +0 -2
- package/dist/interactions/graphic-order/index.js.map +0 -1
- package/dist/interactions/graphic-order/types.d.ts +0 -1
- package/dist/interactions/graphic-order/types.js +0 -2
- package/dist/interactions/graphic-order/types.js.map +0 -1
- package/dist/interactions/hotspot/index.d.ts +0 -2
- package/dist/interactions/hotspot/index.js +0 -2
- package/dist/interactions/hotspot/index.js.map +0 -1
- package/dist/interactions/hotspot/types.d.ts +0 -1
- package/dist/interactions/hotspot/types.js +0 -2
- package/dist/interactions/hotspot/types.js.map +0 -1
- package/dist/interactions/hottext/index.d.ts +0 -2
- package/dist/interactions/hottext/index.js +0 -2
- package/dist/interactions/hottext/index.js.map +0 -1
- package/dist/interactions/hottext/types.d.ts +0 -1
- package/dist/interactions/hottext/types.js +0 -2
- package/dist/interactions/hottext/types.js.map +0 -1
- package/dist/interactions/index.d.ts +0 -32
- package/dist/interactions/index.js +0 -31
- package/dist/interactions/index.js.map +0 -1
- package/dist/interactions/inline/index.d.ts +0 -1
- package/dist/interactions/inline/index.js +0 -2
- package/dist/interactions/inline/index.js.map +0 -1
- package/dist/interactions/inline-choice/index.d.ts +0 -2
- package/dist/interactions/inline-choice/index.js +0 -2
- package/dist/interactions/inline-choice/index.js.map +0 -1
- package/dist/interactions/inline-choice/types.d.ts +0 -1
- package/dist/interactions/inline-choice/types.js +0 -2
- package/dist/interactions/inline-choice/types.js.map +0 -1
- package/dist/interactions/match/index.d.ts +0 -2
- package/dist/interactions/match/index.js +0 -2
- package/dist/interactions/match/index.js.map +0 -1
- package/dist/interactions/match/types.d.ts +0 -1
- package/dist/interactions/match/types.js +0 -2
- package/dist/interactions/match/types.js.map +0 -1
- package/dist/interactions/media/index.d.ts +0 -2
- package/dist/interactions/media/index.js +0 -2
- package/dist/interactions/media/index.js.map +0 -1
- package/dist/interactions/media/types.d.ts +0 -1
- package/dist/interactions/media/types.js +0 -2
- package/dist/interactions/media/types.js.map +0 -1
- package/dist/interactions/order/index.d.ts +0 -2
- package/dist/interactions/order/index.js +0 -2
- package/dist/interactions/order/index.js.map +0 -1
- package/dist/interactions/order/types.d.ts +0 -1
- package/dist/interactions/order/types.js +0 -2
- package/dist/interactions/order/types.js.map +0 -1
- package/dist/interactions/portable-custom/index.d.ts +0 -2
- package/dist/interactions/portable-custom/index.js +0 -2
- package/dist/interactions/portable-custom/index.js.map +0 -1
- package/dist/interactions/portable-custom/types.d.ts +0 -1
- package/dist/interactions/portable-custom/types.js +0 -2
- package/dist/interactions/portable-custom/types.js.map +0 -1
- package/dist/interactions/position-object/index.d.ts +0 -2
- package/dist/interactions/position-object/index.js +0 -2
- package/dist/interactions/position-object/index.js.map +0 -1
- package/dist/interactions/position-object/types.d.ts +0 -1
- package/dist/interactions/position-object/types.js +0 -2
- package/dist/interactions/position-object/types.js.map +0 -1
- package/dist/interactions/select-point/index.d.ts +0 -2
- package/dist/interactions/select-point/index.js +0 -2
- package/dist/interactions/select-point/index.js.map +0 -1
- package/dist/interactions/select-point/types.d.ts +0 -1
- package/dist/interactions/select-point/types.js +0 -2
- package/dist/interactions/select-point/types.js.map +0 -1
- package/dist/interactions/shared/index.d.ts +0 -1
- package/dist/interactions/shared/index.js +0 -2
- package/dist/interactions/shared/index.js.map +0 -1
- package/dist/interactions/slider/index.d.ts +0 -2
- package/dist/interactions/slider/index.js +0 -2
- package/dist/interactions/slider/index.js.map +0 -1
- package/dist/interactions/slider/types.d.ts +0 -1
- package/dist/interactions/slider/types.js +0 -2
- package/dist/interactions/slider/types.js.map +0 -1
- package/dist/interactions/text-entry/index.d.ts +0 -2
- package/dist/interactions/text-entry/index.js +0 -2
- package/dist/interactions/text-entry/index.js.map +0 -1
- package/dist/interactions/text-entry/types.d.ts +0 -1
- package/dist/interactions/text-entry/types.js +0 -2
- package/dist/interactions/text-entry/types.js.map +0 -1
- package/dist/interactions/upload/index.d.ts +0 -2
- package/dist/interactions/upload/index.js +0 -2
- package/dist/interactions/upload/index.js.map +0 -1
- package/dist/interactions/upload/types.d.ts +0 -1
- package/dist/interactions/upload/types.js +0 -2
- package/dist/interactions/upload/types.js.map +0 -1
|
@@ -40,9 +40,9 @@
|
|
|
40
40
|
* ```
|
|
41
41
|
*/
|
|
42
42
|
export { createExtractionContext } from './createContext.js';
|
|
43
|
+
export type { InteractionDeliveryField, InteractionDeliveryPathSegment, InteractionDeliverySchema, } from './deliveryTypes.js';
|
|
44
|
+
export { htmlField, urlField } from './deliveryTypes.js';
|
|
43
45
|
export { createExtractionRegistry, ExtractionRegistry } from './ExtractionRegistry.js';
|
|
44
|
-
export type {
|
|
45
|
-
export { standardAssociateExtractor, standardChoiceExtractor, standardCustomExtractor, standardDrawingExtractor, standardEndAttemptExtractor, standardExtendedTextExtractor, standardGapMatchExtractor, standardGraphicAssociateExtractor, standardGraphicGapMatchExtractor, standardGraphicOrderExtractor, standardHotspotExtractor, standardHottextExtractor, standardInlineChoiceExtractor, standardMatchExtractor, standardMediaExtractor, standardOrderExtractor, standardPositionObjectExtractor, standardSelectPointExtractor, standardSliderExtractor, standardTextEntryExtractor, standardUploadExtractor, } from '../interactions/index.js';
|
|
46
|
-
export type { ElementExtractor, ExtractionContext, ExtractionResult, ExtractionUtils, ValidationResult, VariableDeclaration, } from './types.js';
|
|
46
|
+
export type { ElementExtractor, ExtractionContext, ExtractionDispatchResult, ExtractionResult, ExtractionUtils, ValidationResult, VariableDeclaration, } from './types.js';
|
|
47
47
|
export { ExtractionError, isErrorResult, isSuccessResult } from './types.js';
|
|
48
48
|
export { createExtractionUtils } from './utils.js';
|
package/dist/extraction/index.js
CHANGED
|
@@ -40,10 +40,9 @@
|
|
|
40
40
|
* ```
|
|
41
41
|
*/
|
|
42
42
|
export { createExtractionContext } from './createContext.js';
|
|
43
|
+
export { htmlField, urlField } from './deliveryTypes.js';
|
|
43
44
|
// Registry
|
|
44
45
|
export { createExtractionRegistry, ExtractionRegistry } from './ExtractionRegistry.js';
|
|
45
|
-
// Standard extractors
|
|
46
|
-
export { standardAssociateExtractor, standardChoiceExtractor, standardCustomExtractor, standardDrawingExtractor, standardEndAttemptExtractor, standardExtendedTextExtractor, standardGapMatchExtractor, standardGraphicAssociateExtractor, standardGraphicGapMatchExtractor, standardGraphicOrderExtractor, standardHotspotExtractor, standardHottextExtractor, standardInlineChoiceExtractor, standardMatchExtractor, standardMediaExtractor, standardOrderExtractor, standardPositionObjectExtractor, standardSelectPointExtractor, standardSliderExtractor, standardTextEntryExtractor, standardUploadExtractor, } from '../interactions/index.js';
|
|
47
46
|
export { ExtractionError, isErrorResult, isSuccessResult } from './types.js';
|
|
48
47
|
// Utilities
|
|
49
48
|
export { createExtractionUtils } from './utils.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/extraction/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/extraction/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAM7D,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AACzD,WAAW;AACX,OAAO,EAAE,wBAAwB,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAYvF,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7E,YAAY;AACZ,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC","sourcesContent":["/**\n * Plugin-based element extraction system\n *\n * Provides a pluggable architecture for extracting data from QTI XML elements\n * with priority-based dispatch, validation, and type safety.\n *\n * @example Basic usage\n * ```typescript\n * import { ExtractionRegistry, createExtractionContext } from '@pie-qti/item-player';\n *\n * const registry = new ExtractionRegistry();\n * registry.register(myExtractor);\n *\n * const context = createExtractionContext(element, 'RESPONSE', dom, declarations, config);\n * const result = registry.extract<MyData>(element, context);\n *\n * if (result.success) {\n * console.log('Extracted:', result.data);\n * }\n * ```\n *\n * @example Custom extractor\n * ```typescript\n * const myExtractor: ElementExtractor<ChoiceData> = {\n * id: 'custom:my-choice',\n * name: 'My Custom Choice',\n * priority: 100,\n * elementTypes: ['choiceInteraction'],\n * canHandle: (element, context) => {\n * return context.utils.hasChildWithTag(element, 'customChoice');\n * },\n * extract: (element, context) => {\n * return {\n * choices: [...],\n * shuffle: false,\n * maxChoices: 1\n * };\n * }\n * };\n * ```\n */\n\nexport { createExtractionContext } from './createContext.js';\nexport type {\n\tInteractionDeliveryField,\n\tInteractionDeliveryPathSegment,\n\tInteractionDeliverySchema,\n} from './deliveryTypes.js';\nexport { htmlField, urlField } from './deliveryTypes.js';\n// Registry\nexport { createExtractionRegistry, ExtractionRegistry } from './ExtractionRegistry.js';\n\n// Core types\nexport type {\n\tElementExtractor,\n\tExtractionContext,\n\tExtractionDispatchResult,\n\tExtractionResult,\n\tExtractionUtils,\n\tValidationResult,\n\tVariableDeclaration,\n} from './types.js';\nexport { ExtractionError, isErrorResult, isSuccessResult } from './types.js';\n// Utilities\nexport { createExtractionUtils } from './utils.js';\n"]}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import type { AssessmentItemDocument } from '../document/AssessmentItemDocument.js';
|
|
2
2
|
import type { ExtractionRegistry } from './ExtractionRegistry.js';
|
|
3
3
|
import type { DeclarationMap } from '@pie-qti/qti-processing';
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
4
|
+
import type { ExtractionConfig } from './types.js';
|
|
5
|
+
import type { BaseInteractionData } from '../interactions/shared/types.js';
|
|
6
6
|
export interface InteractionExtractionPipelineInput {
|
|
7
7
|
document: AssessmentItemDocument;
|
|
8
8
|
extractionRegistry: ExtractionRegistry;
|
|
9
9
|
declarations: DeclarationMap;
|
|
10
|
-
config:
|
|
10
|
+
config: ExtractionConfig;
|
|
11
11
|
/**
|
|
12
12
|
* Item session GUID, used to seed `shuffle` for the interactions that support it.
|
|
13
13
|
* Omit to keep the authored order.
|
|
14
14
|
*/
|
|
15
15
|
sessionGuid?: string;
|
|
16
16
|
}
|
|
17
|
-
export declare function extractInteractionData({ document, extractionRegistry, declarations, config, sessionGuid, }: InteractionExtractionPipelineInput):
|
|
17
|
+
export declare function extractInteractionData({ document, extractionRegistry, declarations, config, sessionGuid, }: InteractionExtractionPipelineInput): readonly BaseInteractionData[];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createExtractionContext } from './createContext.js';
|
|
2
|
-
import {
|
|
2
|
+
import { finalizeInteractionDelivery } from './interactionSecurity.js';
|
|
3
3
|
export function extractInteractionData({ document, extractionRegistry, declarations, config, sessionGuid, }) {
|
|
4
4
|
const declMap = projectDeclarationsForExtraction(declarations);
|
|
5
5
|
const elements = document.findExtractionElements(getRegisteredElementTypes(extractionRegistry));
|
|
@@ -8,17 +8,34 @@ export function extractInteractionData({ document, extractionRegistry, declarati
|
|
|
8
8
|
const context = createExtractionContext(discovered.element, discovered.responseIdentifier, discovered.contextRoot, declMap, config, sessionGuid);
|
|
9
9
|
const result = extractionRegistry.extract(discovered.element, context);
|
|
10
10
|
if (!result.success)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
// generic unsupported-custom fallback.
|
|
16
|
-
type: (result.data?.type ?? discovered.normalizedType),
|
|
17
|
-
responseId: discovered.responseIdentifier,
|
|
11
|
+
throw result.error;
|
|
12
|
+
const extractor = result.extractor;
|
|
13
|
+
const outputType = extractor?.outputType ?? discovered.normalizedType;
|
|
14
|
+
const interaction = {
|
|
18
15
|
...result.data,
|
|
19
|
-
|
|
16
|
+
// These are framework-owned routing fields. Applying them after the
|
|
17
|
+
// extractor payload prevents a plugin from redirecting a response or
|
|
18
|
+
// selecting an undeclared renderer through payload properties.
|
|
19
|
+
type: outputType,
|
|
20
|
+
responseId: discovered.responseIdentifier,
|
|
21
|
+
};
|
|
22
|
+
// Sink policy follows the authored interaction kind, while outputType only
|
|
23
|
+
// selects a renderer. A plugin replacing a standard extractor must not be
|
|
24
|
+
// able to shed the standard prompt/choice/URL policy by renaming its output.
|
|
25
|
+
const standardFields = extractionRegistry.getDeliveryFieldsForType(discovered.normalizedType);
|
|
26
|
+
const extractorFields = extractor.delivery?.fields ?? [];
|
|
27
|
+
interactions.push(finalizeInteractionDelivery(interaction, mergeDeliveryFields(standardFields, extractorFields), config.security, discovered.normalizedType));
|
|
28
|
+
}
|
|
29
|
+
return Object.freeze(interactions);
|
|
30
|
+
}
|
|
31
|
+
function mergeDeliveryFields(standard, extension) {
|
|
32
|
+
const fields = new Map();
|
|
33
|
+
for (const field of [...extension, ...standard]) {
|
|
34
|
+
// An output path has one sink meaning. Authored standard policy is
|
|
35
|
+
// authoritative even when a plugin tries to change the field kind itself.
|
|
36
|
+
fields.set(JSON.stringify(field.path), field);
|
|
20
37
|
}
|
|
21
|
-
return
|
|
38
|
+
return [...fields.values()];
|
|
22
39
|
}
|
|
23
40
|
function getRegisteredElementTypes(extractionRegistry) {
|
|
24
41
|
const elementTypes = new Set();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interactionExtractionPipeline.js","sourceRoot":"","sources":["../../src/extraction/interactionExtractionPipeline.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"interactionExtractionPipeline.js","sourceRoot":"","sources":["../../src/extraction/interactionExtractionPipeline.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AAqBvE,MAAM,UAAU,sBAAsB,CAAC,EACtC,QAAQ,EACR,kBAAkB,EAClB,YAAY,EACZ,MAAM,EACN,WAAW,GACyB;IACpC,MAAM,OAAO,GAAG,gCAAgC,CAAC,YAAY,CAAC,CAAC;IAC/D,MAAM,QAAQ,GAAG,QAAQ,CAAC,sBAAsB,CAAC,yBAAyB,CAAC,kBAAkB,CAAC,CAAC,CAAC;IAEhG,MAAM,YAAY,GAA0B,EAAE,CAAC;IAC/C,KAAK,MAAM,UAAU,IAAI,QAAQ,EAAE,CAAC;QACnC,MAAM,OAAO,GAAG,uBAAuB,CACtC,UAAU,CAAC,OAAO,EAClB,UAAU,CAAC,kBAAkB,EAC7B,UAAU,CAAC,WAAW,EACtB,OAAO,EACP,MAAM,EACN,WAAW,CACX,CAAC;QACF,MAAM,MAAM,GAAG,kBAAkB,CAAC,OAAO,CACxC,UAAU,CAAC,OAAO,EAClB,OAAO,CACP,CAAC;QACF,IAAI,CAAC,MAAM,CAAC,OAAO;YAAE,MAAM,MAAM,CAAC,KAAK,CAAC;QAExC,MAAM,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QACnC,MAAM,UAAU,GAAG,SAAS,EAAE,UAAU,IAAI,UAAU,CAAC,cAAc,CAAC;QACtE,MAAM,WAAW,GAAG;YACnB,GAA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AC;QAC/B,CAAC;IACF,CAAC;IACD,OAAO,CAAC,GAAG,YAAY,CAAC,CAAC;AAC1B,CAAC;AAED,SAAS,gCAAgC,CACxC,YAA4B;IAE5B,MAAM,OAAO,GAAG,IAAI,GAAG,EAAyC,CAAC;IACjE,KAAK,MAAM,WAAW,IAAI,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC;QACvD,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,UAAU,EAAE;YACnC,UAAU,EAAE,WAAW,CAAC,UAAU;YAClC,WAAW,EAAE,WAAW,CAAC,WAAkB;YAC3C,QAAQ,EAAE,WAAW,CAAC,QAAQ;SAC9B,CAAC,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC","sourcesContent":["import type { AssessmentItemDocument } from '../document/AssessmentItemDocument.js';\nimport type { ExtractionRegistry } from './ExtractionRegistry.js';\nimport { createExtractionContext } from './createContext.js';\nimport { finalizeInteractionDelivery } from './interactionSecurity.js';\nimport type { VariableDeclaration as ExtractionVariableDeclaration } from './types.js';\nimport type { DeclarationMap } from '@pie-qti/qti-processing';\nimport type { ExtractionConfig } from './types.js';\nimport type {\n\tBaseInteractionData,\n\tDeliveredInteraction,\n} from '../interactions/shared/types.js';\n\nexport interface InteractionExtractionPipelineInput {\n\tdocument: AssessmentItemDocument;\n\textractionRegistry: ExtractionRegistry;\n\tdeclarations: DeclarationMap;\n\tconfig: ExtractionConfig;\n\t/**\n\t * Item session GUID, used to seed `shuffle` for the interactions that support it.\n\t * Omit to keep the authored order.\n\t */\n\tsessionGuid?: string;\n}\n\nexport function extractInteractionData({\n\tdocument,\n\textractionRegistry,\n\tdeclarations,\n\tconfig,\n\tsessionGuid,\n}: InteractionExtractionPipelineInput): readonly BaseInteractionData[] {\n\tconst declMap = projectDeclarationsForExtraction(declarations);\n\tconst elements = document.findExtractionElements(getRegisteredElementTypes(extractionRegistry));\n\n\tconst interactions: BaseInteractionData[] = [];\n\tfor (const discovered of elements) {\n\t\tconst context = createExtractionContext(\n\t\t\tdiscovered.element,\n\t\t\tdiscovered.responseIdentifier,\n\t\t\tdiscovered.contextRoot,\n\t\t\tdeclMap,\n\t\t\tconfig,\n\t\t\tsessionGuid\n\t\t);\n\t\tconst result = extractionRegistry.extract<Record<string, unknown>>(\n\t\t\tdiscovered.element,\n\t\t\tcontext\n\t\t);\n\t\tif (!result.success) throw result.error;\n\n\t\tconst extractor = result.extractor;\n\t\tconst outputType = extractor?.outputType ?? discovered.normalizedType;\n\t\tconst interaction = {\n\t\t\t...result.data,\n\t\t\t// These are framework-owned routing fields. Applying them after the\n\t\t\t// extractor payload prevents a plugin from redirecting a response or\n\t\t\t// selecting an undeclared renderer through payload properties.\n\t\t\ttype: outputType,\n\t\t\tresponseId: discovered.responseIdentifier,\n\t\t} satisfies DeliveredInteraction<string, Record<string, unknown>>;\n\t\t// Sink policy follows the authored interaction kind, while outputType only\n\t\t// selects a renderer. A plugin replacing a standard extractor must not be\n\t\t// able to shed the standard prompt/choice/URL policy by renaming its output.\n\t\tconst standardFields = extractionRegistry.getDeliveryFieldsForType(\n\t\t\tdiscovered.normalizedType,\n\t\t);\n\t\tconst extractorFields = extractor.delivery?.fields ?? [];\n\t\tinteractions.push(\n\t\t\tfinalizeInteractionDelivery(\n\t\t\t\tinteraction,\n\t\t\t\tmergeDeliveryFields(standardFields, extractorFields),\n\t\t\t\tconfig.security,\n\t\t\t\tdiscovered.normalizedType,\n\t\t\t)\n\t\t);\n\t}\n\n\treturn Object.freeze(interactions);\n}\n\nfunction mergeDeliveryFields<T extends { kind: string; path: readonly (string | '*')[] }>(\n\tstandard: readonly T[],\n\textension: readonly T[]\n): T[] {\n\tconst fields = new Map<string, T>();\n\tfor (const field of [...extension, ...standard]) {\n\t\t// An output path has one sink meaning. Authored standard policy is\n\t\t// authoritative even when a plugin tries to change the field kind itself.\n\t\tfields.set(JSON.stringify(field.path), field);\n\t}\n\treturn [...fields.values()];\n}\n\nfunction getRegisteredElementTypes(extractionRegistry: ExtractionRegistry): string[] {\n\tconst elementTypes = new Set<string>();\n\tfor (const extractor of extractionRegistry.getExtractors()) {\n\t\tfor (const elementType of extractor.elementTypes) {\n\t\t\telementTypes.add(elementType);\n\t\t}\n\t}\n\treturn [...elementTypes];\n}\n\nfunction projectDeclarationsForExtraction(\n\tdeclarations: DeclarationMap\n): Map<string, ExtractionVariableDeclaration> {\n\tconst declMap = new Map<string, ExtractionVariableDeclaration>();\n\tfor (const declaration of Object.values(declarations)) {\n\t\tdeclMap.set(declaration.identifier, {\n\t\t\tidentifier: declaration.identifier,\n\t\t\tcardinality: declaration.cardinality as any,\n\t\t\tbaseType: declaration.baseType,\n\t\t});\n\t}\n\treturn declMap;\n}\n"]}
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BaseInteractionData } from '../interactions/shared/types.js';
|
|
2
2
|
import type { PlayerSecurityConfig } from '../types/index.js';
|
|
3
|
-
|
|
3
|
+
import type { InteractionDeliveryField } from './deliveryTypes.js';
|
|
4
|
+
/**
|
|
5
|
+
* Finalize extracted data for its declared render sinks.
|
|
6
|
+
*
|
|
7
|
+
* InteractionModules own field classification. This module owns the common
|
|
8
|
+
* enforcement implementation and is the only extraction egress that may mint
|
|
9
|
+
* TrustedHTML. The returned graph is frozen so no post-egress string transform
|
|
10
|
+
* can invalidate that guarantee.
|
|
11
|
+
*/
|
|
12
|
+
export declare function finalizeInteractionDelivery<TData extends BaseInteractionData>(interaction: TData, fields: readonly InteractionDeliveryField[], security?: PlayerSecurityConfig, authoredType?: string): TData;
|
|
@@ -1,49 +1,114 @@
|
|
|
1
|
+
import { sanitizeHtml } from '../core/sanitizer.js';
|
|
1
2
|
import { toTrustedHtml } from '../core/trustedTypes.js';
|
|
2
3
|
import { sanitizeResourceUrl } from '../core/urlPolicy.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Finalize extracted data for its declared render sinks.
|
|
6
|
+
*
|
|
7
|
+
* InteractionModules own field classification. This module owns the common
|
|
8
|
+
* enforcement implementation and is the only extraction egress that may mint
|
|
9
|
+
* TrustedHTML. The returned graph is frozen so no post-egress string transform
|
|
10
|
+
* can invalidate that guarantee.
|
|
11
|
+
*/
|
|
12
|
+
export function finalizeInteractionDelivery(interaction, fields, security, authoredType = interaction.type) {
|
|
13
|
+
const output = cloneValue(interaction);
|
|
14
|
+
for (const field of fields) {
|
|
15
|
+
visitField(output, field.path, (value, parent, key) => {
|
|
16
|
+
if (field.kind === 'html') {
|
|
17
|
+
// A plugin overriding a standard extractor cannot bypass the common
|
|
18
|
+
// policy by returning an object at a known HTML field. Coerce at this
|
|
19
|
+
// boundary, sanitize, and mint the only render-capable value here.
|
|
20
|
+
const raw = typeof value === 'string' ? value : '';
|
|
21
|
+
const sanitized = sanitizeHtml(raw, { security });
|
|
22
|
+
setChild(parent, key, toTrustedHtml(sanitized, security?.trustedTypesPolicyName));
|
|
23
|
+
return;
|
|
15
24
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (interaction.imageData?.content && typeof interaction.imageData.content === 'string') {
|
|
22
|
-
interaction.imageData.content = toTrustedHtml(interaction.imageData.content, ttPolicyName);
|
|
23
|
-
}
|
|
24
|
-
// positionObject stages.
|
|
25
|
-
if (Array.isArray(interaction.positionObjectStages)) {
|
|
26
|
-
for (const stage of interaction.positionObjectStages) {
|
|
27
|
-
if (stage?.objectData?.src) {
|
|
28
|
-
stage.objectData.src = sanitizeResourceUrl(stage.objectData.src, policy, 'img') ?? '';
|
|
29
|
-
}
|
|
30
|
-
if (stage?.objectData?.content && typeof stage.objectData.content === 'string') {
|
|
31
|
-
stage.objectData.content = toTrustedHtml(stage.objectData.content, ttPolicyName);
|
|
32
|
-
}
|
|
25
|
+
// Unexpected URL shapes fail closed rather than reaching a browser sink
|
|
26
|
+
// where implicit string coercion could reinterpret them.
|
|
27
|
+
if (typeof value !== 'string') {
|
|
28
|
+
setChild(parent, key, '');
|
|
29
|
+
return;
|
|
33
30
|
}
|
|
31
|
+
const use = resolveUrlKind(field.use, output, authoredType);
|
|
32
|
+
setChild(parent, key, sanitizeResourceUrl(value, security?.urlPolicy, use) ?? '');
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
if (authoredType === 'mediaInteraction') {
|
|
36
|
+
output.allowObjectEmbeds = security?.allowObjectEmbeds === true;
|
|
37
|
+
}
|
|
38
|
+
return deepFreeze(output);
|
|
39
|
+
}
|
|
40
|
+
function visitField(value, path, visit, parent, key) {
|
|
41
|
+
if (path.length === 0) {
|
|
42
|
+
if (parent !== undefined && key !== undefined)
|
|
43
|
+
visit(value, parent, key);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (value === null || typeof value !== 'object')
|
|
47
|
+
return;
|
|
48
|
+
const [segment, ...rest] = path;
|
|
49
|
+
if (segment === '*') {
|
|
50
|
+
if (Array.isArray(value)) {
|
|
51
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
52
|
+
visitField(value[index], rest, visit, value, index);
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
34
55
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const kind = interaction.mediaElement.type === 'object' ? 'object' : 'media';
|
|
38
|
-
interaction.mediaElement.src =
|
|
39
|
-
sanitizeResourceUrl(interaction.mediaElement.src, policy, kind) ?? '';
|
|
40
|
-
interaction.allowObjectEmbeds = allowObjectEmbeds;
|
|
41
|
-
}
|
|
42
|
-
// hottextInteraction content is injected via {@html}.
|
|
43
|
-
if (interaction.type === 'hottextInteraction' && typeof interaction.contentHtml === 'string') {
|
|
44
|
-
interaction.contentHtml = toTrustedHtml(interaction.contentHtml, ttPolicyName);
|
|
56
|
+
for (const [childKey, child] of Object.entries(value)) {
|
|
57
|
+
visitField(child, rest, visit, value, childKey);
|
|
45
58
|
}
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const record = value;
|
|
62
|
+
if (!(segment in record))
|
|
63
|
+
return;
|
|
64
|
+
visitField(record[segment], rest, visit, record, segment);
|
|
65
|
+
}
|
|
66
|
+
function resolveUrlKind(use, interaction, authoredType) {
|
|
67
|
+
if (use !== 'media-or-object')
|
|
68
|
+
return use;
|
|
69
|
+
const mediaElement = authoredType === 'mediaInteraction' && 'mediaElement' in interaction
|
|
70
|
+
? interaction.mediaElement
|
|
71
|
+
: null;
|
|
72
|
+
return mediaElement &&
|
|
73
|
+
typeof mediaElement === 'object' &&
|
|
74
|
+
'type' in mediaElement &&
|
|
75
|
+
mediaElement.type === 'object'
|
|
76
|
+
? 'object'
|
|
77
|
+
: 'media';
|
|
78
|
+
}
|
|
79
|
+
function setChild(parent, key, value) {
|
|
80
|
+
if (Array.isArray(parent) && typeof key === 'number') {
|
|
81
|
+
parent[key] = value;
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
parent[String(key)] = value;
|
|
85
|
+
}
|
|
86
|
+
function cloneValue(value) {
|
|
87
|
+
if (Array.isArray(value))
|
|
88
|
+
return value.map((entry) => cloneValue(entry));
|
|
89
|
+
if (value && typeof value === 'object') {
|
|
90
|
+
// TrustedHTML and other host values are terminal leaves. Standard extractor
|
|
91
|
+
// output is plain data; retaining a non-plain value avoids destroying a host
|
|
92
|
+
// object's identity when a plugin supplies one deliberately.
|
|
93
|
+
const prototype = Object.getPrototypeOf(value);
|
|
94
|
+
if (prototype !== Object.prototype && prototype !== null)
|
|
95
|
+
return value;
|
|
96
|
+
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, cloneValue(entry)]));
|
|
97
|
+
}
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
function deepFreeze(value) {
|
|
101
|
+
if (!value || typeof value !== 'object' || Object.isFrozen(value))
|
|
102
|
+
return value;
|
|
103
|
+
const prototype = Object.getPrototypeOf(value);
|
|
104
|
+
if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null) {
|
|
105
|
+
// TrustedHTML and other host objects are opaque terminal values. Freezing a
|
|
106
|
+
// browser-owned object can throw and adds no protection to our data graph.
|
|
107
|
+
return value;
|
|
46
108
|
}
|
|
47
|
-
|
|
109
|
+
Object.freeze(value);
|
|
110
|
+
for (const child of Object.values(value))
|
|
111
|
+
deepFreeze(child);
|
|
112
|
+
return value;
|
|
48
113
|
}
|
|
49
114
|
//# sourceMappingURL=interactionSecurity.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interactionSecurity.js","sourceRoot":"","sources":["../../src/extraction/interactionSecurity.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"interactionSecurity.js","sourceRoot":"","sources":["../../src/extraction/interactionSecurity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,mBAAmB,EAAgB,MAAM,sBAAsB,CAAC;AAUzE;;;;;;;GAOG;AACH,MAAM,UAAU,2BAA2B,CAC1C,WAAkB,EAClB,MAA2C,EAC3C,QAA+B,EAC/B,eAAuB,WAAW,CAAC,IAAI;IAEvC,MAAM,MAAM,GAAG,UAAU,CAAC,WAAW,CAAU,CAAC;IAEhD,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC5B,UAAU,CAAC,MAAkC,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,EAAE;YACjF,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;gBAC3B,oEAAoE;gBACpE,sEAAsE;gBACtE,mEAAmE;gBACnE,MAAM,GAAG,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CA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,CAAC;IAC/C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,SAAS,KAAK,MAAM,CAAC,SAAS,IAAI,SAAS,KAAK,IAAI,EAAE,CAAC;QACnF,4EAA4E;QAC5E,2EAA2E;QAC3E,OAAO,KAAK,CAAC;IACd,CAAC;IACD,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACrB,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,KAAsB,CAAC;QAAE,UAAU,CAAC,KAAK,CAAC,CAAC;IAC7E,OAAO,KAAK,CAAC;AACd,CAAC","sourcesContent":["import { sanitizeHtml } from '../core/sanitizer.js';\nimport { toTrustedHtml } from '../core/trustedTypes.js';\nimport { sanitizeResourceUrl, type UrlKind } from '../core/urlPolicy.js';\nimport type { BaseInteractionData } from '../interactions/shared/types.js';\nimport type { PlayerSecurityConfig } from '../types/index.js';\nimport type {\n\tInteractionDeliveryField,\n\tInteractionDeliveryPathSegment,\n} from './deliveryTypes.js';\n\ntype MutableRecord = Record<string, unknown>;\n\n/**\n * Finalize extracted data for its declared render sinks.\n *\n * InteractionModules own field classification. This module owns the common\n * enforcement implementation and is the only extraction egress that may mint\n * TrustedHTML. The returned graph is frozen so no post-egress string transform\n * can invalidate that guarantee.\n */\nexport function finalizeInteractionDelivery<TData extends BaseInteractionData>(\n\tinteraction: TData,\n\tfields: readonly InteractionDeliveryField[],\n\tsecurity?: PlayerSecurityConfig,\n\tauthoredType: string = interaction.type,\n): TData {\n\tconst output = cloneValue(interaction) as TData;\n\n\tfor (const field of fields) {\n\t\tvisitField(output as unknown as MutableRecord, field.path, (value, parent, key) => {\n\t\t\tif (field.kind === 'html') {\n\t\t\t\t// A plugin overriding a standard extractor cannot bypass the common\n\t\t\t\t// policy by returning an object at a known HTML field. Coerce at this\n\t\t\t\t// boundary, sanitize, and mint the only render-capable value here.\n\t\t\t\tconst raw = typeof value === 'string' ? value : '';\n\t\t\t\tconst sanitized = sanitizeHtml(raw, { security });\n\t\t\t\tsetChild(parent, key, toTrustedHtml(sanitized, security?.trustedTypesPolicyName));\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Unexpected URL shapes fail closed rather than reaching a browser sink\n\t\t\t// where implicit string coercion could reinterpret them.\n\t\t\tif (typeof value !== 'string') {\n\t\t\t\tsetChild(parent, key, '');\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst use = resolveUrlKind(field.use, output, authoredType);\n\t\t\tsetChild(parent, key, sanitizeResourceUrl(value, security?.urlPolicy, use) ?? '');\n\t\t});\n\t}\n\n\tif (authoredType === 'mediaInteraction') {\n\t\t(output as any).allowObjectEmbeds = security?.allowObjectEmbeds === true;\n\t}\n\n\treturn deepFreeze(output);\n}\nfunction visitField(\n\tvalue: unknown,\n\tpath: readonly InteractionDeliveryPathSegment[],\n\tvisit: (value: unknown, parent: MutableRecord | unknown[], key: string | number) => void,\n\tparent?: MutableRecord | unknown[],\n\tkey?: string | number\n): void {\n\tif (path.length === 0) {\n\t\tif (parent !== undefined && key !== undefined) visit(value, parent, key);\n\t\treturn;\n\t}\n\n\tif (value === null || typeof value !== 'object') return;\n\n\tconst [segment, ...rest] = path;\n\tif (segment === '*') {\n\t\tif (Array.isArray(value)) {\n\t\t\tfor (let index = 0; index < value.length; index += 1) {\n\t\t\t\tvisitField(value[index], rest, visit, value, index);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tfor (const [childKey, child] of Object.entries(value)) {\n\t\t\tvisitField(child, rest, visit, value as MutableRecord, childKey);\n\t\t}\n\t\treturn;\n\t}\n\n\tconst record = value as MutableRecord;\n\tif (!(segment in record)) return;\n\tvisitField(record[segment], rest, visit, record, segment);\n}\n\nfunction resolveUrlKind(\n\tuse: UrlKind | 'media-or-object',\n\tinteraction: BaseInteractionData,\n\tauthoredType: string,\n): UrlKind {\n\tif (use !== 'media-or-object') return use;\n\tconst mediaElement =\n\t\tauthoredType === 'mediaInteraction' && 'mediaElement' in interaction\n\t\t\t? interaction.mediaElement\n\t\t\t: null;\n\treturn mediaElement &&\n\t\ttypeof mediaElement === 'object' &&\n\t\t'type' in mediaElement &&\n\t\tmediaElement.type === 'object'\n\t\t? 'object'\n\t\t: 'media';\n}\n\nfunction setChild(\n\tparent: MutableRecord | unknown[],\n\tkey: string | number,\n\tvalue: unknown\n): void {\n\tif (Array.isArray(parent) && typeof key === 'number') {\n\t\tparent[key] = value;\n\t\treturn;\n\t}\n\t(parent as MutableRecord)[String(key)] = value;\n}\n\nfunction cloneValue<T>(value: T): T {\n\tif (Array.isArray(value)) return value.map((entry) => cloneValue(entry)) as T;\n\tif (value && typeof value === 'object') {\n\t\t// TrustedHTML and other host values are terminal leaves. Standard extractor\n\t\t// output is plain data; retaining a non-plain value avoids destroying a host\n\t\t// object's identity when a plugin supplies one deliberately.\n\t\tconst prototype = Object.getPrototypeOf(value);\n\t\tif (prototype !== Object.prototype && prototype !== null) return value;\n\t\treturn Object.fromEntries(\n\t\t\tObject.entries(value).map(([key, entry]) => [key, cloneValue(entry)])\n\t\t) as T;\n\t}\n\treturn value;\n}\n\nfunction deepFreeze<T>(value: T): T {\n\tif (!value || typeof value !== 'object' || Object.isFrozen(value)) return value;\n\tconst prototype = Object.getPrototypeOf(value);\n\tif (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null) {\n\t\t// TrustedHTML and other host objects are opaque terminal values. Freezing a\n\t\t// browser-owned object can throw and adds no protection to our data graph.\n\t\treturn value;\n\t}\n\tObject.freeze(value);\n\tfor (const child of Object.values(value as MutableRecord)) deepFreeze(child);\n\treturn value;\n}\n"]}
|
|
@@ -4,8 +4,17 @@
|
|
|
4
4
|
* Provides interfaces for extracting data from QTI XML elements with
|
|
5
5
|
* priority-based dispatch, validation, and type safety.
|
|
6
6
|
*/
|
|
7
|
-
import type {
|
|
8
|
-
import type {
|
|
7
|
+
import type { AttributeNameMapper, ElementNameMapper } from '@pie-qti/qti-common';
|
|
8
|
+
import type { PlayerSecurityConfig, QTIRole } from '../types/index.js';
|
|
9
|
+
import type { QTIElement } from '../interactions/shared/types.js';
|
|
10
|
+
import type { InteractionDeliverySchema } from './deliveryTypes.js';
|
|
11
|
+
/** Configuration visible to extractor implementations. */
|
|
12
|
+
export interface ExtractionConfig {
|
|
13
|
+
readonly role?: QTIRole;
|
|
14
|
+
readonly security?: PlayerSecurityConfig;
|
|
15
|
+
readonly elementNameMapper?: ElementNameMapper;
|
|
16
|
+
readonly attributeNameMapper?: AttributeNameMapper;
|
|
17
|
+
}
|
|
9
18
|
/**
|
|
10
19
|
* Context provided to extractors during extraction
|
|
11
20
|
* Contains DOM reference, utilities, and metadata
|
|
@@ -21,8 +30,8 @@ export interface ExtractionContext {
|
|
|
21
30
|
declarations: Map<string, VariableDeclaration>;
|
|
22
31
|
/** Utility functions for DOM manipulation and extraction */
|
|
23
32
|
utils: ExtractionUtils;
|
|
24
|
-
/**
|
|
25
|
-
config:
|
|
33
|
+
/** Immutable extraction-relevant configuration. */
|
|
34
|
+
config: ExtractionConfig;
|
|
26
35
|
/**
|
|
27
36
|
* Seeded RNG for interactions honouring the QTI `shuffle` attribute, keyed to the
|
|
28
37
|
* item session and this `responseId`. Stateful: successive calls advance the
|
|
@@ -47,6 +56,8 @@ export interface VariableDeclaration {
|
|
|
47
56
|
* Provides safe, consistent DOM manipulation and content extraction
|
|
48
57
|
*/
|
|
49
58
|
export interface ExtractionUtils {
|
|
59
|
+
/** Check an element tag against a canonical QTI 2.x name across QTI versions. */
|
|
60
|
+
matchesTag(element: QTIElement, tagName: string): boolean;
|
|
50
61
|
/**
|
|
51
62
|
* Get direct children with a specific tag name
|
|
52
63
|
* @example utils.getChildrenByTag(element, 'simpleChoice')
|
|
@@ -140,7 +151,7 @@ export type ExtractionResult<TData> = {
|
|
|
140
151
|
* @template TData - The extracted data type (e.g., ChoiceData, TextEntryData)
|
|
141
152
|
* @template TContext - The extraction context type (defaults to ExtractionContext)
|
|
142
153
|
*/
|
|
143
|
-
export interface ElementExtractor<
|
|
154
|
+
export interface ElementExtractor<TPayload extends object = Record<string, unknown>, TOutputType extends string = string, TContext extends ExtractionContext = ExtractionContext> {
|
|
144
155
|
/**
|
|
145
156
|
* Unique identifier for this extractor
|
|
146
157
|
* Convention: 'namespace:element-type' (e.g., 'qti:choice', 'renaissance:rating-choice')
|
|
@@ -171,6 +182,17 @@ export interface ElementExtractor<TData = unknown, TContext extends ExtractionCo
|
|
|
171
182
|
* @example 'Handles Renaissance rating choice interactions with star/face icons'
|
|
172
183
|
*/
|
|
173
184
|
description?: string;
|
|
185
|
+
/**
|
|
186
|
+
* Renderer-facing type attached by the framework after extraction. When
|
|
187
|
+
* omitted, the normalized authored element type is used.
|
|
188
|
+
*/
|
|
189
|
+
readonly outputType?: TOutputType;
|
|
190
|
+
/**
|
|
191
|
+
* HTML and resource fields produced by this extractor. The common delivery
|
|
192
|
+
* pipeline enforces these rules after extraction, so plugin extractors can
|
|
193
|
+
* safely add rich fields without teaching a central type switch their shape.
|
|
194
|
+
*/
|
|
195
|
+
delivery?: InteractionDeliverySchema;
|
|
174
196
|
/**
|
|
175
197
|
* Predicate to determine if this extractor can handle the element
|
|
176
198
|
* Evaluated in priority order (highest first)
|
|
@@ -192,15 +214,30 @@ export interface ElementExtractor<TData = unknown, TContext extends ExtractionCo
|
|
|
192
214
|
* @throws ExtractionError if extraction fails
|
|
193
215
|
* @returns Extracted data structure
|
|
194
216
|
*/
|
|
195
|
-
extract(element: QTIElement, context: TContext):
|
|
217
|
+
extract(element: QTIElement, context: TContext): TPayload;
|
|
196
218
|
/**
|
|
197
219
|
* Validate extracted data (optional)
|
|
198
220
|
* Called after extract() succeeds
|
|
199
221
|
*
|
|
200
222
|
* @returns Validation result with errors/warnings
|
|
201
223
|
*/
|
|
202
|
-
validate?(data:
|
|
224
|
+
validate?(data: TPayload): ValidationResult;
|
|
203
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* Result of one registry-owned dispatch. On success, `extractor` is the exact
|
|
228
|
+
* snapshotted handler whose predicate matched and whose `extract` method ran.
|
|
229
|
+
* Callers must use this descriptor for renderer and delivery-policy decisions
|
|
230
|
+
* instead of performing a second lookup.
|
|
231
|
+
*/
|
|
232
|
+
export type ExtractionDispatchResult<TData extends object> = {
|
|
233
|
+
success: true;
|
|
234
|
+
data: TData;
|
|
235
|
+
extractor: ElementExtractor<any, string>;
|
|
236
|
+
warnings?: string[];
|
|
237
|
+
} | {
|
|
238
|
+
success: false;
|
|
239
|
+
error: ExtractionError;
|
|
240
|
+
};
|
|
204
241
|
/**
|
|
205
242
|
* Type guard for successful extraction result
|
|
206
243
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/extraction/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAuIH;;GAEG;AACH,MAAM,OAAO,eAAgB,SAAQ,KAAK;IACzC,YACC,OAAe,EACC,WAAmB,EACnB,WAAmB,EACnB,KAAa;QAE7B,KAAK,CAAC,OAAO,CAAC,CAAC;QAJC,gBAAW,GAAX,WAAW,CAAQ;QACnB,gBAAW,GAAX,WAAW,CAAQ;QACnB,UAAK,GAAL,KAAK,CAAQ;QAG7B,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAC/B,CAAC;CACD;AAuFD;;GAEG;AACH,MAAM,UAAU,eAAe,CAC9B,MAA+B;IAE/B,OAAO,MAAM,CAAC,OAAO,KAAK,IAAI,CAAC;AAChC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAC5B,MAA+B;IAE/B,OAAO,MAAM,CAAC,OAAO,KAAK,KAAK,CAAC;AACjC,CAAC","sourcesContent":["/**\n * Core types for the plugin-based element extraction system\n *\n * Provides interfaces for extracting data from QTI XML elements with\n * priority-based dispatch, validation, and type safety.\n */\n\nimport type { PlayerConfig } from '../types/index.js';\nimport type { QTIElement } from '../interactions/index.js';\n\n/**\n * Context provided to extractors during extraction\n * Contains DOM reference, utilities, and metadata\n */\nexport interface ExtractionContext {\n\t/** The element being extracted */\n\telement: QTIElement;\n\n\t/** Response identifier for this interaction */\n\tresponseId: string;\n\n\t/** Root DOM element for document-wide queries */\n\tdom: QTIElement;\n\n\t/** Variable declarations from responseDeclaration/outcomeDeclaration */\n\tdeclarations: Map<string, VariableDeclaration>;\n\n\t/** Utility functions for DOM manipulation and extraction */\n\tutils: ExtractionUtils;\n\n\t/** Player configuration (role, typesetting, etc.) */\n\tconfig: PlayerConfig;\n\n\t/**\n\t * Seeded RNG for interactions honouring the QTI `shuffle` attribute, keyed to the\n\t * item session and this `responseId`. Stateful: successive calls advance the\n\t * sequence, so an extractor with two choice sets (e.g. `matchInteraction`) gets an\n\t * independent permutation for each set.\n\t *\n\t * Optional so that a hand-built context remains valid; extractors that find it\n\t * absent keep the authored order.\n\t */\n\tshuffleRng?: () => number;\n}\n\n/**\n * Variable declaration from QTI responseDeclaration/outcomeDeclaration\n */\nexport interface VariableDeclaration {\n\tidentifier: string;\n\tcardinality: 'single' | 'multiple' | 'ordered' | 'record';\n\tbaseType?: string;\n}\n\n/**\n * Utility functions available to extractors\n * Provides safe, consistent DOM manipulation and content extraction\n */\nexport interface ExtractionUtils {\n\t/**\n\t * Get direct children with a specific tag name\n\t * @example utils.getChildrenByTag(element, 'simpleChoice')\n\t */\n\tgetChildrenByTag(element: QTIElement, tagName: string): QTIElement[];\n\n\t/**\n\t * Query all descendants matching a selector\n\t * @example utils.querySelectorAll(element, 'choiceInteraction simpleChoice')\n\t */\n\tquerySelectorAll(element: QTIElement, selector: string): QTIElement[];\n\n\t/**\n\t * Query first descendant matching a selector\n\t * @example utils.querySelector(element, 'prompt')\n\t */\n\tquerySelector(element: QTIElement, selector: string): QTIElement | null;\n\n\t/**\n\t * Check if element has a child with specific tag (fast boolean check)\n\t * @example utils.hasChildWithTag(element, 'ratingChoice')\n\t */\n\thasChildWithTag(element: QTIElement, tagName: string): boolean;\n\n\t/**\n\t * Get sanitized HTML content from element\n\t * @example utils.getHtmlContent(element) // '<p>Question <math>...</math></p>'\n\t */\n\tgetHtmlContent(element: QTIElement): string;\n\n\t/**\n\t * Get plain text content (strips HTML tags)\n\t * @example utils.getTextContent(element) // 'Question text'\n\t */\n\tgetTextContent(element: QTIElement): string;\n\n\t/**\n\t * Get attribute value with optional default\n\t * @example utils.getAttribute(element, 'identifier', '')\n\t */\n\tgetAttribute(element: QTIElement, name: string, defaultValue?: string): string;\n\n\t/**\n\t * Get boolean attribute value with optional default\n\t * @example utils.getBooleanAttribute(element, 'shuffle', false) // true\n\t */\n\tgetBooleanAttribute(element: QTIElement, name: string, defaultValue?: boolean): boolean;\n\n\t/**\n\t * Get numeric attribute value with optional default\n\t * @example utils.getNumberAttribute(element, 'maxChoices', 1) // 1\n\t */\n\tgetNumberAttribute(element: QTIElement, name: string, defaultValue?: number): number;\n\n\t/**\n\t * Get CSS classes from element\n\t * @example utils.getClasses(element) // ['rating-interaction', 'custom']\n\t */\n\tgetClasses(element: QTIElement): string[];\n\n\t/**\n\t * Extract prompt element text from an interaction\n\t * @example utils.getPrompt(element) // '<p>Select the correct answer</p>'\n\t */\n\tgetPrompt(element: QTIElement): string | null;\n}\n\n/**\n * Result of validation\n */\nexport interface ValidationResult {\n\t/** Whether validation passed */\n\tvalid: boolean;\n\n\t/** Error messages if validation failed */\n\terrors?: string[];\n\n\t/** Warning messages (non-fatal) */\n\twarnings?: string[];\n}\n\n/**\n * Error thrown during extraction\n */\nexport class ExtractionError extends Error {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic readonly extractorId: string,\n\t\tpublic readonly elementType: string,\n\t\tpublic readonly cause?: Error\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'ExtractionError';\n\t}\n}\n\n/**\n * Result of extraction operation\n */\nexport type ExtractionResult<TData> =\n\t| { success: true; data: TData; warnings?: string[] }\n\t| { success: false; error: ExtractionError };\n\n/**\n * Element extractor with priority-based dispatch\n *\n * @template TData - The extracted data type (e.g., ChoiceData, TextEntryData)\n * @template TContext - The extraction context type (defaults to ExtractionContext)\n */\nexport interface ElementExtractor<\n\tTData = unknown,\n\tTContext extends ExtractionContext = ExtractionContext,\n> {\n\t/**\n\t * Unique identifier for this extractor\n\t * Convention: 'namespace:element-type' (e.g., 'qti:choice', 'renaissance:rating-choice')\n\t */\n\tid: string;\n\n\t/**\n\t * Human-readable name for debugging/error messages\n\t * @example 'QTI Standard Choice Interaction'\n\t */\n\tname: string;\n\n\t/**\n\t * Priority for extractor evaluation (higher = checked first)\n\t * - 1000+: Critical system extractors (error handlers, etc.)\n\t * - 500-999: Vendor-specific extractors (Renaissance, custom content)\n\t * - 100-499: Third-party plugin extractors\n\t * - 1-99: Standard QTI extractors with variations\n\t * - 0: Fallback/default extractors\n\t */\n\tpriority: number;\n\n\t/**\n\t * Element types this extractor handles (for type-based indexing)\n\t * Used for O(M) lookup instead of O(N) scanning\n\t * @example ['choiceInteraction', 'inlineChoiceInteraction']\n\t */\n\telementTypes: string[];\n\n\t/**\n\t * Optional description for debugging/error messages\n\t * @example 'Handles Renaissance rating choice interactions with star/face icons'\n\t */\n\tdescription?: string;\n\n\t/**\n\t * Predicate to determine if this extractor can handle the element\n\t * Evaluated in priority order (highest first)\n\t *\n\t * IMPORTANT: Must be FAST - use quick checks first\n\t * @example\n\t * canHandle(element, context) {\n\t * // Fast checks first\n\t * if (!context.utils.hasChildWithTag(element, 'ratingChoice')) return false;\n\t * // Expensive checks only if fast checks pass\n\t * return validateRatingStructure(element);\n\t * }\n\t */\n\tcanHandle(element: QTIElement, context: TContext): boolean;\n\n\t/**\n\t * Extract data from the element\n\t * Called only if canHandle() returns true\n\t *\n\t * @throws ExtractionError if extraction fails\n\t * @returns Extracted data structure\n\t */\n\textract(element: QTIElement, context: TContext): TData;\n\n\t/**\n\t * Validate extracted data (optional)\n\t * Called after extract() succeeds\n\t *\n\t * @returns Validation result with errors/warnings\n\t */\n\tvalidate?(data: TData): ValidationResult;\n}\n\n/**\n * Type guard for successful extraction result\n */\nexport function isSuccessResult<TData>(\n\tresult: ExtractionResult<TData>\n): result is { success: true; data: TData; warnings?: string[] } {\n\treturn result.success === true;\n}\n\n/**\n * Type guard for failed extraction result\n */\nexport function isErrorResult<TData>(\n\tresult: ExtractionResult<TData>\n): result is { success: false; error: ExtractionError } {\n\treturn result.success === false;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/extraction/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAoJH;;GAEG;AACH,MAAM,OAAO,eAAgB,SAAQ,KAAK;IACzC,YACC,OAAe,EACC,WAAmB,EACnB,WAAmB,EACnB,KAAa;QAE7B,KAAK,CAAC,OAAO,CAAC,CAAC;QAJC,gBAAW,GAAX,WAAW,CAAQ;QACnB,gBAAW,GAAX,WAAW,CAAQ;QACnB,UAAK,GAAL,KAAK,CAAQ;QAG7B,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAC/B,CAAC;CACD;AAoHD;;GAEG;AACH,MAAM,UAAU,eAAe,CAC9B,MAA+B;IAE/B,OAAO,MAAM,CAAC,OAAO,KAAK,IAAI,CAAC;AAChC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAC5B,MAA+B;IAE/B,OAAO,MAAM,CAAC,OAAO,KAAK,KAAK,CAAC;AACjC,CAAC","sourcesContent":["/**\n * Core types for the plugin-based element extraction system\n *\n * Provides interfaces for extracting data from QTI XML elements with\n * priority-based dispatch, validation, and type safety.\n */\n\nimport type { AttributeNameMapper, ElementNameMapper } from '@pie-qti/qti-common';\nimport type { PlayerSecurityConfig, QTIRole } from '../types/index.js';\nimport type { QTIElement } from '../interactions/shared/types.js';\nimport type { InteractionDeliverySchema } from './deliveryTypes.js';\n\n/** Configuration visible to extractor implementations. */\nexport interface ExtractionConfig {\n\treadonly role?: QTIRole;\n\treadonly security?: PlayerSecurityConfig;\n\treadonly elementNameMapper?: ElementNameMapper;\n\treadonly attributeNameMapper?: AttributeNameMapper;\n}\n\n/**\n * Context provided to extractors during extraction\n * Contains DOM reference, utilities, and metadata\n */\nexport interface ExtractionContext {\n\t/** The element being extracted */\n\telement: QTIElement;\n\n\t/** Response identifier for this interaction */\n\tresponseId: string;\n\n\t/** Root DOM element for document-wide queries */\n\tdom: QTIElement;\n\n\t/** Variable declarations from responseDeclaration/outcomeDeclaration */\n\tdeclarations: Map<string, VariableDeclaration>;\n\n\t/** Utility functions for DOM manipulation and extraction */\n\tutils: ExtractionUtils;\n\n\t/** Immutable extraction-relevant configuration. */\n\tconfig: ExtractionConfig;\n\n\t/**\n\t * Seeded RNG for interactions honouring the QTI `shuffle` attribute, keyed to the\n\t * item session and this `responseId`. Stateful: successive calls advance the\n\t * sequence, so an extractor with two choice sets (e.g. `matchInteraction`) gets an\n\t * independent permutation for each set.\n\t *\n\t * Optional so that a hand-built context remains valid; extractors that find it\n\t * absent keep the authored order.\n\t */\n\tshuffleRng?: () => number;\n}\n\n/**\n * Variable declaration from QTI responseDeclaration/outcomeDeclaration\n */\nexport interface VariableDeclaration {\n\tidentifier: string;\n\tcardinality: 'single' | 'multiple' | 'ordered' | 'record';\n\tbaseType?: string;\n}\n\n/**\n * Utility functions available to extractors\n * Provides safe, consistent DOM manipulation and content extraction\n */\nexport interface ExtractionUtils {\n\t/** Check an element tag against a canonical QTI 2.x name across QTI versions. */\n\tmatchesTag(element: QTIElement, tagName: string): boolean;\n\n\t/**\n\t * Get direct children with a specific tag name\n\t * @example utils.getChildrenByTag(element, 'simpleChoice')\n\t */\n\tgetChildrenByTag(element: QTIElement, tagName: string): QTIElement[];\n\n\t/**\n\t * Query all descendants matching a selector\n\t * @example utils.querySelectorAll(element, 'choiceInteraction simpleChoice')\n\t */\n\tquerySelectorAll(element: QTIElement, selector: string): QTIElement[];\n\n\t/**\n\t * Query first descendant matching a selector\n\t * @example utils.querySelector(element, 'prompt')\n\t */\n\tquerySelector(element: QTIElement, selector: string): QTIElement | null;\n\n\t/**\n\t * Check if element has a child with specific tag (fast boolean check)\n\t * @example utils.hasChildWithTag(element, 'ratingChoice')\n\t */\n\thasChildWithTag(element: QTIElement, tagName: string): boolean;\n\n\t/**\n\t * Get sanitized HTML content from element\n\t * @example utils.getHtmlContent(element) // '<p>Question <math>...</math></p>'\n\t */\n\tgetHtmlContent(element: QTIElement): string;\n\n\t/**\n\t * Get plain text content (strips HTML tags)\n\t * @example utils.getTextContent(element) // 'Question text'\n\t */\n\tgetTextContent(element: QTIElement): string;\n\n\t/**\n\t * Get attribute value with optional default\n\t * @example utils.getAttribute(element, 'identifier', '')\n\t */\n\tgetAttribute(element: QTIElement, name: string, defaultValue?: string): string;\n\n\t/**\n\t * Get boolean attribute value with optional default\n\t * @example utils.getBooleanAttribute(element, 'shuffle', false) // true\n\t */\n\tgetBooleanAttribute(element: QTIElement, name: string, defaultValue?: boolean): boolean;\n\n\t/**\n\t * Get numeric attribute value with optional default\n\t * @example utils.getNumberAttribute(element, 'maxChoices', 1) // 1\n\t */\n\tgetNumberAttribute(element: QTIElement, name: string, defaultValue?: number): number;\n\n\t/**\n\t * Get CSS classes from element\n\t * @example utils.getClasses(element) // ['rating-interaction', 'custom']\n\t */\n\tgetClasses(element: QTIElement): string[];\n\n\t/**\n\t * Extract prompt element text from an interaction\n\t * @example utils.getPrompt(element) // '<p>Select the correct answer</p>'\n\t */\n\tgetPrompt(element: QTIElement): string | null;\n}\n\n/**\n * Result of validation\n */\nexport interface ValidationResult {\n\t/** Whether validation passed */\n\tvalid: boolean;\n\n\t/** Error messages if validation failed */\n\terrors?: string[];\n\n\t/** Warning messages (non-fatal) */\n\twarnings?: string[];\n}\n\n/**\n * Error thrown during extraction\n */\nexport class ExtractionError extends Error {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic readonly extractorId: string,\n\t\tpublic readonly elementType: string,\n\t\tpublic readonly cause?: Error\n\t) {\n\t\tsuper(message);\n\t\tthis.name = 'ExtractionError';\n\t}\n}\n\n/**\n * Result of extraction operation\n */\nexport type ExtractionResult<TData> =\n\t| { success: true; data: TData; warnings?: string[] }\n\t| { success: false; error: ExtractionError };\n\n/**\n * Element extractor with priority-based dispatch\n *\n * @template TData - The extracted data type (e.g., ChoiceData, TextEntryData)\n * @template TContext - The extraction context type (defaults to ExtractionContext)\n */\nexport interface ElementExtractor<\n\tTPayload extends object = Record<string, unknown>,\n\tTOutputType extends string = string,\n\tTContext extends ExtractionContext = ExtractionContext,\n> {\n\t/**\n\t * Unique identifier for this extractor\n\t * Convention: 'namespace:element-type' (e.g., 'qti:choice', 'renaissance:rating-choice')\n\t */\n\tid: string;\n\n\t/**\n\t * Human-readable name for debugging/error messages\n\t * @example 'QTI Standard Choice Interaction'\n\t */\n\tname: string;\n\n\t/**\n\t * Priority for extractor evaluation (higher = checked first)\n\t * - 1000+: Critical system extractors (error handlers, etc.)\n\t * - 500-999: Vendor-specific extractors (Renaissance, custom content)\n\t * - 100-499: Third-party plugin extractors\n\t * - 1-99: Standard QTI extractors with variations\n\t * - 0: Fallback/default extractors\n\t */\n\tpriority: number;\n\n\t/**\n\t * Element types this extractor handles (for type-based indexing)\n\t * Used for O(M) lookup instead of O(N) scanning\n\t * @example ['choiceInteraction', 'inlineChoiceInteraction']\n\t */\n\telementTypes: string[];\n\n\t/**\n\t * Optional description for debugging/error messages\n\t * @example 'Handles Renaissance rating choice interactions with star/face icons'\n\t */\n\tdescription?: string;\n\n\t/**\n\t * Renderer-facing type attached by the framework after extraction. When\n\t * omitted, the normalized authored element type is used.\n\t */\n\treadonly outputType?: TOutputType;\n\n\t/**\n\t * HTML and resource fields produced by this extractor. The common delivery\n\t * pipeline enforces these rules after extraction, so plugin extractors can\n\t * safely add rich fields without teaching a central type switch their shape.\n\t */\n\tdelivery?: InteractionDeliverySchema;\n\n\t/**\n\t * Predicate to determine if this extractor can handle the element\n\t * Evaluated in priority order (highest first)\n\t *\n\t * IMPORTANT: Must be FAST - use quick checks first\n\t * @example\n\t * canHandle(element, context) {\n\t * // Fast checks first\n\t * if (!context.utils.hasChildWithTag(element, 'ratingChoice')) return false;\n\t * // Expensive checks only if fast checks pass\n\t * return validateRatingStructure(element);\n\t * }\n\t */\n\tcanHandle(element: QTIElement, context: TContext): boolean;\n\n\t/**\n\t * Extract data from the element\n\t * Called only if canHandle() returns true\n\t *\n\t * @throws ExtractionError if extraction fails\n\t * @returns Extracted data structure\n\t */\n\textract(element: QTIElement, context: TContext): TPayload;\n\n\t/**\n\t * Validate extracted data (optional)\n\t * Called after extract() succeeds\n\t *\n\t * @returns Validation result with errors/warnings\n\t */\n\tvalidate?(data: TPayload): ValidationResult;\n}\n\n/**\n * Result of one registry-owned dispatch. On success, `extractor` is the exact\n * snapshotted handler whose predicate matched and whose `extract` method ran.\n * Callers must use this descriptor for renderer and delivery-policy decisions\n * instead of performing a second lookup.\n */\nexport type ExtractionDispatchResult<TData extends object> =\n\t| {\n\t\t\tsuccess: true;\n\t\t\tdata: TData;\n\t\t\textractor: ElementExtractor<any, string>;\n\t\t\twarnings?: string[];\n\t }\n\t| { success: false; error: ExtractionError };\n\n/**\n * Type guard for successful extraction result\n */\nexport function isSuccessResult<TData>(\n\tresult: ExtractionResult<TData>\n): result is { success: true; data: TData; warnings?: string[] } {\n\treturn result.success === true;\n}\n\n/**\n * Type guard for failed extraction result\n */\nexport function isErrorResult<TData>(\n\tresult: ExtractionResult<TData>\n): result is { success: false; error: ExtractionError } {\n\treturn result.success === false;\n}\n"]}
|
package/dist/extraction/utils.js
CHANGED
|
@@ -11,21 +11,6 @@ import { sanitizeTextContent } from '../core/sanitizer.js';
|
|
|
11
11
|
function getTextContent(element) {
|
|
12
12
|
return element?.textContent?.trim() || '';
|
|
13
13
|
}
|
|
14
|
-
/**
|
|
15
|
-
* Extract HTML content from QTI elements (preserves MathML and other markup)
|
|
16
|
-
* Content is sanitized to prevent XSS attacks
|
|
17
|
-
*/
|
|
18
|
-
function getHtmlContent(element) {
|
|
19
|
-
const html = element?.innerHTML?.trim() || '';
|
|
20
|
-
return sanitizeTextContent(html);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Get children by tag name from QTI elements
|
|
24
|
-
* Note: This version does NOT handle QTI version mapping - use the one in ExtractionUtils instead
|
|
25
|
-
*/
|
|
26
|
-
function getChildrenByTag(element, tagName) {
|
|
27
|
-
return element?.childNodes?.filter((n) => n.rawTagName === tagName) || [];
|
|
28
|
-
}
|
|
29
14
|
/**
|
|
30
15
|
* Get children by tag name with QTI version awareness
|
|
31
16
|
*/
|
|
@@ -111,6 +96,9 @@ export function createExtractionUtils(security, mapper, attributeMapper) {
|
|
|
111
96
|
const elementMapper = mapper || new Qti2xElementNameMapper();
|
|
112
97
|
const attrMapper = attributeMapper || new Qti2xAttributeNameMapper();
|
|
113
98
|
return {
|
|
99
|
+
matchesTag(element, tagName) {
|
|
100
|
+
return matchesTagName(element.rawTagName, tagName, elementMapper);
|
|
101
|
+
},
|
|
114
102
|
getChildrenByTag(element, tagName) {
|
|
115
103
|
return getChildrenByTagWithMapper(element, tagName, elementMapper);
|
|
116
104
|
},
|
|
@@ -169,7 +157,7 @@ export function createExtractionUtils(security, mapper, attributeMapper) {
|
|
|
169
157
|
},
|
|
170
158
|
getPrompt(element) {
|
|
171
159
|
const promptElement = getChildrenByTagWithMapper(element, 'prompt', elementMapper)[0];
|
|
172
|
-
return promptElement ? getHtmlContent(promptElement) : null;
|
|
160
|
+
return promptElement ? this.getHtmlContent(promptElement) : null;
|
|
173
161
|
},
|
|
174
162
|
getClasses(element) {
|
|
175
163
|
return getClasses(element);
|