From 4256cf9ea7476ebbbee592ed9d2534021c04e1f8 Mon Sep 17 00:00:00 2001 From: Mats Palmgren Date: Wed, 27 Jan 2016 17:02:13 +0100 Subject: [PATCH] Bug 1240795 - [css-grid] Refactor GetComputedTemplateColumns/Rows to return a self-contained value. r=dholbert --- layout/generic/nsGridContainerFrame.cpp | 15 ++++-- layout/generic/nsGridContainerFrame.h | 45 +++++++++------- layout/style/nsComputedDOMStyle.cpp | 71 +++++++++++-------------- layout/style/nsComputedDOMStyle.h | 5 +- 4 files changed, 68 insertions(+), 68 deletions(-) diff --git a/layout/generic/nsGridContainerFrame.cpp b/layout/generic/nsGridContainerFrame.cpp index edc4bd29d57e..f8df5d6486ec 100644 --- a/layout/generic/nsGridContainerFrame.cpp +++ b/layout/generic/nsGridContainerFrame.cpp @@ -3341,14 +3341,21 @@ nsGridContainerFrame::Reflow(nsPresContext* aPresContext, for (const TrackSize& sz : gridReflowState.mCols.mSizes) { colTrackSizes.AppendElement(sz.mBase); } - Properties().Set(GridColTrackSizes(), - new nsTArray(mozilla::Move(colTrackSizes))); + ComputedGridTrackInfo* colInfo = new ComputedGridTrackInfo( + gridReflowState.mColFunctions.mExplicitGridOffset, + gridReflowState.mColFunctions.NumExplicitTracks(), + Move(colTrackSizes)); + Properties().Set(GridColTrackInfo(), colInfo); + nsTArray rowTrackSizes(gridReflowState.mRows.mSizes.Length()); for (const TrackSize& sz : gridReflowState.mRows.mSizes) { rowTrackSizes.AppendElement(sz.mBase); } - Properties().Set(GridRowTrackSizes(), - new nsTArray(mozilla::Move(rowTrackSizes))); + ComputedGridTrackInfo* rowInfo = new ComputedGridTrackInfo( + gridReflowState.mRowFunctions.mExplicitGridOffset, + gridReflowState.mRowFunctions.NumExplicitTracks(), + Move(rowTrackSizes)); + Properties().Set(GridRowTrackInfo(), rowInfo); nscoord bSize = 0; if (computedBSize == NS_AUTOHEIGHT) { diff --git a/layout/generic/nsGridContainerFrame.h b/layout/generic/nsGridContainerFrame.h index 324cbf63fc4c..cd43dc7e53bd 100644 --- a/layout/generic/nsGridContainerFrame.h +++ b/layout/generic/nsGridContainerFrame.h @@ -21,6 +21,25 @@ nsContainerFrame* NS_NewGridContainerFrame(nsIPresShell* aPresShell, nsStyleContext* aContext); +namespace mozilla { +/** + * The number of implicit / explicit tracks and their sizes. + */ +struct ComputedGridTrackInfo +{ + ComputedGridTrackInfo(uint32_t aNumLeadingImplicitTracks, + uint32_t aNumExplicitTracks, + nsTArray&& aSizes) + : mNumLeadingImplicitTracks(aNumLeadingImplicitTracks) + , mNumExplicitTracks(aNumExplicitTracks) + , mSizes(aSizes) + {} + uint32_t mNumLeadingImplicitTracks; + uint32_t mNumExplicitTracks; + nsTArray mSizes; +}; +} // namespace mozilla + class nsGridContainerFrame final : public nsContainerFrame { public: @@ -88,32 +107,18 @@ public: NS_DECLARE_FRAME_PROPERTY(GridItemContainingBlockRect, DeleteValue) - NS_DECLARE_FRAME_PROPERTY(GridColTrackSizes, DeleteValue>) - - const nsTArray* GetComputedTemplateColumns() + NS_DECLARE_FRAME_PROPERTY(GridColTrackInfo, DeleteValue) + const ComputedGridTrackInfo* GetComputedTemplateColumns() { - return static_cast*>(Properties().Get(GridColTrackSizes())); + return static_cast(Properties().Get(GridColTrackInfo())); } - NS_DECLARE_FRAME_PROPERTY(GridRowTrackSizes, DeleteValue>) - - const nsTArray* GetComputedTemplateRows() + NS_DECLARE_FRAME_PROPERTY(GridRowTrackInfo, DeleteValue) + const ComputedGridTrackInfo* GetComputedTemplateRows() { - return static_cast*>(Properties().Get(GridRowTrackSizes())); + return static_cast(Properties().Get(GridRowTrackInfo())); } - /** - * Return the number of implicit tracks that comes before the explicit grid. - */ - uint32_t NumImplicitLeadingCols() const { return mExplicitGridOffsetCol; } - uint32_t NumImplicitLeadingRows() const { return mExplicitGridOffsetRow; } - - /** - * Return the number of explicit tracks. - */ - uint32_t NumExplicitCols() const { return mExplicitGridColEnd - 1; } - uint32_t NumExplicitRows() const { return mExplicitGridRowEnd - 1; } - protected: static const uint32_t kAutoLine; // The maximum line number, in the zero-based translated grid. diff --git a/layout/style/nsComputedDOMStyle.cpp b/layout/style/nsComputedDOMStyle.cpp index d87877c4e553..4f1813b8664b 100644 --- a/layout/style/nsComputedDOMStyle.cpp +++ b/layout/style/nsComputedDOMStyle.cpp @@ -2449,10 +2449,9 @@ nsComputedDOMStyle::GetGridTrackSize(const nsStyleCoord& aMinValue, } already_AddRefed -nsComputedDOMStyle::GetGridTemplateColumnsRows(const nsStyleGridTemplate& aTrackList, - const uint32_t aNumLeadingImplicitTracks, - const uint32_t aNumExplicitTracks, - const nsTArray* aTrackSizes) +nsComputedDOMStyle::GetGridTemplateColumnsRows( + const nsStyleGridTemplate& aTrackList, + const ComputedGridTrackInfo* aTrackInfo) { if (aTrackList.mIsSubgrid) { // XXX TODO: add support for repeat(auto-fill) for 'subgrid' (bug 1234311) @@ -2491,17 +2490,18 @@ nsComputedDOMStyle::GetGridTemplateColumnsRows(const nsStyleGridTemplate& aTrack uint32_t numSizes = aTrackList.mMinTrackSizingFunctions.Length(); MOZ_ASSERT(aTrackList.mMaxTrackSizingFunctions.Length() == numSizes, "Different number of min and max track sizing functions"); - if (aTrackSizes) { + if (aTrackInfo) { DebugOnly isAutoFill = aTrackList.HasRepeatAuto() && aTrackList.mIsAutoFill; DebugOnly isAutoFit = aTrackList.HasRepeatAuto() && !aTrackList.mIsAutoFill; - MOZ_ASSERT(aNumExplicitTracks == numSizes || - (isAutoFill && aNumExplicitTracks >= numSizes) || - (isAutoFit && aNumExplicitTracks + 1 >= numSizes), + DebugOnly numExplicitTracks = aTrackInfo->mNumExplicitTracks; + MOZ_ASSERT(numExplicitTracks == numSizes || + (isAutoFill && numExplicitTracks >= numSizes) || + (isAutoFit && numExplicitTracks + 1 >= numSizes), "expected all explicit tracks (or possibly one less, if there's " "an 'auto-fit' track, since that can collapse away)"); - numSizes = aTrackSizes->Length(); + numSizes = aTrackInfo->mSizes.Length(); } // An empty is represented as "none" in syntax. @@ -2512,29 +2512,32 @@ nsComputedDOMStyle::GetGridTemplateColumnsRows(const nsStyleGridTemplate& aTrack } RefPtr valueList = GetROCSSValueList(false); - if (aTrackSizes) { + if (aTrackInfo) { // We've done layout on the grid and have resolved the sizes of its tracks, // so we'll return those sizes here. The grid spec says we MAY use // repeat(, Npx) here for consecutive tracks with the same // size, but that doesn't seem worth doing since even for repeat(auto-*) // the resolved size might differ for the repeated tracks. + const nsTArray& trackSizes = aTrackInfo->mSizes; + const uint32_t numExplicitTracks = aTrackInfo->mNumExplicitTracks; + const uint32_t numLeadingImplicitTracks = aTrackInfo->mNumLeadingImplicitTracks; MOZ_ASSERT(numSizes > 0 && - numSizes >= aNumLeadingImplicitTracks + aNumExplicitTracks); + numSizes >= numLeadingImplicitTracks + numExplicitTracks); // Add any leading implicit tracks. - for (uint32_t i = 0; i < aNumLeadingImplicitTracks; ++i) { + for (uint32_t i = 0; i < numLeadingImplicitTracks; ++i) { RefPtr val = new nsROCSSPrimitiveValue; - val->SetAppUnits((*aTrackSizes)[i]); + val->SetAppUnits(trackSizes[i]); valueList->AppendCSSValue(val.forget()); } // Then add any explicit tracks. - if (aNumExplicitTracks) { + if (numExplicitTracks) { int32_t endOfRepeat = 0; // first index after any repeat() tracks int32_t offsetToLastRepeat = 0; if (aTrackList.HasRepeatAuto()) { // offsetToLastRepeat is -1 if all repeat(auto-fit) tracks are empty - offsetToLastRepeat = aNumExplicitTracks + 1 - aTrackList.mLineNameLists.Length(); + offsetToLastRepeat = numExplicitTracks + 1 - aTrackList.mLineNameLists.Length(); endOfRepeat = aTrackList.mRepeatAutoIndex + offsetToLastRepeat + 1; } for (int32_t i = 0;; i++) { @@ -2569,20 +2572,20 @@ nsComputedDOMStyle::GetGridTemplateColumnsRows(const nsStyleGridTemplate& aTrack const nsTArray& lineNames = aTrackList.mLineNameLists[i]; AppendGridLineNames(valueList, lineNames); } - if (uint32_t(i) == aNumExplicitTracks) { + if (uint32_t(i) == numExplicitTracks) { break; } RefPtr val = new nsROCSSPrimitiveValue; - val->SetAppUnits((*aTrackSizes)[i + aNumLeadingImplicitTracks]); + val->SetAppUnits(trackSizes[i + numLeadingImplicitTracks]); valueList->AppendCSSValue(val.forget()); } } // Add any trailing implicit tracks. - for (uint32_t i = aNumLeadingImplicitTracks + aNumExplicitTracks; + for (uint32_t i = numLeadingImplicitTracks + numExplicitTracks; i < numSizes; ++i) { RefPtr val = new nsROCSSPrimitiveValue; - val->SetAppUnits((*aTrackSizes)[i]); + val->SetAppUnits(trackSizes[i]); valueList->AppendCSSValue(val.forget()); } } else { @@ -2656,45 +2659,31 @@ nsComputedDOMStyle::DoGetGridAutoRows() already_AddRefed nsComputedDOMStyle::DoGetGridTemplateColumns() { - const nsTArray* trackSizes = nullptr; - uint32_t numLeadingImplicitTracks = 0; - uint32_t numExplicitTracks = 0; + const ComputedGridTrackInfo* info = nullptr; if (mInnerFrame) { nsIFrame* gridContainerCandidate = mInnerFrame->GetContentInsertionFrame(); if (gridContainerCandidate && gridContainerCandidate->GetType() == nsGkAtoms::gridContainerFrame) { - auto gridContainer = static_cast(gridContainerCandidate); - trackSizes = gridContainer->GetComputedTemplateColumns(); - numLeadingImplicitTracks = gridContainer->NumImplicitLeadingCols(); - numExplicitTracks = gridContainer->NumExplicitCols(); + info = static_cast(gridContainerCandidate)-> + GetComputedTemplateColumns(); } } - return GetGridTemplateColumnsRows(StylePosition()->mGridTemplateColumns, - numLeadingImplicitTracks, - numExplicitTracks, - trackSizes); + return GetGridTemplateColumnsRows(StylePosition()->mGridTemplateColumns, info); } already_AddRefed nsComputedDOMStyle::DoGetGridTemplateRows() { - const nsTArray* trackSizes = nullptr; - uint32_t numLeadingImplicitTracks = 0; - uint32_t numExplicitTracks = 0; + const ComputedGridTrackInfo* info = nullptr; if (mInnerFrame) { nsIFrame* gridContainerCandidate = mInnerFrame->GetContentInsertionFrame(); if (gridContainerCandidate && gridContainerCandidate->GetType() == nsGkAtoms::gridContainerFrame) { - auto gridContainer = static_cast(gridContainerCandidate); - trackSizes = gridContainer->GetComputedTemplateRows(); - numLeadingImplicitTracks = gridContainer->NumImplicitLeadingRows(); - numExplicitTracks = gridContainer->NumExplicitRows(); + info = static_cast(gridContainerCandidate)-> + GetComputedTemplateRows(); } } - return GetGridTemplateColumnsRows(StylePosition()->mGridTemplateRows, - numLeadingImplicitTracks, - numExplicitTracks, - trackSizes); + return GetGridTemplateColumnsRows(StylePosition()->mGridTemplateRows, info); } already_AddRefed diff --git a/layout/style/nsComputedDOMStyle.h b/layout/style/nsComputedDOMStyle.h index e194cf280c01..545cbcbc0f93 100644 --- a/layout/style/nsComputedDOMStyle.h +++ b/layout/style/nsComputedDOMStyle.h @@ -27,6 +27,7 @@ namespace mozilla { namespace dom { class Element; } // namespace dom +struct ComputedGridTrackInfo; } // namespace mozilla struct nsComputedStyleMap; @@ -192,9 +193,7 @@ private: const nsStyleCoord& aMaxSize); already_AddRefed GetGridTemplateColumnsRows( const nsStyleGridTemplate& aTrackList, - const uint32_t aNumLeadingImplicitTracks, - const uint32_t aNumExplicitTracks, - const nsTArray* aTrackSizes); + const mozilla::ComputedGridTrackInfo* aTrackInfo); already_AddRefed GetGridLine(const nsStyleGridLine& aGridLine); bool GetLineHeightCoord(nscoord& aCoord);