<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
</head>
<body>
<iframe id="xmlframe" onload="frameLoaded()" style="height:0px" src="data:application/xhtml+xml,<?xml version='1.0' encoding='UTF-8'?><body/>"></iframe>
<iframe id="xmlframe2" onload="frameLoaded()" style="height:0px" src="data:application/xhtml+xml,<?xml version='1.0' encoding='UTF-8'?><body/>"></iframe>
<script>
description('Test DOMJIT document.body accessor works.');

if (window.testRunner)
    testRunner.waitUntilDone();

var target = null;
var result = null;
function runTest()
{
    var xmlDocument = document.getElementById('xmlframe').contentDocument;
    var xmlDocument2 = document.getElementById('xmlframe2').contentDocument;
    var html = document.createElement('html');
    var body = document.createElement('body');

    var htmlOnly = document.implementation.createDocument('', null, null);
    htmlOnly.appendChild(htmlOnly.createElement('html'));

    var noHTML = document.implementation.createDocument('', null, null);
    var first = noHTML.createElement('nohtml');
    first.appendChild(noHTML.createElement('body'));
    noHTML.appendChild(first);

    var framesetDoc = document.implementation.createDocument('', null, null);
    var first = document.createElement('html');
    var frameset = document.createElement('frameset');
    first.appendChild(frameset);
    framesetDoc.appendChild(first);

    var targets = [
        ['HTMLDocument', document, document.getElementsByTagName('body')[0]],
        ['XMLDocument', xmlDocument, null],
        ['XMLDocument2', xmlDocument2, body],
        ['Null', document.implementation.createDocument('', null, null), null],
        ['HTMLElement', htmlOnly, null],
        ['NoHTML', noHTML, null],
        ['Frameset', framesetDoc, frameset],
    ];

    // Put HTMLBodyElement in XMLDocument!
    html.appendChild(body);
    xmlDocument2.replaceChild(html, xmlDocument2.firstChild);

    for ([name, target, result] of targets) {
        var text = `
            function test${name}(element, result)
            {
                for (var i = 0; i < 1e4; ++i) {
                    if (element.body !== result)
                        return false;
                }
                return true;
            }
        `;
        shouldBeTrue(`(${text})(target, result)`);
    }

    if (window.testRunner)
        testRunner.notifyDone();
}
var count = 0;
function frameLoaded()
{
    if (++count === 2)
        runTest();
}
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
