From: L. David Baron Bug 616339 - Make min-height and max-height stop influencing whether margins collapse, except in one edge case. FIXME: There is no test coverage of the nsBlockReflowState.cpp change, but I believe the change is needed. FIXME: This causes failure of: layout/reftests/margin-collapsing/block-no-content-8.html layout/reftests/margin-collapsing/block-no-content-8-dyn.html Before the patch, the first gap is 60px and the second gap is 40px. With the patch, the first gap is 60px and the second gap is 60px as well. I believe the correct behavior is that the first gap is 60px, and the second gap is -20px (the green div covers the blue one). This is because each margin between the separators should collapse with the one following it, which implies that the margins of the #min-height div should collapse with each other because of the transitivity of collapsing. diff --git a/layout/generic/nsBlockFrame.cpp b/layout/generic/nsBlockFrame.cpp --- a/layout/generic/nsBlockFrame.cpp +++ b/layout/generic/nsBlockFrame.cpp @@ -1542,21 +1542,16 @@ nsBlockFrame::ComputeFinalSize(const nsH } // Don't carry out a block-end margin when our BSize is fixed. aMetrics.mCarriedOutBEndMargin.Zero(); } else if (NS_FRAME_IS_COMPLETE(aState.mReflowStatus)) { nscoord contentBSize = blockEndEdgeOfChildren - borderPadding.BStart(wm); nscoord autoBSize = aReflowState.ApplyMinMaxHeight(contentBSize); - if (autoBSize != contentBSize) { - // Our min-height or max-height made our height change. Don't carry out - // our kids' block-end margins. - aMetrics.mCarriedOutBEndMargin.Zero(); - } autoBSize += borderPadding.BStart(wm) + borderPadding.BEnd(wm); finalSize.BSize(wm) = autoBSize; } else { NS_ASSERTION(aReflowState.AvailableBSize() != NS_UNCONSTRAINEDSIZE, "Shouldn't be incomplete if availableBSize is UNCONSTRAINED."); finalSize.BSize(wm) = std::max(aState.mBCoord, aReflowState.AvailableBSize()); diff --git a/layout/generic/nsBlockReflowState.cpp b/layout/generic/nsBlockReflowState.cpp --- a/layout/generic/nsBlockReflowState.cpp +++ b/layout/generic/nsBlockReflowState.cpp @@ -23,16 +23,29 @@ #endif using namespace mozilla; using namespace mozilla::layout; static bool sFloatFragmentsInsideColumnEnabled; static bool sFloatFragmentsInsideColumnPrefCached; +static bool +BlockHasInFlowChildren(const nsBlockFrame* aBlockFrame) +{ + for (nsBlockFrame::const_line_iterator line = aBlockFrame->begin_lines(), + line_end = aBlockFrame->end_lines(); + line != line_end; ++line) { + if (line->IsBlock() || !line->IsEmpty()) { + return true; + } + } + return false; +} + nsBlockReflowState::nsBlockReflowState(const nsHTMLReflowState& aReflowState, nsPresContext* aPresContext, nsBlockFrame* aFrame, bool aBStartMarginRoot, bool aBEndMarginRoot, bool aBlockNeedsFloatManager, nscoord aConsumedBSize) : mBlock(aFrame), @@ -80,17 +93,30 @@ nsBlockReflowState::nsBlockReflowState(c mContainerWidth += mBorderPadding.LeftRight(wm); if ((aBStartMarginRoot && !logicalSkipSides.BStart()) || 0 != mBorderPadding.BStart(wm)) { SetFlag(BRS_ISBSTARTMARGINROOT, true); SetFlag(BRS_APPLYBSTARTMARGIN, true); } if ((aBEndMarginRoot && !logicalSkipSides.BEnd()) || - 0 != mBorderPadding.BEnd(wm)) { + 0 != mBorderPadding.BEnd(wm) || + // CSS 2.1 says the top and bottom margins of a block collapse + // together only under limited conditions; restrict that here. + // Note that we have to restrict much fewer cases than one might + // think from reading the spec, because in the cases of blocks + // with children, either the top and bottom margins are prevented + // from collapsing in other ways, or the do collapse through + // transitivity. So we only need to restrict top-bottom collapse + // in the case of no in-flow children despite the spec saying that + // that's the only case where it happens at all. + ((aReflowState.ComputedBSize() == NS_UNCONSTRAINEDSIZE || + aReflowState.ComputedBSize() == 0) && + aReflowState.ComputedMinBSize() == 0 && + !BlockHasInFlowChildren(aFrame))) { SetFlag(BRS_ISBENDMARGINROOT, true); } if (aBlockNeedsFloatManager) { SetFlag(BRS_FLOAT_MGR, true); } mFloatManager = aReflowState.mFloatManager; diff --git a/layout/reftests/margin-collapsing/reftest.list b/layout/reftests/margin-collapsing/reftest.list --- a/layout/reftests/margin-collapsing/reftest.list +++ b/layout/reftests/margin-collapsing/reftest.list @@ -363,22 +363,22 @@ skip-if(B2G) == block-sibling-2-dyn.html == block-zero-min-height-3a.html block-zero-min-height-3-ref.html == block-zero-min-height-3b.html block-zero-min-height-3-ref.html == block-zero-min-height-3c.html block-zero-min-height-3-ref.html == block-zero-min-height-3d.html block-zero-min-height-3-ref.html # If a parent box's height is 'auto' and its 'min-height' is non-zero, # then its bottom margin collapses with its last-child's bottom margin. # This is true even if the two margins do not actually touch each other, # as they are still considered 'adjoining'. -fails == block-min-height-last-child-1a.html block-min-height-last-child-1-ref.html # Bug 616339 +== block-min-height-last-child-1a.html block-min-height-last-child-1-ref.html == block-min-height-last-child-1b.html block-min-height-last-child-1-ref.html == block-min-height-last-child-1c.html block-min-height-last-child-1-ref.html -fails == block-min-height-last-child-2a.html block-min-height-last-child-2-ref.html # Bug 616339 -fails == block-min-height-last-child-2b.html block-min-height-last-child-2-ref.html # Bug 616339 -fails == block-min-height-last-child-3a.html block-min-height-last-child-3-ref.html # Bug 616339 +== block-min-height-last-child-2a.html block-min-height-last-child-2-ref.html +== block-min-height-last-child-2b.html block-min-height-last-child-2-ref.html +== block-min-height-last-child-3a.html block-min-height-last-child-3-ref.html == block-min-height-last-child-3b.html block-min-height-last-child-3-ref.html == block-min-height-last-child-3c.html block-min-height-last-child-3-ref.html == block-min-height-last-child-4a.html block-min-height-last-child-4-ref.html == block-min-height-last-child-4b.html block-min-height-last-child-4-ref.html == block-min-height-last-child-4c.html block-min-height-last-child-4-ref.html == block-min-height-last-child-5a.html block-min-height-last-child-5-ref.html == block-min-height-last-child-5b.html block-min-height-last-child-5-ref.html == block-min-height-last-child-6a.html block-min-height-last-child-6-ref.html @@ -387,22 +387,22 @@ fails == block-min-height-last-child-3a. == block-min-height-last-child-7a.html block-min-height-last-child-7-ref.html == block-min-height-last-child-7b.html block-min-height-last-child-7-ref.html == block-min-height-last-child-7c.html block-min-height-last-child-7-ref.html == block-min-height-last-child-8a.html block-min-height-last-child-8-ref.html == block-min-height-last-child-8b.html block-min-height-last-child-8-ref.html == block-min-height-last-child-9a.html block-min-height-last-child-9-ref.html == block-min-height-last-child-9b.html block-min-height-last-child-9-ref.html == block-min-height-last-child-9c.html block-min-height-last-child-9-ref.html -fails == block-min-height-last-child-1a-dyn.html block-min-height-last-child-1-ref.html # Bug 616339 +== block-min-height-last-child-1a-dyn.html block-min-height-last-child-1-ref.html == block-min-height-last-child-1b-dyn.html block-min-height-last-child-1-ref.html == block-min-height-last-child-1c-dyn.html block-min-height-last-child-1-ref.html -fails == block-min-height-last-child-2a-dyn.html block-min-height-last-child-2-ref.html # Bug 616339 -fails == block-min-height-last-child-2b-dyn.html block-min-height-last-child-2-ref.html # Bug 616339 -fails == block-min-height-last-child-3a-dyn.html block-min-height-last-child-3-ref.html # Bug 616339 +== block-min-height-last-child-2a-dyn.html block-min-height-last-child-2-ref.html +== block-min-height-last-child-2b-dyn.html block-min-height-last-child-2-ref.html +== block-min-height-last-child-3a-dyn.html block-min-height-last-child-3-ref.html == block-min-height-last-child-3b-dyn.html block-min-height-last-child-3-ref.html == block-min-height-last-child-3c-dyn.html block-min-height-last-child-3-ref.html == block-min-height-last-child-4a-dyn.html block-min-height-last-child-4-ref.html == block-min-height-last-child-4b-dyn.html block-min-height-last-child-4-ref.html == block-min-height-last-child-4c-dyn.html block-min-height-last-child-4-ref.html == block-min-height-last-child-5a-dyn.html block-min-height-last-child-5-ref.html == block-min-height-last-child-5b-dyn.html block-min-height-last-child-5-ref.html == block-min-height-last-child-6a-dyn.html block-min-height-last-child-6-ref.html @@ -431,22 +431,22 @@ fails == block-min-height-last-child-3a- == block-max-height-last-child-4a.html block-max-height-last-child-4-ref.html == block-max-height-last-child-4b.html block-max-height-last-child-4-ref.html == block-max-height-last-child-4c.html block-max-height-last-child-4-ref.html == block-max-height-last-child-5a.html block-max-height-last-child-5-ref.html == block-max-height-last-child-5b.html block-max-height-last-child-5-ref.html == block-max-height-last-child-6a.html block-max-height-last-child-6-ref.html == block-max-height-last-child-6b.html block-max-height-last-child-6-ref.html == block-max-height-last-child-6c.html block-max-height-last-child-6-ref.html -fails == block-max-height-last-child-7a.html block-max-height-last-child-7-ref.html # Bug 616339 +== block-max-height-last-child-7a.html block-max-height-last-child-7-ref.html == block-max-height-last-child-7b.html block-max-height-last-child-7-ref.html == block-max-height-last-child-7c.html block-max-height-last-child-7-ref.html -fails == block-max-height-last-child-8a.html block-max-height-last-child-8-ref.html # Bug 616339 -fails == block-max-height-last-child-8b.html block-max-height-last-child-8-ref.html # Bug 616339 -fails == block-max-height-last-child-9a.html block-max-height-last-child-9-ref.html # Bug 616339 +== block-max-height-last-child-8a.html block-max-height-last-child-8-ref.html +== block-max-height-last-child-8b.html block-max-height-last-child-8-ref.html +== block-max-height-last-child-9a.html block-max-height-last-child-9-ref.html == block-max-height-last-child-9b.html block-max-height-last-child-9-ref.html == block-max-height-last-child-9c.html block-max-height-last-child-9-ref.html == block-max-height-last-child-1a-dyn.html block-max-height-last-child-1-ref.html == block-max-height-last-child-1b-dyn.html block-max-height-last-child-1-ref.html == block-max-height-last-child-1c-dyn.html block-max-height-last-child-1-ref.html == block-max-height-last-child-2a-dyn.html block-max-height-last-child-2-ref.html == block-max-height-last-child-2b-dyn.html block-max-height-last-child-2-ref.html == block-max-height-last-child-3a-dyn.html block-max-height-last-child-3-ref.html @@ -455,22 +455,22 @@ fails == block-max-height-last-child-9a. == block-max-height-last-child-4a-dyn.html block-max-height-last-child-4-ref.html == block-max-height-last-child-4b-dyn.html block-max-height-last-child-4-ref.html == block-max-height-last-child-4c-dyn.html block-max-height-last-child-4-ref.html == block-max-height-last-child-5a-dyn.html block-max-height-last-child-5-ref.html == block-max-height-last-child-5b-dyn.html block-max-height-last-child-5-ref.html == block-max-height-last-child-6a-dyn.html block-max-height-last-child-6-ref.html == block-max-height-last-child-6b-dyn.html block-max-height-last-child-6-ref.html == block-max-height-last-child-6c-dyn.html block-max-height-last-child-6-ref.html -fails == block-max-height-last-child-7a-dyn.html block-max-height-last-child-7-ref.html # Bug 616339 +== block-max-height-last-child-7a-dyn.html block-max-height-last-child-7-ref.html == block-max-height-last-child-7b-dyn.html block-max-height-last-child-7-ref.html == block-max-height-last-child-7c-dyn.html block-max-height-last-child-7-ref.html -fails == block-max-height-last-child-8a-dyn.html block-max-height-last-child-8-ref.html # Bug 616339 -fails == block-max-height-last-child-8b-dyn.html block-max-height-last-child-8-ref.html # Bug 616339 -fails == block-max-height-last-child-9a-dyn.html block-max-height-last-child-9-ref.html # Bug 616339 +== block-max-height-last-child-8a-dyn.html block-max-height-last-child-8-ref.html +== block-max-height-last-child-8b-dyn.html block-max-height-last-child-8-ref.html +== block-max-height-last-child-9a-dyn.html block-max-height-last-child-9-ref.html == block-max-height-last-child-9b-dyn.html block-max-height-last-child-9-ref.html == block-max-height-last-child-9c-dyn.html block-max-height-last-child-9-ref.html # If a parent box's height is not 'auto' then its bottom margin # never collapses with its last-child's bottom margin, # as these margins are not considered 'adjoining'. # According to CSS 2.1 ยง10.7 (Minimum and maximum heights) this is true, # even if the value of min- or max-height is used instead. == block-fix-height-last-child-1a.html block-fix-height-last-child-1-ref.html