Skip to content

Commit

Permalink
Allow CDATA in text nodes.
Browse files Browse the repository at this point in the history
Fixes #1508

Change-Id: I04817c8077b298b56ea11cb8cb8d1a6e832ce4ee
  • Loading branch information
TheModMaker authored and joeyparrish committed Aug 1, 2018
1 parent df09eeb commit 99c91af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/util/xml_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

Expand Down
15 changes: 15 additions & 0 deletions test/util/xml_utils_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ describe('XmlUtils', function() {

expect(XmlUtils.getContents(xml)).toBeNull();
});

it('handles CDATA sections', function() {
let xmlString = [
'<?xml version="1.0"?>',
'<Root>',
'<![CDATA[<Foo> Bar]]>',
'</Root>',
].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('<Foo> Bar');
});
});

describe('parseAttr', function() {
Expand Down

0 comments on commit 99c91af

Please sign in to comment.