michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsTArray.h" michael@0: #include "nsCellMap.h" michael@0: #include "nsTableFrame.h" michael@0: #include "nsTableCellFrame.h" michael@0: #include "nsTableRowFrame.h" michael@0: #include "nsTableRowGroupFrame.h" michael@0: #include michael@0: michael@0: michael@0: static void michael@0: SetDamageArea(int32_t aXOrigin, michael@0: int32_t aYOrigin, michael@0: int32_t aWidth, michael@0: int32_t aHeight, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: NS_ASSERTION(aXOrigin >= 0, "negative col index"); michael@0: NS_ASSERTION(aYOrigin >= 0, "negative row index"); michael@0: NS_ASSERTION(aWidth >= 0, "negative horizontal damage"); michael@0: NS_ASSERTION(aHeight >= 0, "negative vertical damage"); michael@0: aDamageArea.x = aXOrigin; michael@0: aDamageArea.y = aYOrigin; michael@0: aDamageArea.width = aWidth; michael@0: aDamageArea.height = aHeight; michael@0: } michael@0: michael@0: // Empty static array used for SafeElementAt() calls on mRows. michael@0: static nsCellMap::CellDataArray * sEmptyRow; michael@0: michael@0: // CellData michael@0: michael@0: CellData::CellData(nsTableCellFrame* aOrigCell) michael@0: { michael@0: MOZ_COUNT_CTOR(CellData); michael@0: static_assert(sizeof(mOrigCell) == sizeof(mBits), michael@0: "mOrigCell and mBits must be the same size"); michael@0: mOrigCell = aOrigCell; michael@0: } michael@0: michael@0: CellData::~CellData() michael@0: { michael@0: MOZ_COUNT_DTOR(CellData); michael@0: } michael@0: michael@0: BCCellData::BCCellData(nsTableCellFrame* aOrigCell) michael@0: :CellData(aOrigCell) michael@0: { michael@0: MOZ_COUNT_CTOR(BCCellData); michael@0: } michael@0: michael@0: BCCellData::~BCCellData() michael@0: { michael@0: MOZ_COUNT_DTOR(BCCellData); michael@0: } michael@0: michael@0: // nsTableCellMap michael@0: michael@0: nsTableCellMap::nsTableCellMap(nsTableFrame& aTableFrame, michael@0: bool aBorderCollapse) michael@0: :mTableFrame(aTableFrame), mFirstMap(nullptr), mBCInfo(nullptr) michael@0: { michael@0: MOZ_COUNT_CTOR(nsTableCellMap); michael@0: michael@0: nsTableFrame::RowGroupArray orderedRowGroups; michael@0: aTableFrame.OrderRowGroups(orderedRowGroups); michael@0: michael@0: nsTableRowGroupFrame* prior = nullptr; michael@0: for (uint32_t rgX = 0; rgX < orderedRowGroups.Length(); rgX++) { michael@0: nsTableRowGroupFrame* rgFrame = orderedRowGroups[rgX]; michael@0: InsertGroupCellMap(rgFrame, prior); michael@0: prior = rgFrame; michael@0: } michael@0: if (aBorderCollapse) { michael@0: mBCInfo = new BCInfo(); michael@0: } michael@0: } michael@0: michael@0: nsTableCellMap::~nsTableCellMap() michael@0: { michael@0: MOZ_COUNT_DTOR(nsTableCellMap); michael@0: michael@0: nsCellMap* cellMap = mFirstMap; michael@0: while (cellMap) { michael@0: nsCellMap* next = cellMap->GetNextSibling(); michael@0: delete cellMap; michael@0: cellMap = next; michael@0: } michael@0: michael@0: if (mBCInfo) { michael@0: DeleteRightBottomBorders(); michael@0: delete mBCInfo; michael@0: } michael@0: } michael@0: michael@0: // Get the bcData holding the border segments of the right edge of the table michael@0: BCData* michael@0: nsTableCellMap::GetRightMostBorder(int32_t aRowIndex) michael@0: { michael@0: if (!mBCInfo) ABORT1(nullptr); michael@0: michael@0: int32_t numRows = mBCInfo->mRightBorders.Length(); michael@0: if (aRowIndex < numRows) { michael@0: return &mBCInfo->mRightBorders.ElementAt(aRowIndex); michael@0: } michael@0: michael@0: mBCInfo->mRightBorders.SetLength(aRowIndex+1); michael@0: return &mBCInfo->mRightBorders.ElementAt(aRowIndex); michael@0: } michael@0: michael@0: // Get the bcData holding the border segments of the bottom edge of the table michael@0: BCData* michael@0: nsTableCellMap::GetBottomMostBorder(int32_t aColIndex) michael@0: { michael@0: if (!mBCInfo) ABORT1(nullptr); michael@0: michael@0: int32_t numCols = mBCInfo->mBottomBorders.Length(); michael@0: if (aColIndex < numCols) { michael@0: return &mBCInfo->mBottomBorders.ElementAt(aColIndex); michael@0: } michael@0: michael@0: mBCInfo->mBottomBorders.SetLength(aColIndex+1); michael@0: return &mBCInfo->mBottomBorders.ElementAt(aColIndex); michael@0: } michael@0: michael@0: // delete the borders corresponding to the right and bottom edges of the table michael@0: void michael@0: nsTableCellMap::DeleteRightBottomBorders() michael@0: { michael@0: if (mBCInfo) { michael@0: mBCInfo->mBottomBorders.Clear(); michael@0: mBCInfo->mRightBorders.Clear(); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsTableCellMap::InsertGroupCellMap(nsCellMap* aPrevMap, michael@0: nsCellMap& aNewMap) michael@0: { michael@0: nsCellMap* next; michael@0: if (aPrevMap) { michael@0: next = aPrevMap->GetNextSibling(); michael@0: aPrevMap->SetNextSibling(&aNewMap); michael@0: } michael@0: else { michael@0: next = mFirstMap; michael@0: mFirstMap = &aNewMap; michael@0: } michael@0: aNewMap.SetNextSibling(next); michael@0: } michael@0: michael@0: void nsTableCellMap::InsertGroupCellMap(nsTableRowGroupFrame* aNewGroup, michael@0: nsTableRowGroupFrame*& aPrevGroup) michael@0: { michael@0: nsCellMap* newMap = new nsCellMap(aNewGroup, mBCInfo != nullptr); michael@0: nsCellMap* prevMap = nullptr; michael@0: nsCellMap* lastMap = mFirstMap; michael@0: if (aPrevGroup) { michael@0: nsCellMap* map = mFirstMap; michael@0: while (map) { michael@0: lastMap = map; michael@0: if (map->GetRowGroup() == aPrevGroup) { michael@0: prevMap = map; michael@0: break; michael@0: } michael@0: map = map->GetNextSibling(); michael@0: } michael@0: } michael@0: if (!prevMap) { michael@0: if (aPrevGroup) { michael@0: prevMap = lastMap; michael@0: aPrevGroup = (prevMap) ? prevMap->GetRowGroup() : nullptr; michael@0: } michael@0: else { michael@0: aPrevGroup = nullptr; michael@0: } michael@0: } michael@0: InsertGroupCellMap(prevMap, *newMap); michael@0: } michael@0: michael@0: void nsTableCellMap::RemoveGroupCellMap(nsTableRowGroupFrame* aGroup) michael@0: { michael@0: nsCellMap* map = mFirstMap; michael@0: nsCellMap* prior = nullptr; michael@0: while (map) { michael@0: if (map->GetRowGroup() == aGroup) { michael@0: nsCellMap* next = map->GetNextSibling(); michael@0: if (mFirstMap == map) { michael@0: mFirstMap = next; michael@0: } michael@0: else { michael@0: prior->SetNextSibling(next); michael@0: } michael@0: delete map; michael@0: break; michael@0: } michael@0: prior = map; michael@0: map = map->GetNextSibling(); michael@0: } michael@0: } michael@0: michael@0: static nsCellMap* michael@0: FindMapFor(const nsTableRowGroupFrame* aRowGroup, michael@0: nsCellMap* aStart, michael@0: const nsCellMap* aEnd) michael@0: { michael@0: for (nsCellMap* map = aStart; map != aEnd; map = map->GetNextSibling()) { michael@0: if (aRowGroup == map->GetRowGroup()) { michael@0: return map; michael@0: } michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: nsCellMap* michael@0: nsTableCellMap::GetMapFor(const nsTableRowGroupFrame* aRowGroup, michael@0: nsCellMap* aStartHint) const michael@0: { michael@0: NS_PRECONDITION(aRowGroup, "Must have a rowgroup"); michael@0: NS_ASSERTION(!aRowGroup->GetPrevInFlow(), "GetMapFor called with continuation"); michael@0: if (aStartHint) { michael@0: nsCellMap* map = FindMapFor(aRowGroup, aStartHint, nullptr); michael@0: if (map) { michael@0: return map; michael@0: } michael@0: } michael@0: michael@0: nsCellMap* map = FindMapFor(aRowGroup, mFirstMap, aStartHint); michael@0: if (map) { michael@0: return map; michael@0: } michael@0: michael@0: // if aRowGroup is a repeated header or footer find the header or footer it was repeated from michael@0: if (aRowGroup->IsRepeatable()) { michael@0: nsTableFrame* fifTable = static_cast(mTableFrame.FirstInFlow()); michael@0: michael@0: const nsStyleDisplay* display = aRowGroup->StyleDisplay(); michael@0: nsTableRowGroupFrame* rgOrig = michael@0: (NS_STYLE_DISPLAY_TABLE_HEADER_GROUP == display->mDisplay) ? michael@0: fifTable->GetTHead() : fifTable->GetTFoot(); michael@0: // find the row group cell map using the original header/footer michael@0: if (rgOrig && rgOrig != aRowGroup) { michael@0: return GetMapFor(rgOrig, aStartHint); michael@0: } michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: void michael@0: nsTableCellMap::Synchronize(nsTableFrame* aTableFrame) michael@0: { michael@0: nsTableFrame::RowGroupArray orderedRowGroups; michael@0: nsAutoTArray maps; michael@0: michael@0: aTableFrame->OrderRowGroups(orderedRowGroups); michael@0: if (!orderedRowGroups.Length()) { michael@0: return; michael@0: } michael@0: michael@0: // XXXbz this fails if orderedRowGroups is missing some row groups michael@0: // (due to OOM when appending to the array, e.g. -- we leak maps in michael@0: // that case). michael@0: michael@0: // Scope |map| outside the loop so we can use it as a hint. michael@0: nsCellMap* map = nullptr; michael@0: for (uint32_t rgX = 0; rgX < orderedRowGroups.Length(); rgX++) { michael@0: nsTableRowGroupFrame* rgFrame = orderedRowGroups[rgX]; michael@0: map = GetMapFor(static_cast(rgFrame->FirstInFlow()), michael@0: map); michael@0: if (map) { michael@0: if (!maps.AppendElement(map)) { michael@0: delete map; michael@0: map = nullptr; michael@0: NS_WARNING("Could not AppendElement"); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: if (maps.IsEmpty()) { michael@0: MOZ_ASSERT(!mFirstMap); michael@0: return; michael@0: } michael@0: michael@0: int32_t mapIndex = maps.Length() - 1; // Might end up -1 michael@0: nsCellMap* nextMap = maps.ElementAt(mapIndex); michael@0: nextMap->SetNextSibling(nullptr); michael@0: for (mapIndex-- ; mapIndex >= 0; mapIndex--) { michael@0: nsCellMap* map = maps.ElementAt(mapIndex); michael@0: map->SetNextSibling(nextMap); michael@0: nextMap = map; michael@0: } michael@0: mFirstMap = nextMap; michael@0: } michael@0: michael@0: bool michael@0: nsTableCellMap::HasMoreThanOneCell(int32_t aRowIndex) const michael@0: { michael@0: int32_t rowIndex = aRowIndex; michael@0: nsCellMap* map = mFirstMap; michael@0: while (map) { michael@0: if (map->GetRowCount() > rowIndex) { michael@0: return map->HasMoreThanOneCell(rowIndex); michael@0: } michael@0: rowIndex -= map->GetRowCount(); michael@0: map = map->GetNextSibling(); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: int32_t michael@0: nsTableCellMap::GetNumCellsOriginatingInRow(int32_t aRowIndex) const michael@0: { michael@0: int32_t rowIndex = aRowIndex; michael@0: nsCellMap* map = mFirstMap; michael@0: while (map) { michael@0: if (map->GetRowCount() > rowIndex) { michael@0: return map->GetNumCellsOriginatingInRow(rowIndex); michael@0: } michael@0: rowIndex -= map->GetRowCount(); michael@0: map = map->GetNextSibling(); michael@0: } michael@0: return 0; michael@0: } michael@0: int32_t michael@0: nsTableCellMap::GetEffectiveRowSpan(int32_t aRowIndex, michael@0: int32_t aColIndex) const michael@0: { michael@0: int32_t rowIndex = aRowIndex; michael@0: nsCellMap* map = mFirstMap; michael@0: while (map) { michael@0: if (map->GetRowCount() > rowIndex) { michael@0: return map->GetRowSpan(rowIndex, aColIndex, true); michael@0: } michael@0: rowIndex -= map->GetRowCount(); michael@0: map = map->GetNextSibling(); michael@0: } michael@0: NS_NOTREACHED("Bogus row index?"); michael@0: return 0; michael@0: } michael@0: michael@0: int32_t michael@0: nsTableCellMap::GetEffectiveColSpan(int32_t aRowIndex, michael@0: int32_t aColIndex) const michael@0: { michael@0: int32_t rowIndex = aRowIndex; michael@0: nsCellMap* map = mFirstMap; michael@0: while (map) { michael@0: if (map->GetRowCount() > rowIndex) { michael@0: bool zeroColSpan; michael@0: return map->GetEffectiveColSpan(*this, rowIndex, aColIndex, zeroColSpan); michael@0: } michael@0: rowIndex -= map->GetRowCount(); michael@0: map = map->GetNextSibling(); michael@0: } michael@0: NS_NOTREACHED("Bogus row index?"); michael@0: return 0; michael@0: } michael@0: michael@0: nsTableCellFrame* michael@0: nsTableCellMap::GetCellFrame(int32_t aRowIndex, michael@0: int32_t aColIndex, michael@0: CellData& aData, michael@0: bool aUseRowIfOverlap) const michael@0: { michael@0: int32_t rowIndex = aRowIndex; michael@0: nsCellMap* map = mFirstMap; michael@0: while (map) { michael@0: if (map->GetRowCount() > rowIndex) { michael@0: return map->GetCellFrame(rowIndex, aColIndex, aData, aUseRowIfOverlap); michael@0: } michael@0: rowIndex -= map->GetRowCount(); michael@0: map = map->GetNextSibling(); michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: nsColInfo* michael@0: nsTableCellMap::GetColInfoAt(int32_t aColIndex) michael@0: { michael@0: int32_t numColsToAdd = aColIndex + 1 - mCols.Length(); michael@0: if (numColsToAdd > 0) { michael@0: AddColsAtEnd(numColsToAdd); // XXX this could fail to add cols in theory michael@0: } michael@0: return &mCols.ElementAt(aColIndex); michael@0: } michael@0: michael@0: int32_t michael@0: nsTableCellMap::GetRowCount() const michael@0: { michael@0: int32_t numRows = 0; michael@0: nsCellMap* map = mFirstMap; michael@0: while (map) { michael@0: numRows += map->GetRowCount(); michael@0: map = map->GetNextSibling(); michael@0: } michael@0: return numRows; michael@0: } michael@0: michael@0: CellData* michael@0: nsTableCellMap::GetDataAt(int32_t aRowIndex, michael@0: int32_t aColIndex) const michael@0: { michael@0: int32_t rowIndex = aRowIndex; michael@0: nsCellMap* map = mFirstMap; michael@0: while (map) { michael@0: if (map->GetRowCount() > rowIndex) { michael@0: return map->GetDataAt(rowIndex, aColIndex); michael@0: } michael@0: rowIndex -= map->GetRowCount(); michael@0: map = map->GetNextSibling(); michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: void michael@0: nsTableCellMap::AddColsAtEnd(uint32_t aNumCols) michael@0: { michael@0: if (!mCols.AppendElements(aNumCols)) { michael@0: NS_WARNING("Could not AppendElement"); michael@0: } michael@0: if (mBCInfo) { michael@0: if (!mBCInfo->mBottomBorders.AppendElements(aNumCols)) { michael@0: NS_WARNING("Could not AppendElement"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsTableCellMap::RemoveColsAtEnd() michael@0: { michael@0: // Remove the cols at the end which don't have originating cells or cells spanning michael@0: // into them. Only do this if the col was created as eColAnonymousCell michael@0: int32_t numCols = GetColCount(); michael@0: int32_t lastGoodColIndex = mTableFrame.GetIndexOfLastRealCol(); michael@0: for (int32_t colX = numCols - 1; (colX >= 0) && (colX > lastGoodColIndex); colX--) { michael@0: nsColInfo& colInfo = mCols.ElementAt(colX); michael@0: if ((colInfo.mNumCellsOrig <= 0) && (colInfo.mNumCellsSpan <= 0)) { michael@0: mCols.RemoveElementAt(colX); michael@0: michael@0: if (mBCInfo) { michael@0: int32_t count = mBCInfo->mBottomBorders.Length(); michael@0: if (colX < count) { michael@0: mBCInfo->mBottomBorders.RemoveElementAt(colX); michael@0: } michael@0: } michael@0: } michael@0: else break; // only remove until we encounter the 1st valid one michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsTableCellMap::ClearCols() michael@0: { michael@0: mCols.Clear(); michael@0: if (mBCInfo) michael@0: mBCInfo->mBottomBorders.Clear(); michael@0: } michael@0: void michael@0: nsTableCellMap::InsertRows(nsTableRowGroupFrame* aParent, michael@0: nsTArray& aRows, michael@0: int32_t aFirstRowIndex, michael@0: bool aConsiderSpans, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: int32_t numNewRows = aRows.Length(); michael@0: if ((numNewRows <= 0) || (aFirstRowIndex < 0)) ABORT0(); michael@0: michael@0: int32_t rowIndex = aFirstRowIndex; michael@0: int32_t rgStartRowIndex = 0; michael@0: nsCellMap* cellMap = mFirstMap; michael@0: while (cellMap) { michael@0: nsTableRowGroupFrame* rg = cellMap->GetRowGroup(); michael@0: if (rg == aParent) { michael@0: cellMap->InsertRows(*this, aRows, rowIndex, aConsiderSpans, michael@0: rgStartRowIndex, aDamageArea); michael@0: #ifdef DEBUG_TABLE_CELLMAP michael@0: Dump("after InsertRows"); michael@0: #endif michael@0: if (mBCInfo) { michael@0: int32_t count = mBCInfo->mRightBorders.Length(); michael@0: if (aFirstRowIndex < count) { michael@0: for (int32_t rowX = aFirstRowIndex; rowX < aFirstRowIndex + numNewRows; rowX++) { michael@0: mBCInfo->mRightBorders.InsertElementAt(rowX); michael@0: } michael@0: } michael@0: else { michael@0: GetRightMostBorder(aFirstRowIndex); // this will create missing entries michael@0: for (int32_t rowX = aFirstRowIndex + 1; rowX < aFirstRowIndex + numNewRows; rowX++) { michael@0: mBCInfo->mRightBorders.AppendElement(); michael@0: } michael@0: } michael@0: } michael@0: return; michael@0: } michael@0: int32_t rowCount = cellMap->GetRowCount(); michael@0: rgStartRowIndex += rowCount; michael@0: rowIndex -= rowCount; michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: michael@0: NS_ERROR("Attempt to insert row into wrong map."); michael@0: } michael@0: michael@0: void michael@0: nsTableCellMap::RemoveRows(int32_t aFirstRowIndex, michael@0: int32_t aNumRowsToRemove, michael@0: bool aConsiderSpans, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: int32_t rowIndex = aFirstRowIndex; michael@0: int32_t rgStartRowIndex = 0; michael@0: nsCellMap* cellMap = mFirstMap; michael@0: while (cellMap) { michael@0: int32_t rowCount = cellMap->GetRowCount(); michael@0: if (rowCount > rowIndex) { michael@0: cellMap->RemoveRows(*this, rowIndex, aNumRowsToRemove, aConsiderSpans, michael@0: rgStartRowIndex, aDamageArea); michael@0: if (mBCInfo) { michael@0: for (int32_t rowX = aFirstRowIndex + aNumRowsToRemove - 1; rowX >= aFirstRowIndex; rowX--) { michael@0: if (uint32_t(rowX) < mBCInfo->mRightBorders.Length()) { michael@0: mBCInfo->mRightBorders.RemoveElementAt(rowX); michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: rgStartRowIndex += rowCount; michael@0: rowIndex -= rowCount; michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: #ifdef DEBUG_TABLE_CELLMAP michael@0: Dump("after RemoveRows"); michael@0: #endif michael@0: } michael@0: michael@0: michael@0: michael@0: CellData* michael@0: nsTableCellMap::AppendCell(nsTableCellFrame& aCellFrame, michael@0: int32_t aRowIndex, michael@0: bool aRebuildIfNecessary, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: MOZ_ASSERT(&aCellFrame == aCellFrame.FirstInFlow(), michael@0: "invalid call on continuing frame"); michael@0: nsIFrame* rgFrame = aCellFrame.GetParent(); // get the row michael@0: if (!rgFrame) return 0; michael@0: rgFrame = rgFrame->GetParent(); // get the row group michael@0: if (!rgFrame) return 0; michael@0: michael@0: CellData* result = nullptr; michael@0: int32_t rowIndex = aRowIndex; michael@0: int32_t rgStartRowIndex = 0; michael@0: nsCellMap* cellMap = mFirstMap; michael@0: while (cellMap) { michael@0: if (cellMap->GetRowGroup() == rgFrame) { michael@0: result = cellMap->AppendCell(*this, &aCellFrame, rowIndex, michael@0: aRebuildIfNecessary, rgStartRowIndex, michael@0: aDamageArea); michael@0: break; michael@0: } michael@0: int32_t rowCount = cellMap->GetRowCount(); michael@0: rgStartRowIndex += rowCount; michael@0: rowIndex -= rowCount; michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: #ifdef DEBUG_TABLE_CELLMAP michael@0: Dump("after AppendCell"); michael@0: #endif michael@0: return result; michael@0: } michael@0: michael@0: michael@0: void michael@0: nsTableCellMap::InsertCells(nsTArray& aCellFrames, michael@0: int32_t aRowIndex, michael@0: int32_t aColIndexBefore, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: int32_t rowIndex = aRowIndex; michael@0: int32_t rgStartRowIndex = 0; michael@0: nsCellMap* cellMap = mFirstMap; michael@0: while (cellMap) { michael@0: int32_t rowCount = cellMap->GetRowCount(); michael@0: if (rowCount > rowIndex) { michael@0: cellMap->InsertCells(*this, aCellFrames, rowIndex, aColIndexBefore, michael@0: rgStartRowIndex, aDamageArea); michael@0: break; michael@0: } michael@0: rgStartRowIndex += rowCount; michael@0: rowIndex -= rowCount; michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: #ifdef DEBUG_TABLE_CELLMAP michael@0: Dump("after InsertCells"); michael@0: #endif michael@0: } michael@0: michael@0: michael@0: void michael@0: nsTableCellMap::RemoveCell(nsTableCellFrame* aCellFrame, michael@0: int32_t aRowIndex, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: if (!aCellFrame) ABORT0(); michael@0: MOZ_ASSERT(aCellFrame == aCellFrame->FirstInFlow(), michael@0: "invalid call on continuing frame"); michael@0: int32_t rowIndex = aRowIndex; michael@0: int32_t rgStartRowIndex = 0; michael@0: nsCellMap* cellMap = mFirstMap; michael@0: while (cellMap) { michael@0: int32_t rowCount = cellMap->GetRowCount(); michael@0: if (rowCount > rowIndex) { michael@0: cellMap->RemoveCell(*this, aCellFrame, rowIndex, rgStartRowIndex, michael@0: aDamageArea); michael@0: #ifdef DEBUG_TABLE_CELLMAP michael@0: Dump("after RemoveCell"); michael@0: #endif michael@0: return; michael@0: } michael@0: rgStartRowIndex += rowCount; michael@0: rowIndex -= rowCount; michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: // if we reach this point - the cell did not get removed, the caller of this routine michael@0: // will delete the cell and the cellmap will probably hold a reference to michael@0: // the deleted cell which will cause a subsequent crash when this cell is michael@0: // referenced later michael@0: NS_ERROR("nsTableCellMap::RemoveCell - could not remove cell"); michael@0: } michael@0: michael@0: void michael@0: nsTableCellMap::RebuildConsideringCells(nsCellMap* aCellMap, michael@0: nsTArray* aCellFrames, michael@0: int32_t aRowIndex, michael@0: int32_t aColIndex, michael@0: bool aInsert, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: int32_t numOrigCols = GetColCount(); michael@0: ClearCols(); michael@0: nsCellMap* cellMap = mFirstMap; michael@0: int32_t rowCount = 0; michael@0: while (cellMap) { michael@0: if (cellMap == aCellMap) { michael@0: cellMap->RebuildConsideringCells(*this, numOrigCols, aCellFrames, michael@0: aRowIndex, aColIndex, aInsert); michael@0: } michael@0: else { michael@0: cellMap->RebuildConsideringCells(*this, numOrigCols, nullptr, -1, 0, michael@0: false); michael@0: } michael@0: rowCount += cellMap->GetRowCount(); michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: SetDamageArea(0, 0, GetColCount(), rowCount, aDamageArea); michael@0: } michael@0: michael@0: void michael@0: nsTableCellMap::RebuildConsideringRows(nsCellMap* aCellMap, michael@0: int32_t aStartRowIndex, michael@0: nsTArray* aRowsToInsert, michael@0: int32_t aNumRowsToRemove, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: NS_PRECONDITION(!aRowsToInsert || aNumRowsToRemove == 0, michael@0: "Can't handle both removing and inserting rows at once"); michael@0: michael@0: int32_t numOrigCols = GetColCount(); michael@0: ClearCols(); michael@0: nsCellMap* cellMap = mFirstMap; michael@0: int32_t rowCount = 0; michael@0: while (cellMap) { michael@0: if (cellMap == aCellMap) { michael@0: cellMap->RebuildConsideringRows(*this, aStartRowIndex, aRowsToInsert, michael@0: aNumRowsToRemove); michael@0: } michael@0: else { michael@0: cellMap->RebuildConsideringCells(*this, numOrigCols, nullptr, -1, 0, michael@0: false); michael@0: } michael@0: rowCount += cellMap->GetRowCount(); michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: SetDamageArea(0, 0, GetColCount(), rowCount, aDamageArea); michael@0: } michael@0: michael@0: int32_t michael@0: nsTableCellMap::GetNumCellsOriginatingInCol(int32_t aColIndex) const michael@0: { michael@0: int32_t colCount = mCols.Length(); michael@0: if ((aColIndex >= 0) && (aColIndex < colCount)) { michael@0: return mCols.ElementAt(aColIndex).mNumCellsOrig; michael@0: } michael@0: else { michael@0: NS_ERROR("nsCellMap::GetNumCellsOriginatingInCol - bad col index"); michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: void michael@0: nsTableCellMap::Dump(char* aString) const michael@0: { michael@0: if (aString) michael@0: printf("%s \n", aString); michael@0: printf("***** START TABLE CELL MAP DUMP ***** %p\n", (void*)this); michael@0: // output col info michael@0: int32_t colCount = mCols.Length(); michael@0: printf ("cols array orig/span-> %p", (void*)this); michael@0: for (int32_t colX = 0; colX < colCount; colX++) { michael@0: const nsColInfo& colInfo = mCols.ElementAt(colX); michael@0: printf ("%d=%d/%d ", colX, colInfo.mNumCellsOrig, colInfo.mNumCellsSpan); michael@0: } michael@0: printf(" cols in cache %d\n", mTableFrame.GetColCache().Length()); michael@0: nsCellMap* cellMap = mFirstMap; michael@0: while (cellMap) { michael@0: cellMap->Dump(nullptr != mBCInfo); michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: if (nullptr != mBCInfo) { michael@0: printf("***** bottom borders *****\n"); michael@0: nscoord size; michael@0: BCBorderOwner owner; michael@0: mozilla::css::Side side; michael@0: bool segStart; michael@0: bool bevel; michael@0: int32_t colIndex; michael@0: int32_t numCols = mBCInfo->mBottomBorders.Length(); michael@0: for (int32_t i = 0; i <= 2; i++) { michael@0: michael@0: printf("\n "); michael@0: for (colIndex = 0; colIndex < numCols; colIndex++) { michael@0: BCData& cd = mBCInfo->mBottomBorders.ElementAt(colIndex); michael@0: if (0 == i) { michael@0: size = cd.GetTopEdge(owner, segStart); michael@0: printf("t=%d%X%d ", int32_t(size), owner, segStart); michael@0: } michael@0: else if (1 == i) { michael@0: size = cd.GetLeftEdge(owner, segStart); michael@0: printf("l=%d%X%d ", int32_t(size), owner, segStart); michael@0: } michael@0: else { michael@0: size = cd.GetCorner(side, bevel); michael@0: printf("c=%d%X%d ", int32_t(size), side, bevel); michael@0: } michael@0: } michael@0: BCData& cd = mBCInfo->mLowerRightCorner; michael@0: if (0 == i) { michael@0: size = cd.GetTopEdge(owner, segStart); michael@0: printf("t=%d%X%d ", int32_t(size), owner, segStart); michael@0: } michael@0: else if (1 == i) { michael@0: size = cd.GetLeftEdge(owner, segStart); michael@0: printf("l=%d%X%d ", int32_t(size), owner, segStart); michael@0: } michael@0: else { michael@0: size = cd.GetCorner(side, bevel); michael@0: printf("c=%d%X%d ", int32_t(size), side, bevel); michael@0: } michael@0: } michael@0: printf("\n"); michael@0: } michael@0: printf("***** END TABLE CELL MAP DUMP *****\n"); michael@0: } michael@0: #endif michael@0: michael@0: nsTableCellFrame* michael@0: nsTableCellMap::GetCellInfoAt(int32_t aRowIndex, michael@0: int32_t aColIndex, michael@0: bool* aOriginates, michael@0: int32_t* aColSpan) const michael@0: { michael@0: int32_t rowIndex = aRowIndex; michael@0: nsCellMap* cellMap = mFirstMap; michael@0: while (cellMap) { michael@0: if (cellMap->GetRowCount() > rowIndex) { michael@0: return cellMap->GetCellInfoAt(*this, rowIndex, aColIndex, aOriginates, aColSpan); michael@0: } michael@0: rowIndex -= cellMap->GetRowCount(); michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: int32_t michael@0: nsTableCellMap::GetIndexByRowAndColumn(int32_t aRow, int32_t aColumn) const michael@0: { michael@0: int32_t index = 0; michael@0: michael@0: int32_t colCount = mCols.Length(); michael@0: int32_t rowIndex = aRow; michael@0: michael@0: nsCellMap* cellMap = mFirstMap; michael@0: while (cellMap) { michael@0: int32_t rowCount = cellMap->GetRowCount(); michael@0: if (rowIndex >= rowCount) { michael@0: // If the rowCount is less than the rowIndex, this means that the index is michael@0: // not within the current map. If so, get the index of the last cell in michael@0: // the last row. michael@0: rowIndex -= rowCount; michael@0: michael@0: int32_t cellMapIdx = cellMap->GetHighestIndex(colCount); michael@0: if (cellMapIdx != -1) michael@0: index += cellMapIdx + 1; michael@0: michael@0: } else { michael@0: // Index is in valid range for this cellmap, so get the index of rowIndex michael@0: // and aColumn. michael@0: int32_t cellMapIdx = cellMap->GetIndexByRowAndColumn(colCount, rowIndex, michael@0: aColumn); michael@0: if (cellMapIdx == -1) michael@0: return -1; // no cell at the given row and column. michael@0: michael@0: index += cellMapIdx; michael@0: return index; // no need to look through further maps here michael@0: } michael@0: michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: michael@0: return -1; michael@0: } michael@0: michael@0: void michael@0: nsTableCellMap::GetRowAndColumnByIndex(int32_t aIndex, michael@0: int32_t *aRow, int32_t *aColumn) const michael@0: { michael@0: *aRow = -1; michael@0: *aColumn = -1; michael@0: michael@0: int32_t colCount = mCols.Length(); michael@0: michael@0: int32_t previousRows = 0; michael@0: int32_t index = aIndex; michael@0: michael@0: nsCellMap* cellMap = mFirstMap; michael@0: while (cellMap) { michael@0: int32_t rowCount = cellMap->GetRowCount(); michael@0: // Determine the highest possible index in this map to see michael@0: // if wanted index is in here. michael@0: int32_t cellMapIdx = cellMap->GetHighestIndex(colCount); michael@0: if (cellMapIdx == -1) { michael@0: // The index is not within this map, increase the total row index michael@0: // accordingly. michael@0: previousRows += rowCount; michael@0: } else { michael@0: if (index > cellMapIdx) { michael@0: // The index is not within this map, so decrease it by the cellMapIdx michael@0: // determined index and increase the total row index accordingly. michael@0: index -= cellMapIdx + 1; michael@0: previousRows += rowCount; michael@0: } else { michael@0: cellMap->GetRowAndColumnByIndex(colCount, index, aRow, aColumn); michael@0: // If there were previous indexes, take them into account. michael@0: *aRow += previousRows; michael@0: return; // no need to look any further. michael@0: } michael@0: } michael@0: michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: } michael@0: michael@0: bool nsTableCellMap::RowIsSpannedInto(int32_t aRowIndex, michael@0: int32_t aNumEffCols) const michael@0: { michael@0: int32_t rowIndex = aRowIndex; michael@0: nsCellMap* cellMap = mFirstMap; michael@0: while (cellMap) { michael@0: if (cellMap->GetRowCount() > rowIndex) { michael@0: return cellMap->RowIsSpannedInto(rowIndex, aNumEffCols); michael@0: } michael@0: rowIndex -= cellMap->GetRowCount(); michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool nsTableCellMap::RowHasSpanningCells(int32_t aRowIndex, michael@0: int32_t aNumEffCols) const michael@0: { michael@0: int32_t rowIndex = aRowIndex; michael@0: nsCellMap* cellMap = mFirstMap; michael@0: while (cellMap) { michael@0: if (cellMap->GetRowCount() > rowIndex) { michael@0: return cellMap->RowHasSpanningCells(rowIndex, aNumEffCols); michael@0: } michael@0: rowIndex -= cellMap->GetRowCount(); michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: void nsTableCellMap::ExpandZeroColSpans() michael@0: { michael@0: mTableFrame.SetNeedColSpanExpansion(false); // mark the work done michael@0: mTableFrame.SetHasZeroColSpans(false); // reset the bit, if there is a michael@0: // zerospan it will be set again. michael@0: nsCellMap* cellMap = mFirstMap; michael@0: while (cellMap) { michael@0: cellMap->ExpandZeroColSpans(*this); michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsTableCellMap::ResetTopStart(uint8_t aSide, michael@0: nsCellMap& aCellMap, michael@0: uint32_t aRowIndex, michael@0: uint32_t aColIndex, michael@0: bool aIsLowerRight) michael@0: { michael@0: if (!mBCInfo || aIsLowerRight) ABORT0(); michael@0: michael@0: BCCellData* cellData; michael@0: BCData* bcData = nullptr; michael@0: michael@0: switch(aSide) { michael@0: case NS_SIDE_BOTTOM: michael@0: aRowIndex++; michael@0: // FALLTHROUGH michael@0: case NS_SIDE_TOP: michael@0: cellData = (BCCellData*)aCellMap.GetDataAt(aRowIndex, aColIndex); michael@0: if (cellData) { michael@0: bcData = &cellData->mData; michael@0: } michael@0: else { michael@0: NS_ASSERTION(aSide == NS_SIDE_BOTTOM, "program error"); michael@0: // try the next row group michael@0: nsCellMap* cellMap = aCellMap.GetNextSibling(); michael@0: if (cellMap) { michael@0: cellData = (BCCellData*)cellMap->GetDataAt(0, aColIndex); michael@0: if (cellData) { michael@0: bcData = &cellData->mData; michael@0: } michael@0: else { michael@0: bcData = GetBottomMostBorder(aColIndex); michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: case NS_SIDE_RIGHT: michael@0: aColIndex++; michael@0: // FALLTHROUGH michael@0: case NS_SIDE_LEFT: michael@0: cellData = (BCCellData*)aCellMap.GetDataAt(aRowIndex, aColIndex); michael@0: if (cellData) { michael@0: bcData = &cellData->mData; michael@0: } michael@0: else { michael@0: NS_ASSERTION(aSide == NS_SIDE_RIGHT, "program error"); michael@0: bcData = GetRightMostBorder(aRowIndex); michael@0: } michael@0: break; michael@0: } michael@0: if (bcData) { michael@0: bcData->SetTopStart(false); michael@0: } michael@0: } michael@0: michael@0: // store the aSide border segment at coord = (aRowIndex, aColIndex). For top/left, store michael@0: // the info at coord. For bottom/left store it at the adjacent location so that it is michael@0: // top/left at that location. If the new location is at the right or bottom edge of the michael@0: // table, then store it one of the special arrays (right most borders, bottom most borders). michael@0: void michael@0: nsTableCellMap::SetBCBorderEdge(mozilla::css::Side aSide, michael@0: nsCellMap& aCellMap, michael@0: uint32_t aCellMapStart, michael@0: uint32_t aRowIndex, michael@0: uint32_t aColIndex, michael@0: uint32_t aLength, michael@0: BCBorderOwner aOwner, michael@0: nscoord aSize, michael@0: bool aChanged) michael@0: { michael@0: if (!mBCInfo) ABORT0(); michael@0: michael@0: BCCellData* cellData; michael@0: int32_t lastIndex, xIndex, yIndex; michael@0: int32_t xPos = aColIndex; michael@0: int32_t yPos = aRowIndex; michael@0: int32_t rgYPos = aRowIndex - aCellMapStart; michael@0: bool changed; michael@0: michael@0: switch(aSide) { michael@0: case NS_SIDE_BOTTOM: michael@0: rgYPos++; michael@0: yPos++; michael@0: case NS_SIDE_TOP: michael@0: lastIndex = xPos + aLength - 1; michael@0: for (xIndex = xPos; xIndex <= lastIndex; xIndex++) { michael@0: changed = aChanged && (xIndex == xPos); michael@0: BCData* bcData = nullptr; michael@0: cellData = (BCCellData*)aCellMap.GetDataAt(rgYPos, xIndex); michael@0: if (!cellData) { michael@0: int32_t numRgRows = aCellMap.GetRowCount(); michael@0: if (yPos < numRgRows) { // add a dead cell data michael@0: nsIntRect damageArea; michael@0: cellData = (BCCellData*)aCellMap.AppendCell(*this, nullptr, rgYPos, michael@0: false, 0, damageArea); michael@0: if (!cellData) ABORT0(); michael@0: } michael@0: else { michael@0: NS_ASSERTION(aSide == NS_SIDE_BOTTOM, "program error"); michael@0: // try the next non empty row group michael@0: nsCellMap* cellMap = aCellMap.GetNextSibling(); michael@0: while (cellMap && (0 == cellMap->GetRowCount())) { michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: if (cellMap) { michael@0: cellData = (BCCellData*)cellMap->GetDataAt(0, xIndex); michael@0: if (!cellData) { // add a dead cell michael@0: nsIntRect damageArea; michael@0: cellData = (BCCellData*)cellMap->AppendCell(*this, nullptr, 0, michael@0: false, 0, michael@0: damageArea); michael@0: } michael@0: } michael@0: else { // must be at the end of the table michael@0: bcData = GetBottomMostBorder(xIndex); michael@0: } michael@0: } michael@0: } michael@0: if (!bcData && cellData) { michael@0: bcData = &cellData->mData; michael@0: } michael@0: if (bcData) { michael@0: bcData->SetTopEdge(aOwner, aSize, changed); michael@0: } michael@0: else NS_ERROR("Cellmap: Top edge not found"); michael@0: } michael@0: break; michael@0: case NS_SIDE_RIGHT: michael@0: xPos++; michael@0: case NS_SIDE_LEFT: michael@0: // since top, bottom borders were set, there should already be a cellData entry michael@0: lastIndex = rgYPos + aLength - 1; michael@0: for (yIndex = rgYPos; yIndex <= lastIndex; yIndex++) { michael@0: changed = aChanged && (yIndex == rgYPos); michael@0: cellData = (BCCellData*)aCellMap.GetDataAt(yIndex, xPos); michael@0: if (cellData) { michael@0: cellData->mData.SetLeftEdge(aOwner, aSize, changed); michael@0: } michael@0: else { michael@0: NS_ASSERTION(aSide == NS_SIDE_RIGHT, "program error"); michael@0: BCData* bcData = GetRightMostBorder(yIndex + aCellMapStart); michael@0: if (bcData) { michael@0: bcData->SetLeftEdge(aOwner, aSize, changed); michael@0: } michael@0: else NS_ERROR("Cellmap: Left edge not found"); michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: michael@0: // store corner info (aOwner, aSubSize, aBevel). For aCorner = eTopLeft, store the info at michael@0: // (aRowIndex, aColIndex). For eTopRight, store it in the entry to the right where michael@0: // it would be top left. For eBottomRight, store it in the entry to the bottom. etc. michael@0: void michael@0: nsTableCellMap::SetBCBorderCorner(Corner aCorner, michael@0: nsCellMap& aCellMap, michael@0: uint32_t aCellMapStart, michael@0: uint32_t aRowIndex, michael@0: uint32_t aColIndex, michael@0: mozilla::css::Side aOwner, michael@0: nscoord aSubSize, michael@0: bool aBevel, michael@0: bool aIsBottomRight) michael@0: { michael@0: if (!mBCInfo) ABORT0(); michael@0: michael@0: if (aIsBottomRight) { michael@0: mBCInfo->mLowerRightCorner.SetCorner(aSubSize, aOwner, aBevel); michael@0: return; michael@0: } michael@0: michael@0: int32_t xPos = aColIndex; michael@0: int32_t yPos = aRowIndex; michael@0: int32_t rgYPos = aRowIndex - aCellMapStart; michael@0: michael@0: if (eTopRight == aCorner) { michael@0: xPos++; michael@0: } michael@0: else if (eBottomRight == aCorner) { michael@0: xPos++; michael@0: rgYPos++; michael@0: yPos++; michael@0: } michael@0: else if (eBottomLeft == aCorner) { michael@0: rgYPos++; michael@0: yPos++; michael@0: } michael@0: michael@0: BCCellData* cellData = nullptr; michael@0: BCData* bcData = nullptr; michael@0: if (GetColCount() <= xPos) { michael@0: NS_ASSERTION(xPos == GetColCount(), "program error"); michael@0: // at the right edge of the table as we checked the corner before michael@0: NS_ASSERTION(!aIsBottomRight, "should be handled before"); michael@0: bcData = GetRightMostBorder(yPos); michael@0: } michael@0: else { michael@0: cellData = (BCCellData*)aCellMap.GetDataAt(rgYPos, xPos); michael@0: if (!cellData) { michael@0: int32_t numRgRows = aCellMap.GetRowCount(); michael@0: if (yPos < numRgRows) { // add a dead cell data michael@0: nsIntRect damageArea; michael@0: cellData = (BCCellData*)aCellMap.AppendCell(*this, nullptr, rgYPos, michael@0: false, 0, damageArea); michael@0: } michael@0: else { michael@0: // try the next non empty row group michael@0: nsCellMap* cellMap = aCellMap.GetNextSibling(); michael@0: while (cellMap && (0 == cellMap->GetRowCount())) { michael@0: cellMap = cellMap->GetNextSibling(); michael@0: } michael@0: if (cellMap) { michael@0: cellData = (BCCellData*)cellMap->GetDataAt(0, xPos); michael@0: if (!cellData) { // add a dead cell michael@0: nsIntRect damageArea; michael@0: cellData = (BCCellData*)cellMap->AppendCell(*this, nullptr, 0, michael@0: false, 0, damageArea); michael@0: } michael@0: } michael@0: else { // must be at the bottom of the table michael@0: bcData = GetBottomMostBorder(xPos); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: if (!bcData && cellData) { michael@0: bcData = &cellData->mData; michael@0: } michael@0: if (bcData) { michael@0: bcData->SetCorner(aSubSize, aOwner, aBevel); michael@0: } michael@0: else NS_ERROR("program error: Corner not found"); michael@0: } michael@0: michael@0: nsCellMap::nsCellMap(nsTableRowGroupFrame* aRowGroup, bool aIsBC) michael@0: : mRows(8), mContentRowCount(0), mRowGroupFrame(aRowGroup), michael@0: mNextSibling(nullptr), mIsBC(aIsBC), michael@0: mPresContext(aRowGroup->PresContext()) michael@0: { michael@0: MOZ_COUNT_CTOR(nsCellMap); michael@0: NS_ASSERTION(mPresContext, "Must have prescontext"); michael@0: } michael@0: michael@0: nsCellMap::~nsCellMap() michael@0: { michael@0: MOZ_COUNT_DTOR(nsCellMap); michael@0: michael@0: uint32_t mapRowCount = mRows.Length(); michael@0: for (uint32_t rowX = 0; rowX < mapRowCount; rowX++) { michael@0: CellDataArray &row = mRows[rowX]; michael@0: uint32_t colCount = row.Length(); michael@0: for (uint32_t colX = 0; colX < colCount; colX++) { michael@0: DestroyCellData(row[colX]); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* static */ michael@0: void michael@0: nsCellMap::Init() michael@0: { michael@0: NS_ABORT_IF_FALSE(!sEmptyRow, "How did that happen?"); michael@0: sEmptyRow = new nsCellMap::CellDataArray(); michael@0: } michael@0: michael@0: /* static */ michael@0: void michael@0: nsCellMap::Shutdown() michael@0: { michael@0: delete sEmptyRow; michael@0: sEmptyRow = nullptr; michael@0: } michael@0: michael@0: nsTableCellFrame* michael@0: nsCellMap::GetCellFrame(int32_t aRowIndexIn, michael@0: int32_t aColIndexIn, michael@0: CellData& aData, michael@0: bool aUseRowIfOverlap) const michael@0: { michael@0: int32_t rowIndex = aRowIndexIn - aData.GetRowSpanOffset(); michael@0: int32_t colIndex = aColIndexIn - aData.GetColSpanOffset(); michael@0: if (aData.IsOverlap()) { michael@0: if (aUseRowIfOverlap) { michael@0: colIndex = aColIndexIn; michael@0: } michael@0: else { michael@0: rowIndex = aRowIndexIn; michael@0: } michael@0: } michael@0: michael@0: CellData* data = michael@0: mRows.SafeElementAt(rowIndex, *sEmptyRow).SafeElementAt(colIndex); michael@0: if (data) { michael@0: return data->GetCellFrame(); michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: int32_t michael@0: nsCellMap::GetHighestIndex(int32_t aColCount) michael@0: { michael@0: int32_t index = -1; michael@0: int32_t rowCount = mRows.Length(); michael@0: for (int32_t rowIdx = 0; rowIdx < rowCount; rowIdx++) { michael@0: const CellDataArray& row = mRows[rowIdx]; michael@0: michael@0: for (int32_t colIdx = 0; colIdx < aColCount; colIdx++) { michael@0: CellData* data = row.SafeElementAt(colIdx); michael@0: // No data means row doesn't have more cells. michael@0: if (!data) michael@0: break; michael@0: michael@0: if (data->IsOrig()) michael@0: index++; michael@0: } michael@0: } michael@0: michael@0: return index; michael@0: } michael@0: michael@0: int32_t michael@0: nsCellMap::GetIndexByRowAndColumn(int32_t aColCount, michael@0: int32_t aRow, int32_t aColumn) const michael@0: { michael@0: if (uint32_t(aRow) >= mRows.Length()) michael@0: return -1; michael@0: michael@0: int32_t index = -1; michael@0: int32_t lastColsIdx = aColCount - 1; michael@0: michael@0: // Find row index of the cell where row span is started. michael@0: const CellDataArray& row = mRows[aRow]; michael@0: CellData* data = row.SafeElementAt(aColumn); michael@0: int32_t origRow = data ? aRow - data->GetRowSpanOffset() : aRow; michael@0: michael@0: // Calculate cell index. michael@0: for (int32_t rowIdx = 0; rowIdx <= origRow; rowIdx++) { michael@0: const CellDataArray& row = mRows[rowIdx]; michael@0: int32_t colCount = (rowIdx == origRow) ? aColumn : lastColsIdx; michael@0: michael@0: for (int32_t colIdx = 0; colIdx <= colCount; colIdx++) { michael@0: data = row.SafeElementAt(colIdx); michael@0: // No data means row doesn't have more cells. michael@0: if (!data) michael@0: break; michael@0: michael@0: if (data->IsOrig()) michael@0: index++; michael@0: } michael@0: } michael@0: michael@0: // Given row and column don't point to the cell. michael@0: if (!data) michael@0: return -1; michael@0: michael@0: return index; michael@0: } michael@0: michael@0: void michael@0: nsCellMap::GetRowAndColumnByIndex(int32_t aColCount, int32_t aIndex, michael@0: int32_t *aRow, int32_t *aColumn) const michael@0: { michael@0: *aRow = -1; michael@0: *aColumn = -1; michael@0: michael@0: int32_t index = aIndex; michael@0: int32_t rowCount = mRows.Length(); michael@0: michael@0: for (int32_t rowIdx = 0; rowIdx < rowCount; rowIdx++) { michael@0: const CellDataArray& row = mRows[rowIdx]; michael@0: michael@0: for (int32_t colIdx = 0; colIdx < aColCount; colIdx++) { michael@0: CellData* data = row.SafeElementAt(colIdx); michael@0: michael@0: // The row doesn't have more cells. michael@0: if (!data) michael@0: break; michael@0: michael@0: if (data->IsOrig()) michael@0: index--; michael@0: michael@0: if (index < 0) { michael@0: *aRow = rowIdx; michael@0: *aColumn = colIdx; michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool nsCellMap::Grow(nsTableCellMap& aMap, michael@0: int32_t aNumRows, michael@0: int32_t aRowIndex) michael@0: { michael@0: NS_ASSERTION(aNumRows >= 1, "Why are we calling this?"); michael@0: michael@0: // Get the number of cols we want to use for preallocating the row arrays. michael@0: int32_t numCols = aMap.GetColCount(); michael@0: if (numCols == 0) { michael@0: numCols = 4; michael@0: } michael@0: uint32_t startRowIndex = (aRowIndex >= 0) ? aRowIndex : mRows.Length(); michael@0: NS_ASSERTION(startRowIndex <= mRows.Length(), "Missing grow call inbetween"); michael@0: michael@0: return mRows.InsertElementsAt(startRowIndex, aNumRows, numCols) != nullptr; michael@0: } michael@0: michael@0: void nsCellMap::GrowRow(CellDataArray& aRow, michael@0: int32_t aNumCols) michael@0: michael@0: { michael@0: // Have to have the cast to get the template to do the right thing. michael@0: aRow.InsertElementsAt(aRow.Length(), aNumCols, (CellData*)nullptr); michael@0: } michael@0: michael@0: void michael@0: nsCellMap::InsertRows(nsTableCellMap& aMap, michael@0: nsTArray& aRows, michael@0: int32_t aFirstRowIndex, michael@0: bool aConsiderSpans, michael@0: int32_t aRgFirstRowIndex, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: int32_t numCols = aMap.GetColCount(); michael@0: NS_ASSERTION(aFirstRowIndex >= 0, "nsCellMap::InsertRows called with negative rowIndex"); michael@0: if (uint32_t(aFirstRowIndex) > mRows.Length()) { michael@0: // create (aFirstRowIndex - mRows.Length()) empty rows up to aFirstRowIndex michael@0: int32_t numEmptyRows = aFirstRowIndex - mRows.Length(); michael@0: if (!Grow(aMap, numEmptyRows)) { michael@0: return; michael@0: } michael@0: } michael@0: michael@0: if (!aConsiderSpans) { michael@0: // update mContentRowCount, since non-empty rows will be added michael@0: mContentRowCount = std::max(aFirstRowIndex, mContentRowCount); michael@0: ExpandWithRows(aMap, aRows, aFirstRowIndex, aRgFirstRowIndex, aDamageArea); michael@0: return; michael@0: } michael@0: michael@0: // if any cells span into or out of the row being inserted, then rebuild michael@0: bool spansCauseRebuild = CellsSpanInOrOut(aFirstRowIndex, michael@0: aFirstRowIndex, 0, numCols - 1); michael@0: michael@0: // update mContentRowCount, since non-empty rows will be added michael@0: mContentRowCount = std::max(aFirstRowIndex, mContentRowCount); michael@0: michael@0: // if any of the new cells span out of the new rows being added, then rebuild michael@0: // XXX it would be better to only rebuild the portion of the map that follows the new rows michael@0: if (!spansCauseRebuild && (uint32_t(aFirstRowIndex) < mRows.Length())) { michael@0: spansCauseRebuild = CellsSpanOut(aRows); michael@0: } michael@0: if (spansCauseRebuild) { michael@0: aMap.RebuildConsideringRows(this, aFirstRowIndex, &aRows, 0, aDamageArea); michael@0: } michael@0: else { michael@0: ExpandWithRows(aMap, aRows, aFirstRowIndex, aRgFirstRowIndex, aDamageArea); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsCellMap::RemoveRows(nsTableCellMap& aMap, michael@0: int32_t aFirstRowIndex, michael@0: int32_t aNumRowsToRemove, michael@0: bool aConsiderSpans, michael@0: int32_t aRgFirstRowIndex, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: int32_t numRows = mRows.Length(); michael@0: int32_t numCols = aMap.GetColCount(); michael@0: michael@0: if (aFirstRowIndex >= numRows) { michael@0: // reduce the content based row count based on the function arguments michael@0: // as they are known to be real rows even if the cell map did not create michael@0: // rows for them before. michael@0: mContentRowCount -= aNumRowsToRemove; michael@0: return; michael@0: } michael@0: if (!aConsiderSpans) { michael@0: ShrinkWithoutRows(aMap, aFirstRowIndex, aNumRowsToRemove, aRgFirstRowIndex, michael@0: aDamageArea); michael@0: return; michael@0: } michael@0: int32_t endRowIndex = aFirstRowIndex + aNumRowsToRemove - 1; michael@0: if (endRowIndex >= numRows) { michael@0: NS_ERROR("nsCellMap::RemoveRows tried to remove too many rows"); michael@0: endRowIndex = numRows - 1; michael@0: } michael@0: bool spansCauseRebuild = CellsSpanInOrOut(aFirstRowIndex, endRowIndex, michael@0: 0, numCols - 1); michael@0: if (spansCauseRebuild) { michael@0: aMap.RebuildConsideringRows(this, aFirstRowIndex, nullptr, aNumRowsToRemove, michael@0: aDamageArea); michael@0: } michael@0: else { michael@0: ShrinkWithoutRows(aMap, aFirstRowIndex, aNumRowsToRemove, aRgFirstRowIndex, michael@0: aDamageArea); michael@0: } michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: CellData* michael@0: nsCellMap::AppendCell(nsTableCellMap& aMap, michael@0: nsTableCellFrame* aCellFrame, michael@0: int32_t aRowIndex, michael@0: bool aRebuildIfNecessary, michael@0: int32_t aRgFirstRowIndex, michael@0: nsIntRect& aDamageArea, michael@0: int32_t* aColToBeginSearch) michael@0: { michael@0: NS_ASSERTION(!!aMap.mBCInfo == mIsBC, "BC state mismatch"); michael@0: int32_t origNumMapRows = mRows.Length(); michael@0: int32_t origNumCols = aMap.GetColCount(); michael@0: bool zeroRowSpan = false; michael@0: int32_t rowSpan = (aCellFrame) ? GetRowSpanForNewCell(aCellFrame, aRowIndex, michael@0: zeroRowSpan) : 1; michael@0: // add new rows if necessary michael@0: int32_t endRowIndex = aRowIndex + rowSpan - 1; michael@0: if (endRowIndex >= origNumMapRows) { michael@0: // XXXbz handle allocation failures? michael@0: Grow(aMap, 1 + endRowIndex - origNumMapRows); michael@0: } michael@0: michael@0: // get the first null or dead CellData in the desired row. It will equal origNumCols if there are none michael@0: CellData* origData = nullptr; michael@0: int32_t startColIndex = 0; michael@0: if (aColToBeginSearch) michael@0: startColIndex = *aColToBeginSearch; michael@0: for (; startColIndex < origNumCols; startColIndex++) { michael@0: CellData* data = GetDataAt(aRowIndex, startColIndex); michael@0: if (!data) michael@0: break; michael@0: // The border collapse code relies on having multiple dead cell data entries michael@0: // in a row. michael@0: if (data->IsDead() && aCellFrame) { michael@0: origData = data; michael@0: break; michael@0: } michael@0: if (data->IsZeroColSpan() ) { michael@0: // appending a cell collapses zerospans. michael@0: CollapseZeroColSpan(aMap, data, aRowIndex, startColIndex); michael@0: // ask again for the data as it should be modified michael@0: origData = GetDataAt(aRowIndex, startColIndex); michael@0: NS_ASSERTION(origData->IsDead(), michael@0: "The cellposition should have been cleared"); michael@0: break; michael@0: } michael@0: } michael@0: // We found the place to append the cell, when the next cell is appended michael@0: // the next search does not need to duplicate the search but can start michael@0: // just at the next cell. michael@0: if (aColToBeginSearch) michael@0: *aColToBeginSearch = startColIndex + 1; michael@0: michael@0: bool zeroColSpan = false; michael@0: int32_t colSpan = (aCellFrame) ? michael@0: GetColSpanForNewCell(*aCellFrame, zeroColSpan) : 1; michael@0: if (zeroColSpan) { michael@0: aMap.mTableFrame.SetHasZeroColSpans(true); michael@0: aMap.mTableFrame.SetNeedColSpanExpansion(true); michael@0: } michael@0: michael@0: // if the new cell could potentially span into other rows and collide with michael@0: // originating cells there, we will play it safe and just rebuild the map michael@0: if (aRebuildIfNecessary && (aRowIndex < mContentRowCount - 1) && (rowSpan > 1)) { michael@0: nsAutoTArray newCellArray; michael@0: newCellArray.AppendElement(aCellFrame); michael@0: aMap.RebuildConsideringCells(this, &newCellArray, aRowIndex, startColIndex, true, aDamageArea); michael@0: return origData; michael@0: } michael@0: mContentRowCount = std::max(mContentRowCount, aRowIndex + 1); michael@0: michael@0: // add new cols to the table map if necessary michael@0: int32_t endColIndex = startColIndex + colSpan - 1; michael@0: if (endColIndex >= origNumCols) { michael@0: NS_ASSERTION(aCellFrame, "dead cells should not require new columns"); michael@0: aMap.AddColsAtEnd(1 + endColIndex - origNumCols); michael@0: } michael@0: michael@0: // Setup CellData for this cell michael@0: if (origData) { michael@0: NS_ASSERTION(origData->IsDead(), "replacing a non dead cell is a memory leak"); michael@0: if (aCellFrame) { // do nothing to replace a dead cell with a dead cell michael@0: origData->Init(aCellFrame); michael@0: // we are replacing a dead cell, increase the number of cells michael@0: // originating at this column michael@0: nsColInfo* colInfo = aMap.GetColInfoAt(startColIndex); michael@0: NS_ASSERTION(colInfo, "access to a non existing column"); michael@0: if (colInfo) { michael@0: colInfo->mNumCellsOrig++; michael@0: } michael@0: } michael@0: } michael@0: else { michael@0: origData = AllocCellData(aCellFrame); michael@0: if (!origData) ABORT1(origData); michael@0: SetDataAt(aMap, *origData, aRowIndex, startColIndex); michael@0: } michael@0: michael@0: if (aRebuildIfNecessary) { michael@0: //the caller depends on the damageArea michael@0: // The special case for zeroRowSpan is to adjust for the '2' in michael@0: // GetRowSpanForNewCell. michael@0: uint32_t height = zeroRowSpan ? endRowIndex - aRowIndex : michael@0: 1 + endRowIndex - aRowIndex; michael@0: SetDamageArea(startColIndex, aRgFirstRowIndex + aRowIndex, michael@0: 1 + endColIndex - startColIndex, height, aDamageArea); michael@0: } michael@0: michael@0: if (!aCellFrame) { michael@0: return origData; michael@0: } michael@0: michael@0: // initialize the cell frame michael@0: aCellFrame->SetColIndex(startColIndex); michael@0: michael@0: // Create CellData objects for the rows that this cell spans. Set michael@0: // their mOrigCell to nullptr and their mSpanData to point to data. michael@0: for (int32_t rowX = aRowIndex; rowX <= endRowIndex; rowX++) { michael@0: // The row at rowX will need to have at least endColIndex columns michael@0: mRows[rowX].SetCapacity(endColIndex); michael@0: for (int32_t colX = startColIndex; colX <= endColIndex; colX++) { michael@0: if ((rowX != aRowIndex) || (colX != startColIndex)) { // skip orig cell data done above michael@0: CellData* cellData = GetDataAt(rowX, colX); michael@0: if (cellData) { michael@0: if (cellData->IsOrig()) { michael@0: NS_ERROR("cannot overlap originating cell"); michael@0: continue; michael@0: } michael@0: if (rowX > aRowIndex) { // row spanning into cell michael@0: if (cellData->IsRowSpan()) { michael@0: // do nothing, this can be caused by rowspan which is overlapped michael@0: // by a another cell with a rowspan and a colspan michael@0: } michael@0: else { michael@0: cellData->SetRowSpanOffset(rowX - aRowIndex); michael@0: if (zeroRowSpan) { michael@0: cellData->SetZeroRowSpan(true); michael@0: } michael@0: } michael@0: } michael@0: if (colX > startColIndex) { // col spanning into cell michael@0: if (!cellData->IsColSpan()) { michael@0: if (cellData->IsRowSpan()) { michael@0: cellData->SetOverlap(true); michael@0: } michael@0: cellData->SetColSpanOffset(colX - startColIndex); michael@0: if (zeroColSpan) { michael@0: cellData->SetZeroColSpan(true); michael@0: } michael@0: michael@0: nsColInfo* colInfo = aMap.GetColInfoAt(colX); michael@0: colInfo->mNumCellsSpan++; michael@0: } michael@0: } michael@0: } michael@0: else { michael@0: cellData = AllocCellData(nullptr); michael@0: if (!cellData) return origData; michael@0: if (rowX > aRowIndex) { michael@0: cellData->SetRowSpanOffset(rowX - aRowIndex); michael@0: if (zeroRowSpan) { michael@0: cellData->SetZeroRowSpan(true); michael@0: } michael@0: } michael@0: if (colX > startColIndex) { michael@0: cellData->SetColSpanOffset(colX - startColIndex); michael@0: if (zeroColSpan) { michael@0: cellData->SetZeroColSpan(true); michael@0: } michael@0: } michael@0: SetDataAt(aMap, *cellData, rowX, colX); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #ifdef DEBUG_TABLE_CELLMAP michael@0: printf("appended cell=%p row=%d \n", aCellFrame, aRowIndex); michael@0: aMap.Dump(); michael@0: #endif michael@0: return origData; michael@0: } michael@0: michael@0: void nsCellMap::CollapseZeroColSpan(nsTableCellMap& aMap, michael@0: CellData* aOrigData, michael@0: int32_t aRowIndex, michael@0: int32_t aColIndex) michael@0: { michael@0: // if after a colspan = 0 cell another cell is appended in a row the html 4 michael@0: // spec is already violated. In principle one should then append the cell michael@0: // after the last column but then the zero spanning cell would also have michael@0: // to grow. The only plausible way to break this cycle is ignore the zero michael@0: // colspan and reset the cell to colspan = 1. michael@0: michael@0: NS_ASSERTION(aOrigData && aOrigData->IsZeroColSpan(), michael@0: "zero colspan should have been passed"); michael@0: // find the originating cellframe michael@0: nsTableCellFrame* cell = GetCellFrame(aRowIndex, aColIndex, *aOrigData, true); michael@0: NS_ASSERTION(cell, "originating cell not found"); michael@0: michael@0: // find the clearing region michael@0: int32_t startRowIndex = aRowIndex - aOrigData->GetRowSpanOffset(); michael@0: bool zeroSpan; michael@0: int32_t rowSpan = GetRowSpanForNewCell(cell, startRowIndex, zeroSpan); michael@0: int32_t endRowIndex = startRowIndex + rowSpan; michael@0: michael@0: int32_t origColIndex = aColIndex - aOrigData->GetColSpanOffset(); michael@0: int32_t endColIndex = origColIndex + michael@0: GetEffectiveColSpan(aMap, startRowIndex, michael@0: origColIndex, zeroSpan); michael@0: for (int32_t colX = origColIndex +1; colX < endColIndex; colX++) { michael@0: // Start the collapse just after the originating cell, since michael@0: // we're basically making the originating cell act as if it michael@0: // has colspan="1". michael@0: nsColInfo* colInfo = aMap.GetColInfoAt(colX); michael@0: colInfo->mNumCellsSpan -= rowSpan; michael@0: michael@0: for (int32_t rowX = startRowIndex; rowX < endRowIndex; rowX++) michael@0: { michael@0: CellData* data = mRows[rowX][colX]; michael@0: NS_ASSERTION(data->IsZeroColSpan(), michael@0: "Overwriting previous data - memory leak"); michael@0: data->Init(nullptr); // mark the cell as a dead cell. michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool nsCellMap::CellsSpanOut(nsTArray& aRows) const michael@0: { michael@0: int32_t numNewRows = aRows.Length(); michael@0: for (int32_t rowX = 0; rowX < numNewRows; rowX++) { michael@0: nsIFrame* rowFrame = (nsIFrame *) aRows.ElementAt(rowX); michael@0: nsIFrame* childFrame = rowFrame->GetFirstPrincipalChild(); michael@0: while (childFrame) { michael@0: nsTableCellFrame *cellFrame = do_QueryFrame(childFrame); michael@0: if (cellFrame) { michael@0: bool zeroSpan; michael@0: int32_t rowSpan = GetRowSpanForNewCell(cellFrame, rowX, zeroSpan); michael@0: if (zeroSpan || rowX + rowSpan > numNewRows) { michael@0: return true; michael@0: } michael@0: } michael@0: childFrame = childFrame->GetNextSibling(); michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: // return true if any cells have rows spans into or out of the region michael@0: // defined by the row and col indices or any cells have colspans into the region michael@0: bool nsCellMap::CellsSpanInOrOut(int32_t aStartRowIndex, michael@0: int32_t aEndRowIndex, michael@0: int32_t aStartColIndex, michael@0: int32_t aEndColIndex) const michael@0: { michael@0: /* michael@0: * this routine will watch the cells adjacent to the region or at the edge michael@0: * they are marked with *. The routine will verify whether they span in or michael@0: * are spanned out. michael@0: * michael@0: * startCol endCol michael@0: * r1c1 r1c2 r1c3 r1c4 r1c5 r1rc6 r1c7 michael@0: * startrow r2c1 r2c2 *r2c3 *r2c4 *r2c5 *r2rc6 r2c7 michael@0: * endrow r3c1 r3c2 *r3c3 r3c4 r3c5 *r3rc6 r3c7 michael@0: * r4c1 r4c2 *r4c3 *r4c4 *r4c5 r4rc6 r4c7 michael@0: * r5c1 r5c2 r5c3 r5c4 r5c5 r5rc6 r5c7 michael@0: */ michael@0: michael@0: int32_t numRows = mRows.Length(); // use the cellmap rows to determine the michael@0: // current cellmap extent. michael@0: for (int32_t colX = aStartColIndex; colX <= aEndColIndex; colX++) { michael@0: CellData* cellData; michael@0: if (aStartRowIndex > 0) { michael@0: cellData = GetDataAt(aStartRowIndex, colX); michael@0: if (cellData && (cellData->IsRowSpan())) { michael@0: return true; // there is a row span into the region michael@0: } michael@0: if ((aStartRowIndex >= mContentRowCount) && (mContentRowCount > 0)) { michael@0: cellData = GetDataAt(mContentRowCount - 1, colX); michael@0: if (cellData && cellData->IsZeroRowSpan()) { michael@0: return true; // When we expand the zerospan it'll span into our row michael@0: } michael@0: } michael@0: } michael@0: if (aEndRowIndex < numRows - 1) { // is there anything below aEndRowIndex michael@0: cellData = GetDataAt(aEndRowIndex + 1, colX); michael@0: if ((cellData) && (cellData->IsRowSpan())) { michael@0: return true; // there is a row span out of the region michael@0: } michael@0: } michael@0: else { michael@0: cellData = GetDataAt(aEndRowIndex, colX); michael@0: if ((cellData) && (cellData->IsRowSpan()) && (mContentRowCount < numRows)) { michael@0: return true; // this cell might be the cause of a dead row michael@0: } michael@0: } michael@0: } michael@0: if (aStartColIndex > 0) { michael@0: for (int32_t rowX = aStartRowIndex; rowX <= aEndRowIndex; rowX++) { michael@0: CellData* cellData = GetDataAt(rowX, aStartColIndex); michael@0: if (cellData && (cellData->IsColSpan())) { michael@0: return true; // there is a col span into the region michael@0: } michael@0: cellData = GetDataAt(rowX, aEndColIndex + 1); michael@0: if (cellData && (cellData->IsColSpan())) { michael@0: return true; // there is a col span out of the region michael@0: } michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: void nsCellMap::InsertCells(nsTableCellMap& aMap, michael@0: nsTArray& aCellFrames, michael@0: int32_t aRowIndex, michael@0: int32_t aColIndexBefore, michael@0: int32_t aRgFirstRowIndex, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: if (aCellFrames.Length() == 0) return; michael@0: NS_ASSERTION(aColIndexBefore >= -1, "index out of range"); michael@0: int32_t numCols = aMap.GetColCount(); michael@0: if (aColIndexBefore >= numCols) { michael@0: NS_ERROR("Inserting instead of appending cells indicates a serious cellmap error"); michael@0: aColIndexBefore = numCols - 1; michael@0: } michael@0: michael@0: // get the starting col index of the 1st new cells michael@0: int32_t startColIndex; michael@0: for (startColIndex = aColIndexBefore + 1; startColIndex < numCols; startColIndex++) { michael@0: CellData* data = GetDataAt(aRowIndex, startColIndex); michael@0: if (!data || data->IsOrig() || data->IsDead()) { michael@0: // // Not a span. Stop. michael@0: break; michael@0: } michael@0: if (data->IsZeroColSpan()) { michael@0: // Zero colspans collapse. Stop in this case too. michael@0: CollapseZeroColSpan(aMap, data, aRowIndex, startColIndex); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: // record whether inserted cells are going to cause complications due michael@0: // to existing row spans, col spans or table sizing. michael@0: bool spansCauseRebuild = false; michael@0: michael@0: // check that all cells have the same row span michael@0: int32_t numNewCells = aCellFrames.Length(); michael@0: bool zeroRowSpan = false; michael@0: int32_t rowSpan = 0; michael@0: for (int32_t cellX = 0; cellX < numNewCells; cellX++) { michael@0: nsTableCellFrame* cell = aCellFrames.ElementAt(cellX); michael@0: int32_t rowSpan2 = GetRowSpanForNewCell(cell, aRowIndex, zeroRowSpan); michael@0: if (rowSpan == 0) { michael@0: rowSpan = rowSpan2; michael@0: } michael@0: else if (rowSpan != rowSpan2) { michael@0: spansCauseRebuild = true; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: // check if the new cells will cause the table to add more rows michael@0: if (!spansCauseRebuild) { michael@0: if (mRows.Length() < uint32_t(aRowIndex + rowSpan)) { michael@0: spansCauseRebuild = true; michael@0: } michael@0: } michael@0: michael@0: if (!spansCauseRebuild) { michael@0: spansCauseRebuild = CellsSpanInOrOut(aRowIndex, aRowIndex + rowSpan - 1, michael@0: startColIndex, numCols - 1); michael@0: } michael@0: if (spansCauseRebuild) { michael@0: aMap.RebuildConsideringCells(this, &aCellFrames, aRowIndex, startColIndex, michael@0: true, aDamageArea); michael@0: } michael@0: else { michael@0: ExpandWithCells(aMap, aCellFrames, aRowIndex, startColIndex, rowSpan, michael@0: zeroRowSpan, aRgFirstRowIndex, aDamageArea); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsCellMap::ExpandWithRows(nsTableCellMap& aMap, michael@0: nsTArray& aRowFrames, michael@0: int32_t aStartRowIndexIn, michael@0: int32_t aRgFirstRowIndex, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: int32_t startRowIndex = (aStartRowIndexIn >= 0) ? aStartRowIndexIn : 0; michael@0: NS_ASSERTION(uint32_t(startRowIndex) <= mRows.Length(), "caller should have grown cellmap before"); michael@0: michael@0: int32_t numNewRows = aRowFrames.Length(); michael@0: mContentRowCount += numNewRows; michael@0: michael@0: int32_t endRowIndex = startRowIndex + numNewRows - 1; michael@0: michael@0: // shift the rows after startRowIndex down and insert empty rows that will michael@0: // be filled via the AppendCell call below michael@0: if (!Grow(aMap, numNewRows, startRowIndex)) { michael@0: return; michael@0: } michael@0: michael@0: michael@0: int32_t newRowIndex = 0; michael@0: for (int32_t rowX = startRowIndex; rowX <= endRowIndex; rowX++) { michael@0: nsTableRowFrame* rFrame = aRowFrames.ElementAt(newRowIndex); michael@0: // append cells michael@0: nsIFrame* cFrame = rFrame->GetFirstPrincipalChild(); michael@0: int32_t colIndex = 0; michael@0: while (cFrame) { michael@0: nsTableCellFrame *cellFrame = do_QueryFrame(cFrame); michael@0: if (cellFrame) { michael@0: AppendCell(aMap, cellFrame, rowX, false, aRgFirstRowIndex, aDamageArea, michael@0: &colIndex); michael@0: } michael@0: cFrame = cFrame->GetNextSibling(); michael@0: } michael@0: newRowIndex++; michael@0: } michael@0: // mark all following rows damaged, they might contain a previously set michael@0: // damage area which we can not shift. michael@0: int32_t firstDamagedRow = aRgFirstRowIndex + startRowIndex; michael@0: SetDamageArea(0, firstDamagedRow, aMap.GetColCount(), michael@0: aMap.GetRowCount() - firstDamagedRow, aDamageArea); michael@0: } michael@0: michael@0: void nsCellMap::ExpandWithCells(nsTableCellMap& aMap, michael@0: nsTArray& aCellFrames, michael@0: int32_t aRowIndex, michael@0: int32_t aColIndex, michael@0: int32_t aRowSpan, // same for all cells michael@0: bool aRowSpanIsZero, michael@0: int32_t aRgFirstRowIndex, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: NS_ASSERTION(!!aMap.mBCInfo == mIsBC, "BC state mismatch"); michael@0: int32_t endRowIndex = aRowIndex + aRowSpan - 1; michael@0: int32_t startColIndex = aColIndex; michael@0: int32_t endColIndex = aColIndex; michael@0: int32_t numCells = aCellFrames.Length(); michael@0: int32_t totalColSpan = 0; michael@0: michael@0: // add cellData entries for the space taken up by the new cells michael@0: for (int32_t cellX = 0; cellX < numCells; cellX++) { michael@0: nsTableCellFrame* cellFrame = aCellFrames.ElementAt(cellX); michael@0: CellData* origData = AllocCellData(cellFrame); // the originating cell michael@0: if (!origData) return; michael@0: michael@0: // set the starting and ending col index for the new cell michael@0: bool zeroColSpan = false; michael@0: int32_t colSpan = GetColSpanForNewCell(*cellFrame, zeroColSpan); michael@0: if (zeroColSpan) { michael@0: aMap.mTableFrame.SetHasZeroColSpans(true); michael@0: aMap.mTableFrame.SetNeedColSpanExpansion(true); michael@0: } michael@0: totalColSpan += colSpan; michael@0: if (cellX == 0) { michael@0: endColIndex = aColIndex + colSpan - 1; michael@0: } michael@0: else { michael@0: startColIndex = endColIndex + 1; michael@0: endColIndex = startColIndex + colSpan - 1; michael@0: } michael@0: michael@0: // add the originating cell data and any cell data corresponding to row/col spans michael@0: for (int32_t rowX = aRowIndex; rowX <= endRowIndex; rowX++) { michael@0: CellDataArray& row = mRows[rowX]; michael@0: // Pre-allocate all the cells we'll need in this array, setting michael@0: // them to null. michael@0: // Have to have the cast to get the template to do the right thing. michael@0: int32_t insertionIndex = row.Length(); michael@0: if (insertionIndex > startColIndex) { michael@0: insertionIndex = startColIndex; michael@0: } michael@0: if (!row.InsertElementsAt(insertionIndex, endColIndex - insertionIndex + 1, michael@0: (CellData*)nullptr) && michael@0: rowX == aRowIndex) { michael@0: // Failed to insert the slots, and this is the very first row. That michael@0: // means that we need to clean up |origData| before returning, since michael@0: // the cellmap doesn't own it yet. michael@0: DestroyCellData(origData); michael@0: return; michael@0: } michael@0: michael@0: for (int32_t colX = startColIndex; colX <= endColIndex; colX++) { michael@0: CellData* data = origData; michael@0: if ((rowX != aRowIndex) || (colX != startColIndex)) { michael@0: data = AllocCellData(nullptr); michael@0: if (!data) return; michael@0: if (rowX > aRowIndex) { michael@0: data->SetRowSpanOffset(rowX - aRowIndex); michael@0: if (aRowSpanIsZero) { michael@0: data->SetZeroRowSpan(true); michael@0: } michael@0: } michael@0: if (colX > startColIndex) { michael@0: data->SetColSpanOffset(colX - startColIndex); michael@0: if (zeroColSpan) { michael@0: data->SetZeroColSpan(true); michael@0: } michael@0: } michael@0: } michael@0: SetDataAt(aMap, *data, rowX, colX); michael@0: } michael@0: } michael@0: cellFrame->SetColIndex(startColIndex); michael@0: } michael@0: int32_t damageHeight = std::min(GetRowGroup()->GetRowCount() - aRowIndex, michael@0: aRowSpan); michael@0: SetDamageArea(aColIndex, aRgFirstRowIndex + aRowIndex, michael@0: 1 + endColIndex - aColIndex, damageHeight, aDamageArea); michael@0: michael@0: int32_t rowX; michael@0: michael@0: // update the row and col info due to shifting michael@0: for (rowX = aRowIndex; rowX <= endRowIndex; rowX++) { michael@0: CellDataArray& row = mRows[rowX]; michael@0: uint32_t numCols = row.Length(); michael@0: uint32_t colX; michael@0: for (colX = aColIndex + totalColSpan; colX < numCols; colX++) { michael@0: CellData* data = row[colX]; michael@0: if (data) { michael@0: // increase the origin and span counts beyond the spanned cols michael@0: if (data->IsOrig()) { michael@0: // a cell that gets moved needs adjustment as well as it new orignating col michael@0: data->GetCellFrame()->SetColIndex(colX); michael@0: nsColInfo* colInfo = aMap.GetColInfoAt(colX); michael@0: colInfo->mNumCellsOrig++; michael@0: } michael@0: if (data->IsColSpan()) { michael@0: nsColInfo* colInfo = aMap.GetColInfoAt(colX); michael@0: colInfo->mNumCellsSpan++; michael@0: } michael@0: michael@0: // decrease the origin and span counts within the spanned cols michael@0: int32_t colX2 = colX - totalColSpan; michael@0: nsColInfo* colInfo2 = aMap.GetColInfoAt(colX2); michael@0: if (data->IsOrig()) { michael@0: // the old originating col of a moved cell needs adjustment michael@0: colInfo2->mNumCellsOrig--; michael@0: } michael@0: if (data->IsColSpan()) { michael@0: colInfo2->mNumCellsSpan--; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void nsCellMap::ShrinkWithoutRows(nsTableCellMap& aMap, michael@0: int32_t aStartRowIndex, michael@0: int32_t aNumRowsToRemove, michael@0: int32_t aRgFirstRowIndex, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: NS_ASSERTION(!!aMap.mBCInfo == mIsBC, "BC state mismatch"); michael@0: int32_t endRowIndex = aStartRowIndex + aNumRowsToRemove - 1; michael@0: uint32_t colCount = aMap.GetColCount(); michael@0: for (int32_t rowX = endRowIndex; rowX >= aStartRowIndex; --rowX) { michael@0: CellDataArray& row = mRows[rowX]; michael@0: uint32_t colX; michael@0: for (colX = 0; colX < colCount; colX++) { michael@0: CellData* data = row.SafeElementAt(colX); michael@0: if (data) { michael@0: // Adjust the column counts. michael@0: if (data->IsOrig()) { michael@0: // Decrement the column count. michael@0: nsColInfo* colInfo = aMap.GetColInfoAt(colX); michael@0: colInfo->mNumCellsOrig--; michael@0: } michael@0: // colspan=0 is only counted as a spanned cell in the 1st col it spans michael@0: else if (data->IsColSpan()) { michael@0: nsColInfo* colInfo = aMap.GetColInfoAt(colX); michael@0: colInfo->mNumCellsSpan--; michael@0: } michael@0: } michael@0: } michael@0: michael@0: uint32_t rowLength = row.Length(); michael@0: // Delete our row information. michael@0: for (colX = 0; colX < rowLength; colX++) { michael@0: DestroyCellData(row[colX]); michael@0: } michael@0: michael@0: mRows.RemoveElementAt(rowX); michael@0: michael@0: // Decrement our row and next available index counts. michael@0: mContentRowCount--; michael@0: } michael@0: aMap.RemoveColsAtEnd(); michael@0: // mark all following rows damaged, they might contain a previously set michael@0: // damage area which we can not shift. michael@0: int32_t firstDamagedRow = aRgFirstRowIndex + aStartRowIndex; michael@0: SetDamageArea(0, firstDamagedRow, aMap.GetColCount(), michael@0: aMap.GetRowCount() - firstDamagedRow, aDamageArea); michael@0: } michael@0: michael@0: int32_t nsCellMap::GetColSpanForNewCell(nsTableCellFrame& aCellFrameToAdd, michael@0: bool& aIsZeroColSpan) const michael@0: { michael@0: aIsZeroColSpan = false; michael@0: int32_t colSpan = aCellFrameToAdd.GetColSpan(); michael@0: if (0 == colSpan) { michael@0: colSpan = 1; // set the min colspan it will be expanded later michael@0: aIsZeroColSpan = true; michael@0: } michael@0: return colSpan; michael@0: } michael@0: michael@0: int32_t nsCellMap::GetEffectiveColSpan(const nsTableCellMap& aMap, michael@0: int32_t aRowIndex, michael@0: int32_t aColIndex, michael@0: bool& aZeroColSpan) const michael@0: { michael@0: int32_t numColsInTable = aMap.GetColCount(); michael@0: aZeroColSpan = false; michael@0: int32_t colSpan = 1; michael@0: if (uint32_t(aRowIndex) >= mRows.Length()) { michael@0: return colSpan; michael@0: } michael@0: michael@0: const CellDataArray& row = mRows[aRowIndex]; michael@0: int32_t colX; michael@0: CellData* data; michael@0: int32_t maxCols = numColsInTable; michael@0: bool hitOverlap = false; // XXX this is not ever being set to true michael@0: for (colX = aColIndex + 1; colX < maxCols; colX++) { michael@0: data = row.SafeElementAt(colX); michael@0: if (data) { michael@0: // for an overlapping situation get the colspan from the originating cell and michael@0: // use that as the max number of cols to iterate. Since this is rare, only michael@0: // pay the price of looking up the cell's colspan here. michael@0: if (!hitOverlap && data->IsOverlap()) { michael@0: CellData* origData = row.SafeElementAt(aColIndex); michael@0: if (origData && origData->IsOrig()) { michael@0: nsTableCellFrame* cellFrame = origData->GetCellFrame(); michael@0: if (cellFrame) { michael@0: // possible change the number of colums to iterate michael@0: maxCols = std::min(aColIndex + cellFrame->GetColSpan(), maxCols); michael@0: if (colX >= maxCols) michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: if (data->IsColSpan()) { michael@0: colSpan++; michael@0: if (data->IsZeroColSpan()) { michael@0: aZeroColSpan = true; michael@0: } michael@0: } michael@0: else { michael@0: break; michael@0: } michael@0: } michael@0: else break; michael@0: } michael@0: return colSpan; michael@0: } michael@0: michael@0: int32_t michael@0: nsCellMap::GetRowSpanForNewCell(nsTableCellFrame* aCellFrameToAdd, michael@0: int32_t aRowIndex, michael@0: bool& aIsZeroRowSpan) const michael@0: { michael@0: aIsZeroRowSpan = false; michael@0: int32_t rowSpan = aCellFrameToAdd->GetRowSpan(); michael@0: if (0 == rowSpan) { michael@0: // Use a min value of 2 for a zero rowspan to make computations easier michael@0: // elsewhere. Zero rowspans are only content dependent! michael@0: rowSpan = std::max(2, mContentRowCount - aRowIndex); michael@0: aIsZeroRowSpan = true; michael@0: } michael@0: return rowSpan; michael@0: } michael@0: michael@0: bool nsCellMap::HasMoreThanOneCell(int32_t aRowIndex) const michael@0: { michael@0: const CellDataArray& row = mRows.SafeElementAt(aRowIndex, *sEmptyRow); michael@0: uint32_t maxColIndex = row.Length(); michael@0: uint32_t count = 0; michael@0: uint32_t colIndex; michael@0: for (colIndex = 0; colIndex < maxColIndex; colIndex++) { michael@0: CellData* cellData = row[colIndex]; michael@0: if (cellData && (cellData->GetCellFrame() || cellData->IsRowSpan())) michael@0: count++; michael@0: if (count > 1) michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: int32_t michael@0: nsCellMap::GetNumCellsOriginatingInRow(int32_t aRowIndex) const michael@0: { michael@0: const CellDataArray& row = mRows.SafeElementAt(aRowIndex, *sEmptyRow); michael@0: uint32_t count = 0; michael@0: uint32_t maxColIndex = row.Length(); michael@0: uint32_t colIndex; michael@0: for (colIndex = 0; colIndex < maxColIndex; colIndex++) { michael@0: CellData* cellData = row[colIndex]; michael@0: if (cellData && cellData->IsOrig()) michael@0: count++; michael@0: } michael@0: return count; michael@0: } michael@0: michael@0: int32_t nsCellMap::GetRowSpan(int32_t aRowIndex, michael@0: int32_t aColIndex, michael@0: bool aGetEffective) const michael@0: { michael@0: int32_t rowSpan = 1; michael@0: int32_t rowCount = (aGetEffective) ? mContentRowCount : mRows.Length(); michael@0: int32_t rowX; michael@0: for (rowX = aRowIndex + 1; rowX < rowCount; rowX++) { michael@0: CellData* data = GetDataAt(rowX, aColIndex); michael@0: if (data) { michael@0: if (data->IsRowSpan()) { michael@0: rowSpan++; michael@0: } michael@0: else { michael@0: break; michael@0: } michael@0: } michael@0: else break; michael@0: } michael@0: return rowSpan; michael@0: } michael@0: michael@0: void nsCellMap::ShrinkWithoutCell(nsTableCellMap& aMap, michael@0: nsTableCellFrame& aCellFrame, michael@0: int32_t aRowIndex, michael@0: int32_t aColIndex, michael@0: int32_t aRgFirstRowIndex, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: NS_ASSERTION(!!aMap.mBCInfo == mIsBC, "BC state mismatch"); michael@0: uint32_t colX, rowX; michael@0: michael@0: // get the rowspan and colspan from the cell map since the content may have changed michael@0: bool zeroColSpan; michael@0: uint32_t numCols = aMap.GetColCount(); michael@0: int32_t rowSpan = GetRowSpan(aRowIndex, aColIndex, true); michael@0: uint32_t colSpan = GetEffectiveColSpan(aMap, aRowIndex, aColIndex, zeroColSpan); michael@0: uint32_t endRowIndex = aRowIndex + rowSpan - 1; michael@0: uint32_t endColIndex = aColIndex + colSpan - 1; michael@0: michael@0: if (aMap.mTableFrame.HasZeroColSpans()) { michael@0: aMap.mTableFrame.SetNeedColSpanExpansion(true); michael@0: } michael@0: michael@0: // adjust the col counts due to the deleted cell before removing it michael@0: for (colX = aColIndex; colX <= endColIndex; colX++) { michael@0: nsColInfo* colInfo = aMap.GetColInfoAt(colX); michael@0: if (colX == uint32_t(aColIndex)) { michael@0: colInfo->mNumCellsOrig--; michael@0: } michael@0: else { michael@0: colInfo->mNumCellsSpan--; michael@0: } michael@0: } michael@0: michael@0: // remove the deleted cell and cellData entries for it michael@0: for (rowX = aRowIndex; rowX <= endRowIndex; rowX++) { michael@0: CellDataArray& row = mRows[rowX]; michael@0: michael@0: // endIndexForRow points at the first slot we don't want to clean up. This michael@0: // makes the aColIndex == 0 case work right with our unsigned int colX. michael@0: NS_ASSERTION(endColIndex + 1 <= row.Length(), "span beyond the row size!"); michael@0: uint32_t endIndexForRow = std::min(endColIndex + 1, row.Length()); michael@0: michael@0: // Since endIndexForRow <= row.Length(), enough to compare aColIndex to it. michael@0: if (uint32_t(aColIndex) < endIndexForRow) { michael@0: for (colX = endIndexForRow; colX > uint32_t(aColIndex); colX--) { michael@0: DestroyCellData(row[colX-1]); michael@0: } michael@0: row.RemoveElementsAt(aColIndex, endIndexForRow - aColIndex); michael@0: } michael@0: } michael@0: michael@0: numCols = aMap.GetColCount(); michael@0: michael@0: // update the row and col info due to shifting michael@0: for (rowX = aRowIndex; rowX <= endRowIndex; rowX++) { michael@0: CellDataArray& row = mRows[rowX]; michael@0: for (colX = aColIndex; colX < numCols - colSpan; colX++) { michael@0: CellData* data = row.SafeElementAt(colX); michael@0: if (data) { michael@0: if (data->IsOrig()) { michael@0: // a cell that gets moved to the left needs adjustment in its new location michael@0: data->GetCellFrame()->SetColIndex(colX); michael@0: nsColInfo* colInfo = aMap.GetColInfoAt(colX); michael@0: colInfo->mNumCellsOrig++; michael@0: // a cell that gets moved to the left needs adjustment in its old location michael@0: colInfo = aMap.GetColInfoAt(colX + colSpan); michael@0: if (colInfo) { michael@0: colInfo->mNumCellsOrig--; michael@0: } michael@0: } michael@0: michael@0: else if (data->IsColSpan()) { michael@0: // a cell that gets moved to the left needs adjustment michael@0: // in its new location michael@0: nsColInfo* colInfo = aMap.GetColInfoAt(colX); michael@0: colInfo->mNumCellsSpan++; michael@0: // a cell that gets moved to the left needs adjustment michael@0: // in its old location michael@0: colInfo = aMap.GetColInfoAt(colX + colSpan); michael@0: if (colInfo) { michael@0: colInfo->mNumCellsSpan--; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: aMap.RemoveColsAtEnd(); michael@0: SetDamageArea(aColIndex, aRgFirstRowIndex + aRowIndex, michael@0: std::max(0, aMap.GetColCount() - aColIndex - 1), michael@0: 1 + endRowIndex - aRowIndex, aDamageArea); michael@0: } michael@0: michael@0: void michael@0: nsCellMap::RebuildConsideringRows(nsTableCellMap& aMap, michael@0: int32_t aStartRowIndex, michael@0: nsTArray* aRowsToInsert, michael@0: int32_t aNumRowsToRemove) michael@0: { michael@0: NS_ASSERTION(!!aMap.mBCInfo == mIsBC, "BC state mismatch"); michael@0: // copy the old cell map into a new array michael@0: uint32_t numOrigRows = mRows.Length(); michael@0: nsTArray origRows; michael@0: mRows.SwapElements(origRows); michael@0: michael@0: int32_t rowNumberChange; michael@0: if (aRowsToInsert) { michael@0: rowNumberChange = aRowsToInsert->Length(); michael@0: } else { michael@0: rowNumberChange = -aNumRowsToRemove; michael@0: } michael@0: michael@0: // adjust mContentRowCount based on the function arguments as they are known to michael@0: // be real rows. michael@0: mContentRowCount += rowNumberChange; michael@0: NS_ASSERTION(mContentRowCount >= 0, "previous mContentRowCount was wrong"); michael@0: // mRows is empty now. Grow it to the size we expect it to have. michael@0: if (mContentRowCount) { michael@0: if (!Grow(aMap, mContentRowCount)) { michael@0: // Bail, I guess... Not sure what else we can do here. michael@0: return; michael@0: } michael@0: } michael@0: michael@0: // aStartRowIndex might be after all existing rows so we should limit the michael@0: // copy to the amount of exisiting rows michael@0: uint32_t copyEndRowIndex = std::min(numOrigRows, uint32_t(aStartRowIndex)); michael@0: michael@0: // rowX keeps track of where we are in mRows while setting up the michael@0: // new cellmap. michael@0: uint32_t rowX = 0; michael@0: nsIntRect damageArea; michael@0: // put back the rows before the affected ones just as before. Note that we michael@0: // can't just copy the old rows in bit-for-bit, because they might be michael@0: // spanning out into the rows we're adding/removing. michael@0: for ( ; rowX < copyEndRowIndex; rowX++) { michael@0: const CellDataArray& row = origRows[rowX]; michael@0: uint32_t numCols = row.Length(); michael@0: for (uint32_t colX = 0; colX < numCols; colX++) { michael@0: // put in the original cell from the cell map michael@0: const CellData* data = row.ElementAt(colX); michael@0: if (data && data->IsOrig()) { michael@0: AppendCell(aMap, data->GetCellFrame(), rowX, false, 0, damageArea); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Now handle the new rows being inserted, if any. michael@0: uint32_t copyStartRowIndex; michael@0: rowX = aStartRowIndex; michael@0: if (aRowsToInsert) { michael@0: // add in the new cells and create rows if necessary michael@0: int32_t numNewRows = aRowsToInsert->Length(); michael@0: for (int32_t newRowX = 0; newRowX < numNewRows; newRowX++) { michael@0: nsTableRowFrame* rFrame = aRowsToInsert->ElementAt(newRowX); michael@0: nsIFrame* cFrame = rFrame->GetFirstPrincipalChild(); michael@0: while (cFrame) { michael@0: nsTableCellFrame *cellFrame = do_QueryFrame(cFrame); michael@0: if (cellFrame) { michael@0: AppendCell(aMap, cellFrame, rowX, false, 0, damageArea); michael@0: } michael@0: cFrame = cFrame->GetNextSibling(); michael@0: } michael@0: rowX++; michael@0: } michael@0: copyStartRowIndex = aStartRowIndex; michael@0: } michael@0: else { michael@0: copyStartRowIndex = aStartRowIndex + aNumRowsToRemove; michael@0: } michael@0: michael@0: // put back the rows after the affected ones just as before. Again, we can't michael@0: // just copy the old bits because that would not handle the new rows spanning michael@0: // out or our earlier old rows spanning through the damaged area. michael@0: for (uint32_t copyRowX = copyStartRowIndex; copyRowX < numOrigRows; michael@0: copyRowX++) { michael@0: const CellDataArray& row = origRows[copyRowX]; michael@0: uint32_t numCols = row.Length(); michael@0: for (uint32_t colX = 0; colX < numCols; colX++) { michael@0: // put in the original cell from the cell map michael@0: CellData* data = row.ElementAt(colX); michael@0: if (data && data->IsOrig()) { michael@0: AppendCell(aMap, data->GetCellFrame(), rowX, false, 0, damageArea); michael@0: } michael@0: } michael@0: rowX++; michael@0: } michael@0: michael@0: // delete the old cell map. Now rowX no longer has anything to do with mRows michael@0: for (rowX = 0; rowX < numOrigRows; rowX++) { michael@0: CellDataArray& row = origRows[rowX]; michael@0: uint32_t len = row.Length(); michael@0: for (uint32_t colX = 0; colX < len; colX++) { michael@0: DestroyCellData(row[colX]); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsCellMap::RebuildConsideringCells(nsTableCellMap& aMap, michael@0: int32_t aNumOrigCols, michael@0: nsTArray* aCellFrames, michael@0: int32_t aRowIndex, michael@0: int32_t aColIndex, michael@0: bool aInsert) michael@0: { michael@0: NS_ASSERTION(!!aMap.mBCInfo == mIsBC, "BC state mismatch"); michael@0: // copy the old cell map into a new array michael@0: int32_t numOrigRows = mRows.Length(); michael@0: nsTArray origRows; michael@0: mRows.SwapElements(origRows); michael@0: michael@0: int32_t numNewCells = (aCellFrames) ? aCellFrames->Length() : 0; michael@0: michael@0: // the new cells might extend the previous column number michael@0: NS_ASSERTION(aNumOrigCols >= aColIndex, "Appending cells far beyond cellmap data?!"); michael@0: int32_t numCols = aInsert ? std::max(aNumOrigCols, aColIndex + 1) : aNumOrigCols; michael@0: michael@0: // build the new cell map. Hard to say what, if anything, we can preallocate michael@0: // here... Should come back to that sometime, perhaps. michael@0: int32_t rowX; michael@0: nsIntRect damageArea; michael@0: for (rowX = 0; rowX < numOrigRows; rowX++) { michael@0: const CellDataArray& row = origRows[rowX]; michael@0: for (int32_t colX = 0; colX < numCols; colX++) { michael@0: if ((rowX == aRowIndex) && (colX == aColIndex)) { michael@0: if (aInsert) { // put in the new cells michael@0: for (int32_t cellX = 0; cellX < numNewCells; cellX++) { michael@0: nsTableCellFrame* cell = aCellFrames->ElementAt(cellX); michael@0: if (cell) { michael@0: AppendCell(aMap, cell, rowX, false, 0, damageArea); michael@0: } michael@0: } michael@0: } michael@0: else { michael@0: continue; // do not put the deleted cell back michael@0: } michael@0: } michael@0: // put in the original cell from the cell map michael@0: CellData* data = row.SafeElementAt(colX); michael@0: if (data && data->IsOrig()) { michael@0: AppendCell(aMap, data->GetCellFrame(), rowX, false, 0, damageArea); michael@0: } michael@0: } michael@0: } michael@0: if (aInsert && numOrigRows <= aRowIndex) { // append the new cells below the last original row michael@0: NS_ASSERTION (numOrigRows == aRowIndex, "Appending cells far beyond the last row"); michael@0: for (int32_t cellX = 0; cellX < numNewCells; cellX++) { michael@0: nsTableCellFrame* cell = aCellFrames->ElementAt(cellX); michael@0: if (cell) { michael@0: AppendCell(aMap, cell, aRowIndex, false, 0, damageArea); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // delete the old cell map michael@0: for (rowX = 0; rowX < numOrigRows; rowX++) { michael@0: CellDataArray& row = origRows[rowX]; michael@0: uint32_t len = row.Length(); michael@0: for (uint32_t colX = 0; colX < len; colX++) { michael@0: DestroyCellData(row.SafeElementAt(colX)); michael@0: } michael@0: } michael@0: // expand the cellmap to cover empty content rows michael@0: if (mRows.Length() < uint32_t(mContentRowCount)) { michael@0: Grow(aMap, mContentRowCount - mRows.Length()); michael@0: } michael@0: michael@0: } michael@0: michael@0: void nsCellMap::RemoveCell(nsTableCellMap& aMap, michael@0: nsTableCellFrame* aCellFrame, michael@0: int32_t aRowIndex, michael@0: int32_t aRgFirstRowIndex, michael@0: nsIntRect& aDamageArea) michael@0: { michael@0: uint32_t numRows = mRows.Length(); michael@0: if (uint32_t(aRowIndex) >= numRows) { michael@0: NS_ERROR("bad arg in nsCellMap::RemoveCell"); michael@0: return; michael@0: } michael@0: int32_t numCols = aMap.GetColCount(); michael@0: michael@0: // Now aRowIndex is guaranteed OK. michael@0: michael@0: // get the starting col index of the cell to remove michael@0: int32_t startColIndex; michael@0: for (startColIndex = 0; startColIndex < numCols; startColIndex++) { michael@0: CellData* data = mRows[aRowIndex].SafeElementAt(startColIndex); michael@0: if (data && (data->IsOrig()) && (aCellFrame == data->GetCellFrame())) { michael@0: break; // we found the col index michael@0: } michael@0: } michael@0: michael@0: int32_t rowSpan = GetRowSpan(aRowIndex, startColIndex, false); michael@0: // record whether removing the cells is going to cause complications due michael@0: // to existing row spans, col spans or table sizing. michael@0: bool spansCauseRebuild = CellsSpanInOrOut(aRowIndex, michael@0: aRowIndex + rowSpan - 1, michael@0: startColIndex, numCols - 1); michael@0: // XXX if the cell has a col span to the end of the map, and the end has no originating michael@0: // cells, we need to assume that this the only such cell, and rebuild so that there are michael@0: // no extraneous cols at the end. The same is true for removing rows. michael@0: if (!aCellFrame->GetRowSpan() || !aCellFrame->GetColSpan()) michael@0: spansCauseRebuild = true; michael@0: michael@0: if (spansCauseRebuild) { michael@0: aMap.RebuildConsideringCells(this, nullptr, aRowIndex, startColIndex, false, michael@0: aDamageArea); michael@0: } michael@0: else { michael@0: ShrinkWithoutCell(aMap, *aCellFrame, aRowIndex, startColIndex, michael@0: aRgFirstRowIndex, aDamageArea); michael@0: } michael@0: } michael@0: michael@0: void nsCellMap::ExpandZeroColSpans(nsTableCellMap& aMap) michael@0: { michael@0: NS_ASSERTION(!!aMap.mBCInfo == mIsBC, "BC state mismatch"); michael@0: uint32_t numRows = mRows.Length(); michael@0: uint32_t numCols = aMap.GetColCount(); michael@0: uint32_t rowIndex, colIndex; michael@0: michael@0: for (rowIndex = 0; rowIndex < numRows; rowIndex++) { michael@0: for (colIndex = 0; colIndex < numCols; colIndex++) { michael@0: CellData* data = mRows[rowIndex].SafeElementAt(colIndex); michael@0: if (!data || !data->IsOrig()) michael@0: continue; michael@0: nsTableCellFrame* cell = data->GetCellFrame(); michael@0: NS_ASSERTION(cell, "There has to be a cell"); michael@0: int32_t cellRowSpan = cell->GetRowSpan(); michael@0: int32_t cellColSpan = cell->GetColSpan(); michael@0: bool rowZeroSpan = (0 == cell->GetRowSpan()); michael@0: bool colZeroSpan = (0 == cell->GetColSpan()); michael@0: if (colZeroSpan) { michael@0: aMap.mTableFrame.SetHasZeroColSpans(true); michael@0: // do the expansion michael@0: NS_ASSERTION(numRows > 0, "Bogus numRows"); michael@0: NS_ASSERTION(numCols > 0, "Bogus numCols"); michael@0: uint32_t endRowIndex = rowZeroSpan ? numRows - 1 : michael@0: rowIndex + cellRowSpan - 1; michael@0: uint32_t endColIndex = colZeroSpan ? numCols - 1 : michael@0: colIndex + cellColSpan - 1; michael@0: uint32_t colX, rowX; michael@0: colX = colIndex + 1; michael@0: while (colX <= endColIndex) { michael@0: // look at columns from here to our colspan. For each one, check michael@0: // the rows from here to our rowspan to make sure there is no michael@0: // obstacle to marking that column as a zerospanned column; if there michael@0: // isn't, mark it so michael@0: for (rowX = rowIndex; rowX <= endRowIndex; rowX++) { michael@0: CellData* oldData = GetDataAt(rowX, colX); michael@0: if (oldData) { michael@0: if (oldData->IsOrig()) { michael@0: break; // something is in the way michael@0: } michael@0: if (oldData->IsRowSpan()) { michael@0: if ((rowX - rowIndex) != oldData->GetRowSpanOffset()) { michael@0: break; michael@0: } michael@0: } michael@0: if (oldData->IsColSpan()) { michael@0: if ((colX - colIndex) != oldData->GetColSpanOffset()) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: if (endRowIndex >= rowX) michael@0: break;// we hit something michael@0: for (rowX = rowIndex; rowX <= endRowIndex; rowX++) { michael@0: CellData* newData = AllocCellData(nullptr); michael@0: if (!newData) return; michael@0: michael@0: newData->SetColSpanOffset(colX - colIndex); michael@0: newData->SetZeroColSpan(true); michael@0: michael@0: if (rowX > rowIndex) { michael@0: newData->SetRowSpanOffset(rowX - rowIndex); michael@0: if (rowZeroSpan) michael@0: newData->SetZeroRowSpan(true); michael@0: } michael@0: SetDataAt(aMap, *newData, rowX, colX); michael@0: } michael@0: colX++; michael@0: } // while (colX <= endColIndex) michael@0: } // if zerocolspan michael@0: } michael@0: } michael@0: } michael@0: #ifdef DEBUG michael@0: void nsCellMap::Dump(bool aIsBorderCollapse) const michael@0: { michael@0: printf("\n ***** START GROUP CELL MAP DUMP ***** %p\n", (void*)this); michael@0: nsTableRowGroupFrame* rg = GetRowGroup(); michael@0: const nsStyleDisplay* display = rg->StyleDisplay(); michael@0: switch (display->mDisplay) { michael@0: case NS_STYLE_DISPLAY_TABLE_HEADER_GROUP: michael@0: printf(" thead "); michael@0: break; michael@0: case NS_STYLE_DISPLAY_TABLE_FOOTER_GROUP: michael@0: printf(" tfoot "); michael@0: break; michael@0: case NS_STYLE_DISPLAY_TABLE_ROW_GROUP: michael@0: printf(" tbody "); michael@0: break; michael@0: default: michael@0: printf("HUH? wrong display type on rowgroup"); michael@0: } michael@0: uint32_t mapRowCount = mRows.Length(); michael@0: printf("mapRowCount=%u tableRowCount=%d\n", mapRowCount, mContentRowCount); michael@0: michael@0: michael@0: uint32_t rowIndex, colIndex; michael@0: for (rowIndex = 0; rowIndex < mapRowCount; rowIndex++) { michael@0: const CellDataArray& row = mRows[rowIndex]; michael@0: printf(" row %d : ", rowIndex); michael@0: uint32_t colCount = row.Length(); michael@0: for (colIndex = 0; colIndex < colCount; colIndex++) { michael@0: CellData* cd = row[colIndex]; michael@0: if (cd) { michael@0: if (cd->IsOrig()) { michael@0: printf("C%d,%d ", rowIndex, colIndex); michael@0: } else { michael@0: if (cd->IsRowSpan()) { michael@0: printf("R "); michael@0: } michael@0: if (cd->IsColSpan()) { michael@0: printf("C "); michael@0: } michael@0: if (!(cd->IsRowSpan() && cd->IsColSpan())) { michael@0: printf(" "); michael@0: } michael@0: printf(" "); michael@0: } michael@0: } else { michael@0: printf("---- "); michael@0: } michael@0: } michael@0: if (aIsBorderCollapse) { michael@0: nscoord size; michael@0: BCBorderOwner owner; michael@0: mozilla::css::Side side; michael@0: bool segStart; michael@0: bool bevel; michael@0: for (int32_t i = 0; i <= 2; i++) { michael@0: printf("\n "); michael@0: for (colIndex = 0; colIndex < colCount; colIndex++) { michael@0: BCCellData* cd = (BCCellData *)row[colIndex]; michael@0: if (cd) { michael@0: if (0 == i) { michael@0: size = cd->mData.GetTopEdge(owner, segStart); michael@0: printf("t=%d%d%d ", int32_t(size), owner, segStart); michael@0: } michael@0: else if (1 == i) { michael@0: size = cd->mData.GetLeftEdge(owner, segStart); michael@0: printf("l=%d%d%d ", int32_t(size), owner, segStart); michael@0: } michael@0: else { michael@0: size = cd->mData.GetCorner(side, bevel); michael@0: printf("c=%d%d%d ", int32_t(size), side, bevel); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: printf("\n"); michael@0: } michael@0: michael@0: // output info mapping Ci,j to cell address michael@0: uint32_t cellCount = 0; michael@0: for (uint32_t rIndex = 0; rIndex < mapRowCount; rIndex++) { michael@0: const CellDataArray& row = mRows[rIndex]; michael@0: uint32_t colCount = row.Length(); michael@0: printf(" "); michael@0: for (colIndex = 0; colIndex < colCount; colIndex++) { michael@0: CellData* cd = row[colIndex]; michael@0: if (cd) { michael@0: if (cd->IsOrig()) { michael@0: nsTableCellFrame* cellFrame = cd->GetCellFrame(); michael@0: int32_t cellFrameColIndex; michael@0: cellFrame->GetColIndex(cellFrameColIndex); michael@0: printf("C%d,%d=%p(%d) ", rIndex, colIndex, (void*)cellFrame, michael@0: cellFrameColIndex); michael@0: cellCount++; michael@0: } michael@0: } michael@0: } michael@0: printf("\n"); michael@0: } michael@0: michael@0: printf(" ***** END GROUP CELL MAP DUMP *****\n"); michael@0: } michael@0: #endif michael@0: michael@0: CellData* michael@0: nsCellMap::GetDataAt(int32_t aMapRowIndex, michael@0: int32_t aColIndex) const michael@0: { michael@0: return michael@0: mRows.SafeElementAt(aMapRowIndex, *sEmptyRow).SafeElementAt(aColIndex); michael@0: } michael@0: michael@0: // only called if the cell at aMapRowIndex, aColIndex is null or dead michael@0: // (the latter from ExpandZeroColSpans). michael@0: void nsCellMap::SetDataAt(nsTableCellMap& aMap, michael@0: CellData& aNewCell, michael@0: int32_t aMapRowIndex, michael@0: int32_t aColIndex) michael@0: { michael@0: NS_ASSERTION(!!aMap.mBCInfo == mIsBC, "BC state mismatch"); michael@0: if (uint32_t(aMapRowIndex) >= mRows.Length()) { michael@0: NS_ERROR("SetDataAt called with row index > num rows"); michael@0: return; michael@0: } michael@0: michael@0: CellDataArray& row = mRows[aMapRowIndex]; michael@0: michael@0: // the table map may need cols added michael@0: int32_t numColsToAdd = aColIndex + 1 - aMap.GetColCount(); michael@0: if (numColsToAdd > 0) { michael@0: aMap.AddColsAtEnd(numColsToAdd); michael@0: } michael@0: // the row may need cols added michael@0: numColsToAdd = aColIndex + 1 - row.Length(); michael@0: if (numColsToAdd > 0) { michael@0: // XXXbz need to handle allocation failures. michael@0: GrowRow(row, numColsToAdd); michael@0: } michael@0: michael@0: DestroyCellData(row[aColIndex]); michael@0: michael@0: row.ReplaceElementsAt(aColIndex, 1, &aNewCell); michael@0: // update the originating cell counts if cell originates in this row, col michael@0: nsColInfo* colInfo = aMap.GetColInfoAt(aColIndex); michael@0: if (colInfo) { michael@0: if (aNewCell.IsOrig()) { michael@0: colInfo->mNumCellsOrig++; michael@0: } michael@0: else if (aNewCell.IsColSpan()) { michael@0: colInfo->mNumCellsSpan++; michael@0: } michael@0: } michael@0: else NS_ERROR("SetDataAt called with col index > table map num cols"); michael@0: } michael@0: michael@0: nsTableCellFrame* michael@0: nsCellMap::GetCellInfoAt(const nsTableCellMap& aMap, michael@0: int32_t aRowX, michael@0: int32_t aColX, michael@0: bool* aOriginates, michael@0: int32_t* aColSpan) const michael@0: { michael@0: if (aOriginates) { michael@0: *aOriginates = false; michael@0: } michael@0: CellData* data = GetDataAt(aRowX, aColX); michael@0: nsTableCellFrame* cellFrame = nullptr; michael@0: if (data) { michael@0: if (data->IsOrig()) { michael@0: cellFrame = data->GetCellFrame(); michael@0: if (aOriginates) michael@0: *aOriginates = true; michael@0: } michael@0: else { michael@0: cellFrame = GetCellFrame(aRowX, aColX, *data, true); michael@0: } michael@0: if (cellFrame && aColSpan) { michael@0: int32_t initialColIndex; michael@0: cellFrame->GetColIndex(initialColIndex); michael@0: bool zeroSpan; michael@0: *aColSpan = GetEffectiveColSpan(aMap, aRowX, initialColIndex, zeroSpan); michael@0: } michael@0: } michael@0: return cellFrame; michael@0: } michael@0: michael@0: michael@0: bool nsCellMap::RowIsSpannedInto(int32_t aRowIndex, michael@0: int32_t aNumEffCols) const michael@0: { michael@0: if ((0 > aRowIndex) || (aRowIndex >= mContentRowCount)) { michael@0: return false; michael@0: } michael@0: for (int32_t colIndex = 0; colIndex < aNumEffCols; colIndex++) { michael@0: CellData* cd = GetDataAt(aRowIndex, colIndex); michael@0: if (cd) { // there's really a cell at (aRowIndex, colIndex) michael@0: if (cd->IsSpan()) { // the cell at (aRowIndex, colIndex) is the result of a span michael@0: if (cd->IsRowSpan() && GetCellFrame(aRowIndex, colIndex, *cd, true)) { // XXX why the last check michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool nsCellMap::RowHasSpanningCells(int32_t aRowIndex, michael@0: int32_t aNumEffCols) const michael@0: { michael@0: if ((0 > aRowIndex) || (aRowIndex >= mContentRowCount)) { michael@0: return false; michael@0: } michael@0: if (aRowIndex != mContentRowCount - 1) { michael@0: // aRowIndex is not the last row, so we check the next row after aRowIndex for spanners michael@0: for (int32_t colIndex = 0; colIndex < aNumEffCols; colIndex++) { michael@0: CellData* cd = GetDataAt(aRowIndex, colIndex); michael@0: if (cd && (cd->IsOrig())) { // cell originates michael@0: CellData* cd2 = GetDataAt(aRowIndex + 1, colIndex); michael@0: if (cd2 && cd2->IsRowSpan()) { // cd2 is spanned by a row michael@0: if (cd->GetCellFrame() == GetCellFrame(aRowIndex + 1, colIndex, *cd2, true)) { michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: void nsCellMap::DestroyCellData(CellData* aData) michael@0: { michael@0: if (!aData) { michael@0: return; michael@0: } michael@0: michael@0: if (mIsBC) { michael@0: BCCellData* bcData = static_cast(aData); michael@0: bcData->~BCCellData(); michael@0: mPresContext->FreeToShell(sizeof(BCCellData), bcData); michael@0: } else { michael@0: aData->~CellData(); michael@0: mPresContext->FreeToShell(sizeof(CellData), aData); michael@0: } michael@0: } michael@0: michael@0: CellData* nsCellMap::AllocCellData(nsTableCellFrame* aOrigCell) michael@0: { michael@0: if (mIsBC) { michael@0: BCCellData* data = (BCCellData*) michael@0: mPresContext->AllocateFromShell(sizeof(BCCellData)); michael@0: if (data) { michael@0: new (data) BCCellData(aOrigCell); michael@0: } michael@0: return data; michael@0: } michael@0: michael@0: CellData* data = (CellData*) michael@0: mPresContext->AllocateFromShell(sizeof(CellData)); michael@0: if (data) { michael@0: new (data) CellData(aOrigCell); michael@0: } michael@0: return data; michael@0: } michael@0: michael@0: void michael@0: nsCellMapColumnIterator::AdvanceRowGroup() michael@0: { michael@0: do { michael@0: mCurMapStart += mCurMapContentRowCount; michael@0: mCurMap = mCurMap->GetNextSibling(); michael@0: if (!mCurMap) { michael@0: // Set mCurMapContentRowCount and mCurMapRelevantRowCount to 0 in case michael@0: // mCurMap has no next sibling. This can happen if we just handled the michael@0: // last originating cell. Future calls will end up with mFoundCells == michael@0: // mOrigCells, but for this one mFoundCells was definitely not big enough michael@0: // if we got here. michael@0: mCurMapContentRowCount = 0; michael@0: mCurMapRelevantRowCount = 0; michael@0: break; michael@0: } michael@0: michael@0: mCurMapContentRowCount = mCurMap->GetRowCount(); michael@0: uint32_t rowArrayLength = mCurMap->mRows.Length(); michael@0: mCurMapRelevantRowCount = std::min(mCurMapContentRowCount, rowArrayLength); michael@0: } while (0 == mCurMapRelevantRowCount); michael@0: michael@0: NS_ASSERTION(mCurMapRelevantRowCount != 0 || !mCurMap, michael@0: "How did that happen?"); michael@0: michael@0: // Set mCurMapRow to 0, since cells can't span across table row groups. michael@0: mCurMapRow = 0; michael@0: } michael@0: michael@0: void michael@0: nsCellMapColumnIterator::IncrementRow(int32_t aIncrement) michael@0: { michael@0: NS_PRECONDITION(aIncrement >= 0, "Bogus increment"); michael@0: NS_PRECONDITION(mCurMap, "Bogus mOrigCells?"); michael@0: if (aIncrement == 0) { michael@0: AdvanceRowGroup(); michael@0: } michael@0: else { michael@0: mCurMapRow += aIncrement; michael@0: if (mCurMapRow >= mCurMapRelevantRowCount) { michael@0: AdvanceRowGroup(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: nsTableCellFrame* michael@0: nsCellMapColumnIterator::GetNextFrame(int32_t* aRow, int32_t* aColSpan) michael@0: { michael@0: // Fast-path for the case when we don't have anything left in the column and michael@0: // we know it. michael@0: if (mFoundCells == mOrigCells) { michael@0: *aRow = 0; michael@0: *aColSpan = 1; michael@0: return nullptr; michael@0: } michael@0: michael@0: while (1) { michael@0: NS_ASSERTION(mCurMapRow < mCurMapRelevantRowCount, "Bogus mOrigCells?"); michael@0: // Safe to just get the row (which is faster than calling GetDataAt(), but michael@0: // there may not be that many cells in it, so have to use SafeElementAt for michael@0: // the mCol. michael@0: const nsCellMap::CellDataArray& row = mCurMap->mRows[mCurMapRow]; michael@0: CellData* cellData = row.SafeElementAt(mCol); michael@0: if (!cellData || cellData->IsDead()) { michael@0: // Could hit this if there are fewer cells in this row than others, for michael@0: // example. michael@0: IncrementRow(1); michael@0: continue; michael@0: } michael@0: michael@0: if (cellData->IsColSpan()) { michael@0: // Look up the originating data for this cell, advance by its relative rowspan. michael@0: int32_t rowspanOffset = cellData->GetRowSpanOffset(); michael@0: nsTableCellFrame* cellFrame = mCurMap->GetCellFrame(mCurMapRow, mCol, *cellData, false); michael@0: NS_ASSERTION(cellFrame,"Must have usable originating data here"); michael@0: int32_t rowSpan = cellFrame->GetRowSpan(); michael@0: if (rowSpan == 0) { michael@0: AdvanceRowGroup(); michael@0: } michael@0: else { michael@0: IncrementRow(rowSpan - rowspanOffset); michael@0: } michael@0: continue; michael@0: } michael@0: michael@0: NS_ASSERTION(cellData->IsOrig(), michael@0: "Must have originating cellData by this point. " michael@0: "See comment on mCurMapRow in header."); michael@0: michael@0: nsTableCellFrame* cellFrame = cellData->GetCellFrame(); michael@0: NS_ASSERTION(cellFrame, "Orig data without cellframe?"); michael@0: michael@0: *aRow = mCurMapStart + mCurMapRow; michael@0: bool ignoredZeroSpan; michael@0: *aColSpan = mCurMap->GetEffectiveColSpan(*mMap, mCurMapRow, mCol, michael@0: ignoredZeroSpan); michael@0: michael@0: IncrementRow(cellFrame->GetRowSpan()); michael@0: michael@0: ++mFoundCells; michael@0: michael@0: NS_ABORT_IF_FALSE(cellData == mMap->GetDataAt(*aRow, mCol), michael@0: "Giving caller bogus row?"); michael@0: michael@0: return cellFrame; michael@0: } michael@0: michael@0: NS_NOTREACHED("Can't get here"); michael@0: return nullptr; michael@0: }