From 9313d3d11e03f1c2e39f8c8dc0dfd6ab2c71a022 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Tue, 15 Apr 2014 16:39:42 -0700 Subject: [PATCH] fix edge case of flex: 1 --- spec/LayoutSpec.js | 16 ++++++++++++++-- src/Layout.js | 8 ++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/spec/LayoutSpec.js b/spec/LayoutSpec.js index 2b34cfab..774c60c3 100755 --- a/spec/LayoutSpec.js +++ b/spec/LayoutSpec.js @@ -436,8 +436,8 @@ describe('Layout', function() { {style: {height: 100}} ]} ]}, - {width: 20, height: 120, top: 0, left: 0, children: [{ - width: 20, height: 120, top: 0, left: 0, children: [ + {width: 20, height: 120, top: 0, left: 0, children: [ + {width: 20, height: 120, top: 0, left: 0, children: [ {width: 0, height: 0, top: 10, left: 10}, {width: 0, height: 100, top: 20, left: 20} ]} @@ -445,6 +445,17 @@ describe('Layout', function() { ); }); + it('should layout flex inside of an empty element', function() { + testLayout( + {style: {}, children: [ + {style: {flex: 1}}, + ]}, + {width: 0, height: 0, top: 0, left: 0, children: [ + {width: 0, height: 0, top: 0, left: 0} + ]} + ); + }); + it('should layout randomly', function() { function RNG(seed) { this.state = seed; @@ -487,6 +498,7 @@ describe('Layout', function() { randEnum(node, 0.1, 'justifyContent', ['flex-start', 'center', 'flex-end', 'space-between', 'space-around']); randEnum(node, 0.1, 'alignItems', ['flex-start', 'center', 'flex-end']); randEnum(node, 0.1, 'alignSelf', ['flex-start', 'center', 'flex-end']); + randEnum(node, 0.1, 'flex', ['none', 1]); randChildren(node, 0.2); return node; } diff --git a/src/Layout.js b/src/Layout.js index 23707a10..578f6041 100755 --- a/src/Layout.js +++ b/src/Layout.js @@ -66,6 +66,10 @@ function computeLayout(node) { return 'column'; } + function getFlex(node) { + return node.style.flex === 1; + } + var axis = { left: 'horizontal', right: 'horizontal', @@ -109,7 +113,7 @@ function computeLayout(node) { var mainContentDim = 0; var flexibleChildrenCount = 0; children.forEach(function(child) { - if (!child.style.flex) { + if (node.layout[dim[mainAxis]] === undefined || !getFlex(child)) { layoutNode(child); mainContentDim += child.layout[dim[mainAxis]] + getMargin(leading[mainAxis], child) + @@ -126,7 +130,7 @@ function computeLayout(node) { if (flexibleChildrenCount) { var flexibleMainDim = remainingMainDim / flexibleChildrenCount; children.forEach(function(child) { - if (child.style.flex) { + if (getFlex(child)) { child.layout[dim[mainAxis]] = flexibleMainDim; layoutNode(child); }