diff --git a/src/pugixml.cpp b/src/pugixml.cpp
index 241eaed..e24ba3c 100644
--- a/src/pugixml.cpp
+++ b/src/pugixml.cpp
@@ -2920,11 +2920,14 @@ namespace
}
else if (!node.first_child())
writer.write(' ', '/', '>', '\n');
- else if (node.first_child() == node.last_child() && node.first_child().type() == node_pcdata)
+ else if (node.first_child() == node.last_child() && (node.first_child().type() == node_pcdata || node.first_child().type() == node_cdata))
{
writer.write('>');
- text_output_escaped(writer, node.first_child().value(), ctx_special_pcdata);
+ if (node.first_child().type() == node_pcdata)
+ text_output_escaped(writer, node.first_child().value(), ctx_special_pcdata);
+ else
+ text_output_cdata(writer, node.first_child().value());
writer.write('<', '/');
writer.write(name);
diff --git a/tests/test_write.cpp b/tests/test_write.cpp
index 0904b21..3871bf6 100644
--- a/tests/test_write.cpp
+++ b/tests/test_write.cpp
@@ -45,6 +45,13 @@ TEST_XML(write_cdata_escape, "")
CHECK_NODE(doc, STR("2]]]]>3]]>"));
}
+TEST_XML(write_cdata_inner, "")
+{
+ CHECK_NODE(doc, STR(""));
+ CHECK_NODE_EX(doc, STR("\n"), STR(""), 0);
+}
+
+
TEST_XML_FLAGS(write_comment, "", parse_default | parse_comments)
{
CHECK_NODE(doc, STR(""));