<script src="../../resources/dump-as-markup.js"></script>
<body>
<div id="test"></div>
<script>

function appendNewSpan(title)
{
    var div = document.getElementById('test');
    if (div.innerHTML.length)
        div.innerHTML += '<br>';
    div.innerHTML += title + ' : <span contenteditable>hello, world WebKit</span>';
    return div.lastChild;
}

function setSelection(text, start, end)
{
    var range = document.createRange();
    var selection = window.getSelection();

    range.setStart(text, start);
    range.setEnd(text, end);
    selection.removeAllRanges();
    selection.addRange(range);
}

function dumpLastSpan(description) {
    Markup.dump(document.getElementById('test').lastChild, description)
}

function applyInlineStyleToTextNode(command, value)
{
    setSelection(appendNewSpan(command).firstChild, 0, 7);
    document.execCommand(command, false, value);
    dumpLastSpan(command + ' first');

    setSelection(appendNewSpan(command).firstChild, 7, 12);
    document.execCommand(command, false, value);
    dumpLastSpan(command + ' middle');

    setSelection(appendNewSpan(command).firstChild, 12, 19);
    document.execCommand(command, false, value);
    dumpLastSpan(command + ' last');

    setSelection(appendNewSpan(command).firstChild, 0, 19);
    document.execCommand(command, false, value);
    dumpLastSpan(command + ' all');
}

Markup.description("This tests applying inline style to a text node, which is a child of the editable root. Style should be applied properly and the test should not crash. (See the bug 39989).")
applyInlineStyleToTextNode('bold');
applyInlineStyleToTextNode('italic');
applyInlineStyleToTextNode('underline');
applyInlineStyleToTextNode('strikeThrough');
applyInlineStyleToTextNode('foreColor', 'blue');
applyInlineStyleToTextNode('hiliteColor', 'blue');
applyInlineStyleToTextNode('subscript');
applyInlineStyleToTextNode('superscript');
applyInlineStyleToTextNode('createLink', 'http://webkit.org/');

</script>
</body>
