From e39f13a8ea9e6b3a81284874f75c3255fcdcec8f Mon Sep 17 00:00:00 2001 From: Emil Sjolander Date: Tue, 10 Jan 2017 08:26:50 -0800 Subject: [PATCH] Allow aspect ratio to expand beyond bounds of parent Summary: Allow aspect ratio to expand beyond bounds of parent as is generally accepted in css Reviewed By: passy Differential Revision: D4397547 fbshipit-source-id: d2b1ca7b096f2f17b3efbd8f47a50678bfe7bb5f --- tests/YGAspectRatioTest.cpp | 21 +++++++++++++++++++++ yoga/Yoga.c | 14 ++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/tests/YGAspectRatioTest.cpp b/tests/YGAspectRatioTest.cpp index b6e32de2..cc9626ad 100644 --- a/tests/YGAspectRatioTest.cpp +++ b/tests/YGAspectRatioTest.cpp @@ -676,3 +676,24 @@ TEST(YogaTest, aspect_ratio_height_overrides_align_stretch_column) { YGNodeFreeRecursive(root); } + +TEST(YogaTest, aspect_ratio_allow_child_overflow_parent_size) { + const YGNodeRef root = YGNodeNew(); + YGNodeStyleSetAlignItems(root, YGAlignFlexStart); + YGNodeStyleSetWidth(root, 100); + + const YGNodeRef root_child0 = YGNodeNew(); + YGNodeStyleSetHeight(root_child0, 50); + YGNodeStyleSetAspectRatio(root_child0, 4); + YGNodeInsertChild(root, root_child0, 0); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + ASSERT_EQ(100, YGNodeLayoutGetWidth(root)); + ASSERT_EQ(50, YGNodeLayoutGetHeight(root)); + + ASSERT_EQ(200, YGNodeLayoutGetWidth(root_child0)); + ASSERT_EQ(50, YGNodeLayoutGetHeight(root_child0)); + + YGNodeFreeRecursive(root); +} diff --git a/yoga/Yoga.c b/yoga/Yoga.c index d5e4917b..0e7bec47 100644 --- a/yoga/Yoga.c +++ b/yoga/Yoga.c @@ -2239,8 +2239,11 @@ static void YGNodelayoutImpl(const YGNodeRef node, availableInnerWidth)); childHeightMeasureMode = YGMeasureModeExactly; - childHeight = fminf(childHeight, availableInnerHeight); - childWidth = childHeight * currentRelativeChild->style.aspectRatio; + // Parent size constraint should have higher priority than flex + if (YGNodeIsFlex(currentRelativeChild)) { + childHeight = fminf(childHeight, availableInnerHeight); + childWidth = childHeight * currentRelativeChild->style.aspectRatio; + } } else { childWidth = fmaxf(childHeight * currentRelativeChild->style.aspectRatio, YGNodePaddingAndBorderForAxis(currentRelativeChild, @@ -2248,8 +2251,11 @@ static void YGNodelayoutImpl(const YGNodeRef node, availableInnerWidth)); childWidthMeasureMode = YGMeasureModeExactly; - childWidth = fminf(childWidth, availableInnerWidth); - childHeight = childWidth / currentRelativeChild->style.aspectRatio; + // Parent size constraint should have higher priority than flex + if (YGNodeIsFlex(currentRelativeChild)) { + childWidth = fminf(childWidth, availableInnerWidth); + childHeight = childWidth / currentRelativeChild->style.aspectRatio; + } } }