Fix inconsistent text selection behavior with option-shift-left/right/up/down.
[WebKit-https.git] / LayoutTests / editing / selection / selection-extend-should-not-move-across-caret-on-mac.html
1 <!DOCTYPE html>
2
3 <p>
4 On Mac when word-selecting backwards starting with the
5 caret on the middle of a word and then word-selecting forward, the
6 caret is left in the same place where it was, instead of directly selecting to the end
7 of the word (which is windows/unix behavior).
8 </p>
9
10 <div id="test-div" contenteditable=true>
11     line 1<br>
12     line 2<br>
13     line 3
14 </div>
15
16 <script>
17 function editingTest(behavior) {
18     if (window.layoutTestController) {
19         layoutTestController.dumpAsText();
20         layoutTestController.setEditingBehavior(behavior);
21     }
22
23     function getSetCaretFunction(node, container, offset) {
24         return function () {
25             var selection = window.getSelection();
26             selection.empty();
27
28             var range = document.createRange();
29             range.setStart(container, offset);
30             selection.addRange(range);
31         }
32     }
33
34     function runTest(firstDirection, secondDirection, granularity, expectedText, setCaret) {
35         var selection = window.getSelection();
36         setCaret();
37         selection.modify("extend", firstDirection, granularity);
38         selection.modify("extend", secondDirection, granularity);
39         var s = selection.toString();
40         document.write("Extend " + firstDirection + " and then " + secondDirection + " by " + granularity + ": ");
41         document.write(s === expectedText ? "PASS" : 'FAIL: expected "' + escape(expectedText) + '", got "' + escape(s) + '"');
42         document.write("<br>");
43     }
44
45     var node = document.getElementById("test-div");
46     var children = node.childNodes;
47
48     var wordCaretFunction = getSetCaretFunction(node, children[2], children[2].data.search("ne 2"));
49
50     document.write(behavior + ":<br>");
51     runTest("backward", "forward", "word", behavior == "mac" ? "" : "ne", getSetCaretFunction(node, children[2], children[2].data.search("ne 2")));
52     runTest("forward", "backward", "word", behavior == "mac" ? "" : "li", getSetCaretFunction(node, children[2], children[2].data.search("ne 2")));
53     runTest("backward", "forward", "line", behavior == "mac" ? "" : "1\nline ", getSetCaretFunction(node, children[0], children[0].data.search("1")));
54     runTest("forward", "backward", "line", behavior == "mac" ? "" : "2\nline ", getSetCaretFunction(node, children[4], children[4].data.search("3")));
55     runTest("backward", "forward", "paragraph", behavior == "mac" ? "" : "1\nline ", getSetCaretFunction(node, children[0], children[0].data.search("1")));
56     runTest("forward", "backward", "paragraph", behavior == "mac" ? "" : "2\nline ", getSetCaretFunction(node, children[4], children[4].data.search("3")));
57 }
58
59 editingTest("mac");
60 editingTest("win");
61
62 var node = document.getElementById("test-div");
63 node.parentNode.removeChild(node);
64 </script>