Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 1 | <!doctype html> |
| 2 | <title>Selection direction tests</title> |
| 3 | <meta charset=utf-8> |
| 4 | <div id=test> |
| 5 | <p>This is a manual test, since there's no way to synthesize keyboard or |
| 6 | mouse input. Click after the letter "c" in the following paragraph and |
| 7 | drag backwards so that both the "b" and the "c" are highlighted, then click |
| 8 | the "Test" button: |
| 9 | |
| 10 | <p>abcd <button onclick=testDirection()>Test</button> |
| 11 | |
| 12 | <p>efghi |
| 13 | </div> |
| 14 | <div id=log></div> |
| 15 | <script src=/resources/testharness.js></script> |
| 16 | <script src=/resources/testharnessreport.js></script> |
| 17 | <script> |
| 18 | setup({explicit_done: true}); |
| 19 | |
| 20 | function testDirection() { |
| 21 | var testDiv = document.getElementById("test"); |
| 22 | var p = testDiv.getElementsByTagName("p")[1].firstChild; |
| 23 | var selection = getSelection(); |
| 24 | var range = selection.getRangeAt(0); |
| 25 | test(function() { |
| 26 | assert_equals(range.toString(), "bc"); |
| 27 | }, "The expected range is selected"); |
| 28 | test(function() { |
| 29 | assert_equals(selection.anchorNode, p); |
| 30 | assert_equals(selection.focusNode, p); |
| 31 | }, "Expected node is initially selected"); |
| 32 | test(function() { |
| 33 | assert_array_equals([selection.anchorOffset, selection.focusOffset].sort(), [1, 3]); |
| 34 | }, "Expected offsets are initially selected (maybe not in order)"); |
| 35 | test(function() { |
| 36 | assert_equals(selection.anchorOffset, 3); |
| 37 | assert_equals(selection.focusOffset, 1); |
| 38 | }, "Offsets are backwards for initial selection"), |
| 39 | test(function() { |
| 40 | assert_equals(selection.anchorNode, range.endContainer); |
| 41 | assert_equals(selection.anchorOffset, range.endOffset); |
| 42 | assert_equals(selection.focusNode, range.startContainer); |
| 43 | assert_equals(selection.focusOffset, range.startOffset); |
| 44 | }, "Offsets match the range for initial selection"); |
| 45 | |
| 46 | // Per spec, the direction of the selection remains even if you zap a range |
| 47 | // and add a new one. |
| 48 | test(function() { |
| 49 | selection.removeRange(range); |
| 50 | range = document.createRange(); |
| 51 | p = testDiv.getElementsByTagName("p")[0].firstChild; |
| 52 | range.setStart(p, 0); |
| 53 | range.setEnd(p, 4); |
| 54 | assert_equals(range.toString(), "This"); |
| 55 | selection.addRange(range); |
| 56 | }, "removeRange()/addRange() successful"); |
| 57 | test(function() { |
| 58 | assert_equals(selection.anchorNode, p); |
| 59 | assert_equals(selection.focusNode, p); |
| 60 | }, "Expected node is selected after remove/addRange()"); |
| 61 | test(function() { |
| 62 | assert_array_equals([selection.anchorOffset, selection.focusOffset].sort(), [0, 4]); |
| 63 | }, "Expected offsets are selected after remove/addRange() (maybe not in order)"); |
| 64 | test(function() { |
| 65 | assert_equals(selection.anchorOffset, 4); |
| 66 | assert_equals(selection.focusOffset, 0); |
| 67 | }, "Offsets are backwards after remove/addRange()"), |
| 68 | test(function() { |
| 69 | assert_equals(selection.anchorNode, range.endContainer); |
| 70 | assert_equals(selection.anchorOffset, range.endOffset); |
| 71 | assert_equals(selection.focusNode, range.startContainer); |
| 72 | assert_equals(selection.focusOffset, range.startOffset); |
| 73 | }, "Offsets match the range after remove/addRange()"); |
| 74 | |
| 75 | // But if you call removeAllRanges(), the direction should reset to |
| 76 | // forwards. |
| 77 | test(function() { |
| 78 | selection.removeAllRanges(); |
| 79 | range = document.createRange(); |
| 80 | p = testDiv.getElementsByTagName("p")[2].firstChild; |
| 81 | range.setStart(p, 2); |
| 82 | range.setEnd(p, 5); |
| 83 | assert_equals(range.toString(), "ghi"); |
| 84 | selection.addRange(range); |
| 85 | }, "removeAllRanges() successful"); |
| 86 | test(function() { |
| 87 | assert_equals(selection.anchorNode, p); |
| 88 | assert_equals(selection.focusNode, p); |
| 89 | }, "Expected node is selected after removeAllRanges()"); |
| 90 | test(function() { |
| 91 | assert_array_equals([selection.anchorOffset, selection.focusOffset].sort(), [2, 5]); |
| 92 | }, "Expected offsets are selected after removeAllRanges() (maybe not in order)"); |
| 93 | test(function() { |
| 94 | assert_equals(selection.anchorOffset, 2); |
| 95 | assert_equals(selection.focusOffset, 5); |
| 96 | }, "Offsets are forwards after removeAllRanges()"); |
| 97 | test(function() { |
| 98 | assert_equals(selection.anchorNode, range.startContainer); |
| 99 | assert_equals(selection.anchorOffset, range.startOffset); |
| 100 | assert_equals(selection.focusNode, range.endContainer); |
| 101 | assert_equals(selection.focusOffset, range.endOffset); |
| 102 | }, "Offsets match the range after removeAllRanges()"); |
| 103 | |
| 104 | done(); |
| 105 | } |
| 106 | </script> |