<p>
    This is the paragraph before.
</p>
<p id="paragraph">
    This paragraph contains an <ruby id="ruby">annotated <rt id="rt">annotation</rt></ruby> word.
</p>
<p>
    This is the paragraph after.
</p>
<pre id="console">
</pre>
<script>
    if (window.testRunner)
        testRunner.dumpAsText();

    function log(message)
    {
        document.getElementById("console").appendChild(document.createTextNode(message + "\n"));
    }

    function positionAsString(node, offset)
    {
        if (node.nodeType === Node.TEXT_NODE)
            return "\"" + node.data.trim() + "\"[" + offset + "]";
        return node.tagName + "[" + offset + "]";
    }

    function testMovementToEndOfParagraph(node, offset)
    {
        var selection = getSelection();

        selection.setPosition(node, offset);
        selection.modify("move", "forward", "paragraphBoundary");
        if (selection.baseNode === paragraph.lastChild && selection.baseOffset === 6)
            log("PASS: Reached the end of the paragraph starting from " + positionAsString(node, offset) + ".");
        else
            log("FAIL: Reached " + positionAsString(selection.baseNode, selection.baseOffset) + " instead "
                + "of the end of the paragraph starting from " + positionAsString(node, offset) + ".");
    }

    function testMovementToStartOfParagraph(node, offset)
    {
        var selection = getSelection();

        selection.setPosition(node, offset);
        selection.modify("move", "backward", "paragraphBoundary");
        if (selection.baseNode === paragraph.firstChild && selection.baseOffset === 5)
            log("PASS: Reached the start of the paragraph starting from " + positionAsString(node, offset) + ".");
        else
            log("FAIL: Reached " + positionAsString(selection.baseNode, selection.baseOffset) + " instead "
                + "of the start of the paragraph starting from " + positionAsString(node, offset) + ".");
    }

    var paragraph = document.getElementById("paragraph");
    var ruby = document.getElementById("ruby");
    var rt = document.getElementById("rt");

    testMovementToEndOfParagraph(paragraph, 0);
    testMovementToEndOfParagraph(ruby, 0);
    testMovementToEndOfParagraph(rt, 0);

    testMovementToStartOfParagraph(paragraph, 2);
    testMovementToStartOfParagraph(ruby, 2);
    testMovementToStartOfParagraph(rt, 1);

</script>
