OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <title>Selection.collapse() tests</title> |
| 3 <meta name=timeout content=long> |
| 4 <div id=log></div> |
| 5 <script src=/resources/testharness.js></script> |
| 6 <script src=/resources/testharnessreport.js></script> |
| 7 <script src=common.js></script> |
| 8 <script> |
| 9 "use strict"; |
| 10 |
| 11 function testCollapse(range, point) { |
| 12 selection.removeAllRanges(); |
| 13 var addedRange; |
| 14 if (range) { |
| 15 addedRange = range.cloneRange(); |
| 16 selection.addRange(addedRange); |
| 17 } |
| 18 |
| 19 if (point[0].nodeType == Node.DOCUMENT_TYPE_NODE) { |
| 20 assert_throws("INVALID_NODE_TYPE_ERR", function() { |
| 21 selection.collapse(point[0], point[1]); |
| 22 }, "Must throw INVALID_NODE_TYPE_ERR when collapse()ing if the node is a
DocumentType"); |
| 23 return; |
| 24 } |
| 25 |
| 26 if (point[1] < 0 || point[1] > getNodeLength(point[0])) { |
| 27 assert_throws("INDEX_SIZE_ERR", function() { |
| 28 selection.collapse(point[0], point[1]); |
| 29 }, "Must throw INDEX_SIZE_ERR when collapse()ing if the offset is negati
ve or greater than the node's length"); |
| 30 return; |
| 31 } |
| 32 |
| 33 selection.collapse(point[0], point[1]); |
| 34 |
| 35 assert_equals(selection.rangeCount, 1, |
| 36 "selection.rangeCount must equal 1 after collapse()"); |
| 37 assert_equals(selection.focusNode, point[0], |
| 38 "focusNode must equal the node we collapse()d to"); |
| 39 assert_equals(selection.focusOffset, point[1], |
| 40 "focusOffset must equal the offset we collapse()d to"); |
| 41 assert_equals(selection.focusNode, selection.anchorNode, |
| 42 "focusNode and anchorNode must be equal after collapse()"); |
| 43 assert_equals(selection.focusOffset, selection.anchorOffset, |
| 44 "focusOffset and anchorOffset must be equal after collapse()"); |
| 45 if (range) { |
| 46 assert_equals(addedRange.startContainer, range.startContainer, |
| 47 "collapse() must not change the startContainer of a preexisting Rang
e"); |
| 48 assert_equals(addedRange.endContainer, range.endContainer, |
| 49 "collapse() must not change the endContainer of a preexisting Range"
); |
| 50 assert_equals(addedRange.startOffset, range.startOffset, |
| 51 "collapse() must not change the startOffset of a preexisting Range")
; |
| 52 assert_equals(addedRange.endOffset, range.endOffset, |
| 53 "collapse() must not change the endOffset of a preexisting Range"); |
| 54 } |
| 55 } |
| 56 |
| 57 // Also test a selection with no ranges |
| 58 testRanges.unshift("[]"); |
| 59 |
| 60 // Don't want to eval() each point a bazillion times |
| 61 var testPointsCached = []; |
| 62 for (var i = 0; i < testPoints.length; i++) { |
| 63 testPointsCached.push(eval(testPoints[i])); |
| 64 } |
| 65 |
| 66 var tests = []; |
| 67 for (var i = 0; i < testRanges.length; i++) { |
| 68 var endpoints = eval(testRanges[i]); |
| 69 var range; |
| 70 test(function() { |
| 71 if (endpoints.length) { |
| 72 range = ownerDocument(endpoints[0]).createRange(); |
| 73 range.setStart(endpoints[0], endpoints[1]); |
| 74 range.setEnd(endpoints[2], endpoints[3]); |
| 75 } else { |
| 76 // Empty selection |
| 77 range = null; |
| 78 } |
| 79 }, "Set up range " + i + " " + testRanges[i]); |
| 80 for (var j = 0; j < testPoints.length; j++) { |
| 81 tests.push(["Range " + i + " " + testRanges[i] + ", point " + j + " " +
testPoints[j], range, testPointsCached[j]]); |
| 82 } |
| 83 } |
| 84 |
| 85 generate_tests(testCollapse, tests); |
| 86 |
| 87 testDiv.style.display = "none"; |
| 88 </script> |
OLD | NEW |