queryCaretRectWin.html (7056B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>nsIDOMWindowUtils::sendQueryContentEvent w/QUERY_CARET_RECT test</title> 5 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"> 7 8 <!-- 9 ######################################################### 10 # WARNING: this file has Windows line endings. If you 11 # update this file please maintain them. Line endings are 12 # taken into account when calculating query caret rect 13 # values. 14 ######################################################### 15 --> 16 17 <style> 18 #text { 19 position: absolute; 20 left: 0em; 21 top: 0em; 22 font-size: 10pt; 23 font-family: monospace; 24 line-height: 20px; 25 letter-spacing: 0px; 26 margin-top:-1px; /* nix the text area border */ 27 overflow: hidden; 28 width:800px; 29 height:400px; 30 } 31 #div-hor { 32 position: absolute; 33 left: 0em; 34 border-top:1px solid lightgray; 35 width: 1000px; 36 pointer-events:none; 37 } 38 #div-ver { 39 position: absolute; 40 top: 0em; 41 border-left:1px solid lightgray; 42 height: 500px; 43 pointer-events:none; 44 } 45 </style> 46 47 <script type="application/javascript"> 48 var SimpleTest = window.opener.SimpleTest; 49 50 function ok() { window.opener.ok.apply(window.opener, arguments); } 51 function done() { window.opener.done.apply(window.opener, arguments); } 52 53 function dumpLn() { 54 for (let idx = 0; idx < arguments.length; idx++) 55 dump(arguments[idx] + " "); 56 dump("\n"); 57 } 58 59 // drawn grid is 20x20 60 function drawGrid() { 61 for (var idx = 0; idx < 20; idx++) { 62 var e = document.createElement("div"); 63 e.setAttribute("id", "div-hor"); 64 e.style.top = (idx*20) + "px"; 65 document.getElementById("container").appendChild(e); 66 } 67 for (var idx = 0; idx < 40; idx++) { 68 var e = document.createElement("div"); 69 e.setAttribute("id", "div-ver"); 70 e.style.left = (idx*20) + "px"; 71 document.getElementById("container").appendChild(e); 72 } 73 } 74 75 function getSelection(aElement) { 76 // aElement is know to be a textarea. 77 return aElement.editor.selection; 78 } 79 80 function testCaretPosition(aDomWinUtils, aOffset, aRectDims) { 81 let rect = aDomWinUtils.sendQueryContentEvent( 82 aDomWinUtils.QUERY_CARET_RECT, 83 aOffset, 0, 0, 0, 84 aDomWinUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK); 85 ok(rect, "rect returned"); 86 ok(rect.succeeded, "call succeeded"); 87 88 info("left:" + rect.left + " top:" + rect.top); 89 90 ok(rect.left > aRectDims.min.left, "rect.left > " + aRectDims.min.left); 91 ok(rect.left < aRectDims.max.left, "rect.left < " + aRectDims.max.left); 92 ok(rect.top > aRectDims.min.top, "rect.top > " + aRectDims.min.top); 93 ok(rect.top < aRectDims.max.top, "rect.top < " + aRectDims.max.top); 94 } 95 96 function doTest() { 97 let domWinUtils = window.windowUtils; 98 99 let text = document.getElementById("text"); 100 101 text.focus(); 102 103 let textrect = text.getBoundingClientRect(); 104 105 let cp = document.caretPositionFromPoint(10, 395); 106 let input = cp.offsetNode; 107 let offset = cp.offset; 108 input.selectionStart = input.selectionEnd = offset; 109 110 let selection = getSelection(text); 111 112 testCaretPosition(domWinUtils, input.selectionStart, { 113 min: { left: 10, top: 380 }, 114 max: { left: 25, top: 390 }, 115 }); 116 117 testCaretPosition(domWinUtils, input.selectionEnd, { 118 min: { left: 10, top: 380 }, 119 max: { left: 25, top: 390 }, 120 }); 121 122 testCaretPosition(domWinUtils, text.selectionStart, { 123 min: { left: 10, top: 380 }, 124 max: { left: 25, top: 390 }, 125 }); 126 127 testCaretPosition(domWinUtils, text.selectionEnd, { 128 min: { left: 10, top: 380 }, 129 max: { left: 25, top: 390 }, 130 }); 131 132 testCaretPosition(domWinUtils, selection.anchorOffset, { 133 min: { left: 10, top: 380 }, 134 max: { left: 25, top: 390 }, 135 }); 136 137 testCaretPosition(domWinUtils, selection.focusOffset, { 138 min: { left: 10, top: 380 }, 139 max: { left: 25, top: 390 }, 140 }); 141 142 cp = document.caretPositionFromPoint(395, 395); 143 input = cp.offsetNode; 144 offset = cp.offset; 145 input.selectionStart = input.selectionEnd = offset; 146 147 selection = getSelection(text); 148 149 testCaretPosition(domWinUtils, input.selectionStart, { 150 min: { left: 390, top: 380 }, 151 max: { left: 400, top: 390 }, 152 }); 153 154 testCaretPosition(domWinUtils, input.selectionEnd, { 155 min: { left: 390, top: 380 }, 156 max: { left: 400, top: 390 }, 157 }); 158 159 testCaretPosition(domWinUtils, text.selectionStart, { 160 min: { left: 390, top: 380 }, 161 max: { left: 400, top: 390 }, 162 }); 163 164 testCaretPosition(domWinUtils, text.selectionEnd, { 165 min: { left: 390, top: 380 }, 166 max: { left: 400, top: 390 }, 167 }); 168 169 testCaretPosition(domWinUtils, selection.anchorOffset, { 170 min: { left: 390, top: 380 }, 171 max: { left: 400, top: 390 }, 172 }); 173 174 testCaretPosition(domWinUtils, selection.focusOffset, { 175 min: { left: 390, top: 380 }, 176 max: { left: 400, top: 390 }, 177 }); 178 179 done(); 180 } 181 182 </script> 183 184 <body onload="doTest()"> 185 <textarea id="text" wrap="on"> 186 Alice was beginning to get very tired of sitting by her sister on the bank, 187 and of having nothing to do: once or twice she had peeped into the book 188 her sister was reading, but it had no pictures or conversations in it, 189 `and what is the use of a book,' thought Alice `without pictures or 190 conversation?' 191 192 So she was considering in her own mind (as well as she could, for the 193 hot day made her feel very sleepy and stupid), whether the pleasure of 194 making a daisy-chain would be worth the trouble of getting up and 195 picking the daisies, when suddenly a White Rabbit with pink In another 196 moment down went Alice after it, never once considering how in the world 197 she was to get out again. 198 199 The rabbit-hole went straight on like a tunnel for some way, and then 200 dipped suddenly down, so suddenly that Alice had not a moment to think 201 about stopping herself before she found herself falling down a very deep 202 well. 203 204 Either the well was very deep, or she fell very slowly, for she had 205 plenty of time as she went down to look about her and to wonder what was 206 going to happen next. First, she tried to look down and make out what 207 she was coming to, but it was too dark to see anything; then she looked 208 at the sides of the well, and noticed that they were filled with 209 cupboards and book-shelves; here and there she saw maps and pictures 210 hung upon pegs. She took down a jar from one of the shelves as she 211 passed; it was labelled `ORANGE MARMALADE', but to her great 212 disappointment it was empty: she did not like to drop the jar for fear 213 of killing somebody, so managed to put it into one of the cupboards as 214 she fell past it. 215 </textarea> 216 <div id="container"></div> 217 </body> 218 </html>