setBaseAndExtent.html (5454B)
1 <!doctype html> 2 <title>Selection.setBaseAndExtent() tests</title> 3 <div id=log></div> 4 <script src=/resources/testharness.js></script> 5 <script src=/resources/testharnessreport.js></script> 6 <script src=common.js></script> 7 <script> 8 "use strict"; 9 10 for (var i = 0; i < testRanges.length; i++) { 11 test(function() { 12 var data = eval(testRanges[i]); 13 selection.removeAllRanges(); 14 selection.setBaseAndExtent(data[0], data[1], data[2], data[3]); 15 if (!document.contains(data[0]) || !document.contains(data[2])) { 16 assert_equals(selection.rangeCount, 0); 17 return; 18 } 19 assert_equals(selection.rangeCount, 1, 20 "selection.rangeCount must equal 1"); 21 assert_equals(selection.anchorNode, data[0], 22 "anchorNode must equal the requested anchor node"); 23 assert_equals(selection.anchorOffset, data[1], 24 "anchorOffset must equal the requested anchor offset"); 25 assert_equals(selection.focusNode, data[2], 26 "focusNode must equal the requested focus node"); 27 assert_equals(selection.focusOffset, data[3], 28 "focusOffset must equal the requested focus offset"); 29 }, "Range " + i + " " + testRanges[i] + " setBaseAndExtent()"); 30 31 test(function() { 32 var data = eval(testRanges[i]); 33 selection.removeAllRanges(); 34 selection.setBaseAndExtent(data[2], data[3], data[0], data[1]); 35 if (!document.contains(data[0]) || !document.contains(data[2])) { 36 assert_equals(selection.rangeCount, 0); 37 return; 38 } 39 assert_equals(selection.rangeCount, 1, 40 "selection.rangeCount must equal 1"); 41 assert_equals(selection.anchorNode, data[2], 42 "anchorNode must equal the requested focus node"); 43 assert_equals(selection.anchorOffset, data[3], 44 "anchorOffset must equal the requested focus offset"); 45 assert_equals(selection.focusNode, data[0], 46 "focusNode must equal the requested anchor node"); 47 assert_equals(selection.focusOffset, data[1], 48 "focusOffset must equal the requested anchor offset"); 49 }, "Reverse range " + i + " " + testRanges[i] + " setBaseAndExtent()"); 50 } 51 52 test(function() { 53 var title = document.getElementsByTagName('title')[0]; 54 try { 55 selection.setBaseAndExtent(title, 0, title, 99); 56 assert_true(false, "focus offset, must throw an IndexSizeError exception") 57 } catch (e) { 58 assert_equals(e.name, "IndexSizeError", "focus offset, got an IndexSizeError exception") 59 } 60 try { 61 selection.setBaseAndExtent(title, 99, title, 0); 62 assert_true(false, "anchor offset, must throw an IndexSizeError exception") 63 } catch (e) { 64 assert_equals(e.name, "IndexSizeError", "anchor offset, got an IndexSizeError exception") 65 } 66 }, "setBaseAndExtent() with index larger than length"); 67 68 test(function() { 69 var title = document.getElementsByTagName('title')[0]; 70 try { 71 selection.setBaseAndExtent(title, 0, title, -1); 72 assert_true(false, "focus offset, must throw an IndexSizeError exception") 73 } catch (e) { 74 assert_equals(e.name, "IndexSizeError", "focus offset, got an IndexSizeError exception") 75 } 76 try { 77 selection.setBaseAndExtent(title, -1, title, 0); 78 assert_true(false, "anchor offset, must throw an IndexSizeError exception") 79 } catch (e) { 80 assert_equals(e.name, "IndexSizeError", "anchor offset, got an IndexSizeError exception") 81 } 82 }, "setBaseAndExtent() with negative index"); 83 84 test(function() { 85 var title = document.getElementsByTagName('title')[0]; 86 try { 87 selection.setBaseAndExtent(title, 0, null, 0); 88 assert_true(false, "focus node, must throw an TypeError exception") 89 } catch (e) { 90 assert_equals(e.name, "TypeError", "focus node, got an TypeError exception") 91 } 92 try { 93 selection.setBaseAndExtent(null, 0, title, 0); 94 assert_true(false, "anchor node, must throw an TypeError exception") 95 } catch (e) { 96 assert_equals(e.name, "TypeError", "anchor node, got an TypeError exception") 97 } 98 try { 99 selection.setBaseAndExtent(null, 0, null, 0); 100 assert_true(false, "both nodes, must throw an TypeError exception") 101 } catch (e) { 102 assert_equals(e.name, "TypeError", "both nodes, got an TypeError exception") 103 } 104 }, "setBaseAndExtent() with null nodes"); 105 106 test(function() { 107 var title = document.getElementsByTagName('title')[0]; 108 try { 109 selection.setBaseAndExtent(title, 0, title); 110 assert_true(false, "focus offset, must throw an TypeError exception") 111 } catch (e) { 112 assert_equals(e.name, "TypeError", "focus offset, got an TypeError exception") 113 } 114 try { 115 selection.setBaseAndExtent(title, 0); 116 assert_true(false, "focus node, must throw an TypeError exception") 117 } catch (e) { 118 assert_equals(e.name, "TypeError", "focus node, got an TypeError exception") 119 } 120 try { 121 selection.setBaseAndExtent(title); 122 assert_true(false, "anchor offset, must throw an TypeError exception") 123 } catch (e) { 124 assert_equals(e.name, "TypeError", "anchor offset, got an TypeError exception") 125 } 126 }, "setBaseAndExtent() with too few params"); 127 128 testDiv.style.display = "none"; 129 130 </script>