test_selection.html (5987B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Text Range selection tests</title> 5 <link rel="stylesheet" type="text/css" 6 href="chrome://mochikit/content/tests/SimpleTest/test.css" /> 7 8 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 9 <script type="application/javascript" 10 src="../common.js"></script> 11 <script type="application/javascript" 12 src="../text.js"></script> 13 <script type="application/javascript" 14 src="../layout.js"></script> 15 <script type="application/javascript"> 16 17 function doTest() { 18 var sel = window.getSelection(); 19 var p = getNode("p1"); 20 var a = getNode("p2_a"); 21 22 var range = document.createRange(); 23 sel.addRange(range); 24 25 // the accessible is contained by the range 26 range.selectNode(p); 27 28 var a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges; 29 var a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange); 30 31 testTextRange(a11yrange, "selection range #1", document, 3, document, 4); 32 33 ok(a11yrange.crop(getAccessible(a)), "Range failed to crop #1."); 34 testTextRange(a11yrange, "cropped range #1", a, 0, a, 5); 35 36 // the range is contained by the accessible 37 range.selectNode(a); 38 a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges; 39 a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange); 40 41 testTextRange(a11yrange, "selection range #2", p, 5, p, 6); 42 43 ok(a11yrange.crop(getAccessible(p)), "Range failed to crop #2."); 44 testTextRange(a11yrange, "cropped range #2", p, 5, p, 6); 45 46 // the range starts before the accessible and ends inside it 47 range.setStart(p, 0); 48 range.setEndAfter(a.firstChild, 4); 49 a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges; 50 a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange); 51 52 testTextRange(a11yrange, "selection range #3", p, 0, a, 4); 53 54 ok(a11yrange.crop(getAccessible(a)), "Range failed to crop #3."); 55 testTextRange(a11yrange, "cropped range #3", a, 0, a, 4); 56 57 // the range starts inside the accessible and ends after it 58 range.setStart(a.firstChild, 1); 59 range.setEndAfter(p); 60 a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges; 61 a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange); 62 63 testTextRange(a11yrange, "selection range #4", a, 1, document, 4); 64 65 ok(a11yrange.crop(getAccessible(a)), "Range failed to crop #4."); 66 testTextRange(a11yrange, "cropped range #4", a, 1, a, 5); 67 68 // the range ends before the accessible 69 range.setStart(p.firstChild, 0); 70 range.setEnd(p.firstChild, 4); 71 a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges; 72 a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange); 73 74 testTextRange(a11yrange, "selection range #5", p, 0, p, 4); 75 ok(!a11yrange.crop(getAccessible(a)), "Crop #5 succeeded while it shouldn't"); 76 77 // the range starts after the accessible 78 range.setStart(p.lastChild, 0); 79 range.setEnd(p.lastChild, 4); 80 a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges; 81 a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange); 82 83 testTextRange(a11yrange, "selection range #6", p, 6, p, 10); 84 85 ok(!a11yrange.crop(getAccessible(a)), "Crop #6 succeeded while it shouldn't"); 86 87 // crop a range by a table 88 range.selectNode(getNode("c2")); 89 a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges; 90 a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange); 91 92 testTextRange(a11yrange, "selection range #7", document, 4, document, 5); 93 94 ok(a11yrange.crop(getAccessible("table")), "Range failed to crop #7."); 95 testTextRange(a11yrange, "cropped range #7", "table", 0, "table", 1); 96 97 // test compare points for selection with start in nested node 98 range.setStart(a.firstChild, 2); 99 range.setEnd(p.lastChild, 3); 100 a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges; 101 a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange); 102 var res = a11yrange.compareEndPoints(EndPoint_Start, a11yrange, EndPoint_End); 103 is(res, -1, "start must be lesser than end"); 104 105 res = a11yrange.compareEndPoints(EndPoint_End, a11yrange, EndPoint_Start); 106 is(res, 1, "end must be greater than start"); 107 108 // Crop a range to its next sibling. 109 range.selectNode(getNode("c3p1").firstChild); 110 a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges; 111 a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange); 112 testTextRange(a11yrange, "selection range #8", "c3p1", 0, "c3p1", 1); 113 ok(!a11yrange.crop(getAccessible("c3p2")), "Crop #8 succeeded but shouldn't have."); 114 // Crop a range to its previous sibling. 115 range.selectNode(getNode("c3p2").firstChild); 116 a11yranges = getAccessible(document, [nsIAccessibleText]).selectionRanges; 117 a11yrange = a11yranges.queryElementAt(0, nsIAccessibleTextRange); 118 testTextRange(a11yrange, "selection range #9", "c3p2", 0, "c3p2", 1); 119 ok(!a11yrange.crop(getAccessible("c3p1")), "Crop #9 succeeded but shouldn't have."); 120 121 SimpleTest.finish(); 122 } 123 124 SimpleTest.waitForExplicitFinish(); 125 addA11yLoadEvent(doTest); 126 </script> 127 </head> 128 <body> 129 130 <a target="_blank" 131 title="Implement IAccessible2_3::selectionRanges" 132 href="https://bugzilla.mozilla.org/show_bug.cgi?id=1233118">Bug 1233118</a> 133 <p id="display"></p> 134 <div id="content" style="display: none"></div> 135 <pre id="test"> 136 </pre> 137 138 <p id="p1">text <a id="p2_a" href="www">link<img id="p2_img", src="../moz.png"></a> text</p> 139 140 <div id="c2">start<table id="table"><tr><td>cell</td></tr></table>end</div> 141 142 <div id="c3"><p id="c3p1">a</p><p id="c3p2">b</p></div> 143 </body> 144 </html>