Backed out changeset 496886cb30a5 (bug 1867152) for bc failures on browser_user_input...
[gecko.git] / layout / tables / nsCellMap.cpp
bloba13a6a1c3a389eb448a1655c102e72b85cb5c97a
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/. */
6 #include "nsCellMap.h"
8 #include "mozilla/PresShell.h"
9 #include "mozilla/StaticPtr.h"
10 #include "nsTArray.h"
11 #include "nsTableFrame.h"
12 #include "nsTableCellFrame.h"
13 #include "nsTableRowFrame.h"
14 #include "nsTableRowGroupFrame.h"
15 #include <algorithm>
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;
35 // CellData
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); }
52 // nsTableCellMap
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);
64 prior = rgFrame;
66 if (aBorderCollapse) {
67 mBCInfo = new BCInfo();
71 nsTableCellMap::~nsTableCellMap() {
72 MOZ_COUNT_DTOR(nsTableCellMap);
74 nsCellMap* cellMap = mFirstMap;
75 while (cellMap) {
76 nsCellMap* next = cellMap->GetNextSibling();
77 delete cellMap;
78 cellMap = next;
81 if (mBCInfo) {
82 DeleteIEndBEndBorders();
83 delete mBCInfo;
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() {
115 if (mBCInfo) {
116 mBCInfo->mBEndBorders.Clear();
117 mBCInfo->mIEndBorders.Clear();
121 void nsTableCellMap::InsertGroupCellMap(nsCellMap* aPrevMap,
122 nsCellMap& aNewMap) {
123 nsCellMap* next;
124 if (aPrevMap) {
125 next = aPrevMap->GetNextSibling();
126 aPrevMap->SetNextSibling(&aNewMap);
127 } else {
128 next = mFirstMap;
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;
139 if (aPrevGroup) {
140 nsCellMap* map = mFirstMap;
141 while (map) {
142 lastMap = map;
143 if (map->GetRowGroup() == aPrevGroup) {
144 prevMap = map;
145 break;
147 map = map->GetNextSibling();
150 if (!prevMap) {
151 if (aPrevGroup) {
152 prevMap = lastMap;
153 aPrevGroup = (prevMap) ? prevMap->GetRowGroup() : nullptr;
154 } else {
155 aPrevGroup = nullptr;
158 InsertGroupCellMap(prevMap, *newMap);
161 void nsTableCellMap::RemoveGroupCellMap(nsTableRowGroupFrame* aGroup) {
162 nsCellMap* map = mFirstMap;
163 nsCellMap* prior = nullptr;
164 while (map) {
165 if (map->GetRowGroup() == aGroup) {
166 nsCellMap* next = map->GetNextSibling();
167 if (mFirstMap == map) {
168 mFirstMap = next;
169 } else {
170 prior->SetNextSibling(next);
172 delete map;
173 break;
175 prior = map;
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()) {
184 return map;
188 return nullptr;
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");
196 if (aStartHint) {
197 nsCellMap* map = FindMapFor(aRowGroup, aStartHint, nullptr);
198 if (map) {
199 return map;
203 nsCellMap* map = FindMapFor(aRowGroup, mFirstMap, aStartHint);
204 if (map) {
205 return map;
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);
232 return nullptr;
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");
242 return nullptr;
245 void nsTableCellMap::Synchronize(nsTableFrame* aTableFrame) {
246 AutoTArray<nsCellMap*, 8> maps;
248 nsTableFrame::RowGroupArray orderedRowGroups =
249 aTableFrame->OrderedRowGroups();
250 if (!orderedRowGroups.Length()) {
251 return;
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
256 // that case).
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()),
263 map);
264 if (map) {
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);
272 return;
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);
281 nextMap = map;
283 mFirstMap = nextMap;
286 bool nsTableCellMap::HasMoreThanOneCell(int32_t aRowIndex) const {
287 int32_t rowIndex = aRowIndex;
288 nsCellMap* map = mFirstMap;
289 while (map) {
290 if (map->GetRowCount() > rowIndex) {
291 return map->HasMoreThanOneCell(rowIndex);
293 rowIndex -= map->GetRowCount();
294 map = map->GetNextSibling();
296 return false;
299 int32_t nsTableCellMap::GetNumCellsOriginatingInRow(int32_t aRowIndex) const {
300 int32_t rowIndex = aRowIndex;
301 nsCellMap* map = mFirstMap;
302 while (map) {
303 if (map->GetRowCount() > rowIndex) {
304 return map->GetNumCellsOriginatingInRow(rowIndex);
306 rowIndex -= map->GetRowCount();
307 map = map->GetNextSibling();
309 return 0;
311 int32_t nsTableCellMap::GetEffectiveRowSpan(int32_t aRowIndex,
312 int32_t aColIndex) const {
313 int32_t rowIndex = aRowIndex;
314 nsCellMap* map = mFirstMap;
315 while (map) {
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?");
323 return 0;
326 int32_t nsTableCellMap::GetEffectiveColSpan(int32_t aRowIndex,
327 int32_t aColIndex) const {
328 int32_t rowIndex = aRowIndex;
329 nsCellMap* map = mFirstMap;
330 while (map) {
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?");
338 return 0;
341 nsTableCellFrame* nsTableCellMap::GetCellFrame(int32_t aRowIndex,
342 int32_t aColIndex,
343 CellData& aData,
344 bool aUseRowIfOverlap) const {
345 int32_t rowIndex = aRowIndex;
346 nsCellMap* map = mFirstMap;
347 while (map) {
348 if (map->GetRowCount() > rowIndex) {
349 return map->GetCellFrame(rowIndex, aColIndex, aData, aUseRowIfOverlap);
351 rowIndex -= map->GetRowCount();
352 map = map->GetNextSibling();
354 return nullptr;
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 {
366 int32_t numRows = 0;
367 nsCellMap* map = mFirstMap;
368 while (map) {
369 numRows += map->GetRowCount();
370 map = map->GetNextSibling();
372 return numRows;
375 CellData* nsTableCellMap::GetDataAt(int32_t aRowIndex,
376 int32_t aColIndex) const {
377 int32_t rowIndex = aRowIndex;
378 nsCellMap* map = mFirstMap;
379 while (map) {
380 if (map->GetRowCount() > rowIndex) {
381 return map->GetDataAt(rowIndex, aColIndex);
383 rowIndex -= map->GetRowCount();
384 map = map->GetNextSibling();
386 return nullptr;
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);
393 if (mBCInfo) {
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
403 // eColAnonymousCell
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);
412 if (mBCInfo) {
413 int32_t count = mBCInfo->mBEndBorders.Length();
414 if (colX < count) {
415 mBCInfo->mBEndBorders.RemoveElementAt(colX);
418 } else
419 break; // only remove until we encounter the 1st valid one
423 void nsTableCellMap::ClearCols() {
424 mCols.Clear();
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;
437 while (cellMap) {
438 nsTableRowGroupFrame* rg = cellMap->GetRowGroup();
439 if (rg == aParent) {
440 cellMap->InsertRows(*this, aRows, rowIndex, aConsiderSpans,
441 rgStartRowIndex, aDamageArea);
442 #ifdef DEBUG_TABLE_CELLMAP
443 Dump("after InsertRows");
444 #endif
445 if (mBCInfo) {
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);
452 } else {
453 GetIEndMostBorder(
454 aFirstRowIndex); // this will create missing entries
455 for (int32_t rowX = aFirstRowIndex + 1;
456 rowX < aFirstRowIndex + numNewRows; rowX++) {
457 mBCInfo->mIEndBorders.AppendElement();
461 return;
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;
478 while (cellMap) {
479 int32_t rowCount = cellMap->GetRowCount();
480 if (rowCount > rowIndex) {
481 cellMap->RemoveRows(*this, rowIndex, aNumRowsToRemove, aConsiderSpans,
482 rgStartRowIndex, aDamageArea);
483 if (mBCInfo) {
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);
491 break;
493 rgStartRowIndex += rowCount;
494 rowIndex -= rowCount;
495 cellMap = cellMap->GetNextSibling();
497 #ifdef DEBUG_TABLE_CELLMAP
498 Dump("after RemoveRows");
499 #endif
502 CellData* nsTableCellMap::AppendCell(nsTableCellFrame& aCellFrame,
503 int32_t aRowIndex,
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;
517 while (cellMap) {
518 if (cellMap->GetRowGroup() == rgFrame) {
519 result =
520 cellMap->AppendCell(*this, &aCellFrame, rowIndex, aRebuildIfNecessary,
521 rgStartRowIndex, aDamageArea);
522 break;
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");
531 #endif
532 return result;
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;
541 while (cellMap) {
542 int32_t rowCount = cellMap->GetRowCount();
543 if (rowCount > rowIndex) {
544 cellMap->InsertCells(*this, aCellFrames, rowIndex, aColIndexBefore,
545 rgStartRowIndex, aDamageArea);
546 break;
548 rgStartRowIndex += rowCount;
549 rowIndex -= rowCount;
550 cellMap = cellMap->GetNextSibling();
552 #ifdef DEBUG_TABLE_CELLMAP
553 Dump("after InsertCells");
554 #endif
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;
565 while (cellMap) {
566 int32_t rowCount = cellMap->GetRowCount();
567 if (rowCount > rowIndex) {
568 cellMap->RemoveCell(*this, aCellFrame, rowIndex, rgStartRowIndex,
569 aDamageArea);
570 #ifdef DEBUG_TABLE_CELLMAP
571 Dump("after RemoveCell");
572 #endif
573 return;
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
582 // referenced later
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();
591 ClearCols();
592 nsCellMap* cellMap = mFirstMap;
593 int32_t rowCount = 0;
594 while (cellMap) {
595 if (cellMap == aCellMap) {
596 cellMap->RebuildConsideringCells(*this, numOrigCols, aCellFrames,
597 aRowIndex, aColIndex, aInsert);
598 } else {
599 cellMap->RebuildConsideringCells(*this, numOrigCols, nullptr, -1, 0,
600 false);
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();
616 ClearCols();
617 nsCellMap* cellMap = mFirstMap;
618 int32_t rowCount = 0;
619 while (cellMap) {
620 if (cellMap == aCellMap) {
621 cellMap->RebuildConsideringRows(*this, aStartRowIndex, aRowsToInsert,
622 aNumRowsToRemove);
623 } else {
624 cellMap->RebuildConsideringCells(*this, numOrigCols, nullptr, -1, 0,
625 false);
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;
637 } else {
638 NS_ERROR("nsCellMap::GetNumCellsOriginatingInCol - bad col index");
639 return 0;
643 #ifdef DEBUG
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);
647 // output col info
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;
656 while (cellMap) {
657 cellMap->Dump(nullptr != mBCInfo);
658 cellMap = cellMap->GetNextSibling();
660 if (nullptr != mBCInfo) {
661 printf("***** block-end borders *****\n");
662 nscoord size;
663 BCBorderOwner owner;
664 LogicalSide side;
665 bool segStart;
666 bool bevel;
667 int32_t colIndex;
668 int32_t numCols = mBCInfo->mBEndBorders.Length();
669 for (int32_t i = 0; i <= 2; i++) {
670 printf("\n ");
671 for (colIndex = 0; colIndex < numCols; colIndex++) {
672 BCData& cd = mBCInfo->mBEndBorders.ElementAt(colIndex);
673 if (0 == i) {
674 size = cd.GetBStartEdge(owner, segStart);
675 printf("t=%d%X%d ", int32_t(size), owner, segStart);
676 } else if (1 == i) {
677 size = cd.GetIStartEdge(owner, segStart);
678 printf("l=%d%X%d ", int32_t(size), owner, segStart);
679 } else {
680 size = cd.GetCorner(side, bevel);
681 printf("c=%d%X%d ", int32_t(size), side, bevel);
684 BCData& cd = mBCInfo->mBEndIEndCorner;
685 if (0 == i) {
686 size = cd.GetBStartEdge(owner, segStart);
687 printf("t=%d%X%d ", int32_t(size), owner, segStart);
688 } else if (1 == i) {
689 size = cd.GetIStartEdge(owner, segStart);
690 printf("l=%d%X%d ", int32_t(size), owner, segStart);
691 } else {
692 size = cd.GetCorner(side, bevel);
693 printf("c=%d%X%d ", int32_t(size), side, bevel);
696 printf("\n");
698 printf("***** END TABLE CELL MAP DUMP *****\n");
700 #endif
702 nsTableCellFrame* nsTableCellMap::GetCellInfoAt(int32_t aRowIndex,
703 int32_t aColIndex,
704 bool* aOriginates,
705 int32_t* aColSpan) const {
706 int32_t rowIndex = aRowIndex;
707 nsCellMap* cellMap = mFirstMap;
708 while (cellMap) {
709 if (cellMap->GetRowCount() > rowIndex) {
710 return cellMap->GetCellInfoAt(*this, rowIndex, aColIndex, aOriginates,
711 aColSpan);
713 rowIndex -= cellMap->GetRowCount();
714 cellMap = cellMap->GetNextSibling();
716 return nullptr;
719 int32_t nsTableCellMap::GetIndexByRowAndColumn(int32_t aRow,
720 int32_t aColumn) const {
721 int32_t index = 0;
723 int32_t colCount = mCols.Length();
724 int32_t rowIndex = aRow;
726 nsCellMap* cellMap = mFirstMap;
727 while (cellMap) {
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
732 // the last row.
733 rowIndex -= rowCount;
735 int32_t cellMapIdx = cellMap->GetHighestIndex(colCount);
736 if (cellMapIdx != -1) index += cellMapIdx + 1;
738 } else {
739 // Index is in valid range for this cellmap, so get the index of rowIndex
740 // and aColumn.
741 int32_t cellMapIdx =
742 cellMap->GetIndexByRowAndColumn(colCount, rowIndex, aColumn);
743 if (cellMapIdx == -1) return -1; // no cell at the given row and column.
745 index += cellMapIdx;
746 return index; // no need to look through further maps here
749 cellMap = cellMap->GetNextSibling();
752 return -1;
755 void nsTableCellMap::GetRowAndColumnByIndex(int32_t aIndex, int32_t* aRow,
756 int32_t* aColumn) const {
757 *aRow = -1;
758 *aColumn = -1;
760 int32_t colCount = mCols.Length();
762 int32_t previousRows = 0;
763 int32_t index = aIndex;
765 nsCellMap* cellMap = mFirstMap;
766 while (cellMap) {
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
773 // accordingly.
774 previousRows += rowCount;
775 } else {
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;
781 } else {
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;
797 while (cellMap) {
798 if (cellMap->GetRowCount() > rowIndex) {
799 return cellMap->RowIsSpannedInto(rowIndex, aNumEffCols);
801 rowIndex -= cellMap->GetRowCount();
802 cellMap = cellMap->GetNextSibling();
804 return false;
807 bool nsTableCellMap::RowHasSpanningCells(int32_t aRowIndex,
808 int32_t aNumEffCols) const {
809 int32_t rowIndex = aRowIndex;
810 nsCellMap* cellMap = mFirstMap;
811 while (cellMap) {
812 if (cellMap->GetRowCount() > rowIndex) {
813 return cellMap->RowHasSpanningCells(rowIndex, aNumEffCols);
815 rowIndex -= cellMap->GetRowCount();
816 cellMap = cellMap->GetNextSibling();
818 return false;
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;
831 switch (aSide) {
832 case eLogicalSideBEnd:
833 aRowIndex++;
834 [[fallthrough]];
835 case eLogicalSideBStart:
836 cellData = (BCCellData*)aCellMap.GetDataAt(aRowIndex - aRowGroupStart,
837 aColIndex);
838 if (cellData) {
839 bcData = &cellData->mData;
840 } else {
841 NS_ASSERTION(aSide == eLogicalSideBEnd, "program error");
842 // try the next row group
843 nsCellMap* cellMap = aCellMap.GetNextSibling();
844 if (cellMap) {
845 cellData = (BCCellData*)cellMap->GetDataAt(0, aColIndex);
846 if (cellData) {
847 bcData = &cellData->mData;
848 } else {
849 bcData = GetBEndMostBorder(aColIndex);
853 break;
854 case eLogicalSideIEnd:
855 aColIndex++;
856 [[fallthrough]];
857 case eLogicalSideIStart:
858 cellData = (BCCellData*)aCellMap.GetDataAt(aRowIndex - aRowGroupStart,
859 aColIndex);
860 if (cellData) {
861 bcData = &cellData->mData;
862 } else {
863 NS_ASSERTION(aSide == eLogicalSideIEnd, "program error");
864 bcData = GetIEndMostBorder(aRowIndex);
866 break;
868 if (bcData) {
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,
882 bool aChanged) {
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;
890 bool changed;
892 switch (aSide) {
893 case eLogicalSideBEnd:
894 rgYPos++;
895 yPos++;
896 [[fallthrough]];
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);
903 if (!cellData) {
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();
910 } else {
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();
917 if (cellMap) {
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;
932 if (bcData) {
933 bcData->SetBStartEdge(aOwner, aSize, changed);
934 } else
935 NS_ERROR("Cellmap: BStart edge not found");
937 break;
938 case eLogicalSideIEnd:
939 xPos++;
940 [[fallthrough]];
941 case eLogicalSideIStart:
942 // since bStart, bEnd borders were set, there should already be a cellData
943 // entry
944 lastIndex = rgYPos + aLength - 1;
945 for (yIndex = rgYPos; yIndex <= lastIndex; yIndex++) {
946 changed = aChanged && (yIndex == rgYPos);
947 cellData = (BCCellData*)aCellMap.GetDataAt(yIndex, xPos);
948 if (cellData) {
949 cellData->mData.SetIStartEdge(aOwner, aSize, changed);
950 } else {
951 NS_ASSERTION(aSide == eLogicalSideIEnd, "program error");
952 BCData* bcData = GetIEndMostBorder(yIndex + aCellMapStart);
953 if (bcData) {
954 bcData->SetIStartEdge(aOwner, aSize, changed);
955 } else
956 NS_ERROR("Cellmap: IStart edge not found");
959 break;
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,
968 nsCellMap& aCellMap,
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();
975 if (aIsBEndIEnd) {
976 mBCInfo->mBEndIEndCorner.SetCorner(aSubSize, aOwner, aBevel);
977 return;
980 int32_t xPos = aColIndex;
981 int32_t yPos = aRowIndex;
982 int32_t rgYPos = aRowIndex - aCellMapStart;
984 if (eLogicalCornerBStartIEnd == aCorner) {
985 xPos++;
986 } else if (eLogicalCornerBEndIEnd == aCorner) {
987 xPos++;
988 rgYPos++;
989 yPos++;
990 } else if (eLogicalCornerBEndIStart == aCorner) {
991 rgYPos++;
992 yPos++;
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);
1002 } else {
1003 cellData = (BCCellData*)aCellMap.GetDataAt(rgYPos, xPos);
1004 if (!cellData) {
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);
1010 } else {
1011 // try the next non empty row group
1012 nsCellMap* cellMap = aCellMap.GetNextSibling();
1013 while (cellMap && (0 == cellMap->GetRowCount())) {
1014 cellMap = cellMap->GetNextSibling();
1016 if (cellMap) {
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;
1032 if (bcData) {
1033 bcData->SetCorner(aSubSize, aOwner, aBevel);
1034 } else
1035 NS_ERROR("program error: Corner not found");
1038 nsCellMap::nsCellMap(nsTableRowGroupFrame* aRowGroup, bool aIsBC)
1039 : mRows(8),
1040 mContentRowCount(0),
1041 mRowGroupFrame(aRowGroup),
1042 mNextSibling(nullptr),
1043 mIsBC(aIsBC),
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]);
1062 /* static */
1063 void nsCellMap::Init() {
1064 MOZ_ASSERT(!sEmptyRow, "How did that happen?");
1065 sEmptyRow = new nsCellMap::CellDataArray();
1068 /* static */
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;
1079 } else {
1080 rowIndex = aRowIndexIn;
1084 CellData* data =
1085 mRows.SafeElementAt(rowIndex, *sEmptyRow).SafeElementAt(colIndex);
1086 if (data) {
1087 return data->GetCellFrame();
1089 return nullptr;
1092 int32_t nsCellMap::GetHighestIndex(int32_t aColCount) {
1093 int32_t index = -1;
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.
1101 if (!data) break;
1103 if (data->IsOrig()) index++;
1107 return 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;
1114 int32_t index = -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.
1130 if (!data) break;
1132 if (data->IsOrig()) index++;
1136 // Given row and column don't point to the cell.
1137 if (!data) return -1;
1139 return index;
1142 void nsCellMap::GetRowAndColumnByIndex(int32_t aColCount, int32_t aIndex,
1143 int32_t* aRow, int32_t* aColumn) const {
1144 *aRow = -1;
1145 *aColumn = -1;
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.
1157 if (!data) break;
1159 if (data->IsOrig()) index--;
1161 if (index < 0) {
1162 *aRow = rowIdx;
1163 *aColumn = colIdx;
1164 return;
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();
1176 if (numCols == 0) {
1177 numCols = 4;
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
1183 // operation.
1184 mRows.InsertElementsAt(startRowIndex, aNumRows, numCols);
1185 return true;
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)) {
1206 return;
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);
1214 return;
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
1226 // the new rows
1227 if (!spansCauseRebuild && (uint32_t(aFirstRowIndex) < mRows.Length())) {
1228 spansCauseRebuild = CellsSpanOut(aRows);
1230 if (spansCauseRebuild) {
1231 aMap.RebuildConsideringRows(this, aFirstRowIndex, &aRows, 0, aDamageArea);
1232 } else {
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;
1248 return;
1250 if (!aConsiderSpans) {
1251 ShrinkWithoutRows(aMap, aFirstRowIndex, aNumRowsToRemove, aRgFirstRowIndex,
1252 aDamageArea);
1253 return;
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,
1264 aDamageArea);
1265 } else {
1266 ShrinkWithoutRows(aMap, aFirstRowIndex, aNumRowsToRemove, aRgFirstRowIndex,
1267 aDamageArea);
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;
1281 int32_t rowSpan =
1282 (aCellFrame) ? GetRowSpanForNewCell(aCellFrame, aRowIndex, zeroRowSpan)
1283 : 1;
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);
1298 if (!data) break;
1299 // The border collapse code relies on having multiple dead cell data entries
1300 // in a row.
1301 if (data->IsDead() && aCellFrame) {
1302 origData = data;
1303 break;
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) &&
1316 (rowSpan > 1)) {
1317 AutoTArray<nsTableCellFrame*, 1> newCellArray;
1318 newCellArray.AppendElement(aCellFrame);
1319 aMap.RebuildConsideringCells(this, &newCellArray, aRowIndex, startColIndex,
1320 true, aDamageArea);
1321 return origData;
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
1333 if (origData) {
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");
1342 if (colInfo) {
1343 colInfo->mNumCellsOrig++;
1346 } else {
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);
1362 if (!aCellFrame) {
1363 return origData;
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);
1378 if (cellData) {
1379 if (cellData->IsOrig()) {
1380 NS_ERROR("cannot overlap originating cell");
1381 continue;
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
1387 } else {
1388 cellData->SetRowSpanOffset(rowX - aRowIndex);
1389 if (zeroRowSpan) {
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++;
1404 } else {
1405 cellData = AllocCellData(nullptr);
1406 if (!cellData) return origData;
1407 if (rowX > aRowIndex) {
1408 cellData->SetRowSpanOffset(rowX - aRowIndex);
1409 if (zeroRowSpan) {
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);
1423 aMap.Dump();
1424 #endif
1425 return origData;
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()) {
1434 bool zeroSpan;
1435 int32_t rowSpan = GetRowSpanForNewCell(cellFrame, rowX, zeroSpan);
1436 if (zeroSpan || rowX + rowSpan > numNewRows) {
1437 return true;
1441 return false;
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
1452 * are spanned out.
1454 * startCol endCol
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++) {
1465 CellData* cellData;
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
1483 } else {
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
1503 return false;
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) {
1514 NS_ERROR(
1515 "Inserting instead of appending cells indicates a serious cellmap "
1516 "error");
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;
1523 startColIndex++) {
1524 CellData* data = GetDataAt(aRowIndex, startColIndex);
1525 if (!data || data->IsOrig() || data->IsDead()) {
1526 // // Not a span. Stop.
1527 break;
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);
1542 if (rowSpan == 0) {
1543 rowSpan = rowSpan2;
1544 } else if (rowSpan != rowSpan2) {
1545 spansCauseRebuild = true;
1546 break;
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,
1563 true, aDamageArea);
1564 } else {
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)) {
1587 return;
1590 int32_t newRowIndex = 0;
1591 for (int32_t rowX = startRowIndex; rowX <= endRowIndex; rowX++) {
1592 nsTableRowFrame* rFrame = aRowFrames.ElementAt(newRowIndex);
1593 // append cells
1594 int32_t colIndex = 0;
1595 for (nsTableCellFrame* cellFrame = rFrame->GetFirstCell(); cellFrame;
1596 cellFrame = cellFrame->GetNextCell()) {
1597 AppendCell(aMap, cellFrame, rowX, false, aRgFirstRowIndex, aDamageArea,
1598 &colIndex);
1600 newRowIndex++;
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;
1631 if (cellX == 0) {
1632 endColIndex = aColIndex + colSpan - 1;
1633 } else {
1634 startColIndex = endColIndex + 1;
1635 endColIndex = startColIndex + colSpan - 1;
1638 // add the originating cell data and any cell data corresponding to row/col
1639 // spans
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
1643 // them to null.
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);
1656 if (!data) return;
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);
1677 int32_t rowX;
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();
1683 uint32_t colX;
1684 for (colX = aColIndex + totalColSpan; colX < numCols; colX++) {
1685 CellData* data = row[colX];
1686 if (data) {
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
1690 // orignating col
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];
1724 uint32_t colX;
1725 for (colX = 0; colX < colCount; colX++) {
1726 CellData* data = row.SafeElementAt(colX);
1727 if (data) {
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.
1751 mContentRowCount--;
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,
1762 int32_t aRowIndex,
1763 int32_t aColIndex) const {
1764 int32_t numColsInTable = aMap.GetColCount();
1765 int32_t colSpan = 1;
1766 if (uint32_t(aRowIndex) >= mRows.Length()) {
1767 return colSpan;
1770 const CellDataArray& row = mRows[aRowIndex];
1771 int32_t colX;
1772 CellData* data;
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);
1777 if (data) {
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();
1785 if (cellFrame) {
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()) {
1793 colSpan++;
1794 } else {
1795 break;
1797 } else
1798 break;
1800 return colSpan;
1803 int32_t nsCellMap::GetRowSpanForNewCell(nsTableCellFrame* aCellFrameToAdd,
1804 int32_t aRowIndex,
1805 bool& aIsZeroRowSpan) const {
1806 aIsZeroRowSpan = false;
1807 int32_t rowSpan = aCellFrameToAdd->GetRowSpan();
1808 if (0 == rowSpan) {
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;
1814 return rowSpan;
1817 bool nsCellMap::HasMoreThanOneCell(int32_t aRowIndex) const {
1818 const CellDataArray& row = mRows.SafeElementAt(aRowIndex, *sEmptyRow);
1819 uint32_t maxColIndex = row.Length();
1820 uint32_t colIndex;
1821 bool foundOne = false;
1822 for (colIndex = 0; colIndex < maxColIndex; colIndex++) {
1823 CellData* cellData = row[colIndex];
1824 if (cellData && (cellData->GetCellFrame() || cellData->IsRowSpan())) {
1825 if (foundOne) {
1826 return true;
1828 foundOne = true;
1831 return false;
1834 int32_t nsCellMap::GetNumCellsOriginatingInRow(int32_t aRowIndex) const {
1835 const CellDataArray& row = mRows.SafeElementAt(aRowIndex, *sEmptyRow);
1836 uint32_t count = 0;
1837 uint32_t maxColIndex = row.Length();
1838 uint32_t colIndex;
1839 for (colIndex = 0; colIndex < maxColIndex; colIndex++) {
1840 CellData* cellData = row[colIndex];
1841 if (cellData && cellData->IsOrig()) count++;
1843 return 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();
1850 int32_t rowX;
1851 for (rowX = aRowIndex + 1; rowX < rowCount; rowX++) {
1852 CellData* data = GetDataAt(rowX, aColIndex);
1853 if (data) {
1854 if (data->IsRowSpan()) {
1855 rowSpan++;
1856 } else {
1857 break;
1859 } else
1860 break;
1862 return rowSpan;
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
1874 // changed
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--;
1885 } else {
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);
1915 if (data) {
1916 if (data->IsOrig()) {
1917 // a cell that gets moved to the left needs adjustment in its new
1918 // location
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
1923 // location
1924 colInfo = aMap.GetColInfoAt(colX + colSpan);
1925 if (colInfo) {
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);
1938 if (colInfo) {
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();
1962 } else {
1963 rowNumberChange = -aNumRowsToRemove;
1966 // adjust mContentRowCount based on the function arguments as they are known
1967 // to be real rows.
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.
1974 return;
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
1983 // new cellmap.
1984 uint32_t rowX = 0;
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);
2013 rowX++;
2015 copyStartRowIndex = aStartRowIndex;
2016 } else {
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;
2024 copyRowX++) {
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);
2034 rowX++;
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?!");
2061 int32_t numCols =
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.
2066 int32_t rowX;
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);
2075 if (cell) {
2076 AppendCell(aMap, cell, rowX, false, 0, damageArea);
2079 } else {
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);
2090 if (aInsert &&
2091 numOrigRows <=
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);
2097 if (cell) {
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");
2123 return;
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,
2155 aDamageArea);
2156 } else {
2157 ShrinkWithoutCell(aMap, *aCellFrame, aRowIndex, startColIndex,
2158 aRgFirstRowIndex, aDamageArea);
2162 #ifdef DEBUG
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:
2169 printf(" thead ");
2170 break;
2171 case StyleDisplayInside::TableFooterGroup:
2172 printf(" tfoot ");
2173 break;
2174 case StyleDisplayInside::TableRowGroup:
2175 printf(" tbody ");
2176 break;
2177 default:
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];
2190 if (cd) {
2191 if (cd->IsOrig()) {
2192 printf("C%d,%d ", rowIndex, colIndex);
2193 } else {
2194 if (cd->IsRowSpan()) {
2195 printf("R ");
2197 if (cd->IsColSpan()) {
2198 printf("C ");
2200 if (!(cd->IsRowSpan() && cd->IsColSpan())) {
2201 printf(" ");
2203 printf(" ");
2205 } else {
2206 printf("---- ");
2209 if (aIsBorderCollapse) {
2210 nscoord size;
2211 BCBorderOwner owner;
2212 LogicalSide side;
2213 bool segStart;
2214 bool bevel;
2215 for (int32_t i = 0; i <= 2; i++) {
2216 printf("\n ");
2217 for (colIndex = 0; colIndex < colCount; colIndex++) {
2218 BCCellData* cd = (BCCellData*)row[colIndex];
2219 if (cd) {
2220 if (0 == i) {
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);
2226 } else {
2227 size = cd->mData.GetCorner(side, bevel);
2228 printf("c=%d%d%d ", int32_t(size), side, bevel);
2234 printf("\n");
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();
2241 printf(" ");
2242 for (colIndex = 0; colIndex < colCount; colIndex++) {
2243 CellData* cd = row[colIndex];
2244 if (cd) {
2245 if (cd->IsOrig()) {
2246 nsTableCellFrame* cellFrame = cd->GetCellFrame();
2247 uint32_t cellFrameColIndex = cellFrame->ColIndex();
2248 printf("C%d,%d=%p(%u) ", rIndex, colIndex, (void*)cellFrame,
2249 cellFrameColIndex);
2253 printf("\n");
2256 printf(" ***** END GROUP CELL MAP DUMP *****\n");
2258 #endif
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");
2272 return;
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);
2294 if (colInfo) {
2295 if (aNewCell.IsOrig()) {
2296 colInfo->mNumCellsOrig++;
2297 } else if (aNewCell.IsColSpan()) {
2298 colInfo->mNumCellsSpan++;
2300 } else
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,
2306 bool* aOriginates,
2307 int32_t* aColSpan) const {
2308 if (aOriginates) {
2309 *aOriginates = false;
2311 CellData* data = GetDataAt(aRowX, aColX);
2312 nsTableCellFrame* cellFrame = nullptr;
2313 if (data) {
2314 if (data->IsOrig()) {
2315 cellFrame = data->GetCellFrame();
2316 if (aOriginates) *aOriginates = true;
2317 } else {
2318 cellFrame = GetCellFrame(aRowX, aColX, *data, true);
2320 if (cellFrame && aColSpan) {
2321 uint32_t initialColIndex = cellFrame->ColIndex();
2322 *aColSpan = GetEffectiveColSpan(aMap, aRowX, initialColIndex);
2325 return cellFrame;
2328 bool nsCellMap::RowIsSpannedInto(int32_t aRowIndex, int32_t aNumEffCols) const {
2329 if ((0 > aRowIndex) || (aRowIndex >= mContentRowCount)) {
2330 return false;
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
2336 // a span
2337 if (cd->IsRowSpan() && GetCellFrame(aRowIndex, colIndex, *cd,
2338 true)) { // XXX why the last check
2339 return true;
2344 return false;
2347 bool nsCellMap::RowHasSpanningCells(int32_t aRowIndex,
2348 int32_t aNumEffCols) const {
2349 if ((0 > aRowIndex) || (aRowIndex >= mContentRowCount)) {
2350 return false;
2352 if (aRowIndex != mContentRowCount - 1) {
2353 // aRowIndex is not the last row, so we check the next row after aRowIndex
2354 // for spanners
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)) {
2362 return true;
2368 return false;
2371 void nsCellMap::DestroyCellData(CellData* aData) {
2372 if (!aData) {
2373 return;
2376 if (mIsBC) {
2377 BCCellData* bcData = static_cast<BCCellData*>(aData);
2378 bcData->~BCCellData();
2379 mPresContext->PresShell()->FreeByObjectID(eArenaObjectID_BCCellData,
2380 bcData);
2381 } else {
2382 aData->~CellData();
2383 mPresContext->PresShell()->FreeByObjectID(eArenaObjectID_CellData, aData);
2387 CellData* nsCellMap::AllocCellData(nsTableCellFrame* aOrigCell) {
2388 if (mIsBC) {
2389 BCCellData* data =
2390 (BCCellData*)mPresContext->PresShell()->AllocateByObjectID(
2391 eArenaObjectID_BCCellData, sizeof(BCCellData));
2392 if (data) {
2393 new (data) BCCellData(aOrigCell);
2395 return data;
2398 CellData* data = (CellData*)mPresContext->PresShell()->AllocateByObjectID(
2399 eArenaObjectID_CellData, sizeof(CellData));
2400 if (data) {
2401 new (data) CellData(aOrigCell);
2403 return data;
2406 void nsCellMapColumnIterator::AdvanceRowGroup() {
2407 do {
2408 mCurMapStart += mCurMapContentRowCount;
2409 mCurMap = mCurMap->GetNextSibling();
2410 if (!mCurMap) {
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
2415 // if we got here.
2416 mCurMapContentRowCount = 0;
2417 mCurMapRelevantRowCount = 0;
2418 break;
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.
2430 mCurMapRow = 0;
2433 void nsCellMapColumnIterator::IncrementRow(int32_t aIncrement) {
2434 MOZ_ASSERT(aIncrement >= 0, "Bogus increment");
2435 MOZ_ASSERT(mCurMap, "Bogus mOrigCells?");
2436 if (aIncrement == 0) {
2437 AdvanceRowGroup();
2438 } else {
2439 mCurMapRow += aIncrement;
2440 if (mCurMapRow >= mCurMapRelevantRowCount) {
2441 AdvanceRowGroup();
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
2449 // we know it.
2450 if (mFoundCells == mOrigCells) {
2451 *aRow = 0;
2452 *aColSpan = 1;
2453 return nullptr;
2456 while (true) {
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
2460 // the mCol.
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
2465 // example.
2466 IncrementRow(1);
2467 continue;
2470 if (cellData->IsColSpan()) {
2471 // Look up the originating data for this cell, advance by its relative
2472 // rowspan.
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();
2478 if (rowSpan == 0) {
2479 AdvanceRowGroup();
2480 } else {
2481 IncrementRow(rowSpan - rowspanOffset);
2483 continue;
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());
2498 ++mFoundCells;
2500 MOZ_ASSERT(cellData == mMap->GetDataAt(*aRow, mCol),
2501 "Giving caller bogus row?");
2503 return cellFrame;
2506 MOZ_ASSERT_UNREACHABLE("Can't get here");
2507 return nullptr;