collapse.js (4204B)
1 "use strict"; 2 3 function testCollapse(range, point, method) { 4 selection.removeAllRanges(); 5 var addedRange; 6 if (range) { 7 addedRange = range.cloneRange(); 8 selection.addRange(addedRange); 9 } 10 11 if (point[0].nodeType == Node.DOCUMENT_TYPE_NODE) { 12 assert_throws_dom("INVALID_NODE_TYPE_ERR", function() { 13 selection[method](point[0], point[1]); 14 }, "Must throw INVALID_NODE_TYPE_ERR when " + method + "()ing if the node is a DocumentType"); 15 return; 16 } 17 18 if (point[1] < 0 || point[1] > getNodeLength(point[0])) { 19 assert_throws_dom("INDEX_SIZE_ERR", function() { 20 selection[method](point[0], point[1]); 21 }, "Must throw INDEX_SIZE_ERR when " + method + "()ing if the offset is negative or greater than the node's length"); 22 return; 23 } 24 25 if (!document.contains(point[0])) { 26 assertSelectionNoChange(function() { 27 selection[method](point[0], point[1]); 28 }); 29 return; 30 } 31 32 selection[method](point[0], point[1]); 33 34 assert_equals(selection.rangeCount, 1, 35 "selection.rangeCount must equal 1 after " + method + "()"); 36 assert_equals(selection.focusNode, point[0], 37 "focusNode must equal the node we " + method + "()d to"); 38 assert_equals(selection.focusOffset, point[1], 39 "focusOffset must equal the offset we " + method + "()d to"); 40 assert_equals(selection.focusNode, selection.anchorNode, 41 "focusNode and anchorNode must be equal after " + method + "()"); 42 assert_equals(selection.focusOffset, selection.anchorOffset, 43 "focusOffset and anchorOffset must be equal after " + method + "()"); 44 if (range) { 45 assert_equals(addedRange.startContainer, range.startContainer, 46 method + "() must not change the startContainer of a preexisting Range"); 47 assert_equals(addedRange.endContainer, range.endContainer, 48 method + "() must not change the endContainer of a preexisting Range"); 49 assert_equals(addedRange.startOffset, range.startOffset, 50 method + "() must not change the startOffset of a preexisting Range"); 51 assert_equals(addedRange.endOffset, range.endOffset, 52 method + "() must not change the endOffset of a preexisting Range"); 53 } 54 } 55 56 // Also test a selection with no ranges 57 testRanges.unshift("[]"); 58 59 // Don't want to eval() each point a bazillion times 60 var testPointsCached = []; 61 for (var i = 0; i < testPoints.length; i++) { 62 testPointsCached.push(eval(testPoints[i])); 63 } 64 65 // Run a subset of all of collapse tests. 66 // Huge number of tests in a single file causes problems. Each of 67 // collapse-NN.html runs a part of them. 68 // 69 // startIndex - Start index in testRanges array 70 // optionalEndIndex - End index in testRanges array + 1. If this argument is 71 // omitted, testRanges.length is applied. 72 function testCollapseSubSet(startIndex, optionalEndIndex) { 73 var endIndex = optionalEndIndex === undefined ? testRanges.length : optionalEndIndex; 74 if (startIndex < 0 || startIndex >= testRanges.length) 75 throw "Sanity check: Specified index is invalid."; 76 if (endIndex < 0 || endIndex > testRanges.length) 77 throw "Sanity check: Specified index is invalid."; 78 79 var tests = []; 80 for (var i = startIndex; i < endIndex; i++) { 81 var endpoints = eval(testRanges[i]); 82 var range; 83 test(function() { 84 if (endpoints.length) { 85 range = ownerDocument(endpoints[0]).createRange(); 86 range.setStart(endpoints[0], endpoints[1]); 87 range.setEnd(endpoints[2], endpoints[3]); 88 } else { 89 // Empty selection 90 range = null; 91 } 92 }, "Set up range " + i + " " + testRanges[i]); 93 for (var j = 0; j < testPoints.length; j++) { 94 tests.push(["collapse() on " + testRanges[i] + " to " + testPoints[j], 95 range, testPointsCached[j], "collapse"]); 96 tests.push(["setPosition() on " + testRanges[i] + " to " + testPoints[j], 97 range, testPointsCached[j], "setPosition"]); 98 } 99 } 100 101 generate_tests(testCollapse, tests); 102 }