-
-
Notifications
You must be signed in to change notification settings - Fork 303
feat: implement QTI declaration serialization framework and validation utilities #5984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AlexVelezLl
merged 5 commits into
learningequality:unstable
from
Abhishek-Punhani:Issue5965
Jun 23, 2026
+2,388
−1
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
be5e5cb
feat: implement QTI declaration serialization framework and validatio…
Abhishek-Punhani f0cc8d5
feat: add QTI attribute validation to QTIDeclaration serialization us…
Abhishek-Punhani eb9314f
refactor: integrate QTIDeclaration dependency into QTI response capab…
Abhishek-Punhani 7ce1ed9
feat: add getDeclarationSchema to interaction definition mock and int…
Abhishek-Punhani 6d5b512
refactor: move value coercion logic into QTIDeclaration and improve t…
Abhishek-Punhani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...tentcuration/frontend/shared/views/QTIEditor/serialization/__tests__/assembleItem.spec.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // Disabled because jest-dom matchers (toHaveAttribute/toHaveTextContent) are designed for | ||
| // HTML and do not work reliably on strict XML elements generated by serialization. | ||
| /* eslint-disable jest-dom/prefer-to-have-attribute, jest-dom/prefer-to-have-text-content */ | ||
| import { buildXmlNode } from '../assembleItem.js'; | ||
|
|
||
| const serializer = new XMLSerializer(); | ||
|
|
||
| describe('assembleItem', () => { | ||
| describe('tag and attributes', () => { | ||
| it('creates an element with the given tag name', () => { | ||
| const node = buildXmlNode({ tag: 'qti-response-declaration' }); | ||
| expect(node.tagName).toBe('qti-response-declaration'); | ||
| }); | ||
|
|
||
| it('sets string attributes', () => { | ||
| const node = buildXmlNode({ | ||
| tag: 'qti-response-declaration', | ||
| attrs: { identifier: 'RESPONSE', cardinality: 'single', 'base-type': 'identifier' }, | ||
| }); | ||
| expect(node.getAttribute('identifier')).toBe('RESPONSE'); | ||
| expect(node.getAttribute('cardinality')).toBe('single'); | ||
| expect(node.getAttribute('base-type')).toBe('identifier'); | ||
| }); | ||
|
|
||
| it('coerces numeric attribute values to strings', () => { | ||
| const node = buildXmlNode({ tag: 'qti-map-entry', attrs: { 'mapped-value': 1.5 } }); | ||
| expect(node.getAttribute('mapped-value')).toBe('1.5'); | ||
| }); | ||
|
|
||
| it('skips null attribute values', () => { | ||
| const node = buildXmlNode({ tag: 'qti-response-declaration', attrs: { 'base-type': null } }); | ||
| expect(node.hasAttribute('base-type')).toBe(false); | ||
| }); | ||
|
|
||
| it('skips undefined attribute values', () => { | ||
| const node = buildXmlNode({ | ||
| tag: 'qti-response-declaration', | ||
| attrs: { 'base-type': undefined }, | ||
| }); | ||
| expect(node.hasAttribute('base-type')).toBe(false); | ||
| }); | ||
|
|
||
| it('produces an element with no attributes when attrs is omitted', () => { | ||
| const node = buildXmlNode({ tag: 'qti-correct-response' }); | ||
| expect(node.attributes.length).toBe(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('string children', () => { | ||
| it('appends a text node for a string child', () => { | ||
| const node = buildXmlNode({ tag: 'qti-value', children: ['ChoiceA'] }); | ||
| expect(node.textContent).toBe('ChoiceA'); | ||
| }); | ||
|
|
||
| it('handles multiple string children as separate text nodes', () => { | ||
| const node = buildXmlNode({ tag: 'qti-value', children: ['hello', ' ', 'world'] }); | ||
| expect(node.textContent).toBe('hello world'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('element children', () => { | ||
| it('appends child element nodes', () => { | ||
| const child = buildXmlNode({ tag: 'qti-value', children: ['ChoiceA'] }); | ||
| const parent = buildXmlNode({ tag: 'qti-correct-response', children: [child] }); | ||
| const childEls = parent.getElementsByTagName('qti-value'); | ||
| expect(childEls.length).toBe(1); | ||
| expect(childEls[0].textContent).toBe('ChoiceA'); | ||
| }); | ||
|
|
||
| it('appends multiple element children in order', () => { | ||
| const childA = buildXmlNode({ tag: 'qti-value', children: ['A'] }); | ||
| const childB = buildXmlNode({ tag: 'qti-value', children: ['B'] }); | ||
| const parent = buildXmlNode({ tag: 'qti-correct-response', children: [childA, childB] }); | ||
| const values = [...parent.getElementsByTagName('qti-value')].map(n => n.textContent); | ||
| expect(values).toEqual(['A', 'B']); | ||
| }); | ||
| }); | ||
|
|
||
| describe('mixed children', () => { | ||
| it('handles a mix of element and string children', () => { | ||
| const child = buildXmlNode({ tag: 'qti-value', children: ['X'] }); | ||
| const parent = buildXmlNode({ tag: 'qti-correct-response', children: ['prefix', child] }); | ||
| // First child is a text node | ||
| expect(parent.childNodes[0].nodeType).toBe(Node.TEXT_NODE); | ||
| expect(parent.childNodes[0].textContent).toBe('prefix'); | ||
| // Second child is the element | ||
| expect(parent.childNodes[1].tagName).toBe('qti-value'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('serialization', () => { | ||
| it('round-trips through XMLSerializer', () => { | ||
| const node = buildXmlNode({ | ||
| tag: 'qti-response-declaration', | ||
| attrs: { identifier: 'RESPONSE', cardinality: 'single', 'base-type': 'identifier' }, | ||
| children: [ | ||
| buildXmlNode({ | ||
| tag: 'qti-correct-response', | ||
| children: [buildXmlNode({ tag: 'qti-value', children: ['ChoiceA'] })], | ||
| }), | ||
| ], | ||
| }); | ||
|
|
||
| const xml = serializer.serializeToString(node); | ||
| expect(xml).toContain('qti-response-declaration'); | ||
| expect(xml).toContain('identifier="RESPONSE"'); | ||
| expect(xml).toContain('qti-correct-response'); | ||
| expect(xml).toContain('ChoiceA'); | ||
| }); | ||
| }); | ||
| }); |
41 changes: 41 additions & 0 deletions
41
...entcuration/contentcuration/frontend/shared/views/QTIEditor/serialization/assembleItem.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /** | ||
| * XML node builder for QTI serialization. | ||
| * | ||
| * Used by QTIDeclaration.getXML() and all declaration strategy classes | ||
| * to produce DOM nodes. A module-level XML document is created once so | ||
| * all nodes share the same owner document, avoiding adoptNode requirements | ||
| * when assembling trees. Callers serialize to a string only at the boundary | ||
| * (e.g. XMLSerializer.serializeToString). | ||
| */ | ||
|
|
||
| const xmlDoc = new DOMParser().parseFromString('<root/>', 'text/xml'); | ||
|
|
||
| /** | ||
| * Build an XML element node. | ||
| * | ||
| * @param {object} options | ||
| * @param {string} options.tag - Element tag name (e.g. 'qti-response-declaration') | ||
| * @param {Object.<string,*>} [options.attrs] - Attribute name→value pairs; | ||
| * null/undefined values are skipped | ||
| * @param {Array.<Node|string>} [options.children] - Child nodes or plain strings | ||
| * @returns {Element} | ||
| */ | ||
| export function buildXmlNode({ tag, attrs = {}, children = [] }) { | ||
| const el = xmlDoc.createElement(tag); | ||
|
|
||
| for (const [name, value] of Object.entries(attrs)) { | ||
| if (value !== null && value !== undefined) { | ||
| el.setAttribute(name, String(value)); | ||
| } | ||
| } | ||
|
|
||
| for (const child of children) { | ||
| if (typeof child === 'string') { | ||
| el.appendChild(xmlDoc.createTextNode(child)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we may need this to set the innerHTML property at some point when we need to build the prompt nodes, but we can defer this change to when we actually need it :) |
||
| } else { | ||
| el.appendChild(child); | ||
| } | ||
| } | ||
|
|
||
| return el; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.