summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/forms/textfieldselection/selection.html
blob: 04ab7298e35ae5191a7f9d9df2fdb46faf477889 (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>
<title>test if select() API returns correct attributes</title>
<meta charset="UTF-8">
<meta name="timeout" content="long">
<link rel="author" title="Koji Tashiro" href="mailto:koji.tashiro@gmail.com">
<link rel="help" href="https://html.spec.whatwg.org/multipage/multipage/association-of-controls-and-forms.html#textFieldSelection">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<div id="log"></div>

<script>
  var body = document.getElementsByTagName("body").item(0);
  var dirs = ['forward', 'backward', 'none'];
  var sampleText = "0123456789";

  var createInputElement = function(value, append = true) {
    var el = document.createElement("input");
    el.type = "text";
    el.value = value;
    el.id = "input" + (append ? "-appended" : "-not-appended");
    if (append) {
      body.appendChild(el);
    }
    return el;
  };

  var createTextareaElement = function(value, append = true) {
    var el = document.createElement("textarea");
    el.value = value;
    el.id = "textarea" + (append ? "-appended" : "-not-appended");
    if (append) {
      body.appendChild(el);
    }
    return el;
  };


  test(function() {
    var text = 'a';
    for (var i=0; i<255; i++) {
      var el = createInputElement(text);
      el.select();
      var selectionText = el.value.substring(el.selectionStart, el.selectionEnd);
      assert_equals(selectionText, text, "Selection text mismatched");
      el.parentNode.removeChild(el);
      text += 'a';
    }
  }, "test if selection text is correct for input");


  test(function() {
    var text = 'a';
    for (var i=0; i<255; i++) {
      var el = createTextareaElement(text);
      el.select();
      var selectionText = el.value.substring(el.selectionStart, el.selectionEnd);
      assert_equals(selectionText, text, "Selection text mismatched");
      el.parentNode.removeChild(el);
      text += 'a';
    }
  }, "test if selection text is correct for textarea");


  test(function() {
    var text = 'あ';
    for (var i=0; i<255; i++) {
      var el = createInputElement(text);
      el.select();
      var selectionText = el.value.substring(el.selectionStart, el.selectionEnd);
      assert_equals(selectionText, text, "Selection text mismatched");
      el.parentNode.removeChild(el);
      text += 'あ';
    }
  }, "test if non-ascii selection text is correct for input");


  test(function() {
    var text = 'あ';
    for (var i=0; i<255; i++) {
      var el = createTextareaElement(text);
      el.select();
      var selectionText = el.value.substring(el.selectionStart, el.selectionEnd);
      assert_equals(selectionText, text, "Selection text mismatched");
      el.parentNode.removeChild(el);
      text += 'あ';
    }
  }, "test if non-ascii selection text is correct for textarea");


  for (var append of [true, false]) {
    test(function() {
      var el = createInputElement(sampleText, append);
      // If there is no selection, then it must return the offset(in logical order)
      // to the character that immediately follows the text entry cursor.
      assert_equals(el.selectionStart, el.value.length,
                    "SelectionStart offset without selection in " + el.id);
      el.select();
      assert_equals(el.selectionStart, 0, "SelectionStart offset");
      el.remove();
    }, "test SelectionStart offset for input that is " +
         (append ? "appended" : " not appended"));
  }

  for (var append of [true, false]) {
    test(function() {
      var el = createTextareaElement(sampleText, append);
      // If there is no selection, then it must return the offset(in logical order)
      // to the character that immediately follows the text entry cursor.
      assert_equals(el.selectionStart, el.value.length,
                    "SelectionStart offset without selection in " + el.id);
      el.select();
      assert_equals(el.selectionStart, 0, "SelectionStart offset");
      el.remove();
    }, "test SelectionStart offset for textarea that is " +
         (append ? "appended" : " not appended"));
  }

  for (var append of [true, false]) {
    test(function() {
      var el = createInputElement(sampleText, append);
      // If there is no selection, then it must return the offset(in logical order)
      // to the character that immediately follows the text entry cursor.
      assert_equals(el.selectionEnd, el.value.length,
                    "SelectionEnd offset without selection in " + el.id);
      el.select();
      assert_equals(el.selectionEnd, el.value.length, "SelectionEnd offset");
      el.remove();
    }, "test SelectionEnd offset for input that is " +
         (append ? "appended" : " not appended"));
  }


  for (var append of [true, false]) {
    test(function() {
      var el = createTextareaElement(sampleText, append);
      // If there is no selection, then it must return the offset(in logical order)
      // to the character that immediately follows the text entry cursor.
      assert_equals(el.selectionEnd, el.value.length,
                    "SelectionEnd offset without selection in " + el.id);
      el.select();
      assert_equals(el.selectionEnd, el.value.length, "SelectionEnd offset");
      el.remove();
    }, "test SelectionEnd offset for textarea that is " +
         (append ? "appended" : " not appended"));
  }

  test(function() {
    var el = createInputElement(sampleText);
    assert_in_array(el.selectionDirection, dirs, "SelectionDirection");
    el.select();
    assert_in_array(el.selectionDirection, dirs, "SelectionDirection");
    el.parentNode.removeChild(el);
  }, "test SelectionDirection for input");


  test(function() {
    var el = createTextareaElement(sampleText);
    assert_in_array(el.selectionDirection, dirs, "SelectionDirection");
    el.select();
    assert_in_array(el.selectionDirection, dirs, "SelectionDirection");
    el.parentNode.removeChild(el);
  }, "test SelectionDirection for textarea");

  promise_test(async () => {
    // cause a layout overflow
    const el = createInputElement(sampleText.repeat(100));
    el.selectionEnd = 0;
    await new Promise(requestAnimationFrame);
    assert_equals(el.scrollLeft, 0);

    el.select();
    await new Promise(requestAnimationFrame);
    assert_equals(el.scrollLeft, 0);
    el.remove();
  }, `test scrollLeft for input`);

  promise_test(async () => {
    // cause a layout overflow
    const el = createInputElement(sampleText.repeat(100));
    el.scrollLeft = 33;

    el.select();
    await new Promise(requestAnimationFrame);
    assert_equals(el.scrollLeft, 33);
    el.remove();
  }, `test scrollLeft preservation for input`);

  for (const localName of ["input", "textarea"]) {
    promise_test(async () => {
      const container = document.createElement("div");
      container.style.height = "100px";
      container.style.overflow = "scroll";
      const element = document.createElement(localName);
      element.style.marginTop = "120px";
      container.append(element);
      document.body.append(container);

      element.select();
      await new Promise(requestAnimationFrame);
      assert_equals(container.scrollTop, 0);

      container.remove();
    }, `test container.scrollTop for ${localName}`);
  }
</script>