1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "mozilla/PresShell.h"
9 #include "mozilla/StaticPtr.h"
11 #include "nsTableFrame.h"
12 #include "nsTableCellFrame.h"
13 #include "nsTableRowFrame.h"
14 #include "nsTableRowGroupFrame.h"
17 using namespace mozilla
;
19 static void SetDamageArea(int32_t aStartCol
, int32_t aStartRow
,
20 int32_t aColCount
, int32_t aRowCount
,
21 TableArea
& aDamageArea
) {
22 NS_ASSERTION(aStartCol
>= 0, "negative col index");
23 NS_ASSERTION(aStartRow
>= 0, "negative row index");
24 NS_ASSERTION(aColCount
>= 0, "negative col count");
25 NS_ASSERTION(aRowCount
>= 0, "negative row count");
26 aDamageArea
.StartCol() = aStartCol
;
27 aDamageArea
.StartRow() = aStartRow
;
28 aDamageArea
.ColCount() = aColCount
;
29 aDamageArea
.RowCount() = aRowCount
;
32 // Empty static array used for SafeElementAt() calls on mRows.
33 static StaticAutoPtr
<nsCellMap::CellDataArray
> sEmptyRow
;
37 CellData::CellData(nsTableCellFrame
* aOrigCell
) {
38 MOZ_COUNT_CTOR(CellData
);
39 static_assert(sizeof(mOrigCell
) == sizeof(mBits
),
40 "mOrigCell and mBits must be the same size");
41 mOrigCell
= aOrigCell
;
44 CellData::~CellData() { MOZ_COUNT_DTOR(CellData
); }
46 BCCellData::BCCellData(nsTableCellFrame
* aOrigCell
) : CellData(aOrigCell
) {
47 MOZ_COUNT_CTOR(BCCellData
);
50 BCCellData::~BCCellData() { MOZ_COUNT_DTOR(BCCellData
); }
54 nsTableCellMap::nsTableCellMap(nsTableFrame
& aTableFrame
, bool aBorderCollapse
)
55 : mTableFrame(aTableFrame
), mFirstMap(nullptr), mBCInfo(nullptr) {
56 MOZ_COUNT_CTOR(nsTableCellMap
);
58 nsTableFrame::RowGroupArray orderedRowGroups
= aTableFrame
.OrderedRowGroups();
60 nsTableRowGroupFrame
* prior
= nullptr;
61 for (uint32_t rgX
= 0; rgX
< orderedRowGroups
.Length(); rgX
++) {
62 nsTableRowGroupFrame
* rgFrame
= orderedRowGroups
[rgX
];
63 InsertGroupCellMap(rgFrame
, prior
);
66 if (aBorderCollapse
) {
67 mBCInfo
= new BCInfo();
71 nsTableCellMap::~nsTableCellMap() {
72 MOZ_COUNT_DTOR(nsTableCellMap
);
74 nsCellMap
* cellMap
= mFirstMap
;
76 nsCellMap
* next
= cellMap
->GetNextSibling();
82 DeleteIEndBEndBorders();
87 // Get the bcData holding the border segments of the iEnd edge of the table
88 BCData
* nsTableCellMap::GetIEndMostBorder(int32_t aRowIndex
) {
89 if (!mBCInfo
) ABORT1(nullptr);
91 int32_t numRows
= mBCInfo
->mIEndBorders
.Length();
92 if (aRowIndex
< numRows
) {
93 return &mBCInfo
->mIEndBorders
.ElementAt(aRowIndex
);
96 mBCInfo
->mIEndBorders
.SetLength(aRowIndex
+ 1);
97 return &mBCInfo
->mIEndBorders
.ElementAt(aRowIndex
);
100 // Get the bcData holding the border segments of the bEnd edge of the table
101 BCData
* nsTableCellMap::GetBEndMostBorder(int32_t aColIndex
) {
102 if (!mBCInfo
) ABORT1(nullptr);
104 int32_t numCols
= mBCInfo
->mBEndBorders
.Length();
105 if (aColIndex
< numCols
) {
106 return &mBCInfo
->mBEndBorders
.ElementAt(aColIndex
);
109 mBCInfo
->mBEndBorders
.SetLength(aColIndex
+ 1);
110 return &mBCInfo
->mBEndBorders
.ElementAt(aColIndex
);
113 // delete the borders corresponding to the iEnd and bEnd edges of the table
114 void nsTableCellMap::DeleteIEndBEndBorders() {
116 mBCInfo
->mBEndBorders
.Clear();
117 mBCInfo
->mIEndBorders
.Clear();
121 void nsTableCellMap::InsertGroupCellMap(nsCellMap
* aPrevMap
,
122 nsCellMap
& aNewMap
) {
125 next
= aPrevMap
->GetNextSibling();
126 aPrevMap
->SetNextSibling(&aNewMap
);
129 mFirstMap
= &aNewMap
;
131 aNewMap
.SetNextSibling(next
);
134 void nsTableCellMap::InsertGroupCellMap(nsTableRowGroupFrame
* aNewGroup
,
135 nsTableRowGroupFrame
*& aPrevGroup
) {
136 nsCellMap
* newMap
= new nsCellMap(aNewGroup
, mBCInfo
!= nullptr);
137 nsCellMap
* prevMap
= nullptr;
138 nsCellMap
* lastMap
= mFirstMap
;
140 nsCellMap
* map
= mFirstMap
;
143 if (map
->GetRowGroup() == aPrevGroup
) {
147 map
= map
->GetNextSibling();
153 aPrevGroup
= (prevMap
) ? prevMap
->GetRowGroup() : nullptr;
155 aPrevGroup
= nullptr;
158 InsertGroupCellMap(prevMap
, *newMap
);
161 void nsTableCellMap::RemoveGroupCellMap(nsTableRowGroupFrame
* aGroup
) {
162 nsCellMap
* map
= mFirstMap
;
163 nsCellMap
* prior
= nullptr;
165 if (map
->GetRowGroup() == aGroup
) {
166 nsCellMap
* next
= map
->GetNextSibling();
167 if (mFirstMap
== map
) {
170 prior
->SetNextSibling(next
);
176 map
= map
->GetNextSibling();
180 static nsCellMap
* FindMapFor(const nsTableRowGroupFrame
* aRowGroup
,
181 nsCellMap
* aStart
, const nsCellMap
* aEnd
) {
182 for (nsCellMap
* map
= aStart
; map
!= aEnd
; map
= map
->GetNextSibling()) {
183 if (aRowGroup
== map
->GetRowGroup()) {
191 nsCellMap
* nsTableCellMap::GetMapFor(const nsTableRowGroupFrame
* aRowGroup
,
192 nsCellMap
* aStartHint
) const {
193 MOZ_ASSERT(aRowGroup
, "Must have a rowgroup");
194 NS_ASSERTION(!aRowGroup
->GetPrevInFlow(),
195 "GetMapFor called with continuation");
197 nsCellMap
* map
= FindMapFor(aRowGroup
, aStartHint
, nullptr);
203 nsCellMap
* map
= FindMapFor(aRowGroup
, mFirstMap
, aStartHint
);
208 // If aRowGroup is a repeated header or footer find the header or footer it
209 // was repeated from.
210 // Bug 1442018: we also need this search for header/footer frames that are
211 // not marked as _repeatable_ because they have a next-in-flow, as they may
212 // nevertheless have been _repeated_ from an earlier fragment.
213 auto isTableHeaderFooterGroup
= [](const nsTableRowGroupFrame
* aRG
) -> bool {
214 const auto display
= aRG
->StyleDisplay()->mDisplay
;
215 return display
== StyleDisplay::TableHeaderGroup
||
216 display
== StyleDisplay::TableFooterGroup
;
218 if (aRowGroup
->IsRepeatable() ||
219 (aRowGroup
->GetNextInFlow() && isTableHeaderFooterGroup(aRowGroup
))) {
220 auto findOtherRowGroupOfType
=
221 [aRowGroup
](nsTableFrame
* aTable
) -> nsTableRowGroupFrame
* {
222 const auto display
= aRowGroup
->StyleDisplay()->mDisplay
;
223 auto* table
= aTable
->FirstContinuation();
224 for (; table
; table
= table
->GetNextContinuation()) {
225 for (auto* child
: table
->PrincipalChildList()) {
226 if (child
->StyleDisplay()->mDisplay
== display
&&
227 child
!= aRowGroup
) {
228 return static_cast<nsTableRowGroupFrame
*>(child
);
234 if (auto* rgOrig
= findOtherRowGroupOfType(&mTableFrame
)) {
235 return GetMapFor(rgOrig
, aStartHint
);
237 MOZ_ASSERT_UNREACHABLE(
238 "A repeated header/footer should always have an "
239 "original header/footer it was repeated from");
245 void nsTableCellMap::Synchronize(nsTableFrame
* aTableFrame
) {
246 AutoTArray
<nsCellMap
*, 8> maps
;
248 nsTableFrame::RowGroupArray orderedRowGroups
=
249 aTableFrame
->OrderedRowGroups();
250 if (!orderedRowGroups
.Length()) {
254 // XXXbz this fails if orderedRowGroups is missing some row groups
255 // (due to OOM when appending to the array, e.g. -- we leak maps in
258 // Scope |map| outside the loop so we can use it as a hint.
259 nsCellMap
* map
= nullptr;
260 for (uint32_t rgX
= 0; rgX
< orderedRowGroups
.Length(); rgX
++) {
261 nsTableRowGroupFrame
* rgFrame
= orderedRowGroups
[rgX
];
262 map
= GetMapFor(static_cast<nsTableRowGroupFrame
*>(rgFrame
->FirstInFlow()),
265 // XXX(Bug 1631371) Check if this should use a fallible operation as it
266 // pretended earlier, or change the return type to void.
267 maps
.AppendElement(map
);
270 if (maps
.IsEmpty()) {
271 MOZ_ASSERT(!mFirstMap
);
275 int32_t mapIndex
= maps
.Length() - 1; // Might end up -1
276 nsCellMap
* nextMap
= maps
.ElementAt(mapIndex
);
277 nextMap
->SetNextSibling(nullptr);
278 for (mapIndex
--; mapIndex
>= 0; mapIndex
--) {
279 nsCellMap
* map
= maps
.ElementAt(mapIndex
);
280 map
->SetNextSibling(nextMap
);
286 bool nsTableCellMap::HasMoreThanOneCell(int32_t aRowIndex
) const {
287 int32_t rowIndex
= aRowIndex
;
288 nsCellMap
* map
= mFirstMap
;
290 if (map
->GetRowCount() > rowIndex
) {
291 return map
->HasMoreThanOneCell(rowIndex
);
293 rowIndex
-= map
->GetRowCount();
294 map
= map
->GetNextSibling();
299 int32_t nsTableCellMap::GetNumCellsOriginatingInRow(int32_t aRowIndex
) const {
300 int32_t rowIndex
= aRowIndex
;
301 nsCellMap
* map
= mFirstMap
;
303 if (map
->GetRowCount() > rowIndex
) {
304 return map
->GetNumCellsOriginatingInRow(rowIndex
);
306 rowIndex
-= map
->GetRowCount();
307 map
= map
->GetNextSibling();
311 int32_t nsTableCellMap::GetEffectiveRowSpan(int32_t aRowIndex
,
312 int32_t aColIndex
) const {
313 int32_t rowIndex
= aRowIndex
;
314 nsCellMap
* map
= mFirstMap
;
316 if (map
->GetRowCount() > rowIndex
) {
317 return map
->GetRowSpan(rowIndex
, aColIndex
, true);
319 rowIndex
-= map
->GetRowCount();
320 map
= map
->GetNextSibling();
322 MOZ_ASSERT_UNREACHABLE("Bogus row index?");
326 int32_t nsTableCellMap::GetEffectiveColSpan(int32_t aRowIndex
,
327 int32_t aColIndex
) const {
328 int32_t rowIndex
= aRowIndex
;
329 nsCellMap
* map
= mFirstMap
;
331 if (map
->GetRowCount() > rowIndex
) {
332 return map
->GetEffectiveColSpan(*this, rowIndex
, aColIndex
);
334 rowIndex
-= map
->GetRowCount();
335 map
= map
->GetNextSibling();
337 MOZ_ASSERT_UNREACHABLE("Bogus row index?");
341 nsTableCellFrame
* nsTableCellMap::GetCellFrame(int32_t aRowIndex
,
344 bool aUseRowIfOverlap
) const {
345 int32_t rowIndex
= aRowIndex
;
346 nsCellMap
* map
= mFirstMap
;
348 if (map
->GetRowCount() > rowIndex
) {
349 return map
->GetCellFrame(rowIndex
, aColIndex
, aData
, aUseRowIfOverlap
);
351 rowIndex
-= map
->GetRowCount();
352 map
= map
->GetNextSibling();
357 nsColInfo
* nsTableCellMap::GetColInfoAt(int32_t aColIndex
) {
358 int32_t numColsToAdd
= aColIndex
+ 1 - mCols
.Length();
359 if (numColsToAdd
> 0) {
360 AddColsAtEnd(numColsToAdd
); // XXX this could fail to add cols in theory
362 return &mCols
.ElementAt(aColIndex
);
365 int32_t nsTableCellMap::GetRowCount() const {
367 nsCellMap
* map
= mFirstMap
;
369 numRows
+= map
->GetRowCount();
370 map
= map
->GetNextSibling();
375 CellData
* nsTableCellMap::GetDataAt(int32_t aRowIndex
,
376 int32_t aColIndex
) const {
377 int32_t rowIndex
= aRowIndex
;
378 nsCellMap
* map
= mFirstMap
;
380 if (map
->GetRowCount() > rowIndex
) {
381 return map
->GetDataAt(rowIndex
, aColIndex
);
383 rowIndex
-= map
->GetRowCount();
384 map
= map
->GetNextSibling();
389 void nsTableCellMap::AddColsAtEnd(uint32_t aNumCols
) {
390 // XXX(Bug 1631371) Check if this should use a fallible operation as it
391 // pretended earlier.
392 mCols
.AppendElements(aNumCols
);
394 // XXX(Bug 1631371) Check if this should use a fallible operation as it
395 // pretended earlier.
396 mBCInfo
->mBEndBorders
.AppendElements(aNumCols
);
400 void nsTableCellMap::RemoveColsAtEnd() {
401 // Remove the cols at the end which don't have originating cells or cells
402 // spanning into them. Only do this if the col was created as
404 int32_t numCols
= GetColCount();
405 int32_t lastGoodColIndex
= mTableFrame
.GetIndexOfLastRealCol();
406 MOZ_ASSERT(lastGoodColIndex
>= -1);
407 for (int32_t colX
= numCols
- 1; colX
> lastGoodColIndex
; colX
--) {
408 nsColInfo
& colInfo
= mCols
.ElementAt(colX
);
409 if ((colInfo
.mNumCellsOrig
<= 0) && (colInfo
.mNumCellsSpan
<= 0)) {
410 mCols
.RemoveElementAt(colX
);
413 int32_t count
= mBCInfo
->mBEndBorders
.Length();
415 mBCInfo
->mBEndBorders
.RemoveElementAt(colX
);
419 break; // only remove until we encounter the 1st valid one
424 void nsTableCellMap::ClearCols() {
427 mBCInfo
->mBEndBorders
.Clear();
430 void nsTableCellMap::InsertRows(nsTableRowGroupFrame
* aParent
,
431 nsTArray
<nsTableRowFrame
*>& aRows
,
432 int32_t aFirstRowIndex
, bool aConsiderSpans
,
433 TableArea
& aDamageArea
) {
434 int32_t numNewRows
= aRows
.Length();
435 if ((numNewRows
<= 0) || (aFirstRowIndex
< 0)) ABORT0();
437 int32_t rowIndex
= aFirstRowIndex
;
438 int32_t rgStartRowIndex
= 0;
439 nsCellMap
* cellMap
= mFirstMap
;
441 nsTableRowGroupFrame
* rg
= cellMap
->GetRowGroup();
443 cellMap
->InsertRows(*this, aRows
, rowIndex
, aConsiderSpans
,
444 rgStartRowIndex
, aDamageArea
);
445 #ifdef DEBUG_TABLE_CELLMAP
446 Dump("after InsertRows");
449 int32_t count
= mBCInfo
->mIEndBorders
.Length();
450 if (aFirstRowIndex
< count
) {
451 for (int32_t rowX
= aFirstRowIndex
;
452 rowX
< aFirstRowIndex
+ numNewRows
; rowX
++) {
453 mBCInfo
->mIEndBorders
.InsertElementAt(rowX
);
457 aFirstRowIndex
); // this will create missing entries
458 for (int32_t rowX
= aFirstRowIndex
+ 1;
459 rowX
< aFirstRowIndex
+ numNewRows
; rowX
++) {
460 mBCInfo
->mIEndBorders
.AppendElement();
466 int32_t rowCount
= cellMap
->GetRowCount();
467 rgStartRowIndex
+= rowCount
;
468 rowIndex
-= rowCount
;
469 cellMap
= cellMap
->GetNextSibling();
472 NS_ERROR("Attempt to insert row into wrong map.");
475 void nsTableCellMap::RemoveRows(int32_t aFirstRowIndex
,
476 int32_t aNumRowsToRemove
, bool aConsiderSpans
,
477 TableArea
& aDamageArea
) {
478 int32_t rowIndex
= aFirstRowIndex
;
479 int32_t rgStartRowIndex
= 0;
480 nsCellMap
* cellMap
= mFirstMap
;
482 int32_t rowCount
= cellMap
->GetRowCount();
483 if (rowCount
> rowIndex
) {
484 cellMap
->RemoveRows(*this, rowIndex
, aNumRowsToRemove
, aConsiderSpans
,
485 rgStartRowIndex
, aDamageArea
);
487 for (int32_t rowX
= aFirstRowIndex
+ aNumRowsToRemove
- 1;
488 rowX
>= aFirstRowIndex
; rowX
--) {
489 if (uint32_t(rowX
) < mBCInfo
->mIEndBorders
.Length()) {
490 mBCInfo
->mIEndBorders
.RemoveElementAt(rowX
);
496 rgStartRowIndex
+= rowCount
;
497 rowIndex
-= rowCount
;
498 cellMap
= cellMap
->GetNextSibling();
500 #ifdef DEBUG_TABLE_CELLMAP
501 Dump("after RemoveRows");
505 CellData
* nsTableCellMap::AppendCell(nsTableCellFrame
& aCellFrame
,
507 bool aRebuildIfNecessary
,
508 TableArea
& aDamageArea
) {
509 MOZ_ASSERT(&aCellFrame
== aCellFrame
.FirstInFlow(),
510 "invalid call on continuing frame");
511 nsIFrame
* rgFrame
= aCellFrame
.GetParent(); // get the row
515 rgFrame
= rgFrame
->GetParent(); // get the row group
520 CellData
* result
= nullptr;
521 int32_t rowIndex
= aRowIndex
;
522 int32_t rgStartRowIndex
= 0;
523 nsCellMap
* cellMap
= mFirstMap
;
525 if (cellMap
->GetRowGroup() == rgFrame
) {
527 cellMap
->AppendCell(*this, &aCellFrame
, rowIndex
, aRebuildIfNecessary
,
528 rgStartRowIndex
, aDamageArea
);
531 int32_t rowCount
= cellMap
->GetRowCount();
532 rgStartRowIndex
+= rowCount
;
533 rowIndex
-= rowCount
;
534 cellMap
= cellMap
->GetNextSibling();
536 #ifdef DEBUG_TABLE_CELLMAP
537 Dump("after AppendCell");
542 void nsTableCellMap::InsertCells(nsTArray
<nsTableCellFrame
*>& aCellFrames
,
543 int32_t aRowIndex
, int32_t aColIndexBefore
,
544 TableArea
& aDamageArea
) {
545 int32_t rowIndex
= aRowIndex
;
546 int32_t rgStartRowIndex
= 0;
547 nsCellMap
* cellMap
= mFirstMap
;
549 int32_t rowCount
= cellMap
->GetRowCount();
550 if (rowCount
> rowIndex
) {
551 cellMap
->InsertCells(*this, aCellFrames
, rowIndex
, aColIndexBefore
,
552 rgStartRowIndex
, aDamageArea
);
555 rgStartRowIndex
+= rowCount
;
556 rowIndex
-= rowCount
;
557 cellMap
= cellMap
->GetNextSibling();
559 #ifdef DEBUG_TABLE_CELLMAP
560 Dump("after InsertCells");
564 void nsTableCellMap::RemoveCell(nsTableCellFrame
* aCellFrame
, int32_t aRowIndex
,
565 TableArea
& aDamageArea
) {
566 if (!aCellFrame
) ABORT0();
567 MOZ_ASSERT(aCellFrame
== aCellFrame
->FirstInFlow(),
568 "invalid call on continuing frame");
569 int32_t rowIndex
= aRowIndex
;
570 int32_t rgStartRowIndex
= 0;
571 nsCellMap
* cellMap
= mFirstMap
;
573 int32_t rowCount
= cellMap
->GetRowCount();
574 if (rowCount
> rowIndex
) {
575 cellMap
->RemoveCell(*this, aCellFrame
, rowIndex
, rgStartRowIndex
,
577 #ifdef DEBUG_TABLE_CELLMAP
578 Dump("after RemoveCell");
582 rgStartRowIndex
+= rowCount
;
583 rowIndex
-= rowCount
;
584 cellMap
= cellMap
->GetNextSibling();
586 // if we reach this point - the cell did not get removed, the caller of this
587 // routine will delete the cell and the cellmap will probably hold a reference
588 // to the deleted cell which will cause a subsequent crash when this cell is
590 NS_ERROR("nsTableCellMap::RemoveCell - could not remove cell");
593 void nsTableCellMap::RebuildConsideringCells(
594 nsCellMap
* aCellMap
, nsTArray
<nsTableCellFrame
*>* aCellFrames
,
595 int32_t aRowIndex
, int32_t aColIndex
, bool aInsert
,
596 TableArea
& aDamageArea
) {
597 int32_t numOrigCols
= GetColCount();
599 nsCellMap
* cellMap
= mFirstMap
;
600 int32_t rowCount
= 0;
602 if (cellMap
== aCellMap
) {
603 cellMap
->RebuildConsideringCells(*this, numOrigCols
, aCellFrames
,
604 aRowIndex
, aColIndex
, aInsert
);
606 cellMap
->RebuildConsideringCells(*this, numOrigCols
, nullptr, -1, 0,
609 rowCount
+= cellMap
->GetRowCount();
610 cellMap
= cellMap
->GetNextSibling();
612 SetDamageArea(0, 0, GetColCount(), rowCount
, aDamageArea
);
615 void nsTableCellMap::RebuildConsideringRows(
616 nsCellMap
* aCellMap
, int32_t aStartRowIndex
,
617 nsTArray
<nsTableRowFrame
*>* aRowsToInsert
, int32_t aNumRowsToRemove
,
618 TableArea
& aDamageArea
) {
619 MOZ_ASSERT(!aRowsToInsert
|| aNumRowsToRemove
== 0,
620 "Can't handle both removing and inserting rows at once");
622 int32_t numOrigCols
= GetColCount();
624 nsCellMap
* cellMap
= mFirstMap
;
625 int32_t rowCount
= 0;
627 if (cellMap
== aCellMap
) {
628 cellMap
->RebuildConsideringRows(*this, aStartRowIndex
, aRowsToInsert
,
631 cellMap
->RebuildConsideringCells(*this, numOrigCols
, nullptr, -1, 0,
634 rowCount
+= cellMap
->GetRowCount();
635 cellMap
= cellMap
->GetNextSibling();
637 SetDamageArea(0, 0, GetColCount(), rowCount
, aDamageArea
);
640 int32_t nsTableCellMap::GetNumCellsOriginatingInCol(int32_t aColIndex
) const {
641 int32_t colCount
= mCols
.Length();
642 if ((aColIndex
>= 0) && (aColIndex
< colCount
)) {
643 return mCols
.ElementAt(aColIndex
).mNumCellsOrig
;
645 NS_ERROR("nsCellMap::GetNumCellsOriginatingInCol - bad col index");
651 void nsTableCellMap::Dump(char* aString
) const {
652 if (aString
) printf("%s \n", aString
);
653 printf("***** START TABLE CELL MAP DUMP ***** %p\n", (void*)this);
655 int32_t colCount
= mCols
.Length();
656 printf("cols array orig/span-> %p", (void*)this);
657 for (int32_t colX
= 0; colX
< colCount
; colX
++) {
658 const nsColInfo
& colInfo
= mCols
.ElementAt(colX
);
659 printf("%d=%d/%d ", colX
, colInfo
.mNumCellsOrig
, colInfo
.mNumCellsSpan
);
661 printf(" cols in cache %d\n", int(mTableFrame
.GetColCache().Length()));
662 nsCellMap
* cellMap
= mFirstMap
;
664 cellMap
->Dump(nullptr != mBCInfo
);
665 cellMap
= cellMap
->GetNextSibling();
667 if (nullptr != mBCInfo
) {
668 printf("***** block-end borders *****\n");
675 int32_t numCols
= mBCInfo
->mBEndBorders
.Length();
676 for (int32_t i
= 0; i
<= 2; i
++) {
678 for (colIndex
= 0; colIndex
< numCols
; colIndex
++) {
679 BCData
& cd
= mBCInfo
->mBEndBorders
.ElementAt(colIndex
);
681 size
= cd
.GetBStartEdge(owner
, segStart
);
682 printf("t=%d%X%d ", int32_t(size
), owner
, segStart
);
684 size
= cd
.GetIStartEdge(owner
, segStart
);
685 printf("l=%d%X%d ", int32_t(size
), owner
, segStart
);
687 size
= cd
.GetCorner(side
, bevel
);
688 printf("c=%d%hhX%d ", int32_t(size
), static_cast<uint8_t>(side
),
692 BCData
& cd
= mBCInfo
->mBEndIEndCorner
;
694 size
= cd
.GetBStartEdge(owner
, segStart
);
695 printf("t=%d%X%d ", int32_t(size
), owner
, segStart
);
697 size
= cd
.GetIStartEdge(owner
, segStart
);
698 printf("l=%d%X%d ", int32_t(size
), owner
, segStart
);
700 size
= cd
.GetCorner(side
, bevel
);
701 printf("c=%d%hhX%d ", int32_t(size
), static_cast<uint8_t>(side
), bevel
);
706 printf("***** END TABLE CELL MAP DUMP *****\n");
710 nsTableCellFrame
* nsTableCellMap::GetCellInfoAt(int32_t aRowIndex
,
713 int32_t* aColSpan
) const {
714 int32_t rowIndex
= aRowIndex
;
715 nsCellMap
* cellMap
= mFirstMap
;
717 if (cellMap
->GetRowCount() > rowIndex
) {
718 return cellMap
->GetCellInfoAt(*this, rowIndex
, aColIndex
, aOriginates
,
721 rowIndex
-= cellMap
->GetRowCount();
722 cellMap
= cellMap
->GetNextSibling();
727 int32_t nsTableCellMap::GetIndexByRowAndColumn(int32_t aRow
,
728 int32_t aColumn
) const {
731 int32_t colCount
= mCols
.Length();
732 int32_t rowIndex
= aRow
;
734 nsCellMap
* cellMap
= mFirstMap
;
736 int32_t rowCount
= cellMap
->GetRowCount();
737 if (rowIndex
>= rowCount
) {
738 // If the rowCount is less than the rowIndex, this means that the index is
739 // not within the current map. If so, get the index of the last cell in
741 rowIndex
-= rowCount
;
743 int32_t cellMapIdx
= cellMap
->GetHighestIndex(colCount
);
744 if (cellMapIdx
!= -1) {
745 index
+= cellMapIdx
+ 1;
749 // Index is in valid range for this cellmap, so get the index of rowIndex
752 cellMap
->GetIndexByRowAndColumn(colCount
, rowIndex
, aColumn
);
753 if (cellMapIdx
== -1) {
754 return -1; // no cell at the given row and column.
758 return index
; // no need to look through further maps here
761 cellMap
= cellMap
->GetNextSibling();
767 void nsTableCellMap::GetRowAndColumnByIndex(int32_t aIndex
, int32_t* aRow
,
768 int32_t* aColumn
) const {
772 int32_t colCount
= mCols
.Length();
774 int32_t previousRows
= 0;
775 int32_t index
= aIndex
;
777 nsCellMap
* cellMap
= mFirstMap
;
779 int32_t rowCount
= cellMap
->GetRowCount();
780 // Determine the highest possible index in this map to see
781 // if wanted index is in here.
782 int32_t cellMapIdx
= cellMap
->GetHighestIndex(colCount
);
783 if (cellMapIdx
== -1) {
784 // The index is not within this map, increase the total row index
786 previousRows
+= rowCount
;
788 if (index
> cellMapIdx
) {
789 // The index is not within this map, so decrease it by the cellMapIdx
790 // determined index and increase the total row index accordingly.
791 index
-= cellMapIdx
+ 1;
792 previousRows
+= rowCount
;
794 cellMap
->GetRowAndColumnByIndex(colCount
, index
, aRow
, aColumn
);
795 // If there were previous indexes, take them into account.
796 *aRow
+= previousRows
;
797 return; // no need to look any further.
801 cellMap
= cellMap
->GetNextSibling();
805 bool nsTableCellMap::RowIsSpannedInto(int32_t aRowIndex
,
806 int32_t aNumEffCols
) const {
807 int32_t rowIndex
= aRowIndex
;
808 nsCellMap
* cellMap
= mFirstMap
;
810 if (cellMap
->GetRowCount() > rowIndex
) {
811 return cellMap
->RowIsSpannedInto(rowIndex
, aNumEffCols
);
813 rowIndex
-= cellMap
->GetRowCount();
814 cellMap
= cellMap
->GetNextSibling();
819 bool nsTableCellMap::RowHasSpanningCells(int32_t aRowIndex
,
820 int32_t aNumEffCols
) const {
821 int32_t rowIndex
= aRowIndex
;
822 nsCellMap
* cellMap
= mFirstMap
;
824 if (cellMap
->GetRowCount() > rowIndex
) {
825 return cellMap
->RowHasSpanningCells(rowIndex
, aNumEffCols
);
827 rowIndex
-= cellMap
->GetRowCount();
828 cellMap
= cellMap
->GetNextSibling();
833 // FIXME: The only value callers pass for aSide is LogicalSide::BEnd.
834 // Consider removing support for the other three values.
835 void nsTableCellMap::ResetBStartStart(LogicalSide aSide
, nsCellMap
& aCellMap
,
836 uint32_t aRowGroupStart
,
837 uint32_t aRowIndex
, uint32_t aColIndex
) {
838 if (!mBCInfo
) ABORT0();
840 BCCellData
* cellData
;
841 BCData
* bcData
= nullptr;
844 case LogicalSide::BEnd
:
847 case LogicalSide::BStart
:
848 cellData
= (BCCellData
*)aCellMap
.GetDataAt(aRowIndex
- aRowGroupStart
,
851 bcData
= &cellData
->mData
;
853 NS_ASSERTION(aSide
== LogicalSide::BEnd
, "program error");
854 // try the next row group
855 nsCellMap
* cellMap
= aCellMap
.GetNextSibling();
857 cellData
= (BCCellData
*)cellMap
->GetDataAt(0, aColIndex
);
859 bcData
= &cellData
->mData
;
861 bcData
= GetBEndMostBorder(aColIndex
);
866 case LogicalSide::IEnd
:
869 case LogicalSide::IStart
:
870 cellData
= (BCCellData
*)aCellMap
.GetDataAt(aRowIndex
- aRowGroupStart
,
873 bcData
= &cellData
->mData
;
875 NS_ASSERTION(aSide
== LogicalSide::IEnd
, "program error");
876 bcData
= GetIEndMostBorder(aRowIndex
);
881 bcData
->SetBStartStart(false);
885 // store the aSide border segment at coord = (aRowIndex, aColIndex). For
886 // bStart/iStart, store the info at coord. For bEnd/iEnd store it at the
887 // adjacent location so that it is bStart/iStart at that location. If the new
888 // location is at the iEnd or bEnd edge of the table, then store it one of the
889 // special arrays (iEnd-most borders, bEnd-most borders).
890 void nsTableCellMap::SetBCBorderEdge(LogicalSide aSide
, nsCellMap
& aCellMap
,
891 uint32_t aCellMapStart
, uint32_t aRowIndex
,
892 uint32_t aColIndex
, uint32_t aLength
,
893 BCBorderOwner aOwner
, nscoord aSize
,
895 if (!mBCInfo
) ABORT0();
897 BCCellData
* cellData
;
898 int32_t lastIndex
, xIndex
, yIndex
;
899 int32_t xPos
= aColIndex
;
900 int32_t yPos
= aRowIndex
;
901 int32_t rgYPos
= aRowIndex
- aCellMapStart
;
905 case LogicalSide::BEnd
:
909 case LogicalSide::BStart
:
910 lastIndex
= xPos
+ aLength
- 1;
911 for (xIndex
= xPos
; xIndex
<= lastIndex
; xIndex
++) {
912 changed
= aChanged
&& (xIndex
== xPos
);
913 BCData
* bcData
= nullptr;
914 cellData
= (BCCellData
*)aCellMap
.GetDataAt(rgYPos
, xIndex
);
916 int32_t numRgRows
= aCellMap
.GetRowCount();
917 if (yPos
< numRgRows
) { // add a dead cell data
918 TableArea damageArea
;
919 cellData
= (BCCellData
*)aCellMap
.AppendCell(*this, nullptr, rgYPos
,
920 false, 0, damageArea
);
921 if (!cellData
) ABORT0();
923 NS_ASSERTION(aSide
== LogicalSide::BEnd
, "program error");
924 // try the next non empty row group
925 nsCellMap
* cellMap
= aCellMap
.GetNextSibling();
926 while (cellMap
&& (0 == cellMap
->GetRowCount())) {
927 cellMap
= cellMap
->GetNextSibling();
930 cellData
= (BCCellData
*)cellMap
->GetDataAt(0, xIndex
);
931 if (!cellData
) { // add a dead cell
932 TableArea damageArea
;
933 cellData
= (BCCellData
*)cellMap
->AppendCell(
934 *this, nullptr, 0, false, 0, damageArea
);
936 } else { // must be at the end of the table
937 bcData
= GetBEndMostBorder(xIndex
);
941 if (!bcData
&& cellData
) {
942 bcData
= &cellData
->mData
;
945 bcData
->SetBStartEdge(aOwner
, aSize
, changed
);
947 NS_ERROR("Cellmap: BStart edge not found");
951 case LogicalSide::IEnd
:
954 case LogicalSide::IStart
:
955 // since bStart, bEnd borders were set, there should already be a cellData
957 lastIndex
= rgYPos
+ aLength
- 1;
958 for (yIndex
= rgYPos
; yIndex
<= lastIndex
; yIndex
++) {
959 changed
= aChanged
&& (yIndex
== rgYPos
);
960 cellData
= (BCCellData
*)aCellMap
.GetDataAt(yIndex
, xPos
);
962 cellData
->mData
.SetIStartEdge(aOwner
, aSize
, changed
);
964 NS_ASSERTION(aSide
== LogicalSide::IEnd
, "program error");
965 BCData
* bcData
= GetIEndMostBorder(yIndex
+ aCellMapStart
);
967 bcData
->SetIStartEdge(aOwner
, aSize
, changed
);
969 NS_ERROR("Cellmap: IStart edge not found");
977 // store corner info (aOwner, aSubSize, aBevel). For aCorner = eBStartIStart,
978 // store the info at (aRowIndex, aColIndex). For eBStartIEnd, store it in the
979 // entry to the iEnd-wards where it would be BStartIStart. For eBEndIEnd, store
980 // it in the entry to the bEnd-wards. etc.
981 void nsTableCellMap::SetBCBorderCorner(LogicalCorner aCorner
,
983 uint32_t aCellMapStart
,
984 uint32_t aRowIndex
, uint32_t aColIndex
,
985 LogicalSide aOwner
, nscoord aSubSize
,
986 bool aBevel
, bool aIsBEndIEnd
) {
987 if (!mBCInfo
) ABORT0();
990 mBCInfo
->mBEndIEndCorner
.SetCorner(aSubSize
, aOwner
, aBevel
);
994 int32_t xPos
= aColIndex
;
995 int32_t yPos
= aRowIndex
;
996 int32_t rgYPos
= aRowIndex
- aCellMapStart
;
998 if (LogicalCorner::BStartIEnd
== aCorner
) {
1000 } else if (LogicalCorner::BEndIEnd
== aCorner
) {
1004 } else if (LogicalCorner::BEndIStart
== aCorner
) {
1009 BCCellData
* cellData
= nullptr;
1010 BCData
* bcData
= nullptr;
1011 if (GetColCount() <= xPos
) {
1012 NS_ASSERTION(xPos
== GetColCount(), "program error");
1013 // at the iEnd edge of the table as we checked the corner before
1014 NS_ASSERTION(!aIsBEndIEnd
, "should be handled before");
1015 bcData
= GetIEndMostBorder(yPos
);
1017 cellData
= (BCCellData
*)aCellMap
.GetDataAt(rgYPos
, xPos
);
1019 int32_t numRgRows
= aCellMap
.GetRowCount();
1020 if (yPos
< numRgRows
) { // add a dead cell data
1021 TableArea damageArea
;
1022 cellData
= (BCCellData
*)aCellMap
.AppendCell(*this, nullptr, rgYPos
,
1023 false, 0, damageArea
);
1025 // try the next non empty row group
1026 nsCellMap
* cellMap
= aCellMap
.GetNextSibling();
1027 while (cellMap
&& (0 == cellMap
->GetRowCount())) {
1028 cellMap
= cellMap
->GetNextSibling();
1031 cellData
= (BCCellData
*)cellMap
->GetDataAt(0, xPos
);
1032 if (!cellData
) { // add a dead cell
1033 TableArea damageArea
;
1034 cellData
= (BCCellData
*)cellMap
->AppendCell(*this, nullptr, 0,
1035 false, 0, damageArea
);
1037 } else { // must be at the bEnd of the table
1038 bcData
= GetBEndMostBorder(xPos
);
1043 if (!bcData
&& cellData
) {
1044 bcData
= &cellData
->mData
;
1047 bcData
->SetCorner(aSubSize
, aOwner
, aBevel
);
1049 NS_ERROR("program error: Corner not found");
1053 nsCellMap::nsCellMap(nsTableRowGroupFrame
* aRowGroup
, bool aIsBC
)
1055 mContentRowCount(0),
1056 mRowGroupFrame(aRowGroup
),
1057 mNextSibling(nullptr),
1059 mPresContext(aRowGroup
->PresContext()) {
1060 MOZ_COUNT_CTOR(nsCellMap
);
1061 NS_ASSERTION(mPresContext
, "Must have prescontext");
1064 nsCellMap::~nsCellMap() {
1065 MOZ_COUNT_DTOR(nsCellMap
);
1067 uint32_t mapRowCount
= mRows
.Length();
1068 for (uint32_t rowX
= 0; rowX
< mapRowCount
; rowX
++) {
1069 CellDataArray
& row
= mRows
[rowX
];
1070 uint32_t colCount
= row
.Length();
1071 for (uint32_t colX
= 0; colX
< colCount
; colX
++) {
1072 DestroyCellData(row
[colX
]);
1078 void nsCellMap::Init() {
1079 MOZ_ASSERT(!sEmptyRow
, "How did that happen?");
1080 sEmptyRow
= new nsCellMap::CellDataArray();
1084 void nsCellMap::Shutdown() { sEmptyRow
= nullptr; }
1086 nsTableCellFrame
* nsCellMap::GetCellFrame(int32_t aRowIndexIn
,
1087 int32_t aColIndexIn
, CellData
& aData
,
1088 bool aUseRowIfOverlap
) const {
1089 int32_t rowIndex
= aRowIndexIn
- aData
.GetRowSpanOffset();
1090 int32_t colIndex
= aColIndexIn
- aData
.GetColSpanOffset();
1091 if (aData
.IsOverlap()) {
1092 if (aUseRowIfOverlap
) {
1093 colIndex
= aColIndexIn
;
1095 rowIndex
= aRowIndexIn
;
1100 mRows
.SafeElementAt(rowIndex
, *sEmptyRow
).SafeElementAt(colIndex
);
1102 return data
->GetCellFrame();
1107 int32_t nsCellMap::GetHighestIndex(int32_t aColCount
) {
1109 int32_t rowCount
= mRows
.Length();
1110 for (int32_t rowIdx
= 0; rowIdx
< rowCount
; rowIdx
++) {
1111 const CellDataArray
& row
= mRows
[rowIdx
];
1113 for (int32_t colIdx
= 0; colIdx
< aColCount
; colIdx
++) {
1114 CellData
* data
= row
.SafeElementAt(colIdx
);
1115 // No data means row doesn't have more cells.
1120 if (data
->IsOrig()) {
1129 int32_t nsCellMap::GetIndexByRowAndColumn(int32_t aColCount
, int32_t aRow
,
1130 int32_t aColumn
) const {
1131 if (uint32_t(aRow
) >= mRows
.Length()) {
1136 int32_t lastColsIdx
= aColCount
- 1;
1138 // Find row index of the cell where row span is started.
1139 const CellDataArray
& row
= mRows
[aRow
];
1140 CellData
* data
= row
.SafeElementAt(aColumn
);
1141 int32_t origRow
= data
? aRow
- data
->GetRowSpanOffset() : aRow
;
1143 // Calculate cell index.
1144 for (int32_t rowIdx
= 0; rowIdx
<= origRow
; rowIdx
++) {
1145 const CellDataArray
& row
= mRows
[rowIdx
];
1146 int32_t colCount
= (rowIdx
== origRow
) ? aColumn
: lastColsIdx
;
1148 for (int32_t colIdx
= 0; colIdx
<= colCount
; colIdx
++) {
1149 data
= row
.SafeElementAt(colIdx
);
1150 // No data means row doesn't have more cells.
1155 if (data
->IsOrig()) {
1161 // Given row and column don't point to the cell.
1169 void nsCellMap::GetRowAndColumnByIndex(int32_t aColCount
, int32_t aIndex
,
1170 int32_t* aRow
, int32_t* aColumn
) const {
1174 int32_t index
= aIndex
;
1175 int32_t rowCount
= mRows
.Length();
1177 for (int32_t rowIdx
= 0; rowIdx
< rowCount
; rowIdx
++) {
1178 const CellDataArray
& row
= mRows
[rowIdx
];
1180 for (int32_t colIdx
= 0; colIdx
< aColCount
; colIdx
++) {
1181 CellData
* data
= row
.SafeElementAt(colIdx
);
1183 // The row doesn't have more cells.
1188 if (data
->IsOrig()) {
1201 bool nsCellMap::Grow(nsTableCellMap
& aMap
, int32_t aNumRows
,
1202 int32_t aRowIndex
) {
1203 NS_ASSERTION(aNumRows
>= 1, "Why are we calling this?");
1205 // Get the number of cols we want to use for preallocating the row arrays.
1206 int32_t numCols
= aMap
.GetColCount();
1210 uint32_t startRowIndex
= (aRowIndex
>= 0) ? aRowIndex
: mRows
.Length();
1211 NS_ASSERTION(startRowIndex
<= mRows
.Length(), "Missing grow call inbetween");
1213 // XXX Change the return type of this function to void, or use a fallible
1215 mRows
.InsertElementsAt(startRowIndex
, aNumRows
, numCols
);
1219 void nsCellMap::GrowRow(CellDataArray
& aRow
, int32_t aNumCols
)
1222 // Have to have the cast to get the template to do the right thing.
1223 aRow
.InsertElementsAt(aRow
.Length(), aNumCols
, (CellData
*)nullptr);
1226 void nsCellMap::InsertRows(nsTableCellMap
& aMap
,
1227 nsTArray
<nsTableRowFrame
*>& aRows
,
1228 int32_t aFirstRowIndex
, bool aConsiderSpans
,
1229 int32_t aRgFirstRowIndex
, TableArea
& aDamageArea
) {
1230 int32_t numCols
= aMap
.GetColCount();
1231 NS_ASSERTION(aFirstRowIndex
>= 0,
1232 "nsCellMap::InsertRows called with negative rowIndex");
1233 if (uint32_t(aFirstRowIndex
) > mRows
.Length()) {
1234 // create (aFirstRowIndex - mRows.Length()) empty rows up to aFirstRowIndex
1235 int32_t numEmptyRows
= aFirstRowIndex
- mRows
.Length();
1236 if (!Grow(aMap
, numEmptyRows
)) {
1241 if (!aConsiderSpans
) {
1242 // update mContentRowCount, since non-empty rows will be added
1243 mContentRowCount
= std::max(aFirstRowIndex
, mContentRowCount
);
1244 ExpandWithRows(aMap
, aRows
, aFirstRowIndex
, aRgFirstRowIndex
, aDamageArea
);
1248 // if any cells span into or out of the row being inserted, then rebuild
1249 bool spansCauseRebuild
=
1250 CellsSpanInOrOut(aFirstRowIndex
, aFirstRowIndex
, 0, numCols
- 1);
1252 // update mContentRowCount, since non-empty rows will be added
1253 mContentRowCount
= std::max(aFirstRowIndex
, mContentRowCount
);
1255 // if any of the new cells span out of the new rows being added, then rebuild
1256 // XXX it would be better to only rebuild the portion of the map that follows
1258 if (!spansCauseRebuild
&& (uint32_t(aFirstRowIndex
) < mRows
.Length())) {
1259 spansCauseRebuild
= CellsSpanOut(aRows
);
1261 if (spansCauseRebuild
) {
1262 aMap
.RebuildConsideringRows(this, aFirstRowIndex
, &aRows
, 0, aDamageArea
);
1264 ExpandWithRows(aMap
, aRows
, aFirstRowIndex
, aRgFirstRowIndex
, aDamageArea
);
1268 void nsCellMap::RemoveRows(nsTableCellMap
& aMap
, int32_t aFirstRowIndex
,
1269 int32_t aNumRowsToRemove
, bool aConsiderSpans
,
1270 int32_t aRgFirstRowIndex
, TableArea
& aDamageArea
) {
1271 int32_t numRows
= mRows
.Length();
1272 int32_t numCols
= aMap
.GetColCount();
1274 if (aFirstRowIndex
>= numRows
) {
1275 // reduce the content based row count based on the function arguments
1276 // as they are known to be real rows even if the cell map did not create
1277 // rows for them before.
1278 mContentRowCount
-= aNumRowsToRemove
;
1281 if (!aConsiderSpans
) {
1282 ShrinkWithoutRows(aMap
, aFirstRowIndex
, aNumRowsToRemove
, aRgFirstRowIndex
,
1286 int32_t endRowIndex
= aFirstRowIndex
+ aNumRowsToRemove
- 1;
1287 if (endRowIndex
>= numRows
) {
1288 NS_ERROR("nsCellMap::RemoveRows tried to remove too many rows");
1289 endRowIndex
= numRows
- 1;
1291 bool spansCauseRebuild
=
1292 CellsSpanInOrOut(aFirstRowIndex
, endRowIndex
, 0, numCols
- 1);
1293 if (spansCauseRebuild
) {
1294 aMap
.RebuildConsideringRows(this, aFirstRowIndex
, nullptr, aNumRowsToRemove
,
1297 ShrinkWithoutRows(aMap
, aFirstRowIndex
, aNumRowsToRemove
, aRgFirstRowIndex
,
1302 CellData
* nsCellMap::AppendCell(nsTableCellMap
& aMap
,
1303 nsTableCellFrame
* aCellFrame
, int32_t aRowIndex
,
1304 bool aRebuildIfNecessary
,
1305 int32_t aRgFirstRowIndex
,
1306 TableArea
& aDamageArea
,
1307 int32_t* aColToBeginSearch
) {
1308 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1309 int32_t origNumMapRows
= mRows
.Length();
1310 int32_t origNumCols
= aMap
.GetColCount();
1311 bool zeroRowSpan
= false;
1313 (aCellFrame
) ? GetRowSpanForNewCell(aCellFrame
, aRowIndex
, zeroRowSpan
)
1315 // add new rows if necessary
1316 int32_t endRowIndex
= aRowIndex
+ rowSpan
- 1;
1317 if (endRowIndex
>= origNumMapRows
) {
1318 // XXXbz handle allocation failures?
1319 Grow(aMap
, 1 + endRowIndex
- origNumMapRows
);
1322 // get the first null or dead CellData in the desired row. It will equal
1323 // origNumCols if there are none
1324 CellData
* origData
= nullptr;
1325 int32_t startColIndex
= 0;
1326 if (aColToBeginSearch
) {
1327 startColIndex
= *aColToBeginSearch
;
1329 for (; startColIndex
< origNumCols
; startColIndex
++) {
1330 CellData
* data
= GetDataAt(aRowIndex
, startColIndex
);
1334 // The border collapse code relies on having multiple dead cell data entries
1336 if (data
->IsDead() && aCellFrame
) {
1341 // We found the place to append the cell, when the next cell is appended
1342 // the next search does not need to duplicate the search but can start
1343 // just at the next cell.
1344 if (aColToBeginSearch
) {
1345 *aColToBeginSearch
= startColIndex
+ 1;
1348 int32_t colSpan
= aCellFrame
? aCellFrame
->GetColSpan() : 1;
1350 // if the new cell could potentially span into other rows and collide with
1351 // originating cells there, we will play it safe and just rebuild the map
1352 if (aRebuildIfNecessary
&& (aRowIndex
< mContentRowCount
- 1) &&
1354 AutoTArray
<nsTableCellFrame
*, 1> newCellArray
;
1355 newCellArray
.AppendElement(aCellFrame
);
1356 aMap
.RebuildConsideringCells(this, &newCellArray
, aRowIndex
, startColIndex
,
1360 mContentRowCount
= std::max(mContentRowCount
, aRowIndex
+ 1);
1362 // add new cols to the table map if necessary
1363 int32_t endColIndex
= startColIndex
+ colSpan
- 1;
1364 if (endColIndex
>= origNumCols
) {
1365 NS_ASSERTION(aCellFrame
, "dead cells should not require new columns");
1366 aMap
.AddColsAtEnd(1 + endColIndex
- origNumCols
);
1369 // Setup CellData for this cell
1371 NS_ASSERTION(origData
->IsDead(),
1372 "replacing a non dead cell is a memory leak");
1373 if (aCellFrame
) { // do nothing to replace a dead cell with a dead cell
1374 origData
->Init(aCellFrame
);
1375 // we are replacing a dead cell, increase the number of cells
1376 // originating at this column
1377 nsColInfo
* colInfo
= aMap
.GetColInfoAt(startColIndex
);
1378 NS_ASSERTION(colInfo
, "access to a non existing column");
1380 colInfo
->mNumCellsOrig
++;
1384 origData
= AllocCellData(aCellFrame
);
1385 if (!origData
) ABORT1(origData
);
1386 SetDataAt(aMap
, *origData
, aRowIndex
, startColIndex
);
1389 if (aRebuildIfNecessary
) {
1390 // the caller depends on the damageArea
1391 // The special case for zeroRowSpan is to adjust for the '2' in
1392 // GetRowSpanForNewCell.
1393 uint32_t height
= std::min(zeroRowSpan
? rowSpan
- 1 : rowSpan
,
1394 GetRowCount() - aRowIndex
);
1395 SetDamageArea(startColIndex
, aRgFirstRowIndex
+ aRowIndex
,
1396 1 + endColIndex
- startColIndex
, height
, aDamageArea
);
1403 // initialize the cell frame
1404 aCellFrame
->SetColIndex(startColIndex
);
1406 // Create CellData objects for the rows that this cell spans. Set
1407 // their mOrigCell to nullptr and their mSpanData to point to data.
1408 for (int32_t rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1409 // The row at rowX will need to have at least endColIndex columns
1410 mRows
[rowX
].SetCapacity(endColIndex
);
1411 for (int32_t colX
= startColIndex
; colX
<= endColIndex
; colX
++) {
1412 if ((rowX
!= aRowIndex
) ||
1413 (colX
!= startColIndex
)) { // skip orig cell data done above
1414 CellData
* cellData
= GetDataAt(rowX
, colX
);
1416 if (cellData
->IsOrig()) {
1417 NS_ERROR("cannot overlap originating cell");
1420 if (rowX
> aRowIndex
) { // row spanning into cell
1421 if (cellData
->IsRowSpan()) {
1422 // do nothing, this can be caused by rowspan which is overlapped
1423 // by a another cell with a rowspan and a colspan
1425 cellData
->SetRowSpanOffset(rowX
- aRowIndex
);
1427 cellData
->SetZeroRowSpan(true);
1431 if (colX
> startColIndex
) { // col spanning into cell
1432 if (!cellData
->IsColSpan()) {
1433 if (cellData
->IsRowSpan()) {
1434 cellData
->SetOverlap(true);
1436 cellData
->SetColSpanOffset(colX
- startColIndex
);
1437 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1438 colInfo
->mNumCellsSpan
++;
1442 cellData
= AllocCellData(nullptr);
1446 if (rowX
> aRowIndex
) {
1447 cellData
->SetRowSpanOffset(rowX
- aRowIndex
);
1449 cellData
->SetZeroRowSpan(true);
1452 if (colX
> startColIndex
) {
1453 cellData
->SetColSpanOffset(colX
- startColIndex
);
1455 SetDataAt(aMap
, *cellData
, rowX
, colX
);
1460 #ifdef DEBUG_TABLE_CELLMAP
1461 printf("appended cell=%p row=%d \n", aCellFrame
, aRowIndex
);
1467 bool nsCellMap::CellsSpanOut(nsTArray
<nsTableRowFrame
*>& aRows
) const {
1468 int32_t numNewRows
= aRows
.Length();
1469 for (int32_t rowX
= 0; rowX
< numNewRows
; rowX
++) {
1470 nsTableRowFrame
* rowFrame
= aRows
.ElementAt(rowX
);
1471 for (nsTableCellFrame
* cellFrame
= rowFrame
->GetFirstCell(); cellFrame
;
1472 cellFrame
= cellFrame
->GetNextCell()) {
1474 int32_t rowSpan
= GetRowSpanForNewCell(cellFrame
, rowX
, zeroSpan
);
1475 if (zeroSpan
|| rowX
+ rowSpan
> numNewRows
) {
1483 // return true if any cells have rows spans into or out of the region
1484 // defined by the row and col indices or any cells have colspans into the region
1485 bool nsCellMap::CellsSpanInOrOut(int32_t aStartRowIndex
, int32_t aEndRowIndex
,
1486 int32_t aStartColIndex
,
1487 int32_t aEndColIndex
) const {
1489 * this routine will watch the cells adjacent to the region or at the edge
1490 * they are marked with *. The routine will verify whether they span in or
1494 * r1c1 r1c2 r1c3 r1c4 r1c5 r1rc6 r1c7
1495 * startrow r2c1 r2c2 *r2c3 *r2c4 *r2c5 *r2rc6 r2c7
1496 * endrow r3c1 r3c2 *r3c3 r3c4 r3c5 *r3rc6 r3c7
1497 * r4c1 r4c2 *r4c3 *r4c4 *r4c5 r4rc6 r4c7
1498 * r5c1 r5c2 r5c3 r5c4 r5c5 r5rc6 r5c7
1501 int32_t numRows
= mRows
.Length(); // use the cellmap rows to determine the
1502 // current cellmap extent.
1503 for (int32_t colX
= aStartColIndex
; colX
<= aEndColIndex
; colX
++) {
1505 if (aStartRowIndex
> 0) {
1506 cellData
= GetDataAt(aStartRowIndex
, colX
);
1507 if (cellData
&& (cellData
->IsRowSpan())) {
1508 return true; // there is a row span into the region
1510 if ((aStartRowIndex
>= mContentRowCount
) && (mContentRowCount
> 0)) {
1511 cellData
= GetDataAt(mContentRowCount
- 1, colX
);
1512 if (cellData
&& cellData
->IsZeroRowSpan()) {
1513 return true; // When we expand the zerospan it'll span into our row
1517 if (aEndRowIndex
< numRows
- 1) { // is there anything below aEndRowIndex
1518 cellData
= GetDataAt(aEndRowIndex
+ 1, colX
);
1519 if ((cellData
) && (cellData
->IsRowSpan())) {
1520 return true; // there is a row span out of the region
1523 cellData
= GetDataAt(aEndRowIndex
, colX
);
1524 if ((cellData
) && (cellData
->IsRowSpan()) &&
1525 (mContentRowCount
< numRows
)) {
1526 return true; // this cell might be the cause of a dead row
1530 if (aStartColIndex
> 0) {
1531 for (int32_t rowX
= aStartRowIndex
; rowX
<= aEndRowIndex
; rowX
++) {
1532 CellData
* cellData
= GetDataAt(rowX
, aStartColIndex
);
1533 if (cellData
&& (cellData
->IsColSpan())) {
1534 return true; // there is a col span into the region
1536 cellData
= GetDataAt(rowX
, aEndColIndex
+ 1);
1537 if (cellData
&& (cellData
->IsColSpan())) {
1538 return true; // there is a col span out of the region
1545 void nsCellMap::InsertCells(nsTableCellMap
& aMap
,
1546 nsTArray
<nsTableCellFrame
*>& aCellFrames
,
1547 int32_t aRowIndex
, int32_t aColIndexBefore
,
1548 int32_t aRgFirstRowIndex
, TableArea
& aDamageArea
) {
1549 if (aCellFrames
.Length() == 0) {
1552 NS_ASSERTION(aColIndexBefore
>= -1, "index out of range");
1553 int32_t numCols
= aMap
.GetColCount();
1554 if (aColIndexBefore
>= numCols
) {
1556 "Inserting instead of appending cells indicates a serious cellmap "
1558 aColIndexBefore
= numCols
- 1;
1561 // get the starting col index of the 1st new cells
1562 int32_t startColIndex
;
1563 for (startColIndex
= aColIndexBefore
+ 1; startColIndex
< numCols
;
1565 CellData
* data
= GetDataAt(aRowIndex
, startColIndex
);
1566 if (!data
|| data
->IsOrig() || data
->IsDead()) {
1567 // // Not a span. Stop.
1572 // record whether inserted cells are going to cause complications due
1573 // to existing row spans, col spans or table sizing.
1574 bool spansCauseRebuild
= false;
1576 // check that all cells have the same row span
1577 int32_t numNewCells
= aCellFrames
.Length();
1578 bool zeroRowSpan
= false;
1579 int32_t rowSpan
= 0;
1580 for (int32_t cellX
= 0; cellX
< numNewCells
; cellX
++) {
1581 nsTableCellFrame
* cell
= aCellFrames
.ElementAt(cellX
);
1582 int32_t rowSpan2
= GetRowSpanForNewCell(cell
, aRowIndex
, zeroRowSpan
);
1585 } else if (rowSpan
!= rowSpan2
) {
1586 spansCauseRebuild
= true;
1591 // check if the new cells will cause the table to add more rows
1592 if (!spansCauseRebuild
) {
1593 if (mRows
.Length() < uint32_t(aRowIndex
+ rowSpan
)) {
1594 spansCauseRebuild
= true;
1598 if (!spansCauseRebuild
) {
1599 spansCauseRebuild
= CellsSpanInOrOut(aRowIndex
, aRowIndex
+ rowSpan
- 1,
1600 startColIndex
, numCols
- 1);
1602 if (spansCauseRebuild
) {
1603 aMap
.RebuildConsideringCells(this, &aCellFrames
, aRowIndex
, startColIndex
,
1606 ExpandWithCells(aMap
, aCellFrames
, aRowIndex
, startColIndex
, rowSpan
,
1607 zeroRowSpan
, aRgFirstRowIndex
, aDamageArea
);
1611 void nsCellMap::ExpandWithRows(nsTableCellMap
& aMap
,
1612 nsTArray
<nsTableRowFrame
*>& aRowFrames
,
1613 int32_t aStartRowIndexIn
,
1614 int32_t aRgFirstRowIndex
,
1615 TableArea
& aDamageArea
) {
1616 int32_t startRowIndex
= (aStartRowIndexIn
>= 0) ? aStartRowIndexIn
: 0;
1617 NS_ASSERTION(uint32_t(startRowIndex
) <= mRows
.Length(),
1618 "caller should have grown cellmap before");
1620 int32_t numNewRows
= aRowFrames
.Length();
1621 mContentRowCount
+= numNewRows
;
1623 int32_t endRowIndex
= startRowIndex
+ numNewRows
- 1;
1625 // shift the rows after startRowIndex down and insert empty rows that will
1626 // be filled via the AppendCell call below
1627 if (!Grow(aMap
, numNewRows
, startRowIndex
)) {
1631 int32_t newRowIndex
= 0;
1632 for (int32_t rowX
= startRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1633 nsTableRowFrame
* rFrame
= aRowFrames
.ElementAt(newRowIndex
);
1635 int32_t colIndex
= 0;
1636 for (nsTableCellFrame
* cellFrame
= rFrame
->GetFirstCell(); cellFrame
;
1637 cellFrame
= cellFrame
->GetNextCell()) {
1638 AppendCell(aMap
, cellFrame
, rowX
, false, aRgFirstRowIndex
, aDamageArea
,
1643 // mark all following rows damaged, they might contain a previously set
1644 // damage area which we can not shift.
1645 int32_t firstDamagedRow
= aRgFirstRowIndex
+ startRowIndex
;
1646 SetDamageArea(0, firstDamagedRow
, aMap
.GetColCount(),
1647 aMap
.GetRowCount() - firstDamagedRow
, aDamageArea
);
1650 void nsCellMap::ExpandWithCells(nsTableCellMap
& aMap
,
1651 nsTArray
<nsTableCellFrame
*>& aCellFrames
,
1652 int32_t aRowIndex
, int32_t aColIndex
,
1653 int32_t aRowSpan
, // same for all cells
1654 bool aRowSpanIsZero
, int32_t aRgFirstRowIndex
,
1655 TableArea
& aDamageArea
) {
1656 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1657 int32_t endRowIndex
= aRowIndex
+ aRowSpan
- 1;
1658 int32_t startColIndex
= aColIndex
;
1659 int32_t endColIndex
= aColIndex
;
1660 int32_t numCells
= aCellFrames
.Length();
1661 int32_t totalColSpan
= 0;
1663 // add cellData entries for the space taken up by the new cells
1664 for (int32_t cellX
= 0; cellX
< numCells
; cellX
++) {
1665 nsTableCellFrame
* cellFrame
= aCellFrames
.ElementAt(cellX
);
1666 CellData
* origData
= AllocCellData(cellFrame
); // the originating cell
1671 // set the starting and ending col index for the new cell
1672 int32_t colSpan
= cellFrame
->GetColSpan();
1673 totalColSpan
+= colSpan
;
1675 endColIndex
= aColIndex
+ colSpan
- 1;
1677 startColIndex
= endColIndex
+ 1;
1678 endColIndex
= startColIndex
+ colSpan
- 1;
1681 // add the originating cell data and any cell data corresponding to row/col
1683 for (int32_t rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1684 CellDataArray
& row
= mRows
[rowX
];
1685 // Pre-allocate all the cells we'll need in this array, setting
1687 // Have to have the cast to get the template to do the right thing.
1688 int32_t insertionIndex
= row
.Length();
1689 if (insertionIndex
> startColIndex
) {
1690 insertionIndex
= startColIndex
;
1692 row
.InsertElementsAt(insertionIndex
, endColIndex
- insertionIndex
+ 1,
1693 (CellData
*)nullptr);
1695 for (int32_t colX
= startColIndex
; colX
<= endColIndex
; colX
++) {
1696 CellData
* data
= origData
;
1697 if ((rowX
!= aRowIndex
) || (colX
!= startColIndex
)) {
1698 data
= AllocCellData(nullptr);
1702 if (rowX
> aRowIndex
) {
1703 data
->SetRowSpanOffset(rowX
- aRowIndex
);
1704 if (aRowSpanIsZero
) {
1705 data
->SetZeroRowSpan(true);
1708 if (colX
> startColIndex
) {
1709 data
->SetColSpanOffset(colX
- startColIndex
);
1712 SetDataAt(aMap
, *data
, rowX
, colX
);
1715 cellFrame
->SetColIndex(startColIndex
);
1717 int32_t damageHeight
=
1718 std::min(GetRowGroup()->GetRowCount() - aRowIndex
, aRowSpan
);
1719 SetDamageArea(aColIndex
, aRgFirstRowIndex
+ aRowIndex
,
1720 1 + endColIndex
- aColIndex
, damageHeight
, aDamageArea
);
1724 // update the row and col info due to shifting
1725 for (rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1726 CellDataArray
& row
= mRows
[rowX
];
1727 uint32_t numCols
= row
.Length();
1729 for (colX
= aColIndex
+ totalColSpan
; colX
< numCols
; colX
++) {
1730 CellData
* data
= row
[colX
];
1732 // increase the origin and span counts beyond the spanned cols
1733 if (data
->IsOrig()) {
1734 // a cell that gets moved needs adjustment as well as it new
1736 data
->GetCellFrame()->SetColIndex(colX
);
1737 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1738 colInfo
->mNumCellsOrig
++;
1740 if (data
->IsColSpan()) {
1741 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1742 colInfo
->mNumCellsSpan
++;
1745 // decrease the origin and span counts within the spanned cols
1746 int32_t colX2
= colX
- totalColSpan
;
1747 nsColInfo
* colInfo2
= aMap
.GetColInfoAt(colX2
);
1748 if (data
->IsOrig()) {
1749 // the old originating col of a moved cell needs adjustment
1750 colInfo2
->mNumCellsOrig
--;
1752 if (data
->IsColSpan()) {
1753 colInfo2
->mNumCellsSpan
--;
1760 void nsCellMap::ShrinkWithoutRows(nsTableCellMap
& aMap
, int32_t aStartRowIndex
,
1761 int32_t aNumRowsToRemove
,
1762 int32_t aRgFirstRowIndex
,
1763 TableArea
& aDamageArea
) {
1764 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1765 int32_t endRowIndex
= aStartRowIndex
+ aNumRowsToRemove
- 1;
1766 uint32_t colCount
= aMap
.GetColCount();
1767 for (int32_t rowX
= endRowIndex
; rowX
>= aStartRowIndex
; --rowX
) {
1768 CellDataArray
& row
= mRows
[rowX
];
1770 for (colX
= 0; colX
< colCount
; colX
++) {
1771 CellData
* data
= row
.SafeElementAt(colX
);
1773 // Adjust the column counts.
1774 if (data
->IsOrig()) {
1775 // Decrement the column count.
1776 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1777 colInfo
->mNumCellsOrig
--;
1779 // colspan=0 is only counted as a spanned cell in the 1st col it spans
1780 else if (data
->IsColSpan()) {
1781 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1782 colInfo
->mNumCellsSpan
--;
1787 uint32_t rowLength
= row
.Length();
1788 // Delete our row information.
1789 for (colX
= 0; colX
< rowLength
; colX
++) {
1790 DestroyCellData(row
[colX
]);
1793 mRows
.RemoveElementAt(rowX
);
1795 // Decrement our row and next available index counts.
1798 aMap
.RemoveColsAtEnd();
1799 // mark all following rows damaged, they might contain a previously set
1800 // damage area which we can not shift.
1801 int32_t firstDamagedRow
= aRgFirstRowIndex
+ aStartRowIndex
;
1802 SetDamageArea(0, firstDamagedRow
, aMap
.GetColCount(),
1803 aMap
.GetRowCount() - firstDamagedRow
, aDamageArea
);
1806 int32_t nsCellMap::GetEffectiveColSpan(const nsTableCellMap
& aMap
,
1808 int32_t aColIndex
) const {
1809 int32_t numColsInTable
= aMap
.GetColCount();
1810 int32_t colSpan
= 1;
1811 if (uint32_t(aRowIndex
) >= mRows
.Length()) {
1815 const CellDataArray
& row
= mRows
[aRowIndex
];
1818 int32_t maxCols
= numColsInTable
;
1819 bool hitOverlap
= false; // XXX this is not ever being set to true
1820 for (colX
= aColIndex
+ 1; colX
< maxCols
; colX
++) {
1821 data
= row
.SafeElementAt(colX
);
1823 // for an overlapping situation get the colspan from the originating cell
1824 // and use that as the max number of cols to iterate. Since this is rare,
1825 // only pay the price of looking up the cell's colspan here.
1826 if (!hitOverlap
&& data
->IsOverlap()) {
1827 CellData
* origData
= row
.SafeElementAt(aColIndex
);
1828 if (origData
&& origData
->IsOrig()) {
1829 nsTableCellFrame
* cellFrame
= origData
->GetCellFrame();
1831 // possible change the number of colums to iterate
1832 maxCols
= std::min(aColIndex
+ cellFrame
->GetColSpan(), maxCols
);
1833 if (colX
>= maxCols
) {
1839 if (data
->IsColSpan()) {
1851 int32_t nsCellMap::GetRowSpanForNewCell(nsTableCellFrame
* aCellFrameToAdd
,
1853 bool& aIsZeroRowSpan
) const {
1854 aIsZeroRowSpan
= false;
1855 int32_t rowSpan
= aCellFrameToAdd
->GetRowSpan();
1857 // Use a min value of 2 for a zero rowspan to make computations easier
1858 // elsewhere. Zero rowspans are only content dependent!
1859 rowSpan
= std::max(2, mContentRowCount
- aRowIndex
);
1860 aIsZeroRowSpan
= true;
1865 bool nsCellMap::HasMoreThanOneCell(int32_t aRowIndex
) const {
1866 const CellDataArray
& row
= mRows
.SafeElementAt(aRowIndex
, *sEmptyRow
);
1867 uint32_t maxColIndex
= row
.Length();
1869 bool foundOne
= false;
1870 for (colIndex
= 0; colIndex
< maxColIndex
; colIndex
++) {
1871 CellData
* cellData
= row
[colIndex
];
1872 if (cellData
&& (cellData
->GetCellFrame() || cellData
->IsRowSpan())) {
1882 int32_t nsCellMap::GetNumCellsOriginatingInRow(int32_t aRowIndex
) const {
1883 const CellDataArray
& row
= mRows
.SafeElementAt(aRowIndex
, *sEmptyRow
);
1885 uint32_t maxColIndex
= row
.Length();
1887 for (colIndex
= 0; colIndex
< maxColIndex
; colIndex
++) {
1888 CellData
* cellData
= row
[colIndex
];
1889 if (cellData
&& cellData
->IsOrig()) {
1896 int32_t nsCellMap::GetRowSpan(int32_t aRowIndex
, int32_t aColIndex
,
1897 bool aGetEffective
) const {
1898 int32_t rowSpan
= 1;
1899 int32_t rowCount
= (aGetEffective
) ? mContentRowCount
: mRows
.Length();
1901 for (rowX
= aRowIndex
+ 1; rowX
< rowCount
; rowX
++) {
1902 CellData
* data
= GetDataAt(rowX
, aColIndex
);
1904 if (data
->IsRowSpan()) {
1916 void nsCellMap::ShrinkWithoutCell(nsTableCellMap
& aMap
,
1917 nsTableCellFrame
& aCellFrame
,
1918 int32_t aRowIndex
, int32_t aColIndex
,
1919 int32_t aRgFirstRowIndex
,
1920 TableArea
& aDamageArea
) {
1921 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1922 uint32_t colX
, rowX
;
1924 // get the rowspan and colspan from the cell map since the content may have
1926 int32_t rowSpan
= GetRowSpan(aRowIndex
, aColIndex
, true);
1927 uint32_t colSpan
= GetEffectiveColSpan(aMap
, aRowIndex
, aColIndex
);
1928 uint32_t endRowIndex
= aRowIndex
+ rowSpan
- 1;
1929 uint32_t endColIndex
= aColIndex
+ colSpan
- 1;
1931 // adjust the col counts due to the deleted cell before removing it
1932 for (colX
= aColIndex
; colX
<= endColIndex
; colX
++) {
1933 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1934 if (colX
== uint32_t(aColIndex
)) {
1935 colInfo
->mNumCellsOrig
--;
1937 colInfo
->mNumCellsSpan
--;
1941 // remove the deleted cell and cellData entries for it
1942 for (rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1943 CellDataArray
& row
= mRows
[rowX
];
1945 // endIndexForRow points at the first slot we don't want to clean up. This
1946 // makes the aColIndex == 0 case work right with our unsigned int colX.
1947 NS_ASSERTION(endColIndex
+ 1 <= row
.Length(), "span beyond the row size!");
1948 uint32_t endIndexForRow
= std::min(endColIndex
+ 1, uint32_t(row
.Length()));
1950 // Since endIndexForRow <= row.Length(), enough to compare aColIndex to it.
1951 if (uint32_t(aColIndex
) < endIndexForRow
) {
1952 for (colX
= endIndexForRow
; colX
> uint32_t(aColIndex
); colX
--) {
1953 DestroyCellData(row
[colX
- 1]);
1955 row
.RemoveElementsAt(aColIndex
, endIndexForRow
- aColIndex
);
1959 uint32_t numCols
= aMap
.GetColCount();
1961 // update the row and col info due to shifting
1962 for (rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1963 CellDataArray
& row
= mRows
[rowX
];
1964 for (colX
= aColIndex
; colX
< numCols
- colSpan
; colX
++) {
1965 CellData
* data
= row
.SafeElementAt(colX
);
1967 if (data
->IsOrig()) {
1968 // a cell that gets moved to the left needs adjustment in its new
1970 data
->GetCellFrame()->SetColIndex(colX
);
1971 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1972 colInfo
->mNumCellsOrig
++;
1973 // a cell that gets moved to the left needs adjustment in its old
1975 colInfo
= aMap
.GetColInfoAt(colX
+ colSpan
);
1977 colInfo
->mNumCellsOrig
--;
1981 else if (data
->IsColSpan()) {
1982 // a cell that gets moved to the left needs adjustment
1983 // in its new location
1984 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1985 colInfo
->mNumCellsSpan
++;
1986 // a cell that gets moved to the left needs adjustment
1987 // in its old location
1988 colInfo
= aMap
.GetColInfoAt(colX
+ colSpan
);
1990 colInfo
->mNumCellsSpan
--;
1996 aMap
.RemoveColsAtEnd();
1997 SetDamageArea(aColIndex
, aRgFirstRowIndex
+ aRowIndex
,
1998 std::max(0, aMap
.GetColCount() - aColIndex
- 1),
1999 1 + endRowIndex
- aRowIndex
, aDamageArea
);
2002 void nsCellMap::RebuildConsideringRows(
2003 nsTableCellMap
& aMap
, int32_t aStartRowIndex
,
2004 nsTArray
<nsTableRowFrame
*>* aRowsToInsert
, int32_t aNumRowsToRemove
) {
2005 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
2006 // copy the old cell map into a new array
2007 uint32_t numOrigRows
= mRows
.Length();
2008 nsTArray
<CellDataArray
> origRows
= std::move(mRows
);
2010 int32_t rowNumberChange
;
2011 if (aRowsToInsert
) {
2012 rowNumberChange
= aRowsToInsert
->Length();
2014 rowNumberChange
= -aNumRowsToRemove
;
2017 // adjust mContentRowCount based on the function arguments as they are known
2019 mContentRowCount
+= rowNumberChange
;
2020 NS_ASSERTION(mContentRowCount
>= 0, "previous mContentRowCount was wrong");
2021 // mRows is empty now. Grow it to the size we expect it to have.
2022 if (mContentRowCount
) {
2023 if (!Grow(aMap
, mContentRowCount
)) {
2024 // Bail, I guess... Not sure what else we can do here.
2029 // aStartRowIndex might be after all existing rows so we should limit the
2030 // copy to the amount of exisiting rows
2031 uint32_t copyEndRowIndex
= std::min(numOrigRows
, uint32_t(aStartRowIndex
));
2033 // rowX keeps track of where we are in mRows while setting up the
2036 TableArea damageArea
;
2037 // put back the rows before the affected ones just as before. Note that we
2038 // can't just copy the old rows in bit-for-bit, because they might be
2039 // spanning out into the rows we're adding/removing.
2040 for (; rowX
< copyEndRowIndex
; rowX
++) {
2041 const CellDataArray
& row
= origRows
[rowX
];
2042 uint32_t numCols
= row
.Length();
2043 for (uint32_t colX
= 0; colX
< numCols
; colX
++) {
2044 // put in the original cell from the cell map
2045 const CellData
* data
= row
.ElementAt(colX
);
2046 if (data
&& data
->IsOrig()) {
2047 AppendCell(aMap
, data
->GetCellFrame(), rowX
, false, 0, damageArea
);
2052 // Now handle the new rows being inserted, if any.
2053 uint32_t copyStartRowIndex
;
2054 rowX
= aStartRowIndex
;
2055 if (aRowsToInsert
) {
2056 // add in the new cells and create rows if necessary
2057 int32_t numNewRows
= aRowsToInsert
->Length();
2058 for (int32_t newRowX
= 0; newRowX
< numNewRows
; newRowX
++) {
2059 nsTableRowFrame
* rFrame
= aRowsToInsert
->ElementAt(newRowX
);
2060 for (nsTableCellFrame
* cellFrame
= rFrame
->GetFirstCell(); cellFrame
;
2061 cellFrame
= cellFrame
->GetNextCell()) {
2062 AppendCell(aMap
, cellFrame
, rowX
, false, 0, damageArea
);
2066 copyStartRowIndex
= aStartRowIndex
;
2068 copyStartRowIndex
= aStartRowIndex
+ aNumRowsToRemove
;
2071 // put back the rows after the affected ones just as before. Again, we can't
2072 // just copy the old bits because that would not handle the new rows spanning
2073 // out or our earlier old rows spanning through the damaged area.
2074 for (uint32_t copyRowX
= copyStartRowIndex
; copyRowX
< numOrigRows
;
2076 const CellDataArray
& row
= origRows
[copyRowX
];
2077 uint32_t numCols
= row
.Length();
2078 for (uint32_t colX
= 0; colX
< numCols
; colX
++) {
2079 // put in the original cell from the cell map
2080 CellData
* data
= row
.ElementAt(colX
);
2081 if (data
&& data
->IsOrig()) {
2082 AppendCell(aMap
, data
->GetCellFrame(), rowX
, false, 0, damageArea
);
2088 // delete the old cell map. Now rowX no longer has anything to do with mRows
2089 for (rowX
= 0; rowX
< numOrigRows
; rowX
++) {
2090 CellDataArray
& row
= origRows
[rowX
];
2091 uint32_t len
= row
.Length();
2092 for (uint32_t colX
= 0; colX
< len
; colX
++) {
2093 DestroyCellData(row
[colX
]);
2098 void nsCellMap::RebuildConsideringCells(
2099 nsTableCellMap
& aMap
, int32_t aNumOrigCols
,
2100 nsTArray
<nsTableCellFrame
*>* aCellFrames
, int32_t aRowIndex
,
2101 int32_t aColIndex
, bool aInsert
) {
2102 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
2103 // copy the old cell map into a new array
2104 int32_t numOrigRows
= mRows
.Length();
2105 nsTArray
<CellDataArray
> origRows
= std::move(mRows
);
2107 int32_t numNewCells
= (aCellFrames
) ? aCellFrames
->Length() : 0;
2109 // the new cells might extend the previous column number
2110 NS_ASSERTION(aNumOrigCols
>= aColIndex
,
2111 "Appending cells far beyond cellmap data?!");
2113 aInsert
? std::max(aNumOrigCols
, aColIndex
+ 1) : aNumOrigCols
;
2115 // build the new cell map. Hard to say what, if anything, we can preallocate
2116 // here... Should come back to that sometime, perhaps.
2118 TableArea damageArea
;
2119 for (rowX
= 0; rowX
< numOrigRows
; rowX
++) {
2120 const CellDataArray
& row
= origRows
[rowX
];
2121 for (int32_t colX
= 0; colX
< numCols
; colX
++) {
2122 if ((rowX
== aRowIndex
) && (colX
== aColIndex
)) {
2123 if (aInsert
) { // put in the new cells
2124 for (int32_t cellX
= 0; cellX
< numNewCells
; cellX
++) {
2125 nsTableCellFrame
* cell
= aCellFrames
->ElementAt(cellX
);
2127 AppendCell(aMap
, cell
, rowX
, false, 0, damageArea
);
2131 continue; // do not put the deleted cell back
2134 // put in the original cell from the cell map
2135 CellData
* data
= row
.SafeElementAt(colX
);
2136 if (data
&& data
->IsOrig()) {
2137 AppendCell(aMap
, data
->GetCellFrame(), rowX
, false, 0, damageArea
);
2143 aRowIndex
) { // append the new cells below the last original row
2144 NS_ASSERTION(numOrigRows
== aRowIndex
,
2145 "Appending cells far beyond the last row");
2146 for (int32_t cellX
= 0; cellX
< numNewCells
; cellX
++) {
2147 nsTableCellFrame
* cell
= aCellFrames
->ElementAt(cellX
);
2149 AppendCell(aMap
, cell
, aRowIndex
, false, 0, damageArea
);
2154 // delete the old cell map
2155 for (rowX
= 0; rowX
< numOrigRows
; rowX
++) {
2156 CellDataArray
& row
= origRows
[rowX
];
2157 uint32_t len
= row
.Length();
2158 for (uint32_t colX
= 0; colX
< len
; colX
++) {
2159 DestroyCellData(row
.SafeElementAt(colX
));
2162 // expand the cellmap to cover empty content rows
2163 if (mRows
.Length() < uint32_t(mContentRowCount
)) {
2164 Grow(aMap
, mContentRowCount
- mRows
.Length());
2168 void nsCellMap::RemoveCell(nsTableCellMap
& aMap
, nsTableCellFrame
* aCellFrame
,
2169 int32_t aRowIndex
, int32_t aRgFirstRowIndex
,
2170 TableArea
& aDamageArea
) {
2171 uint32_t numRows
= mRows
.Length();
2172 if (uint32_t(aRowIndex
) >= numRows
) {
2173 NS_ERROR("bad arg in nsCellMap::RemoveCell");
2176 int32_t numCols
= aMap
.GetColCount();
2178 // Now aRowIndex is guaranteed OK.
2180 // get the starting col index of the cell to remove
2181 int32_t startColIndex
;
2182 for (startColIndex
= 0; startColIndex
< numCols
; startColIndex
++) {
2183 CellData
* data
= mRows
[aRowIndex
].SafeElementAt(startColIndex
);
2184 if (data
&& (data
->IsOrig()) && (aCellFrame
== data
->GetCellFrame())) {
2185 break; // we found the col index
2189 int32_t rowSpan
= GetRowSpan(aRowIndex
, startColIndex
, false);
2190 // record whether removing the cells is going to cause complications due
2191 // to existing row spans, col spans or table sizing.
2192 bool spansCauseRebuild
= CellsSpanInOrOut(aRowIndex
, aRowIndex
+ rowSpan
- 1,
2193 startColIndex
, numCols
- 1);
2194 // XXX if the cell has a col span to the end of the map, and the end has no
2195 // originating cells, we need to assume that this the only such cell, and
2196 // rebuild so that there are no extraneous cols at the end. The same is true
2197 // for removing rows.
2198 if (!spansCauseRebuild
) {
2199 if (!aCellFrame
->GetRowSpan() || !aCellFrame
->GetColSpan()) {
2200 spansCauseRebuild
= true;
2204 if (spansCauseRebuild
) {
2205 aMap
.RebuildConsideringCells(this, nullptr, aRowIndex
, startColIndex
, false,
2208 ShrinkWithoutCell(aMap
, *aCellFrame
, aRowIndex
, startColIndex
,
2209 aRgFirstRowIndex
, aDamageArea
);
2214 void nsCellMap::Dump(bool aIsBorderCollapse
) const {
2215 printf("\n ***** START GROUP CELL MAP DUMP ***** %p\n", (void*)this);
2216 nsTableRowGroupFrame
* rg
= GetRowGroup();
2217 const nsStyleDisplay
* display
= rg
->StyleDisplay();
2218 switch (display
->DisplayInside()) {
2219 case StyleDisplayInside::TableHeaderGroup
:
2222 case StyleDisplayInside::TableFooterGroup
:
2225 case StyleDisplayInside::TableRowGroup
:
2229 printf("HUH? wrong display type on rowgroup");
2231 uint32_t mapRowCount
= mRows
.Length();
2232 printf("mapRowCount=%u tableRowCount=%d\n", mapRowCount
, mContentRowCount
);
2234 uint32_t rowIndex
, colIndex
;
2235 for (rowIndex
= 0; rowIndex
< mapRowCount
; rowIndex
++) {
2236 const CellDataArray
& row
= mRows
[rowIndex
];
2237 printf(" row %d : ", rowIndex
);
2238 uint32_t colCount
= row
.Length();
2239 for (colIndex
= 0; colIndex
< colCount
; colIndex
++) {
2240 CellData
* cd
= row
[colIndex
];
2243 printf("C%d,%d ", rowIndex
, colIndex
);
2245 if (cd
->IsRowSpan()) {
2248 if (cd
->IsColSpan()) {
2251 if (!(cd
->IsRowSpan() && cd
->IsColSpan())) {
2260 if (aIsBorderCollapse
) {
2262 BCBorderOwner owner
;
2266 for (int32_t i
= 0; i
<= 2; i
++) {
2268 for (colIndex
= 0; colIndex
< colCount
; colIndex
++) {
2269 BCCellData
* cd
= (BCCellData
*)row
[colIndex
];
2272 size
= cd
->mData
.GetBStartEdge(owner
, segStart
);
2273 printf("t=%d%d%d ", int32_t(size
), owner
, segStart
);
2274 } else if (1 == i
) {
2275 size
= cd
->mData
.GetIStartEdge(owner
, segStart
);
2276 printf("l=%d%d%d ", int32_t(size
), owner
, segStart
);
2278 size
= cd
->mData
.GetCorner(side
, bevel
);
2279 printf("c=%d%hhu%d ", int32_t(size
), static_cast<uint8_t>(side
),
2289 // output info mapping Ci,j to cell address
2290 for (uint32_t rIndex
= 0; rIndex
< mapRowCount
; rIndex
++) {
2291 const CellDataArray
& row
= mRows
[rIndex
];
2292 uint32_t colCount
= row
.Length();
2294 for (colIndex
= 0; colIndex
< colCount
; colIndex
++) {
2295 CellData
* cd
= row
[colIndex
];
2298 nsTableCellFrame
* cellFrame
= cd
->GetCellFrame();
2299 uint32_t cellFrameColIndex
= cellFrame
->ColIndex();
2300 printf("C%d,%d=%p(%u) ", rIndex
, colIndex
, (void*)cellFrame
,
2308 printf(" ***** END GROUP CELL MAP DUMP *****\n");
2312 CellData
* nsCellMap::GetDataAt(int32_t aMapRowIndex
, int32_t aColIndex
) const {
2313 return mRows
.SafeElementAt(aMapRowIndex
, *sEmptyRow
).SafeElementAt(aColIndex
);
2316 // only called if the cell at aMapRowIndex, aColIndex is null or dead
2317 // (the latter from ExpandZeroColSpans (XXXmats which has now been removed -
2318 // are there other ways cells may be dead?)).
2319 void nsCellMap::SetDataAt(nsTableCellMap
& aMap
, CellData
& aNewCell
,
2320 int32_t aMapRowIndex
, int32_t aColIndex
) {
2321 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
2322 if (uint32_t(aMapRowIndex
) >= mRows
.Length()) {
2323 NS_ERROR("SetDataAt called with row index > num rows");
2327 CellDataArray
& row
= mRows
[aMapRowIndex
];
2329 // the table map may need cols added
2330 int32_t numColsToAdd
= aColIndex
+ 1 - aMap
.GetColCount();
2331 if (numColsToAdd
> 0) {
2332 aMap
.AddColsAtEnd(numColsToAdd
);
2334 // the row may need cols added
2335 numColsToAdd
= aColIndex
+ 1 - row
.Length();
2336 if (numColsToAdd
> 0) {
2337 // XXXbz need to handle allocation failures.
2338 GrowRow(row
, numColsToAdd
);
2341 DestroyCellData(row
[aColIndex
]);
2343 row
.ReplaceElementsAt(aColIndex
, 1, &aNewCell
);
2344 // update the originating cell counts if cell originates in this row, col
2345 nsColInfo
* colInfo
= aMap
.GetColInfoAt(aColIndex
);
2347 if (aNewCell
.IsOrig()) {
2348 colInfo
->mNumCellsOrig
++;
2349 } else if (aNewCell
.IsColSpan()) {
2350 colInfo
->mNumCellsSpan
++;
2353 NS_ERROR("SetDataAt called with col index > table map num cols");
2357 nsTableCellFrame
* nsCellMap::GetCellInfoAt(const nsTableCellMap
& aMap
,
2358 int32_t aRowX
, int32_t aColX
,
2360 int32_t* aColSpan
) const {
2362 *aOriginates
= false;
2364 CellData
* data
= GetDataAt(aRowX
, aColX
);
2365 nsTableCellFrame
* cellFrame
= nullptr;
2367 if (data
->IsOrig()) {
2368 cellFrame
= data
->GetCellFrame();
2370 *aOriginates
= true;
2373 cellFrame
= GetCellFrame(aRowX
, aColX
, *data
, true);
2375 if (cellFrame
&& aColSpan
) {
2376 uint32_t initialColIndex
= cellFrame
->ColIndex();
2377 *aColSpan
= GetEffectiveColSpan(aMap
, aRowX
, initialColIndex
);
2383 bool nsCellMap::RowIsSpannedInto(int32_t aRowIndex
, int32_t aNumEffCols
) const {
2384 if ((0 > aRowIndex
) || (aRowIndex
>= mContentRowCount
)) {
2387 for (int32_t colIndex
= 0; colIndex
< aNumEffCols
; colIndex
++) {
2388 CellData
* cd
= GetDataAt(aRowIndex
, colIndex
);
2389 if (cd
) { // there's really a cell at (aRowIndex, colIndex)
2390 if (cd
->IsSpan()) { // the cell at (aRowIndex, colIndex) is the result of
2392 if (cd
->IsRowSpan() && GetCellFrame(aRowIndex
, colIndex
, *cd
,
2393 true)) { // XXX why the last check
2402 bool nsCellMap::RowHasSpanningCells(int32_t aRowIndex
,
2403 int32_t aNumEffCols
) const {
2404 if ((0 > aRowIndex
) || (aRowIndex
>= mContentRowCount
)) {
2407 if (aRowIndex
!= mContentRowCount
- 1) {
2408 // aRowIndex is not the last row, so we check the next row after aRowIndex
2410 for (int32_t colIndex
= 0; colIndex
< aNumEffCols
; colIndex
++) {
2411 CellData
* cd
= GetDataAt(aRowIndex
, colIndex
);
2412 if (cd
&& (cd
->IsOrig())) { // cell originates
2413 CellData
* cd2
= GetDataAt(aRowIndex
+ 1, colIndex
);
2414 if (cd2
&& cd2
->IsRowSpan()) { // cd2 is spanned by a row
2415 if (cd
->GetCellFrame() ==
2416 GetCellFrame(aRowIndex
+ 1, colIndex
, *cd2
, true)) {
2426 void nsCellMap::DestroyCellData(CellData
* aData
) {
2432 BCCellData
* bcData
= static_cast<BCCellData
*>(aData
);
2433 bcData
->~BCCellData();
2434 mPresContext
->PresShell()->FreeByObjectID(eArenaObjectID_BCCellData
,
2438 mPresContext
->PresShell()->FreeByObjectID(eArenaObjectID_CellData
, aData
);
2442 CellData
* nsCellMap::AllocCellData(nsTableCellFrame
* aOrigCell
) {
2445 (BCCellData
*)mPresContext
->PresShell()->AllocateByObjectID(
2446 eArenaObjectID_BCCellData
, sizeof(BCCellData
));
2448 new (data
) BCCellData(aOrigCell
);
2453 CellData
* data
= (CellData
*)mPresContext
->PresShell()->AllocateByObjectID(
2454 eArenaObjectID_CellData
, sizeof(CellData
));
2456 new (data
) CellData(aOrigCell
);
2461 void nsCellMapColumnIterator::AdvanceRowGroup() {
2463 mCurMapStart
+= mCurMapContentRowCount
;
2464 mCurMap
= mCurMap
->GetNextSibling();
2466 // Set mCurMapContentRowCount and mCurMapRelevantRowCount to 0 in case
2467 // mCurMap has no next sibling. This can happen if we just handled the
2468 // last originating cell. Future calls will end up with mFoundCells ==
2469 // mOrigCells, but for this one mFoundCells was definitely not big enough
2471 mCurMapContentRowCount
= 0;
2472 mCurMapRelevantRowCount
= 0;
2476 mCurMapContentRowCount
= mCurMap
->GetRowCount();
2477 uint32_t rowArrayLength
= mCurMap
->mRows
.Length();
2478 mCurMapRelevantRowCount
= std::min(mCurMapContentRowCount
, rowArrayLength
);
2479 } while (0 == mCurMapRelevantRowCount
);
2481 NS_ASSERTION(mCurMapRelevantRowCount
!= 0 || !mCurMap
,
2482 "How did that happen?");
2484 // Set mCurMapRow to 0, since cells can't span across table row groups.
2488 void nsCellMapColumnIterator::IncrementRow(int32_t aIncrement
) {
2489 MOZ_ASSERT(aIncrement
>= 0, "Bogus increment");
2490 MOZ_ASSERT(mCurMap
, "Bogus mOrigCells?");
2491 if (aIncrement
== 0) {
2494 mCurMapRow
+= aIncrement
;
2495 if (mCurMapRow
>= mCurMapRelevantRowCount
) {
2501 nsTableCellFrame
* nsCellMapColumnIterator::GetNextFrame(int32_t* aRow
,
2502 int32_t* aColSpan
) {
2503 // Fast-path for the case when we don't have anything left in the column and
2505 if (mFoundCells
== mOrigCells
) {
2512 NS_ASSERTION(mCurMapRow
< mCurMapRelevantRowCount
, "Bogus mOrigCells?");
2513 // Safe to just get the row (which is faster than calling GetDataAt(), but
2514 // there may not be that many cells in it, so have to use SafeElementAt for
2516 const nsCellMap::CellDataArray
& row
= mCurMap
->mRows
[mCurMapRow
];
2517 CellData
* cellData
= row
.SafeElementAt(mCol
);
2518 if (!cellData
|| cellData
->IsDead()) {
2519 // Could hit this if there are fewer cells in this row than others, for
2525 if (cellData
->IsColSpan()) {
2526 // Look up the originating data for this cell, advance by its relative
2528 int32_t rowspanOffset
= cellData
->GetRowSpanOffset();
2529 nsTableCellFrame
* cellFrame
=
2530 mCurMap
->GetCellFrame(mCurMapRow
, mCol
, *cellData
, false);
2531 NS_ASSERTION(cellFrame
, "Must have usable originating data here");
2532 int32_t rowSpan
= cellFrame
->GetRowSpan();
2536 IncrementRow(rowSpan
- rowspanOffset
);
2541 NS_ASSERTION(cellData
->IsOrig(),
2542 "Must have originating cellData by this point. "
2543 "See comment on mCurMapRow in header.");
2545 nsTableCellFrame
* cellFrame
= cellData
->GetCellFrame();
2546 NS_ASSERTION(cellFrame
, "Orig data without cellframe?");
2548 *aRow
= mCurMapStart
+ mCurMapRow
;
2549 *aColSpan
= mCurMap
->GetEffectiveColSpan(*mMap
, mCurMapRow
, mCol
);
2551 IncrementRow(cellFrame
->GetRowSpan());
2555 MOZ_ASSERT(cellData
== mMap
->GetDataAt(*aRow
, mCol
),
2556 "Giving caller bogus row?");
2561 MOZ_ASSERT_UNREACHABLE("Can't get here");