Skip to content

Commit 58e1667

Browse files
joevilchesfacebook-github-bot
authored andcommitted
Fix issue where padding for box sizing is resolved against wrong reference length (facebook#46799)
Summary: X-link: facebook/yoga#1715 Content box impl had a bug where we resolved padding % against the same reference length as the dimensions. Padding should always be against containing block's width. This is also true for width, but not for height, which should be against containing block's height. This just pipes the width into our box sizing functions. Changelog: [Internal} Differential Revision: D63787577
1 parent 211534b commit 58e1667

File tree

8 files changed

+118
-58
lines changed

8 files changed

+118
-58
lines changed

packages/react-native/ReactCommon/yoga/yoga/algorithm/AbsoluteLayout.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,10 @@ void layoutAbsoluteChild(
326326
if (child->hasDefiniteLength(Dimension::Width, containingBlockWidth)) {
327327
childWidth = child
328328
->getResolvedDimension(
329-
direction, Dimension::Width, containingBlockWidth)
329+
direction,
330+
Dimension::Width,
331+
containingBlockWidth,
332+
containingBlockWidth)
330333
.unwrap() +
331334
marginRow;
332335
} else {
@@ -362,7 +365,10 @@ void layoutAbsoluteChild(
362365
if (child->hasDefiniteLength(Dimension::Height, containingBlockHeight)) {
363366
childHeight = child
364367
->getResolvedDimension(
365-
direction, Dimension::Height, containingBlockHeight)
368+
direction,
369+
Dimension::Height,
370+
containingBlockHeight,
371+
containingBlockWidth)
366372
.unwrap() +
367373
marginColumn;
368374
} else {

packages/react-native/ReactCommon/yoga/yoga/algorithm/BoundAxis.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,21 @@ inline FloatOptional boundAxisWithinMinAndMax(
3232
const Direction direction,
3333
const FlexDirection axis,
3434
const FloatOptional value,
35-
const float axisSize) {
35+
const float axisSize,
36+
const float widthSize) {
3637
FloatOptional min;
3738
FloatOptional max;
3839

3940
if (isColumn(axis)) {
4041
min = node->style().resolvedMinDimension(
41-
direction, Dimension::Height, axisSize);
42+
direction, Dimension::Height, axisSize, widthSize);
4243
max = node->style().resolvedMaxDimension(
43-
direction, Dimension::Height, axisSize);
44+
direction, Dimension::Height, axisSize, widthSize);
4445
} else if (isRow(axis)) {
4546
min = node->style().resolvedMinDimension(
46-
direction, Dimension::Width, axisSize);
47+
direction, Dimension::Width, axisSize, widthSize);
4748
max = node->style().resolvedMaxDimension(
48-
direction, Dimension::Width, axisSize);
49+
direction, Dimension::Width, axisSize, widthSize);
4950
}
5051

5152
if (max >= FloatOptional{0} && value > max) {
@@ -70,7 +71,7 @@ inline float boundAxis(
7071
const float widthSize) {
7172
return yoga::maxOrDefined(
7273
boundAxisWithinMinAndMax(
73-
node, direction, axis, FloatOptional{value}, axisSize)
74+
node, direction, axis, FloatOptional{value}, axisSize, widthSize)
7475
.unwrap(),
7576
paddingAndBorderForAxis(node, axis, direction, widthSize));
7677
}

packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp

Lines changed: 84 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ static void constrainMaxSizeForMode(
4444
float ownerWidth,
4545
/*in_out*/ SizingMode* mode,
4646
/*in_out*/ float* size) {
47-
const FloatOptional maxSize = node->style().resolvedMaxDimension(
48-
direction, dimension(axis), ownerAxisSize) +
47+
const FloatOptional maxSize =
48+
node->style().resolvedMaxDimension(
49+
direction, dimension(axis), ownerAxisSize, ownerWidth) +
4950
FloatOptional(node->style().computeMarginForAxis(axis, ownerWidth));
5051
switch (*mode) {
5152
case SizingMode::StretchFit:
@@ -87,8 +88,8 @@ static void computeFlexBasisForChild(
8788
SizingMode childWidthSizingMode;
8889
SizingMode childHeightSizingMode;
8990

90-
const FloatOptional resolvedFlexBasis =
91-
child->resolveFlexBasis(direction, mainAxis, mainAxisOwnerSize);
91+
const FloatOptional resolvedFlexBasis = child->resolveFlexBasis(
92+
direction, mainAxis, mainAxisOwnerSize, ownerWidth);
9293
const bool isRowStyleDimDefined =
9394
child->hasDefiniteLength(Dimension::Width, ownerWidth);
9495
const bool isColumnStyleDimDefined =
@@ -111,15 +112,17 @@ static void computeFlexBasisForChild(
111112
child, FlexDirection::Row, direction, ownerWidth));
112113

113114
child->setLayoutComputedFlexBasis(yoga::maxOrDefined(
114-
child->getResolvedDimension(direction, Dimension::Width, ownerWidth),
115+
child->getResolvedDimension(
116+
direction, Dimension::Width, ownerWidth, ownerWidth),
115117
paddingAndBorder));
116118
} else if (!isMainAxisRow && isColumnStyleDimDefined) {
117119
// The height is definite, so use that as the flex basis.
118120
const FloatOptional paddingAndBorder =
119121
FloatOptional(paddingAndBorderForAxis(
120122
child, FlexDirection::Column, direction, ownerWidth));
121123
child->setLayoutComputedFlexBasis(yoga::maxOrDefined(
122-
child->getResolvedDimension(direction, Dimension::Height, ownerHeight),
124+
child->getResolvedDimension(
125+
direction, Dimension::Height, ownerHeight, ownerWidth),
123126
paddingAndBorder));
124127
} else {
125128
// Compute the flex basis and hypothetical main size (i.e. the clamped flex
@@ -133,15 +136,18 @@ static void computeFlexBasisForChild(
133136
child->style().computeMarginForAxis(FlexDirection::Column, ownerWidth);
134137

135138
if (isRowStyleDimDefined) {
136-
childWidth =
137-
child->getResolvedDimension(direction, Dimension::Width, ownerWidth)
138-
.unwrap() +
139+
childWidth = child
140+
->getResolvedDimension(
141+
direction, Dimension::Width, ownerWidth, ownerWidth)
142+
.unwrap() +
139143
marginRow;
140144
childWidthSizingMode = SizingMode::StretchFit;
141145
}
142146
if (isColumnStyleDimDefined) {
143147
childHeight =
144-
child->getResolvedDimension(direction, Dimension::Height, ownerHeight)
148+
child
149+
->getResolvedDimension(
150+
direction, Dimension::Height, ownerHeight, ownerWidth)
145151
.unwrap() +
146152
marginColumn;
147153
childHeightSizingMode = SizingMode::StretchFit;
@@ -476,21 +482,24 @@ static float calculateAvailableInnerDimension(
476482
const Dimension dimension,
477483
const float availableDim,
478484
const float paddingAndBorder,
479-
const float ownerDim) {
485+
const float ownerDim,
486+
const float ownerWidth) {
480487
float availableInnerDim = availableDim - paddingAndBorder;
481488
// Max dimension overrides predefined dimension value; Min dimension in turn
482489
// overrides both of the above
483490
if (yoga::isDefined(availableInnerDim)) {
484491
// We want to make sure our available height does not violate min and max
485492
// constraints
486493
const FloatOptional minDimensionOptional =
487-
node->style().resolvedMinDimension(direction, dimension, ownerDim);
494+
node->style().resolvedMinDimension(
495+
direction, dimension, ownerDim, ownerWidth);
488496
const float minInnerDim = minDimensionOptional.isUndefined()
489497
? 0.0f
490498
: minDimensionOptional.unwrap() - paddingAndBorder;
491499

492500
const FloatOptional maxDimensionOptional =
493-
node->style().resolvedMaxDimension(direction, dimension, ownerDim);
501+
node->style().resolvedMaxDimension(
502+
direction, dimension, ownerDim, ownerWidth);
494503

495504
const float maxInnerDim = maxDimensionOptional.isUndefined()
496505
? FLT_MAX
@@ -594,6 +603,7 @@ static float distributeFreeSpaceSecondPass(
594603
const FlexDirection mainAxis,
595604
const FlexDirection crossAxis,
596605
const Direction direction,
606+
const float ownerWidth,
597607
const float mainAxisOwnerSize,
598608
const float availableInnerMainDim,
599609
const float availableInnerCrossDim,
@@ -618,7 +628,8 @@ static float distributeFreeSpaceSecondPass(
618628
direction,
619629
mainAxis,
620630
currentLineChild->getLayout().computedFlexBasis,
621-
mainAxisOwnerSize)
631+
mainAxisOwnerSize,
632+
ownerWidth)
622633
.unwrap();
623634
float updatedMainSize = childFlexBasis;
624635

@@ -713,11 +724,13 @@ static float distributeFreeSpaceSecondPass(
713724
? SizingMode::MaxContent
714725
: SizingMode::FitContent;
715726
} else {
716-
childCrossSize =
717-
currentLineChild
718-
->getResolvedDimension(
719-
direction, dimension(crossAxis), availableInnerCrossDim)
720-
.unwrap() +
727+
childCrossSize = currentLineChild
728+
->getResolvedDimension(
729+
direction,
730+
dimension(crossAxis),
731+
availableInnerCrossDim,
732+
availableInnerWidth)
733+
.unwrap() +
721734
marginCross;
722735
const bool isLoosePercentageMeasurement =
723736
currentLineChild->getProcessedDimension(dimension(crossAxis))
@@ -808,6 +821,7 @@ static void distributeFreeSpaceFirstPass(
808821
FlexLine& flexLine,
809822
const Direction direction,
810823
const FlexDirection mainAxis,
824+
const float ownerWidth,
811825
const float mainAxisOwnerSize,
812826
const float availableInnerMainDim,
813827
const float availableInnerWidth) {
@@ -823,7 +837,8 @@ static void distributeFreeSpaceFirstPass(
823837
direction,
824838
mainAxis,
825839
currentLineChild->getLayout().computedFlexBasis,
826-
mainAxisOwnerSize)
840+
mainAxisOwnerSize,
841+
ownerWidth)
827842
.unwrap();
828843

829844
if (flexLine.layout.remainingFreeSpace < 0) {
@@ -917,6 +932,7 @@ static void resolveFlexibleLength(
917932
const FlexDirection mainAxis,
918933
const FlexDirection crossAxis,
919934
const Direction direction,
935+
const float ownerWidth,
920936
const float mainAxisOwnerSize,
921937
const float availableInnerMainDim,
922938
const float availableInnerCrossDim,
@@ -934,6 +950,7 @@ static void resolveFlexibleLength(
934950
flexLine,
935951
direction,
936952
mainAxis,
953+
ownerWidth,
937954
mainAxisOwnerSize,
938955
availableInnerMainDim,
939956
availableInnerWidth);
@@ -945,6 +962,7 @@ static void resolveFlexibleLength(
945962
mainAxis,
946963
crossAxis,
947964
direction,
965+
ownerWidth,
948966
mainAxisOwnerSize,
949967
availableInnerMainDim,
950968
availableInnerCrossDim,
@@ -993,7 +1011,7 @@ static void justifyMainAxis(
9931011
if (style.minDimension(dimension(mainAxis)).isDefined() &&
9941012
style
9951013
.resolvedMinDimension(
996-
direction, dimension(mainAxis), mainAxisOwnerSize)
1014+
direction, dimension(mainAxis), mainAxisOwnerSize, ownerWidth)
9971015
.isDefined()) {
9981016
// This condition makes sure that if the size of main dimension(after
9991017
// considering child nodes main dim, leading and trailing padding etc)
@@ -1005,7 +1023,7 @@ static void justifyMainAxis(
10051023
const float minAvailableMainDim =
10061024
style
10071025
.resolvedMinDimension(
1008-
direction, dimension(mainAxis), mainAxisOwnerSize)
1026+
direction, dimension(mainAxis), mainAxisOwnerSize, ownerWidth)
10091027
.unwrap() -
10101028
leadingPaddingAndBorderMain - trailingPaddingAndBorderMain;
10111029
const float occupiedSpaceByChildNodes =
@@ -1403,14 +1421,16 @@ static void calculateLayoutImpl(
14031421
Dimension::Width,
14041422
availableWidth - marginAxisRow,
14051423
paddingAndBorderAxisRow,
1424+
ownerWidth,
14061425
ownerWidth);
14071426
float availableInnerHeight = calculateAvailableInnerDimension(
14081427
node,
14091428
direction,
14101429
Dimension::Height,
14111430
availableHeight - marginAxisColumn,
14121431
paddingAndBorderAxisColumn,
1413-
ownerHeight);
1432+
ownerHeight,
1433+
ownerWidth);
14141434

14151435
float availableInnerMainDim =
14161436
isMainAxisRow ? availableInnerWidth : availableInnerHeight;
@@ -1470,6 +1490,7 @@ static void calculateLayoutImpl(
14701490
auto flexLine = calculateFlexLine(
14711491
node,
14721492
ownerDirection,
1493+
ownerWidth,
14731494
mainAxisOwnerSize,
14741495
availableInnerWidth,
14751496
availableInnerMainDim,
@@ -1494,19 +1515,27 @@ static void calculateLayoutImpl(
14941515
if (sizingModeMainDim != SizingMode::StretchFit) {
14951516
const auto& style = node->style();
14961517
const float minInnerWidth =
1497-
style.resolvedMinDimension(direction, Dimension::Width, ownerWidth)
1518+
style
1519+
.resolvedMinDimension(
1520+
direction, Dimension::Width, ownerWidth, ownerWidth)
14981521
.unwrap() -
14991522
paddingAndBorderAxisRow;
15001523
const float maxInnerWidth =
1501-
style.resolvedMaxDimension(direction, Dimension::Width, ownerWidth)
1524+
style
1525+
.resolvedMaxDimension(
1526+
direction, Dimension::Width, ownerWidth, ownerWidth)
15021527
.unwrap() -
15031528
paddingAndBorderAxisRow;
15041529
const float minInnerHeight =
1505-
style.resolvedMinDimension(direction, Dimension::Height, ownerHeight)
1530+
style
1531+
.resolvedMinDimension(
1532+
direction, Dimension::Height, ownerHeight, ownerWidth)
15061533
.unwrap() -
15071534
paddingAndBorderAxisColumn;
15081535
const float maxInnerHeight =
1509-
style.resolvedMaxDimension(direction, Dimension::Height, ownerHeight)
1536+
style
1537+
.resolvedMaxDimension(
1538+
direction, Dimension::Height, ownerHeight, ownerWidth)
15101539
.unwrap() -
15111540
paddingAndBorderAxisColumn;
15121541

@@ -1559,6 +1588,7 @@ static void calculateLayoutImpl(
15591588
mainAxis,
15601589
crossAxis,
15611590
direction,
1591+
ownerWidth,
15621592
mainAxisOwnerSize,
15631593
availableInnerMainDim,
15641594
availableInnerCrossDim,
@@ -1802,7 +1832,10 @@ static void calculateLayoutImpl(
18021832
? availableInnerCrossDim + paddingAndBorderAxisCross
18031833
: node->hasDefiniteLength(dimension(crossAxis), crossAxisOwnerSize)
18041834
? node->getResolvedDimension(
1805-
direction, dimension(crossAxis), crossAxisOwnerSize)
1835+
direction,
1836+
dimension(crossAxis),
1837+
crossAxisOwnerSize,
1838+
ownerWidth)
18061839
.unwrap()
18071840
: totalLineCrossDim + paddingAndBorderAxisCross;
18081841

@@ -2058,7 +2091,8 @@ static void calculateLayoutImpl(
20582091
direction,
20592092
mainAxis,
20602093
FloatOptional{maxLineMainDim},
2061-
mainAxisOwnerSize)
2094+
mainAxisOwnerSize,
2095+
ownerWidth)
20622096
.unwrap()),
20632097
paddingAndBorderAxisMain),
20642098
dimension(mainAxis));
@@ -2092,7 +2126,8 @@ static void calculateLayoutImpl(
20922126
crossAxis,
20932127
FloatOptional{
20942128
totalLineCrossDim + paddingAndBorderAxisCross},
2095-
crossAxisOwnerSize)
2129+
crossAxisOwnerSize,
2130+
ownerWidth)
20962131
.unwrap()),
20972132
paddingAndBorderAxisCross),
20982133
dimension(crossAxis));
@@ -2384,13 +2419,20 @@ void calculateLayout(
23842419
if (node->hasDefiniteLength(Dimension::Width, ownerWidth)) {
23852420
width =
23862421
(node->getResolvedDimension(
2387-
direction, dimension(FlexDirection::Row), ownerWidth)
2422+
direction,
2423+
dimension(FlexDirection::Row),
2424+
ownerWidth,
2425+
ownerWidth)
23882426
.unwrap() +
23892427
node->style().computeMarginForAxis(FlexDirection::Row, ownerWidth));
23902428
widthSizingMode = SizingMode::StretchFit;
2391-
} else if (style.resolvedMaxDimension(direction, Dimension::Width, ownerWidth)
2429+
} else if (style
2430+
.resolvedMaxDimension(
2431+
direction, Dimension::Width, ownerWidth, ownerWidth)
23922432
.isDefined()) {
2393-
width = style.resolvedMaxDimension(direction, Dimension::Width, ownerWidth)
2433+
width = style
2434+
.resolvedMaxDimension(
2435+
direction, Dimension::Width, ownerWidth, ownerWidth)
23942436
.unwrap();
23952437
widthSizingMode = SizingMode::FitContent;
23962438
} else {
@@ -2404,17 +2446,21 @@ void calculateLayout(
24042446
if (node->hasDefiniteLength(Dimension::Height, ownerHeight)) {
24052447
height =
24062448
(node->getResolvedDimension(
2407-
direction, dimension(FlexDirection::Column), ownerHeight)
2449+
direction,
2450+
dimension(FlexDirection::Column),
2451+
ownerHeight,
2452+
ownerWidth)
24082453
.unwrap() +
24092454
node->style().computeMarginForAxis(FlexDirection::Column, ownerWidth));
24102455
heightSizingMode = SizingMode::StretchFit;
24112456
} else if (style
24122457
.resolvedMaxDimension(
2413-
direction, Dimension::Height, ownerHeight)
2458+
direction, Dimension::Height, ownerHeight, ownerWidth)
24142459
.isDefined()) {
2415-
height =
2416-
style.resolvedMaxDimension(direction, Dimension::Height, ownerHeight)
2417-
.unwrap();
2460+
height = style
2461+
.resolvedMaxDimension(
2462+
direction, Dimension::Height, ownerHeight, ownerWidth)
2463+
.unwrap();
24182464
heightSizingMode = SizingMode::FitContent;
24192465
} else {
24202466
height = ownerHeight;

0 commit comments

Comments
 (0)