<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script src="resources/js-test-selection-shared.js"></script>
</head>
<body>
<p>This tests collapsing directionless selection on text in a block with the opposite text direction.
To manually test, on Mac, select text below by mouse and use arrow keys (without pressing shift) to collapse the selection.
The selection should collapse to the left if you pressed the left arrow key and to the right if you pressed the right arrow key.</p>
<div id="tests" contenteditable><span dir="ltr">&#x05d0;&#x05d1;&#x05d2;&#x05d3;&#x05d4;</span
><span dir="rtl">hello</span></div>
<pre id="console"></pre>
<script>

if (window.internals) {
    internals.settings.setEditingBehavior('mac');

    function selectTarget(target) {
        eventSender.mouseMoveTo(target.offsetLeft + target.offsetWidth * 2 / 5, target.offsetTop + 5);
        eventSender.mouseDown();
        eventSender.leapForward(200);
        eventSender.mouseMoveTo(target.offsetLeft + target.offsetWidth * 4 / 5, target.offsetTop + 5);
        eventSender.leapForward(200);
        eventSender.mouseUp();
    }

    var selection = window.getSelection();
    var tests = document.getElementById('tests').getElementsByTagName('span');
    for (var i = 0; i < tests.length; i++) {
        var isLTR = tests[i].dir == 'ltr';

        selectTarget(tests[i]);
        var expectedOffset = isLTR ? window.getSelection().getRangeAt(0).endOffset : window.getSelection().getRangeAt(0).startOffset;
        window.getSelection().modify('move', 'left', 'character');
        assertSelectionAt(tests[i].firstChild, expectedOffset);

        selectTarget(tests[i]);
        expectedOffset = isLTR ? window.getSelection().getRangeAt(0).startOffset : window.getSelection().getRangeAt(0).endOffset;
        window.getSelection().modify('move', 'right', 'character');
        assertSelectionAt(tests[i].firstChild, expectedOffset);
    }

    document.getElementById('tests').style.display = 'none';
}


</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
