From 0f1b03df354f8f726e35e2eb482536c9c2af9aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Velad=20Galv=C3=A1n?= Date: Fri, 13 Sep 2024 13:49:39 +0200 Subject: [PATCH] perf: Only use tXml parent when necessary (#7304) Issue https://github.com/shaka-project/shaka-player/issues/6239 --- lib/dash/mpd_utils.js | 3 --- lib/text/ttml_text_parser.js | 2 +- lib/util/tXml.js | 26 +++++++++++++++++--------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/dash/mpd_utils.js b/lib/dash/mpd_utils.js index a0755aa353..360d11178d 100644 --- a/lib/dash/mpd_utils.js +++ b/lib/dash/mpd_utils.js @@ -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); } diff --git a/lib/text/ttml_text_parser.js b/lib/text/ttml_text_parser.js index ca1c5544b4..8682c6ee19 100644 --- a/lib/text/ttml_text_parser.js +++ b/lib/text/ttml_text_parser.js @@ -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, diff --git a/lib/util/tXml.js b/lib/util/tXml.js index bb60f395a5..395517ebb8 100644 --- a/lib/util/tXml.js +++ b/lib/util/tXml.js @@ -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); } /** @@ -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]; } @@ -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.} */ - static parse(S) { + static parse(S, includeParent) { let pos = 0; const openBracket = '<'; @@ -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; @@ -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(); }