|
1 <!doctype html> |
|
2 <title>Selection.collapseTo(Start|End)() tests</title> |
|
3 <div id=log></div> |
|
4 <script src=/resources/testharness.js></script> |
|
5 <script src=/resources/testharnessreport.js></script> |
|
6 <script src=common.js></script> |
|
7 <script> |
|
8 "use strict"; |
|
9 |
|
10 // Also test a selection with no ranges |
|
11 testRanges.unshift("[]"); |
|
12 |
|
13 for (var i = 0; i < testRanges.length; i++) { |
|
14 test(function() { |
|
15 selection.removeAllRanges(); |
|
16 var endpoints = eval(testRanges[i]); |
|
17 if (!endpoints.length) { |
|
18 assert_throws("INVALID_STATE_ERR", function() { |
|
19 selection.collapseToStart(); |
|
20 }, "Must throw InvalidStateErr if the selection's range is null"); |
|
21 return; |
|
22 } |
|
23 |
|
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); |
|
28 |
|
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()"); |
|
38 |
|
39 var expectedEndpoint = [ |
|
40 selection.getRangeAt(0).startContainer, |
|
41 selection.getRangeAt(0).startOffset |
|
42 ]; |
|
43 |
|
44 selection.collapseToStart(); |
|
45 |
|
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()"); |
|
65 |
|
66 // Copy-paste of above |
|
67 test(function() { |
|
68 selection.removeAllRanges(); |
|
69 var endpoints = eval(testRanges[i]); |
|
70 if (!endpoints.length) { |
|
71 assert_throws("INVALID_STATE_ERR", function() { |
|
72 selection.collapseToEnd(); |
|
73 }, "Must throw InvalidStateErr if the selection's range is null"); |
|
74 return; |
|
75 } |
|
76 |
|
77 var addedRange = ownerDocument(endpoints[0]).createRange(); |
|
78 addedRange.setStart(endpoints[0], endpoints[1]); |
|
79 addedRange.setEnd(endpoints[2], endpoints[3]); |
|
80 selection.addRange(addedRange); |
|
81 |
|
82 // We don't penalize browsers here for mishandling addRange() and |
|
83 // adding a different range than we specified. They fail addRange() |
|
84 // tests for that, and don't have to fail collapseToStart/End() tests |
|
85 // too. They do fail if they throw unexpectedly, though. I also fail |
|
86 // them if there's no range at all, because otherwise they could pass |
|
87 // all tests if addRange() always does nothing and collapseToStart() |
|
88 // always throws. |
|
89 assert_equals(selection.rangeCount, 1, |
|
90 "Sanity check: rangeCount must equal 1 after addRange()"); |
|
91 |
|
92 var expectedEndpoint = [ |
|
93 selection.getRangeAt(0).endContainer, |
|
94 selection.getRangeAt(0).endOffset |
|
95 ]; |
|
96 |
|
97 selection.collapseToEnd(); |
|
98 |
|
99 assert_equals(selection.rangeCount, 1, |
|
100 "selection.rangeCount must equal 1"); |
|
101 assert_equals(selection.focusNode, expectedEndpoint[0], |
|
102 "focusNode must equal the original end node"); |
|
103 assert_equals(selection.focusOffset, expectedEndpoint[1], |
|
104 "focusOffset must equal the original end offset"); |
|
105 assert_equals(selection.anchorNode, expectedEndpoint[0], |
|
106 "anchorNode must equal the original end node"); |
|
107 assert_equals(selection.anchorOffset, expectedEndpoint[1], |
|
108 "anchorOffset must equal the original end offset"); |
|
109 assert_equals(addedRange.startContainer, endpoints[0], |
|
110 "collapseToEnd() must not change the startContainer of the selection's original range"); |
|
111 assert_equals(addedRange.startOffset, endpoints[1], |
|
112 "collapseToEnd() must not change the startOffset of the selection's original range"); |
|
113 assert_equals(addedRange.endContainer, endpoints[2], |
|
114 "collapseToEnd() must not change the endContainer of the selection's original range"); |
|
115 assert_equals(addedRange.endOffset, endpoints[3], |
|
116 "collapseToEnd() must not change the endOffset of the selection's original range"); |
|
117 }, "Range " + i + " " + testRanges[i] + " collapseToEnd()"); |
|
118 } |
|
119 |
|
120 testDiv.style.display = "none"; |
|
121 </script> |