@pie-qti/item-player 0.1.7 → 0.1.8
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/dist/core/sanitizer.d.ts.map +1 -1
- package/dist/core/sanitizer.js +60 -0
- package/dist/document/AssessmentItemDocument.d.ts +4 -0
- package/dist/document/AssessmentItemDocument.d.ts.map +1 -1
- package/dist/document/AssessmentItemDocument.js +53 -4
- package/dist/element.js +1447 -1316
- package/dist/element.js.map +1 -1
- package/dist/interactions/extended-text/extractor.d.ts +1 -0
- package/dist/interactions/extended-text/extractor.d.ts.map +1 -1
- package/dist/interactions/extended-text/extractor.js +2 -0
- package/dist/interactions/shared/types.d.ts +1 -0
- package/dist/interactions/shared/types.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/core/sanitizer.ts +70 -0
- package/src/document/AssessmentItemDocument.ts +76 -7
- package/src/interactions/extended-text/extractor.ts +3 -0
- package/src/interactions/shared/types.ts +1 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sanitizer.d.ts","sourceRoot":"","sources":["../../src/core/sanitizer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,oBAAoB,EAAmB,MAAM,mBAAmB,CAAC;AAI/E,MAAM,WAAW,mBAAmB;IACnC;;;OAGG;IACH,QAAQ,CAAC,EAAE,oBAAoB,CAAC;CAChC;
|
|
1
|
+
{"version":3,"file":"sanitizer.d.ts","sourceRoot":"","sources":["../../src/core/sanitizer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,oBAAoB,EAAmB,MAAM,mBAAmB,CAAC;AAI/E,MAAM,WAAW,mBAAmB;IACnC;;;OAGG;IACH,QAAQ,CAAC,EAAE,oBAAoB,CAAC;CAChC;AAiKD;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,MAAM,CA0ChF;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,MAAM,CAYvF"}
|
package/dist/core/sanitizer.js
CHANGED
|
@@ -25,6 +25,17 @@ function allowObjectEmbeds(options) {
|
|
|
25
25
|
function allowIframes(options) {
|
|
26
26
|
return options?.security?.allowIframes === true;
|
|
27
27
|
}
|
|
28
|
+
function escapeAttr(value) {
|
|
29
|
+
return value
|
|
30
|
+
.replace(/&/g, '&')
|
|
31
|
+
.replace(/"/g, '"')
|
|
32
|
+
.replace(/</g, '<')
|
|
33
|
+
.replace(/>/g, '>');
|
|
34
|
+
}
|
|
35
|
+
function getOptionalAttr(element, attrName) {
|
|
36
|
+
const value = element.getAttribute(attrName);
|
|
37
|
+
return value ? ` ${attrName}="${escapeAttr(value)}"` : '';
|
|
38
|
+
}
|
|
28
39
|
function sanitizeUrlAttr(value, policy, kind) {
|
|
29
40
|
return sanitizeResourceUrl(value, policy, kind) ?? '#';
|
|
30
41
|
}
|
|
@@ -74,6 +85,11 @@ function sanitizeElement(element, options, limitsState, depth) {
|
|
|
74
85
|
return;
|
|
75
86
|
}
|
|
76
87
|
if ((tagName === 'object' || tagName === 'embed') && !allowObjectEmbeds(options)) {
|
|
88
|
+
const replacementHtml = getSafeObjectReplacementHtml(element, policyFromOptions(options));
|
|
89
|
+
if (replacementHtml) {
|
|
90
|
+
element.replaceWith(replacementHtml);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
77
93
|
element.remove();
|
|
78
94
|
return;
|
|
79
95
|
}
|
|
@@ -196,3 +212,47 @@ export function sanitizeTextContent(text, options) {
|
|
|
196
212
|
// Plain text - return as-is
|
|
197
213
|
return text;
|
|
198
214
|
}
|
|
215
|
+
function getSafeObjectReplacementHtml(element, policy) {
|
|
216
|
+
if (element.rawTagName?.toLowerCase() !== 'object') {
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
const rawType = element.getAttribute('type')?.trim().toLowerCase() ?? '';
|
|
220
|
+
const rawData = element.getAttribute('data')?.trim() ?? '';
|
|
221
|
+
if (!rawType || !rawData) {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
const dimensions = `${getOptionalAttr(element, 'width')}${getOptionalAttr(element, 'height')}`;
|
|
225
|
+
const classes = getOptionalAttr(element, 'class');
|
|
226
|
+
const id = getOptionalAttr(element, 'id');
|
|
227
|
+
if (rawType.startsWith('image/')) {
|
|
228
|
+
const src = sanitizeResourceUrl(rawData, policy, 'img');
|
|
229
|
+
if (!src) {
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
const alt = element.getAttribute('aria-label') ??
|
|
233
|
+
element.getAttribute('title') ??
|
|
234
|
+
element.textContent?.trim() ??
|
|
235
|
+
'';
|
|
236
|
+
return `<img src="${escapeAttr(src)}" alt="${escapeAttr(alt)}"${dimensions}${classes}${id}>`;
|
|
237
|
+
}
|
|
238
|
+
if (rawType.startsWith('audio/')) {
|
|
239
|
+
const src = sanitizeResourceUrl(rawData, policy, 'media');
|
|
240
|
+
if (!src) {
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
return `<audio controls${classes}${id}><source src="${escapeAttr(src)}" type="${escapeAttr(rawType)}"></audio>`;
|
|
244
|
+
}
|
|
245
|
+
if (rawType.startsWith('video/')) {
|
|
246
|
+
const src = sanitizeResourceUrl(rawData, policy, 'media');
|
|
247
|
+
if (!src) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
const rawPoster = element.getAttribute('poster')?.trim() ?? '';
|
|
251
|
+
const poster = rawPoster
|
|
252
|
+
? sanitizeResourceUrl(rawPoster, policy, 'img')
|
|
253
|
+
: null;
|
|
254
|
+
const posterAttr = poster ? ` poster="${escapeAttr(poster)}"` : '';
|
|
255
|
+
return `<video controls${dimensions}${classes}${id}${posterAttr}><source src="${escapeAttr(src)}" type="${escapeAttr(rawType)}"></video>`;
|
|
256
|
+
}
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
@@ -17,6 +17,7 @@ export interface AssessmentItemDocumentInput {
|
|
|
17
17
|
export declare class AssessmentItemDocument {
|
|
18
18
|
private readonly itemXml;
|
|
19
19
|
private readonly security?;
|
|
20
|
+
private readonly mathMlPrefixes;
|
|
20
21
|
readonly elementNameMapper: ElementNameMapper;
|
|
21
22
|
readonly attributeNameMapper: AttributeNameMapper;
|
|
22
23
|
private readonly xmlDocument;
|
|
@@ -35,6 +36,9 @@ export declare class AssessmentItemDocument {
|
|
|
35
36
|
findExtractionElements(elementTypes: Iterable<string>): DiscoveredInteractionElement[];
|
|
36
37
|
private getExtractionRoot;
|
|
37
38
|
private getExtractionDocument;
|
|
39
|
+
private getItemBodyElement;
|
|
40
|
+
private serializeItemBodyForExtraction;
|
|
41
|
+
private serializeChildNodes;
|
|
38
42
|
private walkExtractionChildren;
|
|
39
43
|
private getExtractionResponseIdentifier;
|
|
40
44
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AssessmentItemDocument.d.ts","sourceRoot":"","sources":["../../src/document/AssessmentItemDocument.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"AssessmentItemDocument.d.ts","sourceRoot":"","sources":["../../src/document/AssessmentItemDocument.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAWlF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAE3D,MAAM,MAAM,cAAc,GAAG,UAAU,GAAG,UAAU,GAAG,SAAS,CAAC;AAEjE,MAAM,WAAW,4BAA4B;IAC5C,OAAO,EAAE,UAAU,CAAC;IACpB,WAAW,EAA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B;IASzB,OAAO,CAAC,qBAAqB;IAW7B,OAAO,CAAC,kBAAkB;IAO1B,OAAO,CAAC,8BAA8B;IAQtC,OAAO,CAAC,mBAAmB;IAe3B,OAAO,CAAC,sBAAsB;IAS9B,OAAO,CAAC,+BAA+B;CAUvC;AAED,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,2BAA2B,GAAG,sBAAsB,CAEtG"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { findAssessmentItem, findDescendants, findFirstDescendant, parseXml, serializeXml, } from '@pie-qti/qti-processing';
|
|
2
2
|
import { parse } from 'node-html-parser';
|
|
3
3
|
import { enforceItemXmlLimits } from '../core/parsingLimits.js';
|
|
4
4
|
import { normalizeInteractionTypeFromTagName } from '../interactions/modules.js';
|
|
@@ -8,6 +8,7 @@ export class AssessmentItemDocument {
|
|
|
8
8
|
this.extractionRoot = null;
|
|
9
9
|
this.itemXml = itemXml;
|
|
10
10
|
this.security = security;
|
|
11
|
+
this.mathMlPrefixes = findMathMlPrefixes(itemXml);
|
|
11
12
|
this.elementNameMapper = elementNameMapper;
|
|
12
13
|
this.attributeNameMapper = attributeNameMapper;
|
|
13
14
|
enforceItemXmlLimits(this.itemXml, this.security);
|
|
@@ -26,7 +27,7 @@ export class AssessmentItemDocument {
|
|
|
26
27
|
return findFirstDescendant(this.assessmentItem, this.elementNameMapper.toNative(tagName));
|
|
27
28
|
}
|
|
28
29
|
serializeItemBodyChildren() {
|
|
29
|
-
const itemBody =
|
|
30
|
+
const itemBody = this.getItemBodyElement();
|
|
30
31
|
if (!itemBody)
|
|
31
32
|
return '';
|
|
32
33
|
return this.serializeChildren(itemBody);
|
|
@@ -49,7 +50,7 @@ export class AssessmentItemDocument {
|
|
|
49
50
|
return this.assessmentItem.getAttribute(this.attributeNameMapper.toNative(name));
|
|
50
51
|
}
|
|
51
52
|
serializeChildren(element) {
|
|
52
|
-
return
|
|
53
|
+
return normalizeMathMlPrefixes(unwrapCdataSections(this.serializeChildNodes(element)), this.mathMlPrefixes);
|
|
53
54
|
}
|
|
54
55
|
findExtractionElements(elementTypes) {
|
|
55
56
|
const root = this.getExtractionRoot();
|
|
@@ -86,13 +87,35 @@ export class AssessmentItemDocument {
|
|
|
86
87
|
getExtractionDocument() {
|
|
87
88
|
if (!this.extractionDocument) {
|
|
88
89
|
enforceItemXmlLimits(this.itemXml, this.security);
|
|
89
|
-
this.extractionDocument = parse(this.
|
|
90
|
+
this.extractionDocument = parse(this.serializeItemBodyForExtraction(), {
|
|
90
91
|
lowerCaseTagName: false,
|
|
91
92
|
comment: false,
|
|
92
93
|
});
|
|
93
94
|
}
|
|
94
95
|
return this.extractionDocument;
|
|
95
96
|
}
|
|
97
|
+
getItemBodyElement() {
|
|
98
|
+
return findFirstDescendant(this.assessmentItem, this.elementNameMapper.toNative('itembody'));
|
|
99
|
+
}
|
|
100
|
+
serializeItemBodyForExtraction() {
|
|
101
|
+
const itemBodyTag = this.elementNameMapper.toNative('itembody');
|
|
102
|
+
const itemBody = this.getItemBodyElement();
|
|
103
|
+
const itemBodyChildren = itemBody ? this.serializeChildren(itemBody) : '';
|
|
104
|
+
return `<assessmentItem><${itemBodyTag}>${itemBodyChildren}</${itemBodyTag}></assessmentItem>`;
|
|
105
|
+
}
|
|
106
|
+
serializeChildNodes(element) {
|
|
107
|
+
const children = element.childNodes;
|
|
108
|
+
if (!children)
|
|
109
|
+
return '';
|
|
110
|
+
const serialized = [];
|
|
111
|
+
for (let index = 0; index < children.length; index++) {
|
|
112
|
+
const child = children[index];
|
|
113
|
+
if (child) {
|
|
114
|
+
serialized.push(serializeXml(child));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return serialized.join('');
|
|
118
|
+
}
|
|
96
119
|
walkExtractionChildren(root, visit) {
|
|
97
120
|
for (const child of root.childNodes ?? []) {
|
|
98
121
|
const element = child;
|
|
@@ -114,3 +137,29 @@ export class AssessmentItemDocument {
|
|
|
114
137
|
export function parseAssessmentItemDocument(input) {
|
|
115
138
|
return new AssessmentItemDocument(input);
|
|
116
139
|
}
|
|
140
|
+
function unwrapCdataSections(xml) {
|
|
141
|
+
return xml.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, (_match, content) => content);
|
|
142
|
+
}
|
|
143
|
+
function findMathMlPrefixes(xml) {
|
|
144
|
+
const prefixes = new Set();
|
|
145
|
+
for (const match of xml.matchAll(/\sxmlns:([A-Za-z_][\w.-]*)=(["'])http:\/\/www\.w3\.org\/1998\/Math\/MathML\2/g)) {
|
|
146
|
+
const prefix = match[1];
|
|
147
|
+
if (prefix) {
|
|
148
|
+
prefixes.add(prefix);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return prefixes;
|
|
152
|
+
}
|
|
153
|
+
function normalizeMathMlPrefixes(xml, prefixes) {
|
|
154
|
+
let normalized = xml;
|
|
155
|
+
for (const prefix of prefixes) {
|
|
156
|
+
const escapedPrefix = escapeRegExp(prefix);
|
|
157
|
+
normalized = normalized
|
|
158
|
+
.replace(new RegExp(`(<\\/?)${escapedPrefix}:([A-Za-z][\\w.-]*)(?=[\\s>/])`, 'g'), '$1$2')
|
|
159
|
+
.replace(new RegExp(`\\sxmlns:${escapedPrefix}=(["'])http:\\/\\/www\\.w3\\.org\\/1998\\/Math\\/MathML\\1`, 'g'), '');
|
|
160
|
+
}
|
|
161
|
+
return normalized;
|
|
162
|
+
}
|
|
163
|
+
function escapeRegExp(value) {
|
|
164
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
165
|
+
}
|