summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/forms/textfieldselection/selection-start-end.html
blob: 768bce41298e6400371819f25cefbb42d62281af (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!doctype html>
<meta charset=utf-8>
<title></title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div id=log></div>
<script>
  function createInputElement(value, append, suffix) {
    var el = document.createElement("input");
    el.type = "text";
    el.value = value;
    el.id = "input" + (append ? "-appended" : "-not-appended") + (suffix ? suffix : "");
    if (append) {
      document.body.appendChild(el);
    }
    return el;
  };

  function createTextareaElement(value, append, suffix) {
    var el = document.createElement("textarea");
    el.value = value;

    el.id = "textarea" + (append ? "-appended" : "-not-appended") + (suffix ? suffix : "");
    if (append) {
      document.body.appendChild(el);
    }
    return el;
  };

  function createPrefocusedInputElement(value, append) {
    var el = createInputElement(value, append, "-prefocused");
    el.focus();
    el.blur();
    return el;
  }

  function createPrefocusedTextareaElement(value, append) {
    var el = createTextareaElement(value, append, "-prefocused");
    el.focus();
    el.blur();
    return el;
  }

  function createClonedTextareaWithNoDirtyValueFlag(text) {
    const textarea = document.createElement("textarea");
    textarea.textContent = text;
    return textarea.cloneNode(true);
  }

  function createTestElements(value) {
    return [ createInputElement(value, true),
             createInputElement(value, false),
             createPrefocusedInputElement(value, true),
             createPrefocusedInputElement(value, false),
             createTextareaElement(value, true),
             createTextareaElement(value, false),
             createPrefocusedTextareaElement(value, true),
             createPrefocusedTextareaElement(value, false),
             createClonedTextareaWithNoDirtyValueFlag(value)
           ];
  }

  var testValue = "abcdefghij";

  test(function() {
    assert_equals(testValue.length, 10);
  }, "Sanity check for testValue length; if this fails, variou absolute offsets in the test below need to be adjusted to be less than testValue.length");

  for (let prop of ["selectionStart", "selectionEnd"]) {
    for (let el of createTestElements(testValue)) {
      if (el.defaultValue !== el.value) {
        test(function() {
          assert_equals(el[prop], testValue.length);
        }, `Initial .value set on ${el.id} should set ${prop} to end of value`);
      }
    }
  }

  test(function() {
    for (let el of createTestElements(testValue)) {
      var t = async_test(`onselect should fire when selectionStart is changed on ${el.id}`);
      el.onselect = t.step_func_done(function(e) {
        assert_equals(e.type, "select");
        el.remove();
      });
      el.selectionStart = 2;
    }
  }, "onselect should fire when selectionStart is changed");

  test(function() {
    for (let el of createTestElements(testValue)) {
      var t = async_test(`onselect should fire when selectionEnd is changed on ${el.id}`);
      el.onselect = t.step_func_done(function(e) {
        assert_equals(e.type, "select");
        el.remove();
      });
      el.selectionEnd = 2;
    }
  }, "onselect should fire when selectionEnd is changed");

  test(function() {
    for (let el of createTestElements(testValue)) {
      el.selectionStart = 0;
      el.selectionEnd = 5;
      el.selectionStart = 8;
      assert_equals(el.selectionStart, 8, `selectionStart on ${el.id}`);
      assert_equals(el.selectionEnd, 8, `selectionEnd on ${el.id}`);
      el.remove();
    }
  }, "Setting selectionStart to a value larger than selectionEnd should increase selectionEnd");

  test(function() {
    for (let el of createTestElements(testValue)) {
      el.selectionStart = 8;
      el.selectionEnd = 5;
      assert_equals(el.selectionStart, 5, `selectionStart on ${el.id}`);
      assert_equals(el.selectionEnd, 5, `selectionEnd on ${el.id}`);
      el.remove();
    }
  }, "Setting selectionEnd to a value smaller than selectionStart should decrease selectionStart");

  test(function() {
    for (let el of createTestElements(testValue)) {
      el.selectionStart = 0;
      assert_equals(el.selectionStart, 0, `We just set it on ${el.id}`);
      el.selectionStart = -1;
      assert_equals(el.selectionStart, testValue.length,
                    `selectionStart setter on ${el.id} should convert -1 to 2^32-1`);
      el.selectionStart = Math.pow(2, 32);
      assert_equals(el.selectionStart, 0,
                    `selectionStart setter on ${el.id} should convert 2^32 to 0`);
      el.selectionStart = Math.pow(2, 32) - 1;
      assert_equals(el.selectionStart, testValue.length,
                    `selectionStart setter on ${el.id} should leave 2^32-1 as-is`);
      el.remove();
    }
  }, "selectionStart edge-case values");

  test(function() {
    for (let el of createTestElements(testValue)) {
      el.selectionEnd = 0;
      assert_equals(el.selectionEnd, 0, `We just set it on ${el.id}`);
      el.selectionEnd = -1;
      assert_equals(el.selectionEnd, testValue.length,
                    `selectionEnd setter on ${el.id} should convert -1 to 2^32-1`);
      el.selectionEnd = Math.pow(2, 32);
      assert_equals(el.selectionEnd, 0,
                    `selectionEnd setter on ${el.id} should convert 2^32 to 0`);
      el.selectionEnd = Math.pow(2, 32) - 1;
      assert_equals(el.selectionEnd, testValue.length,
                    `selectionEnd setter on ${el.id} should leave 2^32-1 as-is`);
      el.remove();
    }
  }, "selectionEnd edge-case values");

  test(() => {
    for (const el of createTestElements(testValue)) {
      el.selectionStart = 200;
      assert_equals(el.selectionStart, testValue.length);
      el.remove();
    }
  }, "selectionStart should be clamped by the current value length");

  test(() => {
    for (const el of createTestElements(testValue)) {
      el.selectionStart = 300;
      assert_equals(el.selectionEnd, testValue.length);
      el.remove();
    }
  }, "selectionEnd should be clamped by the current value length");

  test(() => {
    for (const el of createTestElements(testValue)) {
      el.setSelectionRange(200, 300);
      assert_equals(el.selectionStart, testValue.length);
      assert_equals(el.selectionEnd, testValue.length);
      el.remove();
    }
  }, "setSelectionRange should be clamped by the current value length");

  test(() => {
    for (let el of createTestElements(testValue)) {
      const start = 1;
      const end = testValue.length - 1;

      el.setSelectionRange(start, end);

      assert_equals(el.selectionStart, start, `selectionStart on ${el.id}`);
      assert_equals(el.selectionEnd, end, `selectionEnd on ${el.id}`);

      el.selectionDirection = "backward";

      assert_equals(el.selectionStart, start,
                    `selectionStart on ${el.id} after setting selectionDirection to "backward"`);
      assert_equals(el.selectionEnd, end,
                    `selectionEnd on ${el.id} after setting selectionDirection to "backward"`);

      el.selectionDirection = "forward";

      assert_equals(el.selectionStart, start,
                    `selectionStart on ${el.id} after setting selectionDirection to "forward"`);
      assert_equals(el.selectionEnd, end,
                    `selectionEnd on ${el.id} after setting selectionDirection to "forward"`);
    }
  }, "selectionStart and selectionEnd should remain the same when selectionDirection is changed");
</script>