textfieldselection-setSelectionRange.html (15332B)
1 <!DOCTYPE HTML> 2 <title>Test of text field setSelectionRange</title> 3 <link rel="author" title="Takeharu.Oshida" href="mailto:georgeosddev@gmail.com"> 4 <link rel="help" href="https://html.spec.whatwg.org/multipage/#dom-textarea/input-setselectionrange"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <div id="log"></div> 8 <div id="hide" style="display: block"> 9 <input id="a" type="text" value="abcde"> 10 <textarea id="b">abcde</textarea> 11 </div> 12 <script> 13 var expected_direction_none; 14 setup(function() { 15 var input = document.createElement("input"); 16 input.setSelectionRange(0, 1, "none"); 17 var direction = input.selectionDirection; 18 if (direction !== "none" && direction !== "forward") { 19 throw new Error("Unexpected direction"); 20 } 21 expected_direction_none = direction; 22 }); 23 24 test(function() { 25 var input = document.getElementById("a"); 26 test(function() { 27 assert_equals(typeof(input.setSelectionRange), "function", "element must have 'setSelectionRange' function"); 28 },"input typeof(input.setSelectionRange)'"); 29 30 test(function() { 31 assert_equals(input.setSelectionRange(0,1,"forward"),undefined,"setSelectionRange is void functuon"); 32 },"input setSelectionRange return void"); 33 34 test(function() { 35 input.setSelectionRange(0,1) 36 assert_equals(input.selectionStart, 0, "element.selectionStart should be 0"); 37 assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1"); 38 },'input setSelectionRange(0,1)'); 39 40 test(function() { 41 input.setSelectionRange(0,input.value.length+1) 42 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"); 43 },'input setSelectionRange(0,input.value.length+1)'); 44 45 test(function() { 46 input.setSelectionRange(input.value.length+1,input.value.length+1) 47 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"); 48 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"); 49 },'input setSelectionRange(input.value.length+1,input.value.length+1)'); 50 51 test(function() { 52 input.setSelectionRange(input.value.length+1,1) 53 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"); 54 assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1"); 55 },'input setSelectionRange(input.value.length+1,1)'); 56 57 test(function() { 58 input.setSelectionRange(2,2) 59 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"); 60 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"); 61 },'input setSelectionRange(2,2)'); 62 63 test(function() { 64 input.setSelectionRange(2,1) 65 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"); 66 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"); 67 },'input setSelectionRange(2,1)'); 68 69 test(function() { 70 input.setSelectionRange(0,1,"backward") 71 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"'); 72 },'input direction of setSelectionRange(0,1,"backward")'); 73 74 test(function() { 75 input.setSelectionRange(0,1,"forward") 76 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"'); 77 },'input direction of setSelectionRange(0,1,"forward")'); 78 79 test(function() { 80 input.setSelectionRange(0,1,"none") 81 assert_equals(input.selectionDirection, expected_direction_none); 82 },'input direction of setSelectionRange(0,1,"none")'); 83 84 test(function() { 85 input.setSelectionRange(0,1,"hoge") 86 assert_equals(input.selectionDirection, expected_direction_none); 87 },'input direction of setSelectionRange(0,1,"hoge")'); 88 89 test(function() { 90 input.setSelectionRange(0,1,"BACKWARD") 91 assert_equals(input.selectionDirection, expected_direction_none); 92 },'input direction of setSelectionRange(0,1,"BACKWARD")'); 93 94 test(function() { 95 input.setSelectionRange(0,1) 96 assert_equals(input.selectionDirection, expected_direction_none); 97 },'input direction of setSelectionRange(0,1)'); 98 99 test(function() { 100 input.setSelectionRange(1,-1); 101 assert_equals(input.selectionStart, 1, "element.selectionStart should be 1"); 102 assert_equals(input.selectionEnd, input.value.length, "ECMAScript conversion to unsigned long"); 103 },'input setSelectionRange(1,-1)'); 104 105 test(function() { 106 input.setSelectionRange(-1,1); 107 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"); 108 assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1"); 109 },'input setSelectionRange(-1,1)'); 110 111 test(function() { 112 input.setSelectionRange("string",1); 113 assert_equals(input.selectionStart, 0, "element.selectionStart should be 0"); 114 assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1"); 115 },'input setSelectionRange("string",1)'); 116 117 test(function() { 118 input.setSelectionRange(true,1); 119 assert_equals(input.selectionStart, 1, "element.selectionStart should be 1"); 120 assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1"); 121 },'input setSelectionRange(true,1)'); 122 123 test(function() { 124 input.setSelectionRange([],1); 125 assert_equals(input.selectionStart, 0, "element.selectionStart should be 0"); 126 assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1"); 127 },'input setSelectionRange([],1)'); 128 129 test(function() { 130 input.setSelectionRange({},1); 131 assert_equals(input.selectionStart, 0, "element.selectionStart should be 0"); 132 assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1"); 133 },'input setSelectionRange({},1)'); 134 135 test(function() { 136 input.setSelectionRange(NaN,1); 137 assert_equals(input.selectionStart, 0, "element.selectionStart should be 0"); 138 assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1"); 139 },'input setSelectionRange(NaN,1)'); 140 141 test(function() { 142 input.setSelectionRange(null,1); 143 assert_equals(input.selectionStart, 0, "element.selectionStart should be 0"); 144 assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1"); 145 },'input setSelectionRange(null,1)'); 146 147 test(function() { 148 input.setSelectionRange(undefined,1); 149 assert_equals(input.selectionStart, 0, "element.selectionStart should be 0"); 150 assert_equals(input.selectionEnd, 1, "element.selectionEnd should be 1"); 151 },'input setSelectionRange(undefined,1)'); 152 153 test(function() { 154 input.setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1); 155 assert_equals(input.selectionStart, input.value.length, 156 "element.selectionStart should be value.length"); 157 assert_equals(input.selectionEnd, input.value.length, 158 "element.selectionEnd should be value.length"); 159 }, 'input setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1)'); 160 161 test(function() { 162 input.setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1); 163 assert_equals(input.selectionStart, input.value.length, 164 "element.selectionStart should be value.length"); 165 assert_equals(input.selectionEnd, input.value.length, 166 "element.selectionEnd should be value.length"); 167 }, 'input setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1)'); 168 },"test of input.setSelectionRange"); 169 170 async_test(function() { 171 var q = false; 172 var input = document.getElementById("a"); 173 input.addEventListener("select", this.step_func_done(function(e) { 174 assert_true(q, "event should be queued"); 175 assert_true(e.isTrusted, "event is trusted"); 176 assert_true(e.bubbles, "event bubbles"); 177 assert_false(e.cancelable, "event is not cancelable"); 178 })); 179 input.setSelectionRange(0, 1); 180 q = true; 181 }, "input setSelectionRange fires a select event"); 182 183 test(function() { 184 var textarea = document.getElementById("b"); 185 test(function() { 186 assert_equals(typeof(textarea.setSelectionRange), "function", "element must have 'setSelectionRange' function"); 187 },"textarea typeof(input.setSelectionRange)'"); 188 189 test(function() { 190 assert_equals(textarea.setSelectionRange(0,1,"forward"),undefined,"setSelectionRange is void functuon"); 191 },"textarea setSelectionRange return void"); 192 193 test(function() { 194 textarea.setSelectionRange(0,1) 195 assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0"); 196 assert_equals(textarea.selectionEnd, 1, "element.selectionEnd should be 1"); 197 },'textarea setSelectionRange(0,1)'); 198 199 test(function() { 200 textarea.setSelectionRange(0,textarea.value.length+1) 201 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"); 202 },'textarea setSelectionRange(0,textarea.value.length+1)'); 203 204 test(function() { 205 textarea.setSelectionRange(2,2) 206 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"); 207 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"); 208 },'textarea setSelectionRange(2,2)'); 209 210 test(function() { 211 textarea.setSelectionRange(2,1) 212 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"); 213 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"); 214 },'textarea setSelectionRange(2,1)'); 215 216 test(function() { 217 textarea.setSelectionRange(0,1,"backward") 218 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"'); 219 },'textarea direction of setSelectionRange(0,1,"backward")'); 220 221 test(function() { 222 textarea.setSelectionRange(0,1,"forward") 223 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"'); 224 },'textarea direction of setSelectionRange(0,1,"forward")'); 225 226 test(function() { 227 textarea.setSelectionRange(0,1,"none") 228 assert_equals(textarea.selectionDirection, expected_direction_none); 229 },'textarea direction of setSelectionRange(0,1,"none")'); 230 231 test(function() { 232 textarea.setSelectionRange(0,1,"hoge") 233 assert_equals(textarea.selectionDirection, expected_direction_none); 234 },'textarea direction of setSelectionRange(0,1,"hoge")'); 235 236 test(function() { 237 textarea.setSelectionRange(0,1,"BACKWARD") 238 assert_equals(textarea.selectionDirection, expected_direction_none); 239 },'textarea direction of setSelectionRange(0,1,"BACKWARD")'); 240 241 test(function() { 242 textarea.setSelectionRange(0,1) 243 assert_equals(textarea.selectionDirection, expected_direction_none); 244 },'textarea direction of setSelectionRange(0,1)'); 245 246 test(function() { 247 textarea.setSelectionRange("string",1); 248 assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0"); 249 assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1"); 250 },'textarea setSelectionRange("string",1)'); 251 252 test(function() { 253 textarea.setSelectionRange(true,1); 254 assert_equals(textarea.selectionStart, 1, "element.selectionStart should be 1"); 255 assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1"); 256 },'textarea setSelectionRange(true,1)'); 257 258 test(function() { 259 textarea.setSelectionRange([],1); 260 assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0"); 261 assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1"); 262 },'textarea setSelectionRange([],1)'); 263 264 test(function() { 265 textarea.setSelectionRange({},1); 266 assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0"); 267 assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1"); 268 },'textarea setSelectionRange({},1)'); 269 270 test(function() { 271 textarea.setSelectionRange(NaN,1); 272 assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0"); 273 assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1"); 274 },'textarea setSelectionRange(NaN,1)'); 275 276 test(function() { 277 textarea.setSelectionRange(null,1); 278 assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0"); 279 assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1"); 280 },'textarea setSelectionRange(null,1)'); 281 282 test(function() { 283 textarea.setSelectionRange(undefined,1); 284 assert_equals(textarea.selectionStart, 0, "element.selectionStart should be 0"); 285 assert_equals(textarea.selectionEnd, 1, "element.selectionStart should be 1"); 286 },'textarea setSelectionRange(undefined,1)'); 287 288 test(function() { 289 textarea.setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1); 290 assert_equals(textarea.selectionStart, textarea.value.length, 291 "element.selectionStart should be value.length"); 292 assert_equals(textarea.selectionEnd, textarea.value.length, 293 "element.selectionEnd should be value.length"); 294 }, 'textarea setSelectionRange(Math.pow(2,32) - 2, Math.pow(2,32) - 1)'); 295 296 test(function() { 297 textarea.setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1); 298 assert_equals(textarea.selectionStart, textarea.value.length, 299 "element.selectionStart should be value.length"); 300 assert_equals(textarea.selectionEnd, textarea.value.length, 301 "element.selectionEnd should be value.length"); 302 }, 'textarea setSelectionRange(Math.pow(2,31), Math.pow(2,32) - 1)'); 303 },"test of textarea.setSelectionRange"); 304 </script>