From 99c91afa325b4a95299789ea6b79cceec7e8bdf6 Mon Sep 17 00:00:00 2001 From: Jacob Trimble Date: Mon, 30 Jul 2018 10:43:11 -0700 Subject: [PATCH] Allow CDATA in text nodes. Fixes #1508 Change-Id: I04817c8077b298b56ea11cb8cb8d1a6e832ce4ee --- lib/util/xml_utils.js | 11 ++++++----- test/util/xml_utils_unit.js | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/util/xml_utils.js b/lib/util/xml_utils.js index ec862789e7..314614ddec 100644 --- a/lib/util/xml_utils.js +++ b/lib/util/xml_utils.js @@ -91,14 +91,15 @@ shaka.util.XmlUtils.getAttributeNS = function(elem, ns, name) { * @return {?string} The text contents, or null if there are none. */ shaka.util.XmlUtils.getContents = function(elem) { - let contents = elem.firstChild; - - // Check content. - if (!contents || contents.nodeType != Node.TEXT_NODE) { + let isText = (child) => { + return child.nodeType == Node.TEXT_NODE || + child.nodeType == Node.CDATA_SECTION_NODE; + }; + if (!Array.prototype.every.call(elem.childNodes, isText)) { return null; } - // Read merged text content from all text nodes (fixes MSIE 11 bug). + // Read merged text content from all text nodes. return elem.textContent.trim(); }; diff --git a/test/util/xml_utils_unit.js b/test/util/xml_utils_unit.js index 9a01ea4913..e78b07e750 100644 --- a/test/util/xml_utils_unit.js +++ b/test/util/xml_utils_unit.js @@ -120,6 +120,21 @@ describe('XmlUtils', function() { expect(XmlUtils.getContents(xml)).toBeNull(); }); + + it('handles CDATA sections', function() { + let xmlString = [ + '', + '', + ' Bar]]>', + '', + ].join('\n'); + let xml = new DOMParser().parseFromString(xmlString, 'application/xml'); + goog.asserts.assert(xml, 'parseFromString should succeed'); + + let root = XmlUtils.findChild(xml, 'Root'); + goog.asserts.assert(root, 'findChild should find element'); + expect(XmlUtils.getContents(root)).toBe(' Bar'); + }); }); describe('parseAttr', function() {