|
1 <!doctype html> |
|
2 <title>Selection extend() tests</title> |
|
3 <meta charset=utf-8> |
|
4 <body> |
|
5 <script src=/resources/testharness.js></script> |
|
6 <script src=/resources/testharnessreport.js></script> |
|
7 <script src=common.js></script> |
|
8 <div id=log></div> |
|
9 <script> |
|
10 "use strict"; |
|
11 |
|
12 // Also test a selection with no ranges |
|
13 testRanges.unshift("[]"); |
|
14 |
|
15 /** |
|
16 * We test Selections that go both forwards and backwards here. In the latter |
|
17 * case we need to use extend() to force it to go backwards, which is fair |
|
18 * enough, since that's what we're testing. We test collapsed selections only |
|
19 * once. |
|
20 */ |
|
21 for (var i = 0; i < testRanges.length; i++) { |
|
22 var endpoints = eval(testRanges[i]); |
|
23 for (var j = 0; j < testPoints.length; j++) { |
|
24 if (endpoints[0] == endpoints[2] |
|
25 && endpoints[1] == endpoints[3]) { |
|
26 // Test collapsed selections only once |
|
27 test(function() { |
|
28 setSelectionForwards(endpoints); |
|
29 testExtend(endpoints, eval(testPoints[j])); |
|
30 }, "extend() with range " + i + " " + testRanges[i] |
|
31 + " and point " + j + " " + testPoints[j]); |
|
32 } else { |
|
33 test(function() { |
|
34 setSelectionForwards(endpoints); |
|
35 testExtend(endpoints, eval(testPoints[j])); |
|
36 }, "extend() forwards with range " + i + " " + testRanges[i] |
|
37 + " and point " + j + " " + testPoints[j]); |
|
38 |
|
39 test(function() { |
|
40 setSelectionBackwards(endpoints); |
|
41 testExtend(endpoints, eval(testPoints[j])); |
|
42 }, "extend() backwards with range " + i + " " + testRanges[i] |
|
43 + " and point " + j + " " + testPoints[j]); |
|
44 } |
|
45 } |
|
46 } |
|
47 |
|
48 function testExtend(endpoints, target) { |
|
49 assert_equals(getSelection().rangeCount, endpoints.length/4, |
|
50 "Sanity check: rangeCount must be correct"); |
|
51 |
|
52 var node = target[0]; |
|
53 var offset = target[1]; |
|
54 |
|
55 // "If the context object's range is null, throw an InvalidStateError |
|
56 // exception and abort these steps." |
|
57 if (getSelection().rangeCount == 0) { |
|
58 assert_throws("INVALID_STATE_ERR", function() { |
|
59 selection.extend(node, offset); |
|
60 }, "extend() when rangeCount is 0 must throw InvalidStateError"); |
|
61 return; |
|
62 } |
|
63 |
|
64 assert_equals(getSelection().getRangeAt(0).startContainer, endpoints[0], |
|
65 "Sanity check: startContainer must be correct"); |
|
66 assert_equals(getSelection().getRangeAt(0).startOffset, endpoints[1], |
|
67 "Sanity check: startOffset must be correct"); |
|
68 assert_equals(getSelection().getRangeAt(0).endContainer, endpoints[2], |
|
69 "Sanity check: endContainer must be correct"); |
|
70 assert_equals(getSelection().getRangeAt(0).endOffset, endpoints[3], |
|
71 "Sanity check: endOffset must be correct"); |
|
72 |
|
73 // "Let anchor and focus be the context object's anchor and focus, and let |
|
74 // new focus be the boundary point (node, offset)." |
|
75 var anchorNode = getSelection().anchorNode; |
|
76 var anchorOffset = getSelection().anchorOffset; |
|
77 var focusNode = getSelection().focusNode; |
|
78 var focusOffset = getSelection().focusOffset; |
|
79 |
|
80 // "Let new range be a new range." |
|
81 // |
|
82 // We'll always be setting either new range's start or its end to new |
|
83 // focus, so we'll always throw at some point. Test that now. |
|
84 // |
|
85 // From DOM4's "set the start or end of a range": "If node is a doctype, |
|
86 // throw an "InvalidNodeTypeError" exception and terminate these steps." |
|
87 if (node.nodeType == Node.DOCUMENT_TYPE_NODE) { |
|
88 assert_throws("INVALID_NODE_TYPE_ERR", function() { |
|
89 selection.extend(node, offset); |
|
90 }, "extend() to a doctype must throw InvalidNodeTypeError"); |
|
91 return; |
|
92 } |
|
93 |
|
94 // From DOM4's "set the start or end of a range": "If offset is greater |
|
95 // than node's length, throw an "IndexSizeError" exception and terminate |
|
96 // these steps." |
|
97 // |
|
98 // FIXME: We should be casting offset to an unsigned int per WebIDL. Until |
|
99 // we do, we need the offset < 0 check too. |
|
100 if (offset < 0 || offset > getNodeLength(node)) { |
|
101 assert_throws("INDEX_SIZE_ERR", function() { |
|
102 selection.extend(node, offset); |
|
103 }, "extend() to an offset that's greater than node length (" + getNodeLength(node) + ") must throw IndexSizeError"); |
|
104 return; |
|
105 } |
|
106 |
|
107 // Now back to the editing spec. |
|
108 var originalRange = getSelection().getRangeAt(0); |
|
109 |
|
110 // "If node's root is not the same as the context object's range's root, |
|
111 // set new range's start and end to (node, offset)." |
|
112 // |
|
113 // "Otherwise, if anchor is before or equal to new focus, set new range's |
|
114 // start to anchor, then set its end to new focus." |
|
115 // |
|
116 // "Otherwise, set new range's start to new focus, then set its end to |
|
117 // anchor." |
|
118 // |
|
119 // "Set the context object's range to new range." |
|
120 // |
|
121 // "If new focus is before anchor, set the context object's direction to |
|
122 // backwards. Otherwise, set it to forwards." |
|
123 // |
|
124 // The upshot of all these is summed up by just testing the anchor and |
|
125 // offset. |
|
126 getSelection().extend(node, offset); |
|
127 |
|
128 if (furthestAncestor(anchorNode) == furthestAncestor(node)) { |
|
129 assert_equals(getSelection().anchorNode, anchorNode, |
|
130 "anchorNode must not change if the node passed to extend() has the same root as the original range"); |
|
131 assert_equals(getSelection().anchorOffset, anchorOffset, |
|
132 "anchorOffset must not change if the node passed to extend() has the same root as the original range"); |
|
133 } else { |
|
134 assert_equals(getSelection().anchorNode, node, |
|
135 "anchorNode must be the node passed to extend() if it has a different root from the original range"); |
|
136 assert_equals(getSelection().anchorOffset, offset, |
|
137 "anchorOffset must be the offset passed to extend() if the node has a different root from the original range"); |
|
138 } |
|
139 assert_equals(getSelection().focusNode, node, |
|
140 "focusNode must be the node passed to extend()"); |
|
141 assert_equals(getSelection().focusOffset, offset, |
|
142 "focusOffset must be the offset passed to extend()"); |
|
143 assert_not_equals(getSelection().getRangeAt(0), originalRange, |
|
144 "extend() must replace any existing range with a new one, not mutate the existing one"); |
|
145 } |
|
146 |
|
147 testDiv.style.display = "none"; |
|
148 </script> |