<html>

<head>

<title>Test of DOM Node.normalize()</title>

<script>

function logLine(message)
{
    var console = document.getElementById("console");
    console.appendChild(document.createTextNode(message));
    console.appendChild(document.createElement('br'));
}

function log(message)
{
    var console = document.getElementById("console");
    console.appendChild(document.createTextNode(message));
}


function prepare_test1(testDiv)
{
    testDiv.firstChild.splitText(4);
    return testDiv.childNodes.length == 2;
}
function check_test1(testDiv)
{
    return testDiv.childNodes.length == 1;
}

function prepare_test2(testDiv)
{
    testDiv.firstChild.splitText(9);
    testDiv.firstChild.splitText(4);
    return testDiv.childNodes.length == 3;
}
function check_test2(testDiv)
{
    return testDiv.childNodes.length == 1;
}

function prepare_test3(testDiv)
{
    testDiv.childNodes[1].splitText(4);
    testDiv.childNodes[4].splitText(3);
    return testDiv.childNodes.length == 6;
}
function check_test3(testDiv)
{
    return testDiv.childNodes.length == 4;
}

function prepare_test4(testDiv)
{
    testDiv.childNodes[0].data = "";
    return testDiv.childNodes.length == 1;
}
function check_test4(testDiv)
{
    return testDiv.childNodes.length == 0;
}

function prepare_test5(testDiv)
{
    testDiv.childNodes[1].data = "";
    return testDiv.childNodes.length == 3;
}
function check_test5(testDiv)
{
    return testDiv.childNodes.length == 2;
}

function prepare_test6(testDiv)
{
    testDiv.childNodes[0].splitText(0);
    testDiv.childNodes[0].splitText(0);
    return testDiv.childNodes.length == 3;
}
function check_test6(testDiv)
{
    return testDiv.childNodes.length == 1;
}

function prepare_test7(testDiv)
{
    testDiv.childNodes[0].splitText(4);
    testDiv.childNodes[0].splitText(4);
    testDiv.childNodes[0].splitText(4);
    return testDiv.childNodes.length == 4;
}
function check_test7(testDiv)
{
    return testDiv.childNodes.length == 1;
}

function prepare_test8(testDiv)
{
    testDiv.childNodes[0].splitText(4);
    testDiv.childNodes[0].splitText(4);
    return testDiv.childNodes.length == 3;
}
function check_test8(testDiv)
{
    return testDiv.childNodes.length == 1;
}

function prepare_test9(testDiv)
{
    testDiv.childNodes[1].splitText(4);
    testDiv.childNodes[1].splitText(0);    // empty text node before other text nodes
    testDiv.childNodes[5].splitText(3);
    testDiv.childNodes[5].splitText(3);    // empty text node between other text nodes
    testDiv.childNodes[7].splitText(2);    // empty text node after other text nodes
    return testDiv.childNodes.length == 9;
}
function check_test9(testDiv)
{
    return testDiv.childNodes.length == 4;
}

function prepare_test10(testDiv)
{
    testDiv.childNodes[0].childNodes[0].splitText(2);
    testDiv.childNodes[1].splitText(4);
    testDiv.childNodes[3].childNodes[0].data = "";    // empty first text node of the second bold node
    testDiv.childNodes[3].childNodes[1].childNodes[0].data = "";    // empty text node of the italic node
    testDiv.childNodes[4].splitText(1);
    return testDiv.childNodes.length == 6;
}
function check_test10(testDiv)
{
    return testDiv.childNodes.length == 4
        && testDiv.childNodes[0].childNodes.length == 1        // first bold node must have single text node child
        && testDiv.childNodes[2].childNodes.length == 1        // first bold node must have single italic node child
        && testDiv.childNodes[2].childNodes[0].childNodes.length == 0;    // italic node must be empty
}

function runTest(testDiv, testName)
{
    if (self["prepare_"+testName](testDiv)) {
        var oldHTML = testDiv.innerHTML;
        testDiv.normalize();
        if (testDiv.innerHTML != oldHTML) {
            log("FAILED: innerHTML changed from \"" + oldHTML + "\" to \"" + testDiv.innerHTML + "\"");
        } else {
            if (self["check_"+testName](testDiv))
                log("PASSED");
            else
                log("FAILED");
        }        
    } else {
        log("FAILED in test preparation");
    }
}

function runTests()
{
    if (window.testRunner)
        testRunner.dumpAsText();

    try {
        var tests = document.getElementById("tests").childNodes;
        for (i = 0; i < tests.length; i++) {
            var testDiv = tests[i];
            // Skip formatting text nodes
            if (testDiv.nodeType == Node.ELEMENT_NODE) {
                var testName = testDiv.getAttribute("name");
                
                log(testName + " (" + testDiv.getAttribute("description") + "): ");
                
                try {
                    runTest(testDiv, testName);
                } catch(e) {
                    log("FAILED with exception: " + e);
                }
                
                logLine("")
            }
        }

    } catch(e) {
        logLine("FAILED, exception thrown during tests: " + e);
    }
}

</script>

</head>

<body onload="runTests()">

<div id="description">Several tests of the DOM normalize() function.</div>

<div id="tests" style="display:none">
    <div name="test1" description="two non-empty text nodes">some text</div>
    <div name="test2" description="three non-empty text nodes">some more text</div>
    <div name="test3" description="non-empty text nodes mixed with elements"><b></b>some more<b></b> text</div>
    <div name="test4" description="single empty text node">text</div>
    <div name="test5" description="empty text node between elements"><b></b>text<i></i></div>
    <div name="test6" description="empty text nodes before non-empty node">text</div>
    <div name="test7" description="empty text nodes after non-empty node">text</div>
    <div name="test8" description="empty text nodes between non-empty nodes">some text</div>
    <div name="test9" description="empty and non-empty text nodes mixed with elements"><b></b>some more<b></b> text</div>
    <div name="test10" description="mixed cases including deeper nested text nodes"><b>text</b>text <b>text<i>text</i></b> text</div>
</div>

<div id="console"></div>

</body>

</html>
