fix DOM layout for more than 2 nested nodes

This commit is contained in:
Christopher Chedeau
2014-04-10 09:40:57 -07:00
parent 1b79c9c215
commit d0d8cd0bcf

View File

@@ -53,18 +53,18 @@ function computeDOMLayout(node) {
var div = renderNode(body, node); var div = renderNode(body, node);
function buildLayout(parentRect, div) { function buildLayout(absoluteRect, div) {
var rect = div.getBoundingClientRect(); var rect = div.getBoundingClientRect();
var result = { var result = {
width: rect.width, width: rect.width,
height: rect.height, height: rect.height,
top: rect.top - parentRect.top, top: rect.top - absoluteRect.top,
left: rect.left - parentRect.left left: rect.left - absoluteRect.left
}; };
var children = []; var children = [];
for (var child = div.firstChild; child; child = child.nextSibling) { for (var child = div.firstChild; child; child = child.nextSibling) {
children.push(buildLayout(result, child)); children.push(buildLayout(rect, child));
} }
if (children.length) { if (children.length) {
result.children = children; result.children = children;
@@ -492,7 +492,7 @@ describe('Layout', function() {
} }
} }
function randChildren(node, chance) { function randChildren(node, chance) {
while (Math.random() < chance) { while (rng.nextFloat() < chance) {
if (!node.children) { if (!node.children) {
node.children = []; node.children = [];
} }
@@ -514,8 +514,8 @@ describe('Layout', function() {
for (var i = 0; i < 100; ++i) { for (var i = 0; i < 100; ++i) {
var node = generateRandomNode(); var node = generateRandomNode();
expect({node: node, layout: computeLayout(node)}) expect({i: i, node: node, layout: computeLayout(node)})
.toEqual({node: node, layout: computeDOMLayout(node)}); .toEqual({i: i, node: node, layout: computeDOMLayout(node)});
} }
}) })