Bug 1240795 - [css-grid] Refactor GetComputedTemplateColumns/Rows to return a self-contained value. r=dholbert

This commit is contained in:
Mats Palmgren 2016-01-27 17:02:13 +01:00
parent d49d44a25a
commit 7fa47cb262
4 changed files with 68 additions and 68 deletions

View File

@ -3341,14 +3341,21 @@ nsGridContainerFrame::Reflow(nsPresContext* aPresContext,
for (const TrackSize& sz : gridReflowState.mCols.mSizes) {
colTrackSizes.AppendElement(sz.mBase);
}
Properties().Set(GridColTrackSizes(),
new nsTArray<nscoord>(mozilla::Move(colTrackSizes)));
ComputedGridTrackInfo* colInfo = new ComputedGridTrackInfo(
gridReflowState.mColFunctions.mExplicitGridOffset,
gridReflowState.mColFunctions.NumExplicitTracks(),
Move(colTrackSizes));
Properties().Set(GridColTrackInfo(), colInfo);
nsTArray<nscoord> rowTrackSizes(gridReflowState.mRows.mSizes.Length());
for (const TrackSize& sz : gridReflowState.mRows.mSizes) {
rowTrackSizes.AppendElement(sz.mBase);
}
Properties().Set(GridRowTrackSizes(),
new nsTArray<nscoord>(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) {

View File

@ -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<nscoord>&& aSizes)
: mNumLeadingImplicitTracks(aNumLeadingImplicitTracks)
, mNumExplicitTracks(aNumExplicitTracks)
, mSizes(aSizes)
{}
uint32_t mNumLeadingImplicitTracks;
uint32_t mNumExplicitTracks;
nsTArray<nscoord> mSizes;
};
} // namespace mozilla
class nsGridContainerFrame final : public nsContainerFrame
{
public:
@ -88,32 +107,18 @@ public:
NS_DECLARE_FRAME_PROPERTY(GridItemContainingBlockRect, DeleteValue<nsRect>)
NS_DECLARE_FRAME_PROPERTY(GridColTrackSizes, DeleteValue<nsTArray<nscoord>>)
const nsTArray<nscoord>* GetComputedTemplateColumns()
NS_DECLARE_FRAME_PROPERTY(GridColTrackInfo, DeleteValue<ComputedGridTrackInfo>)
const ComputedGridTrackInfo* GetComputedTemplateColumns()
{
return static_cast<nsTArray<nscoord>*>(Properties().Get(GridColTrackSizes()));
return static_cast<ComputedGridTrackInfo*>(Properties().Get(GridColTrackInfo()));
}
NS_DECLARE_FRAME_PROPERTY(GridRowTrackSizes, DeleteValue<nsTArray<nscoord>>)
const nsTArray<nscoord>* GetComputedTemplateRows()
NS_DECLARE_FRAME_PROPERTY(GridRowTrackInfo, DeleteValue<ComputedGridTrackInfo>)
const ComputedGridTrackInfo* GetComputedTemplateRows()
{
return static_cast<nsTArray<nscoord>*>(Properties().Get(GridRowTrackSizes()));
return static_cast<ComputedGridTrackInfo*>(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.

View File

@ -2449,10 +2449,9 @@ nsComputedDOMStyle::GetGridTrackSize(const nsStyleCoord& aMinValue,
}
already_AddRefed<CSSValue>
nsComputedDOMStyle::GetGridTemplateColumnsRows(const nsStyleGridTemplate& aTrackList,
const uint32_t aNumLeadingImplicitTracks,
const uint32_t aNumExplicitTracks,
const nsTArray<nscoord>* 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<bool> isAutoFill =
aTrackList.HasRepeatAuto() && aTrackList.mIsAutoFill;
DebugOnly<bool> isAutoFit =
aTrackList.HasRepeatAuto() && !aTrackList.mIsAutoFill;
MOZ_ASSERT(aNumExplicitTracks == numSizes ||
(isAutoFill && aNumExplicitTracks >= numSizes) ||
(isAutoFit && aNumExplicitTracks + 1 >= numSizes),
DebugOnly<uint32_t> 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 <track-list> is represented as "none" in syntax.
@ -2512,29 +2512,32 @@ nsComputedDOMStyle::GetGridTemplateColumnsRows(const nsStyleGridTemplate& aTrack
}
RefPtr<nsDOMCSSValueList> 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(<positive-integer>, 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<nscoord>& 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<nsROCSSPrimitiveValue> 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<nsString>& lineNames = aTrackList.mLineNameLists[i];
AppendGridLineNames(valueList, lineNames);
}
if (uint32_t(i) == aNumExplicitTracks) {
if (uint32_t(i) == numExplicitTracks) {
break;
}
RefPtr<nsROCSSPrimitiveValue> 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<nsROCSSPrimitiveValue> val = new nsROCSSPrimitiveValue;
val->SetAppUnits((*aTrackSizes)[i]);
val->SetAppUnits(trackSizes[i]);
valueList->AppendCSSValue(val.forget());
}
} else {
@ -2656,45 +2659,31 @@ nsComputedDOMStyle::DoGetGridAutoRows()
already_AddRefed<CSSValue>
nsComputedDOMStyle::DoGetGridTemplateColumns()
{
const nsTArray<nscoord>* 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<nsGridContainerFrame*>(gridContainerCandidate);
trackSizes = gridContainer->GetComputedTemplateColumns();
numLeadingImplicitTracks = gridContainer->NumImplicitLeadingCols();
numExplicitTracks = gridContainer->NumExplicitCols();
info = static_cast<nsGridContainerFrame*>(gridContainerCandidate)->
GetComputedTemplateColumns();
}
}
return GetGridTemplateColumnsRows(StylePosition()->mGridTemplateColumns,
numLeadingImplicitTracks,
numExplicitTracks,
trackSizes);
return GetGridTemplateColumnsRows(StylePosition()->mGridTemplateColumns, info);
}
already_AddRefed<CSSValue>
nsComputedDOMStyle::DoGetGridTemplateRows()
{
const nsTArray<nscoord>* 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<nsGridContainerFrame*>(gridContainerCandidate);
trackSizes = gridContainer->GetComputedTemplateRows();
numLeadingImplicitTracks = gridContainer->NumImplicitLeadingRows();
numExplicitTracks = gridContainer->NumExplicitRows();
info = static_cast<nsGridContainerFrame*>(gridContainerCandidate)->
GetComputedTemplateRows();
}
}
return GetGridTemplateColumnsRows(StylePosition()->mGridTemplateRows,
numLeadingImplicitTracks,
numExplicitTracks,
trackSizes);
return GetGridTemplateColumnsRows(StylePosition()->mGridTemplateRows, info);
}
already_AddRefed<CSSValue>

View File

@ -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<CSSValue> GetGridTemplateColumnsRows(
const nsStyleGridTemplate& aTrackList,
const uint32_t aNumLeadingImplicitTracks,
const uint32_t aNumExplicitTracks,
const nsTArray<nscoord>* aTrackSizes);
const mozilla::ComputedGridTrackInfo* aTrackInfo);
already_AddRefed<CSSValue> GetGridLine(const nsStyleGridLine& aGridLine);
bool GetLineHeightCoord(nscoord& aCoord);