Skip to content
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

fix(DASH): Use proper namespace when mapping TXml node to Element #7240

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions lib/util/tXml.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,37 @@ shaka.util.TXml = class {
}

/**
* Parse some data
* Get namespace based on schema
* @param {string} schema
* @return {string}
*/
static getKnownNameSpace(schema) {
if (shaka.util.TXml.knownNameSpaces_.has(schema)) {
return shaka.util.TXml.knownNameSpaces_.get(schema);
if (shaka.util.TXml.uriToNameSpace_.has(schema)) {
return shaka.util.TXml.uriToNameSpace_.get(schema);
}
return '';
}

/**
* Parse some data
* Get schema based on namespace
* @param {string} NS
* @return {string}
*/
static getKnownSchema(NS) {
if (shaka.util.TXml.nameSpaceToUri_.has(NS)) {
return shaka.util.TXml.nameSpaceToUri_.get(NS);
}
return '';
}

/**
* Sets NS <-> schema bidirectional mapping
* @param {string} schema
* @param {string} NS
*/
static setKnownNameSpace(schema, NS) {
shaka.util.TXml.knownNameSpaces_.set(schema, NS);
shaka.util.TXml.uriToNameSpace_.set(schema, NS);
shaka.util.TXml.nameSpaceToUri_.set(NS, schema);
}

/**
Expand Down Expand Up @@ -875,7 +888,12 @@ shaka.util.TXml = class {
*/
static txmlNodeToDomElement(node) {
const TXml = shaka.util.TXml;
const element = document.createElementNS('', node.tagName);
let namespace = '';
const parts = node.tagName.split(':');
if (parts.length > 0) {
namespace = TXml.getKnownSchema(parts[0]);
}
const element = document.createElementNS(namespace, node.tagName);

for (const k in node.attributes) {
const v = node.attributes[k];
Expand Down Expand Up @@ -924,7 +942,11 @@ shaka.util.TXml = class {
}
};

shaka.util.TXml.knownNameSpaces_ = new Map([]);
/** @private {!Map<string, string>} */
shaka.util.TXml.uriToNameSpace_ = new Map();

/** @private {!Map<string, string>} */
shaka.util.TXml.nameSpaceToUri_ = new Map();


/**
Expand Down
29 changes: 29 additions & 0 deletions test/util/tXml_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ describe('tXml', () => {

const TXml = shaka.util.TXml;

beforeAll(() => {
TXml.setKnownNameSpace('urn:scte:scte35:2014:xml+bin', 'scte35');
});

describe('findChild', () => {
it('finds a child node', () => {
const xmlString = [
Expand Down Expand Up @@ -516,6 +520,31 @@ describe('tXml', () => {
]);
});

it('txmlNodeToDomElement', () => {
const node = {
tagName: 'Event',
parent: null,
attributes: {
'presentationTime': '0',
},
children: [
{
tagName: 'scte35:Signal',
parent: null,
attributes: {},
children: [],
},
],
};
node.children[0].parent = node;

const element = TXml.txmlNodeToDomElement(node);
expect(element.tagName).toBe('Event');
expect(element.getAttribute('presentationTime')).toBe('0');
const signal = element.firstElementChild;
expect(signal.tagName).toBe('scte35:Signal');
});

it('cloneNode', () => {
expect(TXml.cloneNode(null)).toBe(null);
const root = {
Expand Down
Loading