Skip to content

Commit 7cb02a7

Browse files
TechQuerydomenic
authored andcommitted
Add serialization of CDATASection nodes
1 parent f00a351 commit 7cb02a7

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

lib/constants.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports.NODE_TYPES = {
88
ELEMENT_NODE: 1,
99
ATTRIBUTE_NODE: 2, // historical
1010
TEXT_NODE: 3,
11-
CDATA_SECTION_NODE: 4, // historical
11+
CDATA_SECTION_NODE: 4,
1212
ENTITY_REFERENCE_NODE: 5, // historical
1313
ENTITY_NODE: 6, // historical
1414
PROCESSING_INSTRUCTION_NODE: 7,

lib/serialization.js

+6
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ function serializeElement(node, namespace, prefixMap, requireWellFormed, refs) {
303303
return markup;
304304
}
305305

306+
function serializeCDATASection(node) {
307+
return "<![CDATA[" + node.data + "]]>";
308+
}
309+
306310
/**
307311
* @param {{prefixIndex: number}} refs
308312
*/
@@ -352,6 +356,8 @@ function xmlSerialization(node, namespace, prefixMap, requireWellFormed, refs) {
352356
);
353357
case NODE_TYPES.ATTRIBUTE_NODE:
354358
return "";
359+
case NODE_TYPES.CDATA_SECTION_NODE:
360+
return serializeCDATASection(node);
355361
default:
356362
throw new TypeError("Only Nodes and Attr objects can be serialized");
357363
}

test/wpt/XMLSerializer-serializeToString.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,13 @@ describe("WPT", () => {
7777
'<root><child1>value1</child1><html:br xmlns:html="http://www.w3.org/1999/xhtml" /></root>'
7878
);
7979
});
80+
81+
test("check CDATASection nodes are serialized correctly", () => {
82+
const markup =
83+
"<xhtml><style><![CDATA[ a > b { color: red; } ]]></style></xhtml>";
84+
85+
const document = new DOMParser().parseFromString(markup, "application/xml");
86+
87+
expect(new XMLSerializer().serializeToString(document)).toEqual(markup);
88+
});
8089
});

0 commit comments

Comments
 (0)