michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: michael@0: /* This Source Code is subject to the terms of the Mozilla Public License michael@0: * version 2.0 (the "License"). You can obtain a copy of the License at michael@0: * http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* rendering object for CSS "display: flex" */ michael@0: michael@0: #include "nsFlexContainerFrame.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsCSSAnonBoxes.h" michael@0: #include "nsDisplayList.h" michael@0: #include "nsIFrameInlines.h" michael@0: #include "nsLayoutUtils.h" michael@0: #include "nsPlaceholderFrame.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsStyleContext.h" michael@0: #include "prlog.h" michael@0: #include michael@0: #include "mozilla/LinkedList.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::css; michael@0: using namespace mozilla::layout; michael@0: michael@0: // Convenience typedefs for helper classes that we forward-declare in .h file michael@0: // (so that nsFlexContainerFrame methods can use them as parameters): michael@0: typedef nsFlexContainerFrame::FlexItem FlexItem; michael@0: typedef nsFlexContainerFrame::FlexLine FlexLine; michael@0: typedef nsFlexContainerFrame::FlexboxAxisTracker FlexboxAxisTracker; michael@0: typedef nsFlexContainerFrame::StrutInfo StrutInfo; michael@0: michael@0: #ifdef PR_LOGGING michael@0: static PRLogModuleInfo* michael@0: GetFlexContainerLog() michael@0: { michael@0: static PRLogModuleInfo *sLog; michael@0: if (!sLog) michael@0: sLog = PR_NewLogModule("nsFlexContainerFrame"); michael@0: return sLog; michael@0: } michael@0: #endif /* PR_LOGGING */ michael@0: michael@0: // XXXdholbert Some of this helper-stuff should be separated out into a general michael@0: // "LogicalAxisUtils.h" helper. Should that be a class, or a namespace (under michael@0: // what super-namespace?), or what? michael@0: michael@0: // Helper enums michael@0: // ============ michael@0: michael@0: // Represents a physical orientation for an axis. michael@0: // The directional suffix indicates the direction in which the axis *grows*. michael@0: // So e.g. eAxis_LR means a horizontal left-to-right axis, whereas eAxis_BT michael@0: // means a vertical bottom-to-top axis. michael@0: // NOTE: The order here is important -- these values are used as indices into michael@0: // the static array 'kAxisOrientationToSidesMap', defined below. michael@0: enum AxisOrientationType { michael@0: eAxis_LR, michael@0: eAxis_RL, michael@0: eAxis_TB, michael@0: eAxis_BT, michael@0: eNumAxisOrientationTypes // For sizing arrays that use these values as indices michael@0: }; michael@0: michael@0: // Represents one or the other extreme of an axis (e.g. for the main axis, the michael@0: // main-start vs. main-end edge. michael@0: // NOTE: The order here is important -- these values are used as indices into michael@0: // the sub-arrays in 'kAxisOrientationToSidesMap', defined below. michael@0: enum AxisEdgeType { michael@0: eAxisEdge_Start, michael@0: eAxisEdge_End, michael@0: eNumAxisEdges // For sizing arrays that use these values as indices michael@0: }; michael@0: michael@0: // This array maps each axis orientation to a pair of corresponding michael@0: // [start, end] physical mozilla::css::Side values. michael@0: static const Side michael@0: kAxisOrientationToSidesMap[eNumAxisOrientationTypes][eNumAxisEdges] = { michael@0: { eSideLeft, eSideRight }, // eAxis_LR michael@0: { eSideRight, eSideLeft }, // eAxis_RL michael@0: { eSideTop, eSideBottom }, // eAxis_TB michael@0: { eSideBottom, eSideTop } // eAxis_BT michael@0: }; michael@0: michael@0: // Helper structs / classes / methods michael@0: // ================================== michael@0: michael@0: // Indicates whether advancing along the given axis is equivalent to michael@0: // increasing our X or Y position (as opposed to decreasing it). michael@0: static inline bool michael@0: AxisGrowsInPositiveDirection(AxisOrientationType aAxis) michael@0: { michael@0: return eAxis_LR == aAxis || eAxis_TB == aAxis; michael@0: } michael@0: michael@0: // Indicates whether the given axis is horizontal. michael@0: static inline bool michael@0: IsAxisHorizontal(AxisOrientationType aAxis) michael@0: { michael@0: return eAxis_LR == aAxis || eAxis_RL == aAxis; michael@0: } michael@0: michael@0: // Given an AxisOrientationType, returns the "reverse" AxisOrientationType michael@0: // (in the same dimension, but the opposite direction) michael@0: static inline AxisOrientationType michael@0: GetReverseAxis(AxisOrientationType aAxis) michael@0: { michael@0: AxisOrientationType reversedAxis; michael@0: michael@0: if (aAxis % 2 == 0) { michael@0: // even enum value. Add 1 to reverse. michael@0: reversedAxis = AxisOrientationType(aAxis + 1); michael@0: } else { michael@0: // odd enum value. Subtract 1 to reverse. michael@0: reversedAxis = AxisOrientationType(aAxis - 1); michael@0: } michael@0: michael@0: // Check that we're still in the enum's valid range michael@0: MOZ_ASSERT(reversedAxis >= eAxis_LR && michael@0: reversedAxis <= eAxis_BT); michael@0: michael@0: return reversedAxis; michael@0: } michael@0: michael@0: // Returns aFrame's computed value for 'height' or 'width' -- whichever is in michael@0: // the same dimension as aAxis. michael@0: static inline const nsStyleCoord& michael@0: GetSizePropertyForAxis(const nsIFrame* aFrame, AxisOrientationType aAxis) michael@0: { michael@0: const nsStylePosition* stylePos = aFrame->StylePosition(); michael@0: michael@0: return IsAxisHorizontal(aAxis) ? michael@0: stylePos->mWidth : michael@0: stylePos->mHeight; michael@0: } michael@0: michael@0: /** michael@0: * Converts a logical position in a given axis into a position in the michael@0: * corresponding physical (x or y) axis. If the logical axis already maps michael@0: * directly onto one of our physical axes (i.e. LTR or TTB), then the logical michael@0: * and physical positions are equal; otherwise, we subtract the logical michael@0: * position from the container-size in that axis, to flip the polarity. michael@0: * (so e.g. a logical position of 2px in a RTL 20px-wide container michael@0: * would correspond to a physical position of 18px.) michael@0: */ michael@0: static nscoord michael@0: PhysicalPosFromLogicalPos(nscoord aLogicalPosn, michael@0: nscoord aLogicalContainerSize, michael@0: AxisOrientationType aAxis) { michael@0: if (AxisGrowsInPositiveDirection(aAxis)) { michael@0: return aLogicalPosn; michael@0: } michael@0: return aLogicalContainerSize - aLogicalPosn; michael@0: } michael@0: michael@0: static nscoord michael@0: MarginComponentForSide(const nsMargin& aMargin, Side aSide) michael@0: { michael@0: switch (aSide) { michael@0: case eSideLeft: michael@0: return aMargin.left; michael@0: case eSideRight: michael@0: return aMargin.right; michael@0: case eSideTop: michael@0: return aMargin.top; michael@0: case eSideBottom: michael@0: return aMargin.bottom; michael@0: } michael@0: michael@0: NS_NOTREACHED("unexpected Side enum"); michael@0: return aMargin.left; // have to return something michael@0: // (but something's busted if we got here) michael@0: } michael@0: michael@0: static nscoord& michael@0: MarginComponentForSide(nsMargin& aMargin, Side aSide) michael@0: { michael@0: switch (aSide) { michael@0: case eSideLeft: michael@0: return aMargin.left; michael@0: case eSideRight: michael@0: return aMargin.right; michael@0: case eSideTop: michael@0: return aMargin.top; michael@0: case eSideBottom: michael@0: return aMargin.bottom; michael@0: } michael@0: michael@0: NS_NOTREACHED("unexpected Side enum"); michael@0: return aMargin.left; // have to return something michael@0: // (but something's busted if we got here) michael@0: } michael@0: michael@0: // Helper-macro to let us pick one of two expressions to evaluate michael@0: // (a width expression vs. a height expression), to get a main-axis or michael@0: // cross-axis component. michael@0: // For code that has e.g. a nsSize object, FlexboxAxisTracker::GetMainComponent michael@0: // and GetCrossComponent are cleaner; but in cases where we simply have michael@0: // two separate expressions for width and height (which may be expensive to michael@0: // evaluate), these macros will ensure that only the expression for the correct michael@0: // axis gets evaluated. michael@0: #define GET_MAIN_COMPONENT(axisTracker_, width_, height_) \ michael@0: IsAxisHorizontal((axisTracker_).GetMainAxis()) ? (width_) : (height_) michael@0: michael@0: #define GET_CROSS_COMPONENT(axisTracker_, width_, height_) \ michael@0: IsAxisHorizontal((axisTracker_).GetCrossAxis()) ? (width_) : (height_) michael@0: michael@0: // Encapsulates our flex container's main & cross axes. michael@0: class MOZ_STACK_CLASS nsFlexContainerFrame::FlexboxAxisTracker { michael@0: public: michael@0: FlexboxAxisTracker(nsFlexContainerFrame* aFlexContainerFrame); michael@0: michael@0: // Accessors: michael@0: AxisOrientationType GetMainAxis() const { return mMainAxis; } michael@0: AxisOrientationType GetCrossAxis() const { return mCrossAxis; } michael@0: michael@0: nscoord GetMainComponent(const nsSize& aSize) const { michael@0: return GET_MAIN_COMPONENT(*this, aSize.width, aSize.height); michael@0: } michael@0: int32_t GetMainComponent(const nsIntSize& aIntSize) const { michael@0: return GET_MAIN_COMPONENT(*this, aIntSize.width, aIntSize.height); michael@0: } michael@0: michael@0: nscoord GetCrossComponent(const nsSize& aSize) const { michael@0: return GET_CROSS_COMPONENT(*this, aSize.width, aSize.height); michael@0: } michael@0: int32_t GetCrossComponent(const nsIntSize& aIntSize) const { michael@0: return GET_CROSS_COMPONENT(*this, aIntSize.width, aIntSize.height); michael@0: } michael@0: michael@0: nscoord GetMarginSizeInMainAxis(const nsMargin& aMargin) const { michael@0: return IsAxisHorizontal(mMainAxis) ? michael@0: aMargin.LeftRight() : michael@0: aMargin.TopBottom(); michael@0: } michael@0: nscoord GetMarginSizeInCrossAxis(const nsMargin& aMargin) const { michael@0: return IsAxisHorizontal(mCrossAxis) ? michael@0: aMargin.LeftRight() : michael@0: aMargin.TopBottom(); michael@0: } michael@0: michael@0: /** michael@0: * Converts a logical point into a "physical" x,y point. michael@0: * michael@0: * In the simplest case where the main-axis is left-to-right and the michael@0: * cross-axis is top-to-bottom, this just returns michael@0: * nsPoint(aMainPosn, aCrossPosn). michael@0: * michael@0: * @arg aMainPosn The main-axis position -- i.e an offset from the michael@0: * main-start edge of the container's content box. michael@0: * @arg aCrossPosn The cross-axis position -- i.e an offset from the michael@0: * cross-start edge of the container's content box. michael@0: * @return A nsPoint representing the same position (in coordinates michael@0: * relative to the container's content box). michael@0: */ michael@0: nsPoint PhysicalPointFromLogicalPoint(nscoord aMainPosn, michael@0: nscoord aCrossPosn, michael@0: nscoord aContainerMainSize, michael@0: nscoord aContainerCrossSize) const { michael@0: nscoord physicalPosnInMainAxis = michael@0: PhysicalPosFromLogicalPos(aMainPosn, aContainerMainSize, mMainAxis); michael@0: nscoord physicalPosnInCrossAxis = michael@0: PhysicalPosFromLogicalPos(aCrossPosn, aContainerCrossSize, mCrossAxis); michael@0: michael@0: return IsAxisHorizontal(mMainAxis) ? michael@0: nsPoint(physicalPosnInMainAxis, physicalPosnInCrossAxis) : michael@0: nsPoint(physicalPosnInCrossAxis, physicalPosnInMainAxis); michael@0: } michael@0: nsSize PhysicalSizeFromLogicalSizes(nscoord aMainSize, michael@0: nscoord aCrossSize) const { michael@0: return IsAxisHorizontal(mMainAxis) ? michael@0: nsSize(aMainSize, aCrossSize) : michael@0: nsSize(aCrossSize, aMainSize); michael@0: } michael@0: michael@0: // Are my axes reversed with respect to what the author asked for? michael@0: // (We may reverse the axes in the FlexboxAxisTracker constructor and set michael@0: // this flag, to avoid reflowing our children in bottom-to-top order.) michael@0: bool AreAxesInternallyReversed() const michael@0: { michael@0: return mAreAxesInternallyReversed; michael@0: } michael@0: michael@0: private: michael@0: AxisOrientationType mMainAxis; michael@0: AxisOrientationType mCrossAxis; michael@0: bool mAreAxesInternallyReversed; michael@0: }; michael@0: michael@0: /** michael@0: * Represents a flex item. michael@0: * Includes the various pieces of input that the Flexbox Layout Algorithm uses michael@0: * to resolve a flexible width. michael@0: */ michael@0: class nsFlexContainerFrame::FlexItem : public LinkedListElement michael@0: { michael@0: public: michael@0: // Normal constructor: michael@0: FlexItem(nsIFrame* aChildFrame, michael@0: float aFlexGrow, float aFlexShrink, nscoord aMainBaseSize, michael@0: nscoord aMainMinSize, nscoord aMainMaxSize, michael@0: nscoord aCrossMinSize, nscoord aCrossMaxSize, michael@0: nsMargin aMargin, nsMargin aBorderPadding, michael@0: const FlexboxAxisTracker& aAxisTracker); michael@0: michael@0: // Simplified constructor, to be used only for generating "struts": michael@0: FlexItem(nsIFrame* aChildFrame, nscoord aCrossSize); michael@0: michael@0: // Accessors michael@0: nsIFrame* Frame() const { return mFrame; } michael@0: nscoord GetFlexBaseSize() const { return mFlexBaseSize; } michael@0: michael@0: nscoord GetMainMinSize() const { return mMainMinSize; } michael@0: nscoord GetMainMaxSize() const { return mMainMaxSize; } michael@0: michael@0: // Note: These return the main-axis position and size of our *content box*. michael@0: nscoord GetMainSize() const { return mMainSize; } michael@0: nscoord GetMainPosition() const { return mMainPosn; } michael@0: michael@0: nscoord GetCrossMinSize() const { return mCrossMinSize; } michael@0: nscoord GetCrossMaxSize() const { return mCrossMaxSize; } michael@0: michael@0: // Note: These return the cross-axis position and size of our *content box*. michael@0: nscoord GetCrossSize() const { return mCrossSize; } michael@0: nscoord GetCrossPosition() const { return mCrossPosn; } michael@0: michael@0: // Convenience methods to compute the main & cross size of our *margin-box*. michael@0: // The caller is responsible for telling us the right axis, so that we can michael@0: // pull out the appropriate components of our margin/border/padding structs. michael@0: nscoord GetOuterMainSize(AxisOrientationType aMainAxis) const michael@0: { michael@0: return mMainSize + GetMarginBorderPaddingSizeInAxis(aMainAxis); michael@0: } michael@0: michael@0: nscoord GetOuterCrossSize(AxisOrientationType aCrossAxis) const michael@0: { michael@0: return mCrossSize + GetMarginBorderPaddingSizeInAxis(aCrossAxis); michael@0: } michael@0: michael@0: // Returns the distance between this FlexItem's baseline and the cross-start michael@0: // edge of its margin-box. Used in baseline alignment. michael@0: // (This function needs to be told what cross axis is & which edge we're michael@0: // measuring the baseline from, so that it can look up the appropriate michael@0: // components from mMargin.) michael@0: nscoord GetBaselineOffsetFromOuterCrossEdge(AxisOrientationType aCrossAxis, michael@0: AxisEdgeType aEdge) const; michael@0: michael@0: float GetShareOfFlexWeightSoFar() const { return mShareOfFlexWeightSoFar; } michael@0: michael@0: bool IsFrozen() const { return mIsFrozen; } michael@0: michael@0: bool HadMinViolation() const { return mHadMinViolation; } michael@0: bool HadMaxViolation() const { return mHadMaxViolation; } michael@0: michael@0: // Indicates whether this item received a preliminary "measuring" reflow michael@0: // before its actual reflow. michael@0: bool HadMeasuringReflow() const { return mHadMeasuringReflow; } michael@0: michael@0: // Indicates whether this item's cross-size has been stretched (from having michael@0: // "align-self: stretch" with an auto cross-size and no auto margins in the michael@0: // cross axis). michael@0: bool IsStretched() const { return mIsStretched; } michael@0: michael@0: // Indicates whether this item is a "strut" left behind by an element with michael@0: // visibility:collapse. michael@0: bool IsStrut() const { return mIsStrut; } michael@0: michael@0: uint8_t GetAlignSelf() const { return mAlignSelf; } michael@0: michael@0: // Returns the flex weight that we should use in the "resolving flexible michael@0: // lengths" algorithm. If we're using flex grow, we just return that; michael@0: // otherwise, we use the "scaled flex shrink weight" (scaled by our flex michael@0: // base size, so that when both large and small items are shrinking, michael@0: // the large items shrink more). michael@0: float GetFlexWeightToUse(bool aIsUsingFlexGrow) michael@0: { michael@0: if (IsFrozen()) { michael@0: return 0.0f; michael@0: } michael@0: michael@0: if (aIsUsingFlexGrow) { michael@0: return mFlexGrow; michael@0: } michael@0: michael@0: // We're using flex-shrink --> return mFlexShrink * mFlexBaseSize michael@0: if (mFlexBaseSize == 0) { michael@0: // Special-case for mFlexBaseSize == 0 -- we have no room to shrink, so michael@0: // regardless of mFlexShrink, we should just return 0. michael@0: // (This is really a special-case for when mFlexShrink is infinity, to michael@0: // avoid performing mFlexShrink * mFlexBaseSize = inf * 0 = undefined.) michael@0: return 0.0f; michael@0: } michael@0: return mFlexShrink * mFlexBaseSize; michael@0: } michael@0: michael@0: // Getters for margin: michael@0: // =================== michael@0: const nsMargin& GetMargin() const { return mMargin; } michael@0: michael@0: // Returns the margin component for a given mozilla::css::Side michael@0: nscoord GetMarginComponentForSide(Side aSide) const michael@0: { return MarginComponentForSide(mMargin, aSide); } michael@0: michael@0: // Returns the total space occupied by this item's margins in the given axis michael@0: nscoord GetMarginSizeInAxis(AxisOrientationType aAxis) const michael@0: { michael@0: Side startSide = kAxisOrientationToSidesMap[aAxis][eAxisEdge_Start]; michael@0: Side endSide = kAxisOrientationToSidesMap[aAxis][eAxisEdge_End]; michael@0: return GetMarginComponentForSide(startSide) + michael@0: GetMarginComponentForSide(endSide); michael@0: } michael@0: michael@0: // Getters for border/padding michael@0: // ========================== michael@0: const nsMargin& GetBorderPadding() const { return mBorderPadding; } michael@0: michael@0: // Returns the border+padding component for a given mozilla::css::Side michael@0: nscoord GetBorderPaddingComponentForSide(Side aSide) const michael@0: { return MarginComponentForSide(mBorderPadding, aSide); } michael@0: michael@0: // Returns the total space occupied by this item's borders and padding in michael@0: // the given axis michael@0: nscoord GetBorderPaddingSizeInAxis(AxisOrientationType aAxis) const michael@0: { michael@0: Side startSide = kAxisOrientationToSidesMap[aAxis][eAxisEdge_Start]; michael@0: Side endSide = kAxisOrientationToSidesMap[aAxis][eAxisEdge_End]; michael@0: return GetBorderPaddingComponentForSide(startSide) + michael@0: GetBorderPaddingComponentForSide(endSide); michael@0: } michael@0: michael@0: // Getter for combined margin/border/padding michael@0: // ========================================= michael@0: // Returns the total space occupied by this item's margins, borders and michael@0: // padding in the given axis michael@0: nscoord GetMarginBorderPaddingSizeInAxis(AxisOrientationType aAxis) const michael@0: { michael@0: return GetMarginSizeInAxis(aAxis) + GetBorderPaddingSizeInAxis(aAxis); michael@0: } michael@0: michael@0: // Setters michael@0: // ======= michael@0: michael@0: // This sets our flex base size, and then updates the main size to the michael@0: // base size clamped to our main-axis [min,max] constraints. michael@0: void SetFlexBaseSizeAndMainSize(nscoord aNewFlexBaseSize) michael@0: { michael@0: MOZ_ASSERT(!mIsFrozen || mFlexBaseSize == NS_INTRINSICSIZE, michael@0: "flex base size shouldn't change after we're frozen " michael@0: "(unless we're just resolving an intrinsic size)"); michael@0: mFlexBaseSize = aNewFlexBaseSize; michael@0: michael@0: // Before we've resolved flexible lengths, we keep mMainSize set to michael@0: // the 'hypothetical main size', which is the flex base size, clamped michael@0: // to the [min,max] range: michael@0: mMainSize = NS_CSS_MINMAX(mFlexBaseSize, mMainMinSize, mMainMaxSize); michael@0: } michael@0: michael@0: // Setters used while we're resolving flexible lengths michael@0: // --------------------------------------------------- michael@0: michael@0: // Sets the main-size of our flex item's content-box. michael@0: void SetMainSize(nscoord aNewMainSize) michael@0: { michael@0: MOZ_ASSERT(!mIsFrozen, "main size shouldn't change after we're frozen"); michael@0: mMainSize = aNewMainSize; michael@0: } michael@0: michael@0: void SetShareOfFlexWeightSoFar(float aNewShare) michael@0: { michael@0: MOZ_ASSERT(!mIsFrozen || aNewShare == 0.0f, michael@0: "shouldn't be giving this item any share of the weight " michael@0: "after it's frozen"); michael@0: mShareOfFlexWeightSoFar = aNewShare; michael@0: } michael@0: michael@0: void Freeze() { mIsFrozen = true; } michael@0: michael@0: void SetHadMinViolation() michael@0: { michael@0: MOZ_ASSERT(!mIsFrozen, michael@0: "shouldn't be changing main size & having violations " michael@0: "after we're frozen"); michael@0: mHadMinViolation = true; michael@0: } michael@0: void SetHadMaxViolation() michael@0: { michael@0: MOZ_ASSERT(!mIsFrozen, michael@0: "shouldn't be changing main size & having violations " michael@0: "after we're frozen"); michael@0: mHadMaxViolation = true; michael@0: } michael@0: void ClearViolationFlags() michael@0: { mHadMinViolation = mHadMaxViolation = false; } michael@0: michael@0: // Setters for values that are determined after we've resolved our main size michael@0: // ------------------------------------------------------------------------- michael@0: michael@0: // Sets the main-axis position of our flex item's content-box. michael@0: // (This is the distance between the main-start edge of the flex container michael@0: // and the main-start edge of the flex item's content-box.) michael@0: void SetMainPosition(nscoord aPosn) { michael@0: MOZ_ASSERT(mIsFrozen, "main size should be resolved before this"); michael@0: mMainPosn = aPosn; michael@0: } michael@0: michael@0: // Sets the cross-size of our flex item's content-box. michael@0: void SetCrossSize(nscoord aCrossSize) { michael@0: MOZ_ASSERT(!mIsStretched, michael@0: "Cross size shouldn't be modified after it's been stretched"); michael@0: mCrossSize = aCrossSize; michael@0: } michael@0: michael@0: // Sets the cross-axis position of our flex item's content-box. michael@0: // (This is the distance between the cross-start edge of the flex container michael@0: // and the cross-start edge of the flex item.) michael@0: void SetCrossPosition(nscoord aPosn) { michael@0: MOZ_ASSERT(mIsFrozen, "main size should be resolved before this"); michael@0: mCrossPosn = aPosn; michael@0: } michael@0: michael@0: void SetAscent(nscoord aAscent) { michael@0: mAscent = aAscent; michael@0: } michael@0: michael@0: void SetHadMeasuringReflow() { michael@0: mHadMeasuringReflow = true; michael@0: } michael@0: michael@0: void SetIsStretched() { michael@0: MOZ_ASSERT(mIsFrozen, "main size should be resolved before this"); michael@0: mIsStretched = true; michael@0: } michael@0: michael@0: // Setter for margin components (for resolving "auto" margins) michael@0: void SetMarginComponentForSide(Side aSide, nscoord aLength) michael@0: { michael@0: MOZ_ASSERT(mIsFrozen, "main size should be resolved before this"); michael@0: MarginComponentForSide(mMargin, aSide) = aLength; michael@0: } michael@0: michael@0: void ResolveStretchedCrossSize(nscoord aLineCrossSize, michael@0: const FlexboxAxisTracker& aAxisTracker); michael@0: michael@0: uint32_t GetNumAutoMarginsInAxis(AxisOrientationType aAxis) const; michael@0: michael@0: protected: michael@0: // Our frame: michael@0: nsIFrame* const mFrame; michael@0: michael@0: // Values that we already know in constructor: (and are hence mostly 'const') michael@0: const float mFlexGrow; michael@0: const float mFlexShrink; michael@0: michael@0: const nsMargin mBorderPadding; michael@0: nsMargin mMargin; // non-const because we need to resolve auto margins michael@0: michael@0: nscoord mFlexBaseSize; michael@0: michael@0: const nscoord mMainMinSize; michael@0: const nscoord mMainMaxSize; michael@0: const nscoord mCrossMinSize; michael@0: const nscoord mCrossMaxSize; michael@0: michael@0: // Values that we compute after constructor: michael@0: nscoord mMainSize; michael@0: nscoord mMainPosn; michael@0: nscoord mCrossSize; michael@0: nscoord mCrossPosn; michael@0: nscoord mAscent; michael@0: michael@0: // Temporary state, while we're resolving flexible widths (for our main size) michael@0: // XXXdholbert To save space, we could use a union to make these variables michael@0: // overlay the same memory as some other member vars that aren't touched michael@0: // until after main-size has been resolved. In particular, these could share michael@0: // memory with mMainPosn through mAscent, and mIsStretched. michael@0: float mShareOfFlexWeightSoFar; michael@0: bool mIsFrozen; michael@0: bool mHadMinViolation; michael@0: bool mHadMaxViolation; michael@0: michael@0: // Misc: michael@0: bool mHadMeasuringReflow; // Did this item get a preliminary reflow, michael@0: // to measure its desired height? michael@0: bool mIsStretched; // See IsStretched() documentation michael@0: bool mIsStrut; // Is this item a "strut" left behind by an element michael@0: // with visibility:collapse? michael@0: uint8_t mAlignSelf; // My "align-self" computed value (with "auto" michael@0: // swapped out for parent"s "align-items" value, michael@0: // in our constructor). michael@0: }; michael@0: michael@0: /** michael@0: * Represents a single flex line in a flex container. michael@0: * Manages a linked list of the FlexItems that are in the line. michael@0: */ michael@0: class nsFlexContainerFrame::FlexLine : public LinkedListElement michael@0: { michael@0: public: michael@0: FlexLine() michael@0: : mNumItems(0), michael@0: mTotalInnerHypotheticalMainSize(0), michael@0: mTotalOuterHypotheticalMainSize(0), michael@0: mLineCrossSize(0), michael@0: mBaselineOffset(nscoord_MIN) michael@0: {} michael@0: michael@0: // Returns the sum of our FlexItems' outer hypothetical main sizes. michael@0: // ("outer" = margin-box, and "hypothetical" = before flexing) michael@0: nscoord GetTotalOuterHypotheticalMainSize() const { michael@0: return mTotalOuterHypotheticalMainSize; michael@0: } michael@0: michael@0: // Accessors for our FlexItems & information about them: michael@0: FlexItem* GetFirstItem() michael@0: { michael@0: MOZ_ASSERT(mItems.isEmpty() == (mNumItems == 0), michael@0: "mNumItems bookkeeping is off"); michael@0: return mItems.getFirst(); michael@0: } michael@0: michael@0: const FlexItem* GetFirstItem() const michael@0: { michael@0: MOZ_ASSERT(mItems.isEmpty() == (mNumItems == 0), michael@0: "mNumItems bookkeeping is off"); michael@0: return mItems.getFirst(); michael@0: } michael@0: michael@0: bool IsEmpty() const michael@0: { michael@0: MOZ_ASSERT(mItems.isEmpty() == (mNumItems == 0), michael@0: "mNumItems bookkeeping is off"); michael@0: return mItems.isEmpty(); michael@0: } michael@0: michael@0: uint32_t NumItems() const michael@0: { michael@0: MOZ_ASSERT(mItems.isEmpty() == (mNumItems == 0), michael@0: "mNumItems bookkeeping is off"); michael@0: return mNumItems; michael@0: } michael@0: michael@0: // Adds the given FlexItem to our list of items (at the front or back michael@0: // depending on aShouldInsertAtFront), and adds its hypothetical michael@0: // outer & inner main sizes to our totals. Use this method instead of michael@0: // directly modifying the item list, so that our bookkeeping remains correct. michael@0: void AddItem(FlexItem* aItem, michael@0: bool aShouldInsertAtFront, michael@0: nscoord aItemInnerHypotheticalMainSize, michael@0: nscoord aItemOuterHypotheticalMainSize) michael@0: { michael@0: if (aShouldInsertAtFront) { michael@0: mItems.insertFront(aItem); michael@0: } else { michael@0: mItems.insertBack(aItem); michael@0: } michael@0: mNumItems++; michael@0: mTotalInnerHypotheticalMainSize += aItemInnerHypotheticalMainSize; michael@0: mTotalOuterHypotheticalMainSize += aItemOuterHypotheticalMainSize; michael@0: } michael@0: michael@0: // Computes the cross-size and baseline position of this FlexLine, based on michael@0: // its FlexItems. michael@0: void ComputeCrossSizeAndBaseline(const FlexboxAxisTracker& aAxisTracker); michael@0: michael@0: // Returns the cross-size of this line. michael@0: nscoord GetLineCrossSize() const { return mLineCrossSize; } michael@0: michael@0: // Setter for line cross-size -- needed for cases where the flex container michael@0: // imposes a cross-size on the line. (e.g. for single-line flexbox, or for michael@0: // multi-line flexbox with 'align-content: stretch') michael@0: void SetLineCrossSize(nscoord aLineCrossSize) { michael@0: mLineCrossSize = aLineCrossSize; michael@0: } michael@0: michael@0: /** michael@0: * Returns the offset within this line where any baseline-aligned FlexItems michael@0: * should place their baseline. Usually, this represents a distance from the michael@0: * line's cross-start edge, but if we're internally reversing the axes (see michael@0: * AreAxesInternallyReversed()), this instead represents the distance from michael@0: * its cross-end edge. michael@0: * michael@0: * If there are no baseline-aligned FlexItems, returns nscoord_MIN. michael@0: */ michael@0: nscoord GetBaselineOffset() const { michael@0: return mBaselineOffset; michael@0: } michael@0: michael@0: // Runs the "Resolving Flexible Lengths" algorithm from section 9.7 of the michael@0: // CSS flexbox spec to distribute aFlexContainerMainSize among our flex items. michael@0: void ResolveFlexibleLengths(nscoord aFlexContainerMainSize); michael@0: michael@0: void PositionItemsInMainAxis(uint8_t aJustifyContent, michael@0: nscoord aContentBoxMainSize, michael@0: const FlexboxAxisTracker& aAxisTracker); michael@0: michael@0: void PositionItemsInCrossAxis(nscoord aLineStartPosition, michael@0: const FlexboxAxisTracker& aAxisTracker); michael@0: michael@0: friend class AutoFlexLineListClearer; // (needs access to mItems) michael@0: michael@0: private: michael@0: // Helper for ResolveFlexibleLengths(): michael@0: void FreezeOrRestoreEachFlexibleSize(const nscoord aTotalViolation, michael@0: bool aIsFinalIteration); michael@0: michael@0: LinkedList mItems; // Linked list of this line's flex items. michael@0: michael@0: uint32_t mNumItems; // Number of FlexItems in this line (in |mItems|). michael@0: // (Shouldn't change after GenerateFlexLines finishes michael@0: // with this line -- at least, not until we add support michael@0: // for splitting lines across continuations. Then we can michael@0: // update this count carefully.) michael@0: michael@0: nscoord mTotalInnerHypotheticalMainSize; michael@0: nscoord mTotalOuterHypotheticalMainSize; michael@0: nscoord mLineCrossSize; michael@0: nscoord mBaselineOffset; michael@0: }; michael@0: michael@0: // Information about a strut left behind by a FlexItem that's been collapsed michael@0: // using "visibility:collapse". michael@0: struct nsFlexContainerFrame::StrutInfo { michael@0: StrutInfo(uint32_t aItemIdx, nscoord aStrutCrossSize) michael@0: : mItemIdx(aItemIdx), michael@0: mStrutCrossSize(aStrutCrossSize) michael@0: { michael@0: } michael@0: michael@0: uint32_t mItemIdx; // Index in the child list. michael@0: nscoord mStrutCrossSize; // The cross-size of this strut. michael@0: }; michael@0: michael@0: static void michael@0: BuildStrutInfoFromCollapsedItems(const FlexLine* aFirstLine, michael@0: nsTArray& aStruts) michael@0: { michael@0: MOZ_ASSERT(aFirstLine, "null first line pointer"); michael@0: MOZ_ASSERT(aStruts.IsEmpty(), michael@0: "We should only build up StrutInfo once per reflow, so " michael@0: "aStruts should be empty when this is called"); michael@0: michael@0: uint32_t itemIdxInContainer = 0; michael@0: for (const FlexLine* line = aFirstLine; line; line = line->getNext()) { michael@0: for (const FlexItem* item = line->GetFirstItem(); item; michael@0: item = item->getNext()) { michael@0: if (NS_STYLE_VISIBILITY_COLLAPSE == michael@0: item->Frame()->StyleVisibility()->mVisible) { michael@0: // Note the cross size of the line as the item's strut size. michael@0: aStruts.AppendElement(StrutInfo(itemIdxInContainer, michael@0: line->GetLineCrossSize())); michael@0: } michael@0: itemIdxInContainer++; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Helper-function to find the first non-anonymous-box descendent of aFrame. michael@0: static nsIFrame* michael@0: GetFirstNonAnonBoxDescendant(nsIFrame* aFrame) michael@0: { michael@0: while (aFrame) { michael@0: nsIAtom* pseudoTag = aFrame->StyleContext()->GetPseudo(); michael@0: michael@0: // If aFrame isn't an anonymous container, then it'll do. michael@0: if (!pseudoTag || // No pseudotag. michael@0: !nsCSSAnonBoxes::IsAnonBox(pseudoTag) || // Pseudotag isn't anon. michael@0: pseudoTag == nsCSSAnonBoxes::mozNonElement) { // Text, not a container. michael@0: break; michael@0: } michael@0: michael@0: // Otherwise, descend to its first child and repeat. michael@0: michael@0: // SPECIAL CASE: if we're dealing with an anonymous table, then it might michael@0: // be wrapping something non-anonymous in its caption or col-group lists michael@0: // (instead of its principal child list), so we have to look there. michael@0: // (Note: For anonymous tables that have a non-anon cell *and* a non-anon michael@0: // column, we'll always return the column. This is fine; we're really just michael@0: // looking for a handle to *anything* with a meaningful content node inside michael@0: // the table, for use in DOM comparisons to things outside of the table.) michael@0: if (MOZ_UNLIKELY(aFrame->GetType() == nsGkAtoms::tableOuterFrame)) { michael@0: nsIFrame* captionDescendant = michael@0: GetFirstNonAnonBoxDescendant(aFrame->GetFirstChild(kCaptionList)); michael@0: if (captionDescendant) { michael@0: return captionDescendant; michael@0: } michael@0: } else if (MOZ_UNLIKELY(aFrame->GetType() == nsGkAtoms::tableFrame)) { michael@0: nsIFrame* colgroupDescendant = michael@0: GetFirstNonAnonBoxDescendant(aFrame->GetFirstChild(kColGroupList)); michael@0: if (colgroupDescendant) { michael@0: return colgroupDescendant; michael@0: } michael@0: } michael@0: michael@0: // USUAL CASE: Descend to the first child in principal list. michael@0: aFrame = aFrame->GetFirstPrincipalChild(); michael@0: } michael@0: return aFrame; michael@0: } michael@0: michael@0: /** michael@0: * Sorting helper-function that compares two frames' "order" property-values, michael@0: * and if they're equal, compares the DOM positions of their corresponding michael@0: * content nodes. Returns true if aFrame1 is "less than or equal to" aFrame2 michael@0: * according to this comparison. michael@0: * michael@0: * Note: This can't be a static function, because we need to pass it as a michael@0: * template argument. (Only functions with external linkage can be passed as michael@0: * template arguments.) michael@0: * michael@0: * @return true if the computed "order" property of aFrame1 is less than that michael@0: * of aFrame2, or if the computed "order" values are equal and aFrame1's michael@0: * corresponding DOM node is earlier than aFrame2's in the DOM tree. michael@0: * Otherwise, returns false. michael@0: */ michael@0: bool michael@0: IsOrderLEQWithDOMFallback(nsIFrame* aFrame1, michael@0: nsIFrame* aFrame2) michael@0: { michael@0: MOZ_ASSERT(aFrame1->IsFlexItem() && aFrame2->IsFlexItem(), michael@0: "this method only intended for comparing flex items"); michael@0: michael@0: if (aFrame1 == aFrame2) { michael@0: // Anything is trivially LEQ itself, so we return "true" here... but it's michael@0: // probably bad if we end up actually needing this, so let's assert. michael@0: NS_ERROR("Why are we checking if a frame is LEQ itself?"); michael@0: return true; michael@0: } michael@0: michael@0: // If we've got a placeholder frame, use its out-of-flow frame's 'order' val. michael@0: { michael@0: nsIFrame* aRealFrame1 = nsPlaceholderFrame::GetRealFrameFor(aFrame1); michael@0: nsIFrame* aRealFrame2 = nsPlaceholderFrame::GetRealFrameFor(aFrame2); michael@0: michael@0: int32_t order1 = aRealFrame1->StylePosition()->mOrder; michael@0: int32_t order2 = aRealFrame2->StylePosition()->mOrder; michael@0: michael@0: if (order1 != order2) { michael@0: return order1 < order2; michael@0: } michael@0: } michael@0: michael@0: // The "order" values are equal, so we need to fall back on DOM comparison. michael@0: // For that, we need to dig through any anonymous box wrapper frames to find michael@0: // the actual frame that corresponds to our child content. michael@0: aFrame1 = GetFirstNonAnonBoxDescendant(aFrame1); michael@0: aFrame2 = GetFirstNonAnonBoxDescendant(aFrame2); michael@0: MOZ_ASSERT(aFrame1 && aFrame2, michael@0: "why do we have an anonymous box without any " michael@0: "non-anonymous descendants?"); michael@0: michael@0: michael@0: // Special case: michael@0: // If either frame is for generated content from ::before or ::after, then michael@0: // we can't use nsContentUtils::PositionIsBefore(), since that method won't michael@0: // recognize generated content as being an actual sibling of other nodes. michael@0: // We know where ::before and ::after nodes *effectively* insert in the DOM michael@0: // tree, though (at the beginning & end), so we can just special-case them. michael@0: nsIAtom* pseudo1 = aFrame1->StyleContext()->GetPseudo(); michael@0: nsIAtom* pseudo2 = aFrame2->StyleContext()->GetPseudo(); michael@0: if (pseudo1 == nsCSSPseudoElements::before || michael@0: pseudo2 == nsCSSPseudoElements::after) { michael@0: // frame1 is ::before and/or frame2 is ::after => frame1 is LEQ frame2. michael@0: return true; michael@0: } michael@0: if (pseudo1 == nsCSSPseudoElements::after || michael@0: pseudo2 == nsCSSPseudoElements::before) { michael@0: // frame1 is ::after and/or frame2 is ::before => frame1 is not LEQ frame2. michael@0: return false; michael@0: } michael@0: michael@0: // Usual case: Compare DOM position. michael@0: nsIContent* content1 = aFrame1->GetContent(); michael@0: nsIContent* content2 = aFrame2->GetContent(); michael@0: MOZ_ASSERT(content1 != content2, michael@0: "Two different flex items are using the same nsIContent node for " michael@0: "comparison, so we may be sorting them in an arbitrary order"); michael@0: michael@0: return nsContentUtils::PositionIsBefore(content1, content2); michael@0: } michael@0: michael@0: /** michael@0: * Sorting helper-function that compares two frames' "order" property-values. michael@0: * Returns true if aFrame1 is "less than or equal to" aFrame2 according to this michael@0: * comparison. michael@0: * michael@0: * Note: This can't be a static function, because we need to pass it as a michael@0: * template argument. (Only functions with external linkage can be passed as michael@0: * template arguments.) michael@0: * michael@0: * @return true if the computed "order" property of aFrame1 is less than or michael@0: * equal to that of aFrame2. Otherwise, returns false. michael@0: */ michael@0: bool michael@0: IsOrderLEQ(nsIFrame* aFrame1, michael@0: nsIFrame* aFrame2) michael@0: { michael@0: MOZ_ASSERT(aFrame1->IsFlexItem() && aFrame2->IsFlexItem(), michael@0: "this method only intended for comparing flex items"); michael@0: michael@0: // If we've got a placeholder frame, use its out-of-flow frame's 'order' val. michael@0: nsIFrame* aRealFrame1 = nsPlaceholderFrame::GetRealFrameFor(aFrame1); michael@0: nsIFrame* aRealFrame2 = nsPlaceholderFrame::GetRealFrameFor(aFrame2); michael@0: michael@0: int32_t order1 = aRealFrame1->StylePosition()->mOrder; michael@0: int32_t order2 = aRealFrame2->StylePosition()->mOrder; michael@0: michael@0: return order1 <= order2; michael@0: } michael@0: michael@0: bool michael@0: nsFlexContainerFrame::IsHorizontal() michael@0: { michael@0: const FlexboxAxisTracker axisTracker(this); michael@0: return IsAxisHorizontal(axisTracker.GetMainAxis()); michael@0: } michael@0: michael@0: FlexItem* michael@0: nsFlexContainerFrame::GenerateFlexItemForChild( michael@0: nsPresContext* aPresContext, michael@0: nsIFrame* aChildFrame, michael@0: const nsHTMLReflowState& aParentReflowState, michael@0: const FlexboxAxisTracker& aAxisTracker) michael@0: { michael@0: // Create temporary reflow state just for sizing -- to get hypothetical michael@0: // main-size and the computed values of min / max main-size property. michael@0: // (This reflow state will _not_ be used for reflow.) michael@0: nsHTMLReflowState childRS(aPresContext, aParentReflowState, aChildFrame, michael@0: nsSize(aParentReflowState.ComputedWidth(), michael@0: aParentReflowState.ComputedHeight())); michael@0: michael@0: // FLEX GROW & SHRINK WEIGHTS michael@0: // -------------------------- michael@0: const nsStylePosition* stylePos = aChildFrame->StylePosition(); michael@0: float flexGrow = stylePos->mFlexGrow; michael@0: float flexShrink = stylePos->mFlexShrink; michael@0: michael@0: // MAIN SIZES (flex base size, min/max size) michael@0: // ----------------------------------------- michael@0: nscoord flexBaseSize = GET_MAIN_COMPONENT(aAxisTracker, michael@0: childRS.ComputedWidth(), michael@0: childRS.ComputedHeight()); michael@0: nscoord mainMinSize = GET_MAIN_COMPONENT(aAxisTracker, michael@0: childRS.ComputedMinWidth(), michael@0: childRS.ComputedMinHeight()); michael@0: nscoord mainMaxSize = GET_MAIN_COMPONENT(aAxisTracker, michael@0: childRS.ComputedMaxWidth(), michael@0: childRS.ComputedMaxHeight()); michael@0: // This is enforced by the nsHTMLReflowState where these values come from: michael@0: MOZ_ASSERT(mainMinSize <= mainMaxSize, "min size is larger than max size"); michael@0: michael@0: // CROSS MIN/MAX SIZE michael@0: // ------------------ michael@0: michael@0: nscoord crossMinSize = GET_CROSS_COMPONENT(aAxisTracker, michael@0: childRS.ComputedMinWidth(), michael@0: childRS.ComputedMinHeight()); michael@0: nscoord crossMaxSize = GET_CROSS_COMPONENT(aAxisTracker, michael@0: childRS.ComputedMaxWidth(), michael@0: childRS.ComputedMaxHeight()); michael@0: michael@0: // SPECIAL-CASE FOR WIDGET-IMPOSED SIZES michael@0: // Check if we're a themed widget, in which case we might have a minimum michael@0: // main & cross size imposed by our widget (which we can't go below), or michael@0: // (more severe) our widget might have only a single valid size. michael@0: bool isFixedSizeWidget = false; michael@0: const nsStyleDisplay* disp = aChildFrame->StyleDisplay(); michael@0: if (aChildFrame->IsThemed(disp)) { michael@0: nsIntSize widgetMinSize(0, 0); michael@0: bool canOverride = true; michael@0: aPresContext->GetTheme()-> michael@0: GetMinimumWidgetSize(childRS.rendContext, aChildFrame, michael@0: disp->mAppearance, michael@0: &widgetMinSize, &canOverride); michael@0: michael@0: nscoord widgetMainMinSize = michael@0: aPresContext->DevPixelsToAppUnits( michael@0: aAxisTracker.GetMainComponent(widgetMinSize)); michael@0: nscoord widgetCrossMinSize = michael@0: aPresContext->DevPixelsToAppUnits( michael@0: aAxisTracker.GetCrossComponent(widgetMinSize)); michael@0: michael@0: // GMWS() returns border-box. We need content-box, so subtract michael@0: // borderPadding (but don't let that push our min sizes below 0). michael@0: nsMargin& bp = childRS.ComputedPhysicalBorderPadding(); michael@0: widgetMainMinSize = std::max(widgetMainMinSize - michael@0: aAxisTracker.GetMarginSizeInMainAxis(bp), 0); michael@0: widgetCrossMinSize = std::max(widgetCrossMinSize - michael@0: aAxisTracker.GetMarginSizeInCrossAxis(bp), 0); michael@0: michael@0: if (!canOverride) { michael@0: // Fixed-size widget: freeze our main-size at the widget's mandated size. michael@0: // (Set min and max main-sizes to that size, too, to keep us from michael@0: // clamping to any other size later on.) michael@0: flexBaseSize = mainMinSize = mainMaxSize = widgetMainMinSize; michael@0: crossMinSize = crossMaxSize = widgetCrossMinSize; michael@0: isFixedSizeWidget = true; michael@0: } else { michael@0: // Variable-size widget: ensure our min/max sizes are at least as large michael@0: // as the widget's mandated minimum size, so we don't flex below that. michael@0: mainMinSize = std::max(mainMinSize, widgetMainMinSize); michael@0: mainMaxSize = std::max(mainMaxSize, widgetMainMinSize); michael@0: michael@0: crossMinSize = std::max(crossMinSize, widgetCrossMinSize); michael@0: crossMaxSize = std::max(crossMaxSize, widgetCrossMinSize); michael@0: } michael@0: } michael@0: michael@0: // Construct the flex item! michael@0: FlexItem* item = new FlexItem(aChildFrame, michael@0: flexGrow, flexShrink, flexBaseSize, michael@0: mainMinSize, mainMaxSize, michael@0: crossMinSize, crossMaxSize, michael@0: childRS.ComputedPhysicalMargin(), michael@0: childRS.ComputedPhysicalBorderPadding(), michael@0: aAxisTracker); michael@0: michael@0: // If we're inflexible, we can just freeze to our hypothetical main-size michael@0: // up-front. Similarly, if we're a fixed-size widget, we only have one michael@0: // valid size, so we freeze to keep ourselves from flexing. michael@0: if (isFixedSizeWidget || (flexGrow == 0.0f && flexShrink == 0.0f)) { michael@0: item->Freeze(); michael@0: } michael@0: michael@0: return item; michael@0: } michael@0: michael@0: nsresult michael@0: nsFlexContainerFrame:: michael@0: ResolveFlexItemMaxContentSizing(nsPresContext* aPresContext, michael@0: FlexItem& aFlexItem, michael@0: const nsHTMLReflowState& aParentReflowState, michael@0: const FlexboxAxisTracker& aAxisTracker) michael@0: { michael@0: if (IsAxisHorizontal(aAxisTracker.GetMainAxis())) { michael@0: // Nothing to do -- this function is only for measuring flex items michael@0: // in a vertical flex container. michael@0: return NS_OK; michael@0: } michael@0: michael@0: if (NS_AUTOHEIGHT != aFlexItem.GetFlexBaseSize()) { michael@0: // Nothing to do; this function's only relevant for flex items michael@0: // with a base size of "auto" (or equivalent). michael@0: // XXXdholbert If & when we handle "min-height: min-content" for flex items, michael@0: // we'll want to resolve that in this function, too. michael@0: return NS_OK; michael@0: } michael@0: michael@0: // If we get here, we're vertical and our main size ended up being michael@0: // unconstrained. We need to use our "max-content" height, which is what we michael@0: // get from reflowing into our available width. michael@0: // Note: This has to come *after* we construct the FlexItem, since we michael@0: // invoke at least one convenience method (ResolveStretchedCrossSize) which michael@0: // requires a FlexItem. michael@0: michael@0: // Give the item a special reflow with "mIsFlexContainerMeasuringHeight" michael@0: // set. This tells it to behave as if it had "height: auto", regardless michael@0: // of what the "height" property is actually set to. michael@0: nsHTMLReflowState michael@0: childRSForMeasuringHeight(aPresContext, aParentReflowState, michael@0: aFlexItem.Frame(), michael@0: nsSize(aParentReflowState.ComputedWidth(), michael@0: NS_UNCONSTRAINEDSIZE), michael@0: -1, -1, nsHTMLReflowState::CALLER_WILL_INIT); michael@0: childRSForMeasuringHeight.mFlags.mIsFlexContainerMeasuringHeight = true; michael@0: childRSForMeasuringHeight.Init(aPresContext); michael@0: michael@0: aFlexItem.ResolveStretchedCrossSize(aParentReflowState.ComputedWidth(), michael@0: aAxisTracker); michael@0: if (aFlexItem.IsStretched()) { michael@0: childRSForMeasuringHeight.SetComputedWidth(aFlexItem.GetCrossSize()); michael@0: childRSForMeasuringHeight.mFlags.mHResize = true; michael@0: } michael@0: michael@0: // If this item is flexible (vertically), then we assume that the michael@0: // computed-height we're reflowing with now could be different michael@0: // from the one we'll use for this flex item's "actual" reflow later on. michael@0: // In that case, we need to be sure the flex item treats this as a michael@0: // vertical resize, even though none of its ancestors are necessarily michael@0: // being vertically resized. michael@0: // (Note: We don't have to do this for width, because InitResizeFlags michael@0: // will always turn on mHResize on when it sees that the computed width michael@0: // is different from current width, and that's all we need.) michael@0: if (!aFlexItem.IsFrozen()) { // Are we flexible? michael@0: childRSForMeasuringHeight.mFlags.mVResize = true; michael@0: } michael@0: michael@0: nsHTMLReflowMetrics childDesiredSize(childRSForMeasuringHeight); michael@0: nsReflowStatus childReflowStatus; michael@0: const uint32_t flags = NS_FRAME_NO_MOVE_FRAME; michael@0: nsresult rv = ReflowChild(aFlexItem.Frame(), aPresContext, michael@0: childDesiredSize, childRSForMeasuringHeight, michael@0: 0, 0, flags, childReflowStatus); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: MOZ_ASSERT(NS_FRAME_IS_COMPLETE(childReflowStatus), michael@0: "We gave flex item unconstrained available height, so it " michael@0: "should be complete"); michael@0: michael@0: rv = FinishReflowChild(aFlexItem.Frame(), aPresContext, michael@0: childDesiredSize, &childRSForMeasuringHeight, michael@0: 0, 0, flags); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Subtract border/padding in vertical axis, to get _just_ michael@0: // the effective computed value of the "height" property. michael@0: nscoord childDesiredHeight = childDesiredSize.Height() - michael@0: childRSForMeasuringHeight.ComputedPhysicalBorderPadding().TopBottom(); michael@0: childDesiredHeight = std::max(0, childDesiredHeight); michael@0: michael@0: aFlexItem.SetFlexBaseSizeAndMainSize(childDesiredHeight); michael@0: aFlexItem.SetHadMeasuringReflow(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: FlexItem::FlexItem(nsIFrame* aChildFrame, michael@0: float aFlexGrow, float aFlexShrink, nscoord aFlexBaseSize, michael@0: nscoord aMainMinSize, nscoord aMainMaxSize, michael@0: nscoord aCrossMinSize, nscoord aCrossMaxSize, michael@0: nsMargin aMargin, nsMargin aBorderPadding, michael@0: const FlexboxAxisTracker& aAxisTracker) michael@0: : mFrame(aChildFrame), michael@0: mFlexGrow(aFlexGrow), michael@0: mFlexShrink(aFlexShrink), michael@0: mBorderPadding(aBorderPadding), michael@0: mMargin(aMargin), michael@0: mMainMinSize(aMainMinSize), michael@0: mMainMaxSize(aMainMaxSize), michael@0: mCrossMinSize(aCrossMinSize), michael@0: mCrossMaxSize(aCrossMaxSize), michael@0: mMainPosn(0), michael@0: mCrossSize(0), michael@0: mCrossPosn(0), michael@0: mAscent(0), michael@0: mShareOfFlexWeightSoFar(0.0f), michael@0: mIsFrozen(false), michael@0: mHadMinViolation(false), michael@0: mHadMaxViolation(false), michael@0: mHadMeasuringReflow(false), michael@0: mIsStretched(false), michael@0: mIsStrut(false), michael@0: mAlignSelf(aChildFrame->StylePosition()->mAlignSelf) michael@0: { michael@0: MOZ_ASSERT(mFrame, "expecting a non-null child frame"); michael@0: MOZ_ASSERT(mFrame->GetType() != nsGkAtoms::placeholderFrame, michael@0: "placeholder frames should not be treated as flex items"); michael@0: MOZ_ASSERT(!(mFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW), michael@0: "out-of-flow frames should not be treated as flex items"); michael@0: michael@0: SetFlexBaseSizeAndMainSize(aFlexBaseSize); michael@0: michael@0: // Assert that any "auto" margin components are set to 0. michael@0: // (We'll resolve them later; until then, we want to treat them as 0-sized.) michael@0: #ifdef DEBUG michael@0: { michael@0: const nsStyleSides& styleMargin = mFrame->StyleMargin()->mMargin; michael@0: NS_FOR_CSS_SIDES(side) { michael@0: if (styleMargin.GetUnit(side) == eStyleUnit_Auto) { michael@0: MOZ_ASSERT(GetMarginComponentForSide(side) == 0, michael@0: "Someone else tried to resolve our auto margin"); michael@0: } michael@0: } michael@0: } michael@0: #endif // DEBUG michael@0: michael@0: // Resolve "align-self: auto" to parent's "align-items" value. michael@0: if (mAlignSelf == NS_STYLE_ALIGN_SELF_AUTO) { michael@0: mAlignSelf = michael@0: mFrame->StyleContext()->GetParent()->StylePosition()->mAlignItems; michael@0: } michael@0: michael@0: // If the flex item's inline axis is the same as the cross axis, then michael@0: // 'align-self:baseline' is identical to 'flex-start'. If that's the case, we michael@0: // just directly convert our align-self value here, so that we don't have to michael@0: // handle this with special cases elsewhere. michael@0: // Moreover: for the time being (until we support writing-modes), michael@0: // all inline axes are horizontal -- so we can just check if the cross axis michael@0: // is horizontal. michael@0: // FIXME: Once we support writing-mode (vertical text), this IsAxisHorizontal michael@0: // check won't be sufficient anymore -- we'll actually need to compare our michael@0: // inline axis vs. the cross axis. michael@0: if (mAlignSelf == NS_STYLE_ALIGN_ITEMS_BASELINE && michael@0: IsAxisHorizontal(aAxisTracker.GetCrossAxis())) { michael@0: mAlignSelf = NS_STYLE_ALIGN_ITEMS_FLEX_START; michael@0: } michael@0: } michael@0: michael@0: // Simplified constructor for creating a special "strut" FlexItem, for a child michael@0: // with visibility:collapse. The strut has 0 main-size, and it only exists to michael@0: // impose a minimum cross size on whichever FlexLine it ends up in. michael@0: FlexItem::FlexItem(nsIFrame* aChildFrame, nscoord aCrossSize) michael@0: : mFrame(aChildFrame), michael@0: mFlexGrow(0.0f), michael@0: mFlexShrink(0.0f), michael@0: // mBorderPadding uses default constructor, michael@0: // mMargin uses default constructor, michael@0: mFlexBaseSize(0), michael@0: mMainMinSize(0), michael@0: mMainMaxSize(0), michael@0: mCrossMinSize(0), michael@0: mCrossMaxSize(0), michael@0: mMainSize(0), michael@0: mMainPosn(0), michael@0: mCrossSize(aCrossSize), michael@0: mCrossPosn(0), michael@0: mAscent(0), michael@0: mShareOfFlexWeightSoFar(0.0f), michael@0: mIsFrozen(true), michael@0: mHadMinViolation(false), michael@0: mHadMaxViolation(false), michael@0: mHadMeasuringReflow(false), michael@0: mIsStretched(false), michael@0: mIsStrut(true), // (this is the constructor for making struts, after all) michael@0: mAlignSelf(NS_STYLE_ALIGN_ITEMS_FLEX_START) michael@0: { michael@0: MOZ_ASSERT(mFrame, "expecting a non-null child frame"); michael@0: MOZ_ASSERT(NS_STYLE_VISIBILITY_COLLAPSE == michael@0: mFrame->StyleVisibility()->mVisible, michael@0: "Should only make struts for children with 'visibility:collapse'"); michael@0: MOZ_ASSERT(mFrame->GetType() != nsGkAtoms::placeholderFrame, michael@0: "placeholder frames should not be treated as flex items"); michael@0: MOZ_ASSERT(!(mFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW), michael@0: "out-of-flow frames should not be treated as flex items"); michael@0: } michael@0: michael@0: nscoord michael@0: FlexItem::GetBaselineOffsetFromOuterCrossEdge(AxisOrientationType aCrossAxis, michael@0: AxisEdgeType aEdge) const michael@0: { michael@0: // NOTE: Currently, 'mAscent' (taken from reflow) is an inherently vertical michael@0: // measurement -- it's the distance from the border-top edge of this FlexItem michael@0: // to its baseline. So, we can really only do baseline alignment when the michael@0: // cross axis is vertical. (The FlexItem constructor enforces this when michael@0: // resolving the item's "mAlignSelf" value). michael@0: MOZ_ASSERT(!IsAxisHorizontal(aCrossAxis), michael@0: "Only expecting to be doing baseline computations when the " michael@0: "cross axis is vertical"); michael@0: michael@0: Side sideToMeasureFrom = kAxisOrientationToSidesMap[aCrossAxis][aEdge]; michael@0: michael@0: nscoord marginTopToBaseline = mAscent + mMargin.top; michael@0: michael@0: if (sideToMeasureFrom == eSideTop) { michael@0: // Measuring from top (normal case): the distance from the margin-box top michael@0: // edge to the baseline is just ascent + margin-top. michael@0: return marginTopToBaseline; michael@0: } michael@0: michael@0: MOZ_ASSERT(sideToMeasureFrom == eSideBottom, michael@0: "We already checked that we're dealing with a vertical axis, and " michael@0: "we're not using the top side, so that only leaves the bottom..."); michael@0: michael@0: // Measuring from bottom: The distance from the margin-box bottom edge to the michael@0: // baseline is just the margin-box cross size (i.e. outer cross size), minus michael@0: // the already-computed distance from margin-top to baseline. michael@0: return GetOuterCrossSize(aCrossAxis) - marginTopToBaseline; michael@0: } michael@0: michael@0: uint32_t michael@0: FlexItem::GetNumAutoMarginsInAxis(AxisOrientationType aAxis) const michael@0: { michael@0: uint32_t numAutoMargins = 0; michael@0: const nsStyleSides& styleMargin = mFrame->StyleMargin()->mMargin; michael@0: for (uint32_t i = 0; i < eNumAxisEdges; i++) { michael@0: Side side = kAxisOrientationToSidesMap[aAxis][i]; michael@0: if (styleMargin.GetUnit(side) == eStyleUnit_Auto) { michael@0: numAutoMargins++; michael@0: } michael@0: } michael@0: michael@0: // Mostly for clarity: michael@0: MOZ_ASSERT(numAutoMargins <= 2, michael@0: "We're just looking at one item along one dimension, so we " michael@0: "should only have examined 2 margins"); michael@0: michael@0: return numAutoMargins; michael@0: } michael@0: michael@0: // Keeps track of our position along a particular axis (where a '0' position michael@0: // corresponds to the 'start' edge of that axis). michael@0: // This class shouldn't be instantiated directly -- rather, it should only be michael@0: // instantiated via its subclasses defined below. michael@0: class MOZ_STACK_CLASS PositionTracker { michael@0: public: michael@0: // Accessor for the current value of the position that we're tracking. michael@0: inline nscoord GetPosition() const { return mPosition; } michael@0: inline AxisOrientationType GetAxis() const { return mAxis; } michael@0: michael@0: // Advances our position across the start edge of the given margin, in the michael@0: // axis we're tracking. michael@0: void EnterMargin(const nsMargin& aMargin) michael@0: { michael@0: Side side = kAxisOrientationToSidesMap[mAxis][eAxisEdge_Start]; michael@0: mPosition += MarginComponentForSide(aMargin, side); michael@0: } michael@0: michael@0: // Advances our position across the end edge of the given margin, in the axis michael@0: // we're tracking. michael@0: void ExitMargin(const nsMargin& aMargin) michael@0: { michael@0: Side side = kAxisOrientationToSidesMap[mAxis][eAxisEdge_End]; michael@0: mPosition += MarginComponentForSide(aMargin, side); michael@0: } michael@0: michael@0: // Advances our current position from the start side of a child frame's michael@0: // border-box to the frame's upper or left edge (depending on our axis). michael@0: // (Note that this is a no-op if our axis grows in positive direction.) michael@0: void EnterChildFrame(nscoord aChildFrameSize) michael@0: { michael@0: if (!AxisGrowsInPositiveDirection(mAxis)) michael@0: mPosition += aChildFrameSize; michael@0: } michael@0: michael@0: // Advances our current position from a frame's upper or left border-box edge michael@0: // (whichever is in the axis we're tracking) to the 'end' side of the frame michael@0: // in the axis that we're tracking. (Note that this is a no-op if our axis michael@0: // grows in the negative direction.) michael@0: void ExitChildFrame(nscoord aChildFrameSize) michael@0: { michael@0: if (AxisGrowsInPositiveDirection(mAxis)) michael@0: mPosition += aChildFrameSize; michael@0: } michael@0: michael@0: protected: michael@0: // Protected constructor, to be sure we're only instantiated via a subclass. michael@0: PositionTracker(AxisOrientationType aAxis) michael@0: : mPosition(0), michael@0: mAxis(aAxis) michael@0: {} michael@0: michael@0: private: michael@0: // Private copy-constructor, since we don't want any instances of our michael@0: // subclasses to be accidentally copied. michael@0: PositionTracker(const PositionTracker& aOther) michael@0: : mPosition(aOther.mPosition), michael@0: mAxis(aOther.mAxis) michael@0: {} michael@0: michael@0: protected: michael@0: // Member data: michael@0: nscoord mPosition; // The position we're tracking michael@0: const AxisOrientationType mAxis; // The axis along which we're moving michael@0: }; michael@0: michael@0: // Tracks our position in the main axis, when we're laying out flex items. michael@0: // The "0" position represents the main-start edge of the flex container's michael@0: // content-box. michael@0: class MOZ_STACK_CLASS MainAxisPositionTracker : public PositionTracker { michael@0: public: michael@0: MainAxisPositionTracker(const FlexboxAxisTracker& aAxisTracker, michael@0: const FlexLine* aLine, michael@0: uint8_t aJustifyContent, michael@0: nscoord aContentBoxMainSize); michael@0: michael@0: ~MainAxisPositionTracker() { michael@0: MOZ_ASSERT(mNumPackingSpacesRemaining == 0, michael@0: "miscounted the number of packing spaces"); michael@0: MOZ_ASSERT(mNumAutoMarginsInMainAxis == 0, michael@0: "miscounted the number of auto margins"); michael@0: } michael@0: michael@0: // Advances past the packing space (if any) between two flex items michael@0: void TraversePackingSpace(); michael@0: michael@0: // If aItem has any 'auto' margins in the main axis, this method updates the michael@0: // corresponding values in its margin. michael@0: void ResolveAutoMarginsInMainAxis(FlexItem& aItem); michael@0: michael@0: private: michael@0: nscoord mPackingSpaceRemaining; michael@0: uint32_t mNumAutoMarginsInMainAxis; michael@0: uint32_t mNumPackingSpacesRemaining; michael@0: uint8_t mJustifyContent; michael@0: }; michael@0: michael@0: // Utility class for managing our position along the cross axis along michael@0: // the whole flex container (at a higher level than a single line). michael@0: // The "0" position represents the cross-start edge of the flex container's michael@0: // content-box. michael@0: class MOZ_STACK_CLASS CrossAxisPositionTracker : public PositionTracker { michael@0: public: michael@0: CrossAxisPositionTracker(FlexLine* aFirstLine, michael@0: uint8_t aAlignContent, michael@0: nscoord aContentBoxCrossSize, michael@0: bool aIsCrossSizeDefinite, michael@0: const FlexboxAxisTracker& aAxisTracker); michael@0: michael@0: // Advances past the packing space (if any) between two flex lines michael@0: void TraversePackingSpace(); michael@0: michael@0: // Advances past the given FlexLine michael@0: void TraverseLine(FlexLine& aLine) { mPosition += aLine.GetLineCrossSize(); } michael@0: michael@0: private: michael@0: // Redeclare the frame-related methods from PositionTracker as private with michael@0: // MOZ_DELETE, to be sure (at compile time) that no client code can invoke michael@0: // them. (Unlike the other PositionTracker derived classes, this class here michael@0: // deals with FlexLines, not with individual FlexItems or frames.) michael@0: void EnterMargin(const nsMargin& aMargin) MOZ_DELETE; michael@0: void ExitMargin(const nsMargin& aMargin) MOZ_DELETE; michael@0: void EnterChildFrame(nscoord aChildFrameSize) MOZ_DELETE; michael@0: void ExitChildFrame(nscoord aChildFrameSize) MOZ_DELETE; michael@0: michael@0: nscoord mPackingSpaceRemaining; michael@0: uint32_t mNumPackingSpacesRemaining; michael@0: uint8_t mAlignContent; michael@0: }; michael@0: michael@0: // Utility class for managing our position along the cross axis, *within* a michael@0: // single flex line. michael@0: class MOZ_STACK_CLASS SingleLineCrossAxisPositionTracker : public PositionTracker { michael@0: public: michael@0: SingleLineCrossAxisPositionTracker(const FlexboxAxisTracker& aAxisTracker); michael@0: michael@0: void ResolveAutoMarginsInCrossAxis(const FlexLine& aLine, michael@0: FlexItem& aItem); michael@0: michael@0: void EnterAlignPackingSpace(const FlexLine& aLine, michael@0: const FlexItem& aItem, michael@0: const FlexboxAxisTracker& aAxisTracker); michael@0: michael@0: // Resets our position to the cross-start edge of this line. michael@0: inline void ResetPosition() { mPosition = 0; } michael@0: }; michael@0: michael@0: //---------------------------------------------------------------------- michael@0: michael@0: // Frame class boilerplate michael@0: // ======================= michael@0: michael@0: NS_QUERYFRAME_HEAD(nsFlexContainerFrame) michael@0: NS_QUERYFRAME_ENTRY(nsFlexContainerFrame) michael@0: NS_QUERYFRAME_TAIL_INHERITING(nsFlexContainerFrameSuper) michael@0: michael@0: NS_IMPL_FRAMEARENA_HELPERS(nsFlexContainerFrame) michael@0: michael@0: nsIFrame* michael@0: NS_NewFlexContainerFrame(nsIPresShell* aPresShell, michael@0: nsStyleContext* aContext) michael@0: { michael@0: return new (aPresShell) nsFlexContainerFrame(aContext); michael@0: } michael@0: michael@0: //---------------------------------------------------------------------- michael@0: michael@0: // nsFlexContainerFrame Method Implementations michael@0: // =========================================== michael@0: michael@0: /* virtual */ michael@0: nsFlexContainerFrame::~nsFlexContainerFrame() michael@0: { michael@0: } michael@0: michael@0: template michael@0: /* static */ bool michael@0: nsFlexContainerFrame::SortChildrenIfNeeded() michael@0: { michael@0: if (nsIFrame::IsFrameListSorted(mFrames)) { michael@0: return false; michael@0: } michael@0: michael@0: nsIFrame::SortFrameList(mFrames); michael@0: return true; michael@0: } michael@0: michael@0: /* virtual */ michael@0: nsIAtom* michael@0: nsFlexContainerFrame::GetType() const michael@0: { michael@0: return nsGkAtoms::flexContainerFrame; michael@0: } michael@0: michael@0: #ifdef DEBUG_FRAME_DUMP michael@0: nsresult michael@0: nsFlexContainerFrame::GetFrameName(nsAString& aResult) const michael@0: { michael@0: return MakeFrameName(NS_LITERAL_STRING("FlexContainer"), aResult); michael@0: } michael@0: #endif michael@0: michael@0: // Helper for BuildDisplayList, to implement this special-case for flex items michael@0: // from the spec: michael@0: // Flex items paint exactly the same as block-level elements in the michael@0: // normal flow, except that 'z-index' values other than 'auto' create michael@0: // a stacking context even if 'position' is 'static'. michael@0: // http://www.w3.org/TR/2012/CR-css3-flexbox-20120918/#painting michael@0: uint32_t michael@0: GetDisplayFlagsForFlexItem(nsIFrame* aFrame) michael@0: { michael@0: MOZ_ASSERT(aFrame->IsFlexItem(), "Should only be called on flex items"); michael@0: michael@0: const nsStylePosition* pos = aFrame->StylePosition(); michael@0: if (pos->mZIndex.GetUnit() == eStyleUnit_Integer) { michael@0: return nsIFrame::DISPLAY_CHILD_FORCE_STACKING_CONTEXT; michael@0: } michael@0: return nsIFrame::DISPLAY_CHILD_FORCE_PSEUDO_STACKING_CONTEXT; michael@0: } michael@0: michael@0: void michael@0: nsFlexContainerFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder, michael@0: const nsRect& aDirtyRect, michael@0: const nsDisplayListSet& aLists) michael@0: { michael@0: NS_ASSERTION( michael@0: nsIFrame::IsFrameListSorted(mFrames), michael@0: "Child frames aren't sorted correctly"); michael@0: michael@0: DisplayBorderBackgroundOutline(aBuilder, aLists); michael@0: michael@0: // Our children are all block-level, so their borders/backgrounds all go on michael@0: // the BlockBorderBackgrounds list. michael@0: nsDisplayListSet childLists(aLists, aLists.BlockBorderBackgrounds()); michael@0: for (nsFrameList::Enumerator e(mFrames); !e.AtEnd(); e.Next()) { michael@0: BuildDisplayListForChild(aBuilder, e.get(), aDirtyRect, childLists, michael@0: GetDisplayFlagsForFlexItem(e.get())); michael@0: } michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: // helper for the debugging method below michael@0: bool michael@0: FrameWantsToBeInAnonymousFlexItem(nsIFrame* aFrame) michael@0: { michael@0: // Note: This needs to match the logic in michael@0: // nsCSSFrameConstructor::FrameConstructionItem::NeedsAnonFlexItem() michael@0: return (aFrame->IsFrameOfType(nsIFrame::eLineParticipant) || michael@0: nsGkAtoms::placeholderFrame == aFrame->GetType()); michael@0: } michael@0: michael@0: // Debugging method, to let us assert that our anonymous flex items are michael@0: // set up correctly -- in particular, we assert: michael@0: // (1) we don't have any inline non-replaced children michael@0: // (2) we don't have any consecutive anonymous flex items michael@0: // (3) we don't have any empty anonymous flex items michael@0: // michael@0: // XXXdholbert This matches what nsCSSFrameConstructor currently does, and what michael@0: // the spec used to say. However, the spec has now changed regarding what michael@0: // types of content get wrapped in an anonymous flexbox item. The patch that michael@0: // implements those changes (in nsCSSFrameConstructor) will need to change michael@0: // this method as well. michael@0: void michael@0: nsFlexContainerFrame::SanityCheckAnonymousFlexItems() const michael@0: { michael@0: bool prevChildWasAnonFlexItem = false; michael@0: for (nsIFrame* child = mFrames.FirstChild(); child; michael@0: child = child->GetNextSibling()) { michael@0: MOZ_ASSERT(!FrameWantsToBeInAnonymousFlexItem(child), michael@0: "frame wants to be inside an anonymous flex item, " michael@0: "but it isn't"); michael@0: if (child->StyleContext()->GetPseudo() == michael@0: nsCSSAnonBoxes::anonymousFlexItem) { michael@0: MOZ_ASSERT(!prevChildWasAnonFlexItem || michael@0: HasAnyStateBits(NS_STATE_FLEX_CHILDREN_REORDERED), michael@0: "two anon flex items in a row (shouldn't happen, unless our " michael@0: "children have been reordered with the 'order' property)"); michael@0: michael@0: nsIFrame* firstWrappedChild = child->GetFirstPrincipalChild(); michael@0: MOZ_ASSERT(firstWrappedChild, michael@0: "anonymous flex item is empty (shouldn't happen)"); michael@0: prevChildWasAnonFlexItem = true; michael@0: } else { michael@0: prevChildWasAnonFlexItem = false; michael@0: } michael@0: } michael@0: } michael@0: #endif // DEBUG michael@0: michael@0: // Based on the sign of aTotalViolation, this function freezes a subset of our michael@0: // flexible sizes, and restores the remaining ones to their initial pref sizes. michael@0: void michael@0: FlexLine::FreezeOrRestoreEachFlexibleSize(const nscoord aTotalViolation, michael@0: bool aIsFinalIteration) michael@0: { michael@0: enum FreezeType { michael@0: eFreezeEverything, michael@0: eFreezeMinViolations, michael@0: eFreezeMaxViolations michael@0: }; michael@0: michael@0: FreezeType freezeType; michael@0: if (aTotalViolation == 0) { michael@0: freezeType = eFreezeEverything; michael@0: } else if (aTotalViolation > 0) { michael@0: freezeType = eFreezeMinViolations; michael@0: } else { // aTotalViolation < 0 michael@0: freezeType = eFreezeMaxViolations; michael@0: } michael@0: michael@0: for (FlexItem* item = mItems.getFirst(); item; item = item->getNext()) { michael@0: MOZ_ASSERT(!item->HadMinViolation() || !item->HadMaxViolation(), michael@0: "Can have either min or max violation, but not both"); michael@0: michael@0: if (!item->IsFrozen()) { michael@0: if (eFreezeEverything == freezeType || michael@0: (eFreezeMinViolations == freezeType && item->HadMinViolation()) || michael@0: (eFreezeMaxViolations == freezeType && item->HadMaxViolation())) { michael@0: michael@0: MOZ_ASSERT(item->GetMainSize() >= item->GetMainMinSize(), michael@0: "Freezing item at a size below its minimum"); michael@0: MOZ_ASSERT(item->GetMainSize() <= item->GetMainMaxSize(), michael@0: "Freezing item at a size above its maximum"); michael@0: michael@0: item->Freeze(); michael@0: } else if (MOZ_UNLIKELY(aIsFinalIteration)) { michael@0: // XXXdholbert If & when bug 765861 is fixed, we should upgrade this michael@0: // assertion to be fatal except in documents with enormous lengths. michael@0: NS_ERROR("Final iteration still has unfrozen items, this shouldn't" michael@0: " happen unless there was nscoord under/overflow."); michael@0: item->Freeze(); michael@0: } // else, we'll reset this item's main size to its flex base size on the michael@0: // next iteration of this algorithm. michael@0: michael@0: // Clear this item's violation(s), now that we've dealt with them michael@0: item->ClearViolationFlags(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: FlexLine::ResolveFlexibleLengths(nscoord aFlexContainerMainSize) michael@0: { michael@0: PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG, ("ResolveFlexibleLengths\n")); michael@0: if (IsEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: // Subtract space occupied by our items' margins/borders/padding, so we can michael@0: // just be dealing with the space available for our flex items' content michael@0: // boxes. michael@0: nscoord spaceReservedForMarginBorderPadding = michael@0: mTotalOuterHypotheticalMainSize - mTotalInnerHypotheticalMainSize; michael@0: michael@0: nscoord spaceAvailableForFlexItemsContentBoxes = michael@0: aFlexContainerMainSize - spaceReservedForMarginBorderPadding; michael@0: michael@0: // Determine whether we're going to be growing or shrinking items. michael@0: const bool isUsingFlexGrow = michael@0: (mTotalOuterHypotheticalMainSize < aFlexContainerMainSize); michael@0: michael@0: // NOTE: I claim that this chunk of the algorithm (the looping part) needs to michael@0: // run the loop at MOST mNumItems times. This claim should hold up michael@0: // because we'll freeze at least one item on each loop iteration, and once michael@0: // we've run out of items to freeze, there's nothing left to do. However, michael@0: // in most cases, we'll break out of this loop long before we hit that many michael@0: // iterations. michael@0: for (uint32_t iterationCounter = 0; michael@0: iterationCounter < mNumItems; iterationCounter++) { michael@0: // Set every not-yet-frozen item's used main size to its michael@0: // flex base size, and subtract all the used main sizes from our michael@0: // total amount of space to determine the 'available free space' michael@0: // (positive or negative) to be distributed among our flexible items. michael@0: nscoord availableFreeSpace = spaceAvailableForFlexItemsContentBoxes; michael@0: for (FlexItem* item = mItems.getFirst(); item; item = item->getNext()) { michael@0: if (!item->IsFrozen()) { michael@0: item->SetMainSize(item->GetFlexBaseSize()); michael@0: } michael@0: availableFreeSpace -= item->GetMainSize(); michael@0: } michael@0: michael@0: PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG, michael@0: (" available free space = %d\n", availableFreeSpace)); michael@0: michael@0: // If sign of free space matches the type of flexing that we're doing, give michael@0: // each flexible item a portion of availableFreeSpace. michael@0: if ((availableFreeSpace > 0 && isUsingFlexGrow) || michael@0: (availableFreeSpace < 0 && !isUsingFlexGrow)) { michael@0: michael@0: // STRATEGY: On each item, we compute & store its "share" of the total michael@0: // flex weight that we've seen so far: michael@0: // curFlexWeight / runningFlexWeightSum michael@0: // michael@0: // Then, when we go to actually distribute the space (in the next loop), michael@0: // we can simply walk backwards through the elements and give each item michael@0: // its "share" multiplied by the remaining available space. michael@0: // michael@0: // SPECIAL CASE: If the sum of the flex weights is larger than the michael@0: // maximum representable float (overflowing to infinity), then we can't michael@0: // sensibly divide out proportional shares anymore. In that case, we michael@0: // simply treat the flex item(s) with the largest flex weights as if michael@0: // their weights were infinite (dwarfing all the others), and we michael@0: // distribute all of the available space among them. michael@0: float runningFlexWeightSum = 0.0f; michael@0: float largestFlexWeight = 0.0f; michael@0: uint32_t numItemsWithLargestFlexWeight = 0; michael@0: for (FlexItem* item = mItems.getFirst(); item; item = item->getNext()) { michael@0: float curFlexWeight = item->GetFlexWeightToUse(isUsingFlexGrow); michael@0: MOZ_ASSERT(curFlexWeight >= 0.0f, "weights are non-negative"); michael@0: michael@0: runningFlexWeightSum += curFlexWeight; michael@0: if (NS_finite(runningFlexWeightSum)) { michael@0: if (curFlexWeight == 0.0f) { michael@0: item->SetShareOfFlexWeightSoFar(0.0f); michael@0: } else { michael@0: item->SetShareOfFlexWeightSoFar(curFlexWeight / michael@0: runningFlexWeightSum); michael@0: } michael@0: } // else, the sum of weights overflows to infinity, in which michael@0: // case we don't bother with "SetShareOfFlexWeightSoFar" since michael@0: // we know we won't use it. (instead, we'll just give every michael@0: // item with the largest flex weight an equal share of space.) michael@0: michael@0: // Update our largest-flex-weight tracking vars michael@0: if (curFlexWeight > largestFlexWeight) { michael@0: largestFlexWeight = curFlexWeight; michael@0: numItemsWithLargestFlexWeight = 1; michael@0: } else if (curFlexWeight == largestFlexWeight) { michael@0: numItemsWithLargestFlexWeight++; michael@0: } michael@0: } michael@0: michael@0: if (runningFlexWeightSum != 0.0f) { // no distribution if no flexibility michael@0: PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG, michael@0: (" Distributing available space:")); michael@0: // NOTE: It's important that we traverse our items in *reverse* order michael@0: // here, for correct width distribution according to the items' michael@0: // "ShareOfFlexWeightSoFar" progressively-calculated values. michael@0: for (FlexItem* item = mItems.getLast(); item; michael@0: item = item->getPrevious()) { michael@0: michael@0: if (!item->IsFrozen()) { michael@0: // To avoid rounding issues, we compute the change in size for this michael@0: // item, and then subtract it from the remaining available space. michael@0: nscoord sizeDelta = 0; michael@0: if (NS_finite(runningFlexWeightSum)) { michael@0: float myShareOfRemainingSpace = michael@0: item->GetShareOfFlexWeightSoFar(); michael@0: michael@0: MOZ_ASSERT(myShareOfRemainingSpace >= 0.0f && michael@0: myShareOfRemainingSpace <= 1.0f, michael@0: "my share should be nonnegative fractional amount"); michael@0: michael@0: if (myShareOfRemainingSpace == 1.0f) { michael@0: // (We special-case 1.0f to avoid float error from converting michael@0: // availableFreeSpace from integer*1.0f --> float --> integer) michael@0: sizeDelta = availableFreeSpace; michael@0: } else if (myShareOfRemainingSpace > 0.0f) { michael@0: sizeDelta = NSToCoordRound(availableFreeSpace * michael@0: myShareOfRemainingSpace); michael@0: } michael@0: } else if (item->GetFlexWeightToUse(isUsingFlexGrow) == michael@0: largestFlexWeight) { michael@0: // Total flexibility is infinite, so we're just distributing michael@0: // the available space equally among the items that are tied for michael@0: // having the largest weight (and this is one of those items). michael@0: sizeDelta = michael@0: NSToCoordRound(availableFreeSpace / michael@0: float(numItemsWithLargestFlexWeight)); michael@0: numItemsWithLargestFlexWeight--; michael@0: } michael@0: michael@0: availableFreeSpace -= sizeDelta; michael@0: michael@0: item->SetMainSize(item->GetMainSize() + sizeDelta); michael@0: PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG, michael@0: (" child %p receives %d, for a total of %d\n", michael@0: item, sizeDelta, item->GetMainSize())); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Fix min/max violations: michael@0: nscoord totalViolation = 0; // keeps track of adjustments for min/max michael@0: PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG, michael@0: (" Checking for violations:")); michael@0: michael@0: for (FlexItem* item = mItems.getFirst(); item; item = item->getNext()) { michael@0: if (!item->IsFrozen()) { michael@0: if (item->GetMainSize() < item->GetMainMinSize()) { michael@0: // min violation michael@0: totalViolation += item->GetMainMinSize() - item->GetMainSize(); michael@0: item->SetMainSize(item->GetMainMinSize()); michael@0: item->SetHadMinViolation(); michael@0: } else if (item->GetMainSize() > item->GetMainMaxSize()) { michael@0: // max violation michael@0: totalViolation += item->GetMainMaxSize() - item->GetMainSize(); michael@0: item->SetMainSize(item->GetMainMaxSize()); michael@0: item->SetHadMaxViolation(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: FreezeOrRestoreEachFlexibleSize(totalViolation, michael@0: iterationCounter + 1 == mNumItems); michael@0: michael@0: PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG, michael@0: (" Total violation: %d\n", totalViolation)); michael@0: michael@0: if (totalViolation == 0) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: // Post-condition: all lengths should've been frozen. michael@0: #ifdef DEBUG michael@0: for (const FlexItem* item = mItems.getFirst(); item; item = item->getNext()) { michael@0: MOZ_ASSERT(item->IsFrozen(), michael@0: "All flexible lengths should've been resolved"); michael@0: } michael@0: #endif // DEBUG michael@0: } michael@0: michael@0: MainAxisPositionTracker:: michael@0: MainAxisPositionTracker(const FlexboxAxisTracker& aAxisTracker, michael@0: const FlexLine* aLine, michael@0: uint8_t aJustifyContent, michael@0: nscoord aContentBoxMainSize) michael@0: : PositionTracker(aAxisTracker.GetMainAxis()), michael@0: mPackingSpaceRemaining(aContentBoxMainSize), // we chip away at this below michael@0: mNumAutoMarginsInMainAxis(0), michael@0: mNumPackingSpacesRemaining(0), michael@0: mJustifyContent(aJustifyContent) michael@0: { michael@0: // mPackingSpaceRemaining is initialized to the container's main size. Now michael@0: // we'll subtract out the main sizes of our flex items, so that it ends up michael@0: // with the *actual* amount of packing space. michael@0: for (const FlexItem* item = aLine->GetFirstItem(); item; michael@0: item = item->getNext()) { michael@0: mPackingSpaceRemaining -= item->GetOuterMainSize(mAxis); michael@0: mNumAutoMarginsInMainAxis += item->GetNumAutoMarginsInAxis(mAxis); michael@0: } michael@0: michael@0: if (mPackingSpaceRemaining <= 0) { michael@0: // No available packing space to use for resolving auto margins. michael@0: mNumAutoMarginsInMainAxis = 0; michael@0: } michael@0: michael@0: // If packing space is negative, 'space-between' behaves like 'flex-start', michael@0: // and 'space-around' behaves like 'center'. In those cases, it's simplest to michael@0: // just pretend we have a different 'justify-content' value and share code. michael@0: if (mPackingSpaceRemaining < 0) { michael@0: if (mJustifyContent == NS_STYLE_JUSTIFY_CONTENT_SPACE_BETWEEN) { michael@0: mJustifyContent = NS_STYLE_JUSTIFY_CONTENT_FLEX_START; michael@0: } else if (mJustifyContent == NS_STYLE_JUSTIFY_CONTENT_SPACE_AROUND) { michael@0: mJustifyContent = NS_STYLE_JUSTIFY_CONTENT_CENTER; michael@0: } michael@0: } michael@0: michael@0: // If our main axis is (internally) reversed, swap the justify-content michael@0: // "flex-start" and "flex-end" behaviors: michael@0: if (aAxisTracker.AreAxesInternallyReversed()) { michael@0: if (mJustifyContent == NS_STYLE_JUSTIFY_CONTENT_FLEX_START) { michael@0: mJustifyContent = NS_STYLE_JUSTIFY_CONTENT_FLEX_END; michael@0: } else if (mJustifyContent == NS_STYLE_JUSTIFY_CONTENT_FLEX_END) { michael@0: mJustifyContent = NS_STYLE_JUSTIFY_CONTENT_FLEX_START; michael@0: } michael@0: } michael@0: michael@0: // Figure out how much space we'll set aside for auto margins or michael@0: // packing spaces, and advance past any leading packing-space. michael@0: if (mNumAutoMarginsInMainAxis == 0 && michael@0: mPackingSpaceRemaining != 0 && michael@0: !aLine->IsEmpty()) { michael@0: switch (mJustifyContent) { michael@0: case NS_STYLE_JUSTIFY_CONTENT_FLEX_START: michael@0: // All packing space should go at the end --> nothing to do here. michael@0: break; michael@0: case NS_STYLE_JUSTIFY_CONTENT_FLEX_END: michael@0: // All packing space goes at the beginning michael@0: mPosition += mPackingSpaceRemaining; michael@0: break; michael@0: case NS_STYLE_JUSTIFY_CONTENT_CENTER: michael@0: // Half the packing space goes at the beginning michael@0: mPosition += mPackingSpaceRemaining / 2; michael@0: break; michael@0: case NS_STYLE_JUSTIFY_CONTENT_SPACE_BETWEEN: michael@0: MOZ_ASSERT(mPackingSpaceRemaining >= 0, michael@0: "negative packing space should make us use 'flex-start' " michael@0: "instead of 'space-between'"); michael@0: // 1 packing space between each flex item, no packing space at ends. michael@0: mNumPackingSpacesRemaining = aLine->NumItems() - 1; michael@0: break; michael@0: case NS_STYLE_JUSTIFY_CONTENT_SPACE_AROUND: michael@0: MOZ_ASSERT(mPackingSpaceRemaining >= 0, michael@0: "negative packing space should make us use 'center' " michael@0: "instead of 'space-around'"); michael@0: // 1 packing space between each flex item, plus half a packing space michael@0: // at beginning & end. So our number of full packing-spaces is equal michael@0: // to the number of flex items. michael@0: mNumPackingSpacesRemaining = aLine->NumItems(); michael@0: if (mNumPackingSpacesRemaining > 0) { michael@0: // The edges (start/end) share one full packing space michael@0: nscoord totalEdgePackingSpace = michael@0: mPackingSpaceRemaining / mNumPackingSpacesRemaining; michael@0: michael@0: // ...and we'll use half of that right now, at the start michael@0: mPosition += totalEdgePackingSpace / 2; michael@0: // ...but we need to subtract all of it right away, so that we won't michael@0: // hand out any of it to intermediate packing spaces. michael@0: mPackingSpaceRemaining -= totalEdgePackingSpace; michael@0: mNumPackingSpacesRemaining--; michael@0: } michael@0: break; michael@0: default: michael@0: MOZ_CRASH("Unexpected justify-content value"); michael@0: } michael@0: } michael@0: michael@0: MOZ_ASSERT(mNumPackingSpacesRemaining == 0 || michael@0: mNumAutoMarginsInMainAxis == 0, michael@0: "extra space should either go to packing space or to " michael@0: "auto margins, but not to both"); michael@0: } michael@0: michael@0: void michael@0: MainAxisPositionTracker::ResolveAutoMarginsInMainAxis(FlexItem& aItem) michael@0: { michael@0: if (mNumAutoMarginsInMainAxis) { michael@0: const nsStyleSides& styleMargin = aItem.Frame()->StyleMargin()->mMargin; michael@0: for (uint32_t i = 0; i < eNumAxisEdges; i++) { michael@0: Side side = kAxisOrientationToSidesMap[mAxis][i]; michael@0: if (styleMargin.GetUnit(side) == eStyleUnit_Auto) { michael@0: // NOTE: This integer math will skew the distribution of remainder michael@0: // app-units towards the end, which is fine. michael@0: nscoord curAutoMarginSize = michael@0: mPackingSpaceRemaining / mNumAutoMarginsInMainAxis; michael@0: michael@0: MOZ_ASSERT(aItem.GetMarginComponentForSide(side) == 0, michael@0: "Expecting auto margins to have value '0' before we " michael@0: "resolve them"); michael@0: aItem.SetMarginComponentForSide(side, curAutoMarginSize); michael@0: michael@0: mNumAutoMarginsInMainAxis--; michael@0: mPackingSpaceRemaining -= curAutoMarginSize; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: MainAxisPositionTracker::TraversePackingSpace() michael@0: { michael@0: if (mNumPackingSpacesRemaining) { michael@0: MOZ_ASSERT(mJustifyContent == NS_STYLE_JUSTIFY_CONTENT_SPACE_BETWEEN || michael@0: mJustifyContent == NS_STYLE_JUSTIFY_CONTENT_SPACE_AROUND, michael@0: "mNumPackingSpacesRemaining only applies for " michael@0: "space-between/space-around"); michael@0: michael@0: MOZ_ASSERT(mPackingSpaceRemaining >= 0, michael@0: "ran out of packing space earlier than we expected"); michael@0: michael@0: // NOTE: This integer math will skew the distribution of remainder michael@0: // app-units towards the end, which is fine. michael@0: nscoord curPackingSpace = michael@0: mPackingSpaceRemaining / mNumPackingSpacesRemaining; michael@0: michael@0: mPosition += curPackingSpace; michael@0: mNumPackingSpacesRemaining--; michael@0: mPackingSpaceRemaining -= curPackingSpace; michael@0: } michael@0: } michael@0: michael@0: CrossAxisPositionTracker:: michael@0: CrossAxisPositionTracker(FlexLine* aFirstLine, michael@0: uint8_t aAlignContent, michael@0: nscoord aContentBoxCrossSize, michael@0: bool aIsCrossSizeDefinite, michael@0: const FlexboxAxisTracker& aAxisTracker) michael@0: : PositionTracker(aAxisTracker.GetCrossAxis()), michael@0: mPackingSpaceRemaining(0), michael@0: mNumPackingSpacesRemaining(0), michael@0: mAlignContent(aAlignContent) michael@0: { michael@0: MOZ_ASSERT(aFirstLine, "null first line pointer"); michael@0: michael@0: if (aIsCrossSizeDefinite && !aFirstLine->getNext()) { michael@0: // "If the flex container has only a single line (even if it's a michael@0: // multi-line flex container) and has a definite cross size, the cross michael@0: // size of the flex line is the flex container's inner cross size." michael@0: // SOURCE: http://dev.w3.org/csswg/css-flexbox/#algo-line-break michael@0: // NOTE: This means (by definition) that there's no packing space, which michael@0: // means we don't need to be concerned with "align-conent" at all and we michael@0: // can return early. This is handy, because this is the usual case (for michael@0: // single-line flexbox). michael@0: aFirstLine->SetLineCrossSize(aContentBoxCrossSize); michael@0: return; michael@0: } michael@0: michael@0: // NOTE: The rest of this function should essentially match michael@0: // MainAxisPositionTracker's constructor, though with FlexLines instead of michael@0: // FlexItems, and with the additional value "stretch" (and of course with michael@0: // cross sizes instead of main sizes.) michael@0: michael@0: // Figure out how much packing space we have (container's cross size minus michael@0: // all the lines' cross sizes). Also, share this loop to count how many michael@0: // lines we have. (We need that count in some cases below.) michael@0: mPackingSpaceRemaining = aContentBoxCrossSize; michael@0: uint32_t numLines = 0; michael@0: for (FlexLine* line = aFirstLine; line; line = line->getNext()) { michael@0: mPackingSpaceRemaining -= line->GetLineCrossSize(); michael@0: numLines++; michael@0: } michael@0: michael@0: // If packing space is negative, 'space-between' and 'stretch' behave like michael@0: // 'flex-start', and 'space-around' behaves like 'center'. In those cases, michael@0: // it's simplest to just pretend we have a different 'align-content' value michael@0: // and share code. michael@0: if (mPackingSpaceRemaining < 0) { michael@0: if (mAlignContent == NS_STYLE_ALIGN_CONTENT_SPACE_BETWEEN || michael@0: mAlignContent == NS_STYLE_ALIGN_CONTENT_STRETCH) { michael@0: mAlignContent = NS_STYLE_ALIGN_CONTENT_FLEX_START; michael@0: } else if (mAlignContent == NS_STYLE_ALIGN_CONTENT_SPACE_AROUND) { michael@0: mAlignContent = NS_STYLE_ALIGN_CONTENT_CENTER; michael@0: } michael@0: } michael@0: michael@0: // If our cross axis is (internally) reversed, swap the align-content michael@0: // "flex-start" and "flex-end" behaviors: michael@0: if (aAxisTracker.AreAxesInternallyReversed()) { michael@0: if (mAlignContent == NS_STYLE_ALIGN_CONTENT_FLEX_START) { michael@0: mAlignContent = NS_STYLE_ALIGN_CONTENT_FLEX_END; michael@0: } else if (mAlignContent == NS_STYLE_ALIGN_CONTENT_FLEX_END) { michael@0: mAlignContent = NS_STYLE_ALIGN_CONTENT_FLEX_START; michael@0: } michael@0: } michael@0: michael@0: // Figure out how much space we'll set aside for packing spaces, and advance michael@0: // past any leading packing-space. michael@0: if (mPackingSpaceRemaining != 0) { michael@0: switch (mAlignContent) { michael@0: case NS_STYLE_ALIGN_CONTENT_FLEX_START: michael@0: // All packing space should go at the end --> nothing to do here. michael@0: break; michael@0: case NS_STYLE_ALIGN_CONTENT_FLEX_END: michael@0: // All packing space goes at the beginning michael@0: mPosition += mPackingSpaceRemaining; michael@0: break; michael@0: case NS_STYLE_ALIGN_CONTENT_CENTER: michael@0: // Half the packing space goes at the beginning michael@0: mPosition += mPackingSpaceRemaining / 2; michael@0: break; michael@0: case NS_STYLE_ALIGN_CONTENT_SPACE_BETWEEN: michael@0: MOZ_ASSERT(mPackingSpaceRemaining >= 0, michael@0: "negative packing space should make us use 'flex-start' " michael@0: "instead of 'space-between'"); michael@0: // 1 packing space between each flex line, no packing space at ends. michael@0: mNumPackingSpacesRemaining = numLines - 1; michael@0: break; michael@0: case NS_STYLE_ALIGN_CONTENT_SPACE_AROUND: { michael@0: MOZ_ASSERT(mPackingSpaceRemaining >= 0, michael@0: "negative packing space should make us use 'center' " michael@0: "instead of 'space-around'"); michael@0: // 1 packing space between each flex line, plus half a packing space michael@0: // at beginning & end. So our number of full packing-spaces is equal michael@0: // to the number of flex lines. michael@0: mNumPackingSpacesRemaining = numLines; michael@0: // The edges (start/end) share one full packing space michael@0: nscoord totalEdgePackingSpace = michael@0: mPackingSpaceRemaining / mNumPackingSpacesRemaining; michael@0: michael@0: // ...and we'll use half of that right now, at the start michael@0: mPosition += totalEdgePackingSpace / 2; michael@0: // ...but we need to subtract all of it right away, so that we won't michael@0: // hand out any of it to intermediate packing spaces. michael@0: mPackingSpaceRemaining -= totalEdgePackingSpace; michael@0: mNumPackingSpacesRemaining--; michael@0: break; michael@0: } michael@0: case NS_STYLE_ALIGN_CONTENT_STRETCH: { michael@0: // Split space equally between the lines: michael@0: MOZ_ASSERT(mPackingSpaceRemaining > 0, michael@0: "negative packing space should make us use 'flex-start' " michael@0: "instead of 'stretch' (and we shouldn't bother with this " michael@0: "code if we have 0 packing space)"); michael@0: michael@0: uint32_t numLinesLeft = numLines; michael@0: for (FlexLine* line = aFirstLine; line; line = line->getNext()) { michael@0: // Our share is the amount of space remaining, divided by the number michael@0: // of lines remainig. michael@0: MOZ_ASSERT(numLinesLeft > 0, "miscalculated num lines"); michael@0: nscoord shareOfExtraSpace = mPackingSpaceRemaining / numLinesLeft; michael@0: nscoord newSize = line->GetLineCrossSize() + shareOfExtraSpace; michael@0: line->SetLineCrossSize(newSize); michael@0: michael@0: mPackingSpaceRemaining -= shareOfExtraSpace; michael@0: numLinesLeft--; michael@0: } michael@0: MOZ_ASSERT(numLinesLeft == 0, "miscalculated num lines"); michael@0: break; michael@0: } michael@0: default: michael@0: MOZ_CRASH("Unexpected align-content value"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: CrossAxisPositionTracker::TraversePackingSpace() michael@0: { michael@0: if (mNumPackingSpacesRemaining) { michael@0: MOZ_ASSERT(mAlignContent == NS_STYLE_ALIGN_CONTENT_SPACE_BETWEEN || michael@0: mAlignContent == NS_STYLE_ALIGN_CONTENT_SPACE_AROUND, michael@0: "mNumPackingSpacesRemaining only applies for " michael@0: "space-between/space-around"); michael@0: michael@0: MOZ_ASSERT(mPackingSpaceRemaining >= 0, michael@0: "ran out of packing space earlier than we expected"); michael@0: michael@0: // NOTE: This integer math will skew the distribution of remainder michael@0: // app-units towards the end, which is fine. michael@0: nscoord curPackingSpace = michael@0: mPackingSpaceRemaining / mNumPackingSpacesRemaining; michael@0: michael@0: mPosition += curPackingSpace; michael@0: mNumPackingSpacesRemaining--; michael@0: mPackingSpaceRemaining -= curPackingSpace; michael@0: } michael@0: } michael@0: michael@0: SingleLineCrossAxisPositionTracker:: michael@0: SingleLineCrossAxisPositionTracker(const FlexboxAxisTracker& aAxisTracker) michael@0: : PositionTracker(aAxisTracker.GetCrossAxis()) michael@0: { michael@0: } michael@0: michael@0: void michael@0: FlexLine::ComputeCrossSizeAndBaseline(const FlexboxAxisTracker& aAxisTracker) michael@0: { michael@0: nscoord crossStartToFurthestBaseline = nscoord_MIN; michael@0: nscoord crossEndToFurthestBaseline = nscoord_MIN; michael@0: nscoord largestOuterCrossSize = 0; michael@0: for (const FlexItem* item = mItems.getFirst(); item; item = item->getNext()) { michael@0: nscoord curOuterCrossSize = michael@0: item->GetOuterCrossSize(aAxisTracker.GetCrossAxis()); michael@0: michael@0: if (item->GetAlignSelf() == NS_STYLE_ALIGN_ITEMS_BASELINE && michael@0: item->GetNumAutoMarginsInAxis(aAxisTracker.GetCrossAxis()) == 0) { michael@0: // FIXME: Once we support "writing-mode", we'll have to do baseline michael@0: // alignment in vertical flex containers here (w/ horizontal cross-axes). michael@0: michael@0: // Find distance from our item's cross-start and cross-end margin-box michael@0: // edges to its baseline. michael@0: // michael@0: // Here's a diagram of a flex-item that we might be doing this on. michael@0: // "mmm" is the margin-box, "bbb" is the border-box. The bottom of michael@0: // the text "BASE" is the baseline. michael@0: // michael@0: // ---(cross-start)--- michael@0: // ___ ___ ___ michael@0: // mmmmmmmmmmmm | |margin-start | michael@0: // m m | _|_ ___ | michael@0: // m bbbbbbbb m |curOuterCrossSize | |crossStartToBaseline michael@0: // m b b m | |ascent | michael@0: // m b BASE b m | _|_ _|_ michael@0: // m b b m | | michael@0: // m bbbbbbbb m | |crossEndToBaseline michael@0: // m m | | michael@0: // mmmmmmmmmmmm _|_ _|_ michael@0: // michael@0: // ---(cross-end)--- michael@0: // michael@0: // We already have the curOuterCrossSize, margin-start, and the ascent. michael@0: // * We can get crossStartToBaseline by adding margin-start + ascent. michael@0: // * If we subtract that from the curOuterCrossSize, we get michael@0: // crossEndToBaseline. michael@0: michael@0: nscoord crossStartToBaseline = michael@0: item->GetBaselineOffsetFromOuterCrossEdge(aAxisTracker.GetCrossAxis(), michael@0: eAxisEdge_Start); michael@0: nscoord crossEndToBaseline = curOuterCrossSize - crossStartToBaseline; michael@0: michael@0: // Now, update our "largest" values for these (across all the flex items michael@0: // in this flex line), so we can use them in computing the line's cross michael@0: // size below: michael@0: crossStartToFurthestBaseline = std::max(crossStartToFurthestBaseline, michael@0: crossStartToBaseline); michael@0: crossEndToFurthestBaseline = std::max(crossEndToFurthestBaseline, michael@0: crossEndToBaseline); michael@0: } else { michael@0: largestOuterCrossSize = std::max(largestOuterCrossSize, curOuterCrossSize); michael@0: } michael@0: } michael@0: michael@0: // The line's baseline offset is the distance from the line's edge (start or michael@0: // end, depending on whether we've flipped the axes) to the furthest michael@0: // item-baseline. The item(s) with that baseline will be exactly aligned with michael@0: // the line's edge. michael@0: mBaselineOffset = aAxisTracker.AreAxesInternallyReversed() ? michael@0: crossEndToFurthestBaseline : crossStartToFurthestBaseline; michael@0: michael@0: // The line's cross-size is the larger of: michael@0: // (a) [largest cross-start-to-baseline + largest baseline-to-cross-end] of michael@0: // all baseline-aligned items with no cross-axis auto margins... michael@0: // and michael@0: // (b) largest cross-size of all other children. michael@0: mLineCrossSize = std::max(crossStartToFurthestBaseline + michael@0: crossEndToFurthestBaseline, michael@0: largestOuterCrossSize); michael@0: } michael@0: michael@0: void michael@0: FlexItem::ResolveStretchedCrossSize(nscoord aLineCrossSize, michael@0: const FlexboxAxisTracker& aAxisTracker) michael@0: { michael@0: AxisOrientationType crossAxis = aAxisTracker.GetCrossAxis(); michael@0: // We stretch IFF we are align-self:stretch, have no auto margins in michael@0: // cross axis, and have cross-axis size property == "auto". If any of those michael@0: // conditions don't hold up, we won't stretch. michael@0: if (mAlignSelf != NS_STYLE_ALIGN_ITEMS_STRETCH || michael@0: GetNumAutoMarginsInAxis(crossAxis) != 0 || michael@0: eStyleUnit_Auto != GetSizePropertyForAxis(mFrame, crossAxis).GetUnit()) { michael@0: return; michael@0: } michael@0: michael@0: // If we've already been stretched, we can bail out early, too. michael@0: // No need to redo the calculation. michael@0: if (mIsStretched) { michael@0: return; michael@0: } michael@0: michael@0: // Reserve space for margins & border & padding, and then use whatever michael@0: // remains as our item's cross-size (clamped to its min/max range). michael@0: nscoord stretchedSize = aLineCrossSize - michael@0: GetMarginBorderPaddingSizeInAxis(crossAxis); michael@0: michael@0: stretchedSize = NS_CSS_MINMAX(stretchedSize, mCrossMinSize, mCrossMaxSize); michael@0: michael@0: // Update the cross-size & make a note that it's stretched, so we know to michael@0: // override the reflow state's computed cross-size in our final reflow. michael@0: SetCrossSize(stretchedSize); michael@0: mIsStretched = true; michael@0: } michael@0: michael@0: void michael@0: SingleLineCrossAxisPositionTracker:: michael@0: ResolveAutoMarginsInCrossAxis(const FlexLine& aLine, michael@0: FlexItem& aItem) michael@0: { michael@0: // Subtract the space that our item is already occupying, to see how much michael@0: // space (if any) is available for its auto margins. michael@0: nscoord spaceForAutoMargins = aLine.GetLineCrossSize() - michael@0: aItem.GetOuterCrossSize(mAxis); michael@0: michael@0: if (spaceForAutoMargins <= 0) { michael@0: return; // No available space --> nothing to do michael@0: } michael@0: michael@0: uint32_t numAutoMargins = aItem.GetNumAutoMarginsInAxis(mAxis); michael@0: if (numAutoMargins == 0) { michael@0: return; // No auto margins --> nothing to do. michael@0: } michael@0: michael@0: // OK, we have at least one auto margin and we have some available space. michael@0: // Give each auto margin a share of the space. michael@0: const nsStyleSides& styleMargin = aItem.Frame()->StyleMargin()->mMargin; michael@0: for (uint32_t i = 0; i < eNumAxisEdges; i++) { michael@0: Side side = kAxisOrientationToSidesMap[mAxis][i]; michael@0: if (styleMargin.GetUnit(side) == eStyleUnit_Auto) { michael@0: MOZ_ASSERT(aItem.GetMarginComponentForSide(side) == 0, michael@0: "Expecting auto margins to have value '0' before we " michael@0: "update them"); michael@0: michael@0: // NOTE: integer divison is fine here; numAutoMargins is either 1 or 2. michael@0: // If it's 2 & spaceForAutoMargins is odd, 1st margin gets smaller half. michael@0: nscoord curAutoMarginSize = spaceForAutoMargins / numAutoMargins; michael@0: aItem.SetMarginComponentForSide(side, curAutoMarginSize); michael@0: numAutoMargins--; michael@0: spaceForAutoMargins -= curAutoMarginSize; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: SingleLineCrossAxisPositionTracker:: michael@0: EnterAlignPackingSpace(const FlexLine& aLine, michael@0: const FlexItem& aItem, michael@0: const FlexboxAxisTracker& aAxisTracker) michael@0: { michael@0: // We don't do align-self alignment on items that have auto margins michael@0: // in the cross axis. michael@0: if (aItem.GetNumAutoMarginsInAxis(mAxis)) { michael@0: return; michael@0: } michael@0: michael@0: uint8_t alignSelf = aItem.GetAlignSelf(); michael@0: // NOTE: 'stretch' behaves like 'flex-start' once we've stretched any michael@0: // auto-sized items (which we've already done). michael@0: if (alignSelf == NS_STYLE_ALIGN_ITEMS_STRETCH) { michael@0: alignSelf = NS_STYLE_ALIGN_ITEMS_FLEX_START; michael@0: } michael@0: michael@0: // If our cross axis is (internally) reversed, swap the align-self michael@0: // "flex-start" and "flex-end" behaviors: michael@0: if (aAxisTracker.AreAxesInternallyReversed()) { michael@0: if (alignSelf == NS_STYLE_ALIGN_ITEMS_FLEX_START) { michael@0: alignSelf = NS_STYLE_ALIGN_ITEMS_FLEX_END; michael@0: } else if (alignSelf == NS_STYLE_ALIGN_ITEMS_FLEX_END) { michael@0: alignSelf = NS_STYLE_ALIGN_ITEMS_FLEX_START; michael@0: } michael@0: } michael@0: michael@0: switch (alignSelf) { michael@0: case NS_STYLE_ALIGN_ITEMS_FLEX_START: michael@0: // No space to skip over -- we're done. michael@0: break; michael@0: case NS_STYLE_ALIGN_ITEMS_FLEX_END: michael@0: mPosition += aLine.GetLineCrossSize() - aItem.GetOuterCrossSize(mAxis); michael@0: break; michael@0: case NS_STYLE_ALIGN_ITEMS_CENTER: michael@0: // Note: If cross-size is odd, the "after" space will get the extra unit. michael@0: mPosition += michael@0: (aLine.GetLineCrossSize() - aItem.GetOuterCrossSize(mAxis)) / 2; michael@0: break; michael@0: case NS_STYLE_ALIGN_ITEMS_BASELINE: { michael@0: // Normally, baseline-aligned items are collectively aligned with the michael@0: // line's cross-start edge; however, if our cross axis is (internally) michael@0: // reversed, we instead align them with the cross-end edge. michael@0: nscoord itemBaselineOffset = michael@0: aItem.GetBaselineOffsetFromOuterCrossEdge(mAxis, michael@0: aAxisTracker.AreAxesInternallyReversed() ? michael@0: eAxisEdge_End : eAxisEdge_Start); michael@0: michael@0: nscoord lineBaselineOffset = aLine.GetBaselineOffset(); michael@0: michael@0: NS_ASSERTION(lineBaselineOffset >= itemBaselineOffset, michael@0: "failed at finding largest baseline offset"); michael@0: michael@0: // How much do we need to adjust our position (from the line edge), michael@0: // to get the item's baseline to hit the line's baseline offset: michael@0: nscoord baselineDiff = lineBaselineOffset - itemBaselineOffset; michael@0: michael@0: if (aAxisTracker.AreAxesInternallyReversed()) { michael@0: // Advance to align item w/ line's flex-end edge (as in FLEX_END case): michael@0: mPosition += aLine.GetLineCrossSize() - aItem.GetOuterCrossSize(mAxis); michael@0: // ...and step *back* by the baseline adjustment: michael@0: mPosition -= baselineDiff; michael@0: } else { michael@0: // mPosition is already at line's flex-start edge. michael@0: // From there, we step *forward* by the baseline adjustment: michael@0: mPosition += baselineDiff; michael@0: } michael@0: break; michael@0: } michael@0: default: michael@0: NS_NOTREACHED("Unexpected align-self value"); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: FlexboxAxisTracker::FlexboxAxisTracker( michael@0: nsFlexContainerFrame* aFlexContainerFrame) michael@0: : mAreAxesInternallyReversed(false) michael@0: { michael@0: const nsStylePosition* pos = aFlexContainerFrame->StylePosition(); michael@0: uint32_t flexDirection = pos->mFlexDirection; michael@0: uint32_t cssDirection = michael@0: aFlexContainerFrame->StyleVisibility()->mDirection; michael@0: michael@0: MOZ_ASSERT(cssDirection == NS_STYLE_DIRECTION_LTR || michael@0: cssDirection == NS_STYLE_DIRECTION_RTL, michael@0: "Unexpected computed value for 'direction' property"); michael@0: // (Not asserting for flexDirection here; it's checked by the switch below.) michael@0: michael@0: // These are defined according to writing-modes' definitions of michael@0: // start/end (for the inline dimension) and before/after (for the block michael@0: // dimension), here: michael@0: // http://www.w3.org/TR/css3-writing-modes/#logical-directions michael@0: // (NOTE: I'm intentionally not calling this "inlineAxis"/"blockAxis", since michael@0: // those terms have explicit definition in the writing-modes spec, which are michael@0: // the opposite of how I'd be using them here.) michael@0: // XXXdholbert Once we support the 'writing-mode' property, use its value michael@0: // here to further customize inlineDimension & blockDimension. michael@0: michael@0: // Inline dimension ("start-to-end"): michael@0: AxisOrientationType inlineDimension = michael@0: cssDirection == NS_STYLE_DIRECTION_RTL ? eAxis_RL : eAxis_LR; michael@0: michael@0: // Block dimension ("before-to-after"): michael@0: AxisOrientationType blockDimension = eAxis_TB; michael@0: michael@0: // Determine main axis: michael@0: switch (flexDirection) { michael@0: case NS_STYLE_FLEX_DIRECTION_ROW: michael@0: mMainAxis = inlineDimension; michael@0: break; michael@0: case NS_STYLE_FLEX_DIRECTION_ROW_REVERSE: michael@0: mMainAxis = GetReverseAxis(inlineDimension); michael@0: break; michael@0: case NS_STYLE_FLEX_DIRECTION_COLUMN: michael@0: mMainAxis = blockDimension; michael@0: break; michael@0: case NS_STYLE_FLEX_DIRECTION_COLUMN_REVERSE: michael@0: mMainAxis = GetReverseAxis(blockDimension); michael@0: break; michael@0: default: michael@0: MOZ_CRASH("Unexpected computed value for 'flex-flow' property"); michael@0: } michael@0: michael@0: // Determine cross axis: michael@0: // (This is set up so that a bogus |flexDirection| value will michael@0: // give us blockDimension. michael@0: if (flexDirection == NS_STYLE_FLEX_DIRECTION_COLUMN || michael@0: flexDirection == NS_STYLE_FLEX_DIRECTION_COLUMN_REVERSE) { michael@0: mCrossAxis = inlineDimension; michael@0: } else { michael@0: mCrossAxis = blockDimension; michael@0: } michael@0: michael@0: // "flex-wrap: wrap-reverse" reverses our cross axis. michael@0: if (pos->mFlexWrap == NS_STYLE_FLEX_WRAP_WRAP_REVERSE) { michael@0: mCrossAxis = GetReverseAxis(mCrossAxis); michael@0: } michael@0: michael@0: // Master switch to enable/disable bug 983427's code for reversing our axes michael@0: // and reversing some logic, to avoid reflowing children in bottom-to-top michael@0: // order. (This switch can be removed eventually, but for now, it allows michael@0: // this special-case code path to be compared against the normal code path.) michael@0: static bool sPreventBottomToTopChildOrdering = true; michael@0: michael@0: if (sPreventBottomToTopChildOrdering) { michael@0: // If either axis is bottom-to-top, we flip both axes (and set a flag michael@0: // so that we can flip some logic to make the reversal transparent). michael@0: if (eAxis_BT == mMainAxis || eAxis_BT == mCrossAxis) { michael@0: mMainAxis = GetReverseAxis(mMainAxis); michael@0: mCrossAxis = GetReverseAxis(mCrossAxis); michael@0: mAreAxesInternallyReversed = true; michael@0: } michael@0: } michael@0: michael@0: MOZ_ASSERT(IsAxisHorizontal(mMainAxis) != IsAxisHorizontal(mCrossAxis), michael@0: "main & cross axes should be in different dimensions"); michael@0: } michael@0: michael@0: // Allocates a new FlexLine, adds it to the given LinkedList (at the front or michael@0: // back depending on aShouldInsertAtFront), and returns a pointer to it. michael@0: static FlexLine* michael@0: AddNewFlexLineToList(LinkedList& aLines, michael@0: bool aShouldInsertAtFront) michael@0: { michael@0: FlexLine* newLine = new FlexLine(); michael@0: if (aShouldInsertAtFront) { michael@0: aLines.insertFront(newLine); michael@0: } else { michael@0: aLines.insertBack(newLine); michael@0: } michael@0: return newLine; michael@0: } michael@0: michael@0: nsresult michael@0: nsFlexContainerFrame::GenerateFlexLines( michael@0: nsPresContext* aPresContext, michael@0: const nsHTMLReflowState& aReflowState, michael@0: nscoord aContentBoxMainSize, michael@0: nscoord aAvailableHeightForContent, michael@0: const nsTArray& aStruts, michael@0: const FlexboxAxisTracker& aAxisTracker, michael@0: LinkedList& aLines) michael@0: { michael@0: MOZ_ASSERT(aLines.isEmpty(), "Expecting outparam to start out empty"); michael@0: michael@0: const bool isSingleLine = michael@0: NS_STYLE_FLEX_WRAP_NOWRAP == aReflowState.mStylePosition->mFlexWrap; michael@0: michael@0: // If we're transparently reversing axes, then we'll need to link up our michael@0: // FlexItems and FlexLines in the reverse order, so that the rest of flex michael@0: // layout (with flipped axes) will still produce the correct result. michael@0: // Here, we declare a convenience bool that we'll pass when adding a new michael@0: // FlexLine or FlexItem, to make us insert it at the beginning of its list michael@0: // (so the list ends up reversed). michael@0: const bool shouldInsertAtFront = aAxisTracker.AreAxesInternallyReversed(); michael@0: michael@0: // We have at least one FlexLine. Even an empty flex container has a single michael@0: // (empty) flex line. michael@0: FlexLine* curLine = AddNewFlexLineToList(aLines, shouldInsertAtFront); michael@0: michael@0: nscoord wrapThreshold; michael@0: if (isSingleLine) { michael@0: // Not wrapping. Set threshold to sentinel value that tells us not to wrap. michael@0: wrapThreshold = NS_UNCONSTRAINEDSIZE; michael@0: } else { michael@0: // Wrapping! Set wrap threshold to flex container's content-box main-size. michael@0: wrapThreshold = aContentBoxMainSize; michael@0: michael@0: // If the flex container doesn't have a definite content-box main-size michael@0: // (e.g. if we're 'height:auto'), make sure we at least wrap when we hit michael@0: // its max main-size. michael@0: if (wrapThreshold == NS_UNCONSTRAINEDSIZE) { michael@0: const nscoord flexContainerMaxMainSize = michael@0: GET_MAIN_COMPONENT(aAxisTracker, michael@0: aReflowState.ComputedMaxWidth(), michael@0: aReflowState.ComputedMaxHeight()); michael@0: michael@0: wrapThreshold = flexContainerMaxMainSize; michael@0: } michael@0: michael@0: // Also: if we're vertical and paginating, we may need to wrap sooner michael@0: // (before we run off the end of the page) michael@0: if (!IsAxisHorizontal(aAxisTracker.GetMainAxis()) && michael@0: aAvailableHeightForContent != NS_UNCONSTRAINEDSIZE) { michael@0: wrapThreshold = std::min(wrapThreshold, aAvailableHeightForContent); michael@0: } michael@0: } michael@0: michael@0: // Tracks the index of the next strut, in aStruts (and when this hits michael@0: // aStruts.Length(), that means there are no more struts): michael@0: uint32_t nextStrutIdx = 0; michael@0: michael@0: // Overall index of the current flex item in the flex container. (This gets michael@0: // checked against entries in aStruts.) michael@0: uint32_t itemIdxInContainer = 0; michael@0: michael@0: for (nsFrameList::Enumerator e(mFrames); !e.AtEnd(); e.Next()) { michael@0: nsIFrame* childFrame = e.get(); michael@0: michael@0: // Honor "page-break-before", if we're multi-line and this line isn't empty: michael@0: if (!isSingleLine && !curLine->IsEmpty() && michael@0: childFrame->StyleDisplay()->mBreakBefore) { michael@0: curLine = AddNewFlexLineToList(aLines, shouldInsertAtFront); michael@0: } michael@0: michael@0: nsAutoPtr item; michael@0: if (nextStrutIdx < aStruts.Length() && michael@0: aStruts[nextStrutIdx].mItemIdx == itemIdxInContainer) { michael@0: michael@0: // Use the simplified "strut" FlexItem constructor: michael@0: item = new FlexItem(childFrame, aStruts[nextStrutIdx].mStrutCrossSize); michael@0: nextStrutIdx++; michael@0: } else { michael@0: item = GenerateFlexItemForChild(aPresContext, childFrame, michael@0: aReflowState, aAxisTracker); michael@0: michael@0: nsresult rv = ResolveFlexItemMaxContentSizing(aPresContext, *item, michael@0: aReflowState, aAxisTracker); michael@0: NS_ENSURE_SUCCESS(rv,rv); michael@0: } michael@0: michael@0: nscoord itemInnerHypotheticalMainSize = item->GetMainSize(); michael@0: nscoord itemOuterHypotheticalMainSize = michael@0: item->GetOuterMainSize(aAxisTracker.GetMainAxis()); michael@0: michael@0: // Check if we need to wrap |item| to a new line michael@0: // (i.e. check if its outer hypothetical main size pushes our line over michael@0: // the threshold) michael@0: if (wrapThreshold != NS_UNCONSTRAINEDSIZE && michael@0: !curLine->IsEmpty() && // No need to wrap at start of a line. michael@0: wrapThreshold < (curLine->GetTotalOuterHypotheticalMainSize() + michael@0: itemOuterHypotheticalMainSize)) { michael@0: curLine = AddNewFlexLineToList(aLines, shouldInsertAtFront); michael@0: } michael@0: michael@0: // Add item to current flex line (and update the line's bookkeeping about michael@0: // how large its items collectively are). michael@0: curLine->AddItem(item.forget(), shouldInsertAtFront, michael@0: itemInnerHypotheticalMainSize, michael@0: itemOuterHypotheticalMainSize); michael@0: michael@0: // Honor "page-break-after", if we're multi-line and have more children: michael@0: if (!isSingleLine && childFrame->GetNextSibling() && michael@0: childFrame->StyleDisplay()->mBreakAfter) { michael@0: curLine = AddNewFlexLineToList(aLines, shouldInsertAtFront); michael@0: } michael@0: itemIdxInContainer++; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Retrieves the content-box main-size of our flex container from the michael@0: // reflow state (specifically, the main-size of *this continuation* of the michael@0: // flex container). michael@0: nscoord michael@0: nsFlexContainerFrame::GetMainSizeFromReflowState( michael@0: const nsHTMLReflowState& aReflowState, michael@0: const FlexboxAxisTracker& aAxisTracker) michael@0: { michael@0: if (IsAxisHorizontal(aAxisTracker.GetMainAxis())) { michael@0: // Horizontal case is easy -- our main size is our computed width michael@0: // (which is already resolved). michael@0: return aReflowState.ComputedWidth(); michael@0: } michael@0: michael@0: return GetEffectiveComputedHeight(aReflowState); michael@0: } michael@0: michael@0: // Returns the largest outer hypothetical main-size of any line in |aLines|. michael@0: // (i.e. the hypothetical main-size of the largest line) michael@0: static nscoord michael@0: GetLargestLineMainSize(const FlexLine* aFirstLine) michael@0: { michael@0: nscoord largestLineOuterSize = 0; michael@0: for (const FlexLine* line = aFirstLine; line; line = line->getNext()) { michael@0: largestLineOuterSize = std::max(largestLineOuterSize, michael@0: line->GetTotalOuterHypotheticalMainSize()); michael@0: } michael@0: return largestLineOuterSize; michael@0: } michael@0: michael@0: // Returns the content-box main-size of our flex container, based on the michael@0: // available height (if appropriate) and the main-sizes of the flex items. michael@0: static nscoord michael@0: ClampFlexContainerMainSize(const nsHTMLReflowState& aReflowState, michael@0: const FlexboxAxisTracker& aAxisTracker, michael@0: nscoord aUnclampedMainSize, michael@0: nscoord aAvailableHeightForContent, michael@0: const FlexLine* aFirstLine, michael@0: nsReflowStatus& aStatus) michael@0: { michael@0: MOZ_ASSERT(aFirstLine, "null first line pointer"); michael@0: michael@0: if (IsAxisHorizontal(aAxisTracker.GetMainAxis())) { michael@0: // Horizontal case is easy -- our main size should already be resolved michael@0: // before we get a call to Reflow. We don't have to worry about doing michael@0: // page-breaking or shrinkwrapping in the horizontal axis. michael@0: return aUnclampedMainSize; michael@0: } michael@0: michael@0: if (aUnclampedMainSize != NS_INTRINSICSIZE) { michael@0: // Vertical case, with fixed height: michael@0: if (aAvailableHeightForContent == NS_UNCONSTRAINEDSIZE || michael@0: aUnclampedMainSize < aAvailableHeightForContent) { michael@0: // Not in a fragmenting context, OR no need to fragment because we have michael@0: // more available height than we need. Either way, just use our fixed michael@0: // height. (Note that the reflow state has already done the appropriate michael@0: // min/max-height clamping.) michael@0: return aUnclampedMainSize; michael@0: } michael@0: michael@0: // Fragmenting *and* our fixed height is too tall for available height: michael@0: // Mark incomplete so we get a next-in-flow, and take up all of the michael@0: // available height (or the amount of height required by our children, if michael@0: // that's larger; but of course not more than our own computed height). michael@0: // XXXdholbert For now, we don't support pushing children to our next michael@0: // continuation or splitting children, so "amount of height required by michael@0: // our children" is just the main-size (height) of our longest flex line. michael@0: NS_FRAME_SET_INCOMPLETE(aStatus); michael@0: nscoord largestLineOuterSize = GetLargestLineMainSize(aFirstLine); michael@0: michael@0: if (largestLineOuterSize <= aAvailableHeightForContent) { michael@0: return aAvailableHeightForContent; michael@0: } michael@0: return std::min(aUnclampedMainSize, largestLineOuterSize); michael@0: } michael@0: michael@0: // Vertical case, with auto-height: michael@0: // Resolve auto-height to the largest FlexLine-length, clamped to our michael@0: // computed min/max main-size properties (min-height & max-height). michael@0: // XXXdholbert Handle constrained-aAvailableHeightForContent case here. michael@0: nscoord largestLineOuterSize = GetLargestLineMainSize(aFirstLine); michael@0: return NS_CSS_MINMAX(largestLineOuterSize, michael@0: aReflowState.ComputedMinHeight(), michael@0: aReflowState.ComputedMaxHeight()); michael@0: } michael@0: michael@0: nscoord michael@0: nsFlexContainerFrame::ComputeCrossSize(const nsHTMLReflowState& aReflowState, michael@0: const FlexboxAxisTracker& aAxisTracker, michael@0: nscoord aSumLineCrossSizes, michael@0: nscoord aAvailableHeightForContent, michael@0: bool* aIsDefinite, michael@0: nsReflowStatus& aStatus) michael@0: { michael@0: MOZ_ASSERT(aIsDefinite, "outparam pointer must be non-null"); michael@0: michael@0: if (IsAxisHorizontal(aAxisTracker.GetCrossAxis())) { michael@0: // Cross axis is horizontal: our cross size is our computed width michael@0: // (which is already resolved). michael@0: *aIsDefinite = true; michael@0: return aReflowState.ComputedWidth(); michael@0: } michael@0: michael@0: nscoord effectiveComputedHeight = GetEffectiveComputedHeight(aReflowState); michael@0: if (effectiveComputedHeight != NS_INTRINSICSIZE) { michael@0: // Cross-axis is vertical, and we have a fixed height: michael@0: *aIsDefinite = true; michael@0: if (aAvailableHeightForContent == NS_UNCONSTRAINEDSIZE || michael@0: effectiveComputedHeight < aAvailableHeightForContent) { michael@0: // Not in a fragmenting context, OR no need to fragment because we have michael@0: // more available height than we need. Either way, just use our fixed michael@0: // height. (Note that the reflow state has already done the appropriate michael@0: // min/max-height clamping.) michael@0: return effectiveComputedHeight; michael@0: } michael@0: michael@0: // Fragmenting *and* our fixed height is too tall for available height: michael@0: // Mark incomplete so we get a next-in-flow, and take up all of the michael@0: // available height (or the amount of height required by our children, if michael@0: // that's larger; but of course not more than our own computed height). michael@0: // XXXdholbert For now, we don't support pushing children to our next michael@0: // continuation or splitting children, so "amount of height required by michael@0: // our children" is just our line-height. michael@0: NS_FRAME_SET_INCOMPLETE(aStatus); michael@0: if (aSumLineCrossSizes <= aAvailableHeightForContent) { michael@0: return aAvailableHeightForContent; michael@0: } michael@0: return std::min(effectiveComputedHeight, aSumLineCrossSizes); michael@0: } michael@0: michael@0: // Cross axis is vertical and we have auto-height: shrink-wrap our line(s), michael@0: // subject to our min-size / max-size constraints in that (vertical) axis. michael@0: // XXXdholbert Handle constrained-aAvailableHeightForContent case here. michael@0: *aIsDefinite = false; michael@0: return NS_CSS_MINMAX(aSumLineCrossSizes, michael@0: aReflowState.ComputedMinHeight(), michael@0: aReflowState.ComputedMaxHeight()); michael@0: } michael@0: michael@0: void michael@0: FlexLine::PositionItemsInMainAxis(uint8_t aJustifyContent, michael@0: nscoord aContentBoxMainSize, michael@0: const FlexboxAxisTracker& aAxisTracker) michael@0: { michael@0: MainAxisPositionTracker mainAxisPosnTracker(aAxisTracker, this, michael@0: aJustifyContent, michael@0: aContentBoxMainSize); michael@0: for (FlexItem* item = mItems.getFirst(); item; item = item->getNext()) { michael@0: nscoord itemMainBorderBoxSize = michael@0: item->GetMainSize() + michael@0: item->GetBorderPaddingSizeInAxis(mainAxisPosnTracker.GetAxis()); michael@0: michael@0: // Resolve any main-axis 'auto' margins on aChild to an actual value. michael@0: mainAxisPosnTracker.ResolveAutoMarginsInMainAxis(*item); michael@0: michael@0: // Advance our position tracker to child's upper-left content-box corner, michael@0: // and use that as its position in the main axis. michael@0: mainAxisPosnTracker.EnterMargin(item->GetMargin()); michael@0: mainAxisPosnTracker.EnterChildFrame(itemMainBorderBoxSize); michael@0: michael@0: item->SetMainPosition(mainAxisPosnTracker.GetPosition()); michael@0: michael@0: mainAxisPosnTracker.ExitChildFrame(itemMainBorderBoxSize); michael@0: mainAxisPosnTracker.ExitMargin(item->GetMargin()); michael@0: mainAxisPosnTracker.TraversePackingSpace(); michael@0: } michael@0: } michael@0: michael@0: // Helper method to take care of children who ASK_FOR_BASELINE, when michael@0: // we need their baseline. michael@0: static void michael@0: ResolveReflowedChildAscent(nsIFrame* aFrame, michael@0: nsHTMLReflowMetrics& aChildDesiredSize) michael@0: { michael@0: if (aChildDesiredSize.TopAscent() == nsHTMLReflowMetrics::ASK_FOR_BASELINE) { michael@0: // Use GetFirstLineBaseline(), or just GetBaseline() if that fails. michael@0: nscoord ascent; michael@0: if (nsLayoutUtils::GetFirstLineBaseline(aFrame, &ascent)) { michael@0: aChildDesiredSize.SetTopAscent(ascent); michael@0: } else { michael@0: aChildDesiredSize.SetTopAscent(aFrame->GetBaseline()); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Given the flex container's "logical ascent" (i.e. distance from the michael@0: * flex container's content-box cross-start edge to its baseline), returns michael@0: * its actual physical ascent value (the distance from the *border-box* top michael@0: * edge to its baseline). michael@0: */ michael@0: static nscoord michael@0: ComputePhysicalAscentFromLogicalAscent(nscoord aLogicalAscent, michael@0: nscoord aContentBoxCrossSize, michael@0: const nsHTMLReflowState& aReflowState, michael@0: const FlexboxAxisTracker& aAxisTracker) michael@0: { michael@0: return aReflowState.ComputedPhysicalBorderPadding().top + michael@0: PhysicalPosFromLogicalPos(aLogicalAscent, aContentBoxCrossSize, michael@0: aAxisTracker.GetCrossAxis()); michael@0: } michael@0: michael@0: nsresult michael@0: nsFlexContainerFrame::SizeItemInCrossAxis( michael@0: nsPresContext* aPresContext, michael@0: const FlexboxAxisTracker& aAxisTracker, michael@0: nsHTMLReflowState& aChildReflowState, michael@0: FlexItem& aItem) michael@0: { michael@0: // In vertical flexbox (with horizontal cross-axis), we can just trust the michael@0: // reflow state's computed-width as our cross-size. We also don't need to michael@0: // record the baseline because we'll have converted any "align-self:baseline" michael@0: // items to be "align-self:flex-start" in the FlexItem constructor. michael@0: // FIXME: Once we support writing-mode (vertical text), we will be able to michael@0: // have baseline-aligned items in a vertical flexbox, and we'll need to michael@0: // record baseline information here. michael@0: if (IsAxisHorizontal(aAxisTracker.GetCrossAxis())) { michael@0: MOZ_ASSERT(aItem.GetAlignSelf() != NS_STYLE_ALIGN_ITEMS_BASELINE, michael@0: "In vert flex container, we depend on FlexItem constructor to " michael@0: "convert 'align-self: baseline' to 'align-self: flex-start'"); michael@0: aItem.SetCrossSize(aChildReflowState.ComputedWidth()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: MOZ_ASSERT(!aItem.HadMeasuringReflow(), michael@0: "We shouldn't need more than one measuring reflow"); michael@0: michael@0: if (aItem.GetAlignSelf() == NS_STYLE_ALIGN_ITEMS_STRETCH) { michael@0: // This item's got "align-self: stretch", so we probably imposed a michael@0: // stretched computed height on it during its previous reflow. We're michael@0: // not imposing that height for *this* measuring reflow, so we need to michael@0: // tell it to treat this reflow as a vertical resize (regardless of michael@0: // whether any of its ancestors are being resized). michael@0: aChildReflowState.mFlags.mVResize = true; michael@0: } michael@0: nsHTMLReflowMetrics childDesiredSize(aChildReflowState); michael@0: nsReflowStatus childReflowStatus; michael@0: const uint32_t flags = NS_FRAME_NO_MOVE_FRAME; michael@0: nsresult rv = ReflowChild(aItem.Frame(), aPresContext, michael@0: childDesiredSize, aChildReflowState, michael@0: 0, 0, flags, childReflowStatus); michael@0: aItem.SetHadMeasuringReflow(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // XXXdholbert Once we do pagination / splitting, we'll need to actually michael@0: // handle incomplete childReflowStatuses. But for now, we give our kids michael@0: // unconstrained available height, which means they should always complete. michael@0: MOZ_ASSERT(NS_FRAME_IS_COMPLETE(childReflowStatus), michael@0: "We gave flex item unconstrained available height, so it " michael@0: "should be complete"); michael@0: michael@0: // Tell the child we're done with its initial reflow. michael@0: // (Necessary for e.g. GetBaseline() to work below w/out asserting) michael@0: rv = FinishReflowChild(aItem.Frame(), aPresContext, michael@0: childDesiredSize, &aChildReflowState, 0, 0, flags); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Save the sizing info that we learned from this reflow michael@0: // ----------------------------------------------------- michael@0: michael@0: // Tentatively store the child's desired content-box cross-size. michael@0: // Note that childDesiredSize is the border-box size, so we have to michael@0: // subtract border & padding to get the content-box size. michael@0: // (Note that at this point in the code, we know our cross axis is vertical, michael@0: // so we don't bother with making aAxisTracker pick the cross-axis component michael@0: // for us.) michael@0: nscoord crossAxisBorderPadding = aItem.GetBorderPadding().TopBottom(); michael@0: if (childDesiredSize.Height() < crossAxisBorderPadding) { michael@0: // Child's requested size isn't large enough for its border/padding! michael@0: // This is OK for the trivial nsFrame::Reflow() impl, but other frame michael@0: // classes should know better. So, if we get here, the child had better be michael@0: // an instance of nsFrame (i.e. it should return null from GetType()). michael@0: // XXXdholbert Once we've fixed bug 765861, we should upgrade this to an michael@0: // assertion that trivially passes if bug 765861's flag has been flipped. michael@0: NS_WARN_IF_FALSE(!aItem.Frame()->GetType(), michael@0: "Child should at least request space for border/padding"); michael@0: aItem.SetCrossSize(0); michael@0: } else { michael@0: // (normal case) michael@0: aItem.SetCrossSize(childDesiredSize.Height() - crossAxisBorderPadding); michael@0: } michael@0: michael@0: // If we need to do baseline-alignment, store the child's ascent. michael@0: if (aItem.GetAlignSelf() == NS_STYLE_ALIGN_ITEMS_BASELINE) { michael@0: ResolveReflowedChildAscent(aItem.Frame(), childDesiredSize); michael@0: aItem.SetAscent(childDesiredSize.TopAscent()); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: FlexLine::PositionItemsInCrossAxis(nscoord aLineStartPosition, michael@0: const FlexboxAxisTracker& aAxisTracker) michael@0: { michael@0: SingleLineCrossAxisPositionTracker lineCrossAxisPosnTracker(aAxisTracker); michael@0: michael@0: for (FlexItem* item = mItems.getFirst(); item; item = item->getNext()) { michael@0: // First, stretch the item's cross size (if appropriate), and resolve any michael@0: // auto margins in this axis. michael@0: item->ResolveStretchedCrossSize(mLineCrossSize, aAxisTracker); michael@0: lineCrossAxisPosnTracker.ResolveAutoMarginsInCrossAxis(*this, *item); michael@0: michael@0: // Compute the cross-axis position of this item michael@0: nscoord itemCrossBorderBoxSize = michael@0: item->GetCrossSize() + michael@0: item->GetBorderPaddingSizeInAxis(aAxisTracker.GetCrossAxis()); michael@0: lineCrossAxisPosnTracker.EnterAlignPackingSpace(*this, *item, aAxisTracker); michael@0: lineCrossAxisPosnTracker.EnterMargin(item->GetMargin()); michael@0: lineCrossAxisPosnTracker.EnterChildFrame(itemCrossBorderBoxSize); michael@0: michael@0: item->SetCrossPosition(aLineStartPosition + michael@0: lineCrossAxisPosnTracker.GetPosition()); michael@0: michael@0: // Back out to cross-axis edge of the line. michael@0: lineCrossAxisPosnTracker.ResetPosition(); michael@0: } michael@0: } michael@0: michael@0: nsresult michael@0: nsFlexContainerFrame::Reflow(nsPresContext* aPresContext, michael@0: nsHTMLReflowMetrics& aDesiredSize, michael@0: const nsHTMLReflowState& aReflowState, michael@0: nsReflowStatus& aStatus) michael@0: { michael@0: DO_GLOBAL_REFLOW_COUNT("nsFlexContainerFrame"); michael@0: DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus); michael@0: PR_LOG(GetFlexContainerLog(), PR_LOG_DEBUG, michael@0: ("Reflow() for nsFlexContainerFrame %p\n", this)); michael@0: michael@0: if (IsFrameTreeTooDeep(aReflowState, aDesiredSize, aStatus)) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: // We (and our children) can only depend on our ancestor's height if we have michael@0: // a percent-height, or if we're positioned and we have "top" and "bottom" michael@0: // set and have height:auto. (There are actually other cases, too -- e.g. if michael@0: // our parent is itself a vertical flex container and we're flexible -- but michael@0: // we'll let our ancestors handle those sorts of cases.) michael@0: const nsStylePosition* stylePos = StylePosition(); michael@0: if (stylePos->mHeight.HasPercent() || michael@0: (StyleDisplay()->IsAbsolutelyPositionedStyle() && michael@0: eStyleUnit_Auto == stylePos->mHeight.GetUnit() && michael@0: eStyleUnit_Auto != stylePos->mOffset.GetTopUnit() && michael@0: eStyleUnit_Auto != stylePos->mOffset.GetBottomUnit())) { michael@0: AddStateBits(NS_FRAME_CONTAINS_RELATIVE_HEIGHT); michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: SanityCheckAnonymousFlexItems(); michael@0: #endif // DEBUG michael@0: michael@0: // If we've never reordered our children, then we can trust that they're michael@0: // already in DOM-order, and we only need to consider their "order" property michael@0: // when checking them for sortedness & sorting them. michael@0: // michael@0: // After we actually sort them, though, we can't trust that they're in DOM michael@0: // order anymore. So, from that point on, our sort & sorted-order-checking michael@0: // operations need to use a fancier LEQ function that also takes DOM order michael@0: // into account, so that we can honor the spec's requirement that frames w/ michael@0: // equal "order" values are laid out in DOM order. michael@0: michael@0: if (!HasAnyStateBits(NS_STATE_FLEX_CHILDREN_REORDERED)) { michael@0: if (SortChildrenIfNeeded()) { michael@0: AddStateBits(NS_STATE_FLEX_CHILDREN_REORDERED); michael@0: } michael@0: } else { michael@0: SortChildrenIfNeeded(); michael@0: } michael@0: michael@0: const FlexboxAxisTracker axisTracker(this); michael@0: michael@0: // If we're being fragmented into a constrained height, subtract off michael@0: // borderpadding-top from it, to get the available height for our michael@0: // content box. (Don't subtract if we're skipping top border/padding, michael@0: // though.) michael@0: nscoord availableHeightForContent = aReflowState.AvailableHeight(); michael@0: if (availableHeightForContent != NS_UNCONSTRAINEDSIZE && michael@0: !(GetSkipSides() & (1 << NS_SIDE_TOP))) { michael@0: availableHeightForContent -= aReflowState.ComputedPhysicalBorderPadding().top; michael@0: // (Don't let that push availableHeightForContent below zero, though): michael@0: availableHeightForContent = std::max(availableHeightForContent, 0); michael@0: } michael@0: michael@0: nscoord contentBoxMainSize = GetMainSizeFromReflowState(aReflowState, michael@0: axisTracker); michael@0: michael@0: nsAutoTArray struts; michael@0: nsresult rv = DoFlexLayout(aPresContext, aDesiredSize, aReflowState, aStatus, michael@0: contentBoxMainSize, availableHeightForContent, michael@0: struts, axisTracker); michael@0: michael@0: if (NS_SUCCEEDED(rv) && !struts.IsEmpty()) { michael@0: // We're restarting flex layout, with new knowledge of collapsed items. michael@0: rv = DoFlexLayout(aPresContext, aDesiredSize, aReflowState, aStatus, michael@0: contentBoxMainSize, availableHeightForContent, michael@0: struts, axisTracker); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: // RAII class to clean up a list of FlexLines. michael@0: // Specifically, this removes each line from the list, deletes all the michael@0: // FlexItems in its list, and deletes the FlexLine. michael@0: class MOZ_STACK_CLASS AutoFlexLineListClearer michael@0: { michael@0: public: michael@0: AutoFlexLineListClearer(LinkedList& aLines michael@0: MOZ_GUARD_OBJECT_NOTIFIER_PARAM) michael@0: : mLines(aLines) michael@0: { michael@0: MOZ_GUARD_OBJECT_NOTIFIER_INIT; michael@0: } michael@0: michael@0: ~AutoFlexLineListClearer() michael@0: { michael@0: while (FlexLine* line = mLines.popFirst()) { michael@0: while (FlexItem* item = line->mItems.popFirst()) { michael@0: delete item; michael@0: } michael@0: delete line; michael@0: } michael@0: } michael@0: michael@0: private: michael@0: LinkedList& mLines; michael@0: MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER michael@0: }; michael@0: michael@0: nsresult michael@0: nsFlexContainerFrame::DoFlexLayout(nsPresContext* aPresContext, michael@0: nsHTMLReflowMetrics& aDesiredSize, michael@0: const nsHTMLReflowState& aReflowState, michael@0: nsReflowStatus& aStatus, michael@0: nscoord aContentBoxMainSize, michael@0: nscoord aAvailableHeightForContent, michael@0: nsTArray& aStruts, michael@0: const FlexboxAxisTracker& aAxisTracker) michael@0: { michael@0: aStatus = NS_FRAME_COMPLETE; michael@0: michael@0: LinkedList lines; michael@0: AutoFlexLineListClearer cleanupLines(lines); michael@0: michael@0: nsresult rv = GenerateFlexLines(aPresContext, aReflowState, michael@0: aContentBoxMainSize, michael@0: aAvailableHeightForContent, michael@0: aStruts, aAxisTracker, lines); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: aContentBoxMainSize = michael@0: ClampFlexContainerMainSize(aReflowState, aAxisTracker, michael@0: aContentBoxMainSize, aAvailableHeightForContent, michael@0: lines.getFirst(), aStatus); michael@0: michael@0: for (FlexLine* line = lines.getFirst(); line; line = line->getNext()) { michael@0: line->ResolveFlexibleLengths(aContentBoxMainSize); michael@0: } michael@0: michael@0: // Cross Size Determination - Flexbox spec section 9.4 michael@0: // =================================================== michael@0: // Calculate the hypothetical cross size of each item: michael@0: nscoord sumLineCrossSizes = 0; michael@0: for (FlexLine* line = lines.getFirst(); line; line = line->getNext()) { michael@0: for (FlexItem* item = line->GetFirstItem(); item; item = item->getNext()) { michael@0: // (If the item's already been stretched, or it's a strut, then it michael@0: // already knows its cross size. Don't bother trying to recalculate it.) michael@0: if (!item->IsStretched() && !item->IsStrut()) { michael@0: nsHTMLReflowState childReflowState(aPresContext, aReflowState, michael@0: item->Frame(), michael@0: nsSize(aReflowState.ComputedWidth(), michael@0: NS_UNCONSTRAINEDSIZE)); michael@0: // Override computed main-size michael@0: if (IsAxisHorizontal(aAxisTracker.GetMainAxis())) { michael@0: childReflowState.SetComputedWidth(item->GetMainSize()); michael@0: } else { michael@0: childReflowState.SetComputedHeight(item->GetMainSize()); michael@0: } michael@0: michael@0: nsresult rv = SizeItemInCrossAxis(aPresContext, aAxisTracker, michael@0: childReflowState, *item); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: } michael@0: // Now that we've finished with this line's items, size the line itself: michael@0: line->ComputeCrossSizeAndBaseline(aAxisTracker); michael@0: sumLineCrossSizes += line->GetLineCrossSize(); michael@0: } michael@0: michael@0: bool isCrossSizeDefinite; michael@0: const nscoord contentBoxCrossSize = michael@0: ComputeCrossSize(aReflowState, aAxisTracker, sumLineCrossSizes, michael@0: aAvailableHeightForContent, &isCrossSizeDefinite, aStatus); michael@0: michael@0: // Set up state for cross-axis alignment, at a high level (outside the michael@0: // scope of a particular flex line) michael@0: CrossAxisPositionTracker michael@0: crossAxisPosnTracker(lines.getFirst(), michael@0: aReflowState.mStylePosition->mAlignContent, michael@0: contentBoxCrossSize, isCrossSizeDefinite, michael@0: aAxisTracker); michael@0: michael@0: // Now that we know the cross size of each line (including michael@0: // "align-content:stretch" adjustments, from the CrossAxisPositionTracker michael@0: // constructor), we can create struts for any flex items with michael@0: // "visibility: collapse" (and restart flex layout). michael@0: if (aStruts.IsEmpty()) { // (Don't make struts if we already did) michael@0: BuildStrutInfoFromCollapsedItems(lines.getFirst(), aStruts); michael@0: if (!aStruts.IsEmpty()) { michael@0: // Restart flex layout, using our struts. michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: // If the container should derive its baseline from the first FlexLine, michael@0: // do that here (while crossAxisPosnTracker is conveniently pointing michael@0: // at the cross-start edge of that line, which the line's baseline offset is michael@0: // measured from): michael@0: nscoord flexContainerAscent; michael@0: if (!aAxisTracker.AreAxesInternallyReversed()) { michael@0: nscoord firstLineBaselineOffset = lines.getFirst()->GetBaselineOffset(); michael@0: if (firstLineBaselineOffset == nscoord_MIN) { michael@0: // No baseline-aligned items in line. Use sentinel value to prompt us to michael@0: // get baseline from the first FlexItem after we've reflowed it. michael@0: flexContainerAscent = nscoord_MIN; michael@0: } else { michael@0: flexContainerAscent = michael@0: ComputePhysicalAscentFromLogicalAscent( michael@0: crossAxisPosnTracker.GetPosition() + firstLineBaselineOffset, michael@0: contentBoxCrossSize, aReflowState, aAxisTracker); michael@0: } michael@0: } michael@0: michael@0: for (FlexLine* line = lines.getFirst(); line; line = line->getNext()) { michael@0: michael@0: // Main-Axis Alignment - Flexbox spec section 9.5 michael@0: // ============================================== michael@0: line->PositionItemsInMainAxis(aReflowState.mStylePosition->mJustifyContent, michael@0: aContentBoxMainSize, michael@0: aAxisTracker); michael@0: michael@0: // Cross-Axis Alignment - Flexbox spec section 9.6 michael@0: // =============================================== michael@0: line->PositionItemsInCrossAxis(crossAxisPosnTracker.GetPosition(), michael@0: aAxisTracker); michael@0: crossAxisPosnTracker.TraverseLine(*line); michael@0: crossAxisPosnTracker.TraversePackingSpace(); michael@0: } michael@0: michael@0: // If the container should derive its baseline from the last FlexLine, michael@0: // do that here (while crossAxisPosnTracker is conveniently pointing michael@0: // at the cross-end edge of that line, which the line's baseline offset is michael@0: // measured from): michael@0: if (aAxisTracker.AreAxesInternallyReversed()) { michael@0: nscoord lastLineBaselineOffset = lines.getLast()->GetBaselineOffset(); michael@0: if (lastLineBaselineOffset == nscoord_MIN) { michael@0: // No baseline-aligned items in line. Use sentinel value to prompt us to michael@0: // get baseline from the last FlexItem after we've reflowed it. michael@0: flexContainerAscent = nscoord_MIN; michael@0: } else { michael@0: flexContainerAscent = michael@0: ComputePhysicalAscentFromLogicalAscent( michael@0: crossAxisPosnTracker.GetPosition() - lastLineBaselineOffset, michael@0: contentBoxCrossSize, aReflowState, aAxisTracker); michael@0: } michael@0: } michael@0: michael@0: // Before giving each child a final reflow, calculate the origin of the michael@0: // flex container's content box (with respect to its border-box), so that michael@0: // we can compute our flex item's final positions. michael@0: nsMargin containerBorderPadding(aReflowState.ComputedPhysicalBorderPadding()); michael@0: ApplySkipSides(containerBorderPadding, &aReflowState); michael@0: const nsPoint containerContentBoxOrigin(containerBorderPadding.left, michael@0: containerBorderPadding.top); michael@0: michael@0: // FINAL REFLOW: Give each child frame another chance to reflow, now that michael@0: // we know its final size and position. michael@0: for (const FlexLine* line = lines.getFirst(); line; line = line->getNext()) { michael@0: for (const FlexItem* item = line->GetFirstItem(); item; michael@0: item = item->getNext()) { michael@0: nsPoint physicalPosn = aAxisTracker.PhysicalPointFromLogicalPoint( michael@0: item->GetMainPosition(), michael@0: item->GetCrossPosition(), michael@0: aContentBoxMainSize, michael@0: contentBoxCrossSize); michael@0: // Adjust physicalPosn to be relative to the container's border-box michael@0: // (i.e. its frame rect), instead of the container's content-box: michael@0: physicalPosn += containerContentBoxOrigin; michael@0: michael@0: nsHTMLReflowState childReflowState(aPresContext, aReflowState, michael@0: item->Frame(), michael@0: nsSize(aReflowState.ComputedWidth(), michael@0: NS_UNCONSTRAINEDSIZE)); michael@0: michael@0: // Keep track of whether we've overriden the child's computed height michael@0: // and/or width, so we can set its resize flags accordingly. michael@0: bool didOverrideComputedWidth = false; michael@0: bool didOverrideComputedHeight = false; michael@0: michael@0: // Override computed main-size michael@0: if (IsAxisHorizontal(aAxisTracker.GetMainAxis())) { michael@0: childReflowState.SetComputedWidth(item->GetMainSize()); michael@0: didOverrideComputedWidth = true; michael@0: } else { michael@0: childReflowState.SetComputedHeight(item->GetMainSize()); michael@0: didOverrideComputedHeight = true; michael@0: } michael@0: michael@0: // Override reflow state's computed cross-size, for stretched items. michael@0: if (item->IsStretched()) { michael@0: MOZ_ASSERT(item->GetAlignSelf() == NS_STYLE_ALIGN_ITEMS_STRETCH, michael@0: "stretched item w/o 'align-self: stretch'?"); michael@0: if (IsAxisHorizontal(aAxisTracker.GetCrossAxis())) { michael@0: childReflowState.SetComputedWidth(item->GetCrossSize()); michael@0: didOverrideComputedWidth = true; michael@0: } else { michael@0: // If this item's height is stretched, it's a relative height. michael@0: item->Frame()->AddStateBits(NS_FRAME_CONTAINS_RELATIVE_HEIGHT); michael@0: childReflowState.SetComputedHeight(item->GetCrossSize()); michael@0: didOverrideComputedHeight = true; michael@0: } michael@0: } michael@0: michael@0: // XXXdholbert Might need to actually set the correct margins in the michael@0: // reflow state at some point, so that they can be saved on the frame for michael@0: // UsedMarginProperty(). Maybe doesn't matter though...? michael@0: michael@0: // If we're overriding the computed width or height, *and* we had an michael@0: // earlier "measuring" reflow, then this upcoming reflow needs to be michael@0: // treated as a resize. michael@0: if (item->HadMeasuringReflow()) { michael@0: if (didOverrideComputedWidth) { michael@0: // (This is somewhat redundant, since the reflow state already michael@0: // sets mHResize whenever our computed width has changed since the michael@0: // previous reflow. Still, it's nice for symmetry, and it may become michael@0: // necessary once we support orthogonal flows.) michael@0: childReflowState.mFlags.mHResize = true; michael@0: } michael@0: if (didOverrideComputedHeight) { michael@0: childReflowState.mFlags.mVResize = true; michael@0: } michael@0: } michael@0: // NOTE: Be very careful about doing anything else with childReflowState michael@0: // after this point, because some of its methods (e.g. SetComputedWidth) michael@0: // internally call InitResizeFlags and stomp on mVResize & mHResize. michael@0: michael@0: nsHTMLReflowMetrics childDesiredSize(childReflowState); michael@0: nsReflowStatus childReflowStatus; michael@0: nsresult rv = ReflowChild(item->Frame(), aPresContext, michael@0: childDesiredSize, childReflowState, michael@0: physicalPosn.x, physicalPosn.y, michael@0: 0, childReflowStatus); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // XXXdholbert Once we do pagination / splitting, we'll need to actually michael@0: // handle incomplete childReflowStatuses. But for now, we give our kids michael@0: // unconstrained available height, which means they should always michael@0: // complete. michael@0: MOZ_ASSERT(NS_FRAME_IS_COMPLETE(childReflowStatus), michael@0: "We gave flex item unconstrained available height, so it " michael@0: "should be complete"); michael@0: michael@0: childReflowState.ApplyRelativePositioning(&physicalPosn); michael@0: michael@0: rv = FinishReflowChild(item->Frame(), aPresContext, michael@0: childDesiredSize, &childReflowState, michael@0: physicalPosn.x, physicalPosn.y, 0); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // If this is our first child and we haven't established a baseline for michael@0: // the container yet (i.e. if we don't have 'align-self: baseline' on any michael@0: // children), then use this child's baseline as the container's baseline. michael@0: if (item->Frame() == mFrames.FirstChild() && michael@0: flexContainerAscent == nscoord_MIN) { michael@0: ResolveReflowedChildAscent(item->Frame(), childDesiredSize); michael@0: michael@0: // (We use GetNormalPosition() instead of physicalPosn because we don't michael@0: // want relative positioning on the child to affect the baseline that we michael@0: // read from it). michael@0: flexContainerAscent = item->Frame()->GetNormalPosition().y + michael@0: childDesiredSize.TopAscent(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: nsSize desiredContentBoxSize = michael@0: aAxisTracker.PhysicalSizeFromLogicalSizes(aContentBoxMainSize, michael@0: contentBoxCrossSize); michael@0: michael@0: aDesiredSize.Width() = desiredContentBoxSize.width + michael@0: containerBorderPadding.LeftRight(); michael@0: // Does *NOT* include bottom border/padding yet (we add that a bit lower down) michael@0: aDesiredSize.Height() = desiredContentBoxSize.height + michael@0: containerBorderPadding.top; michael@0: michael@0: if (flexContainerAscent == nscoord_MIN) { michael@0: // Still don't have our baseline set -- this happens if we have no michael@0: // children (or if our children are huge enough that they have nscoord_MIN michael@0: // as their baseline... in which case, we'll use the wrong baseline, but no michael@0: // big deal) michael@0: NS_WARN_IF_FALSE(lines.getFirst()->IsEmpty(), michael@0: "Have flex items but didn't get an ascent - that's odd " michael@0: "(or there are just gigantic sizes involved)"); michael@0: // Per spec, just use the bottom of content-box. michael@0: flexContainerAscent = aDesiredSize.Height(); michael@0: } michael@0: aDesiredSize.SetTopAscent(flexContainerAscent); michael@0: michael@0: // Now: If we're complete, add bottom border/padding to desired height michael@0: // (unless that pushes us over available height, in which case we become michael@0: // incomplete (unless we already weren't asking for any height, in which case michael@0: // we stay complete to avoid looping forever)). michael@0: // NOTE: If we're auto-height, we allow our bottom border/padding to push us michael@0: // over the available height without requesting a continuation, for michael@0: // consistency with the behavior of "display:block" elements. michael@0: if (NS_FRAME_IS_COMPLETE(aStatus)) { michael@0: // NOTE: We can't use containerBorderPadding.bottom for this, because if michael@0: // we're auto-height, ApplySkipSides will have zeroed it (because it michael@0: // assumed we might get a continuation). We have the correct value in michael@0: // aReflowState.ComputedPhyiscalBorderPadding().bottom, though, so we use that. michael@0: nscoord desiredHeightWithBottomBP = michael@0: aDesiredSize.Height() + aReflowState.ComputedPhysicalBorderPadding().bottom; michael@0: michael@0: if (aReflowState.AvailableHeight() == NS_UNCONSTRAINEDSIZE || michael@0: aDesiredSize.Height() == 0 || michael@0: desiredHeightWithBottomBP <= aReflowState.AvailableHeight() || michael@0: aReflowState.ComputedHeight() == NS_INTRINSICSIZE) { michael@0: // Update desired height to include bottom border/padding michael@0: aDesiredSize.Height() = desiredHeightWithBottomBP; michael@0: } else { michael@0: // We couldn't fit bottom border/padding, so we'll need a continuation. michael@0: NS_FRAME_SET_INCOMPLETE(aStatus); michael@0: } michael@0: } michael@0: michael@0: // Overflow area = union(my overflow area, kids' overflow areas) michael@0: aDesiredSize.SetOverflowAreasToDesiredBounds(); michael@0: for (nsFrameList::Enumerator e(mFrames); !e.AtEnd(); e.Next()) { michael@0: ConsiderChildOverflow(aDesiredSize.mOverflowAreas, e.get()); michael@0: } michael@0: michael@0: FinishReflowWithAbsoluteFrames(aPresContext, aDesiredSize, michael@0: aReflowState, aStatus); michael@0: michael@0: NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize) michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* virtual */ nscoord michael@0: nsFlexContainerFrame::GetMinWidth(nsRenderingContext* aRenderingContext) michael@0: { michael@0: nscoord minWidth = 0; michael@0: DISPLAY_MIN_WIDTH(this, minWidth); michael@0: michael@0: FlexboxAxisTracker axisTracker(this); michael@0: michael@0: for (nsFrameList::Enumerator e(mFrames); !e.AtEnd(); e.Next()) { michael@0: nscoord childMinWidth = michael@0: nsLayoutUtils::IntrinsicForContainer(aRenderingContext, e.get(), michael@0: nsLayoutUtils::MIN_WIDTH); michael@0: // For a horizontal single-line flex container, the intrinsic min width is michael@0: // the sum of its items' min widths. michael@0: // For a vertical flex container, or for a multi-line horizontal flex michael@0: // container, the intrinsic min width is the max of its items' min widths. michael@0: if (IsAxisHorizontal(axisTracker.GetMainAxis()) && michael@0: NS_STYLE_FLEX_WRAP_NOWRAP == StylePosition()->mFlexWrap) { michael@0: minWidth += childMinWidth; michael@0: } else { michael@0: minWidth = std::max(minWidth, childMinWidth); michael@0: } michael@0: } michael@0: return minWidth; michael@0: } michael@0: michael@0: /* virtual */ nscoord michael@0: nsFlexContainerFrame::GetPrefWidth(nsRenderingContext* aRenderingContext) michael@0: { michael@0: nscoord prefWidth = 0; michael@0: DISPLAY_PREF_WIDTH(this, prefWidth); michael@0: michael@0: // XXXdholbert Optimization: We could cache our intrinsic widths like michael@0: // nsBlockFrame does (and return it early from this function if it's set). michael@0: // Whenever anything happens that might change it, set it to michael@0: // NS_INTRINSIC_WIDTH_UNKNOWN (like nsBlockFrame::MarkIntrinsicWidthsDirty michael@0: // does) michael@0: FlexboxAxisTracker axisTracker(this); michael@0: michael@0: for (nsFrameList::Enumerator e(mFrames); !e.AtEnd(); e.Next()) { michael@0: nscoord childPrefWidth = michael@0: nsLayoutUtils::IntrinsicForContainer(aRenderingContext, e.get(), michael@0: nsLayoutUtils::PREF_WIDTH); michael@0: if (IsAxisHorizontal(axisTracker.GetMainAxis())) { michael@0: prefWidth += childPrefWidth; michael@0: } else { michael@0: prefWidth = std::max(prefWidth, childPrefWidth); michael@0: } michael@0: } michael@0: return prefWidth; michael@0: }