From d6b03337dca6a0cf6804460e01fa6d32dcf95489 Mon Sep 17 00:00:00 2001 From: "karnaze%netscape.com" Date: Tue, 18 May 1999 05:18:16 +0000 Subject: [PATCH] support for css padding in table cells git-svn-id: svn://10.0.0.236/trunk@32042 18797224-902f-48f8-a5cc-f745e15eee43 --- .../html/content/src/nsHTMLTableElement.cpp | 8 +-- .../html/content/src/nsHTMLTableElement.cpp | 8 +-- mozilla/layout/html/document/src/ua.css | 5 +- .../table/src/BasicTableLayoutStrategy.cpp | 65 +++++++++++++++---- .../html/table/src/BasicTableLayoutStrategy.h | 2 + .../html/table/src/nsTableCellFrame.cpp | 48 +++++++++----- .../layout/html/table/src/nsTableCellFrame.h | 4 +- .../layout/html/table/src/nsTableFrame.cpp | 31 ++------- mozilla/layout/html/table/src/nsTableFrame.h | 3 - mozilla/layout/style/ua.css | 5 +- .../tables/BasicTableLayoutStrategy.cpp | 65 +++++++++++++++---- .../layout/tables/BasicTableLayoutStrategy.h | 2 + mozilla/layout/tables/nsTableCellFrame.cpp | 48 +++++++++----- mozilla/layout/tables/nsTableCellFrame.h | 4 +- mozilla/layout/tables/nsTableFrame.cpp | 31 ++------- mozilla/layout/tables/nsTableFrame.h | 3 - 16 files changed, 190 insertions(+), 142 deletions(-) diff --git a/mozilla/content/html/content/src/nsHTMLTableElement.cpp b/mozilla/content/html/content/src/nsHTMLTableElement.cpp index 05a29337f7d..276c3a19649 100644 --- a/mozilla/content/html/content/src/nsHTMLTableElement.cpp +++ b/mozilla/content/html/content/src/nsHTMLTableElement.cpp @@ -1176,6 +1176,7 @@ MapAttributesInto(nsIHTMLAttributes* aAttributes, } // cellspacing (reuses tableStyle if already resolved) + // ua.css sets cellspacing aAttributes->GetAttribute(nsHTMLAtoms::cellspacing, value); if (value.GetUnit() == eHTMLUnit_Pixel) { if (nsnull==tableStyle) @@ -1183,13 +1184,6 @@ MapAttributesInto(nsIHTMLAttributes* aAttributes, tableStyle->mBorderSpacingX.SetCoordValue(NSIntPixelsToTwips(value.GetPixelValue(), p2t)); tableStyle->mBorderSpacingY.SetCoordValue(NSIntPixelsToTwips(value.GetPixelValue(), p2t)); } - else - { // XXX: remove me as soon as we get this from the style sheet - if (nsnull==tableStyle) - tableStyle = (nsStyleTable*)aContext->GetMutableStyleData(eStyleStruct_Table); - tableStyle->mBorderSpacingX.SetCoordValue(NSIntPixelsToTwips(2, p2t)); - tableStyle->mBorderSpacingY.SetCoordValue(NSIntPixelsToTwips(2, p2t)); - } // cols aAttributes->GetAttribute(nsHTMLAtoms::cols, value); diff --git a/mozilla/layout/html/content/src/nsHTMLTableElement.cpp b/mozilla/layout/html/content/src/nsHTMLTableElement.cpp index 05a29337f7d..276c3a19649 100644 --- a/mozilla/layout/html/content/src/nsHTMLTableElement.cpp +++ b/mozilla/layout/html/content/src/nsHTMLTableElement.cpp @@ -1176,6 +1176,7 @@ MapAttributesInto(nsIHTMLAttributes* aAttributes, } // cellspacing (reuses tableStyle if already resolved) + // ua.css sets cellspacing aAttributes->GetAttribute(nsHTMLAtoms::cellspacing, value); if (value.GetUnit() == eHTMLUnit_Pixel) { if (nsnull==tableStyle) @@ -1183,13 +1184,6 @@ MapAttributesInto(nsIHTMLAttributes* aAttributes, tableStyle->mBorderSpacingX.SetCoordValue(NSIntPixelsToTwips(value.GetPixelValue(), p2t)); tableStyle->mBorderSpacingY.SetCoordValue(NSIntPixelsToTwips(value.GetPixelValue(), p2t)); } - else - { // XXX: remove me as soon as we get this from the style sheet - if (nsnull==tableStyle) - tableStyle = (nsStyleTable*)aContext->GetMutableStyleData(eStyleStruct_Table); - tableStyle->mBorderSpacingX.SetCoordValue(NSIntPixelsToTwips(2, p2t)); - tableStyle->mBorderSpacingY.SetCoordValue(NSIntPixelsToTwips(2, p2t)); - } // cols aAttributes->GetAttribute(nsHTMLAtoms::cols, value); diff --git a/mozilla/layout/html/document/src/ua.css b/mozilla/layout/html/document/src/ua.css index 0879988d4df..cc212e969f8 100644 --- a/mozilla/layout/html/document/src/ua.css +++ b/mozilla/layout/html/document/src/ua.css @@ -131,12 +131,13 @@ table { display: table; /* border-style: outset; */ border-color: #c0c0c0; - cell-spacing: 2px; - cell-padding: 2px; + border-spacing: 2px; margin-top: 0; margin-bottom: 0; background: transparent; } + +/* must never set padding in td, th */ td, th { vertical-align: inherit; background-color: inherit; diff --git a/mozilla/layout/html/table/src/BasicTableLayoutStrategy.cpp b/mozilla/layout/html/table/src/BasicTableLayoutStrategy.cpp index a9947f82a87..6627d57e0b7 100644 --- a/mozilla/layout/html/table/src/BasicTableLayoutStrategy.cpp +++ b/mozilla/layout/html/table/src/BasicTableLayoutStrategy.cpp @@ -345,6 +345,47 @@ PRBool BasicTableLayoutStrategy::BalanceColumnWidths(nsIStyleContext* aT return result; } +nscoord BasicTableLayoutStrategy::CalcHorizontalPadding(PRInt32 aColX) +{ + nscoord maxLeftPadding = 0; // max left padding for cells starting in this col + nscoord maxRightPadding = 0; // max right padding for cells ending in this col + PRInt32 numRows = mTableFrame->GetRowCount(); + + for (PRInt32 rowX = 0; rowX < numRows; rowX++) { + nsTableCellFrame* cellFrame = mTableFrame->GetCellFrameAt(rowX, aColX); + if (nsnull == cellFrame) { // there is no cell in this row that corresponds to this column + continue; + } + PRInt32 cellRowX; + cellFrame->GetRowIndex(cellRowX); + if (rowX != cellRowX) { // don't do anything except for 1st row this cell is in + continue; + } + const nsStyleSpacing* spacing; + cellFrame->GetStyleData(eStyleStruct_Spacing,(const nsStyleStruct *&)spacing); + nsMargin marg; + spacing->CalcPaddingFor(cellFrame, marg); + maxLeftPadding = PR_MAX(maxLeftPadding, marg.left); + + PRInt32 colSpan = mTableFrame->GetEffectiveColSpan(aColX, cellFrame); + + nsStyleCoord coord; + PRInt32 cellColX; + cellFrame->GetColIndex(cellColX); + if (aColX != cellColX) { // the cell does not originate in this col + if (-1 == aColX - cellColX - colSpan) { // the cell ends in this col + spacing->mPadding.GetLeft(coord); + maxRightPadding = PR_MAX(maxLeftPadding, marg.right); + } + continue; + } + if (1 == colSpan) { // the cell ends in this col + maxRightPadding = PR_MAX(maxLeftPadding, marg.right); + } + } + return maxLeftPadding + maxRightPadding; +} + // Step 1 - assign the width of all fixed-width columns, all other columns get there max, // and calculate min/max table width PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() @@ -362,11 +403,9 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() maxColWidthArray = new PRInt32[mNumCols]; } - nscoord cellPadding = mTableFrame->GetCellPadding(); - TDBG_SD("table cell padding = %d\n", cellPadding); - PRInt32 numRows = mTableFrame->GetRowCount(); PRInt32 colIndex, rowIndex; + PRInt32* horPadding = new PRInt32[mNumCols]; // for every column, determine it's min and max width, and keep track of the table width for (colIndex = 0; colIndex < mNumCols; colIndex++) { @@ -380,7 +419,7 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() PRBool haveColWidth = PR_FALSE; // if true, the column has a width either from HTML width attribute, // from a style rule on the column, // or from a width attr/style on a cell that has colspan==1 - + horPadding[colIndex] = 0; // Get column information nsTableColFrame* colFrame = mTableFrame->GetColFrame(colIndex); TDBG_SDD("BTLS::APCW - got colFrame %p for colIndex %d\n", colFrame, colIndex); @@ -396,7 +435,8 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() if (eStyleUnit_Coord == colPosition->mWidth.GetUnit()) { haveColWidth = PR_TRUE; specifiedFixedColWidth = colPosition->mWidth.GetCoordValue(); - specifiedFixedColWidth += (cellPadding*2); + horPadding[colIndex] = CalcHorizontalPadding(colIndex); + specifiedFixedColWidth += horPadding[colIndex]; TDBG_SD("BTLS::APCW - got specified col width = %d\n", specifiedFixedColWidth); } @@ -421,8 +461,10 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() NS_ASSERTION(1 != cellFrame->GetRowSpan(), "row index does not match row span"); // sanity check continue; } + PRInt32 colSpan = mTableFrame->GetEffectiveColSpan(colIndex, cellFrame); maxColSpan = PR_MAX(maxColSpan,colSpan); + PRInt32 cellColIndex; cellFrame->GetColIndex(cellColIndex); if (colIndex != cellColIndex) { @@ -437,12 +479,8 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() nscoord cellMinWidth = cellMinSize.width; if (1 == colSpan) { - if (0 == minColContentWidth) { - minColContentWidth = cellMinWidth; - } - else { - minColContentWidth = PR_MAX(minColContentWidth, cellMinWidth); - } + minColContentWidth = (0 == minColContentWidth) + ? cellMinWidth : PR_MAX(minColContentWidth, cellMinWidth); } else { minColContentWidth = PR_MAX(minColContentWidth, cellMinWidth/colSpan); // no need to divide this proportionately @@ -556,7 +594,7 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() } // end for (rowIndex = 0; rowIndex < numRows; rowIndex++) // adjust the "fixed" width for content that is too wide - if (effectiveMinColumnWidth > specifiedFixedColWidth) { + if (haveColWidth && (effectiveMinColumnWidth > specifiedFixedColWidth)) { specifiedFixedColWidth = effectiveMinColumnWidth; } // do all the global bookkeeping, factoring in margins @@ -707,7 +745,7 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() if (eStyleUnit_Coord == colPosition->mWidth.GetUnit()) { if (nsTableColFrame::eWIDTH_SOURCE_CELL_WITH_SPAN!=colFrame->GetWidthSource()) { nscoord specifiedFixedColWidth = colPosition->mWidth.GetCoordValue(); - specifiedFixedColWidth += (cellPadding*2); + specifiedFixedColWidth += horPadding[colIndex]; if (specifiedFixedColWidth>=colFrame->GetEffectiveMinColWidth()) { mTableFrame->SetColumnWidth(colIndex, specifiedFixedColWidth); colFrame->SetMaxColWidth(specifiedFixedColWidth); @@ -719,6 +757,7 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() } } } + delete [] horPadding; // now set the min and max table widths SetMinAndMaxTableWidths(); diff --git a/mozilla/layout/html/table/src/BasicTableLayoutStrategy.h b/mozilla/layout/html/table/src/BasicTableLayoutStrategy.h index a24086ce76c..121c4baafd5 100644 --- a/mozilla/layout/html/table/src/BasicTableLayoutStrategy.h +++ b/mozilla/layout/html/table/src/BasicTableLayoutStrategy.h @@ -277,6 +277,8 @@ protected: void GetColumnsThatActLikeAutoWidth(PRInt32& aOutNumColumns, PRInt32*& aOutColumnIndexes); + nscoord CalcHorizontalPadding(PRInt32 aColX); + protected: nsTableFrame * mTableFrame; diff --git a/mozilla/layout/html/table/src/nsTableCellFrame.cpp b/mozilla/layout/html/table/src/nsTableCellFrame.cpp index f87e6422bb0..c2ea25cf28c 100644 --- a/mozilla/layout/html/table/src/nsTableCellFrame.cpp +++ b/mozilla/layout/html/table/src/nsTableCellFrame.cpp @@ -678,8 +678,8 @@ NS_METHOD nsTableCellFrame::IR_StyleChanged(nsIPresContext& aPresContex */ void nsTableCellFrame::MapHTMLBorderStyle(nsIPresContext* aPresContext, nsStyleSpacing& aSpacingStyle, - nscoord aBorderWidth, - nsTableFrame *aTableFrame) + nscoord aBorderWidth, + nsTableFrame* aTableFrame) { nsStyleCoord width; width.SetCoordValue(aBorderWidth); @@ -704,7 +704,7 @@ void nsTableCellFrame::MapHTMLBorderStyle(nsIPresContext* aPresContext, nsIStyleContext* styleContext = nsnull; tableFrame->GetStyleContext(&styleContext); - + const nsStyleColor* colorData = nsStyleUtil::FindNonTransparentBackground(styleContext); NS_IF_RELEASE(styleContext); @@ -754,7 +754,7 @@ void nsTableCellFrame::MapHTMLBorderStyle(nsIPresContext* aPresContext, */ break; case NS_STYLE_TABLE_RULES_COLS: - aSpacingStyle.SetBorderStyle(NS_SIDE_TOP, NS_STYLE_BORDER_STYLE_NONE); + aSpacingStyle.SetBorderStyle(NS_SIDE_TOP, NS_STYLE_BORDER_STYLE_NONE); aSpacingStyle.SetBorderStyle(NS_SIDE_BOTTOM, NS_STYLE_BORDER_STYLE_NONE); break; @@ -796,11 +796,8 @@ void nsTableCellFrame::MapBorderMarginPadding(nsIPresContext* aPresContext) if (!tableFrame) return; - nscoord padding = tableFrame->GetCellPadding(); nscoord spacingX = tableFrame->GetCellSpacingX(); nscoord spacingY = tableFrame->GetCellSpacingY(); - nscoord border = 1; - // get the table frame style context, and from it get cellpadding, cellspacing, and border info const nsStyleTable* tableStyle; @@ -809,23 +806,38 @@ void nsTableCellFrame::MapBorderMarginPadding(nsIPresContext* aPresContext) tableFrame->GetStyleData(eStyleStruct_Spacing,(const nsStyleStruct *&)tableSpacingStyle); nsStyleSpacing* spacingData = (nsStyleSpacing*)mStyleContext->GetMutableStyleData(eStyleStruct_Spacing); + // Cache the border-spacing into margin and wipe out any previous + // margins, since CSS doesn't allow margins to be set on cells spacingData->mMargin.SetTop(spacingY); - spacingData->mMargin.SetLeft(spacingX); - spacingData->mMargin.SetBottom(spacingY); spacingData->mMargin.SetRight(spacingX); - spacingData->mPadding.SetTop(padding); - spacingData->mPadding.SetLeft(padding); - spacingData->mPadding.SetBottom(padding); - spacingData->mPadding.SetRight(padding); + spacingData->mMargin.SetBottom(spacingY); + spacingData->mMargin.SetLeft(spacingX); + + float p2t; + aPresContext->GetPixelsToTwips(&p2t); + + // Get the table's cellpadding or use 2 pixels as the default if it is not set. + // This assumes that ua.css does not set padding for the cell. + nscoord defaultPadding = tableFrame->GetCellPadding(); + if (-1 == defaultPadding) { // not set in table + defaultPadding = NSIntPixelsToTwips(1, p2t); + } + + // if the padding is not already set, set it to the table's cellpadding + if (eHTMLUnit_Null == spacingData->mPadding.GetTopUnit()) + spacingData->mPadding.SetTop(defaultPadding); + if (eHTMLUnit_Null == spacingData->mPadding.GetRightUnit()) + spacingData->mPadding.SetRight(defaultPadding); + if (eHTMLUnit_Null == spacingData->mPadding.GetBottomUnit()) + spacingData->mPadding.SetBottom(defaultPadding); + if (eHTMLUnit_Null == spacingData->mPadding.GetLeftUnit()) + spacingData->mPadding.SetLeft(defaultPadding); // get border information from the table - if (tableStyle->mRules!= NS_STYLE_TABLE_RULES_NONE) - { + if (tableStyle->mRules!= NS_STYLE_TABLE_RULES_NONE) { // XXX: need to get border width here // in HTML, cell borders are always 1 pixel by default - float p2t; - aPresContext->GetScaledPixelsToTwips(&p2t); - border = NSIntPixelsToTwips(1, p2t); + nscoord border = NSIntPixelsToTwips(1, p2t); MapHTMLBorderStyle(aPresContext, *spacingData, border, tableFrame); } diff --git a/mozilla/layout/html/table/src/nsTableCellFrame.h b/mozilla/layout/html/table/src/nsTableCellFrame.h index 23376a5b3cb..8cd9d7d5dbd 100644 --- a/mozilla/layout/html/table/src/nsTableCellFrame.h +++ b/mozilla/layout/html/table/src/nsTableCellFrame.h @@ -200,8 +200,8 @@ protected: void MapHTMLBorderStyle(nsIPresContext* aPresContext, nsStyleSpacing& aSpacingStyle, - nscoord aBorderWidth, - nsTableFrame *aTableFrame); + nscoord aBorderWidth, + nsTableFrame* aTableFrame); void MapVAlignAttribute(nsIPresContext* aPresContext, nsTableFrame *aTableFrame); void MapHAlignAttribute(nsIPresContext* aPresContext, nsTableFrame *aTableFrame); diff --git a/mozilla/layout/html/table/src/nsTableFrame.cpp b/mozilla/layout/html/table/src/nsTableFrame.cpp index 0e5227502da..9df87cdd063 100644 --- a/mozilla/layout/html/table/src/nsTableFrame.cpp +++ b/mozilla/layout/html/table/src/nsTableFrame.cpp @@ -290,9 +290,6 @@ nsTableFrame::nsTableFrame() mColumnWidths = new PRInt32[mColumnWidthsLength]; nsCRT::memset (mColumnWidths, 0, mColumnWidthsLength*sizeof(PRInt32)); mCellMap = new nsCellMap(0, 0); - mDefaultCellSpacingX=0; - mDefaultCellSpacingY=0; - mDefaultCellPadding=0; mBorderEdges.mOutsideEdge=PR_TRUE; } @@ -304,12 +301,6 @@ nsTableFrame::Init(nsIPresContext& aPresContext, nsIFrame* aPrevInFlow) { nsresult rv; - float p2t; - - aPresContext.GetPixelsToTwips(&p2t); - mDefaultCellSpacingX = NSIntPixelsToTwips(2, p2t); - mDefaultCellSpacingY = NSIntPixelsToTwips(2, p2t); - mDefaultCellPadding = NSIntPixelsToTwips(1, p2t); // Let the base class do its processing rv = nsHTMLContainerFrame::Init(aPresContext, aContent, aParent, aContext, @@ -4850,50 +4841,38 @@ nscoord nsTableFrame::GetCellSpacingX() GetStyleData(eStyleStruct_Table, (const nsStyleStruct *&)tableStyle); nscoord cellSpacing = 0; PRUint8 borderCollapseStyle = GetBorderCollapseStyle(); - if (NS_STYLE_BORDER_COLLAPSE!=borderCollapseStyle) - { + if (NS_STYLE_BORDER_COLLAPSE!=borderCollapseStyle) { if (tableStyle->mBorderSpacingX.GetUnit() == eStyleUnit_Coord) { cellSpacing = tableStyle->mBorderSpacingX.GetCoordValue(); } - else { - cellSpacing = mDefaultCellSpacingX; - } } return cellSpacing; } -// XXX: could cache this. But be sure to check style changes if you do! +// XXX: could cache this. But be sure to check style changes if you do! nscoord nsTableFrame::GetCellSpacingY() { const nsStyleTable* tableStyle; GetStyleData(eStyleStruct_Table, (const nsStyleStruct *&)tableStyle); nscoord cellSpacing = 0; PRUint8 borderCollapseStyle = GetBorderCollapseStyle(); - if (NS_STYLE_BORDER_COLLAPSE!=borderCollapseStyle) - { + if (NS_STYLE_BORDER_COLLAPSE!=borderCollapseStyle) { if (tableStyle->mBorderSpacingY.GetUnit() == eStyleUnit_Coord) { cellSpacing = tableStyle->mBorderSpacingY.GetCoordValue(); } - else { - cellSpacing = mDefaultCellSpacingY; - } } return cellSpacing; } - -// XXX: could cache this. But be sure to check style changes if you do! +// Get the cellpadding defined on the table. Each cell can override this with style nscoord nsTableFrame::GetCellPadding() { const nsStyleTable* tableStyle; GetStyleData(eStyleStruct_Table, (const nsStyleStruct *&)tableStyle); - nscoord cellPadding = 0; + nscoord cellPadding = -1; if (tableStyle->mCellPadding.GetUnit() == eStyleUnit_Coord) { cellPadding = tableStyle->mCellPadding.GetCoordValue(); } - else { - cellPadding = mDefaultCellPadding; - } return cellPadding; } diff --git a/mozilla/layout/html/table/src/nsTableFrame.h b/mozilla/layout/html/table/src/nsTableFrame.h index bc9fe1b5d5f..51b1d2fe744 100644 --- a/mozilla/layout/html/table/src/nsTableFrame.h +++ b/mozilla/layout/html/table/src/nsTableFrame.h @@ -828,9 +828,6 @@ protected: ColumnInfoCache *mColCache; // cached information about the table columns nsITableLayoutStrategy * mTableLayoutStrategy; // the layout strategy for this frame nsFrameList mColGroups; // the list of colgroup frames - nscoord mDefaultCellSpacingX;// the default cell spacing X for this table - nscoord mDefaultCellSpacingY;// the default cell spacing X for this table - nscoord mDefaultCellPadding; // the default cell padding for this table nsBorderEdges mBorderEdges; // one list of border segments for each side of the table frame // used only for the collapsing border model diff --git a/mozilla/layout/style/ua.css b/mozilla/layout/style/ua.css index 0879988d4df..cc212e969f8 100644 --- a/mozilla/layout/style/ua.css +++ b/mozilla/layout/style/ua.css @@ -131,12 +131,13 @@ table { display: table; /* border-style: outset; */ border-color: #c0c0c0; - cell-spacing: 2px; - cell-padding: 2px; + border-spacing: 2px; margin-top: 0; margin-bottom: 0; background: transparent; } + +/* must never set padding in td, th */ td, th { vertical-align: inherit; background-color: inherit; diff --git a/mozilla/layout/tables/BasicTableLayoutStrategy.cpp b/mozilla/layout/tables/BasicTableLayoutStrategy.cpp index a9947f82a87..6627d57e0b7 100644 --- a/mozilla/layout/tables/BasicTableLayoutStrategy.cpp +++ b/mozilla/layout/tables/BasicTableLayoutStrategy.cpp @@ -345,6 +345,47 @@ PRBool BasicTableLayoutStrategy::BalanceColumnWidths(nsIStyleContext* aT return result; } +nscoord BasicTableLayoutStrategy::CalcHorizontalPadding(PRInt32 aColX) +{ + nscoord maxLeftPadding = 0; // max left padding for cells starting in this col + nscoord maxRightPadding = 0; // max right padding for cells ending in this col + PRInt32 numRows = mTableFrame->GetRowCount(); + + for (PRInt32 rowX = 0; rowX < numRows; rowX++) { + nsTableCellFrame* cellFrame = mTableFrame->GetCellFrameAt(rowX, aColX); + if (nsnull == cellFrame) { // there is no cell in this row that corresponds to this column + continue; + } + PRInt32 cellRowX; + cellFrame->GetRowIndex(cellRowX); + if (rowX != cellRowX) { // don't do anything except for 1st row this cell is in + continue; + } + const nsStyleSpacing* spacing; + cellFrame->GetStyleData(eStyleStruct_Spacing,(const nsStyleStruct *&)spacing); + nsMargin marg; + spacing->CalcPaddingFor(cellFrame, marg); + maxLeftPadding = PR_MAX(maxLeftPadding, marg.left); + + PRInt32 colSpan = mTableFrame->GetEffectiveColSpan(aColX, cellFrame); + + nsStyleCoord coord; + PRInt32 cellColX; + cellFrame->GetColIndex(cellColX); + if (aColX != cellColX) { // the cell does not originate in this col + if (-1 == aColX - cellColX - colSpan) { // the cell ends in this col + spacing->mPadding.GetLeft(coord); + maxRightPadding = PR_MAX(maxLeftPadding, marg.right); + } + continue; + } + if (1 == colSpan) { // the cell ends in this col + maxRightPadding = PR_MAX(maxLeftPadding, marg.right); + } + } + return maxLeftPadding + maxRightPadding; +} + // Step 1 - assign the width of all fixed-width columns, all other columns get there max, // and calculate min/max table width PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() @@ -362,11 +403,9 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() maxColWidthArray = new PRInt32[mNumCols]; } - nscoord cellPadding = mTableFrame->GetCellPadding(); - TDBG_SD("table cell padding = %d\n", cellPadding); - PRInt32 numRows = mTableFrame->GetRowCount(); PRInt32 colIndex, rowIndex; + PRInt32* horPadding = new PRInt32[mNumCols]; // for every column, determine it's min and max width, and keep track of the table width for (colIndex = 0; colIndex < mNumCols; colIndex++) { @@ -380,7 +419,7 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() PRBool haveColWidth = PR_FALSE; // if true, the column has a width either from HTML width attribute, // from a style rule on the column, // or from a width attr/style on a cell that has colspan==1 - + horPadding[colIndex] = 0; // Get column information nsTableColFrame* colFrame = mTableFrame->GetColFrame(colIndex); TDBG_SDD("BTLS::APCW - got colFrame %p for colIndex %d\n", colFrame, colIndex); @@ -396,7 +435,8 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() if (eStyleUnit_Coord == colPosition->mWidth.GetUnit()) { haveColWidth = PR_TRUE; specifiedFixedColWidth = colPosition->mWidth.GetCoordValue(); - specifiedFixedColWidth += (cellPadding*2); + horPadding[colIndex] = CalcHorizontalPadding(colIndex); + specifiedFixedColWidth += horPadding[colIndex]; TDBG_SD("BTLS::APCW - got specified col width = %d\n", specifiedFixedColWidth); } @@ -421,8 +461,10 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() NS_ASSERTION(1 != cellFrame->GetRowSpan(), "row index does not match row span"); // sanity check continue; } + PRInt32 colSpan = mTableFrame->GetEffectiveColSpan(colIndex, cellFrame); maxColSpan = PR_MAX(maxColSpan,colSpan); + PRInt32 cellColIndex; cellFrame->GetColIndex(cellColIndex); if (colIndex != cellColIndex) { @@ -437,12 +479,8 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() nscoord cellMinWidth = cellMinSize.width; if (1 == colSpan) { - if (0 == minColContentWidth) { - minColContentWidth = cellMinWidth; - } - else { - minColContentWidth = PR_MAX(minColContentWidth, cellMinWidth); - } + minColContentWidth = (0 == minColContentWidth) + ? cellMinWidth : PR_MAX(minColContentWidth, cellMinWidth); } else { minColContentWidth = PR_MAX(minColContentWidth, cellMinWidth/colSpan); // no need to divide this proportionately @@ -556,7 +594,7 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() } // end for (rowIndex = 0; rowIndex < numRows; rowIndex++) // adjust the "fixed" width for content that is too wide - if (effectiveMinColumnWidth > specifiedFixedColWidth) { + if (haveColWidth && (effectiveMinColumnWidth > specifiedFixedColWidth)) { specifiedFixedColWidth = effectiveMinColumnWidth; } // do all the global bookkeeping, factoring in margins @@ -707,7 +745,7 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() if (eStyleUnit_Coord == colPosition->mWidth.GetUnit()) { if (nsTableColFrame::eWIDTH_SOURCE_CELL_WITH_SPAN!=colFrame->GetWidthSource()) { nscoord specifiedFixedColWidth = colPosition->mWidth.GetCoordValue(); - specifiedFixedColWidth += (cellPadding*2); + specifiedFixedColWidth += horPadding[colIndex]; if (specifiedFixedColWidth>=colFrame->GetEffectiveMinColWidth()) { mTableFrame->SetColumnWidth(colIndex, specifiedFixedColWidth); colFrame->SetMaxColWidth(specifiedFixedColWidth); @@ -719,6 +757,7 @@ PRBool BasicTableLayoutStrategy::AssignPreliminaryColumnWidths() } } } + delete [] horPadding; // now set the min and max table widths SetMinAndMaxTableWidths(); diff --git a/mozilla/layout/tables/BasicTableLayoutStrategy.h b/mozilla/layout/tables/BasicTableLayoutStrategy.h index a24086ce76c..121c4baafd5 100644 --- a/mozilla/layout/tables/BasicTableLayoutStrategy.h +++ b/mozilla/layout/tables/BasicTableLayoutStrategy.h @@ -277,6 +277,8 @@ protected: void GetColumnsThatActLikeAutoWidth(PRInt32& aOutNumColumns, PRInt32*& aOutColumnIndexes); + nscoord CalcHorizontalPadding(PRInt32 aColX); + protected: nsTableFrame * mTableFrame; diff --git a/mozilla/layout/tables/nsTableCellFrame.cpp b/mozilla/layout/tables/nsTableCellFrame.cpp index f87e6422bb0..c2ea25cf28c 100644 --- a/mozilla/layout/tables/nsTableCellFrame.cpp +++ b/mozilla/layout/tables/nsTableCellFrame.cpp @@ -678,8 +678,8 @@ NS_METHOD nsTableCellFrame::IR_StyleChanged(nsIPresContext& aPresContex */ void nsTableCellFrame::MapHTMLBorderStyle(nsIPresContext* aPresContext, nsStyleSpacing& aSpacingStyle, - nscoord aBorderWidth, - nsTableFrame *aTableFrame) + nscoord aBorderWidth, + nsTableFrame* aTableFrame) { nsStyleCoord width; width.SetCoordValue(aBorderWidth); @@ -704,7 +704,7 @@ void nsTableCellFrame::MapHTMLBorderStyle(nsIPresContext* aPresContext, nsIStyleContext* styleContext = nsnull; tableFrame->GetStyleContext(&styleContext); - + const nsStyleColor* colorData = nsStyleUtil::FindNonTransparentBackground(styleContext); NS_IF_RELEASE(styleContext); @@ -754,7 +754,7 @@ void nsTableCellFrame::MapHTMLBorderStyle(nsIPresContext* aPresContext, */ break; case NS_STYLE_TABLE_RULES_COLS: - aSpacingStyle.SetBorderStyle(NS_SIDE_TOP, NS_STYLE_BORDER_STYLE_NONE); + aSpacingStyle.SetBorderStyle(NS_SIDE_TOP, NS_STYLE_BORDER_STYLE_NONE); aSpacingStyle.SetBorderStyle(NS_SIDE_BOTTOM, NS_STYLE_BORDER_STYLE_NONE); break; @@ -796,11 +796,8 @@ void nsTableCellFrame::MapBorderMarginPadding(nsIPresContext* aPresContext) if (!tableFrame) return; - nscoord padding = tableFrame->GetCellPadding(); nscoord spacingX = tableFrame->GetCellSpacingX(); nscoord spacingY = tableFrame->GetCellSpacingY(); - nscoord border = 1; - // get the table frame style context, and from it get cellpadding, cellspacing, and border info const nsStyleTable* tableStyle; @@ -809,23 +806,38 @@ void nsTableCellFrame::MapBorderMarginPadding(nsIPresContext* aPresContext) tableFrame->GetStyleData(eStyleStruct_Spacing,(const nsStyleStruct *&)tableSpacingStyle); nsStyleSpacing* spacingData = (nsStyleSpacing*)mStyleContext->GetMutableStyleData(eStyleStruct_Spacing); + // Cache the border-spacing into margin and wipe out any previous + // margins, since CSS doesn't allow margins to be set on cells spacingData->mMargin.SetTop(spacingY); - spacingData->mMargin.SetLeft(spacingX); - spacingData->mMargin.SetBottom(spacingY); spacingData->mMargin.SetRight(spacingX); - spacingData->mPadding.SetTop(padding); - spacingData->mPadding.SetLeft(padding); - spacingData->mPadding.SetBottom(padding); - spacingData->mPadding.SetRight(padding); + spacingData->mMargin.SetBottom(spacingY); + spacingData->mMargin.SetLeft(spacingX); + + float p2t; + aPresContext->GetPixelsToTwips(&p2t); + + // Get the table's cellpadding or use 2 pixels as the default if it is not set. + // This assumes that ua.css does not set padding for the cell. + nscoord defaultPadding = tableFrame->GetCellPadding(); + if (-1 == defaultPadding) { // not set in table + defaultPadding = NSIntPixelsToTwips(1, p2t); + } + + // if the padding is not already set, set it to the table's cellpadding + if (eHTMLUnit_Null == spacingData->mPadding.GetTopUnit()) + spacingData->mPadding.SetTop(defaultPadding); + if (eHTMLUnit_Null == spacingData->mPadding.GetRightUnit()) + spacingData->mPadding.SetRight(defaultPadding); + if (eHTMLUnit_Null == spacingData->mPadding.GetBottomUnit()) + spacingData->mPadding.SetBottom(defaultPadding); + if (eHTMLUnit_Null == spacingData->mPadding.GetLeftUnit()) + spacingData->mPadding.SetLeft(defaultPadding); // get border information from the table - if (tableStyle->mRules!= NS_STYLE_TABLE_RULES_NONE) - { + if (tableStyle->mRules!= NS_STYLE_TABLE_RULES_NONE) { // XXX: need to get border width here // in HTML, cell borders are always 1 pixel by default - float p2t; - aPresContext->GetScaledPixelsToTwips(&p2t); - border = NSIntPixelsToTwips(1, p2t); + nscoord border = NSIntPixelsToTwips(1, p2t); MapHTMLBorderStyle(aPresContext, *spacingData, border, tableFrame); } diff --git a/mozilla/layout/tables/nsTableCellFrame.h b/mozilla/layout/tables/nsTableCellFrame.h index 23376a5b3cb..8cd9d7d5dbd 100644 --- a/mozilla/layout/tables/nsTableCellFrame.h +++ b/mozilla/layout/tables/nsTableCellFrame.h @@ -200,8 +200,8 @@ protected: void MapHTMLBorderStyle(nsIPresContext* aPresContext, nsStyleSpacing& aSpacingStyle, - nscoord aBorderWidth, - nsTableFrame *aTableFrame); + nscoord aBorderWidth, + nsTableFrame* aTableFrame); void MapVAlignAttribute(nsIPresContext* aPresContext, nsTableFrame *aTableFrame); void MapHAlignAttribute(nsIPresContext* aPresContext, nsTableFrame *aTableFrame); diff --git a/mozilla/layout/tables/nsTableFrame.cpp b/mozilla/layout/tables/nsTableFrame.cpp index 0e5227502da..9df87cdd063 100644 --- a/mozilla/layout/tables/nsTableFrame.cpp +++ b/mozilla/layout/tables/nsTableFrame.cpp @@ -290,9 +290,6 @@ nsTableFrame::nsTableFrame() mColumnWidths = new PRInt32[mColumnWidthsLength]; nsCRT::memset (mColumnWidths, 0, mColumnWidthsLength*sizeof(PRInt32)); mCellMap = new nsCellMap(0, 0); - mDefaultCellSpacingX=0; - mDefaultCellSpacingY=0; - mDefaultCellPadding=0; mBorderEdges.mOutsideEdge=PR_TRUE; } @@ -304,12 +301,6 @@ nsTableFrame::Init(nsIPresContext& aPresContext, nsIFrame* aPrevInFlow) { nsresult rv; - float p2t; - - aPresContext.GetPixelsToTwips(&p2t); - mDefaultCellSpacingX = NSIntPixelsToTwips(2, p2t); - mDefaultCellSpacingY = NSIntPixelsToTwips(2, p2t); - mDefaultCellPadding = NSIntPixelsToTwips(1, p2t); // Let the base class do its processing rv = nsHTMLContainerFrame::Init(aPresContext, aContent, aParent, aContext, @@ -4850,50 +4841,38 @@ nscoord nsTableFrame::GetCellSpacingX() GetStyleData(eStyleStruct_Table, (const nsStyleStruct *&)tableStyle); nscoord cellSpacing = 0; PRUint8 borderCollapseStyle = GetBorderCollapseStyle(); - if (NS_STYLE_BORDER_COLLAPSE!=borderCollapseStyle) - { + if (NS_STYLE_BORDER_COLLAPSE!=borderCollapseStyle) { if (tableStyle->mBorderSpacingX.GetUnit() == eStyleUnit_Coord) { cellSpacing = tableStyle->mBorderSpacingX.GetCoordValue(); } - else { - cellSpacing = mDefaultCellSpacingX; - } } return cellSpacing; } -// XXX: could cache this. But be sure to check style changes if you do! +// XXX: could cache this. But be sure to check style changes if you do! nscoord nsTableFrame::GetCellSpacingY() { const nsStyleTable* tableStyle; GetStyleData(eStyleStruct_Table, (const nsStyleStruct *&)tableStyle); nscoord cellSpacing = 0; PRUint8 borderCollapseStyle = GetBorderCollapseStyle(); - if (NS_STYLE_BORDER_COLLAPSE!=borderCollapseStyle) - { + if (NS_STYLE_BORDER_COLLAPSE!=borderCollapseStyle) { if (tableStyle->mBorderSpacingY.GetUnit() == eStyleUnit_Coord) { cellSpacing = tableStyle->mBorderSpacingY.GetCoordValue(); } - else { - cellSpacing = mDefaultCellSpacingY; - } } return cellSpacing; } - -// XXX: could cache this. But be sure to check style changes if you do! +// Get the cellpadding defined on the table. Each cell can override this with style nscoord nsTableFrame::GetCellPadding() { const nsStyleTable* tableStyle; GetStyleData(eStyleStruct_Table, (const nsStyleStruct *&)tableStyle); - nscoord cellPadding = 0; + nscoord cellPadding = -1; if (tableStyle->mCellPadding.GetUnit() == eStyleUnit_Coord) { cellPadding = tableStyle->mCellPadding.GetCoordValue(); } - else { - cellPadding = mDefaultCellPadding; - } return cellPadding; } diff --git a/mozilla/layout/tables/nsTableFrame.h b/mozilla/layout/tables/nsTableFrame.h index bc9fe1b5d5f..51b1d2fe744 100644 --- a/mozilla/layout/tables/nsTableFrame.h +++ b/mozilla/layout/tables/nsTableFrame.h @@ -828,9 +828,6 @@ protected: ColumnInfoCache *mColCache; // cached information about the table columns nsITableLayoutStrategy * mTableLayoutStrategy; // the layout strategy for this frame nsFrameList mColGroups; // the list of colgroup frames - nscoord mDefaultCellSpacingX;// the default cell spacing X for this table - nscoord mDefaultCellSpacingY;// the default cell spacing X for this table - nscoord mDefaultCellPadding; // the default cell padding for this table nsBorderEdges mBorderEdges; // one list of border segments for each side of the table frame // used only for the collapsing border model