From 694e181b8e863bc4079e7739bb96e078417e515b Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Thu, 12 Jun 2014 10:22:57 -0700 Subject: [PATCH] split the absolute loop to make main axis work --- src/Layout.js | 45 ++++++++++++++++++++++++++++++------ src/__tests__/Layout-test.js | 21 +++++++++++++---- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/Layout.js b/src/Layout.js index ab0533bc..9f9e6acd 100755 --- a/src/Layout.js +++ b/src/Layout.js @@ -412,14 +412,12 @@ var computeLayout = (function() { for (var/*int*/ i = 0; i < node.children.length; ++i) { var/*css_node_t**/ child = node.children[i]; + var/*bool*/ leadingPos = isPosDefined(child, leading[mainAxis]); + var/*bool*/ trailingPos = isPosDefined(child, trailing[mainAxis]); + if (getPositionType(child) === CSS_POSITION_ABSOLUTE && - isPosDefined(child, leading[mainAxis])) { - // In case the child is position absolute and has left/top being - // defined, we override the position to whatever the user said - // (and margin/border). - child.layout[pos[mainAxis]] = getPosition(child, leading[mainAxis]) + - getBorder(node, leading[mainAxis]) + - getMargin(child, leading[mainAxis]); + (leadingPos || trailingPos)) { + // see the loop afterwards } else { // If the child is position absolute (without top/left) or relative, // we put it at the current accumulated offset. @@ -461,6 +459,39 @@ var computeLayout = (function() { ); } + for (var/*int*/ i = 0; i < node.children.length; ++i) { + var/*css_node_t**/ child = node.children[i]; + + var/*bool*/ leadingPos = isPosDefined(child, leading[mainAxis]); + var/*bool*/ trailingPos = isPosDefined(child, trailing[mainAxis]); + + if (getPositionType(child) === CSS_POSITION_ABSOLUTE && + (leadingPos || trailingPos)) { + // In case the child is absolutely positionned and has a + // top/left/bottom/right being set, we override all the previously + // computed positions to set it correctly. + if (leadingPos) { + child.layout[pos[mainAxis]] = + getPosition(child, leading[mainAxis]) + + getBorder(node, leading[mainAxis]) + + getMargin(child, leading[mainAxis]); + } + if (!leadingPos && trailingPos) { + child.layout[pos[mainAxis]] = + node.layout[dim[mainAxis]] - + child.layout[dim[mainAxis]] - + getMargin(child, trailing[mainAxis]) - + getPosition(child, trailing[mainAxis]); + } + if (leadingPos && trailingPos) { + child.layout[dim[mainAxis]] = + node.layout[dim[mainAxis]] - + child.layout[pos[mainAxis]] - + getMargin(child, trailing[mainAxis]) - + getPosition(child, trailing[mainAxis]); + } + } + } // Position elements in the cross axis diff --git a/src/__tests__/Layout-test.js b/src/__tests__/Layout-test.js index 0a4b85e0..b110b6e7 100755 --- a/src/__tests__/Layout-test.js +++ b/src/__tests__/Layout-test.js @@ -939,6 +939,17 @@ describe('Layout', function() { ); }); + it('should layout with position absolute bottom', function() { + testLayout( + {style: {}, children: [ + {style: {bottom: 5, paddingTop: 5, position: 'absolute'}} + ]}, + {width: 0, height: 0, top: 0, left: 0, children: [ + {width: 0, height: 5, top: -10, left: 0} + ]} + ); + }); + xit('should layout text with alignItems: stretch', function() { testLayout( {style: {width: 80, padding: 7, alignItems: 'stretch', measure: text('loooooooooong with space')}}, @@ -997,10 +1008,10 @@ describe('Layout', function() { var node = {style: {}}; randMinMax(node, 0.5, 'width', -100, 1000); randMinMax(node, 0.5, 'height', -100, 1000); - randMinMax(node, 0.5, 'top', -10, 10); - randMinMax(node, 0.5, 'left', -10, 10); - randMinMax(node, 0.5, 'right', -10, 10); - randMinMax(node, 0.5, 'bottom', -10, 10); + randMinMax(node, 0.9, 'top', -10, 10); + randMinMax(node, 0.9, 'left', -10, 10); + randMinMax(node, 0.9, 'right', -10, 10); + randMinMax(node, 0.9, 'bottom', -10, 10); randSpacing(node, 0.5, 'margin', '', -10, 20); randSpacing(node, 0.5, 'padding', '', -10, 20); randSpacing(node, 0.5, 'border', 'Width', -4, 4); @@ -1010,7 +1021,7 @@ describe('Layout', function() { randEnum(node, 0.5, 'alignSelf', ['flex-start', 'center', 'flex-end', 'stretch']); randEnum(node, 0.5, 'flex', ['none', 1]); randEnum(node, 0.5, 'position', ['relative', 'absolute']); - randEnum(node, 0.5, 'measure', [text('small'), text('loooooooooong with space')]); +// randEnum(node, 0.5, 'measure', [text('small'), text('loooooooooong with space')]); if (node.style.measure) { // align-items: stretch on a text node makes it wrap in a different way.