summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/forms/textfieldselection/textfieldselection-setSelectionRange.html
blob: 3aba6b7adb347c508e34d3e895e316b0cb453a90 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<!DOCTYPE HTML>
<title>Test of text field setSelectionRange</title>
<link rel="author" title="Takeharu.Oshida" href="mailto:georgeosddev@gmail.com">
<link rel="help" href="https://html.spec.whatwg.org/multipage/#dom-textarea/input-setselectionrange">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<div id="hide" style="display: block">
  <input id="a" type="text" value="abcde">
  <textarea id="b">abcde</textarea>
</div>
<script>
var expected_direction_none;
setup(function() {
  var input = document.createElement("input");
  input.setSelectionRange(0, 1, "none");
  var direction = input.selectionDirection;
  if (direction !== "none" && direction !== "forward") {
    throw new Error("Unexpected direction");
  }
  expected_direction_none = direction;
});

test(function() {
  var input = document.getElementById("a");
  test(function() {
    assert_equals(typeof(input.setSelectionRange), "function", "element must have 'setSelectionRange' function");
  },"input typeof(input.setSelectionRange)'");

  test(function() {
    assert_equals(input.setSelectionRange(0,1,"forward"),undefined,"setSelectionRange is void functuon");
  },"input setSelectionRange return void");

  test(function() {
    input.setSelectionRange(0,1)
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(0,1)');

  test(function() {
    input.setSelectionRange(0,input.value.length+1)
    assert_equals(input.selectionEnd, input.value.length, "Arguments greater than the length of the value of the text field must be treated as pointing at the end of the text field");
  },'input setSelectionRange(0,input.value.length+1)');

  test(function() {
    input.setSelectionRange(input.value.length+1,input.value.length+1)
    assert_equals(input.selectionStart, input.value.length, "Arguments (start) greater than the length of the value of the text field must be treated as pointing at the end of the text field");
    assert_equals(input.selectionEnd, input.value.length, "Arguments (end) greater than the length of the value of the text field must be treated as pointing at the end of the text field");
  },'input setSelectionRange(input.value.length+1,input.value.length+1)');

  test(function() {
    input.setSelectionRange(input.value.length+1,1)
    assert_equals(input.selectionStart, 1, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(input.value.length+1,1)');

  test(function() {
    input.setSelectionRange(2,2)
    assert_equals(input.selectionStart, 2, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
    assert_equals(input.selectionEnd, 2, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
  },'input setSelectionRange(2,2)');

  test(function() {
    input.setSelectionRange(2,1)
    assert_equals(input.selectionStart, 1, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
    assert_equals(input.selectionEnd, 1, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
  },'input setSelectionRange(2,1)');

  test(function() {
    input.setSelectionRange(0,1,"backward")
    assert_equals(input.selectionDirection, "backward", 'The direction of the selection must be set to backward if direction is a case-sensitive match for the string "backward"');
  },'input direction of setSelectionRange(0,1,"backward")');

  test(function() {
    input.setSelectionRange(0,1,"forward")
    assert_equals(input.selectionDirection, "forward", 'The direction of the selection must be set to forward if direction is a case-sensitive match for the string "forward"');
  },'input direction of setSelectionRange(0,1,"forward")');

  test(function() {
    input.setSelectionRange(0,1,"none")
    assert_equals(input.selectionDirection, expected_direction_none);
  },'input direction of setSelectionRange(0,1,"none")');

  test(function() {
    input.setSelectionRange(0,1,"hoge")
    assert_equals(input.selectionDirection, expected_direction_none);
  },'input direction of setSelectionRange(0,1,"hoge")');

  test(function() {
    input.setSelectionRange(0,1,"BACKWARD")
    assert_equals(input.selectionDirection, expected_direction_none);
  },'input direction of setSelectionRange(0,1,"BACKWARD")');

  test(function() {
    input.setSelectionRange(0,1)
    assert_equals(input.selectionDirection, expected_direction_none);
  },'input direction of setSelectionRange(0,1)');

  test(function() {
    input.setSelectionRange(1,-1);
    assert_equals(input.selectionStart, 1, "element.selectionStart should be 1");
    assert_equals(input.selectionEnd, input.value.length, "ECMAScript conversion to unsigned long");
  },'input setSelectionRange(1,-1)');

  test(function() {
    input.setSelectionRange(-1,1);
    assert_equals(input.selectionStart, 1, "ECMAScript conversion to unsigned long + if end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(-1,1)');

  test(function() {
    input.setSelectionRange("string",1);
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange("string",1)');

  test(function() {
    input.setSelectionRange(true,1);
    assert_equals(input.selectionStart, 1, "element.selectionStart should be 1");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(true,1)');

  test(function() {
    input.setSelectionRange([],1);
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange([],1)');

  test(function() {
    input.setSelectionRange({},1);
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange({},1)');

  test(function() {
    input.setSelectionRange(NaN,1);
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(NaN,1)');

  test(function() {
    input.setSelectionRange(null,1);
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(null,1)');

  test(function() {
    input.setSelectionRange(undefined,1);
    assert_equals(input.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1");
  },'input setSelectionRange(undefined,1)');

  test(function() {
    input.setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1);
    assert_equals(input.selectionStart, input.value.length,
                 "element.selectionStart should be value.length");
    assert_equals(input.selectionEnd, input.value.length,
                  "element.selectionEnd should be value.length");
  }, 'input setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1)');

  test(function() {
    input.setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1);
    assert_equals(input.selectionStart, input.value.length,
                 "element.selectionStart should be value.length");
    assert_equals(input.selectionEnd, input.value.length,
                  "element.selectionEnd should be value.length");
  }, 'input setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1)');
},"test of input.setSelectionRange");

async_test(function() {
  var q = false;
  var input = document.getElementById("a");
  input.addEventListener("select", this.step_func_done(function(e) {
    assert_true(q, "event should be queued");
    assert_true(e.isTrusted, "event is trusted");
    assert_true(e.bubbles, "event bubbles");
    assert_false(e.cancelable, "event is not cancelable");
  }));
  input.setSelectionRange(0, 1);
  q = true;
}, "input setSelectionRange fires a select event");

test(function() {
  var textarea = document.getElementById("b");
  test(function() {
    assert_equals(typeof(textarea.setSelectionRange), "function", "element must have 'setSelectionRange' function");
  },"textarea typeof(input.setSelectionRange)'");

  test(function() {
    assert_equals(textarea.setSelectionRange(0,1,"forward"),undefined,"setSelectionRange is void functuon");
  },"textarea setSelectionRange return void");

  test(function() {
    textarea.setSelectionRange(0,1)
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionEnd should be 1");
  },'textarea setSelectionRange(0,1)');

  test(function() {
    textarea.setSelectionRange(0,textarea.value.length+1)
    assert_equals(textarea.selectionEnd, textarea.value.length, "Arguments greater than the length of the value of the text field must be treated as pointing at the end of the text field");
  },'textarea setSelectionRange(0,textarea.value.length+1)');

  test(function() {
    textarea.setSelectionRange(2,2)
    assert_equals(textarea.selectionStart, 2, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
    assert_equals(textarea.selectionEnd, 2, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
  },'textarea setSelectionRange(2,2)');

  test(function() {
    textarea.setSelectionRange(2,1)
    assert_equals(textarea.selectionStart, 1, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
    assert_equals(textarea.selectionEnd, 1, "If end is less than or equal to start then the start of the selection and the end of the selection must both be placed immediately before the character with offset end");
  },'textarea setSelectionRange(2,1)');

  test(function() {
    textarea.setSelectionRange(0,1,"backward")
    assert_equals(textarea.selectionDirection, "backward", 'The direction of the selection must be set to backward if direction is a case-sensitive match for the string "backward"');
  },'textarea direction of setSelectionRange(0,1,"backward")');

  test(function() {
    textarea.setSelectionRange(0,1,"forward")
    assert_equals(textarea.selectionDirection, "forward", 'The direction of the selection must be set to forward if direction is a case-sensitive match for the string "forward"');
  },'textarea direction of setSelectionRange(0,1,"forward")');

  test(function() {
    textarea.setSelectionRange(0,1,"none")
    assert_equals(textarea.selectionDirection, expected_direction_none);
  },'textarea direction of setSelectionRange(0,1,"none")');

  test(function() {
    textarea.setSelectionRange(0,1,"hoge")
    assert_equals(textarea.selectionDirection, expected_direction_none);
  },'textarea direction of setSelectionRange(0,1,"hoge")');

  test(function() {
    textarea.setSelectionRange(0,1,"BACKWARD")
    assert_equals(textarea.selectionDirection, expected_direction_none);
  },'textarea direction of setSelectionRange(0,1,"BACKWARD")');

  test(function() {
    textarea.setSelectionRange(0,1)
    assert_equals(textarea.selectionDirection, expected_direction_none);
  },'textarea direction of setSelectionRange(0,1)');

  test(function() {
    textarea.setSelectionRange("string",1);
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange("string",1)');

  test(function() {
    textarea.setSelectionRange(true,1);
    assert_equals(textarea.selectionStart, 1, "element.selectionStart should be 1");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange(true,1)');

  test(function() {
    textarea.setSelectionRange([],1);
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange([],1)');

  test(function() {
    textarea.setSelectionRange({},1);
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange({},1)');

  test(function() {
    textarea.setSelectionRange(NaN,1);
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange(NaN,1)');

  test(function() {
    textarea.setSelectionRange(null,1);
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange(null,1)');

  test(function() {
    textarea.setSelectionRange(undefined,1);
    assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0");
    assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1");
  },'textarea setSelectionRange(undefined,1)');

  test(function() {
    textarea.setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1);
    assert_equals(textarea.selectionStart, textarea.value.length,
                 "element.selectionStart should be value.length");
    assert_equals(textarea.selectionEnd, textarea.value.length,
                  "element.selectionEnd should be value.length");
  }, 'textarea setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1)');

  test(function() {
    textarea.setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1);
    assert_equals(textarea.selectionStart, textarea.value.length,
                 "element.selectionStart should be value.length");
    assert_equals(textarea.selectionEnd, textarea.value.length,
                  "element.selectionEnd should be value.length");
  }, 'textarea setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1)');
},"test of textarea.setSelectionRange");
</script>