<!DOCTYPE html>
<script src="../../../resources/js-test-pre.js"></script>
<script>
    var parser = new DOMParser();
    var serializer = new XMLSerializer();
    var doc, newChild, fragment;

    function dumpDocument() {
        debug(escapeHTML(serializer.serializeToString(doc)) + '<br>');
    }

    function test(name, fn) {
        debug(name);
        fn();
        dumpDocument();
    }

    description('This tests that various combinations of replaceChild on the document works as specified.');

    test('replacing element with element', function() {
        doc = parser.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
        newChild = doc.createElement('div');
        
        shouldNotThrow('doc.replaceChild(newChild, doc.documentElement)');
    });

    test('replacing element with element in fragment', function() {        
        doc = parser.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
        fragment = doc.createDocumentFragment();
        fragment.appendChild(doc.createElement('div'));

        shouldNotThrow('doc.replaceChild(fragment, doc.documentElement);');
    });

    test('replacing element with multiple elements in fragment', function() {
        doc = parser.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
        fragment = doc.createDocumentFragment();
        fragment.appendChild(doc.createElement('div'));
        fragment.appendChild(doc.createElement('span'));
        
        shouldThrow('doc.replaceChild(fragment, doc.documentElement);');
    });

    test('replacing element with doctype', function() {
        doc = parser.parseFromString('<body/>', 'text/xml');
        newChild = doc.implementation.createDocumentType('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd');
        
        shouldNotThrow('doc.replaceChild(newChild, doc.documentElement)');
    });

    test('replacing element with doctype when a doctype already exists', function() {
        doc = parser.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
        newChild = doc.implementation.createDocumentType('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd');
        
        shouldThrow('doc.replaceChild(newChild, doc.documentElement)');
    });

    test('replacing doctype with doctype', function() {
        doc = parser.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
        newChild = doc.implementation.createDocumentType('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd');
        
        shouldNotThrow('doc.replaceChild(newChild, doc.doctype)');
    });
    
    test('replacing doctype with element', function() {
        doc = parser.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
        doc.removeChild(doc.documentElement);             
        newChild = doc.createElement('bar');

        shouldNotThrow('doc.replaceChild(newChild, doc.doctype)'); 
    });
    
    test('replacing element with doctype when an element already exists', function() {
        doc = parser.parseFromString('<!DOCTYPE html><body/>', 'text/xml');
        newChild = doc.implementation.createDocumentType('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd');

        shouldThrow('doc.replaceChild(newChild, doc.documentElement)');
    });
</script>
<script src="../../../resources/js-test-post.js"></script>
