Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
# default on new configs to preserve the pre-fix geometry. Clear this
# bit to opt into the spec-correct single distribution.
("FlexFirstPassUsesRunningTotals", 1 << 4),
# Use min/max-clamped flex bases when distributing positive free
# space, instead of the flex bases of unfrozen items. Set by default
# on new configs to preserve the pre-fix geometry. Clear this bit to
# opt into CSS Flexbox §9.7 grow free-space accounting.
("FlexGrowFreeSpaceUsesClampedBasis", 1 << 5),
# Enable all incorrect behavior (preserve compatibility)
("All", 0x7FFFFFFF),
# Enable all errata except for "StretchFlexBasis" (Defaults behavior
Expand Down
2 changes: 2 additions & 0 deletions java/com/facebook/yoga/YogaErrata.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum class YogaErrata(public val intValue: Int) {
ABSOLUTE_PERCENT_AGAINST_INNER_SIZE(4),
MIN_SIZE_UNDEFINED_INSTEAD_OF_AUTO(8),
FLEX_FIRST_PASS_USES_RUNNING_TOTALS(16),
FLEX_GROW_FREE_SPACE_USES_CLAMPED_BASIS(32),
ALL(2147483647),
CLASSIC(2147483646);

Expand All @@ -31,6 +32,7 @@ public enum class YogaErrata(public val intValue: Int) {
4 -> ABSOLUTE_PERCENT_AGAINST_INNER_SIZE
8 -> MIN_SIZE_UNDEFINED_INSTEAD_OF_AUTO
16 -> FLEX_FIRST_PASS_USES_RUNNING_TOTALS
32 -> FLEX_GROW_FREE_SPACE_USES_CLAMPED_BASIS
2147483647 -> ALL
2147483646 -> CLASSIC
else -> throw IllegalArgumentException("Unknown enum value: $value")
Expand Down
2 changes: 2 additions & 0 deletions javascript/src/generated/YGEnums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export enum Errata {
AbsolutePercentAgainstInnerSize = 4,
MinSizeUndefinedInsteadOfAuto = 8,
FlexFirstPassUsesRunningTotals = 16,
FlexGrowFreeSpaceUsesClampedBasis = 32,
All = 2147483647,
Classic = 2147483646,
}
Expand Down Expand Up @@ -192,6 +193,7 @@ const constants = {
ERRATA_ABSOLUTE_PERCENT_AGAINST_INNER_SIZE: Errata.AbsolutePercentAgainstInnerSize,
ERRATA_MIN_SIZE_UNDEFINED_INSTEAD_OF_AUTO: Errata.MinSizeUndefinedInsteadOfAuto,
ERRATA_FLEX_FIRST_PASS_USES_RUNNING_TOTALS: Errata.FlexFirstPassUsesRunningTotals,
ERRATA_FLEX_GROW_FREE_SPACE_USES_CLAMPED_BASIS: Errata.FlexGrowFreeSpaceUsesClampedBasis,
ERRATA_ALL: Errata.All,
ERRATA_CLASSIC: Errata.Classic,
EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS: ExperimentalFeature.WebFlexBasis,
Expand Down
6 changes: 4 additions & 2 deletions tests/YGConfigTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,12 @@ TEST(YogaTest, config_errata_bitmask_add_remove_operations) {

// Default configs carry the errata that preserve legacy geometry
// (MinSizeUndefinedInsteadOfAuto for CSS §4.5 auto-min,
// FlexFirstPassUsesRunningTotals for free-space distribution). Clear them
// for this test so we can assert exact equality at the end.
// FlexFirstPassUsesRunningTotals and FlexGrowFreeSpaceUsesClampedBasis for
// free-space distribution). Clear them for this test so we can assert exact
// equality at the end.
config->removeErrata(yoga::Errata::MinSizeUndefinedInsteadOfAuto);
config->removeErrata(yoga::Errata::FlexFirstPassUsesRunningTotals);
config->removeErrata(yoga::Errata::FlexGrowFreeSpaceUsesClampedBasis);

// Initially no errata
ASSERT_FALSE(config->hasErrata(yoga::Errata::StretchFlexBasis));
Expand Down
270 changes: 270 additions & 0 deletions tests/YGFlexGrowMinSizeFreeSpaceTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <gtest/gtest.h>
#include <yoga/Yoga.h>

class YGFlexGrowMinSizeFreeSpace : public testing::Test {
protected:
YGConfigRef config = YGConfigNew();
YGNodeRef root = YGNodeNewWithConfig(config);

void SetUp() override {
YGConfigSetErrata(config, YGErrataNone);
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
YGNodeStyleSetHeight(root, 10);
}

void TearDown() override {
YGNodeFreeRecursive(root);
YGConfigFree(config);
}

YGNodeRef addChild(float grow, float basis = 0) {
YGNodeRef child = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexGrow(child, grow);
YGNodeStyleSetFlexBasis(child, basis);
YGNodeInsertChild(root, child, YGNodeGetChildCount(root));
return child;
}
};

TEST_F(YGFlexGrowMinSizeFreeSpace, min_width_below_proportional_share) {
YGNodeStyleSetWidth(root, 120);
YGNodeRef first = addChild(1);
YGNodeStyleSetMinWidth(first, 30);
YGNodeRef second = addChild(2);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(40, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(80, YGNodeLayoutGetWidth(second));
}

TEST_F(YGFlexGrowMinSizeFreeSpace, min_width_above_proportional_share) {
YGNodeStyleSetWidth(root, 300);
YGNodeRef first = addChild(1);
YGNodeStyleSetMinWidth(first, 200);
YGNodeRef second = addChild(1);
YGNodeRef third = addChild(1);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(200, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(second));
EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(third));
}

TEST_F(YGFlexGrowMinSizeFreeSpace, errata_preserves_original_sizes) {
YGConfigSetErrata(config, YGErrataFlexGrowFreeSpaceUsesClampedBasis);
YGNodeStyleSetWidth(root, 300);
YGNodeRef first = addChild(1);
YGNodeStyleSetMinWidth(first, 200);
YGNodeRef second = addChild(1);
YGNodeRef third = addChild(1);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(233, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(34, YGNodeLayoutGetWidth(second));
EXPECT_FLOAT_EQ(33, YGNodeLayoutGetWidth(third));
}

TEST_F(YGFlexGrowMinSizeFreeSpace, errata_enabled_by_default) {
YGConfigRef defaults = YGConfigNew();
const YGErrata errata = YGConfigGetErrata(defaults);
YGConfigFree(defaults);
EXPECT_NE(errata & YGErrataFlexGrowFreeSpaceUsesClampedBasis, 0);
EXPECT_NE(YGErrataAll & YGErrataFlexGrowFreeSpaceUsesClampedBasis, 0);
EXPECT_NE(YGErrataClassic & YGErrataFlexGrowFreeSpaceUsesClampedBasis, 0);
YGConfigSetErrata(config, errata);
YGNodeStyleSetWidth(root, 120);
YGNodeRef first = addChild(1);
YGNodeStyleSetMinWidth(first, 30);
YGNodeRef second = addChild(2);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(60, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(60, YGNodeLayoutGetWidth(second));

YGConfigSetErrata(
config,
static_cast<YGErrata>(
errata & ~YGErrataFlexGrowFreeSpaceUsesClampedBasis));
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(40, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(80, YGNodeLayoutGetWidth(second));
}

TEST_F(YGFlexGrowMinSizeFreeSpace, max_width_freezes_before_distribution) {
YGNodeStyleSetWidth(root, 300);
YGNodeRef first = addChild(1, 150);
YGNodeStyleSetMaxWidth(first, 50);
YGNodeRef second = addChild(1);
YGNodeStyleSetMinWidth(second, 100);
YGNodeRef third = addChild(1);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(125, YGNodeLayoutGetWidth(second));
EXPECT_FLOAT_EQ(125, YGNodeLayoutGetWidth(third));
}

TEST_F(YGFlexGrowMinSizeFreeSpace, gap_is_removed_from_free_space) {
YGNodeStyleSetWidth(root, 140);
YGNodeStyleSetGap(root, YGGutterColumn, 20);
YGNodeRef first = addChild(1);
YGNodeStyleSetMinWidth(first, 30);
YGNodeRef second = addChild(2);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(40, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(80, YGNodeLayoutGetWidth(second));
EXPECT_FLOAT_EQ(60, YGNodeLayoutGetLeft(second));
}

TEST_F(YGFlexGrowMinSizeFreeSpace, column_uses_unclamped_height) {
YGNodeStyleSetFlexDirection(root, YGFlexDirectionColumn);
YGNodeStyleSetWidth(root, 10);
YGNodeStyleSetHeight(root, 120);
YGNodeRef first = addChild(1);
YGNodeStyleSetMinHeight(first, 30);
YGNodeRef second = addChild(2);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(40, YGNodeLayoutGetHeight(first));
EXPECT_FLOAT_EQ(80, YGNodeLayoutGetHeight(second));
EXPECT_FLOAT_EQ(40, YGNodeLayoutGetTop(second));
}

TEST_F(YGFlexGrowMinSizeFreeSpace, wrapping_uses_hypothetical_sizes) {
YGNodeStyleSetWidth(root, 120);
YGNodeStyleSetHeight(root, 20);
YGNodeStyleSetFlexWrap(root, YGWrapWrap);
YGNodeRef first = addChild(1);
YGNodeStyleSetMinWidth(first, 30);
YGNodeRef second = addChild(2);
YGNodeStyleSetMinWidth(second, 30);
YGNodeRef third = addChild(1);
YGNodeStyleSetMinWidth(third, 100);
for (YGNodeRef child : {first, second, third}) {
YGNodeStyleSetHeight(child, 10);
}

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(40, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(80, YGNodeLayoutGetWidth(second));
EXPECT_FLOAT_EQ(120, YGNodeLayoutGetWidth(third));
EXPECT_FLOAT_EQ(0, YGNodeLayoutGetTop(first));
EXPECT_FLOAT_EQ(0, YGNodeLayoutGetTop(second));
EXPECT_FLOAT_EQ(10, YGNodeLayoutGetTop(third));

YGConfigSetErrata(config, YGErrataFlexGrowFreeSpaceUsesClampedBasis);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(70, YGNodeLayoutGetWidth(second));
EXPECT_FLOAT_EQ(120, YGNodeLayoutGetWidth(third));
EXPECT_FLOAT_EQ(0, YGNodeLayoutGetTop(first));
EXPECT_FLOAT_EQ(0, YGNodeLayoutGetTop(second));
EXPECT_FLOAT_EQ(10, YGNodeLayoutGetTop(third));
}

TEST_F(YGFlexGrowMinSizeFreeSpace, zero_grow_uses_hypothetical_size) {
YGNodeStyleSetWidth(root, 200);
YGNodeRef first = addChild(0);
YGNodeStyleSetMinWidth(first, 50);
YGNodeRef second = addChild(1);
YGNodeRef third = addChild(2);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(second));
EXPECT_FLOAT_EQ(100, YGNodeLayoutGetWidth(third));
}

TEST_F(YGFlexGrowMinSizeFreeSpace, nonzero_basis_and_margins) {
YGNodeStyleSetWidth(root, 200);
YGNodeRef first = addChild(1, 20);
YGNodeStyleSetMinWidth(first, 60);
YGNodeStyleSetMargin(first, YGEdgeRight, 20);
YGNodeRef second = addChild(2, 40);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(60, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(120, YGNodeLayoutGetWidth(second));
EXPECT_FLOAT_EQ(80, YGNodeLayoutGetLeft(second));
}

TEST_F(YGFlexGrowMinSizeFreeSpace, mixed_min_max_freezes_only_max_violation) {
YGNodeStyleSetWidth(root, 300);
YGNodeRef first = addChild(1);
YGNodeStyleSetMinWidth(first, 160);
YGNodeRef second = addChild(1);
YGNodeStyleSetMaxWidth(second, 100);

for (YGErrata errata :
{YGErrataNone, YGErrataFlexFirstPassUsesRunningTotals}) {
YGConfigSetErrata(config, errata);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(200, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(100, YGNodeLayoutGetWidth(second));
}
}

TEST_F(YGFlexGrowMinSizeFreeSpace, mixed_violations_cancel) {
YGNodeStyleSetWidth(root, 300);
YGNodeRef first = addChild(1);
YGNodeStyleSetMinWidth(first, 180);
YGNodeRef second = addChild(1);
YGNodeStyleSetMaxWidth(second, 120);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(180, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(120, YGNodeLayoutGetWidth(second));
}

TEST_F(YGFlexGrowMinSizeFreeSpace, fractional_grow_after_min_freezes) {
YGConfigSetPointScaleFactor(config, 0);
YGNodeStyleSetWidth(root, 200);
YGNodeRef first = addChild(0.25f);
YGNodeStyleSetMinWidth(first, 100);
YGNodeRef second = addChild(0.25f);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(100, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(50, YGNodeLayoutGetWidth(second));
}

TEST_F(YGFlexGrowMinSizeFreeSpace, successive_min_violations) {
YGNodeStyleSetWidth(root, 600);
YGNodeRef first = addChild(1);
YGNodeStyleSetMinWidth(first, 200);
YGNodeRef second = addChild(1);
YGNodeStyleSetMinWidth(second, 180);
YGNodeRef third = addChild(1);
YGNodeStyleSetMinWidth(third, 120);
YGNodeRef fourth = addChild(1);

YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

EXPECT_FLOAT_EQ(200, YGNodeLayoutGetWidth(first));
EXPECT_FLOAT_EQ(180, YGNodeLayoutGetWidth(second));
EXPECT_FLOAT_EQ(120, YGNodeLayoutGetWidth(third));
EXPECT_FLOAT_EQ(100, YGNodeLayoutGetWidth(fourth));
}
2 changes: 2 additions & 0 deletions yoga/YGEnums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ const char* YGErrataToString(const YGErrata value) {
return "min-size-undefined-instead-of-auto";
case YGErrataFlexFirstPassUsesRunningTotals:
return "flex-first-pass-uses-running-totals";
case YGErrataFlexGrowFreeSpaceUsesClampedBasis:
return "flex-grow-free-space-uses-clamped-basis";
case YGErrataAll:
return "all";
case YGErrataClassic:
Expand Down
1 change: 1 addition & 0 deletions yoga/YGEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ YG_ENUM_DECL(
YGErrataAbsolutePercentAgainstInnerSize = 4,
YGErrataMinSizeUndefinedInsteadOfAuto = 8,
YGErrataFlexFirstPassUsesRunningTotals = 16,
YGErrataFlexGrowFreeSpaceUsesClampedBasis = 32,
YGErrataAll = 2147483647,
YGErrataClassic = 2147483646)
YG_DEFINE_ENUM_FLAG_OPERATORS(YGErrata)
Expand Down
Loading