<!DOCTYPE HTML>
<meta charset=utf-8>
<html>
<head>
<title>domparsing Test: XMLSerializer.serializeToString</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<h1>domparsing_XMLSerializer_serializeToString</h1>
<script>
function createXmlDoc(){
var input = '<?xml version="1.0" encoding="UTF-8"?><root><child1>value1</child1></root>';
var parser = new DOMParser();
return parser.parseFromString(input, 'text/xml');
}
test(function() {
var serializer = new XMLSerializer();
var root = createXmlDoc().documentElement;
var xmlString = serializer.serializeToString(root);
assert_equals(xmlString, '<root><child1>value1</child1></root>');
}, 'check XMLSerializer.serializeToString method could parsing xmldoc to string');
test(function() {
var serializer = new XMLSerializer();
var root = createXmlDoc().documentElement;
var element = root.ownerDocument.createElementNS('urn:foo', 'another');
var child1 = root.firstChild;
root.replaceChild(element, child1);
element.appendChild(child1);
var xmlString = serializer.serializeToString(root);
assert_equals(xmlString, '<root><another xmlns="urn:foo"><child1 xmlns="">value1</child1></another></root>');
}, 'Check if the default namespace is correctly reset.');
test(function() {
var input = '<root xmlns="urn:bar"><outer xmlns=""><inner>value1</inner></outer></root>';
var root = (new DOMParser()).parseFromString(input, 'text/xml').documentElement;
var xmlString = (new XMLSerializer()).serializeToString(root);
assert_equals(xmlString, '<root xmlns="urn:bar"><outer xmlns=""><inner>value1</inner></outer></root>');
}, 'Check if there is no redundant empty namespace declaration.');
test(function() {
var serializer = new XMLSerializer();
var parser = new DOMParser();
var root = parser.parseFromString('<root />', 'text/xml').documentElement;
root.setAttribute('attr', '\t');
assert_in_array(serializer.serializeToString(root), [
'<root attr="	"/>', '<root attr="	"/>']);
root.setAttribute('attr', '\n');
assert_in_array(serializer.serializeToString(root), [
'<root attr="
"/>', '<root attr=" "/>']);
root.setAttribute('attr', '\r');
assert_in_array(serializer.serializeToString(root), [
'<root attr="
"/>', '<root attr=" "/>']);
}, 'check XMLSerializer.serializeToString escapes attribute values for roundtripping');
</script>
</body>
</html>