content/base/test/test_range_bounds.html

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=421640
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for Bug 396392</title>
michael@0 8 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
michael@0 9 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 10 <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
michael@0 11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 12 </head>
michael@0 13 <body>
michael@0 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=396392">Mozilla Bug Range getClientRects and getBoundingClientRect</a>
michael@0 15 <div id="content" style="font-family:monospace;font-size:12px;width:100px">
michael@0 16 <p>000000<span>0</span></p><div>00000<span>0</span></div><p>0000<span>0000</span>0000</p><div><span>000000000000 00000000000000 000000</span></div><div>000000000000 00000000000003 100305</div>
michael@0 17 </div>
michael@0 18 <div id="mixeddir" style="font-family:monospace;font-size:12px;width:100px"><span>english <bdo id="bdo" dir="rtl">rtl-overide english</bdo> word</span></div>
michael@0 19 <div id="mixeddir2" style="font-family:monospace;font-size:12px"><span>english <bdo id="bdo2" dir="rtl">rtl-override english</bdo> word</span></div>
michael@0 20 <pre id="test">
michael@0 21 <script class="testbody" type="text/javascript">
michael@0 22
michael@0 23 var isLTR=true;
michael@0 24 function isEmptyRect(rect, name) {
michael@0 25 name = (isLTR ? 'isLTR ' : 'isRTL ') + name;
michael@0 26 is(rect.left, 0, name+'empty rect should have left = 0');
michael@0 27 is(rect.right, 0, name+'empty rect should have right = 0');
michael@0 28 is(rect.top, 0, name+'empty rect should have top = 0');
michael@0 29 is(rect.bottom, 0, name+'empty rect should have bottom = 0');
michael@0 30 is(rect.width, 0, name+'empty rect should have width = 0');
michael@0 31 is(rect.height, 0, name+'empty rect should have height = 0');
michael@0 32 }
michael@0 33
michael@0 34 function isEmptyRectList(rectlist, name) {
michael@0 35 name = (isLTR ? 'isLTR ' : 'isRTL ') + name;
michael@0 36 is(rectlist.length, 0, name + 'empty rectlist should have zero rects');
michael@0 37 }
michael@0 38
michael@0 39 // round coordinates to the nearest 1/256 of a pixel
michael@0 40 function roundCoord(x) {
michael@0 41 return Math.round(x * 256) / 256;
michael@0 42 }
michael@0 43
michael@0 44 function _getRect(r) {
michael@0 45 if (r.length) //array
michael@0 46 return "{left:"+roundCoord(r[0])+",right:"+roundCoord(r[1])+
michael@0 47 ",top:" +roundCoord(r[2])+",bottom:"+roundCoord(r[3])+
michael@0 48 ",width:"+roundCoord(r[4])+",height:"+roundCoord(r[5])+"}";
michael@0 49 else
michael@0 50 return "{left:"+roundCoord(r.left)+",right:"+roundCoord(r.right)+
michael@0 51 ",top:"+roundCoord(r.top)+",bottom:"+roundCoord(r.bottom)+
michael@0 52 ",width:"+roundCoord(r.width)+",height:"+roundCoord(r.height)+"}";
michael@0 53 }
michael@0 54
michael@0 55 function runATest(obj) {
michael@0 56 var range = document.createRange();
michael@0 57 try {
michael@0 58 range.setStart(obj.range[0],obj.range[1]);
michael@0 59 if (obj.range.length>2) {
michael@0 60 range.setEnd(obj.range[2]||obj.range[0], obj.range[3]);
michael@0 61 }
michael@0 62 //test getBoundingClientRect()
michael@0 63 var rect = range.getBoundingClientRect();
michael@0 64 var testname = (isLTR ? 'isLTR ' : 'isRTL ') +
michael@0 65 'range.getBoundingClientRect for ' + obj.name;
michael@0 66 if (obj.rect) {
michael@0 67 is(_getRect(rect),_getRect(obj.rect), testname);
michael@0 68 } else {
michael@0 69 isEmptyRect(rect,testname+": ");
michael@0 70 }
michael@0 71 //test getClientRects()
michael@0 72 var rectlist = range.getClientRects();
michael@0 73 testname = (isLTR ? 'isLTR ' : 'isRTL ') +
michael@0 74 'range.getClientRects for '+obj.name;
michael@0 75 if (!obj.rectList) {
michael@0 76 //rectList is not specified, use obj.rect to figure out rectList
michael@0 77 obj.rectList = obj.rect?[obj.rect]:[];
michael@0 78 }
michael@0 79 if (!obj.rectList.length) {
michael@0 80 isEmptyRectList(rectlist, testname+": ");
michael@0 81 } else {
michael@0 82 is(rectlist.length, obj.rectList.length, testname+' should return '+obj.rectList.length+' rects.');
michael@0 83 if(!obj.rectList.forEach){
michael@0 84 //convert RectList to a real array
michael@0 85 obj.rectList=Array.prototype.slice.call(obj.rectList, 0);
michael@0 86 }
michael@0 87 obj.rectList.forEach(function(rect,i) {
michael@0 88 is(_getRect(rectlist[i]),_getRect(rect),testname+": item at "+i);
michael@0 89 });
michael@0 90 }
michael@0 91 } finally {
michael@0 92 range.detach();
michael@0 93 }
michael@0 94 }
michael@0 95 /** Test for Bug 396392 **/
michael@0 96 function doTest(){
michael@0 97 var root = document.getElementById('content');
michael@0 98 var firstP = root.firstElementChild, spanInFirstP = firstP.childNodes[1],
michael@0 99 firstDiv = root.childNodes[2], spanInFirstDiv = firstDiv.childNodes[1],
michael@0 100 secondP = root.childNodes[3], spanInSecondP = secondP.childNodes[1],
michael@0 101 secondDiv = root.childNodes[4], spanInSecondDiv = secondDiv.firstChild,
michael@0 102 thirdDiv = root.childNodes[5];
michael@0 103 var firstPRect = firstP.getBoundingClientRect(),
michael@0 104 spanInFirstPRect = spanInFirstP.getBoundingClientRect(),
michael@0 105 firstDivRect = firstDiv.getBoundingClientRect(),
michael@0 106 spanInFirstDivRect = spanInFirstDiv.getBoundingClientRect(),
michael@0 107 secondPRect = secondP.getBoundingClientRect(),
michael@0 108 secondDivRect = secondDiv.getBoundingClientRect(),
michael@0 109 spanInSecondPRect = spanInSecondP.getBoundingClientRect(),
michael@0 110 spanInSecondDivRect = spanInSecondDiv.getBoundingClientRect(),
michael@0 111 spanInSecondDivRectList = spanInSecondDiv.getClientRects();
michael@0 112 var widthPerchar = spanInSecondPRect.width / spanInSecondP.firstChild.length;
michael@0 113 var testcases = [
michael@0 114 {name:'nodesNotInDocument', range:[document.createTextNode('abc'), 1],
michael@0 115 rect:null},
michael@0 116 {name:'collapsedInBlockNode', range:[firstP, 2], rect:null},
michael@0 117 {name:'collapsedAtBeginningOfTextNode', range:[firstP.firstChild, 0],
michael@0 118 rect:[spanInFirstPRect.left - 6 * widthPerchar,
michael@0 119 spanInFirstPRect.left - 6 * widthPerchar, spanInFirstPRect.top,
michael@0 120 spanInFirstPRect.bottom, 0, spanInFirstPRect.height]},
michael@0 121 {name:'collapsedWithinTextNode', range:[firstP.firstChild, 1],
michael@0 122 rect:[spanInFirstPRect.left - 5 * widthPerchar,
michael@0 123 spanInFirstPRect.left - 5 * widthPerchar,
michael@0 124 spanInFirstPRect.top, spanInFirstPRect.bottom, 0, spanInFirstPRect.height]},
michael@0 125 {name:'collapsedAtEndOfTextNode', range:[firstP.firstChild, 6],
michael@0 126 rect:[spanInFirstPRect.left, spanInFirstPRect.left,
michael@0 127 spanInFirstPRect.top, spanInFirstPRect.bottom, 0, spanInFirstPRect.height]},
michael@0 128 {name:'singleBlockNode', range:[root, 1, root, 2], rect:firstPRect},
michael@0 129 {name:'twoBlockNodes', range:[root, 1, root, 3],
michael@0 130 rect:[firstPRect.left, firstPRect.right, firstPRect.top,
michael@0 131 firstDivRect.bottom, firstPRect.width,
michael@0 132 firstDivRect.bottom - firstPRect.top],
michael@0 133 rectList:[firstPRect, firstDivRect]},
michael@0 134 {name:'endOfTextNodeToEndOfAnotherTextNodeInAnotherBlock',
michael@0 135 range:[spanInFirstP.firstChild, 1, firstDiv.firstChild, 5],
michael@0 136 rect:[spanInFirstDivRect.left - 5*widthPerchar, spanInFirstDivRect.left,
michael@0 137 spanInFirstDivRect.top, spanInFirstDivRect.bottom, 5 * widthPerchar,
michael@0 138 spanInFirstDivRect.height]},
michael@0 139 {name:'startOfTextNodeToStartOfAnotherTextNodeInAnotherBlock',
michael@0 140 range:[spanInFirstP.firstChild, 0, firstDiv.firstChild, 0],
michael@0 141 rect:[spanInFirstPRect.left, spanInFirstPRect.left + widthPerchar, spanInFirstPRect.top,
michael@0 142 spanInFirstPRect.bottom, widthPerchar, spanInFirstPRect.height]},
michael@0 143 {name:'endPortionOfATextNode', range:[firstP.firstChild, 3,
michael@0 144 firstP.firstChild, 6],
michael@0 145 rect:[spanInFirstPRect.left - 3*widthPerchar, spanInFirstPRect.left,
michael@0 146 spanInFirstPRect.top, spanInFirstPRect.bottom, 3*widthPerchar, spanInFirstPRect.height]},
michael@0 147 {name:'startPortionOfATextNode', range:[firstP.firstChild, 0,
michael@0 148 firstP.firstChild, 3],
michael@0 149 rect:[spanInFirstPRect.left - 6*widthPerchar,
michael@0 150 spanInFirstPRect.left - 3*widthPerchar, spanInFirstPRect.top,
michael@0 151 spanInFirstPRect.bottom, 3 * widthPerchar, spanInFirstPRect.height]},
michael@0 152 {name:'spanTextNodes', range:[secondP.firstChild, 1, secondP.lastChild, 1],
michael@0 153 rect:[spanInSecondPRect.left - 3*widthPerchar, spanInSecondPRect.right +
michael@0 154 widthPerchar, spanInSecondPRect.top, spanInSecondPRect.bottom,
michael@0 155 spanInSecondPRect.width + 4*widthPerchar, spanInSecondPRect.height],
michael@0 156 rectList:[[spanInSecondPRect.left - 3*widthPerchar, spanInSecondPRect.left,
michael@0 157 spanInSecondPRect.top, spanInSecondPRect.bottom, 3 * widthPerchar,
michael@0 158 spanInSecondPRect.height],
michael@0 159 spanInSecondPRect,
michael@0 160 [spanInSecondPRect.right, spanInSecondPRect.right + widthPerchar,
michael@0 161 spanInSecondPRect.top, spanInSecondPRect.bottom, widthPerchar,
michael@0 162 spanInSecondPRect.height]]}
michael@0 163 ];
michael@0 164 testcases.forEach(runATest);
michael@0 165
michael@0 166 // testcases that have different ranges in LTR and RTL
michael@0 167 var directionDependentTestcases;
michael@0 168 if (isLTR) {
michael@0 169 directionDependentTestcases = [
michael@0 170 {name:'spanAcrossLines',range:[spanInSecondDiv.firstChild, 1, spanInSecondDiv.firstChild, 30],
michael@0 171 rect: spanInSecondDivRect,
michael@0 172 rectList:[[spanInSecondDivRectList[0].left+widthPerchar,
michael@0 173 spanInSecondDivRectList[0].right, spanInSecondDivRectList[0].top,
michael@0 174 spanInSecondDivRectList[0].bottom, spanInSecondDivRectList[0].width - widthPerchar,
michael@0 175 spanInSecondDivRectList[0].height],
michael@0 176 spanInSecondDivRectList[1],
michael@0 177 [spanInSecondDivRectList[2].left,
michael@0 178 spanInSecondDivRectList[2].right - 4 * widthPerchar, spanInSecondDivRectList[2].top,
michael@0 179 spanInSecondDivRectList[2].bottom,
michael@0 180 spanInSecondDivRectList[2].width - 4 * widthPerchar,
michael@0 181 spanInSecondDivRectList[2].height]]},
michael@0 182 {name:'textAcrossLines',range:[thirdDiv.firstChild, 13, thirdDiv.firstChild, 28],
michael@0 183 rect: [spanInSecondDivRectList[1].left, spanInSecondDivRectList[1].right,
michael@0 184 spanInSecondDivRectList[1].top + secondDivRect.height,
michael@0 185 spanInSecondDivRectList[1].bottom + secondDivRect.height,
michael@0 186 spanInSecondDivRectList[1].width, spanInSecondDivRectList[1].height]}
michael@0 187 ];
michael@0 188 } else {
michael@0 189 directionDependentTestcases = [
michael@0 190 {name:'spanAcrossLines',range:[spanInSecondDiv.firstChild, 1, spanInSecondDiv.firstChild, 30],
michael@0 191 rect: spanInSecondDivRect,
michael@0 192 rectList:[[spanInSecondDivRectList[0].left+widthPerchar,
michael@0 193 spanInSecondDivRectList[0].right, spanInSecondDivRectList[0].top,
michael@0 194 spanInSecondDivRectList[0].bottom, spanInSecondDivRectList[0].width - widthPerchar,
michael@0 195 spanInSecondDivRectList[0].height],
michael@0 196 spanInSecondDivRectList[1],
michael@0 197 spanInSecondDivRectList[2],
michael@0 198 spanInSecondDivRectList[3],
michael@0 199 [spanInSecondDivRectList[4].left,
michael@0 200 spanInSecondDivRectList[4].right - 4 * widthPerchar,
michael@0 201 spanInSecondDivRectList[4].top,
michael@0 202 spanInSecondDivRectList[4].bottom,
michael@0 203 spanInSecondDivRectList[4].width - 4 * widthPerchar,
michael@0 204 spanInSecondDivRectList[4].height]]},
michael@0 205 {name:'textAcrossLines',range:[thirdDiv.firstChild, 13, thirdDiv.firstChild, 28],
michael@0 206 rect: [spanInSecondDivRectList[2].left, spanInSecondDivRectList[2].right,
michael@0 207 spanInSecondDivRectList[2].top + secondDivRect.height,
michael@0 208 spanInSecondDivRectList[2].bottom + secondDivRect.height,
michael@0 209 spanInSecondDivRectList[2].width, spanInSecondDivRectList[2].height],
michael@0 210 rectList:[[spanInSecondDivRectList[2].left, spanInSecondDivRectList[2].right,
michael@0 211 spanInSecondDivRectList[2].top + secondDivRect.height,
michael@0 212 spanInSecondDivRectList[2].bottom + secondDivRect.height,
michael@0 213 spanInSecondDivRectList[2].width, spanInSecondDivRectList[2].height],
michael@0 214 [spanInSecondDivRectList[2].left, spanInSecondDivRectList[2].left,
michael@0 215 spanInSecondDivRectList[2].top + secondDivRect.height,
michael@0 216 spanInSecondDivRectList[2].bottom + secondDivRect.height,
michael@0 217 0, spanInSecondDivRectList[2].height]]}
michael@0 218 ];
michael@0 219 }
michael@0 220 directionDependentTestcases.forEach(runATest);
michael@0 221 }
michael@0 222 function testMixedDir(){
michael@0 223 var root = document.getElementById('mixeddir');
michael@0 224 var firstSpan = root.firstElementChild, firstSpanRect=firstSpan.getBoundingClientRect(),
michael@0 225 firstSpanRectList = firstSpan.getClientRects();
michael@0 226 runATest({name:'mixeddir',range:[firstSpan.firstChild,0,firstSpan.lastChild,firstSpan.lastChild.length],
michael@0 227 rect: firstSpanRect, rectList:firstSpanRectList});
michael@0 228
michael@0 229 root = document.getElementById('mixeddir2');
michael@0 230 firstSpan = root.firstElementChild;
michael@0 231 firstSpanRect = firstSpan.getBoundingClientRect();
michael@0 232 bdo = document.getElementById('bdo2');
michael@0 233 bdoRect=bdo.getBoundingClientRect();
michael@0 234 var widthPerChar = bdoRect.width / bdo.firstChild.length;
michael@0 235 runATest({name:'mixeddirPartial', range:[firstSpan.firstChild, 3,
michael@0 236 bdo.firstChild, 7],
michael@0 237 rect: [firstSpanRect.left + 3*widthPerChar, bdoRect.right,
michael@0 238 bdoRect.top, bdoRect.bottom,
michael@0 239 (firstSpan.firstChild.length + bdo.firstChild.length - 3) *
michael@0 240 widthPerChar,
michael@0 241 bdoRect.height],
michael@0 242 rectList:[[firstSpanRect.left + 3*widthPerChar,
michael@0 243 bdoRect.left,
michael@0 244 firstSpanRect.top, firstSpanRect.bottom,
michael@0 245 (firstSpan.firstChild.length - 3) * widthPerChar,
michael@0 246 firstSpanRect.height],
michael@0 247 [bdoRect.right - 7 * widthPerChar, bdoRect.right,
michael@0 248 bdoRect.top, bdoRect.bottom,
michael@0 249 7*widthPerChar, bdoRect.height]]});
michael@0 250 }
michael@0 251 function test(){
michael@0 252 //test ltr
michael@0 253 doTest();
michael@0 254 testMixedDir();
michael@0 255
michael@0 256 isLTR = false;
michael@0 257 var root = document.getElementById('content');
michael@0 258 root.dir = 'rtl';
michael@0 259
michael@0 260 //test rtl
michael@0 261 doTest();
michael@0 262 testMixedDir();
michael@0 263
michael@0 264 SimpleTest.finish();
michael@0 265 }
michael@0 266
michael@0 267 window.onload = function() {
michael@0 268 SimpleTest.waitForExplicitFinish();
michael@0 269 setTimeout(test, 0);
michael@0 270 };
michael@0 271
michael@0 272 </script>
michael@0 273 </pre>
michael@0 274 </body>
michael@0 275 </html>

mercurial