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%X%d ", int32_t(size
), side
, bevel
);
684 BCData
& cd
= mBCInfo
->mBEndIEndCorner
;
686 size
= cd
.GetBStartEdge(owner
, segStart
);
687 printf("t=%d%X%d ", int32_t(size
), owner
, segStart
);
689 size
= cd
.GetIStartEdge(owner
, segStart
);
690 printf("l=%d%X%d ", int32_t(size
), owner
, segStart
);
692 size
= cd
.GetCorner(side
, bevel
);
693 printf("c=%d%X%d ", int32_t(size
), side
, bevel
);
698 printf("***** END TABLE CELL MAP DUMP *****\n");
702 nsTableCellFrame
* nsTableCellMap::GetCellInfoAt(int32_t aRowIndex
,
705 int32_t* aColSpan
) const {
706 int32_t rowIndex
= aRowIndex
;
707 nsCellMap
* cellMap
= mFirstMap
;
709 if (cellMap
->GetRowCount() > rowIndex
) {
710 return cellMap
->GetCellInfoAt(*this, rowIndex
, aColIndex
, aOriginates
,
713 rowIndex
-= cellMap
->GetRowCount();
714 cellMap
= cellMap
->GetNextSibling();
719 int32_t nsTableCellMap::GetIndexByRowAndColumn(int32_t aRow
,
720 int32_t aColumn
) const {
723 int32_t colCount
= mCols
.Length();
724 int32_t rowIndex
= aRow
;
726 nsCellMap
* cellMap
= mFirstMap
;
728 int32_t rowCount
= cellMap
->GetRowCount();
729 if (rowIndex
>= rowCount
) {
730 // If the rowCount is less than the rowIndex, this means that the index is
731 // not within the current map. If so, get the index of the last cell in
733 rowIndex
-= rowCount
;
735 int32_t cellMapIdx
= cellMap
->GetHighestIndex(colCount
);
736 if (cellMapIdx
!= -1) index
+= cellMapIdx
+ 1;
739 // Index is in valid range for this cellmap, so get the index of rowIndex
742 cellMap
->GetIndexByRowAndColumn(colCount
, rowIndex
, aColumn
);
743 if (cellMapIdx
== -1) return -1; // no cell at the given row and column.
746 return index
; // no need to look through further maps here
749 cellMap
= cellMap
->GetNextSibling();
755 void nsTableCellMap::GetRowAndColumnByIndex(int32_t aIndex
, int32_t* aRow
,
756 int32_t* aColumn
) const {
760 int32_t colCount
= mCols
.Length();
762 int32_t previousRows
= 0;
763 int32_t index
= aIndex
;
765 nsCellMap
* cellMap
= mFirstMap
;
767 int32_t rowCount
= cellMap
->GetRowCount();
768 // Determine the highest possible index in this map to see
769 // if wanted index is in here.
770 int32_t cellMapIdx
= cellMap
->GetHighestIndex(colCount
);
771 if (cellMapIdx
== -1) {
772 // The index is not within this map, increase the total row index
774 previousRows
+= rowCount
;
776 if (index
> cellMapIdx
) {
777 // The index is not within this map, so decrease it by the cellMapIdx
778 // determined index and increase the total row index accordingly.
779 index
-= cellMapIdx
+ 1;
780 previousRows
+= rowCount
;
782 cellMap
->GetRowAndColumnByIndex(colCount
, index
, aRow
, aColumn
);
783 // If there were previous indexes, take them into account.
784 *aRow
+= previousRows
;
785 return; // no need to look any further.
789 cellMap
= cellMap
->GetNextSibling();
793 bool nsTableCellMap::RowIsSpannedInto(int32_t aRowIndex
,
794 int32_t aNumEffCols
) const {
795 int32_t rowIndex
= aRowIndex
;
796 nsCellMap
* cellMap
= mFirstMap
;
798 if (cellMap
->GetRowCount() > rowIndex
) {
799 return cellMap
->RowIsSpannedInto(rowIndex
, aNumEffCols
);
801 rowIndex
-= cellMap
->GetRowCount();
802 cellMap
= cellMap
->GetNextSibling();
807 bool nsTableCellMap::RowHasSpanningCells(int32_t aRowIndex
,
808 int32_t aNumEffCols
) const {
809 int32_t rowIndex
= aRowIndex
;
810 nsCellMap
* cellMap
= mFirstMap
;
812 if (cellMap
->GetRowCount() > rowIndex
) {
813 return cellMap
->RowHasSpanningCells(rowIndex
, aNumEffCols
);
815 rowIndex
-= cellMap
->GetRowCount();
816 cellMap
= cellMap
->GetNextSibling();
821 // FIXME: The only value callers pass for aSide is eLogicalSideBEnd.
822 // Consider removing support for the other three values.
823 void nsTableCellMap::ResetBStartStart(LogicalSide aSide
, nsCellMap
& aCellMap
,
824 uint32_t aRowGroupStart
,
825 uint32_t aRowIndex
, uint32_t aColIndex
) {
826 if (!mBCInfo
) ABORT0();
828 BCCellData
* cellData
;
829 BCData
* bcData
= nullptr;
832 case eLogicalSideBEnd
:
835 case eLogicalSideBStart
:
836 cellData
= (BCCellData
*)aCellMap
.GetDataAt(aRowIndex
- aRowGroupStart
,
839 bcData
= &cellData
->mData
;
841 NS_ASSERTION(aSide
== eLogicalSideBEnd
, "program error");
842 // try the next row group
843 nsCellMap
* cellMap
= aCellMap
.GetNextSibling();
845 cellData
= (BCCellData
*)cellMap
->GetDataAt(0, aColIndex
);
847 bcData
= &cellData
->mData
;
849 bcData
= GetBEndMostBorder(aColIndex
);
854 case eLogicalSideIEnd
:
857 case eLogicalSideIStart
:
858 cellData
= (BCCellData
*)aCellMap
.GetDataAt(aRowIndex
- aRowGroupStart
,
861 bcData
= &cellData
->mData
;
863 NS_ASSERTION(aSide
== eLogicalSideIEnd
, "program error");
864 bcData
= GetIEndMostBorder(aRowIndex
);
869 bcData
->SetBStartStart(false);
873 // store the aSide border segment at coord = (aRowIndex, aColIndex). For
874 // bStart/iStart, store the info at coord. For bEnd/iEnd store it at the
875 // adjacent location so that it is bStart/iStart at that location. If the new
876 // location is at the iEnd or bEnd edge of the table, then store it one of the
877 // special arrays (iEnd-most borders, bEnd-most borders).
878 void nsTableCellMap::SetBCBorderEdge(LogicalSide aSide
, nsCellMap
& aCellMap
,
879 uint32_t aCellMapStart
, uint32_t aRowIndex
,
880 uint32_t aColIndex
, uint32_t aLength
,
881 BCBorderOwner aOwner
, nscoord aSize
,
883 if (!mBCInfo
) ABORT0();
885 BCCellData
* cellData
;
886 int32_t lastIndex
, xIndex
, yIndex
;
887 int32_t xPos
= aColIndex
;
888 int32_t yPos
= aRowIndex
;
889 int32_t rgYPos
= aRowIndex
- aCellMapStart
;
893 case eLogicalSideBEnd
:
897 case eLogicalSideBStart
:
898 lastIndex
= xPos
+ aLength
- 1;
899 for (xIndex
= xPos
; xIndex
<= lastIndex
; xIndex
++) {
900 changed
= aChanged
&& (xIndex
== xPos
);
901 BCData
* bcData
= nullptr;
902 cellData
= (BCCellData
*)aCellMap
.GetDataAt(rgYPos
, xIndex
);
904 int32_t numRgRows
= aCellMap
.GetRowCount();
905 if (yPos
< numRgRows
) { // add a dead cell data
906 TableArea damageArea
;
907 cellData
= (BCCellData
*)aCellMap
.AppendCell(*this, nullptr, rgYPos
,
908 false, 0, damageArea
);
909 if (!cellData
) ABORT0();
911 NS_ASSERTION(aSide
== eLogicalSideBEnd
, "program error");
912 // try the next non empty row group
913 nsCellMap
* cellMap
= aCellMap
.GetNextSibling();
914 while (cellMap
&& (0 == cellMap
->GetRowCount())) {
915 cellMap
= cellMap
->GetNextSibling();
918 cellData
= (BCCellData
*)cellMap
->GetDataAt(0, xIndex
);
919 if (!cellData
) { // add a dead cell
920 TableArea damageArea
;
921 cellData
= (BCCellData
*)cellMap
->AppendCell(
922 *this, nullptr, 0, false, 0, damageArea
);
924 } else { // must be at the end of the table
925 bcData
= GetBEndMostBorder(xIndex
);
929 if (!bcData
&& cellData
) {
930 bcData
= &cellData
->mData
;
933 bcData
->SetBStartEdge(aOwner
, aSize
, changed
);
935 NS_ERROR("Cellmap: BStart edge not found");
938 case eLogicalSideIEnd
:
941 case eLogicalSideIStart
:
942 // since bStart, bEnd borders were set, there should already be a cellData
944 lastIndex
= rgYPos
+ aLength
- 1;
945 for (yIndex
= rgYPos
; yIndex
<= lastIndex
; yIndex
++) {
946 changed
= aChanged
&& (yIndex
== rgYPos
);
947 cellData
= (BCCellData
*)aCellMap
.GetDataAt(yIndex
, xPos
);
949 cellData
->mData
.SetIStartEdge(aOwner
, aSize
, changed
);
951 NS_ASSERTION(aSide
== eLogicalSideIEnd
, "program error");
952 BCData
* bcData
= GetIEndMostBorder(yIndex
+ aCellMapStart
);
954 bcData
->SetIStartEdge(aOwner
, aSize
, changed
);
956 NS_ERROR("Cellmap: IStart edge not found");
963 // store corner info (aOwner, aSubSize, aBevel). For aCorner = eBStartIStart,
964 // store the info at (aRowIndex, aColIndex). For eBStartIEnd, store it in the
965 // entry to the iEnd-wards where it would be BStartIStart. For eBEndIEnd, store
966 // it in the entry to the bEnd-wards. etc.
967 void nsTableCellMap::SetBCBorderCorner(LogicalCorner aCorner
,
969 uint32_t aCellMapStart
,
970 uint32_t aRowIndex
, uint32_t aColIndex
,
971 LogicalSide aOwner
, nscoord aSubSize
,
972 bool aBevel
, bool aIsBEndIEnd
) {
973 if (!mBCInfo
) ABORT0();
976 mBCInfo
->mBEndIEndCorner
.SetCorner(aSubSize
, aOwner
, aBevel
);
980 int32_t xPos
= aColIndex
;
981 int32_t yPos
= aRowIndex
;
982 int32_t rgYPos
= aRowIndex
- aCellMapStart
;
984 if (eLogicalCornerBStartIEnd
== aCorner
) {
986 } else if (eLogicalCornerBEndIEnd
== aCorner
) {
990 } else if (eLogicalCornerBEndIStart
== aCorner
) {
995 BCCellData
* cellData
= nullptr;
996 BCData
* bcData
= nullptr;
997 if (GetColCount() <= xPos
) {
998 NS_ASSERTION(xPos
== GetColCount(), "program error");
999 // at the iEnd edge of the table as we checked the corner before
1000 NS_ASSERTION(!aIsBEndIEnd
, "should be handled before");
1001 bcData
= GetIEndMostBorder(yPos
);
1003 cellData
= (BCCellData
*)aCellMap
.GetDataAt(rgYPos
, xPos
);
1005 int32_t numRgRows
= aCellMap
.GetRowCount();
1006 if (yPos
< numRgRows
) { // add a dead cell data
1007 TableArea damageArea
;
1008 cellData
= (BCCellData
*)aCellMap
.AppendCell(*this, nullptr, rgYPos
,
1009 false, 0, damageArea
);
1011 // try the next non empty row group
1012 nsCellMap
* cellMap
= aCellMap
.GetNextSibling();
1013 while (cellMap
&& (0 == cellMap
->GetRowCount())) {
1014 cellMap
= cellMap
->GetNextSibling();
1017 cellData
= (BCCellData
*)cellMap
->GetDataAt(0, xPos
);
1018 if (!cellData
) { // add a dead cell
1019 TableArea damageArea
;
1020 cellData
= (BCCellData
*)cellMap
->AppendCell(*this, nullptr, 0,
1021 false, 0, damageArea
);
1023 } else { // must be at the bEnd of the table
1024 bcData
= GetBEndMostBorder(xPos
);
1029 if (!bcData
&& cellData
) {
1030 bcData
= &cellData
->mData
;
1033 bcData
->SetCorner(aSubSize
, aOwner
, aBevel
);
1035 NS_ERROR("program error: Corner not found");
1038 nsCellMap::nsCellMap(nsTableRowGroupFrame
* aRowGroup
, bool aIsBC
)
1040 mContentRowCount(0),
1041 mRowGroupFrame(aRowGroup
),
1042 mNextSibling(nullptr),
1044 mPresContext(aRowGroup
->PresContext()) {
1045 MOZ_COUNT_CTOR(nsCellMap
);
1046 NS_ASSERTION(mPresContext
, "Must have prescontext");
1049 nsCellMap::~nsCellMap() {
1050 MOZ_COUNT_DTOR(nsCellMap
);
1052 uint32_t mapRowCount
= mRows
.Length();
1053 for (uint32_t rowX
= 0; rowX
< mapRowCount
; rowX
++) {
1054 CellDataArray
& row
= mRows
[rowX
];
1055 uint32_t colCount
= row
.Length();
1056 for (uint32_t colX
= 0; colX
< colCount
; colX
++) {
1057 DestroyCellData(row
[colX
]);
1063 void nsCellMap::Init() {
1064 MOZ_ASSERT(!sEmptyRow
, "How did that happen?");
1065 sEmptyRow
= new nsCellMap::CellDataArray();
1069 void nsCellMap::Shutdown() { sEmptyRow
= nullptr; }
1071 nsTableCellFrame
* nsCellMap::GetCellFrame(int32_t aRowIndexIn
,
1072 int32_t aColIndexIn
, CellData
& aData
,
1073 bool aUseRowIfOverlap
) const {
1074 int32_t rowIndex
= aRowIndexIn
- aData
.GetRowSpanOffset();
1075 int32_t colIndex
= aColIndexIn
- aData
.GetColSpanOffset();
1076 if (aData
.IsOverlap()) {
1077 if (aUseRowIfOverlap
) {
1078 colIndex
= aColIndexIn
;
1080 rowIndex
= aRowIndexIn
;
1085 mRows
.SafeElementAt(rowIndex
, *sEmptyRow
).SafeElementAt(colIndex
);
1087 return data
->GetCellFrame();
1092 int32_t nsCellMap::GetHighestIndex(int32_t aColCount
) {
1094 int32_t rowCount
= mRows
.Length();
1095 for (int32_t rowIdx
= 0; rowIdx
< rowCount
; rowIdx
++) {
1096 const CellDataArray
& row
= mRows
[rowIdx
];
1098 for (int32_t colIdx
= 0; colIdx
< aColCount
; colIdx
++) {
1099 CellData
* data
= row
.SafeElementAt(colIdx
);
1100 // No data means row doesn't have more cells.
1103 if (data
->IsOrig()) index
++;
1110 int32_t nsCellMap::GetIndexByRowAndColumn(int32_t aColCount
, int32_t aRow
,
1111 int32_t aColumn
) const {
1112 if (uint32_t(aRow
) >= mRows
.Length()) return -1;
1115 int32_t lastColsIdx
= aColCount
- 1;
1117 // Find row index of the cell where row span is started.
1118 const CellDataArray
& row
= mRows
[aRow
];
1119 CellData
* data
= row
.SafeElementAt(aColumn
);
1120 int32_t origRow
= data
? aRow
- data
->GetRowSpanOffset() : aRow
;
1122 // Calculate cell index.
1123 for (int32_t rowIdx
= 0; rowIdx
<= origRow
; rowIdx
++) {
1124 const CellDataArray
& row
= mRows
[rowIdx
];
1125 int32_t colCount
= (rowIdx
== origRow
) ? aColumn
: lastColsIdx
;
1127 for (int32_t colIdx
= 0; colIdx
<= colCount
; colIdx
++) {
1128 data
= row
.SafeElementAt(colIdx
);
1129 // No data means row doesn't have more cells.
1132 if (data
->IsOrig()) index
++;
1136 // Given row and column don't point to the cell.
1137 if (!data
) return -1;
1142 void nsCellMap::GetRowAndColumnByIndex(int32_t aColCount
, int32_t aIndex
,
1143 int32_t* aRow
, int32_t* aColumn
) const {
1147 int32_t index
= aIndex
;
1148 int32_t rowCount
= mRows
.Length();
1150 for (int32_t rowIdx
= 0; rowIdx
< rowCount
; rowIdx
++) {
1151 const CellDataArray
& row
= mRows
[rowIdx
];
1153 for (int32_t colIdx
= 0; colIdx
< aColCount
; colIdx
++) {
1154 CellData
* data
= row
.SafeElementAt(colIdx
);
1156 // The row doesn't have more cells.
1159 if (data
->IsOrig()) index
--;
1170 bool nsCellMap::Grow(nsTableCellMap
& aMap
, int32_t aNumRows
,
1171 int32_t aRowIndex
) {
1172 NS_ASSERTION(aNumRows
>= 1, "Why are we calling this?");
1174 // Get the number of cols we want to use for preallocating the row arrays.
1175 int32_t numCols
= aMap
.GetColCount();
1179 uint32_t startRowIndex
= (aRowIndex
>= 0) ? aRowIndex
: mRows
.Length();
1180 NS_ASSERTION(startRowIndex
<= mRows
.Length(), "Missing grow call inbetween");
1182 // XXX Change the return type of this function to void, or use a fallible
1184 mRows
.InsertElementsAt(startRowIndex
, aNumRows
, numCols
);
1188 void nsCellMap::GrowRow(CellDataArray
& aRow
, int32_t aNumCols
)
1191 // Have to have the cast to get the template to do the right thing.
1192 aRow
.InsertElementsAt(aRow
.Length(), aNumCols
, (CellData
*)nullptr);
1195 void nsCellMap::InsertRows(nsTableCellMap
& aMap
,
1196 nsTArray
<nsTableRowFrame
*>& aRows
,
1197 int32_t aFirstRowIndex
, bool aConsiderSpans
,
1198 int32_t aRgFirstRowIndex
, TableArea
& aDamageArea
) {
1199 int32_t numCols
= aMap
.GetColCount();
1200 NS_ASSERTION(aFirstRowIndex
>= 0,
1201 "nsCellMap::InsertRows called with negative rowIndex");
1202 if (uint32_t(aFirstRowIndex
) > mRows
.Length()) {
1203 // create (aFirstRowIndex - mRows.Length()) empty rows up to aFirstRowIndex
1204 int32_t numEmptyRows
= aFirstRowIndex
- mRows
.Length();
1205 if (!Grow(aMap
, numEmptyRows
)) {
1210 if (!aConsiderSpans
) {
1211 // update mContentRowCount, since non-empty rows will be added
1212 mContentRowCount
= std::max(aFirstRowIndex
, mContentRowCount
);
1213 ExpandWithRows(aMap
, aRows
, aFirstRowIndex
, aRgFirstRowIndex
, aDamageArea
);
1217 // if any cells span into or out of the row being inserted, then rebuild
1218 bool spansCauseRebuild
=
1219 CellsSpanInOrOut(aFirstRowIndex
, aFirstRowIndex
, 0, numCols
- 1);
1221 // update mContentRowCount, since non-empty rows will be added
1222 mContentRowCount
= std::max(aFirstRowIndex
, mContentRowCount
);
1224 // if any of the new cells span out of the new rows being added, then rebuild
1225 // XXX it would be better to only rebuild the portion of the map that follows
1227 if (!spansCauseRebuild
&& (uint32_t(aFirstRowIndex
) < mRows
.Length())) {
1228 spansCauseRebuild
= CellsSpanOut(aRows
);
1230 if (spansCauseRebuild
) {
1231 aMap
.RebuildConsideringRows(this, aFirstRowIndex
, &aRows
, 0, aDamageArea
);
1233 ExpandWithRows(aMap
, aRows
, aFirstRowIndex
, aRgFirstRowIndex
, aDamageArea
);
1237 void nsCellMap::RemoveRows(nsTableCellMap
& aMap
, int32_t aFirstRowIndex
,
1238 int32_t aNumRowsToRemove
, bool aConsiderSpans
,
1239 int32_t aRgFirstRowIndex
, TableArea
& aDamageArea
) {
1240 int32_t numRows
= mRows
.Length();
1241 int32_t numCols
= aMap
.GetColCount();
1243 if (aFirstRowIndex
>= numRows
) {
1244 // reduce the content based row count based on the function arguments
1245 // as they are known to be real rows even if the cell map did not create
1246 // rows for them before.
1247 mContentRowCount
-= aNumRowsToRemove
;
1250 if (!aConsiderSpans
) {
1251 ShrinkWithoutRows(aMap
, aFirstRowIndex
, aNumRowsToRemove
, aRgFirstRowIndex
,
1255 int32_t endRowIndex
= aFirstRowIndex
+ aNumRowsToRemove
- 1;
1256 if (endRowIndex
>= numRows
) {
1257 NS_ERROR("nsCellMap::RemoveRows tried to remove too many rows");
1258 endRowIndex
= numRows
- 1;
1260 bool spansCauseRebuild
=
1261 CellsSpanInOrOut(aFirstRowIndex
, endRowIndex
, 0, numCols
- 1);
1262 if (spansCauseRebuild
) {
1263 aMap
.RebuildConsideringRows(this, aFirstRowIndex
, nullptr, aNumRowsToRemove
,
1266 ShrinkWithoutRows(aMap
, aFirstRowIndex
, aNumRowsToRemove
, aRgFirstRowIndex
,
1271 CellData
* nsCellMap::AppendCell(nsTableCellMap
& aMap
,
1272 nsTableCellFrame
* aCellFrame
, int32_t aRowIndex
,
1273 bool aRebuildIfNecessary
,
1274 int32_t aRgFirstRowIndex
,
1275 TableArea
& aDamageArea
,
1276 int32_t* aColToBeginSearch
) {
1277 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1278 int32_t origNumMapRows
= mRows
.Length();
1279 int32_t origNumCols
= aMap
.GetColCount();
1280 bool zeroRowSpan
= false;
1282 (aCellFrame
) ? GetRowSpanForNewCell(aCellFrame
, aRowIndex
, zeroRowSpan
)
1284 // add new rows if necessary
1285 int32_t endRowIndex
= aRowIndex
+ rowSpan
- 1;
1286 if (endRowIndex
>= origNumMapRows
) {
1287 // XXXbz handle allocation failures?
1288 Grow(aMap
, 1 + endRowIndex
- origNumMapRows
);
1291 // get the first null or dead CellData in the desired row. It will equal
1292 // origNumCols if there are none
1293 CellData
* origData
= nullptr;
1294 int32_t startColIndex
= 0;
1295 if (aColToBeginSearch
) startColIndex
= *aColToBeginSearch
;
1296 for (; startColIndex
< origNumCols
; startColIndex
++) {
1297 CellData
* data
= GetDataAt(aRowIndex
, startColIndex
);
1299 // The border collapse code relies on having multiple dead cell data entries
1301 if (data
->IsDead() && aCellFrame
) {
1306 // We found the place to append the cell, when the next cell is appended
1307 // the next search does not need to duplicate the search but can start
1308 // just at the next cell.
1309 if (aColToBeginSearch
) *aColToBeginSearch
= startColIndex
+ 1;
1311 int32_t colSpan
= aCellFrame
? aCellFrame
->GetColSpan() : 1;
1313 // if the new cell could potentially span into other rows and collide with
1314 // originating cells there, we will play it safe and just rebuild the map
1315 if (aRebuildIfNecessary
&& (aRowIndex
< mContentRowCount
- 1) &&
1317 AutoTArray
<nsTableCellFrame
*, 1> newCellArray
;
1318 newCellArray
.AppendElement(aCellFrame
);
1319 aMap
.RebuildConsideringCells(this, &newCellArray
, aRowIndex
, startColIndex
,
1323 mContentRowCount
= std::max(mContentRowCount
, aRowIndex
+ 1);
1325 // add new cols to the table map if necessary
1326 int32_t endColIndex
= startColIndex
+ colSpan
- 1;
1327 if (endColIndex
>= origNumCols
) {
1328 NS_ASSERTION(aCellFrame
, "dead cells should not require new columns");
1329 aMap
.AddColsAtEnd(1 + endColIndex
- origNumCols
);
1332 // Setup CellData for this cell
1334 NS_ASSERTION(origData
->IsDead(),
1335 "replacing a non dead cell is a memory leak");
1336 if (aCellFrame
) { // do nothing to replace a dead cell with a dead cell
1337 origData
->Init(aCellFrame
);
1338 // we are replacing a dead cell, increase the number of cells
1339 // originating at this column
1340 nsColInfo
* colInfo
= aMap
.GetColInfoAt(startColIndex
);
1341 NS_ASSERTION(colInfo
, "access to a non existing column");
1343 colInfo
->mNumCellsOrig
++;
1347 origData
= AllocCellData(aCellFrame
);
1348 if (!origData
) ABORT1(origData
);
1349 SetDataAt(aMap
, *origData
, aRowIndex
, startColIndex
);
1352 if (aRebuildIfNecessary
) {
1353 // the caller depends on the damageArea
1354 // The special case for zeroRowSpan is to adjust for the '2' in
1355 // GetRowSpanForNewCell.
1356 uint32_t height
= std::min(zeroRowSpan
? rowSpan
- 1 : rowSpan
,
1357 GetRowCount() - aRowIndex
);
1358 SetDamageArea(startColIndex
, aRgFirstRowIndex
+ aRowIndex
,
1359 1 + endColIndex
- startColIndex
, height
, aDamageArea
);
1366 // initialize the cell frame
1367 aCellFrame
->SetColIndex(startColIndex
);
1369 // Create CellData objects for the rows that this cell spans. Set
1370 // their mOrigCell to nullptr and their mSpanData to point to data.
1371 for (int32_t rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1372 // The row at rowX will need to have at least endColIndex columns
1373 mRows
[rowX
].SetCapacity(endColIndex
);
1374 for (int32_t colX
= startColIndex
; colX
<= endColIndex
; colX
++) {
1375 if ((rowX
!= aRowIndex
) ||
1376 (colX
!= startColIndex
)) { // skip orig cell data done above
1377 CellData
* cellData
= GetDataAt(rowX
, colX
);
1379 if (cellData
->IsOrig()) {
1380 NS_ERROR("cannot overlap originating cell");
1383 if (rowX
> aRowIndex
) { // row spanning into cell
1384 if (cellData
->IsRowSpan()) {
1385 // do nothing, this can be caused by rowspan which is overlapped
1386 // by a another cell with a rowspan and a colspan
1388 cellData
->SetRowSpanOffset(rowX
- aRowIndex
);
1390 cellData
->SetZeroRowSpan(true);
1394 if (colX
> startColIndex
) { // col spanning into cell
1395 if (!cellData
->IsColSpan()) {
1396 if (cellData
->IsRowSpan()) {
1397 cellData
->SetOverlap(true);
1399 cellData
->SetColSpanOffset(colX
- startColIndex
);
1400 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1401 colInfo
->mNumCellsSpan
++;
1405 cellData
= AllocCellData(nullptr);
1406 if (!cellData
) return origData
;
1407 if (rowX
> aRowIndex
) {
1408 cellData
->SetRowSpanOffset(rowX
- aRowIndex
);
1410 cellData
->SetZeroRowSpan(true);
1413 if (colX
> startColIndex
) {
1414 cellData
->SetColSpanOffset(colX
- startColIndex
);
1416 SetDataAt(aMap
, *cellData
, rowX
, colX
);
1421 #ifdef DEBUG_TABLE_CELLMAP
1422 printf("appended cell=%p row=%d \n", aCellFrame
, aRowIndex
);
1428 bool nsCellMap::CellsSpanOut(nsTArray
<nsTableRowFrame
*>& aRows
) const {
1429 int32_t numNewRows
= aRows
.Length();
1430 for (int32_t rowX
= 0; rowX
< numNewRows
; rowX
++) {
1431 nsTableRowFrame
* rowFrame
= aRows
.ElementAt(rowX
);
1432 for (nsTableCellFrame
* cellFrame
= rowFrame
->GetFirstCell(); cellFrame
;
1433 cellFrame
= cellFrame
->GetNextCell()) {
1435 int32_t rowSpan
= GetRowSpanForNewCell(cellFrame
, rowX
, zeroSpan
);
1436 if (zeroSpan
|| rowX
+ rowSpan
> numNewRows
) {
1444 // return true if any cells have rows spans into or out of the region
1445 // defined by the row and col indices or any cells have colspans into the region
1446 bool nsCellMap::CellsSpanInOrOut(int32_t aStartRowIndex
, int32_t aEndRowIndex
,
1447 int32_t aStartColIndex
,
1448 int32_t aEndColIndex
) const {
1450 * this routine will watch the cells adjacent to the region or at the edge
1451 * they are marked with *. The routine will verify whether they span in or
1455 * r1c1 r1c2 r1c3 r1c4 r1c5 r1rc6 r1c7
1456 * startrow r2c1 r2c2 *r2c3 *r2c4 *r2c5 *r2rc6 r2c7
1457 * endrow r3c1 r3c2 *r3c3 r3c4 r3c5 *r3rc6 r3c7
1458 * r4c1 r4c2 *r4c3 *r4c4 *r4c5 r4rc6 r4c7
1459 * r5c1 r5c2 r5c3 r5c4 r5c5 r5rc6 r5c7
1462 int32_t numRows
= mRows
.Length(); // use the cellmap rows to determine the
1463 // current cellmap extent.
1464 for (int32_t colX
= aStartColIndex
; colX
<= aEndColIndex
; colX
++) {
1466 if (aStartRowIndex
> 0) {
1467 cellData
= GetDataAt(aStartRowIndex
, colX
);
1468 if (cellData
&& (cellData
->IsRowSpan())) {
1469 return true; // there is a row span into the region
1471 if ((aStartRowIndex
>= mContentRowCount
) && (mContentRowCount
> 0)) {
1472 cellData
= GetDataAt(mContentRowCount
- 1, colX
);
1473 if (cellData
&& cellData
->IsZeroRowSpan()) {
1474 return true; // When we expand the zerospan it'll span into our row
1478 if (aEndRowIndex
< numRows
- 1) { // is there anything below aEndRowIndex
1479 cellData
= GetDataAt(aEndRowIndex
+ 1, colX
);
1480 if ((cellData
) && (cellData
->IsRowSpan())) {
1481 return true; // there is a row span out of the region
1484 cellData
= GetDataAt(aEndRowIndex
, colX
);
1485 if ((cellData
) && (cellData
->IsRowSpan()) &&
1486 (mContentRowCount
< numRows
)) {
1487 return true; // this cell might be the cause of a dead row
1491 if (aStartColIndex
> 0) {
1492 for (int32_t rowX
= aStartRowIndex
; rowX
<= aEndRowIndex
; rowX
++) {
1493 CellData
* cellData
= GetDataAt(rowX
, aStartColIndex
);
1494 if (cellData
&& (cellData
->IsColSpan())) {
1495 return true; // there is a col span into the region
1497 cellData
= GetDataAt(rowX
, aEndColIndex
+ 1);
1498 if (cellData
&& (cellData
->IsColSpan())) {
1499 return true; // there is a col span out of the region
1506 void nsCellMap::InsertCells(nsTableCellMap
& aMap
,
1507 nsTArray
<nsTableCellFrame
*>& aCellFrames
,
1508 int32_t aRowIndex
, int32_t aColIndexBefore
,
1509 int32_t aRgFirstRowIndex
, TableArea
& aDamageArea
) {
1510 if (aCellFrames
.Length() == 0) return;
1511 NS_ASSERTION(aColIndexBefore
>= -1, "index out of range");
1512 int32_t numCols
= aMap
.GetColCount();
1513 if (aColIndexBefore
>= numCols
) {
1515 "Inserting instead of appending cells indicates a serious cellmap "
1517 aColIndexBefore
= numCols
- 1;
1520 // get the starting col index of the 1st new cells
1521 int32_t startColIndex
;
1522 for (startColIndex
= aColIndexBefore
+ 1; startColIndex
< numCols
;
1524 CellData
* data
= GetDataAt(aRowIndex
, startColIndex
);
1525 if (!data
|| data
->IsOrig() || data
->IsDead()) {
1526 // // Not a span. Stop.
1531 // record whether inserted cells are going to cause complications due
1532 // to existing row spans, col spans or table sizing.
1533 bool spansCauseRebuild
= false;
1535 // check that all cells have the same row span
1536 int32_t numNewCells
= aCellFrames
.Length();
1537 bool zeroRowSpan
= false;
1538 int32_t rowSpan
= 0;
1539 for (int32_t cellX
= 0; cellX
< numNewCells
; cellX
++) {
1540 nsTableCellFrame
* cell
= aCellFrames
.ElementAt(cellX
);
1541 int32_t rowSpan2
= GetRowSpanForNewCell(cell
, aRowIndex
, zeroRowSpan
);
1544 } else if (rowSpan
!= rowSpan2
) {
1545 spansCauseRebuild
= true;
1550 // check if the new cells will cause the table to add more rows
1551 if (!spansCauseRebuild
) {
1552 if (mRows
.Length() < uint32_t(aRowIndex
+ rowSpan
)) {
1553 spansCauseRebuild
= true;
1557 if (!spansCauseRebuild
) {
1558 spansCauseRebuild
= CellsSpanInOrOut(aRowIndex
, aRowIndex
+ rowSpan
- 1,
1559 startColIndex
, numCols
- 1);
1561 if (spansCauseRebuild
) {
1562 aMap
.RebuildConsideringCells(this, &aCellFrames
, aRowIndex
, startColIndex
,
1565 ExpandWithCells(aMap
, aCellFrames
, aRowIndex
, startColIndex
, rowSpan
,
1566 zeroRowSpan
, aRgFirstRowIndex
, aDamageArea
);
1570 void nsCellMap::ExpandWithRows(nsTableCellMap
& aMap
,
1571 nsTArray
<nsTableRowFrame
*>& aRowFrames
,
1572 int32_t aStartRowIndexIn
,
1573 int32_t aRgFirstRowIndex
,
1574 TableArea
& aDamageArea
) {
1575 int32_t startRowIndex
= (aStartRowIndexIn
>= 0) ? aStartRowIndexIn
: 0;
1576 NS_ASSERTION(uint32_t(startRowIndex
) <= mRows
.Length(),
1577 "caller should have grown cellmap before");
1579 int32_t numNewRows
= aRowFrames
.Length();
1580 mContentRowCount
+= numNewRows
;
1582 int32_t endRowIndex
= startRowIndex
+ numNewRows
- 1;
1584 // shift the rows after startRowIndex down and insert empty rows that will
1585 // be filled via the AppendCell call below
1586 if (!Grow(aMap
, numNewRows
, startRowIndex
)) {
1590 int32_t newRowIndex
= 0;
1591 for (int32_t rowX
= startRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1592 nsTableRowFrame
* rFrame
= aRowFrames
.ElementAt(newRowIndex
);
1594 int32_t colIndex
= 0;
1595 for (nsTableCellFrame
* cellFrame
= rFrame
->GetFirstCell(); cellFrame
;
1596 cellFrame
= cellFrame
->GetNextCell()) {
1597 AppendCell(aMap
, cellFrame
, rowX
, false, aRgFirstRowIndex
, aDamageArea
,
1602 // mark all following rows damaged, they might contain a previously set
1603 // damage area which we can not shift.
1604 int32_t firstDamagedRow
= aRgFirstRowIndex
+ startRowIndex
;
1605 SetDamageArea(0, firstDamagedRow
, aMap
.GetColCount(),
1606 aMap
.GetRowCount() - firstDamagedRow
, aDamageArea
);
1609 void nsCellMap::ExpandWithCells(nsTableCellMap
& aMap
,
1610 nsTArray
<nsTableCellFrame
*>& aCellFrames
,
1611 int32_t aRowIndex
, int32_t aColIndex
,
1612 int32_t aRowSpan
, // same for all cells
1613 bool aRowSpanIsZero
, int32_t aRgFirstRowIndex
,
1614 TableArea
& aDamageArea
) {
1615 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1616 int32_t endRowIndex
= aRowIndex
+ aRowSpan
- 1;
1617 int32_t startColIndex
= aColIndex
;
1618 int32_t endColIndex
= aColIndex
;
1619 int32_t numCells
= aCellFrames
.Length();
1620 int32_t totalColSpan
= 0;
1622 // add cellData entries for the space taken up by the new cells
1623 for (int32_t cellX
= 0; cellX
< numCells
; cellX
++) {
1624 nsTableCellFrame
* cellFrame
= aCellFrames
.ElementAt(cellX
);
1625 CellData
* origData
= AllocCellData(cellFrame
); // the originating cell
1626 if (!origData
) return;
1628 // set the starting and ending col index for the new cell
1629 int32_t colSpan
= cellFrame
->GetColSpan();
1630 totalColSpan
+= colSpan
;
1632 endColIndex
= aColIndex
+ colSpan
- 1;
1634 startColIndex
= endColIndex
+ 1;
1635 endColIndex
= startColIndex
+ colSpan
- 1;
1638 // add the originating cell data and any cell data corresponding to row/col
1640 for (int32_t rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1641 CellDataArray
& row
= mRows
[rowX
];
1642 // Pre-allocate all the cells we'll need in this array, setting
1644 // Have to have the cast to get the template to do the right thing.
1645 int32_t insertionIndex
= row
.Length();
1646 if (insertionIndex
> startColIndex
) {
1647 insertionIndex
= startColIndex
;
1649 row
.InsertElementsAt(insertionIndex
, endColIndex
- insertionIndex
+ 1,
1650 (CellData
*)nullptr);
1652 for (int32_t colX
= startColIndex
; colX
<= endColIndex
; colX
++) {
1653 CellData
* data
= origData
;
1654 if ((rowX
!= aRowIndex
) || (colX
!= startColIndex
)) {
1655 data
= AllocCellData(nullptr);
1657 if (rowX
> aRowIndex
) {
1658 data
->SetRowSpanOffset(rowX
- aRowIndex
);
1659 if (aRowSpanIsZero
) {
1660 data
->SetZeroRowSpan(true);
1663 if (colX
> startColIndex
) {
1664 data
->SetColSpanOffset(colX
- startColIndex
);
1667 SetDataAt(aMap
, *data
, rowX
, colX
);
1670 cellFrame
->SetColIndex(startColIndex
);
1672 int32_t damageHeight
=
1673 std::min(GetRowGroup()->GetRowCount() - aRowIndex
, aRowSpan
);
1674 SetDamageArea(aColIndex
, aRgFirstRowIndex
+ aRowIndex
,
1675 1 + endColIndex
- aColIndex
, damageHeight
, aDamageArea
);
1679 // update the row and col info due to shifting
1680 for (rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1681 CellDataArray
& row
= mRows
[rowX
];
1682 uint32_t numCols
= row
.Length();
1684 for (colX
= aColIndex
+ totalColSpan
; colX
< numCols
; colX
++) {
1685 CellData
* data
= row
[colX
];
1687 // increase the origin and span counts beyond the spanned cols
1688 if (data
->IsOrig()) {
1689 // a cell that gets moved needs adjustment as well as it new
1691 data
->GetCellFrame()->SetColIndex(colX
);
1692 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1693 colInfo
->mNumCellsOrig
++;
1695 if (data
->IsColSpan()) {
1696 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1697 colInfo
->mNumCellsSpan
++;
1700 // decrease the origin and span counts within the spanned cols
1701 int32_t colX2
= colX
- totalColSpan
;
1702 nsColInfo
* colInfo2
= aMap
.GetColInfoAt(colX2
);
1703 if (data
->IsOrig()) {
1704 // the old originating col of a moved cell needs adjustment
1705 colInfo2
->mNumCellsOrig
--;
1707 if (data
->IsColSpan()) {
1708 colInfo2
->mNumCellsSpan
--;
1715 void nsCellMap::ShrinkWithoutRows(nsTableCellMap
& aMap
, int32_t aStartRowIndex
,
1716 int32_t aNumRowsToRemove
,
1717 int32_t aRgFirstRowIndex
,
1718 TableArea
& aDamageArea
) {
1719 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1720 int32_t endRowIndex
= aStartRowIndex
+ aNumRowsToRemove
- 1;
1721 uint32_t colCount
= aMap
.GetColCount();
1722 for (int32_t rowX
= endRowIndex
; rowX
>= aStartRowIndex
; --rowX
) {
1723 CellDataArray
& row
= mRows
[rowX
];
1725 for (colX
= 0; colX
< colCount
; colX
++) {
1726 CellData
* data
= row
.SafeElementAt(colX
);
1728 // Adjust the column counts.
1729 if (data
->IsOrig()) {
1730 // Decrement the column count.
1731 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1732 colInfo
->mNumCellsOrig
--;
1734 // colspan=0 is only counted as a spanned cell in the 1st col it spans
1735 else if (data
->IsColSpan()) {
1736 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1737 colInfo
->mNumCellsSpan
--;
1742 uint32_t rowLength
= row
.Length();
1743 // Delete our row information.
1744 for (colX
= 0; colX
< rowLength
; colX
++) {
1745 DestroyCellData(row
[colX
]);
1748 mRows
.RemoveElementAt(rowX
);
1750 // Decrement our row and next available index counts.
1753 aMap
.RemoveColsAtEnd();
1754 // mark all following rows damaged, they might contain a previously set
1755 // damage area which we can not shift.
1756 int32_t firstDamagedRow
= aRgFirstRowIndex
+ aStartRowIndex
;
1757 SetDamageArea(0, firstDamagedRow
, aMap
.GetColCount(),
1758 aMap
.GetRowCount() - firstDamagedRow
, aDamageArea
);
1761 int32_t nsCellMap::GetEffectiveColSpan(const nsTableCellMap
& aMap
,
1763 int32_t aColIndex
) const {
1764 int32_t numColsInTable
= aMap
.GetColCount();
1765 int32_t colSpan
= 1;
1766 if (uint32_t(aRowIndex
) >= mRows
.Length()) {
1770 const CellDataArray
& row
= mRows
[aRowIndex
];
1773 int32_t maxCols
= numColsInTable
;
1774 bool hitOverlap
= false; // XXX this is not ever being set to true
1775 for (colX
= aColIndex
+ 1; colX
< maxCols
; colX
++) {
1776 data
= row
.SafeElementAt(colX
);
1778 // for an overlapping situation get the colspan from the originating cell
1779 // and use that as the max number of cols to iterate. Since this is rare,
1780 // only pay the price of looking up the cell's colspan here.
1781 if (!hitOverlap
&& data
->IsOverlap()) {
1782 CellData
* origData
= row
.SafeElementAt(aColIndex
);
1783 if (origData
&& origData
->IsOrig()) {
1784 nsTableCellFrame
* cellFrame
= origData
->GetCellFrame();
1786 // possible change the number of colums to iterate
1787 maxCols
= std::min(aColIndex
+ cellFrame
->GetColSpan(), maxCols
);
1788 if (colX
>= maxCols
) break;
1792 if (data
->IsColSpan()) {
1803 int32_t nsCellMap::GetRowSpanForNewCell(nsTableCellFrame
* aCellFrameToAdd
,
1805 bool& aIsZeroRowSpan
) const {
1806 aIsZeroRowSpan
= false;
1807 int32_t rowSpan
= aCellFrameToAdd
->GetRowSpan();
1809 // Use a min value of 2 for a zero rowspan to make computations easier
1810 // elsewhere. Zero rowspans are only content dependent!
1811 rowSpan
= std::max(2, mContentRowCount
- aRowIndex
);
1812 aIsZeroRowSpan
= true;
1817 bool nsCellMap::HasMoreThanOneCell(int32_t aRowIndex
) const {
1818 const CellDataArray
& row
= mRows
.SafeElementAt(aRowIndex
, *sEmptyRow
);
1819 uint32_t maxColIndex
= row
.Length();
1821 bool foundOne
= false;
1822 for (colIndex
= 0; colIndex
< maxColIndex
; colIndex
++) {
1823 CellData
* cellData
= row
[colIndex
];
1824 if (cellData
&& (cellData
->GetCellFrame() || cellData
->IsRowSpan())) {
1834 int32_t nsCellMap::GetNumCellsOriginatingInRow(int32_t aRowIndex
) const {
1835 const CellDataArray
& row
= mRows
.SafeElementAt(aRowIndex
, *sEmptyRow
);
1837 uint32_t maxColIndex
= row
.Length();
1839 for (colIndex
= 0; colIndex
< maxColIndex
; colIndex
++) {
1840 CellData
* cellData
= row
[colIndex
];
1841 if (cellData
&& cellData
->IsOrig()) count
++;
1846 int32_t nsCellMap::GetRowSpan(int32_t aRowIndex
, int32_t aColIndex
,
1847 bool aGetEffective
) const {
1848 int32_t rowSpan
= 1;
1849 int32_t rowCount
= (aGetEffective
) ? mContentRowCount
: mRows
.Length();
1851 for (rowX
= aRowIndex
+ 1; rowX
< rowCount
; rowX
++) {
1852 CellData
* data
= GetDataAt(rowX
, aColIndex
);
1854 if (data
->IsRowSpan()) {
1865 void nsCellMap::ShrinkWithoutCell(nsTableCellMap
& aMap
,
1866 nsTableCellFrame
& aCellFrame
,
1867 int32_t aRowIndex
, int32_t aColIndex
,
1868 int32_t aRgFirstRowIndex
,
1869 TableArea
& aDamageArea
) {
1870 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1871 uint32_t colX
, rowX
;
1873 // get the rowspan and colspan from the cell map since the content may have
1875 int32_t rowSpan
= GetRowSpan(aRowIndex
, aColIndex
, true);
1876 uint32_t colSpan
= GetEffectiveColSpan(aMap
, aRowIndex
, aColIndex
);
1877 uint32_t endRowIndex
= aRowIndex
+ rowSpan
- 1;
1878 uint32_t endColIndex
= aColIndex
+ colSpan
- 1;
1880 // adjust the col counts due to the deleted cell before removing it
1881 for (colX
= aColIndex
; colX
<= endColIndex
; colX
++) {
1882 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1883 if (colX
== uint32_t(aColIndex
)) {
1884 colInfo
->mNumCellsOrig
--;
1886 colInfo
->mNumCellsSpan
--;
1890 // remove the deleted cell and cellData entries for it
1891 for (rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1892 CellDataArray
& row
= mRows
[rowX
];
1894 // endIndexForRow points at the first slot we don't want to clean up. This
1895 // makes the aColIndex == 0 case work right with our unsigned int colX.
1896 NS_ASSERTION(endColIndex
+ 1 <= row
.Length(), "span beyond the row size!");
1897 uint32_t endIndexForRow
= std::min(endColIndex
+ 1, uint32_t(row
.Length()));
1899 // Since endIndexForRow <= row.Length(), enough to compare aColIndex to it.
1900 if (uint32_t(aColIndex
) < endIndexForRow
) {
1901 for (colX
= endIndexForRow
; colX
> uint32_t(aColIndex
); colX
--) {
1902 DestroyCellData(row
[colX
- 1]);
1904 row
.RemoveElementsAt(aColIndex
, endIndexForRow
- aColIndex
);
1908 uint32_t numCols
= aMap
.GetColCount();
1910 // update the row and col info due to shifting
1911 for (rowX
= aRowIndex
; rowX
<= endRowIndex
; rowX
++) {
1912 CellDataArray
& row
= mRows
[rowX
];
1913 for (colX
= aColIndex
; colX
< numCols
- colSpan
; colX
++) {
1914 CellData
* data
= row
.SafeElementAt(colX
);
1916 if (data
->IsOrig()) {
1917 // a cell that gets moved to the left needs adjustment in its new
1919 data
->GetCellFrame()->SetColIndex(colX
);
1920 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1921 colInfo
->mNumCellsOrig
++;
1922 // a cell that gets moved to the left needs adjustment in its old
1924 colInfo
= aMap
.GetColInfoAt(colX
+ colSpan
);
1926 colInfo
->mNumCellsOrig
--;
1930 else if (data
->IsColSpan()) {
1931 // a cell that gets moved to the left needs adjustment
1932 // in its new location
1933 nsColInfo
* colInfo
= aMap
.GetColInfoAt(colX
);
1934 colInfo
->mNumCellsSpan
++;
1935 // a cell that gets moved to the left needs adjustment
1936 // in its old location
1937 colInfo
= aMap
.GetColInfoAt(colX
+ colSpan
);
1939 colInfo
->mNumCellsSpan
--;
1945 aMap
.RemoveColsAtEnd();
1946 SetDamageArea(aColIndex
, aRgFirstRowIndex
+ aRowIndex
,
1947 std::max(0, aMap
.GetColCount() - aColIndex
- 1),
1948 1 + endRowIndex
- aRowIndex
, aDamageArea
);
1951 void nsCellMap::RebuildConsideringRows(
1952 nsTableCellMap
& aMap
, int32_t aStartRowIndex
,
1953 nsTArray
<nsTableRowFrame
*>* aRowsToInsert
, int32_t aNumRowsToRemove
) {
1954 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
1955 // copy the old cell map into a new array
1956 uint32_t numOrigRows
= mRows
.Length();
1957 nsTArray
<CellDataArray
> origRows
= std::move(mRows
);
1959 int32_t rowNumberChange
;
1960 if (aRowsToInsert
) {
1961 rowNumberChange
= aRowsToInsert
->Length();
1963 rowNumberChange
= -aNumRowsToRemove
;
1966 // adjust mContentRowCount based on the function arguments as they are known
1968 mContentRowCount
+= rowNumberChange
;
1969 NS_ASSERTION(mContentRowCount
>= 0, "previous mContentRowCount was wrong");
1970 // mRows is empty now. Grow it to the size we expect it to have.
1971 if (mContentRowCount
) {
1972 if (!Grow(aMap
, mContentRowCount
)) {
1973 // Bail, I guess... Not sure what else we can do here.
1978 // aStartRowIndex might be after all existing rows so we should limit the
1979 // copy to the amount of exisiting rows
1980 uint32_t copyEndRowIndex
= std::min(numOrigRows
, uint32_t(aStartRowIndex
));
1982 // rowX keeps track of where we are in mRows while setting up the
1985 TableArea damageArea
;
1986 // put back the rows before the affected ones just as before. Note that we
1987 // can't just copy the old rows in bit-for-bit, because they might be
1988 // spanning out into the rows we're adding/removing.
1989 for (; rowX
< copyEndRowIndex
; rowX
++) {
1990 const CellDataArray
& row
= origRows
[rowX
];
1991 uint32_t numCols
= row
.Length();
1992 for (uint32_t colX
= 0; colX
< numCols
; colX
++) {
1993 // put in the original cell from the cell map
1994 const CellData
* data
= row
.ElementAt(colX
);
1995 if (data
&& data
->IsOrig()) {
1996 AppendCell(aMap
, data
->GetCellFrame(), rowX
, false, 0, damageArea
);
2001 // Now handle the new rows being inserted, if any.
2002 uint32_t copyStartRowIndex
;
2003 rowX
= aStartRowIndex
;
2004 if (aRowsToInsert
) {
2005 // add in the new cells and create rows if necessary
2006 int32_t numNewRows
= aRowsToInsert
->Length();
2007 for (int32_t newRowX
= 0; newRowX
< numNewRows
; newRowX
++) {
2008 nsTableRowFrame
* rFrame
= aRowsToInsert
->ElementAt(newRowX
);
2009 for (nsTableCellFrame
* cellFrame
= rFrame
->GetFirstCell(); cellFrame
;
2010 cellFrame
= cellFrame
->GetNextCell()) {
2011 AppendCell(aMap
, cellFrame
, rowX
, false, 0, damageArea
);
2015 copyStartRowIndex
= aStartRowIndex
;
2017 copyStartRowIndex
= aStartRowIndex
+ aNumRowsToRemove
;
2020 // put back the rows after the affected ones just as before. Again, we can't
2021 // just copy the old bits because that would not handle the new rows spanning
2022 // out or our earlier old rows spanning through the damaged area.
2023 for (uint32_t copyRowX
= copyStartRowIndex
; copyRowX
< numOrigRows
;
2025 const CellDataArray
& row
= origRows
[copyRowX
];
2026 uint32_t numCols
= row
.Length();
2027 for (uint32_t colX
= 0; colX
< numCols
; colX
++) {
2028 // put in the original cell from the cell map
2029 CellData
* data
= row
.ElementAt(colX
);
2030 if (data
&& data
->IsOrig()) {
2031 AppendCell(aMap
, data
->GetCellFrame(), rowX
, false, 0, damageArea
);
2037 // delete the old cell map. Now rowX no longer has anything to do with mRows
2038 for (rowX
= 0; rowX
< numOrigRows
; rowX
++) {
2039 CellDataArray
& row
= origRows
[rowX
];
2040 uint32_t len
= row
.Length();
2041 for (uint32_t colX
= 0; colX
< len
; colX
++) {
2042 DestroyCellData(row
[colX
]);
2047 void nsCellMap::RebuildConsideringCells(
2048 nsTableCellMap
& aMap
, int32_t aNumOrigCols
,
2049 nsTArray
<nsTableCellFrame
*>* aCellFrames
, int32_t aRowIndex
,
2050 int32_t aColIndex
, bool aInsert
) {
2051 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
2052 // copy the old cell map into a new array
2053 int32_t numOrigRows
= mRows
.Length();
2054 nsTArray
<CellDataArray
> origRows
= std::move(mRows
);
2056 int32_t numNewCells
= (aCellFrames
) ? aCellFrames
->Length() : 0;
2058 // the new cells might extend the previous column number
2059 NS_ASSERTION(aNumOrigCols
>= aColIndex
,
2060 "Appending cells far beyond cellmap data?!");
2062 aInsert
? std::max(aNumOrigCols
, aColIndex
+ 1) : aNumOrigCols
;
2064 // build the new cell map. Hard to say what, if anything, we can preallocate
2065 // here... Should come back to that sometime, perhaps.
2067 TableArea damageArea
;
2068 for (rowX
= 0; rowX
< numOrigRows
; rowX
++) {
2069 const CellDataArray
& row
= origRows
[rowX
];
2070 for (int32_t colX
= 0; colX
< numCols
; colX
++) {
2071 if ((rowX
== aRowIndex
) && (colX
== aColIndex
)) {
2072 if (aInsert
) { // put in the new cells
2073 for (int32_t cellX
= 0; cellX
< numNewCells
; cellX
++) {
2074 nsTableCellFrame
* cell
= aCellFrames
->ElementAt(cellX
);
2076 AppendCell(aMap
, cell
, rowX
, false, 0, damageArea
);
2080 continue; // do not put the deleted cell back
2083 // put in the original cell from the cell map
2084 CellData
* data
= row
.SafeElementAt(colX
);
2085 if (data
&& data
->IsOrig()) {
2086 AppendCell(aMap
, data
->GetCellFrame(), rowX
, false, 0, damageArea
);
2092 aRowIndex
) { // append the new cells below the last original row
2093 NS_ASSERTION(numOrigRows
== aRowIndex
,
2094 "Appending cells far beyond the last row");
2095 for (int32_t cellX
= 0; cellX
< numNewCells
; cellX
++) {
2096 nsTableCellFrame
* cell
= aCellFrames
->ElementAt(cellX
);
2098 AppendCell(aMap
, cell
, aRowIndex
, false, 0, damageArea
);
2103 // delete the old cell map
2104 for (rowX
= 0; rowX
< numOrigRows
; rowX
++) {
2105 CellDataArray
& row
= origRows
[rowX
];
2106 uint32_t len
= row
.Length();
2107 for (uint32_t colX
= 0; colX
< len
; colX
++) {
2108 DestroyCellData(row
.SafeElementAt(colX
));
2111 // expand the cellmap to cover empty content rows
2112 if (mRows
.Length() < uint32_t(mContentRowCount
)) {
2113 Grow(aMap
, mContentRowCount
- mRows
.Length());
2117 void nsCellMap::RemoveCell(nsTableCellMap
& aMap
, nsTableCellFrame
* aCellFrame
,
2118 int32_t aRowIndex
, int32_t aRgFirstRowIndex
,
2119 TableArea
& aDamageArea
) {
2120 uint32_t numRows
= mRows
.Length();
2121 if (uint32_t(aRowIndex
) >= numRows
) {
2122 NS_ERROR("bad arg in nsCellMap::RemoveCell");
2125 int32_t numCols
= aMap
.GetColCount();
2127 // Now aRowIndex is guaranteed OK.
2129 // get the starting col index of the cell to remove
2130 int32_t startColIndex
;
2131 for (startColIndex
= 0; startColIndex
< numCols
; startColIndex
++) {
2132 CellData
* data
= mRows
[aRowIndex
].SafeElementAt(startColIndex
);
2133 if (data
&& (data
->IsOrig()) && (aCellFrame
== data
->GetCellFrame())) {
2134 break; // we found the col index
2138 int32_t rowSpan
= GetRowSpan(aRowIndex
, startColIndex
, false);
2139 // record whether removing the cells is going to cause complications due
2140 // to existing row spans, col spans or table sizing.
2141 bool spansCauseRebuild
= CellsSpanInOrOut(aRowIndex
, aRowIndex
+ rowSpan
- 1,
2142 startColIndex
, numCols
- 1);
2143 // XXX if the cell has a col span to the end of the map, and the end has no
2144 // originating cells, we need to assume that this the only such cell, and
2145 // rebuild so that there are no extraneous cols at the end. The same is true
2146 // for removing rows.
2147 if (!spansCauseRebuild
) {
2148 if (!aCellFrame
->GetRowSpan() || !aCellFrame
->GetColSpan()) {
2149 spansCauseRebuild
= true;
2153 if (spansCauseRebuild
) {
2154 aMap
.RebuildConsideringCells(this, nullptr, aRowIndex
, startColIndex
, false,
2157 ShrinkWithoutCell(aMap
, *aCellFrame
, aRowIndex
, startColIndex
,
2158 aRgFirstRowIndex
, aDamageArea
);
2163 void nsCellMap::Dump(bool aIsBorderCollapse
) const {
2164 printf("\n ***** START GROUP CELL MAP DUMP ***** %p\n", (void*)this);
2165 nsTableRowGroupFrame
* rg
= GetRowGroup();
2166 const nsStyleDisplay
* display
= rg
->StyleDisplay();
2167 switch (display
->DisplayInside()) {
2168 case StyleDisplayInside::TableHeaderGroup
:
2171 case StyleDisplayInside::TableFooterGroup
:
2174 case StyleDisplayInside::TableRowGroup
:
2178 printf("HUH? wrong display type on rowgroup");
2180 uint32_t mapRowCount
= mRows
.Length();
2181 printf("mapRowCount=%u tableRowCount=%d\n", mapRowCount
, mContentRowCount
);
2183 uint32_t rowIndex
, colIndex
;
2184 for (rowIndex
= 0; rowIndex
< mapRowCount
; rowIndex
++) {
2185 const CellDataArray
& row
= mRows
[rowIndex
];
2186 printf(" row %d : ", rowIndex
);
2187 uint32_t colCount
= row
.Length();
2188 for (colIndex
= 0; colIndex
< colCount
; colIndex
++) {
2189 CellData
* cd
= row
[colIndex
];
2192 printf("C%d,%d ", rowIndex
, colIndex
);
2194 if (cd
->IsRowSpan()) {
2197 if (cd
->IsColSpan()) {
2200 if (!(cd
->IsRowSpan() && cd
->IsColSpan())) {
2209 if (aIsBorderCollapse
) {
2211 BCBorderOwner owner
;
2215 for (int32_t i
= 0; i
<= 2; i
++) {
2217 for (colIndex
= 0; colIndex
< colCount
; colIndex
++) {
2218 BCCellData
* cd
= (BCCellData
*)row
[colIndex
];
2221 size
= cd
->mData
.GetBStartEdge(owner
, segStart
);
2222 printf("t=%d%d%d ", int32_t(size
), owner
, segStart
);
2223 } else if (1 == i
) {
2224 size
= cd
->mData
.GetIStartEdge(owner
, segStart
);
2225 printf("l=%d%d%d ", int32_t(size
), owner
, segStart
);
2227 size
= cd
->mData
.GetCorner(side
, bevel
);
2228 printf("c=%d%d%d ", int32_t(size
), side
, bevel
);
2237 // output info mapping Ci,j to cell address
2238 for (uint32_t rIndex
= 0; rIndex
< mapRowCount
; rIndex
++) {
2239 const CellDataArray
& row
= mRows
[rIndex
];
2240 uint32_t colCount
= row
.Length();
2242 for (colIndex
= 0; colIndex
< colCount
; colIndex
++) {
2243 CellData
* cd
= row
[colIndex
];
2246 nsTableCellFrame
* cellFrame
= cd
->GetCellFrame();
2247 uint32_t cellFrameColIndex
= cellFrame
->ColIndex();
2248 printf("C%d,%d=%p(%u) ", rIndex
, colIndex
, (void*)cellFrame
,
2256 printf(" ***** END GROUP CELL MAP DUMP *****\n");
2260 CellData
* nsCellMap::GetDataAt(int32_t aMapRowIndex
, int32_t aColIndex
) const {
2261 return mRows
.SafeElementAt(aMapRowIndex
, *sEmptyRow
).SafeElementAt(aColIndex
);
2264 // only called if the cell at aMapRowIndex, aColIndex is null or dead
2265 // (the latter from ExpandZeroColSpans (XXXmats which has now been removed -
2266 // are there other ways cells may be dead?)).
2267 void nsCellMap::SetDataAt(nsTableCellMap
& aMap
, CellData
& aNewCell
,
2268 int32_t aMapRowIndex
, int32_t aColIndex
) {
2269 NS_ASSERTION(!!aMap
.mBCInfo
== mIsBC
, "BC state mismatch");
2270 if (uint32_t(aMapRowIndex
) >= mRows
.Length()) {
2271 NS_ERROR("SetDataAt called with row index > num rows");
2275 CellDataArray
& row
= mRows
[aMapRowIndex
];
2277 // the table map may need cols added
2278 int32_t numColsToAdd
= aColIndex
+ 1 - aMap
.GetColCount();
2279 if (numColsToAdd
> 0) {
2280 aMap
.AddColsAtEnd(numColsToAdd
);
2282 // the row may need cols added
2283 numColsToAdd
= aColIndex
+ 1 - row
.Length();
2284 if (numColsToAdd
> 0) {
2285 // XXXbz need to handle allocation failures.
2286 GrowRow(row
, numColsToAdd
);
2289 DestroyCellData(row
[aColIndex
]);
2291 row
.ReplaceElementsAt(aColIndex
, 1, &aNewCell
);
2292 // update the originating cell counts if cell originates in this row, col
2293 nsColInfo
* colInfo
= aMap
.GetColInfoAt(aColIndex
);
2295 if (aNewCell
.IsOrig()) {
2296 colInfo
->mNumCellsOrig
++;
2297 } else if (aNewCell
.IsColSpan()) {
2298 colInfo
->mNumCellsSpan
++;
2301 NS_ERROR("SetDataAt called with col index > table map num cols");
2304 nsTableCellFrame
* nsCellMap::GetCellInfoAt(const nsTableCellMap
& aMap
,
2305 int32_t aRowX
, int32_t aColX
,
2307 int32_t* aColSpan
) const {
2309 *aOriginates
= false;
2311 CellData
* data
= GetDataAt(aRowX
, aColX
);
2312 nsTableCellFrame
* cellFrame
= nullptr;
2314 if (data
->IsOrig()) {
2315 cellFrame
= data
->GetCellFrame();
2316 if (aOriginates
) *aOriginates
= true;
2318 cellFrame
= GetCellFrame(aRowX
, aColX
, *data
, true);
2320 if (cellFrame
&& aColSpan
) {
2321 uint32_t initialColIndex
= cellFrame
->ColIndex();
2322 *aColSpan
= GetEffectiveColSpan(aMap
, aRowX
, initialColIndex
);
2328 bool nsCellMap::RowIsSpannedInto(int32_t aRowIndex
, int32_t aNumEffCols
) const {
2329 if ((0 > aRowIndex
) || (aRowIndex
>= mContentRowCount
)) {
2332 for (int32_t colIndex
= 0; colIndex
< aNumEffCols
; colIndex
++) {
2333 CellData
* cd
= GetDataAt(aRowIndex
, colIndex
);
2334 if (cd
) { // there's really a cell at (aRowIndex, colIndex)
2335 if (cd
->IsSpan()) { // the cell at (aRowIndex, colIndex) is the result of
2337 if (cd
->IsRowSpan() && GetCellFrame(aRowIndex
, colIndex
, *cd
,
2338 true)) { // XXX why the last check
2347 bool nsCellMap::RowHasSpanningCells(int32_t aRowIndex
,
2348 int32_t aNumEffCols
) const {
2349 if ((0 > aRowIndex
) || (aRowIndex
>= mContentRowCount
)) {
2352 if (aRowIndex
!= mContentRowCount
- 1) {
2353 // aRowIndex is not the last row, so we check the next row after aRowIndex
2355 for (int32_t colIndex
= 0; colIndex
< aNumEffCols
; colIndex
++) {
2356 CellData
* cd
= GetDataAt(aRowIndex
, colIndex
);
2357 if (cd
&& (cd
->IsOrig())) { // cell originates
2358 CellData
* cd2
= GetDataAt(aRowIndex
+ 1, colIndex
);
2359 if (cd2
&& cd2
->IsRowSpan()) { // cd2 is spanned by a row
2360 if (cd
->GetCellFrame() ==
2361 GetCellFrame(aRowIndex
+ 1, colIndex
, *cd2
, true)) {
2371 void nsCellMap::DestroyCellData(CellData
* aData
) {
2377 BCCellData
* bcData
= static_cast<BCCellData
*>(aData
);
2378 bcData
->~BCCellData();
2379 mPresContext
->PresShell()->FreeByObjectID(eArenaObjectID_BCCellData
,
2383 mPresContext
->PresShell()->FreeByObjectID(eArenaObjectID_CellData
, aData
);
2387 CellData
* nsCellMap::AllocCellData(nsTableCellFrame
* aOrigCell
) {
2390 (BCCellData
*)mPresContext
->PresShell()->AllocateByObjectID(
2391 eArenaObjectID_BCCellData
, sizeof(BCCellData
));
2393 new (data
) BCCellData(aOrigCell
);
2398 CellData
* data
= (CellData
*)mPresContext
->PresShell()->AllocateByObjectID(
2399 eArenaObjectID_CellData
, sizeof(CellData
));
2401 new (data
) CellData(aOrigCell
);
2406 void nsCellMapColumnIterator::AdvanceRowGroup() {
2408 mCurMapStart
+= mCurMapContentRowCount
;
2409 mCurMap
= mCurMap
->GetNextSibling();
2411 // Set mCurMapContentRowCount and mCurMapRelevantRowCount to 0 in case
2412 // mCurMap has no next sibling. This can happen if we just handled the
2413 // last originating cell. Future calls will end up with mFoundCells ==
2414 // mOrigCells, but for this one mFoundCells was definitely not big enough
2416 mCurMapContentRowCount
= 0;
2417 mCurMapRelevantRowCount
= 0;
2421 mCurMapContentRowCount
= mCurMap
->GetRowCount();
2422 uint32_t rowArrayLength
= mCurMap
->mRows
.Length();
2423 mCurMapRelevantRowCount
= std::min(mCurMapContentRowCount
, rowArrayLength
);
2424 } while (0 == mCurMapRelevantRowCount
);
2426 NS_ASSERTION(mCurMapRelevantRowCount
!= 0 || !mCurMap
,
2427 "How did that happen?");
2429 // Set mCurMapRow to 0, since cells can't span across table row groups.
2433 void nsCellMapColumnIterator::IncrementRow(int32_t aIncrement
) {
2434 MOZ_ASSERT(aIncrement
>= 0, "Bogus increment");
2435 MOZ_ASSERT(mCurMap
, "Bogus mOrigCells?");
2436 if (aIncrement
== 0) {
2439 mCurMapRow
+= aIncrement
;
2440 if (mCurMapRow
>= mCurMapRelevantRowCount
) {
2446 nsTableCellFrame
* nsCellMapColumnIterator::GetNextFrame(int32_t* aRow
,
2447 int32_t* aColSpan
) {
2448 // Fast-path for the case when we don't have anything left in the column and
2450 if (mFoundCells
== mOrigCells
) {
2457 NS_ASSERTION(mCurMapRow
< mCurMapRelevantRowCount
, "Bogus mOrigCells?");
2458 // Safe to just get the row (which is faster than calling GetDataAt(), but
2459 // there may not be that many cells in it, so have to use SafeElementAt for
2461 const nsCellMap::CellDataArray
& row
= mCurMap
->mRows
[mCurMapRow
];
2462 CellData
* cellData
= row
.SafeElementAt(mCol
);
2463 if (!cellData
|| cellData
->IsDead()) {
2464 // Could hit this if there are fewer cells in this row than others, for
2470 if (cellData
->IsColSpan()) {
2471 // Look up the originating data for this cell, advance by its relative
2473 int32_t rowspanOffset
= cellData
->GetRowSpanOffset();
2474 nsTableCellFrame
* cellFrame
=
2475 mCurMap
->GetCellFrame(mCurMapRow
, mCol
, *cellData
, false);
2476 NS_ASSERTION(cellFrame
, "Must have usable originating data here");
2477 int32_t rowSpan
= cellFrame
->GetRowSpan();
2481 IncrementRow(rowSpan
- rowspanOffset
);
2486 NS_ASSERTION(cellData
->IsOrig(),
2487 "Must have originating cellData by this point. "
2488 "See comment on mCurMapRow in header.");
2490 nsTableCellFrame
* cellFrame
= cellData
->GetCellFrame();
2491 NS_ASSERTION(cellFrame
, "Orig data without cellframe?");
2493 *aRow
= mCurMapStart
+ mCurMapRow
;
2494 *aColSpan
= mCurMap
->GetEffectiveColSpan(*mMap
, mCurMapRow
, mCol
);
2496 IncrementRow(cellFrame
->GetRowSpan());
2500 MOZ_ASSERT(cellData
== mMap
->GetDataAt(*aRow
, mCol
),
2501 "Giving caller bogus row?");
2506 MOZ_ASSERT_UNREACHABLE("Can't get here");