| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 1 | <!doctype html> |
| 2 | <title>Selection.collapseTo(Start|End)() tests</title> |
| 3 | <div id=log></div> |
| Aryeh Gregor | 33fcf01 | 2015-08-09 12:21:10 | [diff] [blame] | 4 | <script src=/resources/testharness.js></script> |
| 5 | <script src=/resources/testharnessreport.js></script> |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 6 | <script src=common.js></script> |
| 7 | <script> |
| 8 | "use strict"; |
| 9 | |
| tkent | 69ff91c | 2017-02-21 20:10:15 | [diff] [blame] | 10 | test(function() { |
| 11 | selection.removeAllRanges(); |
| Stephen McGruer | 3696f22 | 2020-01-23 19:11:58 | [diff] [blame] | 12 | assert_throws_dom("INVALID_STATE_ERR", function() { |
| tkent | 69ff91c | 2017-02-21 20:10:15 | [diff] [blame] | 13 | selection.collapseToStart(); |
| 14 | }); |
| 15 | }, "Must throw InvalidStateErr if the selection's range is null"); |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 16 | |
| 17 | for (var i = 0; i < testRanges.length; i++) { |
| tkent | 69ff91c | 2017-02-21 20:10:15 | [diff] [blame] | 18 | var endpoints = eval(testRanges[i]); |
| 19 | if (!isSelectableNode(endpoints[0]) || !isSelectableNode(endpoints[2])) |
| 20 | continue; |
| Aryeh Gregor | 01a1172 | 2015-08-09 13:04:25 | [diff] [blame] | 21 | test(function() { |
| 22 | selection.removeAllRanges(); |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 23 | |
| Aryeh Gregor | 01a1172 | 2015-08-09 13:04:25 | [diff] [blame] | 24 | var addedRange = ownerDocument(endpoints[0]).createRange(); |
| 25 | addedRange.setStart(endpoints[0], endpoints[1]); |
| 26 | addedRange.setEnd(endpoints[2], endpoints[3]); |
| 27 | selection.addRange(addedRange); |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 28 | |
| Aryeh Gregor | 01a1172 | 2015-08-09 13:04:25 | [diff] [blame] | 29 | // We don't penalize browsers here for mishandling addRange() and |
| 30 | // adding a different range than we specified. They fail addRange() |
| 31 | // tests for that, and don't have to fail collapseToStart/End() tests |
| 32 | // too. They do fail if they throw unexpectedly, though. I also fail |
| 33 | // them if there's no range at all, because otherwise they could pass |
| 34 | // all tests if addRange() always does nothing and collapseToStart() |
| 35 | // always throws. |
| 36 | assert_equals(selection.rangeCount, 1, |
| 37 | "Sanity check: rangeCount must equal 1 after addRange()"); |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 38 | |
| Aryeh Gregor | 01a1172 | 2015-08-09 13:04:25 | [diff] [blame] | 39 | var expectedEndpoint = [ |
| 40 | selection.getRangeAt(0).startContainer, |
| 41 | selection.getRangeAt(0).startOffset |
| 42 | ]; |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 43 | |
| Aryeh Gregor | 01a1172 | 2015-08-09 13:04:25 | [diff] [blame] | 44 | selection.collapseToStart(); |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 45 | |
| Aryeh Gregor | 01a1172 | 2015-08-09 13:04:25 | [diff] [blame] | 46 | assert_equals(selection.rangeCount, 1, |
| 47 | "selection.rangeCount must equal 1"); |
| 48 | assert_equals(selection.focusNode, expectedEndpoint[0], |
| 49 | "focusNode must equal the original start node"); |
| 50 | assert_equals(selection.focusOffset, expectedEndpoint[1], |
| 51 | "focusOffset must equal the original start offset"); |
| 52 | assert_equals(selection.anchorNode, expectedEndpoint[0], |
| 53 | "anchorNode must equal the original start node"); |
| 54 | assert_equals(selection.anchorOffset, expectedEndpoint[1], |
| 55 | "anchorOffset must equal the original start offset"); |
| 56 | assert_equals(addedRange.startContainer, endpoints[0], |
| 57 | "collapseToStart() must not change the startContainer of the selection's original range"); |
| 58 | assert_equals(addedRange.startOffset, endpoints[1], |
| 59 | "collapseToStart() must not change the startOffset of the selection's original range"); |
| 60 | assert_equals(addedRange.endContainer, endpoints[2], |
| 61 | "collapseToStart() must not change the endContainer of the selection's original range"); |
| 62 | assert_equals(addedRange.endOffset, endpoints[3], |
| 63 | "collapseToStart() must not change the endOffset of the selection's original range"); |
| 64 | }, "Range " + i + " " + testRanges[i] + " collapseToStart()"); |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 65 | |
| Aryeh Gregor | 01a1172 | 2015-08-09 13:04:25 | [diff] [blame] | 66 | // Copy-paste of above |
| 67 | test(function() { |
| 68 | selection.removeAllRanges(); |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 69 | |
| Aryeh Gregor | 01a1172 | 2015-08-09 13:04:25 | [diff] [blame] | 70 | var addedRange = ownerDocument(endpoints[0]).createRange(); |
| 71 | addedRange.setStart(endpoints[0], endpoints[1]); |
| 72 | addedRange.setEnd(endpoints[2], endpoints[3]); |
| 73 | selection.addRange(addedRange); |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 74 | |
| Aryeh Gregor | 01a1172 | 2015-08-09 13:04:25 | [diff] [blame] | 75 | // We don't penalize browsers here for mishandling addRange() and |
| 76 | // adding a different range than we specified. They fail addRange() |
| 77 | // tests for that, and don't have to fail collapseToStart/End() tests |
| 78 | // too. They do fail if they throw unexpectedly, though. I also fail |
| 79 | // them if there's no range at all, because otherwise they could pass |
| 80 | // all tests if addRange() always does nothing and collapseToStart() |
| 81 | // always throws. |
| 82 | assert_equals(selection.rangeCount, 1, |
| 83 | "Sanity check: rangeCount must equal 1 after addRange()"); |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 84 | |
| Aryeh Gregor | 01a1172 | 2015-08-09 13:04:25 | [diff] [blame] | 85 | var expectedEndpoint = [ |
| 86 | selection.getRangeAt(0).endContainer, |
| 87 | selection.getRangeAt(0).endOffset |
| 88 | ]; |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 89 | |
| Aryeh Gregor | 01a1172 | 2015-08-09 13:04:25 | [diff] [blame] | 90 | selection.collapseToEnd(); |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 91 | |
| Aryeh Gregor | 01a1172 | 2015-08-09 13:04:25 | [diff] [blame] | 92 | assert_equals(selection.rangeCount, 1, |
| 93 | "selection.rangeCount must equal 1"); |
| 94 | assert_equals(selection.focusNode, expectedEndpoint[0], |
| 95 | "focusNode must equal the original end node"); |
| 96 | assert_equals(selection.focusOffset, expectedEndpoint[1], |
| 97 | "focusOffset must equal the original end offset"); |
| 98 | assert_equals(selection.anchorNode, expectedEndpoint[0], |
| 99 | "anchorNode must equal the original end node"); |
| 100 | assert_equals(selection.anchorOffset, expectedEndpoint[1], |
| 101 | "anchorOffset must equal the original end offset"); |
| 102 | assert_equals(addedRange.startContainer, endpoints[0], |
| 103 | "collapseToEnd() must not change the startContainer of the selection's original range"); |
| 104 | assert_equals(addedRange.startOffset, endpoints[1], |
| 105 | "collapseToEnd() must not change the startOffset of the selection's original range"); |
| 106 | assert_equals(addedRange.endContainer, endpoints[2], |
| 107 | "collapseToEnd() must not change the endContainer of the selection's original range"); |
| 108 | assert_equals(addedRange.endOffset, endpoints[3], |
| 109 | "collapseToEnd() must not change the endOffset of the selection's original range"); |
| 110 | }, "Range " + i + " " + testRanges[i] + " collapseToEnd()"); |
| Aryeh Gregor | 8fe7c02 | 2011-10-11 21:30:30 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | testDiv.style.display = "none"; |
| 114 | </script> |