|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>Setting selection through APIs does not focus unless selection is inside the text control.</title> |
| 5 | +</head> |
| 6 | +<body> |
| 7 | + <p>Setting selection through APIs does not focus unless selection is inside the text control.</p> |
| 8 | + <input id="input" value="XXXXXXXX"> |
| 9 | + <textarea id="textarea">XXXXXXXX</textarea> |
| 10 | + <script src="../../resources/testharness.js"></script> |
| 11 | + <script src="../../resources/testharnessreport.js"></script> |
| 12 | + <script> |
| 13 | + function testSetSelectionRange(element, expectFocus) { |
| 14 | + const selection = window.getSelection(); |
| 15 | + selection.removeAllRanges(); |
| 16 | + element.setSelectionRange(null, null); |
| 17 | + assert_equals(document.activeElement == element, expectFocus, `Element is should ${!expectFocus ? "not " : ""}be focused`); |
| 18 | + assert_equals(element.selectionStart, 0, "selectionStart is correctly set"); |
| 19 | + assert_equals(element.selectionStart, 0, "selectionEnd is correctly set"); |
| 20 | + element.setSelectionRange(2, 4); |
| 21 | + assert_equals(document.activeElement == element, expectFocus, `Element is should ${!expectFocus ? "not " : ""}be focused`); |
| 22 | + assert_equals(element.selectionStart, 2, "selectionStart is correctly set"); |
| 23 | + assert_equals(element.selectionEnd, 4, "selectionEnd is correctly set"); |
| 24 | + } |
| 25 | + function testSelectionStartEnd(element, expectFocus) { |
| 26 | + element.selectionStart = 3; |
| 27 | + element.selectionEnd = 5; |
| 28 | + assert_equals(document.activeElement == element, expectFocus, `Element is should ${!expectFocus ? "not " : ""}be focused`); |
| 29 | + assert_equals(element.selectionStart, 3, "selectionStart is correctly set"); |
| 30 | + assert_equals(element.selectionEnd, 5, "selectionEnd is correctly set"); |
| 31 | + } |
| 32 | + |
| 33 | + function testSetRangeText(element, expectFocus) { |
| 34 | + element.setRangeText('barrr', 0, 3, 'select'); |
| 35 | + assert_equals(document.activeElement == element, expectFocus, `Element is should ${!expectFocus ? "not " : ""}be focused`); |
| 36 | + } |
| 37 | + |
| 38 | + test(t => { |
| 39 | + t.add_cleanup(() => { input.blur(); textarea.blur(); getSelection().removeAllRanges(); }); |
| 40 | + testSetSelectionRange(input, false); |
| 41 | + testSetSelectionRange(textarea, false); |
| 42 | + input.focus(); |
| 43 | + testSetSelectionRange(input, true); |
| 44 | + textarea.focus(); |
| 45 | + testSetSelectionRange(textarea, true); |
| 46 | + }, "setSelectionRange does not focus unless selection is inside the text control."); |
| 47 | + |
| 48 | + test(t => { |
| 49 | + t.add_cleanup(() => { input.blur(); textarea.blur(); getSelection().removeAllRanges(); }); |
| 50 | + testSelectionStartEnd(input, false); |
| 51 | + testSelectionStartEnd(textarea, false); |
| 52 | + input.focus(); |
| 53 | + testSelectionStartEnd(input, true); |
| 54 | + textarea.focus(); |
| 55 | + testSelectionStartEnd(textarea, true); |
| 56 | + }, "selectionStart/selectionEnd does not focus unless selection is inside the text control."); |
| 57 | + |
| 58 | + test(t => { |
| 59 | + t.add_cleanup(() => { input.blur(); textarea.blur(); getSelection().removeAllRanges(); }); |
| 60 | + testSetRangeText(input, false); |
| 61 | + testSetRangeText(textarea, false); |
| 62 | + input.focus(); |
| 63 | + testSetRangeText(input, true); |
| 64 | + textarea.focus(); |
| 65 | + testSetRangeText(textarea, true); |
| 66 | + }, "setRangeText does not focus unless selection is inside the text control."); |
| 67 | + </script> |
| 68 | +</body> |
| 69 | +</html> |
0 commit comments