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