<!DOCTYPE html>
<html>
<body>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
function parseDocument() {
    str = '<test><child1>First child</child1>';
    str += '<child2 attr="an attribute">Second child</child2>';
    str += '<!-- A comment --></test>';
	
    parser = new DOMParser();
    return parser.parseFromString(str, 'text/xml');
}

var doc = parseDocument();
var child1 = doc.documentElement.firstChild;
var child2 = child1.nextSibling
var serializer = new XMLSerializer();
var comment = child2.nextSibling;

test(function() {
    assert_equals(serializer.serializeToString(child1), '<child1>First child</child1>');
    assert_equals(serializer.serializeToString(child2), '<child2 attr="an attribute">Second child</child2>');
}, 'Check Element serialization.');

test(function() {
    assert_equals(serializer.serializeToString(comment), '<!-- A comment -->');
}, 'Check Comment serialization.');

test(function() {
    assert_equals(serializer.serializeToString(doc), '<test><child1>First child</child1><child2 attr="an attribute">Second child</child2><!-- A comment --></test>');
}, 'Check Document serialization.');

test(function() {
    var fragment = doc.createDocumentFragment();
    fragment.appendChild(doc.documentElement.cloneNode(true));
    assert_equals(serializer.serializeToString(fragment), '<test><child1>First child</child1><child2 attr="an attribute">Second child</child2><!-- A comment --></test>');
}, 'Check DocumentFragment serialization.');

test(function() {
    var attr = child2.getAttributeNode('attr');
    assert_equals(serializer.serializeToString(attr), 'an attribute');

    var element = doc.createElement('foo');
    element.setAttribute('attr1', ' abc\ndef\tghi\r');
    assert_equals(serializer.serializeToString(element), '<foo attr1=" abc&#10;def&#9;ghi&#13;"/>');
}, 'Check Attr serializiation.');
</script>
</body>
</html>
