@pie-qti/item-player 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -0
- package/CHANGELOG.md +9 -0
- package/README.md +466 -0
- package/docs/QTI-COMPLIANCE.md +197 -0
- package/docs/WCAG-2.2-COMPLIANCE.md +466 -0
- package/docs/custom-extractors.md +679 -0
- package/docs/iframe-mode.md +78 -0
- package/docs/security-audit.md +236 -0
- package/docs/server-api.md +691 -0
- package/package.json +78 -0
- package/src/components/ItemBody.svelte +382 -0
- package/src/components/ItemPlayer.svelte +155 -0
- package/src/components/ModalFeedbackDisplay.svelte +87 -0
- package/src/components/actions/typesetAction.ts +74 -0
- package/src/components/index.ts +19 -0
- package/src/components/utils/assignProps.ts +24 -0
- package/src/components/utils/feedbackUtils.ts +140 -0
- package/src/components/utils/index.ts +13 -0
- package/src/core/ComponentRegistry.ts +219 -0
- package/src/core/ItemRenderer.ts +367 -0
- package/src/core/Player.ts +1152 -0
- package/src/core/Plugin.ts +123 -0
- package/src/core/PluginManager.ts +182 -0
- package/src/core/constants.ts +117 -0
- package/src/core/declarations.ts +171 -0
- package/src/core/parsingLimits.ts +62 -0
- package/src/core/random.ts +24 -0
- package/src/core/sanitizer.ts +235 -0
- package/src/core/trustedTypes.ts +63 -0
- package/src/core/urlPolicy.ts +95 -0
- package/src/extensions/MathLiveEditor.ts +310 -0
- package/src/extraction/ExtractionRegistry.ts +295 -0
- package/src/extraction/createContext.ts +47 -0
- package/src/extraction/extractors/associateExtractor.ts +129 -0
- package/src/extraction/extractors/choiceExtractor.ts +110 -0
- package/src/extraction/extractors/customExtractor.ts +72 -0
- package/src/extraction/extractors/drawingExtractor.ts +113 -0
- package/src/extraction/extractors/endAttemptExtractor.ts +74 -0
- package/src/extraction/extractors/extendedTextExtractor.ts +80 -0
- package/src/extraction/extractors/gapMatchExtractor.ts +178 -0
- package/src/extraction/extractors/graphicAssociateExtractor.ts +188 -0
- package/src/extraction/extractors/graphicGapMatchExtractor.ts +185 -0
- package/src/extraction/extractors/graphicOrderExtractor.ts +162 -0
- package/src/extraction/extractors/hotspotExtractor.ts +190 -0
- package/src/extraction/extractors/hottextExtractor.ts +153 -0
- package/src/extraction/extractors/index.ts +111 -0
- package/src/extraction/extractors/inlineChoiceExtractor.ts +88 -0
- package/src/extraction/extractors/matchExtractor.ts +160 -0
- package/src/extraction/extractors/mediaExtractor.ts +168 -0
- package/src/extraction/extractors/orderExtractor.ts +114 -0
- package/src/extraction/extractors/positionObjectExtractor.ts +243 -0
- package/src/extraction/extractors/selectPointExtractor.ts +143 -0
- package/src/extraction/extractors/sliderExtractor.ts +102 -0
- package/src/extraction/extractors/textEntryExtractor.ts +70 -0
- package/src/extraction/extractors/uploadExtractor.ts +73 -0
- package/src/extraction/index.ts +109 -0
- package/src/extraction/types.ts +246 -0
- package/src/extraction/utils.ts +160 -0
- package/src/iframe/IFramePlayerHost.ts +265 -0
- package/src/iframe/index.ts +11 -0
- package/src/iframe/protocol.ts +137 -0
- package/src/index.ts +87 -0
- package/src/server.ts +14 -0
- package/src/types/index.ts +301 -0
- package/src/types/interactions.ts +354 -0
- package/src/types/responseValidation.ts +29 -0
- package/src/utils/PerformanceMonitor.ts +335 -0
- package/src/utils/responseUtils.ts +51 -0
- package/src/utils/touchDragHelper.ts +211 -0
- package/src/web-components/QTIInteractionBase.ts +302 -0
- package/src/web-components/QTIInteractionElement.ts +118 -0
- package/src/web-components/index.ts +21 -0
- package/tests/conformance/FIXTURE_POLICY.md +29 -0
- package/tests/conformance/README.md +70 -0
- package/tests/conformance/fixtures/baseTypes_all/cases.json +61 -0
- package/tests/conformance/fixtures/baseTypes_all/item.xml +131 -0
- package/tests/conformance/fixtures/baseTypes_all/manifest.json +7 -0
- package/tests/conformance/fixtures/choice_match_correct/cases.json +17 -0
- package/tests/conformance/fixtures/choice_match_correct/item.xml +55 -0
- package/tests/conformance/fixtures/choice_match_correct/manifest.json +14 -0
- package/tests/conformance/fixtures/contains_multiple/cases.json +12 -0
- package/tests/conformance/fixtures/contains_multiple/item.xml +37 -0
- package/tests/conformance/fixtures/contains_multiple/manifest.json +13 -0
- package/tests/conformance/fixtures/isNull_isNotNull/cases.json +9 -0
- package/tests/conformance/fixtures/isNull_isNotNull/item.xml +41 -0
- package/tests/conformance/fixtures/isNull_isNotNull/manifest.json +23 -0
- package/tests/conformance/fixtures/mapResponse_bounds_default_duplicates/cases.json +27 -0
- package/tests/conformance/fixtures/mapResponse_bounds_default_duplicates/item.xml +30 -0
- package/tests/conformance/fixtures/mapResponse_bounds_default_duplicates/manifest.json +15 -0
- package/tests/conformance/fixtures/mapResponse_integer_mapKey/cases.json +11 -0
- package/tests/conformance/fixtures/mapResponse_integer_mapKey/item.xml +26 -0
- package/tests/conformance/fixtures/mapResponse_integer_mapKey/manifest.json +13 -0
- package/tests/conformance/fixtures/mapResponse_mapEntry_caseSensitive_override/cases.json +27 -0
- package/tests/conformance/fixtures/mapResponse_mapEntry_caseSensitive_override/item.xml +32 -0
- package/tests/conformance/fixtures/mapResponse_mapEntry_caseSensitive_override/manifest.json +7 -0
- package/tests/conformance/fixtures/mapResponse_mapping_caseSensitive_defaultValue/cases.json +9 -0
- package/tests/conformance/fixtures/mapResponse_mapping_caseSensitive_defaultValue/item.xml +25 -0
- package/tests/conformance/fixtures/mapResponse_mapping_caseSensitive_defaultValue/manifest.json +12 -0
- package/tests/conformance/fixtures/mapResponse_multiple_cardinality/cases.json +12 -0
- package/tests/conformance/fixtures/mapResponse_multiple_cardinality/item.xml +35 -0
- package/tests/conformance/fixtures/mapResponse_multiple_cardinality/manifest.json +11 -0
- package/tests/conformance/fixtures/mapping_bounds_upperLower/cases.json +12 -0
- package/tests/conformance/fixtures/mapping_bounds_upperLower/item.xml +37 -0
- package/tests/conformance/fixtures/mapping_bounds_upperLower/manifest.json +11 -0
- package/tests/conformance/fixtures/mapping_mapResponse/cases.json +22 -0
- package/tests/conformance/fixtures/mapping_mapResponse/item.xml +53 -0
- package/tests/conformance/fixtures/mapping_mapResponse/manifest.json +16 -0
- package/tests/conformance/fixtures/member_multiple/cases.json +12 -0
- package/tests/conformance/fixtures/member_multiple/item.xml +37 -0
- package/tests/conformance/fixtures/member_multiple/manifest.json +13 -0
- package/tests/conformance/fixtures/modalFeedback_basic/cases.json +17 -0
- package/tests/conformance/fixtures/modalFeedback_basic/item.xml +52 -0
- package/tests/conformance/fixtures/modalFeedback_basic/manifest.json +13 -0
- package/tests/conformance/fixtures/multiple_unordered_match/cases.json +17 -0
- package/tests/conformance/fixtures/multiple_unordered_match/item.xml +44 -0
- package/tests/conformance/fixtures/multiple_unordered_match/manifest.json +11 -0
- package/tests/conformance/fixtures/ordered_match/cases.json +17 -0
- package/tests/conformance/fixtures/ordered_match/item.xml +48 -0
- package/tests/conformance/fixtures/ordered_match/manifest.json +13 -0
- package/tests/conformance/fixtures/outcomeProcessing_derive_percent_passfail/cases.json +27 -0
- package/tests/conformance/fixtures/outcomeProcessing_derive_percent_passfail/item.xml +103 -0
- package/tests/conformance/fixtures/outcomeProcessing_derive_percent_passfail/manifest.json +7 -0
- package/tests/conformance/fixtures/outcomeProcessing_exitTest/cases.json +15 -0
- package/tests/conformance/fixtures/outcomeProcessing_exitTest/item.xml +33 -0
- package/tests/conformance/fixtures/outcomeProcessing_exitTest/manifest.json +10 -0
- package/tests/conformance/fixtures/outcomeProcessing_numberExpressions/cases.json +14 -0
- package/tests/conformance/fixtures/outcomeProcessing_numberExpressions/item.xml +26 -0
- package/tests/conformance/fixtures/outcomeProcessing_numberExpressions/manifest.json +8 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeCondition_equal/cases.json +17 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeCondition_equal/item.xml +43 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeCondition_equal/manifest.json +16 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeCondition_toBoolean/cases.json +27 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeCondition_toBoolean/item.xml +37 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeCondition_toBoolean/manifest.json +16 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeMaximum/cases.json +14 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeMaximum/item.xml +35 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeMaximum/manifest.json +8 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeMinimum/cases.json +14 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeMinimum/item.xml +35 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeMinimum/manifest.json +8 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeProcessingFragment/cases.json +15 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeProcessingFragment/item.xml +39 -0
- package/tests/conformance/fixtures/outcomeProcessing_outcomeProcessingFragment/manifest.json +11 -0
- package/tests/conformance/fixtures/outcomeProcessing_testVariables/cases.json +14 -0
- package/tests/conformance/fixtures/outcomeProcessing_testVariables/item.xml +35 -0
- package/tests/conformance/fixtures/outcomeProcessing_testVariables/manifest.json +8 -0
- package/tests/conformance/fixtures/pair_unordered_match/cases.json +22 -0
- package/tests/conformance/fixtures/pair_unordered_match/item.xml +44 -0
- package/tests/conformance/fixtures/pair_unordered_match/manifest.json +13 -0
- package/tests/conformance/fixtures/positionObject_coordinateSystem_aspectRatio/cases.json +95 -0
- package/tests/conformance/fixtures/positionObject_coordinateSystem_aspectRatio/item.xml +53 -0
- package/tests/conformance/fixtures/positionObject_mapResponsePoint_scoring/cases.json +115 -0
- package/tests/conformance/fixtures/positionObject_mapResponsePoint_scoring/item.xml +45 -0
- package/tests/conformance/fixtures/positionObject_mapResponse_wrong/cases.json +29 -0
- package/tests/conformance/fixtures/positionObject_mapResponse_wrong/item.xml +40 -0
- package/tests/conformance/fixtures/responseCondition_elseIf_chain/cases.json +10 -0
- package/tests/conformance/fixtures/responseCondition_elseIf_chain/item.xml +49 -0
- package/tests/conformance/fixtures/responseCondition_elseIf_chain/manifest.json +15 -0
- package/tests/conformance/fixtures/responseProcessing_anyN/cases.json +27 -0
- package/tests/conformance/fixtures/responseProcessing_anyN/item.xml +47 -0
- package/tests/conformance/fixtures/responseProcessing_anyN/manifest.json +16 -0
- package/tests/conformance/fixtures/responseProcessing_anyN_allN/cases.json +27 -0
- package/tests/conformance/fixtures/responseProcessing_anyN_allN/item.xml +69 -0
- package/tests/conformance/fixtures/responseProcessing_anyN_allN/manifest.json +23 -0
- package/tests/conformance/fixtures/responseProcessing_arithmetic_nulls/cases.json +15 -0
- package/tests/conformance/fixtures/responseProcessing_arithmetic_nulls/item.xml +58 -0
- package/tests/conformance/fixtures/responseProcessing_arithmetic_nulls/manifest.json +15 -0
- package/tests/conformance/fixtures/responseProcessing_boolean_compare_ops/cases.json +37 -0
- package/tests/conformance/fixtures/responseProcessing_boolean_compare_ops/item.xml +94 -0
- package/tests/conformance/fixtures/responseProcessing_boolean_compare_ops/manifest.json +27 -0
- package/tests/conformance/fixtures/responseProcessing_containerSize_index/cases.json +33 -0
- package/tests/conformance/fixtures/responseProcessing_containerSize_index/item.xml +79 -0
- package/tests/conformance/fixtures/responseProcessing_containerSize_index/manifest.json +16 -0
- package/tests/conformance/fixtures/responseProcessing_customOperator/cases.json +15 -0
- package/tests/conformance/fixtures/responseProcessing_customOperator/item.xml +30 -0
- package/tests/conformance/fixtures/responseProcessing_customOperator/manifest.json +12 -0
- package/tests/conformance/fixtures/responseProcessing_default_expression/cases.json +12 -0
- package/tests/conformance/fixtures/responseProcessing_default_expression/item.xml +48 -0
- package/tests/conformance/fixtures/responseProcessing_default_expression/manifest.json +13 -0
- package/tests/conformance/fixtures/responseProcessing_deleteValue_repeat/cases.json +29 -0
- package/tests/conformance/fixtures/responseProcessing_deleteValue_repeat/item.xml +68 -0
- package/tests/conformance/fixtures/responseProcessing_deleteValue_repeat/manifest.json +17 -0
- package/tests/conformance/fixtures/responseProcessing_equalRounded_tolerance/cases.json +39 -0
- package/tests/conformance/fixtures/responseProcessing_equalRounded_tolerance/item.xml +39 -0
- package/tests/conformance/fixtures/responseProcessing_equalRounded_tolerance/manifest.json +17 -0
- package/tests/conformance/fixtures/responseProcessing_exitResponse/cases.json +12 -0
- package/tests/conformance/fixtures/responseProcessing_exitResponse/item.xml +25 -0
- package/tests/conformance/fixtures/responseProcessing_exitResponse/manifest.json +12 -0
- package/tests/conformance/fixtures/responseProcessing_inside_duration_statsOperator/cases.json +26 -0
- package/tests/conformance/fixtures/responseProcessing_inside_duration_statsOperator/item.xml +134 -0
- package/tests/conformance/fixtures/responseProcessing_inside_duration_statsOperator/manifest.json +16 -0
- package/tests/conformance/fixtures/responseProcessing_lookupTable_interpolationTable/cases.json +27 -0
- package/tests/conformance/fixtures/responseProcessing_lookupTable_interpolationTable/item.xml +33 -0
- package/tests/conformance/fixtures/responseProcessing_lookupTable_interpolationTable/manifest.json +13 -0
- package/tests/conformance/fixtures/responseProcessing_lookupTable_matchTable/cases.json +22 -0
- package/tests/conformance/fixtures/responseProcessing_lookupTable_matchTable/item.xml +33 -0
- package/tests/conformance/fixtures/responseProcessing_lookupTable_matchTable/manifest.json +13 -0
- package/tests/conformance/fixtures/responseProcessing_mapOutcome_basic/cases.json +27 -0
- package/tests/conformance/fixtures/responseProcessing_mapOutcome_basic/item.xml +40 -0
- package/tests/conformance/fixtures/responseProcessing_mapOutcome_basic/manifest.json +18 -0
- package/tests/conformance/fixtures/responseProcessing_mapResponsePoint_basic/cases.json +22 -0
- package/tests/conformance/fixtures/responseProcessing_mapResponsePoint_basic/item.xml +33 -0
- package/tests/conformance/fixtures/responseProcessing_mapResponsePoint_basic/manifest.json +12 -0
- package/tests/conformance/fixtures/responseProcessing_mapResponse_directedPair_ordered/cases.json +29 -0
- package/tests/conformance/fixtures/responseProcessing_mapResponse_directedPair_ordered/item.xml +26 -0
- package/tests/conformance/fixtures/responseProcessing_mapResponse_directedPair_ordered/manifest.json +7 -0
- package/tests/conformance/fixtures/responseProcessing_mapResponse_directedPair_whitespace/cases.json +23 -0
- package/tests/conformance/fixtures/responseProcessing_mapResponse_directedPair_whitespace/item.xml +28 -0
- package/tests/conformance/fixtures/responseProcessing_mapResponse_directedPair_whitespace/manifest.json +11 -0
- package/tests/conformance/fixtures/responseProcessing_mapResponse_pair_unordered/cases.json +29 -0
- package/tests/conformance/fixtures/responseProcessing_mapResponse_pair_unordered/item.xml +26 -0
- package/tests/conformance/fixtures/responseProcessing_mapResponse_pair_unordered/manifest.json +7 -0
- package/tests/conformance/fixtures/responseProcessing_mathOperator_integerUtils/cases.json +31 -0
- package/tests/conformance/fixtures/responseProcessing_mathOperator_integerUtils/item.xml +102 -0
- package/tests/conformance/fixtures/responseProcessing_mathOperator_integerUtils/manifest.json +18 -0
- package/tests/conformance/fixtures/responseProcessing_math_max_min_round_truncate/cases.json +22 -0
- package/tests/conformance/fixtures/responseProcessing_math_max_min_round_truncate/item.xml +45 -0
- package/tests/conformance/fixtures/responseProcessing_math_max_min_round_truncate/manifest.json +21 -0
- package/tests/conformance/fixtures/responseProcessing_misc_math_string_stats/cases.json +26 -0
- package/tests/conformance/fixtures/responseProcessing_misc_math_string_stats/item.xml +157 -0
- package/tests/conformance/fixtures/responseProcessing_misc_math_string_stats/manifest.json +22 -0
- package/tests/conformance/fixtures/responseProcessing_notEqual_basic/cases.json +22 -0
- package/tests/conformance/fixtures/responseProcessing_notEqual_basic/item.xml +39 -0
- package/tests/conformance/fixtures/responseProcessing_notEqual_basic/manifest.json +18 -0
- package/tests/conformance/fixtures/responseProcessing_patternMatch_caseSensitive/cases.json +22 -0
- package/tests/conformance/fixtures/responseProcessing_patternMatch_caseSensitive/item.xml +51 -0
- package/tests/conformance/fixtures/responseProcessing_patternMatch_caseSensitive/manifest.json +19 -0
- package/tests/conformance/fixtures/responseProcessing_preCondition/cases.json +31 -0
- package/tests/conformance/fixtures/responseProcessing_preCondition/item.xml +44 -0
- package/tests/conformance/fixtures/responseProcessing_preCondition/manifest.json +12 -0
- package/tests/conformance/fixtures/responseProcessing_random_lookupOutcomeValue/cases.json +27 -0
- package/tests/conformance/fixtures/responseProcessing_random_lookupOutcomeValue/item.xml +42 -0
- package/tests/conformance/fixtures/responseProcessing_random_lookupOutcomeValue/manifest.json +14 -0
- package/tests/conformance/fixtures/responseProcessing_record_fieldValue/cases.json +12 -0
- package/tests/conformance/fixtures/responseProcessing_record_fieldValue/item.xml +40 -0
- package/tests/conformance/fixtures/responseProcessing_record_fieldValue/manifest.json +17 -0
- package/tests/conformance/fixtures/responseProcessing_responseCondition_toBoolean/cases.json +17 -0
- package/tests/conformance/fixtures/responseProcessing_responseCondition_toBoolean/item.xml +63 -0
- package/tests/conformance/fixtures/responseProcessing_responseCondition_toBoolean/manifest.json +9 -0
- package/tests/conformance/fixtures/responseProcessing_responseProcessingFragment/cases.json +12 -0
- package/tests/conformance/fixtures/responseProcessing_responseProcessingFragment/item.xml +40 -0
- package/tests/conformance/fixtures/responseProcessing_responseProcessingFragment/manifest.json +13 -0
- package/tests/conformance/fixtures/responseProcessing_setDefaultValue/cases.json +15 -0
- package/tests/conformance/fixtures/responseProcessing_setDefaultValue/item.xml +67 -0
- package/tests/conformance/fixtures/responseProcessing_setDefaultValue/manifest.json +9 -0
- package/tests/conformance/fixtures/responseProcessing_setResponseValue_setDefaultValue/cases.json +27 -0
- package/tests/conformance/fixtures/responseProcessing_setResponseValue_setDefaultValue/item.xml +69 -0
- package/tests/conformance/fixtures/responseProcessing_setResponseValue_setDefaultValue/manifest.json +10 -0
- package/tests/conformance/fixtures/responseProcessing_subtract_divide/cases.json +15 -0
- package/tests/conformance/fixtures/responseProcessing_subtract_divide/item.xml +35 -0
- package/tests/conformance/fixtures/responseProcessing_subtract_divide/manifest.json +11 -0
- package/tests/conformance/fixtures/responseProcessing_template_map_response/cases.json +17 -0
- package/tests/conformance/fixtures/responseProcessing_template_map_response/item.xml +31 -0
- package/tests/conformance/fixtures/responseProcessing_template_map_response/manifest.json +8 -0
- package/tests/conformance/fixtures/responseProcessing_template_map_response_point/cases.json +22 -0
- package/tests/conformance/fixtures/responseProcessing_template_map_response_point/item.xml +27 -0
- package/tests/conformance/fixtures/responseProcessing_template_map_response_point/manifest.json +8 -0
- package/tests/conformance/fixtures/responseProcessing_template_match_correct/cases.json +17 -0
- package/tests/conformance/fixtures/responseProcessing_template_match_correct/item.xml +34 -0
- package/tests/conformance/fixtures/responseProcessing_template_match_correct/manifest.json +8 -0
- package/tests/conformance/fixtures/responseProcessing_template_match_correct_directedPair/cases.json +22 -0
- package/tests/conformance/fixtures/responseProcessing_template_match_correct_directedPair/item.xml +30 -0
- package/tests/conformance/fixtures/responseProcessing_template_match_correct_directedPair/manifest.json +7 -0
- package/tests/conformance/fixtures/responseProcessing_template_match_correct_two_responses/cases.json +17 -0
- package/tests/conformance/fixtures/responseProcessing_template_match_correct_two_responses/item.xml +30 -0
- package/tests/conformance/fixtures/responseProcessing_template_match_correct_two_responses/manifest.json +9 -0
- package/tests/conformance/fixtures/responseProcessing_template_match_nothing/cases.json +17 -0
- package/tests/conformance/fixtures/responseProcessing_template_match_nothing/item.xml +30 -0
- package/tests/conformance/fixtures/responseProcessing_template_match_nothing/manifest.json +8 -0
- package/tests/conformance/fixtures/responseProcessing_xiInclude/cases.json +14 -0
- package/tests/conformance/fixtures/responseProcessing_xiInclude/item.xml +25 -0
- package/tests/conformance/fixtures/responseProcessing_xiInclude/manifest.json +9 -0
- package/tests/conformance/fixtures/stringMatch_caseSensitive_substring/cases.json +12 -0
- package/tests/conformance/fixtures/stringMatch_caseSensitive_substring/item.xml +52 -0
- package/tests/conformance/fixtures/stringMatch_caseSensitive_substring/manifest.json +14 -0
- package/tests/conformance/fixtures/templateProcessing_exitTemplate/cases.json +12 -0
- package/tests/conformance/fixtures/templateProcessing_exitTemplate/item.xml +48 -0
- package/tests/conformance/fixtures/templateProcessing_exitTemplate/manifest.json +13 -0
- package/tests/conformance/fixtures/templateProcessing_setCorrectResponse/cases.json +27 -0
- package/tests/conformance/fixtures/templateProcessing_setCorrectResponse/item.xml +74 -0
- package/tests/conformance/fixtures/templateProcessing_setCorrectResponse/manifest.json +22 -0
- package/tests/conformance/fixtures/templateProcessing_templateCondition_branching/cases.json +17 -0
- package/tests/conformance/fixtures/templateProcessing_templateCondition_branching/item.xml +133 -0
- package/tests/conformance/fixtures/templateProcessing_templateCondition_branching/manifest.json +22 -0
- package/tests/conformance/fixtures/templateProcessing_templateCondition_toBoolean/cases.json +12 -0
- package/tests/conformance/fixtures/templateProcessing_templateCondition_toBoolean/item.xml +107 -0
- package/tests/conformance/fixtures/templateProcessing_templateCondition_toBoolean/manifest.json +16 -0
- package/tests/conformance/fixtures/templateProcessing_templateConstraint/cases.json +15 -0
- package/tests/conformance/fixtures/templateProcessing_templateConstraint/item.xml +52 -0
- package/tests/conformance/fixtures/templateProcessing_templateConstraint/manifest.json +14 -0
- package/tests/conformance/fixtures/template_randomFloat_roundTo_mathConstant/cases.json +16 -0
- package/tests/conformance/fixtures/template_randomFloat_roundTo_mathConstant/item.xml +49 -0
- package/tests/conformance/fixtures/template_randomFloat_roundTo_mathConstant/manifest.json +15 -0
- package/tests/conformance/fixtures/template_randomInteger_printedVariable/cases.json +16 -0
- package/tests/conformance/fixtures/template_randomInteger_printedVariable/item.xml +38 -0
- package/tests/conformance/fixtures/template_randomInteger_printedVariable/manifest.json +13 -0
- package/tests/conformance/harness.ts +84 -0
- package/tests/conformance/run-fixtures.test.ts +127 -0
- package/tests/conformance/scripts/normalize-covers.ts +152 -0
- package/tests/conformance/types.ts +53 -0
- package/tests/core/Player.test.ts +563 -0
- package/tests/core/adaptiveItems.test.ts +442 -0
- package/tests/core/correctResponse.test.ts +280 -0
- package/tests/core/interaction-plumbing.test.ts +141 -0
- package/tests/core/modalFeedback.test.ts +308 -0
- package/tests/core/outcome-reset.test.ts +43 -0
- package/tests/extraction/extractors/associateExtractor.test.ts +182 -0
- package/tests/extraction/extractors/choiceExtractor.test.ts +239 -0
- package/tests/extraction/extractors/customExtractor.test.ts +83 -0
- package/tests/extraction/extractors/drawingExtractor.test.ts +94 -0
- package/tests/extraction/extractors/endAttemptExtractor.test.ts +188 -0
- package/tests/extraction/extractors/extendedTextExtractor.test.ts +178 -0
- package/tests/extraction/extractors/gapMatchExtractor.test.ts +318 -0
- package/tests/extraction/extractors/graphicAssociateExtractor.test.ts +260 -0
- package/tests/extraction/extractors/graphicGapMatchExtractor.test.ts +320 -0
- package/tests/extraction/extractors/graphicOrderExtractor.test.ts +223 -0
- package/tests/extraction/extractors/hotspotExtractor.test.ts +298 -0
- package/tests/extraction/extractors/hottextExtractor.test.ts +240 -0
- package/tests/extraction/extractors/inlineChoiceExtractor.test.ts +292 -0
- package/tests/extraction/extractors/matchExtractor.test.ts +241 -0
- package/tests/extraction/extractors/mediaExtractor.test.ts +295 -0
- package/tests/extraction/extractors/orderExtractor.test.ts +212 -0
- package/tests/extraction/extractors/positionObjectExtractor.test.ts +363 -0
- package/tests/extraction/extractors/selectPointExtractor.test.ts +214 -0
- package/tests/extraction/extractors/sliderExtractor.test.ts +293 -0
- package/tests/extraction/extractors/textEntryExtractor.test.ts +133 -0
- package/tests/extraction/extractors/uploadExtractor.test.ts +119 -0
- package/tests/extraction/test-utils.ts +57 -0
- package/tests/iframe/host.test.ts +145 -0
- package/tests/iframe/protocol.test.ts +42 -0
- package/tests/performance/player-benchmarks.bench.ts +191 -0
- package/tests/security/dos-limits.test.ts +56 -0
- package/tests/security/xss-prevention.test.ts +522 -0
- package/tsconfig.json +20 -0
- package/tsconfig.svelte.json +14 -0
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML Sanitizer for QTI Content
|
|
3
|
+
*
|
|
4
|
+
* Removes dangerous HTML/scripts while preserving safe educational content.
|
|
5
|
+
* Protects against XSS attacks via:
|
|
6
|
+
* - Script tags
|
|
7
|
+
* - Event handlers (onclick, onerror, onload, etc.)
|
|
8
|
+
* - Dangerous protocols (javascript:, data:text/html)
|
|
9
|
+
* - Unsafe iframe/object sources
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { type HTMLElement, parse } from 'node-html-parser';
|
|
13
|
+
import type { PlayerSecurityConfig, UrlPolicyConfig } from '../types/index.js';
|
|
14
|
+
import { normalizeParsingLimits } from './parsingLimits.js';
|
|
15
|
+
import { sanitizeResourceUrl } from './urlPolicy.js';
|
|
16
|
+
|
|
17
|
+
export interface SanitizeHtmlOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Security config (URL policy and tag allowances).
|
|
20
|
+
* Defaults are conservative for same-DOM embedding.
|
|
21
|
+
*/
|
|
22
|
+
security?: PlayerSecurityConfig;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
class HtmlLimitExceeded extends Error {
|
|
26
|
+
constructor(message: string) {
|
|
27
|
+
super(message);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function policyFromOptions(options?: SanitizeHtmlOptions): UrlPolicyConfig | undefined {
|
|
32
|
+
return options?.security?.urlPolicy;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function allowObjectEmbeds(options?: SanitizeHtmlOptions): boolean {
|
|
36
|
+
return options?.security?.allowObjectEmbeds === true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function allowIframes(options?: SanitizeHtmlOptions): boolean {
|
|
40
|
+
return options?.security?.allowIframes === true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function sanitizeUrlAttr(value: string, policy: UrlPolicyConfig | undefined, kind: 'img' | 'media' | 'object' | 'link' | 'any'): string {
|
|
44
|
+
return sanitizeResourceUrl(value, policy, kind) ?? '#';
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function sanitizeSrcset(value: string, policy: UrlPolicyConfig | undefined): string {
|
|
48
|
+
// srcset grammar is complex; we implement a conservative filter:
|
|
49
|
+
// split by commas into candidate strings; sanitize the URL portion of each candidate.
|
|
50
|
+
const parts = value
|
|
51
|
+
.split(',')
|
|
52
|
+
.map((p) => p.trim())
|
|
53
|
+
.filter(Boolean);
|
|
54
|
+
const out: string[] = [];
|
|
55
|
+
for (const part of parts) {
|
|
56
|
+
// URL is the first token; the rest are descriptors (e.g., 1x, 320w).
|
|
57
|
+
const tokens = part.split(/\s+/).filter(Boolean);
|
|
58
|
+
if (tokens.length === 0) continue;
|
|
59
|
+
const url = tokens[0]!;
|
|
60
|
+
const sanitized = sanitizeResourceUrl(url, policy, 'img');
|
|
61
|
+
if (!sanitized) continue;
|
|
62
|
+
out.push([sanitized, ...tokens.slice(1)].join(' '));
|
|
63
|
+
}
|
|
64
|
+
return out.join(', ');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Recursively sanitize an HTML element and its children
|
|
69
|
+
*/
|
|
70
|
+
function sanitizeElement(
|
|
71
|
+
element: HTMLElement,
|
|
72
|
+
options: SanitizeHtmlOptions | undefined,
|
|
73
|
+
limitsState: { enabled: boolean; nodes: number; maxNodes: number; maxDepth: number },
|
|
74
|
+
depth: number
|
|
75
|
+
): void {
|
|
76
|
+
if (limitsState.enabled) {
|
|
77
|
+
if (depth > limitsState.maxDepth) {
|
|
78
|
+
throw new HtmlLimitExceeded(`HTML depth exceeds maxHtmlDepth (${depth} > ${limitsState.maxDepth})`);
|
|
79
|
+
}
|
|
80
|
+
limitsState.nodes++;
|
|
81
|
+
if (limitsState.nodes > limitsState.maxNodes) {
|
|
82
|
+
throw new HtmlLimitExceeded(
|
|
83
|
+
`HTML node count exceeds maxHtmlNodes (${limitsState.nodes} > ${limitsState.maxNodes})`
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const tagName = (element as any).rawTagName?.toLowerCase();
|
|
89
|
+
|
|
90
|
+
// Remove script tags entirely
|
|
91
|
+
if (tagName === 'script') {
|
|
92
|
+
element.remove();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Remove high-risk elements by default (same-DOM embedding).
|
|
97
|
+
if (tagName === 'iframe' && !allowIframes(options)) {
|
|
98
|
+
element.remove();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if ((tagName === 'object' || tagName === 'embed') && !allowObjectEmbeds(options)) {
|
|
102
|
+
element.remove();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (tagName === 'base' || tagName === 'meta' || tagName === 'link' || tagName === 'style' || tagName === 'foreignobject') {
|
|
106
|
+
element.remove();
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Remove dangerous event handler attributes (case-insensitive).
|
|
111
|
+
const attrs = element.attributes || {};
|
|
112
|
+
for (const attrName of Object.keys(attrs)) {
|
|
113
|
+
if (attrName.toLowerCase().startsWith('on')) {
|
|
114
|
+
element.removeAttribute(attrName);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Sanitize URL attributes
|
|
119
|
+
const policy = policyFromOptions(options);
|
|
120
|
+
for (const attrName of Object.keys(attrs)) {
|
|
121
|
+
const lower = attrName.toLowerCase();
|
|
122
|
+
const value = element.getAttribute(attrName);
|
|
123
|
+
if (!value) continue;
|
|
124
|
+
|
|
125
|
+
// Block srcdoc everywhere (HTML document string).
|
|
126
|
+
if (lower === 'srcdoc') {
|
|
127
|
+
element.removeAttribute(attrName);
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (lower === 'srcset') {
|
|
132
|
+
const sanitized = sanitizeSrcset(value, policy);
|
|
133
|
+
if (!sanitized) element.removeAttribute(attrName);
|
|
134
|
+
else if (sanitized !== value) element.setAttribute(attrName, sanitized);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// URL-bearing attributes (include SVG xlink:href).
|
|
139
|
+
if (
|
|
140
|
+
lower === 'href' ||
|
|
141
|
+
lower === 'xlink:href' ||
|
|
142
|
+
lower === 'src' ||
|
|
143
|
+
lower === 'data' ||
|
|
144
|
+
lower === 'action' ||
|
|
145
|
+
lower === 'formaction' ||
|
|
146
|
+
lower === 'poster'
|
|
147
|
+
) {
|
|
148
|
+
const kind =
|
|
149
|
+
tagName === 'img' ? 'img' :
|
|
150
|
+
tagName === 'audio' || tagName === 'video' || tagName === 'source' ? 'media' :
|
|
151
|
+
tagName === 'object' || tagName === 'embed' ? 'object' :
|
|
152
|
+
tagName === 'a' ? 'link' :
|
|
153
|
+
'any';
|
|
154
|
+
const sanitized = sanitizeUrlAttr(value, policy, kind);
|
|
155
|
+
if (sanitized !== value) element.setAttribute(attrName, sanitized);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Recursively sanitize children
|
|
160
|
+
const children = element.childNodes.filter((n) => (n as any).rawTagName) as HTMLElement[];
|
|
161
|
+
for (const child of children) {
|
|
162
|
+
sanitizeElement(child, options, limitsState, depth + 1);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Sanitize HTML string by removing XSS vectors
|
|
168
|
+
*
|
|
169
|
+
* @param html - HTML string to sanitize
|
|
170
|
+
* @returns Sanitized HTML string safe for rendering
|
|
171
|
+
*/
|
|
172
|
+
export function sanitizeHtml(html: string, options?: SanitizeHtmlOptions): string {
|
|
173
|
+
if (!html || typeof html !== 'string') {
|
|
174
|
+
return '';
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
try {
|
|
178
|
+
const limits = normalizeParsingLimits(options?.security);
|
|
179
|
+
if (limits.enabled) {
|
|
180
|
+
const TE: typeof TextEncoder | undefined = (globalThis as any).TextEncoder;
|
|
181
|
+
const bytes = TE ? new TE().encode(html).byteLength : html.length * 2;
|
|
182
|
+
if (bytes > limits.maxHtmlBytes) {
|
|
183
|
+
return '';
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Parse HTML
|
|
188
|
+
const root = parse(html, {
|
|
189
|
+
comment: false, // Remove comments (potential XSS vector)
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// Sanitize all elements
|
|
193
|
+
const elements = root.childNodes.filter((n) => (n as any).rawTagName) as HTMLElement[];
|
|
194
|
+
const limitsState = {
|
|
195
|
+
enabled: limits.enabled,
|
|
196
|
+
nodes: 0,
|
|
197
|
+
maxNodes: limits.maxHtmlNodes,
|
|
198
|
+
maxDepth: limits.maxHtmlDepth,
|
|
199
|
+
};
|
|
200
|
+
for (const element of elements) {
|
|
201
|
+
sanitizeElement(element, options, limitsState, 1);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Return sanitized HTML
|
|
205
|
+
return root.toString();
|
|
206
|
+
} catch (error) {
|
|
207
|
+
if (error instanceof HtmlLimitExceeded) {
|
|
208
|
+
return '';
|
|
209
|
+
}
|
|
210
|
+
// If parsing fails, return empty string (safer than returning potentially malicious content)
|
|
211
|
+
console.error('HTML sanitization failed:', error);
|
|
212
|
+
return '';
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Sanitize text content in choice labels, prompts, etc.
|
|
218
|
+
* This is for content that gets serialized to JSON and displayed in UI.
|
|
219
|
+
*
|
|
220
|
+
* @param text - Text content that may contain HTML
|
|
221
|
+
* @returns Sanitized text
|
|
222
|
+
*/
|
|
223
|
+
export function sanitizeTextContent(text: string, options?: SanitizeHtmlOptions): string {
|
|
224
|
+
if (!text || typeof text !== 'string') {
|
|
225
|
+
return '';
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// If it looks like HTML (contains tags), sanitize it
|
|
229
|
+
if (/<[^>]+>/.test(text)) {
|
|
230
|
+
return sanitizeHtml(text, options);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Plain text - return as-is
|
|
234
|
+
return text;
|
|
235
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { HtmlContent, TrustedTypesHtml } from '../types/index.js';
|
|
2
|
+
|
|
3
|
+
type TrustedTypesPolicyLike = {
|
|
4
|
+
createHTML: (input: string) => TrustedTypesHtml;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
function getTrustedTypesGlobal(): any {
|
|
8
|
+
return (globalThis as any).trustedTypes;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const policyCache = new Map<string, TrustedTypesPolicyLike>();
|
|
12
|
+
|
|
13
|
+
function getOrCreatePolicy(policyName: string): TrustedTypesPolicyLike | null {
|
|
14
|
+
const existing = policyCache.get(policyName);
|
|
15
|
+
if (existing) return existing;
|
|
16
|
+
|
|
17
|
+
const tt = getTrustedTypesGlobal();
|
|
18
|
+
if (!tt?.createPolicy) return null;
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const policy = tt.createPolicy(policyName, {
|
|
22
|
+
createHTML: (s: string) => s,
|
|
23
|
+
});
|
|
24
|
+
// Policy may be blocked/throw depending on CSP allowlist.
|
|
25
|
+
if (!policy?.createHTML) return null;
|
|
26
|
+
policyCache.set(policyName, policy as TrustedTypesPolicyLike);
|
|
27
|
+
return policy as TrustedTypesPolicyLike;
|
|
28
|
+
} catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Convert a sanitized HTML string into a Trusted Types HTML value when:
|
|
35
|
+
* - the host configured a policy name, and
|
|
36
|
+
* - the browser supports Trusted Types, and
|
|
37
|
+
* - CSP allows creating/using that policy.
|
|
38
|
+
*
|
|
39
|
+
* Otherwise returns the original string.
|
|
40
|
+
*/
|
|
41
|
+
export function toTrustedHtml(html: string, policyName?: string): HtmlContent {
|
|
42
|
+
if (!policyName) return html;
|
|
43
|
+
if (!html) return '';
|
|
44
|
+
|
|
45
|
+
const policy = getOrCreatePolicy(policyName);
|
|
46
|
+
if (!policy) return html;
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
return policy.createHTML(html);
|
|
50
|
+
} catch {
|
|
51
|
+
return html;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Normalize HtmlContent back to a string.
|
|
57
|
+
* Useful when you need to perform string operations (replace/substr/etc) before re-wrapping.
|
|
58
|
+
*/
|
|
59
|
+
export function htmlToString(html: HtmlContent): string {
|
|
60
|
+
return typeof html === 'string' ? html : String(html as any);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { UrlPolicyConfig } from '../types/index.js';
|
|
2
|
+
|
|
3
|
+
export type UrlKind = 'img' | 'media' | 'object' | 'link' | 'any';
|
|
4
|
+
|
|
5
|
+
const DEFAULT_POLICY: Required<Pick<
|
|
6
|
+
UrlPolicyConfig,
|
|
7
|
+
| 'allowHttps'
|
|
8
|
+
| 'allowHttp'
|
|
9
|
+
| 'allowProtocolRelative'
|
|
10
|
+
| 'allowDataImages'
|
|
11
|
+
| 'allowSvgDataImages'
|
|
12
|
+
>> = {
|
|
13
|
+
allowHttps: true,
|
|
14
|
+
allowHttp: false,
|
|
15
|
+
allowProtocolRelative: false,
|
|
16
|
+
allowDataImages: true,
|
|
17
|
+
allowSvgDataImages: false,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function normalizePolicy(policy?: UrlPolicyConfig): UrlPolicyConfig & typeof DEFAULT_POLICY {
|
|
21
|
+
return { ...DEFAULT_POLICY, ...(policy ?? {}) };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isDangerousScheme(u: string): boolean {
|
|
25
|
+
const s = u.toLowerCase().trim();
|
|
26
|
+
return s.startsWith('javascript:') || s.startsWith('vbscript:') || s.startsWith('data:text/html');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function allowHost(hostname: string, policy: UrlPolicyConfig): boolean {
|
|
30
|
+
const allow = policy.allowedHosts;
|
|
31
|
+
if (!allow || allow.length === 0) return true;
|
|
32
|
+
return allow.includes(hostname);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function sanitizeDataUrl(raw: string, kind: UrlKind, policy: UrlPolicyConfig & typeof DEFAULT_POLICY): string | null {
|
|
36
|
+
const s = raw.toLowerCase().trim();
|
|
37
|
+
if (!s.startsWith('data:')) return null;
|
|
38
|
+
if (kind !== 'img') return null;
|
|
39
|
+
if (!policy.allowDataImages) return null;
|
|
40
|
+
if (!s.startsWith('data:image/')) return null;
|
|
41
|
+
if (!policy.allowSvgDataImages && s.startsWith('data:image/svg+xml')) return null;
|
|
42
|
+
return raw.trim();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Sanitize a URL used for embedding resources (img/audio/video/object/link).
|
|
47
|
+
* Returns null when the URL should be blocked.
|
|
48
|
+
*/
|
|
49
|
+
export function sanitizeResourceUrl(raw: string, policy?: UrlPolicyConfig, kind: UrlKind = 'any'): string | null {
|
|
50
|
+
const p = normalizePolicy(policy);
|
|
51
|
+
const s = String(raw ?? '').trim();
|
|
52
|
+
if (!s) return null;
|
|
53
|
+
|
|
54
|
+
if (isDangerousScheme(s)) return null;
|
|
55
|
+
|
|
56
|
+
// data: URLs (context-sensitive)
|
|
57
|
+
const dataOk = sanitizeDataUrl(s, kind, p);
|
|
58
|
+
if (dataOk) return dataOk;
|
|
59
|
+
if (s.toLowerCase().trim().startsWith('data:')) return null;
|
|
60
|
+
|
|
61
|
+
// Protocol-relative URLs are treated as network loads and blocked by default.
|
|
62
|
+
if (s.startsWith('//')) return p.allowProtocolRelative ? `https:${s}` : null;
|
|
63
|
+
|
|
64
|
+
// Relative/absolute-path URLs: allow and optionally resolve.
|
|
65
|
+
if (s.startsWith('/') || s.startsWith('./') || s.startsWith('../')) {
|
|
66
|
+
if (p.assetBaseUrl) {
|
|
67
|
+
try {
|
|
68
|
+
// Strip leading '/' to make URL constructor resolve relative to base path, not origin
|
|
69
|
+
const relativePath = s.startsWith('/') ? s.slice(1) : s;
|
|
70
|
+
return new URL(relativePath, p.assetBaseUrl).toString();
|
|
71
|
+
} catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return s;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Absolute URLs: allow only http/https (by policy) and optional host allowlist.
|
|
79
|
+
let u: URL;
|
|
80
|
+
try {
|
|
81
|
+
u = new URL(s);
|
|
82
|
+
} catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const proto = u.protocol.toLowerCase();
|
|
87
|
+
if (proto === 'https:' && !p.allowHttps) return null;
|
|
88
|
+
if (proto === 'http:' && !p.allowHttp) return null;
|
|
89
|
+
if (proto !== 'https:' && proto !== 'http:') return null;
|
|
90
|
+
|
|
91
|
+
if (!allowHost(u.hostname, p)) return null;
|
|
92
|
+
return u.toString();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MathLive Editor Extension for TipTap
|
|
3
|
+
*
|
|
4
|
+
* Integrates MathLive (visual math editor) with TipTap's Mathematics extension.
|
|
5
|
+
* Provides a modal dialog with MathLive mathfield for editing LaTeX formulas.
|
|
6
|
+
*
|
|
7
|
+
* Note: MathLive CSS is loaded dynamically to avoid build issues with package exports.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { MathfieldElement } from 'mathlive';
|
|
11
|
+
import type { I18nProvider } from '@pie-qti/i18n';
|
|
12
|
+
|
|
13
|
+
const DEFAULT_MATHLIVE_CSS_URL = 'https://cdn.jsdelivr.net/npm/mathlive@0.108.2/dist/mathlive-static.css';
|
|
14
|
+
|
|
15
|
+
// Dynamically import MathLive to register the custom element
|
|
16
|
+
let mathLiveLoaded: Promise<void> | null = null;
|
|
17
|
+
|
|
18
|
+
async function ensureMathLiveLoaded(cssUrl: string = DEFAULT_MATHLIVE_CSS_URL): Promise<void> {
|
|
19
|
+
if (!mathLiveLoaded && typeof window !== 'undefined') {
|
|
20
|
+
mathLiveLoaded = (async () => {
|
|
21
|
+
// Import MathLive to register the <math-field> custom element
|
|
22
|
+
await import('mathlive');
|
|
23
|
+
|
|
24
|
+
// Load MathLive CSS if not already loaded
|
|
25
|
+
if (!document.querySelector('link[href*="mathlive"]')) {
|
|
26
|
+
const link = document.createElement('link');
|
|
27
|
+
link.rel = 'stylesheet';
|
|
28
|
+
link.href = cssUrl;
|
|
29
|
+
document.head.appendChild(link);
|
|
30
|
+
}
|
|
31
|
+
})();
|
|
32
|
+
}
|
|
33
|
+
return mathLiveLoaded || Promise.resolve();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface MathLiveEditorOptions {
|
|
37
|
+
/** Called when user confirms the LaTeX input */
|
|
38
|
+
onConfirm: (latex: string) => void;
|
|
39
|
+
/** Called when user cancels */
|
|
40
|
+
onCancel: () => void;
|
|
41
|
+
/** Initial LaTeX value */
|
|
42
|
+
initialLatex?: string;
|
|
43
|
+
/** Whether this is inline or block math */
|
|
44
|
+
isBlock?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Optional MathLive stylesheet URL. Defaults to jsdelivr CDN.
|
|
47
|
+
* Useful for CSP-restricted deployments (serve from your own domain).
|
|
48
|
+
*/
|
|
49
|
+
cssUrl?: string;
|
|
50
|
+
/** Optional i18n provider for translations */
|
|
51
|
+
i18n?: I18nProvider;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Opens a modal dialog with MathLive editor for LaTeX input
|
|
56
|
+
*/
|
|
57
|
+
export async function openMathLiveEditor(options: MathLiveEditorOptions): Promise<() => void> {
|
|
58
|
+
const { onConfirm, onCancel, initialLatex = '', isBlock = false, cssUrl = DEFAULT_MATHLIVE_CSS_URL, i18n } = options;
|
|
59
|
+
|
|
60
|
+
// Ensure MathLive is loaded and registered before creating the mathfield
|
|
61
|
+
await ensureMathLiveLoaded(cssUrl);
|
|
62
|
+
|
|
63
|
+
// Create modal overlay - use DaisyUI modal classes
|
|
64
|
+
const overlay = document.createElement('div');
|
|
65
|
+
overlay.className = 'modal modal-open';
|
|
66
|
+
overlay.style.cssText = `
|
|
67
|
+
position: fixed;
|
|
68
|
+
top: 0;
|
|
69
|
+
left: 0;
|
|
70
|
+
right: 0;
|
|
71
|
+
bottom: 0;
|
|
72
|
+
background: rgba(0, 0, 0, 0.5);
|
|
73
|
+
display: flex;
|
|
74
|
+
align-items: center;
|
|
75
|
+
justify-content: center;
|
|
76
|
+
z-index: 9999;
|
|
77
|
+
`;
|
|
78
|
+
|
|
79
|
+
// Create modal dialog - use DaisyUI modal-box class
|
|
80
|
+
const modal = document.createElement('div');
|
|
81
|
+
modal.className = 'modal-box max-w-2xl';
|
|
82
|
+
modal.style.cssText = `
|
|
83
|
+
padding: 1.5rem;
|
|
84
|
+
position: relative;
|
|
85
|
+
z-index: 10000;
|
|
86
|
+
`;
|
|
87
|
+
|
|
88
|
+
// Create header - use DaisyUI font-bold text-lg
|
|
89
|
+
const header = document.createElement('h3');
|
|
90
|
+
header.textContent = isBlock
|
|
91
|
+
? (i18n?.t('interactions.extendedText.insertBlockMath') ?? 'Insert Block Math')
|
|
92
|
+
: (i18n?.t('interactions.extendedText.insertInlineMath') ?? 'Insert Inline Math');
|
|
93
|
+
header.className = 'font-bold text-lg mb-4';
|
|
94
|
+
|
|
95
|
+
// Create form control wrapper
|
|
96
|
+
const formControl = document.createElement('div');
|
|
97
|
+
formControl.className = 'form-control';
|
|
98
|
+
|
|
99
|
+
// Create label for mathfield
|
|
100
|
+
const label = document.createElement('label');
|
|
101
|
+
label.className = 'label';
|
|
102
|
+
const labelText = document.createElement('span');
|
|
103
|
+
labelText.className = 'label-text';
|
|
104
|
+
labelText.textContent = 'Enter your math expression:';
|
|
105
|
+
label.appendChild(labelText);
|
|
106
|
+
|
|
107
|
+
// Create MathLive mathfield - style like DaisyUI input
|
|
108
|
+
const mathfield = document.createElement('math-field') as MathfieldElement;
|
|
109
|
+
mathfield.value = initialLatex;
|
|
110
|
+
mathfield.className = 'input input-bordered w-full';
|
|
111
|
+
mathfield.style.cssText = `
|
|
112
|
+
display: block;
|
|
113
|
+
min-height: 100px;
|
|
114
|
+
padding: 0.75rem;
|
|
115
|
+
font-size: 1.125rem;
|
|
116
|
+
`;
|
|
117
|
+
|
|
118
|
+
// Set MathLive options for better UX via attributes
|
|
119
|
+
// Use 'manual' mode so keyboard only shows when explicitly requested
|
|
120
|
+
mathfield.setAttribute('virtual-keyboard-mode', 'manual');
|
|
121
|
+
mathfield.setAttribute('smart-mode', 'on');
|
|
122
|
+
// Explicitly enable virtual keyboard
|
|
123
|
+
mathfield.setAttribute('use-shared-virtual-keyboard', 'true');
|
|
124
|
+
// Hide the built-in virtual keyboard toggle
|
|
125
|
+
mathfield.setAttribute('virtual-keyboard-toggle-glyph', '');
|
|
126
|
+
|
|
127
|
+
// Inject CSS to ensure virtual keyboard appears above modal and accepts clicks
|
|
128
|
+
const style = document.createElement('style');
|
|
129
|
+
style.textContent = `
|
|
130
|
+
/* Ensure MathLive virtual keyboard appears above the modal and is clickable */
|
|
131
|
+
.ML__keyboard {
|
|
132
|
+
z-index: 10001 !important;
|
|
133
|
+
pointer-events: auto !important;
|
|
134
|
+
}
|
|
135
|
+
/* Hide the built-in keyboard toggle button in the mathfield - try multiple selectors */
|
|
136
|
+
.ML__virtual-keyboard-toggle,
|
|
137
|
+
.ML__keyboard-toggle,
|
|
138
|
+
math-field::part(virtual-keyboard-toggle) {
|
|
139
|
+
display: none !important;
|
|
140
|
+
visibility: hidden !important;
|
|
141
|
+
opacity: 0 !important;
|
|
142
|
+
pointer-events: none !important;
|
|
143
|
+
}
|
|
144
|
+
`;
|
|
145
|
+
document.head.appendChild(style);
|
|
146
|
+
|
|
147
|
+
// Create LaTeX preview label
|
|
148
|
+
const previewLabel = document.createElement('label');
|
|
149
|
+
previewLabel.className = 'label';
|
|
150
|
+
const previewLabelText = document.createElement('span');
|
|
151
|
+
previewLabelText.className = 'label-text';
|
|
152
|
+
previewLabelText.textContent = 'LaTeX source:';
|
|
153
|
+
previewLabel.appendChild(previewLabelText);
|
|
154
|
+
|
|
155
|
+
// Create LaTeX preview/source - use alert info styling
|
|
156
|
+
const latexPreview = document.createElement('div');
|
|
157
|
+
latexPreview.className = 'alert alert-info';
|
|
158
|
+
latexPreview.style.cssText = `
|
|
159
|
+
font-family: 'Monaco', 'Courier New', monospace;
|
|
160
|
+
font-size: 0.875rem;
|
|
161
|
+
word-break: break-all;
|
|
162
|
+
margin-bottom: 1rem;
|
|
163
|
+
`;
|
|
164
|
+
latexPreview.textContent = initialLatex || '(LaTeX will appear here)';
|
|
165
|
+
|
|
166
|
+
// Update preview when mathfield changes
|
|
167
|
+
mathfield.addEventListener('input', () => {
|
|
168
|
+
const latex = mathfield.value;
|
|
169
|
+
latexPreview.textContent = latex || '(LaTeX will appear here)';
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// Create button container - use DaisyUI modal-action class
|
|
173
|
+
const buttons = document.createElement('div');
|
|
174
|
+
buttons.className = 'modal-action';
|
|
175
|
+
buttons.style.cssText = 'justify-content: space-between;';
|
|
176
|
+
|
|
177
|
+
// Create left-side buttons (keyboard toggle)
|
|
178
|
+
const leftButtons = document.createElement('div');
|
|
179
|
+
leftButtons.style.cssText = 'display: flex; gap: 0.5rem;';
|
|
180
|
+
|
|
181
|
+
// Create Keyboard Toggle button
|
|
182
|
+
const keyboardBtn = document.createElement('button');
|
|
183
|
+
keyboardBtn.textContent = '⌨️ Keyboard';
|
|
184
|
+
keyboardBtn.type = 'button';
|
|
185
|
+
keyboardBtn.className = 'btn btn-sm btn-outline';
|
|
186
|
+
keyboardBtn.onclick = () => {
|
|
187
|
+
if (mathfield.executeCommand) {
|
|
188
|
+
mathfield.executeCommand('toggleVirtualKeyboard');
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
leftButtons.appendChild(keyboardBtn);
|
|
193
|
+
|
|
194
|
+
// Create right-side buttons (Cancel/Insert)
|
|
195
|
+
const rightButtons = document.createElement('div');
|
|
196
|
+
rightButtons.style.cssText = 'display: flex; gap: 0.5rem;';
|
|
197
|
+
|
|
198
|
+
// Create Cancel button - use DaisyUI classes
|
|
199
|
+
const cancelBtn = document.createElement('button');
|
|
200
|
+
cancelBtn.textContent = 'Cancel';
|
|
201
|
+
cancelBtn.type = 'button';
|
|
202
|
+
cancelBtn.className = 'btn btn-ghost';
|
|
203
|
+
|
|
204
|
+
// Create Insert button - use DaisyUI classes
|
|
205
|
+
const insertBtn = document.createElement('button');
|
|
206
|
+
insertBtn.textContent = initialLatex ? 'Update' : 'Insert';
|
|
207
|
+
insertBtn.type = 'button';
|
|
208
|
+
insertBtn.className = 'btn btn-primary';
|
|
209
|
+
|
|
210
|
+
rightButtons.appendChild(cancelBtn);
|
|
211
|
+
rightButtons.appendChild(insertBtn);
|
|
212
|
+
|
|
213
|
+
// Assemble modal
|
|
214
|
+
buttons.appendChild(leftButtons);
|
|
215
|
+
buttons.appendChild(rightButtons);
|
|
216
|
+
|
|
217
|
+
// Assemble form-control
|
|
218
|
+
formControl.appendChild(label);
|
|
219
|
+
formControl.appendChild(mathfield);
|
|
220
|
+
formControl.appendChild(previewLabel);
|
|
221
|
+
formControl.appendChild(latexPreview);
|
|
222
|
+
|
|
223
|
+
modal.appendChild(header);
|
|
224
|
+
modal.appendChild(formControl);
|
|
225
|
+
modal.appendChild(buttons);
|
|
226
|
+
|
|
227
|
+
overlay.appendChild(modal);
|
|
228
|
+
|
|
229
|
+
// Cleanup function
|
|
230
|
+
const cleanup = () => {
|
|
231
|
+
// Remove event listeners to prevent memory leaks
|
|
232
|
+
cancelBtn.removeEventListener('click', handleCancel);
|
|
233
|
+
insertBtn.removeEventListener('click', handleConfirm);
|
|
234
|
+
document.removeEventListener('keydown', handleEscape);
|
|
235
|
+
overlay.removeEventListener('click', handleOverlayClick);
|
|
236
|
+
|
|
237
|
+
// Hide virtual keyboard before cleanup
|
|
238
|
+
if (mathfield.executeCommand) {
|
|
239
|
+
mathfield.executeCommand('hideVirtualKeyboard');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Remove DOM elements
|
|
243
|
+
overlay.remove();
|
|
244
|
+
style.remove();
|
|
245
|
+
|
|
246
|
+
// Restore body scroll
|
|
247
|
+
document.body.style.overflow = '';
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
// Event handlers
|
|
251
|
+
const handleCancel = () => {
|
|
252
|
+
cleanup();
|
|
253
|
+
onCancel();
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const handleConfirm = () => {
|
|
257
|
+
const latex = mathfield.value;
|
|
258
|
+
cleanup();
|
|
259
|
+
onConfirm(latex);
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
const handleEscape = (e: KeyboardEvent) => {
|
|
263
|
+
if (e.key === 'Escape') {
|
|
264
|
+
handleCancel();
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
const handleOverlayClick = (e: MouseEvent) => {
|
|
269
|
+
// Don't close if clicking on the modal content or virtual keyboard
|
|
270
|
+
const target = e.target as HTMLElement;
|
|
271
|
+
|
|
272
|
+
// Check if click is on overlay background (not modal or keyboard)
|
|
273
|
+
if (target === overlay) {
|
|
274
|
+
// Additional check: don't close if virtual keyboard is open
|
|
275
|
+
// MathLive virtual keyboard has class 'ML__keyboard'
|
|
276
|
+
const virtualKeyboard = document.querySelector('.ML__keyboard');
|
|
277
|
+
if (!virtualKeyboard) {
|
|
278
|
+
handleCancel();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
// Attach event listeners
|
|
284
|
+
cancelBtn.addEventListener('click', handleCancel);
|
|
285
|
+
insertBtn.addEventListener('click', handleConfirm);
|
|
286
|
+
document.addEventListener('keydown', handleEscape);
|
|
287
|
+
overlay.addEventListener('click', handleOverlayClick);
|
|
288
|
+
|
|
289
|
+
// Prevent body scroll when modal is open
|
|
290
|
+
document.body.style.overflow = 'hidden';
|
|
291
|
+
|
|
292
|
+
// Add to DOM
|
|
293
|
+
document.body.appendChild(overlay);
|
|
294
|
+
|
|
295
|
+
// Focus the mathfield (but don't auto-show keyboard)
|
|
296
|
+
setTimeout(() => {
|
|
297
|
+
mathfield.focus();
|
|
298
|
+
// Try to programmatically disable the built-in toggle via options
|
|
299
|
+
try {
|
|
300
|
+
(mathfield as any).setOptions({
|
|
301
|
+
virtualKeyboardToggleGlyph: '',
|
|
302
|
+
});
|
|
303
|
+
} catch (e) {
|
|
304
|
+
// Silently ignore if this API doesn't exist
|
|
305
|
+
}
|
|
306
|
+
}, 100);
|
|
307
|
+
|
|
308
|
+
// Return cleanup function
|
|
309
|
+
return cleanup;
|
|
310
|
+
}
|