2009-09-24 21:01:48 +00:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<!--
|
|
|
|
https://bugzilla.mozilla.org/show_bug.cgi?id=421640
|
|
|
|
-->
|
|
|
|
<head>
|
|
|
|
<title>Test for Bug 396392</title>
|
2009-12-24 10:29:27 +00:00
|
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
2009-09-24 21:01:48 +00:00
|
|
|
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
|
|
|
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
|
|
|
|
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=396392">Mozilla Bug Range getClientRects and getBoundingClientRect</a>
|
2010-01-03 08:46:48 +00:00
|
|
|
<div id="content" style="font-family:monospace;font-size:12px;width:100px">
|
2010-01-03 08:46:05 +00:00
|
|
|
<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>
|
2009-09-24 21:01:48 +00:00
|
|
|
</div>
|
2010-01-03 08:46:48 +00:00
|
|
|
<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>
|
|
|
|
<div id="mixeddir2" style="font-family:monospace;font-size:12px"><span>english <bdo id="bdo2" dir="rtl">rtl-override english</bdo> word</span></div>
|
2009-09-24 21:01:48 +00:00
|
|
|
<pre id="test">
|
|
|
|
<script class="testbody" type="text/javascript">
|
|
|
|
|
2009-12-24 10:29:27 +00:00
|
|
|
var isLTR=true;
|
2009-09-24 21:01:48 +00:00
|
|
|
function isEmptyRect(rect, name) {
|
2009-12-24 10:29:27 +00:00
|
|
|
name = (isLTR ? 'isLTR ' : 'isRTL ') + name;
|
2009-09-24 21:01:48 +00:00
|
|
|
is(rect.left, 0, name+'empty rect should have left = 0');
|
|
|
|
is(rect.right, 0, name+'empty rect should have right = 0');
|
|
|
|
is(rect.top, 0, name+'empty rect should have top = 0');
|
|
|
|
is(rect.bottom, 0, name+'empty rect should have bottom = 0');
|
|
|
|
is(rect.width, 0, name+'empty rect should have width = 0');
|
|
|
|
is(rect.height, 0, name+'empty rect should have height = 0');
|
|
|
|
}
|
|
|
|
|
|
|
|
function isEmptyRectList(rectlist, name) {
|
2009-12-24 10:29:27 +00:00
|
|
|
name = (isLTR ? 'isLTR ' : 'isRTL ') + name;
|
2009-09-24 21:01:48 +00:00
|
|
|
is(rectlist.length, 0, name + 'empty rectlist should have zero rects');
|
|
|
|
}
|
|
|
|
|
2010-01-03 08:46:05 +00:00
|
|
|
// round coordinates to the nearest 1/256 of a pixel
|
|
|
|
function roundCoord(x) {
|
|
|
|
return Math.round(x * 256) / 256;
|
|
|
|
}
|
|
|
|
|
2009-09-24 21:01:48 +00:00
|
|
|
function _getRect(r) {
|
|
|
|
if (r.length) //array
|
2010-01-03 08:46:05 +00:00
|
|
|
return "{left:"+roundCoord(r[0])+",right:"+roundCoord(r[1])+
|
|
|
|
",top:" +roundCoord(r[2])+",bottom:"+roundCoord(r[3])+
|
|
|
|
",width:"+roundCoord(r[4])+",height:"+roundCoord(r[5])+"}";
|
2009-09-24 21:01:48 +00:00
|
|
|
else
|
2010-01-03 08:46:05 +00:00
|
|
|
return "{left:"+roundCoord(r.left)+",right:"+roundCoord(r.right)+
|
|
|
|
",top:"+roundCoord(r.top)+",bottom:"+roundCoord(r.bottom)+
|
|
|
|
",width:"+roundCoord(r.width)+",height:"+roundCoord(r.height)+"}";
|
2009-09-24 21:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function runATest(obj) {
|
|
|
|
var range = document.createRange();
|
|
|
|
try {
|
|
|
|
range.setStart(obj.range[0],obj.range[1]);
|
|
|
|
if (obj.range.length>2) {
|
|
|
|
range.setEnd(obj.range[2]||obj.range[0], obj.range[3]);
|
|
|
|
}
|
|
|
|
//test getBoundingClientRect()
|
|
|
|
var rect = range.getBoundingClientRect();
|
2009-12-24 10:29:27 +00:00
|
|
|
var testname = (isLTR ? 'isLTR ' : 'isRTL ') +
|
|
|
|
'range.getBoundingClientRect for ' + obj.name;
|
2009-09-24 21:01:48 +00:00
|
|
|
if (obj.rect) {
|
|
|
|
is(_getRect(rect),_getRect(obj.rect), testname);
|
|
|
|
} else {
|
|
|
|
isEmptyRect(rect,testname+": ");
|
|
|
|
}
|
|
|
|
//test getClientRects()
|
|
|
|
var rectlist = range.getClientRects();
|
2009-12-24 10:29:27 +00:00
|
|
|
testname = (isLTR ? 'isLTR ' : 'isRTL ') +
|
|
|
|
'range.getClientRects for '+obj.name;
|
2009-09-24 21:01:48 +00:00
|
|
|
if (!obj.rectList) {
|
|
|
|
//rectList is not specified, use obj.rect to figure out rectList
|
|
|
|
obj.rectList = obj.rect?[obj.rect]:[];
|
|
|
|
}
|
|
|
|
if (!obj.rectList.length) {
|
|
|
|
isEmptyRectList(rectlist, testname+": ");
|
|
|
|
} else {
|
|
|
|
is(rectlist.length, obj.rectList.length, testname+' should return '+obj.rectList.length+' rects.');
|
2009-12-24 10:29:27 +00:00
|
|
|
if(!obj.rectList.forEach){
|
|
|
|
//convert RectList to a real array
|
|
|
|
obj.rectList=Array.prototype.slice.call(obj.rectList, 0);
|
|
|
|
}
|
2009-09-24 21:01:48 +00:00
|
|
|
obj.rectList.forEach(function(rect,i) {
|
|
|
|
is(_getRect(rectlist[i]),_getRect(rect),testname+": item at "+i);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
range.detach();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/** Test for Bug 396392 **/
|
2009-12-24 10:29:27 +00:00
|
|
|
function doTest(){
|
2009-09-24 21:01:48 +00:00
|
|
|
var root = document.getElementById('content');
|
2009-09-25 05:18:21 +00:00
|
|
|
var firstP = root.firstElementChild, spanInFirstP = firstP.childNodes[1],
|
|
|
|
firstDiv = root.childNodes[2], spanInFirstDiv = firstDiv.childNodes[1],
|
2010-01-03 08:46:05 +00:00
|
|
|
secondP = root.childNodes[3], spanInSecondP = secondP.childNodes[1],
|
|
|
|
secondDiv = root.childNodes[4], spanInSecondDiv = secondDiv.firstChild,
|
|
|
|
thirdDiv = root.childNodes[5];
|
2009-09-25 05:18:21 +00:00
|
|
|
var firstPRect = firstP.getBoundingClientRect(),
|
|
|
|
spanInFirstPRect = spanInFirstP.getBoundingClientRect(),
|
2009-09-24 21:01:48 +00:00
|
|
|
firstDivRect = firstDiv.getBoundingClientRect(),
|
2009-09-25 05:18:21 +00:00
|
|
|
spanInFirstDivRect = spanInFirstDiv.getBoundingClientRect(),
|
2009-09-24 21:01:48 +00:00
|
|
|
secondPRect = secondP.getBoundingClientRect(),
|
2010-01-03 08:46:05 +00:00
|
|
|
secondDivRect = secondDiv.getBoundingClientRect(),
|
|
|
|
spanInSecondPRect = spanInSecondP.getBoundingClientRect(),
|
|
|
|
spanInSecondDivRect = spanInSecondDiv.getBoundingClientRect(),
|
|
|
|
spanInSecondDivRectList = spanInSecondDiv.getClientRects();
|
2009-09-24 21:01:48 +00:00
|
|
|
var widthPerchar = spanInSecondPRect.width / spanInSecondP.firstChild.length;
|
|
|
|
var testcases = [
|
|
|
|
{name:'nodesNotInDocument', range:[document.createTextNode('abc'), 1],
|
|
|
|
rect:null},
|
2009-09-25 05:18:21 +00:00
|
|
|
{name:'collapsedInBlockNode', range:[firstP, 2], rect:null},
|
2009-09-24 21:01:48 +00:00
|
|
|
{name:'collapsedAtBeginningOfTextNode', range:[firstP.firstChild, 0],
|
2009-12-24 10:29:27 +00:00
|
|
|
rect:[spanInFirstPRect.left - 6 * widthPerchar,
|
|
|
|
spanInFirstPRect.left - 6 * widthPerchar, spanInFirstPRect.top,
|
2009-09-25 05:18:21 +00:00
|
|
|
spanInFirstPRect.bottom, 0, spanInFirstPRect.height]},
|
2009-09-24 21:01:48 +00:00
|
|
|
{name:'collapsedWithinTextNode', range:[firstP.firstChild, 1],
|
2009-12-24 10:29:27 +00:00
|
|
|
rect:[spanInFirstPRect.left - 5 * widthPerchar,
|
|
|
|
spanInFirstPRect.left - 5 * widthPerchar,
|
2009-09-25 05:18:21 +00:00
|
|
|
spanInFirstPRect.top, spanInFirstPRect.bottom, 0, spanInFirstPRect.height]},
|
2009-09-24 21:01:48 +00:00
|
|
|
{name:'collapsedAtEndOfTextNode', range:[firstP.firstChild, 6],
|
2009-12-24 10:29:27 +00:00
|
|
|
rect:[spanInFirstPRect.left, spanInFirstPRect.left,
|
2009-09-25 05:18:21 +00:00
|
|
|
spanInFirstPRect.top, spanInFirstPRect.bottom, 0, spanInFirstPRect.height]},
|
2009-09-24 21:01:48 +00:00
|
|
|
{name:'singleBlockNode', range:[root, 1, root, 2], rect:firstPRect},
|
|
|
|
{name:'twoBlockNodes', range:[root, 1, root, 3],
|
|
|
|
rect:[firstPRect.left, firstPRect.right, firstPRect.top,
|
|
|
|
firstDivRect.bottom, firstPRect.width,
|
|
|
|
firstDivRect.bottom - firstPRect.top],
|
|
|
|
rectList:[firstPRect, firstDivRect]},
|
|
|
|
{name:'endOfTextNodeToEndOfAnotherTextNodeInAnotherBlock',
|
2009-09-25 05:18:21 +00:00
|
|
|
range:[spanInFirstP.firstChild, 1, firstDiv.firstChild, 5],
|
2009-12-24 10:29:27 +00:00
|
|
|
rect:[spanInFirstDivRect.left - 5*widthPerchar, spanInFirstDivRect.left,
|
2009-09-25 05:18:21 +00:00
|
|
|
spanInFirstDivRect.top, spanInFirstDivRect.bottom, 5 * widthPerchar,
|
|
|
|
spanInFirstDivRect.height]},
|
2009-09-24 21:01:48 +00:00
|
|
|
{name:'startOfTextNodeToStartOfAnotherTextNodeInAnotherBlock',
|
2009-09-25 05:18:21 +00:00
|
|
|
range:[spanInFirstP.firstChild, 0, firstDiv.firstChild, 0],
|
|
|
|
rect:[spanInFirstPRect.left, spanInFirstPRect.left + widthPerchar, spanInFirstPRect.top,
|
|
|
|
spanInFirstPRect.bottom, widthPerchar, spanInFirstPRect.height]},
|
2009-09-24 21:01:48 +00:00
|
|
|
{name:'endPortionOfATextNode', range:[firstP.firstChild, 3,
|
|
|
|
firstP.firstChild, 6],
|
2009-12-24 10:29:27 +00:00
|
|
|
rect:[spanInFirstPRect.left - 3*widthPerchar, spanInFirstPRect.left,
|
2009-09-25 05:18:21 +00:00
|
|
|
spanInFirstPRect.top, spanInFirstPRect.bottom, 3*widthPerchar, spanInFirstPRect.height]},
|
2009-09-24 21:01:48 +00:00
|
|
|
{name:'startPortionOfATextNode', range:[firstP.firstChild, 0,
|
|
|
|
firstP.firstChild, 3],
|
2009-12-24 10:29:27 +00:00
|
|
|
rect:[spanInFirstPRect.left - 6*widthPerchar,
|
|
|
|
spanInFirstPRect.left - 3*widthPerchar, spanInFirstPRect.top,
|
2009-09-25 05:18:21 +00:00
|
|
|
spanInFirstPRect.bottom, 3 * widthPerchar, spanInFirstPRect.height]},
|
2009-09-24 21:01:48 +00:00
|
|
|
{name:'spanTextNodes', range:[secondP.firstChild, 1, secondP.lastChild, 1],
|
2009-12-24 10:29:27 +00:00
|
|
|
rect:[spanInSecondPRect.left - 3*widthPerchar, spanInSecondPRect.right +
|
2009-09-24 21:01:48 +00:00
|
|
|
widthPerchar, spanInSecondPRect.top, spanInSecondPRect.bottom,
|
2009-09-25 05:18:21 +00:00
|
|
|
spanInSecondPRect.width + 4*widthPerchar, spanInSecondPRect.height],
|
2009-12-24 10:29:27 +00:00
|
|
|
rectList:[[spanInSecondPRect.left - 3*widthPerchar, spanInSecondPRect.left,
|
2009-09-24 21:01:48 +00:00
|
|
|
spanInSecondPRect.top, spanInSecondPRect.bottom, 3 * widthPerchar,
|
|
|
|
spanInSecondPRect.height],
|
|
|
|
spanInSecondPRect,
|
|
|
|
[spanInSecondPRect.right, spanInSecondPRect.right + widthPerchar,
|
|
|
|
spanInSecondPRect.top, spanInSecondPRect.bottom, widthPerchar,
|
2010-01-03 08:46:38 +00:00
|
|
|
spanInSecondPRect.height]]}
|
|
|
|
];
|
|
|
|
testcases.forEach(runATest);
|
|
|
|
|
|
|
|
// testcases that have different ranges in LTR and RTL
|
|
|
|
var directionDependentTestcases;
|
|
|
|
if (isLTR) {
|
|
|
|
directionDependentTestcases = [
|
|
|
|
{name:'spanAcrossLines',range:[spanInSecondDiv.firstChild, 1, spanInSecondDiv.firstChild, 30],
|
|
|
|
rect: spanInSecondDivRect,
|
|
|
|
rectList:[[spanInSecondDivRectList[0].left+widthPerchar,
|
2010-01-03 08:46:05 +00:00
|
|
|
spanInSecondDivRectList[0].right, spanInSecondDivRectList[0].top,
|
|
|
|
spanInSecondDivRectList[0].bottom, spanInSecondDivRectList[0].width - widthPerchar,
|
|
|
|
spanInSecondDivRectList[0].height],
|
|
|
|
spanInSecondDivRectList[1],
|
|
|
|
[spanInSecondDivRectList[2].left,
|
|
|
|
spanInSecondDivRectList[2].right - 4 * widthPerchar, spanInSecondDivRectList[2].top,
|
|
|
|
spanInSecondDivRectList[2].bottom,
|
|
|
|
spanInSecondDivRectList[2].width - 4 * widthPerchar,
|
|
|
|
spanInSecondDivRectList[2].height]]},
|
2010-01-03 08:46:38 +00:00
|
|
|
{name:'textAcrossLines',range:[thirdDiv.firstChild, 13, thirdDiv.firstChild, 28],
|
|
|
|
rect: [spanInSecondDivRectList[1].left, spanInSecondDivRectList[1].right,
|
|
|
|
spanInSecondDivRectList[1].top + secondDivRect.height,
|
|
|
|
spanInSecondDivRectList[1].bottom + secondDivRect.height,
|
|
|
|
spanInSecondDivRectList[1].width, spanInSecondDivRectList[1].height]}
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
directionDependentTestcases = [
|
|
|
|
{name:'spanAcrossLines',range:[spanInSecondDiv.firstChild, 1, spanInSecondDiv.firstChild, 30],
|
|
|
|
rect: spanInSecondDivRect,
|
|
|
|
rectList:[[spanInSecondDivRectList[0].left+widthPerchar,
|
|
|
|
spanInSecondDivRectList[0].right, spanInSecondDivRectList[0].top,
|
|
|
|
spanInSecondDivRectList[0].bottom, spanInSecondDivRectList[0].width - widthPerchar,
|
|
|
|
spanInSecondDivRectList[0].height],
|
|
|
|
spanInSecondDivRectList[1],
|
|
|
|
spanInSecondDivRectList[2],
|
|
|
|
spanInSecondDivRectList[3],
|
|
|
|
[spanInSecondDivRectList[4].left,
|
|
|
|
spanInSecondDivRectList[4].right - 4 * widthPerchar,
|
|
|
|
spanInSecondDivRectList[4].top,
|
|
|
|
spanInSecondDivRectList[4].bottom,
|
|
|
|
spanInSecondDivRectList[4].width - 4 * widthPerchar,
|
|
|
|
spanInSecondDivRectList[4].height]]},
|
|
|
|
{name:'textAcrossLines',range:[thirdDiv.firstChild, 13, thirdDiv.firstChild, 28],
|
|
|
|
rect: [spanInSecondDivRectList[2].left, spanInSecondDivRectList[2].right,
|
|
|
|
spanInSecondDivRectList[2].top + secondDivRect.height,
|
|
|
|
spanInSecondDivRectList[2].bottom + secondDivRect.height,
|
|
|
|
spanInSecondDivRectList[2].width, spanInSecondDivRectList[2].height],
|
|
|
|
rectList:[[spanInSecondDivRectList[2].left, spanInSecondDivRectList[2].right,
|
|
|
|
spanInSecondDivRectList[2].top + secondDivRect.height,
|
|
|
|
spanInSecondDivRectList[2].bottom + secondDivRect.height,
|
|
|
|
spanInSecondDivRectList[2].width, spanInSecondDivRectList[2].height],
|
|
|
|
[spanInSecondDivRectList[2].left, spanInSecondDivRectList[2].left,
|
|
|
|
spanInSecondDivRectList[2].top + secondDivRect.height,
|
|
|
|
spanInSecondDivRectList[2].bottom + secondDivRect.height,
|
|
|
|
0, spanInSecondDivRectList[2].height]]}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
directionDependentTestcases.forEach(runATest);
|
2009-12-24 10:29:27 +00:00
|
|
|
}
|
|
|
|
function testMixedDir(){
|
|
|
|
var root = document.getElementById('mixeddir');
|
|
|
|
var firstSpan = root.firstElementChild, firstSpanRect=firstSpan.getBoundingClientRect(),
|
|
|
|
firstSpanRectList = firstSpan.getClientRects();
|
|
|
|
runATest({name:'mixeddir',range:[firstSpan.firstChild,0,firstSpan.lastChild,firstSpan.lastChild.length],
|
|
|
|
rect: firstSpanRect, rectList:firstSpanRectList});
|
2010-01-03 08:46:48 +00:00
|
|
|
|
|
|
|
root = document.getElementById('mixeddir2');
|
|
|
|
firstSpan = root.firstElementChild;
|
|
|
|
firstSpanRect = firstSpan.getBoundingClientRect();
|
|
|
|
bdo = document.getElementById('bdo2');
|
|
|
|
bdoRect=bdo.getBoundingClientRect();
|
|
|
|
var widthPerChar = bdoRect.width / bdo.firstChild.length;
|
|
|
|
runATest({name:'mixeddirPartial', range:[firstSpan.firstChild, 3,
|
|
|
|
bdo.firstChild, 7],
|
|
|
|
rect: [firstSpanRect.left + 3*widthPerChar, bdoRect.right,
|
|
|
|
bdoRect.top, bdoRect.bottom,
|
|
|
|
(firstSpan.firstChild.length + bdo.firstChild.length - 3) *
|
|
|
|
widthPerChar,
|
|
|
|
bdoRect.height],
|
|
|
|
rectList:[[firstSpanRect.left + 3*widthPerChar,
|
|
|
|
bdoRect.left,
|
|
|
|
firstSpanRect.top, firstSpanRect.bottom,
|
|
|
|
(firstSpan.firstChild.length - 3) * widthPerChar,
|
|
|
|
firstSpanRect.height],
|
|
|
|
[bdoRect.right - 7 * widthPerChar, bdoRect.right,
|
|
|
|
bdoRect.top, bdoRect.bottom,
|
|
|
|
7*widthPerChar, bdoRect.height]]});
|
2009-12-24 10:29:27 +00:00
|
|
|
}
|
|
|
|
function test(){
|
|
|
|
//test ltr
|
|
|
|
doTest();
|
|
|
|
testMixedDir();
|
|
|
|
|
|
|
|
isLTR = false;
|
|
|
|
var root = document.getElementById('content');
|
|
|
|
root.dir = 'rtl';
|
|
|
|
|
|
|
|
//test rtl
|
|
|
|
doTest();
|
|
|
|
testMixedDir();
|
2009-09-24 21:01:48 +00:00
|
|
|
|
|
|
|
SimpleTest.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
window.onload = function() {
|
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
setTimeout(test, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|