From 756babcdb70341e311a8d2007b54f0af1acb21e7 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 21 Apr 2014 18:34:28 -0700 Subject: [PATCH] position: absolute, top and left --- src/Layout.js | 10 +++++++++- src/__tests__/Layout-test.js | 17 +++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Layout.js b/src/Layout.js index 8ebcabdc..fd59daeb 100755 --- a/src/Layout.js +++ b/src/Layout.js @@ -102,6 +102,10 @@ var computeLayout = (function() { return !isUndefined(node.style[dim[axis]]); } + function isPosDefined(node, pos) { + return !isUndefined(node.style[pos]); + } + function getPosition(node, pos) { if (pos in node.style) { @@ -226,7 +230,11 @@ var computeLayout = (function() { var/*float*/ mainPos = getPadding(node, leading[mainAxis]) + leadingMainDim; for (var/*int*/ i = 0; i < node.children.length; ++i) { var/*css_node_t**/ child = node.children[i]; - child.layout[pos[mainAxis]] += mainPos; + if (getPositionType(child) === 'absolute' && isPosDefined(child, leading[mainAxis])) { + child.layout[pos[mainAxis]] = getPosition(child, leading[mainAxis]); + } else { + child.layout[pos[mainAxis]] += mainPos; + } if (getPositionType(child) === 'relative') { mainPos += getDimWithMargin(child, mainAxis) + betweenMainDim; diff --git a/src/__tests__/Layout-test.js b/src/__tests__/Layout-test.js index e1621496..49944954 100755 --- a/src/__tests__/Layout-test.js +++ b/src/__tests__/Layout-test.js @@ -535,7 +535,7 @@ describe('Layout', function() { ); }); - it('should layout node with specified width', function() { + it('should layout node with padding and child with position absolute', function() { testLayout( {style: {padding: 5}, children: [ {style: {position: 'absolute'}} @@ -546,6 +546,19 @@ describe('Layout', function() { ); }); + it('should layout node with position absolute, top and left', function() { + testLayout( + {style: {}, children: [ + {style: {height: 100}}, + {style: {position: 'absolute', top: 10, left: 10}} + ]}, + {width: 0, height: 100, top: 0, left: 0, children: [ + {width: 0, height: 100, top: 0, left: 0}, + {width: 0, height: 0, top: 10, left: 10} + ]} + ); + }); + it('should layout randomly', function() { function RNG(seed) { this.state = seed; @@ -559,7 +572,7 @@ describe('Layout', function() { var rng = new RNG(0); function randMinMax(node, chance, attribute, min, max) { if (rng.nextFloat() < chance) { - if (attribute === 'left' || attribute === 'top' || attribute === 'right' || attribute === 'bottom') { + if (attribute === 'right' || attribute === 'bottom') { return; } node.style[attribute] = Math.floor(rng.nextFloat() * (max - min)) + min;