1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/imptests/editing/selecttest/test_collapseToStartEnd.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,121 @@ 1.4 +<!doctype html> 1.5 +<title>Selection.collapseTo(Start|End)() tests</title> 1.6 +<div id=log></div> 1.7 +<script src=/resources/testharness.js></script> 1.8 +<script src=/resources/testharnessreport.js></script> 1.9 +<script src=common.js></script> 1.10 +<script> 1.11 +"use strict"; 1.12 + 1.13 +// Also test a selection with no ranges 1.14 +testRanges.unshift("[]"); 1.15 + 1.16 +for (var i = 0; i < testRanges.length; i++) { 1.17 + test(function() { 1.18 + selection.removeAllRanges(); 1.19 + var endpoints = eval(testRanges[i]); 1.20 + if (!endpoints.length) { 1.21 + assert_throws("INVALID_STATE_ERR", function() { 1.22 + selection.collapseToStart(); 1.23 + }, "Must throw InvalidStateErr if the selection's range is null"); 1.24 + return; 1.25 + } 1.26 + 1.27 + var addedRange = ownerDocument(endpoints[0]).createRange(); 1.28 + addedRange.setStart(endpoints[0], endpoints[1]); 1.29 + addedRange.setEnd(endpoints[2], endpoints[3]); 1.30 + selection.addRange(addedRange); 1.31 + 1.32 + // We don't penalize browsers here for mishandling addRange() and 1.33 + // adding a different range than we specified. They fail addRange() 1.34 + // tests for that, and don't have to fail collapseToStart/End() tests 1.35 + // too. They do fail if they throw unexpectedly, though. I also fail 1.36 + // them if there's no range at all, because otherwise they could pass 1.37 + // all tests if addRange() always does nothing and collapseToStart() 1.38 + // always throws. 1.39 + assert_equals(selection.rangeCount, 1, 1.40 + "Sanity check: rangeCount must equal 1 after addRange()"); 1.41 + 1.42 + var expectedEndpoint = [ 1.43 + selection.getRangeAt(0).startContainer, 1.44 + selection.getRangeAt(0).startOffset 1.45 + ]; 1.46 + 1.47 + selection.collapseToStart(); 1.48 + 1.49 + assert_equals(selection.rangeCount, 1, 1.50 + "selection.rangeCount must equal 1"); 1.51 + assert_equals(selection.focusNode, expectedEndpoint[0], 1.52 + "focusNode must equal the original start node"); 1.53 + assert_equals(selection.focusOffset, expectedEndpoint[1], 1.54 + "focusOffset must equal the original start offset"); 1.55 + assert_equals(selection.anchorNode, expectedEndpoint[0], 1.56 + "anchorNode must equal the original start node"); 1.57 + assert_equals(selection.anchorOffset, expectedEndpoint[1], 1.58 + "anchorOffset must equal the original start offset"); 1.59 + assert_equals(addedRange.startContainer, endpoints[0], 1.60 + "collapseToStart() must not change the startContainer of the selection's original range"); 1.61 + assert_equals(addedRange.startOffset, endpoints[1], 1.62 + "collapseToStart() must not change the startOffset of the selection's original range"); 1.63 + assert_equals(addedRange.endContainer, endpoints[2], 1.64 + "collapseToStart() must not change the endContainer of the selection's original range"); 1.65 + assert_equals(addedRange.endOffset, endpoints[3], 1.66 + "collapseToStart() must not change the endOffset of the selection's original range"); 1.67 + }, "Range " + i + " " + testRanges[i] + " collapseToStart()"); 1.68 + 1.69 + // Copy-paste of above 1.70 + test(function() { 1.71 + selection.removeAllRanges(); 1.72 + var endpoints = eval(testRanges[i]); 1.73 + if (!endpoints.length) { 1.74 + assert_throws("INVALID_STATE_ERR", function() { 1.75 + selection.collapseToEnd(); 1.76 + }, "Must throw InvalidStateErr if the selection's range is null"); 1.77 + return; 1.78 + } 1.79 + 1.80 + var addedRange = ownerDocument(endpoints[0]).createRange(); 1.81 + addedRange.setStart(endpoints[0], endpoints[1]); 1.82 + addedRange.setEnd(endpoints[2], endpoints[3]); 1.83 + selection.addRange(addedRange); 1.84 + 1.85 + // We don't penalize browsers here for mishandling addRange() and 1.86 + // adding a different range than we specified. They fail addRange() 1.87 + // tests for that, and don't have to fail collapseToStart/End() tests 1.88 + // too. They do fail if they throw unexpectedly, though. I also fail 1.89 + // them if there's no range at all, because otherwise they could pass 1.90 + // all tests if addRange() always does nothing and collapseToStart() 1.91 + // always throws. 1.92 + assert_equals(selection.rangeCount, 1, 1.93 + "Sanity check: rangeCount must equal 1 after addRange()"); 1.94 + 1.95 + var expectedEndpoint = [ 1.96 + selection.getRangeAt(0).endContainer, 1.97 + selection.getRangeAt(0).endOffset 1.98 + ]; 1.99 + 1.100 + selection.collapseToEnd(); 1.101 + 1.102 + assert_equals(selection.rangeCount, 1, 1.103 + "selection.rangeCount must equal 1"); 1.104 + assert_equals(selection.focusNode, expectedEndpoint[0], 1.105 + "focusNode must equal the original end node"); 1.106 + assert_equals(selection.focusOffset, expectedEndpoint[1], 1.107 + "focusOffset must equal the original end offset"); 1.108 + assert_equals(selection.anchorNode, expectedEndpoint[0], 1.109 + "anchorNode must equal the original end node"); 1.110 + assert_equals(selection.anchorOffset, expectedEndpoint[1], 1.111 + "anchorOffset must equal the original end offset"); 1.112 + assert_equals(addedRange.startContainer, endpoints[0], 1.113 + "collapseToEnd() must not change the startContainer of the selection's original range"); 1.114 + assert_equals(addedRange.startOffset, endpoints[1], 1.115 + "collapseToEnd() must not change the startOffset of the selection's original range"); 1.116 + assert_equals(addedRange.endContainer, endpoints[2], 1.117 + "collapseToEnd() must not change the endContainer of the selection's original range"); 1.118 + assert_equals(addedRange.endOffset, endpoints[3], 1.119 + "collapseToEnd() must not change the endOffset of the selection's original range"); 1.120 + }, "Range " + i + " " + testRanges[i] + " collapseToEnd()"); 1.121 +} 1.122 + 1.123 +testDiv.style.display = "none"; 1.124 +</script>