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
423 void nsTableCellMap::ClearCols() {
425 if (mBCInfo
) mBCInfo
->mBEndBorders
.Clear();
427 void nsTableCellMap::InsertRows(nsTableRowGroupFrame
* aParent
,
428 nsTArray
<nsTableRowFrame
*>& aRows
,
429 int32_t aFirstRowIndex
, bool aConsiderSpans
,
430 TableArea
& aDamageArea
) {
431 int32_t numNewRows
= aRows
.Length();
432 if ((numNewRows
<= 0) || (aFirstRowIndex
< 0)) ABORT0();
434 int32_t rowIndex
= aFirstRowIndex
;
435 int32_t rgStartRowIndex
= 0;
436 nsCellMap
* cellMap
= mFirstMap
;
438 nsTableRowGroupFrame
* rg
= cellMap
->GetRowGroup();
440 cellMap
->InsertRows(*this, aRows
, rowIndex
, aConsiderSpans
,
441 rgStartRowIndex
, aDamageArea
);
442 #ifdef DEBUG_TABLE_CELLMAP
443 Dump("after InsertRows");
446 int32_t count
= mBCInfo
->mIEndBorders
.Length();
447 if (aFirstRowIndex
< count
) {
448 for (int32_t rowX
= aFirstRowIndex
;
449 rowX
< aFirstRowIndex
+ numNewRows
; rowX
++) {
450 mBCInfo
->mIEndBorders
.InsertElementAt(rowX
);
454 aFirstRowIndex
); // this will create missing entries
455 for (int32_t rowX
= aFirstRowIndex
+ 1;
456 rowX
< aFirstRowIndex
+ numNewRows
; rowX
++) {
457 mBCInfo
->mIEndBorders
.AppendElement();
463 int32_t rowCount
= cellMap
->GetRowCount();
464 rgStartRowIndex
+= rowCount
;
465 rowIndex
-= rowCount
;
466 cellMap
= cellMap
->GetNextSibling();
469 NS_ERROR("Attempt to insert row into wrong map.");
472 void nsTableCellMap::RemoveRows(int32_t aFirstRowIndex
,
473 int32_t aNumRowsToRemove
, bool aConsiderSpans
,
474 TableArea
& aDamageArea
) {
475 int32_t rowIndex
= aFirstRowIndex
;
476 int32_t rgStartRowIndex
= 0;
477 nsCellMap
* cellMap
= mFirstMap
;
479 int32_t rowCount
= cellMap
->GetRowCount();
480 if (rowCount
> rowIndex
) {
481 cellMap
->RemoveRows(*this, rowIndex
, aNumRowsToRemove
, aConsiderSpans
,
482 rgStartRowIndex
, aDamageArea
);
484 for (int32_t rowX
= aFirstRowIndex
+ aNumRowsToRemove
- 1;
485 rowX
>= aFirstRowIndex
; rowX
--) {
486 if (uint32_t(rowX
) < mBCInfo
->mIEndBorders
.Length()) {
487 mBCInfo
->mIEndBorders
.RemoveElementAt(rowX
);
493 rgStartRowIndex
+= rowCount
;
494 rowIndex
-= rowCount
;
495 cellMap
= cellMap
->GetNextSibling();
497 #ifdef DEBUG_TABLE_CELLMAP
498 Dump("after RemoveRows");
502 CellData
* nsTableCellMap::AppendCell(nsTableCellFrame
& aCellFrame
,
504 bool aRebuildIfNecessary
,
505 TableArea
& aDamageArea
) {
506 MOZ_ASSERT(&aCellFrame
== aCellFrame
.FirstInFlow(),
507 "invalid call on continuing frame");
508 nsIFrame
* rgFrame
= aCellFrame
.GetParent(); // get the row
509 if (!rgFrame
) return 0;
510 rgFrame
= rgFrame
->GetParent(); // get the row group
511 if (!rgFrame
) return 0;
513 CellData
* result
= nullptr;
514 int32_t rowIndex
= aRowIndex
;
515 int32_t rgStartRowIndex
= 0;
516 nsCellMap
* cellMap
= mFirstMap
;
518 if (cellMap
->GetRowGroup() == rgFrame
) {
520 cellMap
->AppendCell(*this, &aCellFrame
, rowIndex
, aRebuildIfNecessary
,
521 rgStartRowIndex
, aDamageArea
);
524 int32_t rowCount
= cellMap
->GetRowCount();
525 rgStartRowIndex
+= rowCount
;
526 rowIndex
-= rowCount
;
527 cellMap
= cellMap
->GetNextSibling();
529 #ifdef DEBUG_TABLE_CELLMAP
530 Dump("after AppendCell");
535 void nsTableCellMap::InsertCells(nsTArray
<nsTableCellFrame
*>& aCellFrames
,
536 int32_t aRowIndex
, int32_t aColIndexBefore
,
537 TableArea
& aDamageArea
) {
538 int32_t rowIndex
= aRowIndex
;
539 int32_t rgStartRowIndex
= 0;
540 nsCellMap
* cellMap
= mFirstMap
;
542 int32_t rowCount
= cellMap
->GetRowCount();
543 if (rowCount
> rowIndex
) {
544 cellMap
->InsertCells(*this, aCellFrames
, rowIndex
, aColIndexBefore
,
545 rgStartRowIndex
, aDamageArea
);
548 rgStartRowIndex
+= rowCount
;
549 rowIndex
-= rowCount
;
550 cellMap
= cellMap
->GetNextSibling();
552 #ifdef DEBUG_TABLE_CELLMAP
553 Dump("after InsertCells");
557 void nsTableCellMap::RemoveCell(nsTableCellFrame
* aCellFrame
, int32_t aRowIndex
,
558 TableArea
& aDamageArea
) {
559 if (!aCellFrame
) ABORT0();
560 MOZ_ASSERT(aCellFrame
== aCellFrame
->FirstInFlow(),
561 "invalid call on continuing frame");
562 int32_t rowIndex
= aRowIndex
;
563 int32_t rgStartRowIndex
= 0;
564 nsCellMap
* cellMap
= mFirstMap
;
566 int32_t rowCount
= cellMap
->GetRowCount();
567 if (rowCount
> rowIndex
) {
568 cellMap
->RemoveCell(*this, aCellFrame
, rowIndex
, rgStartRowIndex
,
570 #ifdef DEBUG_TABLE_CELLMAP
571 Dump("after RemoveCell");
575 rgStartRowIndex
+= rowCount
;
576 rowIndex
-= rowCount
;
577 cellMap
= cellMap
->GetNextSibling();
579 // if we reach this point - the cell did not get removed, the caller of this
580 // routine will delete the cell and the cellmap will probably hold a reference
581 // to the deleted cell which will cause a subsequent crash when this cell is
583 NS_ERROR("nsTableCellMap::RemoveCell - could not remove cell");
586 void nsTableCellMap::RebuildConsideringCells(
587 nsCellMap
* aCellMap
, nsTArray
<nsTableCellFrame
*>* aCellFrames
,
588 int32_t aRowIndex
, int32_t aColIndex
, bool aInsert
,
589 TableArea
& aDamageArea
) {
590 int32_t numOrigCols
= GetColCount();
592 nsCellMap
* cellMap
= mFirstMap
;
593 int32_t rowCount
= 0;
595 if (cellMap
== aCellMap
) {
596 cellMap
->RebuildConsideringCells(*this, numOrigCols
, aCellFrames
,
597 aRowIndex
, aColIndex
, aInsert
);
599 cellMap
->RebuildConsideringCells(*this, numOrigCols
, nullptr, -1, 0,
602 rowCount
+= cellMap
->GetRowCount();
603 cellMap
= cellMap
->GetNextSibling();
605 SetDamageArea(0, 0, GetColCount(), rowCount
, aDamageArea
);
608 void nsTableCellMap::RebuildConsideringRows(
609 nsCellMap
* aCellMap
, int32_t aStartRowIndex
,
610 nsTArray
<nsTableRowFrame
*>* aRowsToInsert
, int32_t aNumRowsToRemove
,
611 TableArea
& aDamageArea
) {
612 MOZ_ASSERT(!aRowsToInsert
|| aNumRowsToRemove
== 0,
613 "Can't handle both removing and inserting rows at once");
615 int32_t numOrigCols
= GetColCount();
617 nsCellMap
* cellMap
= mFirstMap
;
618 int32_t rowCount
= 0;
620 if (cellMap
== aCellMap
) {
621 cellMap
->RebuildConsideringRows(*this, aStartRowIndex
, aRowsToInsert
,
624 cellMap
->RebuildConsideringCells(*this, numOrigCols
, nullptr, -1, 0,
627 rowCount
+= cellMap
->GetRowCount();
628 cellMap
= cellMap
->GetNextSibling();
630 SetDamageArea(0, 0, GetColCount(), rowCount
, aDamageArea
);
633 int32_t nsTableCellMap::GetNumCellsOriginatingInCol(int32_t aColIndex
) const {
634 int32_t colCount
= mCols
.Length();
635 if ((aColIndex
>= 0) && (aColIndex
< colCount
)) {
636 return mCols
.ElementAt(aColIndex
).mNumCellsOrig
;
638 NS_ERROR("nsCellMap::GetNumCellsOriginatingInCol - bad col index");
644 void nsTableCellMap::Dump(char* aString
) const {
645 if (aString
) printf("%s \n", aString
);
646 printf("***** START TABLE CELL MAP DUMP ***** %p\n", (void*)this);
648 int32_t colCount
= mCols
.Length();
649 printf("cols array orig/span-> %p", (void*)this);
650 for (int32_t colX
= 0; colX
< colCount
; colX
++) {
651 const nsColInfo
& colInfo
= mCols
.ElementAt(colX
);
652 printf("%d=%d/%d ", colX
, colInfo
.mNumCellsOrig
, colInfo
.mNumCellsSpan
);
654 printf(" cols in cache %d\n", int(mTableFrame
.GetColCache().Length()));
655 nsCellMap
* cellMap
= mFirstMap
;
657 cellMap
->Dump(nullptr != mBCInfo
);
658 cellMap
= cellMap
->GetNextSibling();
660 if (nullptr != mBCInfo
) {
661 printf("***** block-end borders *****\n");
668 int32_t numCols
= mBCInfo
->mBEndBorders
.Length();
669 for (int32_t i
= 0; i
<= 2; i
++) {
671 for (colIndex
= 0; colIndex
< numCols
; colIndex
++) {
672 BCData
& cd
= mBCInfo
->mBEndBorders
.ElementAt(colIndex
);
674 size
= cd
.GetBStartEdge(owner
, segStart
);
675 printf("t=%d%X%d ", int32_t(size
), owner
, segStart
);
677 size
= cd
.GetIStartEdge(owner
, segStart
);
678 printf("l=%d%X%d ", int32_t(size
), owner
, segStart
);
680 size
= cd
.GetCorner(side
, bevel
);
681 printf("c=%d%hhX%d ", int32_t(size
), static_cast<uint8_t>(side
),
685 BCData
& cd
= mBCInfo
->mBEndIEndCorner
;
687 size
= cd
.GetBStartEdge(owner
, segStart
);
688 printf("t=%d%X%d ", int32_t(size
), owner
, segStart
);
690 size
= cd
.GetIStartEdge(owner
, segStart
);
691 printf("l=%d%X%d ", int32_t(size
), owner
, segStart
);
693 size
= cd
.GetCorner(side
, bevel
);
694 printf("c=%d%hhX%d ", int32_t(size
), static_cast<uint8_t>(side
), bevel
);
699 printf("***** END TABLE CELL MAP DUMP *****\n");
703 nsTableCellFrame
* nsTableCellMap::GetCellInfoAt(int32_t aRowIndex
,
706 int32_t* aColSpan
) const {
707 int32_t rowIndex
= aRowIndex
;
708 nsCellMap
* cellMap
= mFirstMap
;
710 if (cellMap
->GetRowCount() > rowIndex
) {
711 return cellMap
->GetCellInfoAt(*this, rowIndex
, aColIndex
, aOriginates
,
714 rowIndex
-= cellMap
->GetRowCount();
715 cellMap
= cellMap
->GetNextSibling();
720 int32_t nsTableCellMap::GetIndexByRowAndColumn(int32_t aRow
,
721 int32_t aColumn
) const {
724 int32_t colCount
= mCols
.Length();
725 int32_t rowIndex
= aRow
;
727 nsCellMap
* cellMap
= mFirstMap
;
729 int32_t rowCount
= cellMap
->GetRowCount();
730 if (rowIndex
>= rowCount
) {
731 // If the rowCount is less than the rowIndex, this means that the index is
732 // not within the current map. If so, get the index of the last cell in
734 rowIndex
-= rowCount
;
736 int32_t cellMapIdx
= cellMap
->GetHighestIndex(colCount
);
737 if (cellMapIdx
!= -1) index
+= cellMapIdx
+ 1;
740 // Index is in valid range for this cellmap, so get the index of rowIndex
743 cellMap
->GetIndexByRowAndColumn(colCount
, rowIndex
, aColumn
);
744 if (cellMapIdx
== -1) return -1; // no cell at the given row and column.
747 return index
; // no need to look through further maps here
750 cellMap
= cellMap
->GetNextSibling();
756 void nsTableCellMap::GetRowAndColumnByIndex(int32_t aIndex
, int32_t* aRow
,
757 int32_t* aColumn
) const {
761 int32_t colCount
= mCols
.Length();
763 int32_t previousRows
= 0;
764 int32_t index
= aIndex
;
766 nsCellMap
* cellMap
= mFirstMap
;
768 int32_t rowCount
= cellMap
->GetRowCount();
769 // Determine the highest possible index in this map to see
770 // if wanted index is in here.
771 int32_t cellMapIdx
= cellMap
->GetHighestIndex(colCount
);
772 if (cellMapIdx
== -1) {
773 // The index is not within this map, increase the total row index
775 previousRows
+= rowCount
;
777 if (index
> cellMapIdx
) {
778 // The index is not within this map, so decrease it by the cellMapIdx
779 // determined index and increase the total row index accordingly.
780 index
-= cellMapIdx
+ 1;
781 previousRows
+= rowCount
;
783 cellMap
->GetRowAndColumnByIndex(colCount
, index
, aRow
, aColumn
);
784 // If there were previous indexes, take them into account.
785 *aRow
+= previousRows
;
786 return; // no need to look any further.
790 cellMap
= cellMap
->GetNextSibling();
794 bool nsTableCellMap::RowIsSpannedInto(int32_t aRowIndex
,
795 int32_t aNumEffCols
) const {
796 int32_t rowIndex
= aRowIndex
;
797 nsCellMap
* cellMap
= mFirstMap
;
799 if (cellMap
->GetRowCount() > rowIndex
) {
800 return cellMap
->RowIsSpannedInto(rowIndex
, aNumEffCols
);
802 rowIndex
-= cellMap
->GetRowCount();
803 cellMap
= cellMap
->GetNextSibling();
808 bool nsTableCellMap::RowHasSpanningCells(int32_t aRowIndex
,
809 int32_t aNumEffCols
) const {
810 int32_t rowIndex
= aRowIndex
;
811 nsCellMap
* cellMap
= mFirstMap
;
813 if (cellMap
->GetRowCount() > rowIndex
) {
814 return cellMap
->RowHasSpanningCells(rowIndex
, aNumEffCols
);
816 rowIndex
-= cellMap
->GetRowCount();
817 cellMap
= cellMap
->GetNextSibling();
822 // FIXME: The only value callers pass for aSide is LogicalSide::BEnd.
823 // Consider removing support for the other three values.
824 void nsTableCellMap::ResetBStartStart(LogicalSide aSide
, nsCellMap
& aCellMap
,
825 uint32_t aRowGroupStart
,
826 uint32_t aRowIndex
, uint32_t aColIndex
) {
827 if (!mBCInfo
) ABORT0();
829 BCCellData
* cellData
;
830 BCData
* bcData
= nullptr;
833 case LogicalSide::BEnd
:
836 case LogicalSide::BStart
:
837 cellData
= (BCCellData
*)aCellMap
.GetDataAt(aRowIndex
- aRowGroupStart
,
840 bcData
= &cellData
->mData
;
842 NS_ASSERTION(aSide
== LogicalSide::BEnd
, "program error");
843 // try the next row group
844 nsCellMap
* cellMap
= aCellMap
.GetNextSibling();
846 cellData
= (BCCellData
*)cellMap
->GetDataAt(0, aColIndex
);
848 bcData
= &cellData
->mData
;
850 bcData
= GetBEndMostBorder(aColIndex
);
855 case LogicalSide::IEnd
:
858 case LogicalSide::IStart
:
859 cellData
= (BCCellData
*)aCellMap
.GetDataAt(aRowIndex
- aRowGroupStart
,
862 bcData
= &cellData
->mData
;
864 NS_ASSERTION(aSide
== LogicalSide::IEnd
, "program error");
865 bcData
= GetIEndMostBorder(aRowIndex
);
870 bcData
->SetBStartStart(false);
874 // store the aSide border segment at coord = (aRowIndex, aColIndex). For
875 // bStart/iStart, store the info at coord. For bEnd/iEnd store it at the
876 // adjacent location so that it is bStart/iStart at that location. If the new
877 // location is at the iEnd or bEnd edge of the table, then store it one of the
878 // special arrays (iEnd-most borders, bEnd-most borders).
879 void nsTableCellMap::SetBCBorderEdge(LogicalSide aSide
, nsCellMap
& aCellMap
,
880 uint32_t aCellMapStart
, uint32_t aRowIndex
,
881 uint32_t aColIndex
, uint32_t aLength
,
882 BCBorderOwner aOwner
, nscoord aSize
,
884 if (!mBCInfo
) ABORT0();
886 BCCellData
* cellData
;
887 int32_t lastIndex
, xIndex
, yIndex
;
888 int32_t xPos
= aColIndex
;
889 int32_t yPos
= aRowIndex
;
890 int32_t rgYPos
= aRowIndex
- aCellMapStart
;
894 case LogicalSide::BEnd
:
898 case LogicalSide::BStart
:
899 lastIndex
= xPos
+ aLength
- 1;
900 for (xIndex
= xPos
; xIndex
<= lastIndex
; xIndex
++) {
901 changed
= aChanged
&& (xIndex
== xPos
);
902 BCData
* bcData
= nullptr;
903 cellData
= (BCCellData
*)aCellMap
.GetDataAt(rgYPos
, xIndex
);
905 int32_t numRgRows
= aCellMap
.GetRowCount();
906 if (yPos
< numRgRows
) { // add a dead cell data
907 TableArea damageArea
;
908 cellData
= (BCCellData
*)aCellMap
.AppendCell(*this, nullptr, rgYPos
,
909 false, 0, damageArea
);
910 if (!cellData
) ABORT0();
912 NS_ASSERTION(aSide
== LogicalSide::BEnd
, "program error");
913 // try the next non empty row group
914 nsCellMap
* cellMap
= aCellMap
.GetNextSibling();
915 while (cellMap
&& (0 == cellMap
->GetRowCount())) {
916 cellMap
= cellMap
->GetNextSibling();
919 cellData
= (BCCellData
*)cellMap
->GetDataAt(0, xIndex
);
920 if (!cellData
) { // add a dead cell
921 TableArea damageArea
;
922 cellData
= (BCCellData
*)cellMap
->AppendCell(
923 *this, nullptr, 0, false, 0, damageArea
);
925 } else { // must be at the end of the table
926 bcData
= GetBEndMostBorder(xIndex
);
930 if (!bcData
&& cellData
) {
931 bcData
= &cellData
->mData
;
934 bcData
->SetBStartEdge(aOwner
, aSize
, changed
);
936 NS_ERROR("Cellmap: BStart edge not found");
939 case LogicalSide::IEnd
:
942 case LogicalSide::IStart
:
943 // since bStart, bEnd borders were set, there should already be a cellData
945 lastIndex
= rgYPos
+ aLength
- 1;
946 for (yIndex
= rgYPos
; yIndex
<= lastIndex
; yIndex
++) {
947 changed
= aChanged
&& (yIndex
== rgYPos
);
948 cellData
= (BCCellData
*)aCellMap
.GetDataAt(yIndex
, xPos
);
950 cellData
->mData
.SetIStartEdge(aOwner
, aSize
, changed
);
952 NS_ASSERTION(aSide
== LogicalSide::IEnd
, "program error");
953 BCData
* bcData
= GetIEndMostBorder(yIndex
+ aCellMapStart
);
955 bcData
->SetIStartEdge(aOwner
, aSize
, changed
);
957 NS_ERROR("Cellmap: IStart edge not found");
964 // store corner info (aOwner, aSubSize, aBevel). For aCorner = eBStartIStart,
965 // store the info at (aRowIndex, aColIndex). For eBStartIEnd, store it in the
966 // entry to the iEnd-wards where it would be BStartIStart. For eBEndIEnd, store
967 // it in the entry to the bEnd-wards. etc.
968 void nsTableCellMap::SetBCBorderCorner(LogicalCorner aCorner
,
970 uint32_t aCellMapStart
,
971 uint32_t aRowIndex
, uint32_t aColIndex
,
972 LogicalSide aOwner
, nscoord aSubSize
,
973 bool aBevel
, bool aIsBEndIEnd
) {
974 if (!mBCInfo
) ABORT0();
977 mBCInfo
->mBEndIEndCorner
.SetCorner(aSubSize
, aOwner
, aBevel
);
981 int32_t xPos
= aColIndex
;
982 int32_t yPos
= aRowIndex
;
983 int32_t rgYPos
= aRowIndex
- aCellMapStart
;
985 if (LogicalCorner::BStartIEnd
== aCorner
) {
987 } else if (LogicalCorner::BEndIEnd
== aCorner
) {
991 } else if (LogicalCorner::BEndIStart
== aCorner
) {
996 BCCellData
* cellData
= nullptr;
997 BCData
* bcData
= nullptr;
998 if (GetColCount() <= xPos
) {
999 NS_ASSERTION(xPos
== GetColCount(), "program error");
1000 // at the iEnd edge of the table as we checked the corner before
1001 NS_ASSERTION(!aIsBEndIEnd
, "should be handled before");
1002 bcData
= GetIEndMostBorder(yPos
);
1004 cellData
= (BCCellData
*)aCellMap
.GetDataAt(rgYPos
, xPos
);
1006 int32_t numRgRows
= aCellMap
.GetRowCount();
1007 if (yPos
< numRgRows
) { // add a dead cell data
1008 TableArea damageArea
;
1009 cellData
= (BCCellData
*)aCellMap
.AppendCell(*this, nullptr, rgYPos
,
1010 false, 0, damageArea
);
1012 // try the next non empty row group
1013 nsCellMap
* cellMap
= aCellMap
.GetNextSibling();
1014 while (cellMap
&& (0 == cellMap
->GetRowCount())) {
1015 cellMap
= cellMap
->GetNextSibling();
1018 cellData
= (BCCellData
*)cellMap
->GetDataAt(0, xPos
);
1019 if (!cellData
) { // add a dead cell
1020 TableArea damageArea
;
1021 cellData
= (BCCellData
*)cellMap
->AppendCell(*this, nullptr, 0,
1022 false, 0, damageArea
);
1024 } else { // must be at the bEnd of the table
1025 bcData
= GetBEndMostBorder(xPos
);
1030 if (!bcData
&& cellData
) {
1031 bcData
= &cellData
->mData
;
1034 bcData
->SetCorner(aSubSize
, aOwner
, aBevel
);
1036 NS_ERROR("program error: Corner not found");
1039 nsCellMap::nsCellMap(nsTableRowGroupFrame
* aRowGroup
, bool aIsBC
)
1041 mContentRowCount(0),
1042 mRowGroupFrame(aRowGroup
),
1043 mNextSibling(nullptr),
1045 mPresContext(aRowGroup
->PresContext()) {
1046 MOZ_COUNT_CTOR(nsCellMap
);
1047 NS_ASSERTION(mPresContext
, "Must have prescontext");
1050 nsCellMap::~nsCellMap() {
1051 MOZ_COUNT_DTOR(nsCellMap
);
1053 uint32_t mapRowCount
= mRows
.Length();
1054 for (uint32_t rowX
= 0; rowX
< mapRowCount
; rowX
++) {
1055 CellDataArray
& row
= mRows
[rowX
];
1056 uint32_t colCount
= row
.Length();
1057 for (uint32_t colX
= 0; colX
< colCount
; colX
++) {
1058 DestroyCellData(row
[colX
]);
1064 void nsCellMap::Init() {
1065 MOZ_ASSERT(!sEmptyRow
, "How did that happen?");
1066 sEmptyRow
= new nsCellMap::CellDataArray();
1070 void nsCellMap::Shutdown() { sEmptyRow
= nullptr; }
1072 nsTableCellFrame
* nsCellMap::GetCellFrame(int32_t aRowIndexIn
,
1073 int32_t aColIndexIn
, CellData
& aData
,
1074 bool aUseRowIfOverlap
) const {
1075 int32_t rowIndex
= aRowIndexIn
- aData
.GetRowSpanOffset();
1076 int32_t colIndex
= aColIndexIn
- aData
.GetColSpanOffset();
1077 if (aData
.IsOverlap()) {
1078 if (aUseRowIfOverlap
) {
1079 colIndex
= aColIndexIn
;
1081 rowIndex
= aRowIndexIn
;
1086 mRows
.SafeElementAt(rowIndex
, *sEmptyRow
).SafeElementAt(colIndex
);
1088 return data
->GetCellFrame();
1093 int32_t nsCellMap::GetHighestIndex(int32_t aColCount
) {
1095 int32_t rowCount
= mRows
.Length();
1096 for (int32_t rowIdx
= 0; rowIdx
< rowCount
; rowIdx
++) {
1097 const CellDataArray
& row
= mRows
[rowIdx
];
1099 for (int32_t colIdx
= 0; colIdx
< aColCount
; colIdx
++) {
1100 CellData
* data
= row
.SafeElementAt(colIdx
);
1101 // No data means row doesn't have more cells.
1104 if (data
->IsOrig()) index
++;
1111 int32_t nsCellMap::GetIndexByRowAndColumn(int32_t aColCount
, int32_t aRow
,
1112 int32_t aColumn
) const {
1113 if (uint32_t(aRow
) >= mRows
.Length()) return -1;
1116 int32_t lastColsIdx
= aColCount
- 1;
1118 // Find row index of the cell where row span is started.
1119 const CellDataArray
& row
= mRows
[aRow
];
1120 CellData
* data
= row
.SafeElementAt(aColumn
);
1121 int32_t origRow
= data
? aRow
- data
->GetRowSpanOffset() : aRow
;
1123 // Calculate cell index.
1124 for (int32_t rowIdx
= 0; rowIdx
<= origRow
; rowIdx
++) {
1125 const CellDataArray
& row
= mRows
[rowIdx
];
1126 int32_t colCount
= (rowIdx
== origRow
) ? aColumn
: lastColsIdx
;
1128 for (int32_t colIdx
= 0; colIdx
<= colCount
; colIdx
++) {
1129 data
= row
.SafeElementAt(colIdx
);
1130 // No data means row doesn't have more cells.
1133 if (data
->IsOrig()) index
++;
1137 // Given row and column don't point to the cell.
1138 if (!data
) return -1;
1143 void nsCellMap::GetRowAndColumnByIndex(int32_t aColCount
, int32_t aIndex
,
1144 int32_t* aRow
, int32_t* aColumn
) const {
1148 int32_t index
= aIndex
;
1149 int32_t rowCount
= mRows
.Length();
1151 for (int32_t rowIdx
= 0; rowIdx
< rowCount
; rowIdx
++) {
1152 const CellDataArray
& row
= mRows
[rowIdx
];
1154 for (int32_t colIdx
= 0; colIdx
< aColCount
; colIdx
++) {
1155 CellData
* data
= row
.SafeElementAt(colIdx
);
1157 // The row doesn't have more cells.
1160 if (data
->IsOrig()) index
--;
1171 bool nsCellMap::Grow(nsTableCellMap
& aMap
, int32_t aNumRows
,
1172 int32_t aRowIndex
) {
1173 NS_ASSERTION(aNumRows
>= 1, "Why are we calling this?");
1175 // Get the number of cols we want to use for preallocating the row arrays.
1176 int32_t numCols
= aMap
.GetColCount();
1180 uint32_t startRowIndex
= (aRowIndex
>= 0) ? aRowIndex
: mRows
.Length();
1181 NS_ASSERTION(startRowIndex
<= mRows
.Length(), "Missing grow call inbetween");
1183 // XXX Change the return type of this function to void, or use a fallible
1185 mRows
.InsertElementsAt(startRowIndex
, aNumRows
, numCols
);
1189 void nsCellMap::GrowRow(CellDataArray
& aRow
, int32_t aNumCols
)
1192 // Have to have the cast to get the template to do the right thing.
1193 aRow
.InsertElementsAt(aRow
.Length(), aNumCols
, (CellData
*)nullptr);
1196 void nsCellMap::InsertRows(nsTableCellMap
& aMap
,
1197 nsTArray
<nsTableRowFrame
*>& aRows
,
1198 int32_t aFirstRowIndex
, bool aConsiderSpans
,
1199 int32_t aRgFirstRowIndex
, TableArea
& aDamageArea
) {
1200 int32_t numCols
= aMap
.GetColCount();
1201 NS_ASSERTION(aFirstRowIndex
>= 0,
1202 "nsCellMap::InsertRows called with negative rowIndex");
1203 if (uint32_t(aFirstRowIndex
) > mRows
.Length()) {
1204 // create (aFirstRowIndex - mRows.Length()) empty rows up to aFirstRowIndex
1205 int32_t numEmptyRows
= aFirstRowIndex
- mRows
.Length();
1206 if (!Grow(aMap
, numEmptyRows
)) {
1211 if (!aConsiderSpans
) {
1212 // update mContentRowCount, since non-empty rows will be added
1213 mContentRowCount
= std::max(aFirstRowIndex
, mContentRowCount
);
1214 ExpandWithRows(aMap
, aRows
, aFirstRowIndex
, aRgFirstRowIndex
, aDamageArea
);
1218 // if any cells span into or out of the row being inserted, then rebuild
1219 bool spansCauseRebuild
=
1220 CellsSpanInOrOut(aFirstRowIndex
, aFirstRowIndex
, 0, numCols
- 1);
1222 // update mContentRowCount, since non-empty rows will be added
1223 mContentRowCount
= std::max(aFirstRowIndex
, mContentRowCount
);
1225 // if any of the new cells span out of the new rows being added, then rebuild
1226 // XXX it would be better to only rebuild the portion of the map that follows
1228 if (!spansCauseRebuild
&& (uint32_t(aFirstRowIndex
) < mRows
.Length())) {
1229 spansCauseRebuild
= CellsSpanOut(aRows
);
1231 if (spansCauseRebuild
) {
1232 aMap
.RebuildConsideringRows(this, aFirstRowIndex
, &aRows
, 0, aDamageArea
);
1234 ExpandWithRows(aMap
, aRows
, aFirstRowIndex
, aRgFirstRowIndex
, aDamageArea
);
1238 void nsCellMap::RemoveRows(nsTableCellMap
& aMap
, int32_t aFirstRowIndex
,
1239 int32_t aNumRowsToRemove
, bool aConsiderSpans
,
1240 int32_t aRgFirstRowIndex
, TableArea
& aDamageArea
) {
1241 int32_t numRows
= mRows
.Length();
1242 int32_t numCols
= aMap
.GetColCount();
1244 if (aFirstRowIndex
>= numRows
) {
1245 // reduce the content based row count based on the function arguments
1246 // as they are known to be real rows even if the cell map did not create
1247 // rows for them before.
1248 mContentRowCount
-= aNumRowsToRemove
;
1251 if (!aConsiderSpans
) {
1252 ShrinkWithoutRows(aMap
, aFirstRowIndex
, aNumRowsToRemove
, aRgFirstRowIndex
,
1256 int32_t endRowIndex
= aFirstRowIndex
+ aNumRowsToRemove
- 1;
1257 if (endRowIndex
>= numRows
) {
1258 NS_ERROR("nsCellMap::RemoveRows tried to remove too many rows");
1259 endRowIndex
= numRows
- 1;
1261 bool spansCauseRebuild
=
1262 CellsSpanInOrOut(aFirstRowIndex
, endRowIndex
, 0, numCols
- 1);
1263 if (spansCauseRebuild
) {
1264 aMap
.RebuildConsideringRows(this, aFirstRowIndex
, nullptr, aNumRowsToRemove
,
1267 ShrinkWithoutRows(aMap
, aFirstRowIndex
, aNumRowsToRemove
, aRgFirstRowIndex
,
1272 CellData
* nsCellMap::AppendCell(nsTableCellMap
& aMap
,
1273 nsTableCellFrame
* aCellFrame
, int32_t aRowIndex
,
1274 bool aRebuildIfNecessary
,
1275 int32_t aRgFirstRowIndex
,
1276 TableArea
& aDamageArea
,
1277 int32_t* aColToBeginSearch
) {
1278 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1279 int32_t origNumMapRows
= mRows
.Length();
1280 int32_t origNumCols
= aMap
.GetColCount();
1281 bool zeroRowSpan
= false;
1283 (aCellFrame
) ? GetRowSpanForNewCell(aCellFrame
, aRowIndex
, zeroRowSpan
)
1285 // add new rows if necessary
1286 int32_t endRowIndex
= aRowIndex
+ rowSpan
- 1;
1287 if (endRowIndex
>= origNumMapRows
) {
1288 // XXXbz handle allocation failures?
1289 Grow(aMap
, 1 + endRowIndex
- origNumMapRows
);
1292 // get the first null or dead CellData in the desired row. It will equal
1293 // origNumCols if there are none
1294 CellData
* origData
= nullptr;
1295 int32_t startColIndex
= 0;
1296 if (aColToBeginSearch
) startColIndex
= *aColToBeginSearch
;
1297 for (; startColIndex
< origNumCols
; startColIndex
++) {
1298 CellData
* data
= GetDataAt(aRowIndex
, startColIndex
);
1300 // The border collapse code relies on having multiple dead cell data entries
1302 if (data
->IsDead() && aCellFrame
) {
1307 // We found the place to append the cell, when the next cell is appended
1308 // the next search does not need to duplicate the search but can start
1309 // just at the next cell.
1310 if (aColToBeginSearch
) *aColToBeginSearch
= startColIndex
+ 1;
1312 int32_t colSpan
= aCellFrame
? aCellFrame
->GetColSpan() : 1;
1314 // if the new cell could potentially span into other rows and collide with
1315 // originating cells there, we will play it safe and just rebuild the map
1316 if (aRebuildIfNecessary
&& (aRowIndex
< mContentRowCount
- 1) &&
1318 AutoTArray
<nsTableCellFrame
*, 1> newCellArray
;
1319 newCellArray
.AppendElement(aCellFrame
);
1320 aMap
.RebuildConsideringCells(this, &newCellArray
, aRowIndex
, startColIndex
,
1324 mContentRowCount
= std::max(mContentRowCount
, aRowIndex
+ 1);
1326 // add new cols to the table map if necessary
1327 int32_t endColIndex
= startColIndex
+ colSpan
- 1;
1328 if (endColIndex
>= origNumCols
) {
1329 NS_ASSERTION(aCellFrame
, "dead cells should not require new columns");
1330 aMap
.AddColsAtEnd(1 + endColIndex
- origNumCols
);
1333 // Setup CellData for this cell
1335 NS_ASSERTION(origData
->IsDead(),
1336 "replacing a non dead cell is a memory leak");
1337 if (aCellFrame
) { // do nothing to replace a dead cell with a dead cell
1338 origData
->Init(aCellFrame
);
1339 // we are replacing a dead cell, increase the number of cells
1340 // originating at this column
1341 nsColInfo
* colInfo
= aMap
.GetColInfoAt(startColIndex
);
1342 NS_ASSERTION(colInfo
, "access to a non existing column");
1344 colInfo
->mNumCellsOrig
++;
1348 origData
= AllocCellData(aCellFrame
);
1349 if (!origData
) ABORT1(origData
);
1350 SetDataAt(aMap
, *origData
, aRowIndex
, startColIndex
);
1353 if (aRebuildIfNecessary
) {
1354 // the caller depends on the damageArea
1355 // The special case for zeroRowSpan is to adjust for the '2' in
1356 // GetRowSpanForNewCell.
1357 uint32_t height
= std::min(zeroRowSpan
? rowSpan
- 1 : rowSpan
,
1358 GetRowCount() - aRowIndex
);
1359 SetDamageArea(startColIndex
, aRgFirstRowIndex
+ aRowIndex
,
1360 1 + endColIndex
- startColIndex
, height
, aDamageArea
);
1367 // initialize the cell frame
1368 aCellFrame
->SetColIndex(startColIndex
);
1370 // Create CellData objects for the rows that this cell spans. Set
1371 // their mOrigCell to nullptr and their mSpanData to point to data.
1372 for (int32_t rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1373 // The row at rowX will need to have at least endColIndex columns
1374 mRows
[rowX
].SetCapacity(endColIndex
);
1375 for (int32_t colX
= startColIndex
; colX
<= endColIndex
; colX
++) {
1376 if ((rowX
!= aRowIndex
) ||
1377 (colX
!= startColIndex
)) { // skip orig cell data done above
1378 CellData
* cellData
= GetDataAt(rowX
, colX
);
1380 if (cellData
->IsOrig()) {
1381 NS_ERROR("cannot overlap originating cell");
1384 if (rowX
> aRowIndex
) { // row spanning into cell
1385 if (cellData
->IsRowSpan()) {
1386 // do nothing, this can be caused by rowspan which is overlapped
1387 // by a another cell with a rowspan and a colspan
1389 cellData
->SetRowSpanOffset(rowX
- aRowIndex
);
1391 cellData
->SetZeroRowSpan(true);
1395 if (colX
> startColIndex
) { // col spanning into cell
1396 if (!cellData
->IsColSpan()) {
1397 if (cellData
->IsRowSpan()) {
1398 cellData
->SetOverlap(true);
1400 cellData
->SetColSpanOffset(colX
- startColIndex
);
1401 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1402 colInfo
->mNumCellsSpan
++;
1406 cellData
= AllocCellData(nullptr);
1407 if (!cellData
) return origData
;
1408 if (rowX
> aRowIndex
) {
1409 cellData
->SetRowSpanOffset(rowX
- aRowIndex
);
1411 cellData
->SetZeroRowSpan(true);
1414 if (colX
> startColIndex
) {
1415 cellData
->SetColSpanOffset(colX
- startColIndex
);
1417 SetDataAt(aMap
, *cellData
, rowX
, colX
);
1422 #ifdef DEBUG_TABLE_CELLMAP
1423 printf("appended cell=%p row=%d \n", aCellFrame
, aRowIndex
);
1429 bool nsCellMap::CellsSpanOut(nsTArray
<nsTableRowFrame
*>& aRows
) const {
1430 int32_t numNewRows
= aRows
.Length();
1431 for (int32_t rowX
= 0; rowX
< numNewRows
; rowX
++) {
1432 nsTableRowFrame
* rowFrame
= aRows
.ElementAt(rowX
);
1433 for (nsTableCellFrame
* cellFrame
= rowFrame
->GetFirstCell(); cellFrame
;
1434 cellFrame
= cellFrame
->GetNextCell()) {
1436 int32_t rowSpan
= GetRowSpanForNewCell(cellFrame
, rowX
, zeroSpan
);
1437 if (zeroSpan
|| rowX
+ rowSpan
> numNewRows
) {
1445 // return true if any cells have rows spans into or out of the region
1446 // defined by the row and col indices or any cells have colspans into the region
1447 bool nsCellMap::CellsSpanInOrOut(int32_t aStartRowIndex
, int32_t aEndRowIndex
,
1448 int32_t aStartColIndex
,
1449 int32_t aEndColIndex
) const {
1451 * this routine will watch the cells adjacent to the region or at the edge
1452 * they are marked with *. The routine will verify whether they span in or
1456 * r1c1 r1c2 r1c3 r1c4 r1c5 r1rc6 r1c7
1457 * startrow r2c1 r2c2 *r2c3 *r2c4 *r2c5 *r2rc6 r2c7
1458 * endrow r3c1 r3c2 *r3c3 r3c4 r3c5 *r3rc6 r3c7
1459 * r4c1 r4c2 *r4c3 *r4c4 *r4c5 r4rc6 r4c7
1460 * r5c1 r5c2 r5c3 r5c4 r5c5 r5rc6 r5c7
1463 int32_t numRows
= mRows
.Length(); // use the cellmap rows to determine the
1464 // current cellmap extent.
1465 for (int32_t colX
= aStartColIndex
; colX
<= aEndColIndex
; colX
++) {
1467 if (aStartRowIndex
> 0) {
1468 cellData
= GetDataAt(aStartRowIndex
, colX
);
1469 if (cellData
&& (cellData
->IsRowSpan())) {
1470 return true; // there is a row span into the region
1472 if ((aStartRowIndex
>= mContentRowCount
) && (mContentRowCount
> 0)) {
1473 cellData
= GetDataAt(mContentRowCount
- 1, colX
);
1474 if (cellData
&& cellData
->IsZeroRowSpan()) {
1475 return true; // When we expand the zerospan it'll span into our row
1479 if (aEndRowIndex
< numRows
- 1) { // is there anything below aEndRowIndex
1480 cellData
= GetDataAt(aEndRowIndex
+ 1, colX
);
1481 if ((cellData
) && (cellData
->IsRowSpan())) {
1482 return true; // there is a row span out of the region
1485 cellData
= GetDataAt(aEndRowIndex
, colX
);
1486 if ((cellData
) && (cellData
->IsRowSpan()) &&
1487 (mContentRowCount
< numRows
)) {
1488 return true; // this cell might be the cause of a dead row
1492 if (aStartColIndex
> 0) {
1493 for (int32_t rowX
= aStartRowIndex
; rowX
<= aEndRowIndex
; rowX
++) {
1494 CellData
* cellData
= GetDataAt(rowX
, aStartColIndex
);
1495 if (cellData
&& (cellData
->IsColSpan())) {
1496 return true; // there is a col span into the region
1498 cellData
= GetDataAt(rowX
, aEndColIndex
+ 1);
1499 if (cellData
&& (cellData
->IsColSpan())) {
1500 return true; // there is a col span out of the region
1507 void nsCellMap::InsertCells(nsTableCellMap
& aMap
,
1508 nsTArray
<nsTableCellFrame
*>& aCellFrames
,
1509 int32_t aRowIndex
, int32_t aColIndexBefore
,
1510 int32_t aRgFirstRowIndex
, TableArea
& aDamageArea
) {
1511 if (aCellFrames
.Length() == 0) return;
1512 NS_ASSERTION(aColIndexBefore
>= -1, "index out of range");
1513 int32_t numCols
= aMap
.GetColCount();
1514 if (aColIndexBefore
>= numCols
) {
1516 "Inserting instead of appending cells indicates a serious cellmap "
1518 aColIndexBefore
= numCols
- 1;
1521 // get the starting col index of the 1st new cells
1522 int32_t startColIndex
;
1523 for (startColIndex
= aColIndexBefore
+ 1; startColIndex
< numCols
;
1525 CellData
* data
= GetDataAt(aRowIndex
, startColIndex
);
1526 if (!data
|| data
->IsOrig() || data
->IsDead()) {
1527 // // Not a span. Stop.
1532 // record whether inserted cells are going to cause complications due
1533 // to existing row spans, col spans or table sizing.
1534 bool spansCauseRebuild
= false;
1536 // check that all cells have the same row span
1537 int32_t numNewCells
= aCellFrames
.Length();
1538 bool zeroRowSpan
= false;
1539 int32_t rowSpan
= 0;
1540 for (int32_t cellX
= 0; cellX
< numNewCells
; cellX
++) {
1541 nsTableCellFrame
* cell
= aCellFrames
.ElementAt(cellX
);
1542 int32_t rowSpan2
= GetRowSpanForNewCell(cell
, aRowIndex
, zeroRowSpan
);
1545 } else if (rowSpan
!= rowSpan2
) {
1546 spansCauseRebuild
= true;
1551 // check if the new cells will cause the table to add more rows
1552 if (!spansCauseRebuild
) {
1553 if (mRows
.Length() < uint32_t(aRowIndex
+ rowSpan
)) {
1554 spansCauseRebuild
= true;
1558 if (!spansCauseRebuild
) {
1559 spansCauseRebuild
= CellsSpanInOrOut(aRowIndex
, aRowIndex
+ rowSpan
- 1,
1560 startColIndex
, numCols
- 1);
1562 if (spansCauseRebuild
) {
1563 aMap
.RebuildConsideringCells(this, &aCellFrames
, aRowIndex
, startColIndex
,
1566 ExpandWithCells(aMap
, aCellFrames
, aRowIndex
, startColIndex
, rowSpan
,
1567 zeroRowSpan
, aRgFirstRowIndex
, aDamageArea
);
1571 void nsCellMap::ExpandWithRows(nsTableCellMap
& aMap
,
1572 nsTArray
<nsTableRowFrame
*>& aRowFrames
,
1573 int32_t aStartRowIndexIn
,
1574 int32_t aRgFirstRowIndex
,
1575 TableArea
& aDamageArea
) {
1576 int32_t startRowIndex
= (aStartRowIndexIn
>= 0) ? aStartRowIndexIn
: 0;
1577 NS_ASSERTION(uint32_t(startRowIndex
) <= mRows
.Length(),
1578 "caller should have grown cellmap before");
1580 int32_t numNewRows
= aRowFrames
.Length();
1581 mContentRowCount
+= numNewRows
;
1583 int32_t endRowIndex
= startRowIndex
+ numNewRows
- 1;
1585 // shift the rows after startRowIndex down and insert empty rows that will
1586 // be filled via the AppendCell call below
1587 if (!Grow(aMap
, numNewRows
, startRowIndex
)) {
1591 int32_t newRowIndex
= 0;
1592 for (int32_t rowX
= startRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1593 nsTableRowFrame
* rFrame
= aRowFrames
.ElementAt(newRowIndex
);
1595 int32_t colIndex
= 0;
1596 for (nsTableCellFrame
* cellFrame
= rFrame
->GetFirstCell(); cellFrame
;
1597 cellFrame
= cellFrame
->GetNextCell()) {
1598 AppendCell(aMap
, cellFrame
, rowX
, false, aRgFirstRowIndex
, aDamageArea
,
1603 // mark all following rows damaged, they might contain a previously set
1604 // damage area which we can not shift.
1605 int32_t firstDamagedRow
= aRgFirstRowIndex
+ startRowIndex
;
1606 SetDamageArea(0, firstDamagedRow
, aMap
.GetColCount(),
1607 aMap
.GetRowCount() - firstDamagedRow
, aDamageArea
);
1610 void nsCellMap::ExpandWithCells(nsTableCellMap
& aMap
,
1611 nsTArray
<nsTableCellFrame
*>& aCellFrames
,
1612 int32_t aRowIndex
, int32_t aColIndex
,
1613 int32_t aRowSpan
, // same for all cells
1614 bool aRowSpanIsZero
, int32_t aRgFirstRowIndex
,
1615 TableArea
& aDamageArea
) {
1616 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1617 int32_t endRowIndex
= aRowIndex
+ aRowSpan
- 1;
1618 int32_t startColIndex
= aColIndex
;
1619 int32_t endColIndex
= aColIndex
;
1620 int32_t numCells
= aCellFrames
.Length();
1621 int32_t totalColSpan
= 0;
1623 // add cellData entries for the space taken up by the new cells
1624 for (int32_t cellX
= 0; cellX
< numCells
; cellX
++) {
1625 nsTableCellFrame
* cellFrame
= aCellFrames
.ElementAt(cellX
);
1626 CellData
* origData
= AllocCellData(cellFrame
); // the originating cell
1627 if (!origData
) return;
1629 // set the starting and ending col index for the new cell
1630 int32_t colSpan
= cellFrame
->GetColSpan();
1631 totalColSpan
+= colSpan
;
1633 endColIndex
= aColIndex
+ colSpan
- 1;
1635 startColIndex
= endColIndex
+ 1;
1636 endColIndex
= startColIndex
+ colSpan
- 1;
1639 // add the originating cell data and any cell data corresponding to row/col
1641 for (int32_t rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1642 CellDataArray
& row
= mRows
[rowX
];
1643 // Pre-allocate all the cells we'll need in this array, setting
1645 // Have to have the cast to get the template to do the right thing.
1646 int32_t insertionIndex
= row
.Length();
1647 if (insertionIndex
> startColIndex
) {
1648 insertionIndex
= startColIndex
;
1650 row
.InsertElementsAt(insertionIndex
, endColIndex
- insertionIndex
+ 1,
1651 (CellData
*)nullptr);
1653 for (int32_t colX
= startColIndex
; colX
<= endColIndex
; colX
++) {
1654 CellData
* data
= origData
;
1655 if ((rowX
!= aRowIndex
) || (colX
!= startColIndex
)) {
1656 data
= AllocCellData(nullptr);
1658 if (rowX
> aRowIndex
) {
1659 data
->SetRowSpanOffset(rowX
- aRowIndex
);
1660 if (aRowSpanIsZero
) {
1661 data
->SetZeroRowSpan(true);
1664 if (colX
> startColIndex
) {
1665 data
->SetColSpanOffset(colX
- startColIndex
);
1668 SetDataAt(aMap
, *data
, rowX
, colX
);
1671 cellFrame
->SetColIndex(startColIndex
);
1673 int32_t damageHeight
=
1674 std::min(GetRowGroup()->GetRowCount() - aRowIndex
, aRowSpan
);
1675 SetDamageArea(aColIndex
, aRgFirstRowIndex
+ aRowIndex
,
1676 1 + endColIndex
- aColIndex
, damageHeight
, aDamageArea
);
1680 // update the row and col info due to shifting
1681 for (rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1682 CellDataArray
& row
= mRows
[rowX
];
1683 uint32_t numCols
= row
.Length();
1685 for (colX
= aColIndex
+ totalColSpan
; colX
< numCols
; colX
++) {
1686 CellData
* data
= row
[colX
];
1688 // increase the origin and span counts beyond the spanned cols
1689 if (data
->IsOrig()) {
1690 // a cell that gets moved needs adjustment as well as it new
1692 data
->GetCellFrame()->SetColIndex(colX
);
1693 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1694 colInfo
->mNumCellsOrig
++;
1696 if (data
->IsColSpan()) {
1697 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1698 colInfo
->mNumCellsSpan
++;
1701 // decrease the origin and span counts within the spanned cols
1702 int32_t colX2
= colX
- totalColSpan
;
1703 nsColInfo
* colInfo2
= aMap
.GetColInfoAt(colX2
);
1704 if (data
->IsOrig()) {
1705 // the old originating col of a moved cell needs adjustment
1706 colInfo2
->mNumCellsOrig
--;
1708 if (data
->IsColSpan()) {
1709 colInfo2
->mNumCellsSpan
--;
1716 void nsCellMap::ShrinkWithoutRows(nsTableCellMap
& aMap
, int32_t aStartRowIndex
,
1717 int32_t aNumRowsToRemove
,
1718 int32_t aRgFirstRowIndex
,
1719 TableArea
& aDamageArea
) {
1720 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1721 int32_t endRowIndex
= aStartRowIndex
+ aNumRowsToRemove
- 1;
1722 uint32_t colCount
= aMap
.GetColCount();
1723 for (int32_t rowX
= endRowIndex
; rowX
>= aStartRowIndex
; --rowX
) {
1724 CellDataArray
& row
= mRows
[rowX
];
1726 for (colX
= 0; colX
< colCount
; colX
++) {
1727 CellData
* data
= row
.SafeElementAt(colX
);
1729 // Adjust the column counts.
1730 if (data
->IsOrig()) {
1731 // Decrement the column count.
1732 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1733 colInfo
->mNumCellsOrig
--;
1735 // colspan=0 is only counted as a spanned cell in the 1st col it spans
1736 else if (data
->IsColSpan()) {
1737 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1738 colInfo
->mNumCellsSpan
--;
1743 uint32_t rowLength
= row
.Length();
1744 // Delete our row information.
1745 for (colX
= 0; colX
< rowLength
; colX
++) {
1746 DestroyCellData(row
[colX
]);
1749 mRows
.RemoveElementAt(rowX
);
1751 // Decrement our row and next available index counts.
1754 aMap
.RemoveColsAtEnd();
1755 // mark all following rows damaged, they might contain a previously set
1756 // damage area which we can not shift.
1757 int32_t firstDamagedRow
= aRgFirstRowIndex
+ aStartRowIndex
;
1758 SetDamageArea(0, firstDamagedRow
, aMap
.GetColCount(),
1759 aMap
.GetRowCount() - firstDamagedRow
, aDamageArea
);
1762 int32_t nsCellMap::GetEffectiveColSpan(const nsTableCellMap
& aMap
,
1764 int32_t aColIndex
) const {
1765 int32_t numColsInTable
= aMap
.GetColCount();
1766 int32_t colSpan
= 1;
1767 if (uint32_t(aRowIndex
) >= mRows
.Length()) {
1771 const CellDataArray
& row
= mRows
[aRowIndex
];
1774 int32_t maxCols
= numColsInTable
;
1775 bool hitOverlap
= false; // XXX this is not ever being set to true
1776 for (colX
= aColIndex
+ 1; colX
< maxCols
; colX
++) {
1777 data
= row
.SafeElementAt(colX
);
1779 // for an overlapping situation get the colspan from the originating cell
1780 // and use that as the max number of cols to iterate. Since this is rare,
1781 // only pay the price of looking up the cell's colspan here.
1782 if (!hitOverlap
&& data
->IsOverlap()) {
1783 CellData
* origData
= row
.SafeElementAt(aColIndex
);
1784 if (origData
&& origData
->IsOrig()) {
1785 nsTableCellFrame
* cellFrame
= origData
->GetCellFrame();
1787 // possible change the number of colums to iterate
1788 maxCols
= std::min(aColIndex
+ cellFrame
->GetColSpan(), maxCols
);
1789 if (colX
>= maxCols
) break;
1793 if (data
->IsColSpan()) {
1804 int32_t nsCellMap::GetRowSpanForNewCell(nsTableCellFrame
* aCellFrameToAdd
,
1806 bool& aIsZeroRowSpan
) const {
1807 aIsZeroRowSpan
= false;
1808 int32_t rowSpan
= aCellFrameToAdd
->GetRowSpan();
1810 // Use a min value of 2 for a zero rowspan to make computations easier
1811 // elsewhere. Zero rowspans are only content dependent!
1812 rowSpan
= std::max(2, mContentRowCount
- aRowIndex
);
1813 aIsZeroRowSpan
= true;
1818 bool nsCellMap::HasMoreThanOneCell(int32_t aRowIndex
) const {
1819 const CellDataArray
& row
= mRows
.SafeElementAt(aRowIndex
, *sEmptyRow
);
1820 uint32_t maxColIndex
= row
.Length();
1822 bool foundOne
= false;
1823 for (colIndex
= 0; colIndex
< maxColIndex
; colIndex
++) {
1824 CellData
* cellData
= row
[colIndex
];
1825 if (cellData
&& (cellData
->GetCellFrame() || cellData
->IsRowSpan())) {
1835 int32_t nsCellMap::GetNumCellsOriginatingInRow(int32_t aRowIndex
) const {
1836 const CellDataArray
& row
= mRows
.SafeElementAt(aRowIndex
, *sEmptyRow
);
1838 uint32_t maxColIndex
= row
.Length();
1840 for (colIndex
= 0; colIndex
< maxColIndex
; colIndex
++) {
1841 CellData
* cellData
= row
[colIndex
];
1842 if (cellData
&& cellData
->IsOrig()) count
++;
1847 int32_t nsCellMap::GetRowSpan(int32_t aRowIndex
, int32_t aColIndex
,
1848 bool aGetEffective
) const {
1849 int32_t rowSpan
= 1;
1850 int32_t rowCount
= (aGetEffective
) ? mContentRowCount
: mRows
.Length();
1852 for (rowX
= aRowIndex
+ 1; rowX
< rowCount
; rowX
++) {
1853 CellData
* data
= GetDataAt(rowX
, aColIndex
);
1855 if (data
->IsRowSpan()) {
1866 void nsCellMap::ShrinkWithoutCell(nsTableCellMap
& aMap
,
1867 nsTableCellFrame
& aCellFrame
,
1868 int32_t aRowIndex
, int32_t aColIndex
,
1869 int32_t aRgFirstRowIndex
,
1870 TableArea
& aDamageArea
) {
1871 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1872 uint32_t colX
, rowX
;
1874 // get the rowspan and colspan from the cell map since the content may have
1876 int32_t rowSpan
= GetRowSpan(aRowIndex
, aColIndex
, true);
1877 uint32_t colSpan
= GetEffectiveColSpan(aMap
, aRowIndex
, aColIndex
);
1878 uint32_t endRowIndex
= aRowIndex
+ rowSpan
- 1;
1879 uint32_t endColIndex
= aColIndex
+ colSpan
- 1;
1881 // adjust the col counts due to the deleted cell before removing it
1882 for (colX
= aColIndex
; colX
<= endColIndex
; colX
++) {
1883 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1884 if (colX
== uint32_t(aColIndex
)) {
1885 colInfo
->mNumCellsOrig
--;
1887 colInfo
->mNumCellsSpan
--;
1891 // remove the deleted cell and cellData entries for it
1892 for (rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1893 CellDataArray
& row
= mRows
[rowX
];
1895 // endIndexForRow points at the first slot we don't want to clean up. This
1896 // makes the aColIndex == 0 case work right with our unsigned int colX.
1897 NS_ASSERTION(endColIndex
+ 1 <= row
.Length(), "span beyond the row size!");
1898 uint32_t endIndexForRow
= std::min(endColIndex
+ 1, uint32_t(row
.Length()));
1900 // Since endIndexForRow <= row.Length(), enough to compare aColIndex to it.
1901 if (uint32_t(aColIndex
) < endIndexForRow
) {
1902 for (colX
= endIndexForRow
; colX
> uint32_t(aColIndex
); colX
--) {
1903 DestroyCellData(row
[colX
- 1]);
1905 row
.RemoveElementsAt(aColIndex
, endIndexForRow
- aColIndex
);
1909 uint32_t numCols
= aMap
.GetColCount();
1911 // update the row and col info due to shifting
1912 for (rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1913 CellDataArray
& row
= mRows
[rowX
];
1914 for (colX
= aColIndex
; colX
< numCols
- colSpan
; colX
++) {
1915 CellData
* data
= row
.SafeElementAt(colX
);
1917 if (data
->IsOrig()) {
1918 // a cell that gets moved to the left needs adjustment in its new
1920 data
->GetCellFrame()->SetColIndex(colX
);
1921 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1922 colInfo
->mNumCellsOrig
++;
1923 // a cell that gets moved to the left needs adjustment in its old
1925 colInfo
= aMap
.GetColInfoAt(colX
+ colSpan
);
1927 colInfo
->mNumCellsOrig
--;
1931 else if (data
->IsColSpan()) {
1932 // a cell that gets moved to the left needs adjustment
1933 // in its new location
1934 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1935 colInfo
->mNumCellsSpan
++;
1936 // a cell that gets moved to the left needs adjustment
1937 // in its old location
1938 colInfo
= aMap
.GetColInfoAt(colX
+ colSpan
);
1940 colInfo
->mNumCellsSpan
--;
1946 aMap
.RemoveColsAtEnd();
1947 SetDamageArea(aColIndex
, aRgFirstRowIndex
+ aRowIndex
,
1948 std::max(0, aMap
.GetColCount() - aColIndex
- 1),
1949 1 + endRowIndex
- aRowIndex
, aDamageArea
);
1952 void nsCellMap::RebuildConsideringRows(
1953 nsTableCellMap
& aMap
, int32_t aStartRowIndex
,
1954 nsTArray
<nsTableRowFrame
*>* aRowsToInsert
, int32_t aNumRowsToRemove
) {
1955 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1956 // copy the old cell map into a new array
1957 uint32_t numOrigRows
= mRows
.Length();
1958 nsTArray
<CellDataArray
> origRows
= std::move(mRows
);
1960 int32_t rowNumberChange
;
1961 if (aRowsToInsert
) {
1962 rowNumberChange
= aRowsToInsert
->Length();
1964 rowNumberChange
= -aNumRowsToRemove
;
1967 // adjust mContentRowCount based on the function arguments as they are known
1969 mContentRowCount
+= rowNumberChange
;
1970 NS_ASSERTION(mContentRowCount
>= 0, "previous mContentRowCount was wrong");
1971 // mRows is empty now. Grow it to the size we expect it to have.
1972 if (mContentRowCount
) {
1973 if (!Grow(aMap
, mContentRowCount
)) {
1974 // Bail, I guess... Not sure what else we can do here.
1979 // aStartRowIndex might be after all existing rows so we should limit the
1980 // copy to the amount of exisiting rows
1981 uint32_t copyEndRowIndex
= std::min(numOrigRows
, uint32_t(aStartRowIndex
));
1983 // rowX keeps track of where we are in mRows while setting up the
1986 TableArea damageArea
;
1987 // put back the rows before the affected ones just as before. Note that we
1988 // can't just copy the old rows in bit-for-bit, because they might be
1989 // spanning out into the rows we're adding/removing.
1990 for (; rowX
< copyEndRowIndex
; rowX
++) {
1991 const CellDataArray
& row
= origRows
[rowX
];
1992 uint32_t numCols
= row
.Length();
1993 for (uint32_t colX
= 0; colX
< numCols
; colX
++) {
1994 // put in the original cell from the cell map
1995 const CellData
* data
= row
.ElementAt(colX
);
1996 if (data
&& data
->IsOrig()) {
1997 AppendCell(aMap
, data
->GetCellFrame(), rowX
, false, 0, damageArea
);
2002 // Now handle the new rows being inserted, if any.
2003 uint32_t copyStartRowIndex
;
2004 rowX
= aStartRowIndex
;
2005 if (aRowsToInsert
) {
2006 // add in the new cells and create rows if necessary
2007 int32_t numNewRows
= aRowsToInsert
->Length();
2008 for (int32_t newRowX
= 0; newRowX
< numNewRows
; newRowX
++) {
2009 nsTableRowFrame
* rFrame
= aRowsToInsert
->ElementAt(newRowX
);
2010 for (nsTableCellFrame
* cellFrame
= rFrame
->GetFirstCell(); cellFrame
;
2011 cellFrame
= cellFrame
->GetNextCell()) {
2012 AppendCell(aMap
, cellFrame
, rowX
, false, 0, damageArea
);
2016 copyStartRowIndex
= aStartRowIndex
;
2018 copyStartRowIndex
= aStartRowIndex
+ aNumRowsToRemove
;
2021 // put back the rows after the affected ones just as before. Again, we can't
2022 // just copy the old bits because that would not handle the new rows spanning
2023 // out or our earlier old rows spanning through the damaged area.
2024 for (uint32_t copyRowX
= copyStartRowIndex
; copyRowX
< numOrigRows
;
2026 const CellDataArray
& row
= origRows
[copyRowX
];
2027 uint32_t numCols
= row
.Length();
2028 for (uint32_t colX
= 0; colX
< numCols
; colX
++) {
2029 // put in the original cell from the cell map
2030 CellData
* data
= row
.ElementAt(colX
);
2031 if (data
&& data
->IsOrig()) {
2032 AppendCell(aMap
, data
->GetCellFrame(), rowX
, false, 0, damageArea
);
2038 // delete the old cell map. Now rowX no longer has anything to do with mRows
2039 for (rowX
= 0; rowX
< numOrigRows
; rowX
++) {
2040 CellDataArray
& row
= origRows
[rowX
];
2041 uint32_t len
= row
.Length();
2042 for (uint32_t colX
= 0; colX
< len
; colX
++) {
2043 DestroyCellData(row
[colX
]);
2048 void nsCellMap::RebuildConsideringCells(
2049 nsTableCellMap
& aMap
, int32_t aNumOrigCols
,
2050 nsTArray
<nsTableCellFrame
*>* aCellFrames
, int32_t aRowIndex
,
2051 int32_t aColIndex
, bool aInsert
) {
2052 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
2053 // copy the old cell map into a new array
2054 int32_t numOrigRows
= mRows
.Length();
2055 nsTArray
<CellDataArray
> origRows
= std::move(mRows
);
2057 int32_t numNewCells
= (aCellFrames
) ? aCellFrames
->Length() : 0;
2059 // the new cells might extend the previous column number
2060 NS_ASSERTION(aNumOrigCols
>= aColIndex
,
2061 "Appending cells far beyond cellmap data?!");
2063 aInsert
? std::max(aNumOrigCols
, aColIndex
+ 1) : aNumOrigCols
;
2065 // build the new cell map. Hard to say what, if anything, we can preallocate
2066 // here... Should come back to that sometime, perhaps.
2068 TableArea damageArea
;
2069 for (rowX
= 0; rowX
< numOrigRows
; rowX
++) {
2070 const CellDataArray
& row
= origRows
[rowX
];
2071 for (int32_t colX
= 0; colX
< numCols
; colX
++) {
2072 if ((rowX
== aRowIndex
) && (colX
== aColIndex
)) {
2073 if (aInsert
) { // put in the new cells
2074 for (int32_t cellX
= 0; cellX
< numNewCells
; cellX
++) {
2075 nsTableCellFrame
* cell
= aCellFrames
->ElementAt(cellX
);
2077 AppendCell(aMap
, cell
, rowX
, false, 0, damageArea
);
2081 continue; // do not put the deleted cell back
2084 // put in the original cell from the cell map
2085 CellData
* data
= row
.SafeElementAt(colX
);
2086 if (data
&& data
->IsOrig()) {
2087 AppendCell(aMap
, data
->GetCellFrame(), rowX
, false, 0, damageArea
);
2093 aRowIndex
) { // append the new cells below the last original row
2094 NS_ASSERTION(numOrigRows
== aRowIndex
,
2095 "Appending cells far beyond the last row");
2096 for (int32_t cellX
= 0; cellX
< numNewCells
; cellX
++) {
2097 nsTableCellFrame
* cell
= aCellFrames
->ElementAt(cellX
);
2099 AppendCell(aMap
, cell
, aRowIndex
, false, 0, damageArea
);
2104 // delete the old cell map
2105 for (rowX
= 0; rowX
< numOrigRows
; rowX
++) {
2106 CellDataArray
& row
= origRows
[rowX
];
2107 uint32_t len
= row
.Length();
2108 for (uint32_t colX
= 0; colX
< len
; colX
++) {
2109 DestroyCellData(row
.SafeElementAt(colX
));
2112 // expand the cellmap to cover empty content rows
2113 if (mRows
.Length() < uint32_t(mContentRowCount
)) {
2114 Grow(aMap
, mContentRowCount
- mRows
.Length());
2118 void nsCellMap::RemoveCell(nsTableCellMap
& aMap
, nsTableCellFrame
* aCellFrame
,
2119 int32_t aRowIndex
, int32_t aRgFirstRowIndex
,
2120 TableArea
& aDamageArea
) {
2121 uint32_t numRows
= mRows
.Length();
2122 if (uint32_t(aRowIndex
) >= numRows
) {
2123 NS_ERROR("bad arg in nsCellMap::RemoveCell");
2126 int32_t numCols
= aMap
.GetColCount();
2128 // Now aRowIndex is guaranteed OK.
2130 // get the starting col index of the cell to remove
2131 int32_t startColIndex
;
2132 for (startColIndex
= 0; startColIndex
< numCols
; startColIndex
++) {
2133 CellData
* data
= mRows
[aRowIndex
].SafeElementAt(startColIndex
);
2134 if (data
&& (data
->IsOrig()) && (aCellFrame
== data
->GetCellFrame())) {
2135 break; // we found the col index
2139 int32_t rowSpan
= GetRowSpan(aRowIndex
, startColIndex
, false);
2140 // record whether removing the cells is going to cause complications due
2141 // to existing row spans, col spans or table sizing.
2142 bool spansCauseRebuild
= CellsSpanInOrOut(aRowIndex
, aRowIndex
+ rowSpan
- 1,
2143 startColIndex
, numCols
- 1);
2144 // XXX if the cell has a col span to the end of the map, and the end has no
2145 // originating cells, we need to assume that this the only such cell, and
2146 // rebuild so that there are no extraneous cols at the end. The same is true
2147 // for removing rows.
2148 if (!spansCauseRebuild
) {
2149 if (!aCellFrame
->GetRowSpan() || !aCellFrame
->GetColSpan()) {
2150 spansCauseRebuild
= true;
2154 if (spansCauseRebuild
) {
2155 aMap
.RebuildConsideringCells(this, nullptr, aRowIndex
, startColIndex
, false,
2158 ShrinkWithoutCell(aMap
, *aCellFrame
, aRowIndex
, startColIndex
,
2159 aRgFirstRowIndex
, aDamageArea
);
2164 void nsCellMap::Dump(bool aIsBorderCollapse
) const {
2165 printf("\n ***** START GROUP CELL MAP DUMP ***** %p\n", (void*)this);
2166 nsTableRowGroupFrame
* rg
= GetRowGroup();
2167 const nsStyleDisplay
* display
= rg
->StyleDisplay();
2168 switch (display
->DisplayInside()) {
2169 case StyleDisplayInside::TableHeaderGroup
:
2172 case StyleDisplayInside::TableFooterGroup
:
2175 case StyleDisplayInside::TableRowGroup
:
2179 printf("HUH? wrong display type on rowgroup");
2181 uint32_t mapRowCount
= mRows
.Length();
2182 printf("mapRowCount=%u tableRowCount=%d\n", mapRowCount
, mContentRowCount
);
2184 uint32_t rowIndex
, colIndex
;
2185 for (rowIndex
= 0; rowIndex
< mapRowCount
; rowIndex
++) {
2186 const CellDataArray
& row
= mRows
[rowIndex
];
2187 printf(" row %d : ", rowIndex
);
2188 uint32_t colCount
= row
.Length();
2189 for (colIndex
= 0; colIndex
< colCount
; colIndex
++) {
2190 CellData
* cd
= row
[colIndex
];
2193 printf("C%d,%d ", rowIndex
, colIndex
);
2195 if (cd
->IsRowSpan()) {
2198 if (cd
->IsColSpan()) {
2201 if (!(cd
->IsRowSpan() && cd
->IsColSpan())) {
2210 if (aIsBorderCollapse
) {
2212 BCBorderOwner owner
;
2216 for (int32_t i
= 0; i
<= 2; i
++) {
2218 for (colIndex
= 0; colIndex
< colCount
; colIndex
++) {
2219 BCCellData
* cd
= (BCCellData
*)row
[colIndex
];
2222 size
= cd
->mData
.GetBStartEdge(owner
, segStart
);
2223 printf("t=%d%d%d ", int32_t(size
), owner
, segStart
);
2224 } else if (1 == i
) {
2225 size
= cd
->mData
.GetIStartEdge(owner
, segStart
);
2226 printf("l=%d%d%d ", int32_t(size
), owner
, segStart
);
2228 size
= cd
->mData
.GetCorner(side
, bevel
);
2229 printf("c=%d%hhu%d ", int32_t(size
), static_cast<uint8_t>(side
),
2239 // output info mapping Ci,j to cell address
2240 for (uint32_t rIndex
= 0; rIndex
< mapRowCount
; rIndex
++) {
2241 const CellDataArray
& row
= mRows
[rIndex
];
2242 uint32_t colCount
= row
.Length();
2244 for (colIndex
= 0; colIndex
< colCount
; colIndex
++) {
2245 CellData
* cd
= row
[colIndex
];
2248 nsTableCellFrame
* cellFrame
= cd
->GetCellFrame();
2249 uint32_t cellFrameColIndex
= cellFrame
->ColIndex();
2250 printf("C%d,%d=%p(%u) ", rIndex
, colIndex
, (void*)cellFrame
,
2258 printf(" ***** END GROUP CELL MAP DUMP *****\n");
2262 CellData
* nsCellMap::GetDataAt(int32_t aMapRowIndex
, int32_t aColIndex
) const {
2263 return mRows
.SafeElementAt(aMapRowIndex
, *sEmptyRow
).SafeElementAt(aColIndex
);
2266 // only called if the cell at aMapRowIndex, aColIndex is null or dead
2267 // (the latter from ExpandZeroColSpans (XXXmats which has now been removed -
2268 // are there other ways cells may be dead?)).
2269 void nsCellMap::SetDataAt(nsTableCellMap
& aMap
, CellData
& aNewCell
,
2270 int32_t aMapRowIndex
, int32_t aColIndex
) {
2271 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
2272 if (uint32_t(aMapRowIndex
) >= mRows
.Length()) {
2273 NS_ERROR("SetDataAt called with row index > num rows");
2277 CellDataArray
& row
= mRows
[aMapRowIndex
];
2279 // the table map may need cols added
2280 int32_t numColsToAdd
= aColIndex
+ 1 - aMap
.GetColCount();
2281 if (numColsToAdd
> 0) {
2282 aMap
.AddColsAtEnd(numColsToAdd
);
2284 // the row may need cols added
2285 numColsToAdd
= aColIndex
+ 1 - row
.Length();
2286 if (numColsToAdd
> 0) {
2287 // XXXbz need to handle allocation failures.
2288 GrowRow(row
, numColsToAdd
);
2291 DestroyCellData(row
[aColIndex
]);
2293 row
.ReplaceElementsAt(aColIndex
, 1, &aNewCell
);
2294 // update the originating cell counts if cell originates in this row, col
2295 nsColInfo
* colInfo
= aMap
.GetColInfoAt(aColIndex
);
2297 if (aNewCell
.IsOrig()) {
2298 colInfo
->mNumCellsOrig
++;
2299 } else if (aNewCell
.IsColSpan()) {
2300 colInfo
->mNumCellsSpan
++;
2303 NS_ERROR("SetDataAt called with col index > table map num cols");
2306 nsTableCellFrame
* nsCellMap::GetCellInfoAt(const nsTableCellMap
& aMap
,
2307 int32_t aRowX
, int32_t aColX
,
2309 int32_t* aColSpan
) const {
2311 *aOriginates
= false;
2313 CellData
* data
= GetDataAt(aRowX
, aColX
);
2314 nsTableCellFrame
* cellFrame
= nullptr;
2316 if (data
->IsOrig()) {
2317 cellFrame
= data
->GetCellFrame();
2318 if (aOriginates
) *aOriginates
= true;
2320 cellFrame
= GetCellFrame(aRowX
, aColX
, *data
, true);
2322 if (cellFrame
&& aColSpan
) {
2323 uint32_t initialColIndex
= cellFrame
->ColIndex();
2324 *aColSpan
= GetEffectiveColSpan(aMap
, aRowX
, initialColIndex
);
2330 bool nsCellMap::RowIsSpannedInto(int32_t aRowIndex
, int32_t aNumEffCols
) const {
2331 if ((0 > aRowIndex
) || (aRowIndex
>= mContentRowCount
)) {
2334 for (int32_t colIndex
= 0; colIndex
< aNumEffCols
; colIndex
++) {
2335 CellData
* cd
= GetDataAt(aRowIndex
, colIndex
);
2336 if (cd
) { // there's really a cell at (aRowIndex, colIndex)
2337 if (cd
->IsSpan()) { // the cell at (aRowIndex, colIndex) is the result of
2339 if (cd
->IsRowSpan() && GetCellFrame(aRowIndex
, colIndex
, *cd
,
2340 true)) { // XXX why the last check
2349 bool nsCellMap::RowHasSpanningCells(int32_t aRowIndex
,
2350 int32_t aNumEffCols
) const {
2351 if ((0 > aRowIndex
) || (aRowIndex
>= mContentRowCount
)) {
2354 if (aRowIndex
!= mContentRowCount
- 1) {
2355 // aRowIndex is not the last row, so we check the next row after aRowIndex
2357 for (int32_t colIndex
= 0; colIndex
< aNumEffCols
; colIndex
++) {
2358 CellData
* cd
= GetDataAt(aRowIndex
, colIndex
);
2359 if (cd
&& (cd
->IsOrig())) { // cell originates
2360 CellData
* cd2
= GetDataAt(aRowIndex
+ 1, colIndex
);
2361 if (cd2
&& cd2
->IsRowSpan()) { // cd2 is spanned by a row
2362 if (cd
->GetCellFrame() ==
2363 GetCellFrame(aRowIndex
+ 1, colIndex
, *cd2
, true)) {
2373 void nsCellMap::DestroyCellData(CellData
* aData
) {
2379 BCCellData
* bcData
= static_cast<BCCellData
*>(aData
);
2380 bcData
->~BCCellData();
2381 mPresContext
->PresShell()->FreeByObjectID(eArenaObjectID_BCCellData
,
2385 mPresContext
->PresShell()->FreeByObjectID(eArenaObjectID_CellData
, aData
);
2389 CellData
* nsCellMap::AllocCellData(nsTableCellFrame
* aOrigCell
) {
2392 (BCCellData
*)mPresContext
->PresShell()->AllocateByObjectID(
2393 eArenaObjectID_BCCellData
, sizeof(BCCellData
));
2395 new (data
) BCCellData(aOrigCell
);
2400 CellData
* data
= (CellData
*)mPresContext
->PresShell()->AllocateByObjectID(
2401 eArenaObjectID_CellData
, sizeof(CellData
));
2403 new (data
) CellData(aOrigCell
);
2408 void nsCellMapColumnIterator::AdvanceRowGroup() {
2410 mCurMapStart
+= mCurMapContentRowCount
;
2411 mCurMap
= mCurMap
->GetNextSibling();
2413 // Set mCurMapContentRowCount and mCurMapRelevantRowCount to 0 in case
2414 // mCurMap has no next sibling. This can happen if we just handled the
2415 // last originating cell. Future calls will end up with mFoundCells ==
2416 // mOrigCells, but for this one mFoundCells was definitely not big enough
2418 mCurMapContentRowCount
= 0;
2419 mCurMapRelevantRowCount
= 0;
2423 mCurMapContentRowCount
= mCurMap
->GetRowCount();
2424 uint32_t rowArrayLength
= mCurMap
->mRows
.Length();
2425 mCurMapRelevantRowCount
= std::min(mCurMapContentRowCount
, rowArrayLength
);
2426 } while (0 == mCurMapRelevantRowCount
);
2428 NS_ASSERTION(mCurMapRelevantRowCount
!= 0 || !mCurMap
,
2429 "How did that happen?");
2431 // Set mCurMapRow to 0, since cells can't span across table row groups.
2435 void nsCellMapColumnIterator::IncrementRow(int32_t aIncrement
) {
2436 MOZ_ASSERT(aIncrement
>= 0, "Bogus increment");
2437 MOZ_ASSERT(mCurMap
, "Bogus mOrigCells?");
2438 if (aIncrement
== 0) {
2441 mCurMapRow
+= aIncrement
;
2442 if (mCurMapRow
>= mCurMapRelevantRowCount
) {
2448 nsTableCellFrame
* nsCellMapColumnIterator::GetNextFrame(int32_t* aRow
,
2449 int32_t* aColSpan
) {
2450 // Fast-path for the case when we don't have anything left in the column and
2452 if (mFoundCells
== mOrigCells
) {
2459 NS_ASSERTION(mCurMapRow
< mCurMapRelevantRowCount
, "Bogus mOrigCells?");
2460 // Safe to just get the row (which is faster than calling GetDataAt(), but
2461 // there may not be that many cells in it, so have to use SafeElementAt for
2463 const nsCellMap::CellDataArray
& row
= mCurMap
->mRows
[mCurMapRow
];
2464 CellData
* cellData
= row
.SafeElementAt(mCol
);
2465 if (!cellData
|| cellData
->IsDead()) {
2466 // Could hit this if there are fewer cells in this row than others, for
2472 if (cellData
->IsColSpan()) {
2473 // Look up the originating data for this cell, advance by its relative
2475 int32_t rowspanOffset
= cellData
->GetRowSpanOffset();
2476 nsTableCellFrame
* cellFrame
=
2477 mCurMap
->GetCellFrame(mCurMapRow
, mCol
, *cellData
, false);
2478 NS_ASSERTION(cellFrame
, "Must have usable originating data here");
2479 int32_t rowSpan
= cellFrame
->GetRowSpan();
2483 IncrementRow(rowSpan
- rowspanOffset
);
2488 NS_ASSERTION(cellData
->IsOrig(),
2489 "Must have originating cellData by this point. "
2490 "See comment on mCurMapRow in header.");
2492 nsTableCellFrame
* cellFrame
= cellData
->GetCellFrame();
2493 NS_ASSERTION(cellFrame
, "Orig data without cellframe?");
2495 *aRow
= mCurMapStart
+ mCurMapRow
;
2496 *aColSpan
= mCurMap
->GetEffectiveColSpan(*mMap
, mCurMapRow
, mCol
);
2498 IncrementRow(cellFrame
->GetRowSpan());
2502 MOZ_ASSERT(cellData
== mMap
->GetDataAt(*aRow
, mCol
),
2503 "Giving caller bogus row?");
2508 MOZ_ASSERT_UNREACHABLE("Can't get here");