Skip to content

Commit

Permalink
perf: Only use tXml parent when necessary (#7304)
Browse files Browse the repository at this point in the history
Issue #6239
  • Loading branch information
avelad authored and joeyparrish committed Sep 13, 2024
1 parent 729afb1 commit 81a0efa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
3 changes: 0 additions & 3 deletions lib/dash/mpd_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,6 @@ shaka.dash.MpdUtils = class {
// Move the children of the loaded xml into the current element.
while (rootElem.children.length) {
const child = rootElem.children.shift();
if (TXml.isNode(child)) {
child.parent = element;
}
element.children.push(child);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/text/ttml_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ shaka.text.TtmlTextParser = class {
return cues;
}

const tt = TXml.parseXmlString(str, 'tt');
const tt = TXml.parseXmlString(str, 'tt', /* includeParent= */ true);
if (!tt) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
Expand Down
26 changes: 17 additions & 9 deletions lib/util/tXml.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ shaka.util.TXml = class {
* Parse some data
* @param {BufferSource} data
* @param {string=} expectedRootElemName
* @param {boolean=} includeParent
* @return {shaka.extern.xml.Node | null}
*/
static parseXml(data, expectedRootElemName) {
static parseXml(data, expectedRootElemName, includeParent = false) {
const xmlString = shaka.util.StringUtils.fromBytesAutoDetect(data);
return shaka.util.TXml.parseXmlString(xmlString, expectedRootElemName);
return shaka.util.TXml.parseXmlString(
xmlString, expectedRootElemName, includeParent);
}

/**
Expand All @@ -57,8 +59,9 @@ shaka.util.TXml = class {
* @param {string=} expectedRootElemName
* @return {shaka.extern.xml.Node | null}
*/
static parseXmlString(xmlString, expectedRootElemName) {
const result = shaka.util.TXml.parse(xmlString);
static parseXmlString(xmlString, expectedRootElemName,
includeParent = false) {
const result = shaka.util.TXml.parse(xmlString, includeParent);
if (!expectedRootElemName && result.length) {
return result[0];
}
Expand Down Expand Up @@ -109,9 +112,10 @@ shaka.util.TXml = class {
* parseXML / html into a DOM Object,
* with no validation and some failure tolerance
* @param {string} S your XML to parse
* @param {boolean} includeParent
* @return {Array.<shaka.extern.xml.Node>}
*/
static parse(S) {
static parse(S, includeParent) {
let pos = 0;

const openBracket = '<';
Expand Down Expand Up @@ -286,9 +290,11 @@ shaka.util.TXml = class {
children,
parent: null,
};
for (let i = 0; i < children.length; i++) {
if (typeof children[i] !== 'string') {
children[i].parent = node;
if (includeParent) {
for (let i = 0; i < children.length; i++) {
if (typeof children[i] !== 'string') {
children[i].parent = node;
}
}
}
return node;
Expand Down Expand Up @@ -330,7 +336,9 @@ shaka.util.TXml = class {
for (let i = 0; i < childrenLength; i++) {
const childrenValue = children[i];
if (typeof childrenValue !== 'string') {
childrenValue.parent = node;
if (includeParent) {
childrenValue.parent = node;
}
} else if (i == childrenLength - 1 && childrenValue == '\n') {
children.pop();
}
Expand Down

0 comments on commit 81a0efa

Please sign in to comment.