@@ -2698,7 +2698,7 @@ void RenderBox::computeLogicalWidthInFragment(LogicalExtentComputedValues& compu
26982698 if (avoidsFloats () && cb.containsFloats ())
26992699 containerLogicalWidthForAutoMargins = containingBlockAvailableLineWidthInFragment (fragment);
27002700 bool hasInvertedDirection = cb.style ().isLeftToRightDirection () != style ().isLeftToRightDirection ();
2701- computeInlineDirectionMargins (cb, containerLogicalWidthForAutoMargins, computedValues.m_extent ,
2701+ computeInlineDirectionMargins (cb, containerLogicalWidth, containerLogicalWidthForAutoMargins, computedValues.m_extent ,
27022702 hasInvertedDirection ? computedValues.m_margins .m_end : computedValues.m_margins .m_start ,
27032703 hasInvertedDirection ? computedValues.m_margins .m_start : computedValues.m_margins .m_end );
27042704 }
@@ -2944,9 +2944,8 @@ LayoutUnit RenderBox::computeOrTrimInlineMargin(const RenderBlock& containingBlo
29442944 return computeInlineMargin ();
29452945}
29462946
2947- void RenderBox::computeInlineDirectionMargins (const RenderBlock& containingBlock, LayoutUnit containerWidth, LayoutUnit childWidth, LayoutUnit& marginStart, LayoutUnit& marginEnd) const
2947+ void RenderBox::computeInlineDirectionMargins (const RenderBlock& containingBlock, LayoutUnit containerWidth, std::optional<LayoutUnit> availableSpaceAdjustedWithFloats, LayoutUnit childWidth, LayoutUnit& marginStart, LayoutUnit& marginEnd) const
29482948{
2949-
29502949 const RenderStyle& containingBlockStyle = containingBlock.style ();
29512950 Length marginStartLength = style ().marginStartUsing (&containingBlockStyle);
29522951 Length marginEndLength = style ().marginEndUsing (&containingBlockStyle);
@@ -2972,44 +2971,51 @@ void RenderBox::computeInlineDirectionMargins(const RenderBlock& containingBlock
29722971 marginEndLength = Length (0 , LengthType::Fixed);
29732972 }
29742973
2975- // Case One: The object is being centered in the containing block's available logical width.
2976- if ((marginStartLength.isAuto () && marginEndLength.isAuto () && childWidth < containerWidth)
2977- || (!marginStartLength.isAuto () && !marginEndLength.isAuto () && containingBlock.style ().textAlign () == TextAlignMode::WebKitCenter)) {
2978- // Other browsers center the margin box for align=center elements so we match them here.
2979- marginStart = computeOrTrimInlineMargin (containingBlock, MarginTrimType::InlineStart, [containerWidth, childWidth, marginStartLength, marginEndLength]() {
2980- LayoutUnit marginStartWidth = minimumValueForLength (marginStartLength, containerWidth);
2981- LayoutUnit marginEndWidth = minimumValueForLength (marginEndLength, containerWidth);
2982- LayoutUnit centeredMarginBoxStart = std::max<LayoutUnit>(0 , (containerWidth - childWidth - marginStartWidth - marginEndWidth) / 2 );
2983- return centeredMarginBoxStart + marginStartWidth;
2984- });
2985- marginEnd = computeOrTrimInlineMargin (containingBlock, MarginTrimType::InlineEnd, [containerWidth, childWidth, marginEndLength, marginStart]() {
2986- LayoutUnit marginEndWidth = minimumValueForLength (marginEndLength, containerWidth);
2987- return containerWidth - childWidth - marginStart + marginEndWidth;
2988- });
2989- return ;
2990- }
2991-
2992- // Case Two: The object is being pushed to the start of the containing block's available logical width.
2993- if (marginEndLength.isAuto () && childWidth < containerWidth) {
2994- marginStart = valueForLength (marginStartLength, containerWidth);
2995- marginEnd = containerWidth - childWidth - marginStart;
2996- return ;
2997- }
2998-
2999- // Case Three: The object is being pushed to the end of the containing block's available logical width.
3000- bool pushToEndFromTextAlign = !marginEndLength.isAuto () && ((!containingBlockStyle.isLeftToRightDirection () && containingBlockStyle.textAlign () == TextAlignMode::WebKitLeft)
3001- || (containingBlockStyle.isLeftToRightDirection () && containingBlockStyle.textAlign () == TextAlignMode::WebKitRight));
3002- if ((marginStartLength.isAuto () || pushToEndFromTextAlign) && childWidth < containerWidth) {
3003- marginEnd = computeOrTrimInlineMargin (containingBlock, MarginTrimType::InlineEnd, [containerWidth, marginEndLength]() {
3004- return valueForLength (marginEndLength, containerWidth);
3005- });
3006- marginStart = computeOrTrimInlineMargin (containingBlock, MarginTrimType::InlineStart, [containerWidth, childWidth, marginEnd]() {
3007- return containerWidth - childWidth - marginEnd;
3008- });
2974+ auto handleMarginAuto = [&] {
2975+ auto containerWidthForMarginAuto = availableSpaceAdjustedWithFloats.value_or (containerWidth);
2976+ // Case One: The object is being centered in the containing block's available logical width.
2977+ auto marginAutoCenter = marginStartLength.isAuto () && marginEndLength.isAuto () && childWidth < containerWidthForMarginAuto;
2978+ auto alignModeCenter = containingBlock.style ().textAlign () == TextAlignMode::WebKitCenter && !marginStartLength.isAuto () && !marginEndLength.isAuto ();
2979+ if (marginAutoCenter || alignModeCenter) {
2980+ // Other browsers center the margin box for align=center elements so we match them here.
2981+ marginStart = computeOrTrimInlineMargin (containingBlock, MarginTrimType::InlineStart, [containerWidthForMarginAuto, childWidth, marginStartLength, marginEndLength]() {
2982+ LayoutUnit marginStartWidth = minimumValueForLength (marginStartLength, containerWidthForMarginAuto);
2983+ LayoutUnit marginEndWidth = minimumValueForLength (marginEndLength, containerWidthForMarginAuto);
2984+ LayoutUnit centeredMarginBoxStart = std::max<LayoutUnit>(0 , (containerWidthForMarginAuto - childWidth - marginStartWidth - marginEndWidth) / 2 );
2985+ return centeredMarginBoxStart + marginStartWidth;
2986+ });
2987+ marginEnd = computeOrTrimInlineMargin (containingBlock, MarginTrimType::InlineEnd, [containerWidthForMarginAuto, childWidth, marginEndLength, marginStart]() {
2988+ LayoutUnit marginEndWidth = minimumValueForLength (marginEndLength, containerWidthForMarginAuto);
2989+ return containerWidthForMarginAuto - childWidth - marginStart + marginEndWidth;
2990+ });
2991+ return true ;
2992+ }
2993+
2994+ // Case Two: The object is being pushed to the start of the containing block's available logical width.
2995+ if (marginEndLength.isAuto () && childWidth < containerWidthForMarginAuto) {
2996+ marginStart = valueForLength (marginStartLength, containerWidthForMarginAuto);
2997+ marginEnd = containerWidthForMarginAuto - childWidth - marginStart;
2998+ return true ;
2999+ }
3000+
3001+ // Case Three: The object is being pushed to the end of the containing block's available logical width.
3002+ auto pushToEndFromTextAlign = !marginEndLength.isAuto () && ((!containingBlockStyle.isLeftToRightDirection () && containingBlockStyle.textAlign () == TextAlignMode::WebKitLeft)
3003+ || (containingBlockStyle.isLeftToRightDirection () && containingBlockStyle.textAlign () == TextAlignMode::WebKitRight));
3004+ if ((marginStartLength.isAuto () || pushToEndFromTextAlign) && childWidth < containerWidthForMarginAuto) {
3005+ marginEnd = computeOrTrimInlineMargin (containingBlock, MarginTrimType::InlineEnd, [containerWidthForMarginAuto, marginEndLength]() {
3006+ return valueForLength (marginEndLength, containerWidthForMarginAuto);
3007+ });
3008+ marginStart = computeOrTrimInlineMargin (containingBlock, MarginTrimType::InlineStart, [containerWidthForMarginAuto, childWidth, marginEnd]() {
3009+ return containerWidthForMarginAuto - childWidth - marginEnd;
3010+ });
3011+ return true ;
3012+ }
3013+ return false ;
3014+ };
3015+ if (handleMarginAuto ())
30093016 return ;
3010- }
30113017
3012- // Case Four: Either no auto margins, or our width is >= the container width (css2.1, 10.3.3). In that case
3018+ // Case Four: Either no auto margins, or our width is >= the container width (css2.1, 10.3.3). In that case
30133019 // auto margins will just turn into 0.
30143020 marginStart = computeOrTrimInlineMargin (containingBlock, MarginTrimType::InlineStart, [containerWidth, marginStartLength] {
30153021 return minimumValueForLength (marginStartLength, containerWidth);
@@ -3132,7 +3138,7 @@ RenderBox::LogicalExtentComputedValues RenderBox::computeLogicalHeight(LayoutUni
31323138 computedValues.m_extent = blockSizeFromAspectRatio (horizontalBorderAndPaddingExtent (), verticalBorderAndPaddingExtent (), style ().logicalAspectRatio (), style ().boxSizingForAspectRatio (), logicalWidth (), style ().aspectRatioType (), isRenderReplaced ());
31333139 if (hasPerpendicularContainingBlock) {
31343140 bool shouldFlipBeforeAfter = shouldFlipBeforeAfterMargins (cb.style (), &style ());
3135- computeInlineDirectionMargins (cb, containingBlockLogicalWidthForContent (), computedValues.m_extent ,
3141+ computeInlineDirectionMargins (cb, containingBlockLogicalWidthForContent (), { }, computedValues.m_extent ,
31363142 shouldFlipBeforeAfter ? computedValues.m_margins .m_after : computedValues.m_margins .m_before ,
31373143 shouldFlipBeforeAfter ? computedValues.m_margins .m_before : computedValues.m_margins .m_after );
31383144 }
@@ -3195,7 +3201,7 @@ RenderBox::LogicalExtentComputedValues RenderBox::computeLogicalHeight(LayoutUni
31953201
31963202 if (hasPerpendicularContainingBlock) {
31973203 bool shouldFlipBeforeAfter = shouldFlipBeforeAfterMargins (cb.style (), &style ());
3198- computeInlineDirectionMargins (cb, containingBlockLogicalWidthForContent (), heightResult,
3204+ computeInlineDirectionMargins (cb, containingBlockLogicalWidthForContent (), { }, heightResult,
31993205 shouldFlipBeforeAfter ? computedValues.m_margins .m_after : computedValues.m_margins .m_before ,
32003206 shouldFlipBeforeAfter ? computedValues.m_margins .m_before : computedValues.m_margins .m_after );
32013207 }
0 commit comments