summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/forms/selection-functions.html
blob: 71faa29d66ebbf25ffdf04f8d9570a6ee527ec42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<html>
    <head>
        <script type="text/javascript">
            function print(message)
            {
                var paragraph = document.createElement("div");
                if (message == "") {
                    paragraph.appendChild(document.createElement("br"));
                } else {
                    paragraph.appendChild(document.createTextNode(message));
                }
                document.getElementById("console").appendChild(paragraph);
            }
            function test()
            {
                if (window.testRunner) {
                    testRunner.dumpAsText();
                }
                var elt = document.getElementById("text");
                print("===textarea===");
                testElt(elt);
                
                elt = document.getElementById("input");
                print("");
                print("===input===");
                testElt(elt);

                print("");
                print("===button===");

                // Make sure that accessing selectionStart and selectionEnd on
                // the button throws exceptions.
                elt = document.getElementById("button");
                testButtonSelectionAccess(elt, ".selectionStart");
                testButtonSelectionAccess(elt, ".selectionStart = 0");
                testButtonSelectionAccess(elt, ".selectionEnd");
                testButtonSelectionAccess(elt, ".selectionEnd = 0");
                // Make sure that setSelectionRange is defined on the button element,
                // but throws an exception if called.
                if (elt.setSelectionRange != undefined) {
                    print("button.setSelectionRange defined");
                    testButtonSelectionAccess(elt, ".setSelectionRange(0,0)");
                }
            }
            function testElt(elt)
            {
                // make sure that setSelectionRange is defined
                if (elt.setSelectionRange == undefined) {
                    print("Failed: no setSelectionRange");
                    return;
                }
                elt.value = "This is a test value. Just filling in some text.";
                // the value is 48 characters long
                print("setSelectionRange():");
                elt.setSelectionRange(3,7);
                display(elt);
                elt.setSelectionRange(-2,5);
                display(elt);
                elt.setSelectionRange(42,54);
                display(elt);
                elt.setSelectionRange(5,2);
                display(elt);
                
                print("");
                print("selectionStart:");
                elt.selectionStart = 3;
                display(elt);
                elt.selectionStart = 7;
                display(elt);
                elt.selectionStart = -1;
                display(elt);
                elt.selectionStart = 54;
                display(elt);
                elt.selectionStart = 3;
                display(elt);
                
                print("");
                print("selectionEnd:");
                elt.selectionEnd = 5;
                display(elt);
                elt.selectionEnd = 2;
                display(elt);
                elt.selectionEnd = -1;
                display(elt);
                elt.selectionEnd = 54;
                display(elt);
                elt.selectionStart = 7;
                elt.selectionEnd = 7;
                display(elt);
                
                print("");
                print("selectionStart and selectionEnd in focus handler don't return wrong values 0,0:");
                elt.selectionStart = elt.selectionEnd = 7;
                // Need to clear selection. Selection API calls above modified
                // both of real selection and |elt|'s cached selection even
                // though |elt| has no focus.
                // We'd like to check the behavior in case that real selection
                // and the cached selection are mismatched.
                getSelection().removeAllRanges();
                // selectionStart and selectionEnd are still 7.
                elt.onfocus = function() { display(elt); };
                elt.focus();

                elt.value = "";
            }
            function testButtonSelectionAccess(button, access)
            {
                var source = "button" + access;
                try {
                    eval(source);
                    print(source + " did not throw exception");
                } catch(e) {
                    print(source + " threw exception");
                }
            }
            function display(elt)
            {
                var actStart = elt.selectionStart;
                var actEnd = elt.selectionEnd;
                var txt = actStart.toString() + ", " + actEnd.toString();
                print(txt);
            }
        </script>
    </head>
    <body onload="test();">
        <p>This test checks if setSelectionRange(), selectionStart, and selectionEnd on a textarea and input work as expected. This includes checking edge cases such as out-of-bound values.</p>
        <p>If this test passed you'll see a bunch of correct selection ranges below. Check the expected file for the correct ranges.</p>
        <hr />
        <form>
            <textarea id="text"></textarea>
            <input type="text" id="input" />
            <input type="button" id="button" />
        </form>
        <hr />
        <p id="console"></p>
    </body>
</html>