forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsCellMap.cpp
2716 lines (2476 loc) · 86.3 KB
/
nsCellMap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsTArray.h"
#include "nsCellMap.h"
#include "nsTableFrame.h"
#include "nsTableCellFrame.h"
#include "nsTableRowFrame.h"
#include "nsTableRowGroupFrame.h"
#include <algorithm>
using namespace mozilla;
static void
SetDamageArea(int32_t aStartCol,
int32_t aStartRow,
int32_t aColCount,
int32_t aRowCount,
TableArea& aDamageArea)
{
NS_ASSERTION(aStartCol >= 0, "negative col index");
NS_ASSERTION(aStartRow >= 0, "negative row index");
NS_ASSERTION(aColCount >= 0, "negative col count");
NS_ASSERTION(aRowCount >= 0, "negative row count");
aDamageArea.StartCol() = aStartCol;
aDamageArea.StartRow() = aStartRow;
aDamageArea.ColCount() = aColCount;
aDamageArea.RowCount() = aRowCount;
}
// Empty static array used for SafeElementAt() calls on mRows.
static nsCellMap::CellDataArray * sEmptyRow;
// CellData
CellData::CellData(nsTableCellFrame* aOrigCell)
{
MOZ_COUNT_CTOR(CellData);
static_assert(sizeof(mOrigCell) == sizeof(mBits),
"mOrigCell and mBits must be the same size");
mOrigCell = aOrigCell;
}
CellData::~CellData()
{
MOZ_COUNT_DTOR(CellData);
}
BCCellData::BCCellData(nsTableCellFrame* aOrigCell)
:CellData(aOrigCell)
{
MOZ_COUNT_CTOR(BCCellData);
}
BCCellData::~BCCellData()
{
MOZ_COUNT_DTOR(BCCellData);
}
// nsTableCellMap
nsTableCellMap::nsTableCellMap(nsTableFrame& aTableFrame,
bool aBorderCollapse)
:mTableFrame(aTableFrame), mFirstMap(nullptr), mBCInfo(nullptr)
{
MOZ_COUNT_CTOR(nsTableCellMap);
nsTableFrame::RowGroupArray orderedRowGroups;
aTableFrame.OrderRowGroups(orderedRowGroups);
nsTableRowGroupFrame* prior = nullptr;
for (uint32_t rgX = 0; rgX < orderedRowGroups.Length(); rgX++) {
nsTableRowGroupFrame* rgFrame = orderedRowGroups[rgX];
InsertGroupCellMap(rgFrame, prior);
prior = rgFrame;
}
if (aBorderCollapse) {
mBCInfo = new BCInfo();
}
}
nsTableCellMap::~nsTableCellMap()
{
MOZ_COUNT_DTOR(nsTableCellMap);
nsCellMap* cellMap = mFirstMap;
while (cellMap) {
nsCellMap* next = cellMap->GetNextSibling();
delete cellMap;
cellMap = next;
}
if (mBCInfo) {
DeleteIEndBEndBorders();
delete mBCInfo;
}
}
// Get the bcData holding the border segments of the iEnd edge of the table
BCData*
nsTableCellMap::GetIEndMostBorder(int32_t aRowIndex)
{
if (!mBCInfo) ABORT1(nullptr);
int32_t numRows = mBCInfo->mIEndBorders.Length();
if (aRowIndex < numRows) {
return &mBCInfo->mIEndBorders.ElementAt(aRowIndex);
}
mBCInfo->mIEndBorders.SetLength(aRowIndex+1);
return &mBCInfo->mIEndBorders.ElementAt(aRowIndex);
}
// Get the bcData holding the border segments of the bEnd edge of the table
BCData*
nsTableCellMap::GetBEndMostBorder(int32_t aColIndex)
{
if (!mBCInfo) ABORT1(nullptr);
int32_t numCols = mBCInfo->mBEndBorders.Length();
if (aColIndex < numCols) {
return &mBCInfo->mBEndBorders.ElementAt(aColIndex);
}
mBCInfo->mBEndBorders.SetLength(aColIndex+1);
return &mBCInfo->mBEndBorders.ElementAt(aColIndex);
}
// delete the borders corresponding to the iEnd and bEnd edges of the table
void
nsTableCellMap::DeleteIEndBEndBorders()
{
if (mBCInfo) {
mBCInfo->mBEndBorders.Clear();
mBCInfo->mIEndBorders.Clear();
}
}
void
nsTableCellMap::InsertGroupCellMap(nsCellMap* aPrevMap,
nsCellMap& aNewMap)
{
nsCellMap* next;
if (aPrevMap) {
next = aPrevMap->GetNextSibling();
aPrevMap->SetNextSibling(&aNewMap);
}
else {
next = mFirstMap;
mFirstMap = &aNewMap;
}
aNewMap.SetNextSibling(next);
}
void nsTableCellMap::InsertGroupCellMap(nsTableRowGroupFrame* aNewGroup,
nsTableRowGroupFrame*& aPrevGroup)
{
nsCellMap* newMap = new nsCellMap(aNewGroup, mBCInfo != nullptr);
nsCellMap* prevMap = nullptr;
nsCellMap* lastMap = mFirstMap;
if (aPrevGroup) {
nsCellMap* map = mFirstMap;
while (map) {
lastMap = map;
if (map->GetRowGroup() == aPrevGroup) {
prevMap = map;
break;
}
map = map->GetNextSibling();
}
}
if (!prevMap) {
if (aPrevGroup) {
prevMap = lastMap;
aPrevGroup = (prevMap) ? prevMap->GetRowGroup() : nullptr;
}
else {
aPrevGroup = nullptr;
}
}
InsertGroupCellMap(prevMap, *newMap);
}
void nsTableCellMap::RemoveGroupCellMap(nsTableRowGroupFrame* aGroup)
{
nsCellMap* map = mFirstMap;
nsCellMap* prior = nullptr;
while (map) {
if (map->GetRowGroup() == aGroup) {
nsCellMap* next = map->GetNextSibling();
if (mFirstMap == map) {
mFirstMap = next;
}
else {
prior->SetNextSibling(next);
}
delete map;
break;
}
prior = map;
map = map->GetNextSibling();
}
}
static nsCellMap*
FindMapFor(const nsTableRowGroupFrame* aRowGroup,
nsCellMap* aStart,
const nsCellMap* aEnd)
{
for (nsCellMap* map = aStart; map != aEnd; map = map->GetNextSibling()) {
if (aRowGroup == map->GetRowGroup()) {
return map;
}
}
return nullptr;
}
nsCellMap*
nsTableCellMap::GetMapFor(const nsTableRowGroupFrame* aRowGroup,
nsCellMap* aStartHint) const
{
NS_PRECONDITION(aRowGroup, "Must have a rowgroup");
NS_ASSERTION(!aRowGroup->GetPrevInFlow(), "GetMapFor called with continuation");
if (aStartHint) {
nsCellMap* map = FindMapFor(aRowGroup, aStartHint, nullptr);
if (map) {
return map;
}
}
nsCellMap* map = FindMapFor(aRowGroup, mFirstMap, aStartHint);
if (map) {
return map;
}
// if aRowGroup is a repeated header or footer find the header or footer it was repeated from
if (aRowGroup->IsRepeatable()) {
nsTableFrame* fifTable = static_cast<nsTableFrame*>(mTableFrame.FirstInFlow());
const nsStyleDisplay* display = aRowGroup->StyleDisplay();
nsTableRowGroupFrame* rgOrig =
(NS_STYLE_DISPLAY_TABLE_HEADER_GROUP == display->mDisplay) ?
fifTable->GetTHead() : fifTable->GetTFoot();
// find the row group cell map using the original header/footer
if (rgOrig && rgOrig != aRowGroup) {
return GetMapFor(rgOrig, aStartHint);
}
}
return nullptr;
}
void
nsTableCellMap::Synchronize(nsTableFrame* aTableFrame)
{
nsTableFrame::RowGroupArray orderedRowGroups;
AutoTArray<nsCellMap*, 8> maps;
aTableFrame->OrderRowGroups(orderedRowGroups);
if (!orderedRowGroups.Length()) {
return;
}
// XXXbz this fails if orderedRowGroups is missing some row groups
// (due to OOM when appending to the array, e.g. -- we leak maps in
// that case).
// Scope |map| outside the loop so we can use it as a hint.
nsCellMap* map = nullptr;
for (uint32_t rgX = 0; rgX < orderedRowGroups.Length(); rgX++) {
nsTableRowGroupFrame* rgFrame = orderedRowGroups[rgX];
map = GetMapFor(static_cast<nsTableRowGroupFrame*>(rgFrame->FirstInFlow()),
map);
if (map) {
if (!maps.AppendElement(map)) {
delete map;
map = nullptr;
NS_WARNING("Could not AppendElement");
break;
}
}
}
if (maps.IsEmpty()) {
MOZ_ASSERT(!mFirstMap);
return;
}
int32_t mapIndex = maps.Length() - 1; // Might end up -1
nsCellMap* nextMap = maps.ElementAt(mapIndex);
nextMap->SetNextSibling(nullptr);
for (mapIndex-- ; mapIndex >= 0; mapIndex--) {
nsCellMap* map = maps.ElementAt(mapIndex);
map->SetNextSibling(nextMap);
nextMap = map;
}
mFirstMap = nextMap;
}
bool
nsTableCellMap::HasMoreThanOneCell(int32_t aRowIndex) const
{
int32_t rowIndex = aRowIndex;
nsCellMap* map = mFirstMap;
while (map) {
if (map->GetRowCount() > rowIndex) {
return map->HasMoreThanOneCell(rowIndex);
}
rowIndex -= map->GetRowCount();
map = map->GetNextSibling();
}
return false;
}
int32_t
nsTableCellMap::GetNumCellsOriginatingInRow(int32_t aRowIndex) const
{
int32_t rowIndex = aRowIndex;
nsCellMap* map = mFirstMap;
while (map) {
if (map->GetRowCount() > rowIndex) {
return map->GetNumCellsOriginatingInRow(rowIndex);
}
rowIndex -= map->GetRowCount();
map = map->GetNextSibling();
}
return 0;
}
int32_t
nsTableCellMap::GetEffectiveRowSpan(int32_t aRowIndex,
int32_t aColIndex) const
{
int32_t rowIndex = aRowIndex;
nsCellMap* map = mFirstMap;
while (map) {
if (map->GetRowCount() > rowIndex) {
return map->GetRowSpan(rowIndex, aColIndex, true);
}
rowIndex -= map->GetRowCount();
map = map->GetNextSibling();
}
NS_NOTREACHED("Bogus row index?");
return 0;
}
int32_t
nsTableCellMap::GetEffectiveColSpan(int32_t aRowIndex,
int32_t aColIndex) const
{
int32_t rowIndex = aRowIndex;
nsCellMap* map = mFirstMap;
while (map) {
if (map->GetRowCount() > rowIndex) {
return map->GetEffectiveColSpan(*this, rowIndex, aColIndex);
}
rowIndex -= map->GetRowCount();
map = map->GetNextSibling();
}
NS_NOTREACHED("Bogus row index?");
return 0;
}
nsTableCellFrame*
nsTableCellMap::GetCellFrame(int32_t aRowIndex,
int32_t aColIndex,
CellData& aData,
bool aUseRowIfOverlap) const
{
int32_t rowIndex = aRowIndex;
nsCellMap* map = mFirstMap;
while (map) {
if (map->GetRowCount() > rowIndex) {
return map->GetCellFrame(rowIndex, aColIndex, aData, aUseRowIfOverlap);
}
rowIndex -= map->GetRowCount();
map = map->GetNextSibling();
}
return nullptr;
}
nsColInfo*
nsTableCellMap::GetColInfoAt(int32_t aColIndex)
{
int32_t numColsToAdd = aColIndex + 1 - mCols.Length();
if (numColsToAdd > 0) {
AddColsAtEnd(numColsToAdd); // XXX this could fail to add cols in theory
}
return &mCols.ElementAt(aColIndex);
}
int32_t
nsTableCellMap::GetRowCount() const
{
int32_t numRows = 0;
nsCellMap* map = mFirstMap;
while (map) {
numRows += map->GetRowCount();
map = map->GetNextSibling();
}
return numRows;
}
CellData*
nsTableCellMap::GetDataAt(int32_t aRowIndex,
int32_t aColIndex) const
{
int32_t rowIndex = aRowIndex;
nsCellMap* map = mFirstMap;
while (map) {
if (map->GetRowCount() > rowIndex) {
return map->GetDataAt(rowIndex, aColIndex);
}
rowIndex -= map->GetRowCount();
map = map->GetNextSibling();
}
return nullptr;
}
void
nsTableCellMap::AddColsAtEnd(uint32_t aNumCols)
{
if (!mCols.AppendElements(aNumCols)) {
NS_WARNING("Could not AppendElement");
}
if (mBCInfo) {
if (!mBCInfo->mBEndBorders.AppendElements(aNumCols)) {
NS_WARNING("Could not AppendElement");
}
}
}
void
nsTableCellMap::RemoveColsAtEnd()
{
// Remove the cols at the end which don't have originating cells or cells spanning
// into them. Only do this if the col was created as eColAnonymousCell
int32_t numCols = GetColCount();
int32_t lastGoodColIndex = mTableFrame.GetIndexOfLastRealCol();
for (int32_t colX = numCols - 1; (colX >= 0) && (colX > lastGoodColIndex); colX--) {
nsColInfo& colInfo = mCols.ElementAt(colX);
if ((colInfo.mNumCellsOrig <= 0) && (colInfo.mNumCellsSpan <= 0)) {
mCols.RemoveElementAt(colX);
if (mBCInfo) {
int32_t count = mBCInfo->mBEndBorders.Length();
if (colX < count) {
mBCInfo->mBEndBorders.RemoveElementAt(colX);
}
}
}
else break; // only remove until we encounter the 1st valid one
}
}
void
nsTableCellMap::ClearCols()
{
mCols.Clear();
if (mBCInfo)
mBCInfo->mBEndBorders.Clear();
}
void
nsTableCellMap::InsertRows(nsTableRowGroupFrame* aParent,
nsTArray<nsTableRowFrame*>& aRows,
int32_t aFirstRowIndex,
bool aConsiderSpans,
TableArea& aDamageArea)
{
int32_t numNewRows = aRows.Length();
if ((numNewRows <= 0) || (aFirstRowIndex < 0)) ABORT0();
int32_t rowIndex = aFirstRowIndex;
int32_t rgStartRowIndex = 0;
nsCellMap* cellMap = mFirstMap;
while (cellMap) {
nsTableRowGroupFrame* rg = cellMap->GetRowGroup();
if (rg == aParent) {
cellMap->InsertRows(*this, aRows, rowIndex, aConsiderSpans,
rgStartRowIndex, aDamageArea);
#ifdef DEBUG_TABLE_CELLMAP
Dump("after InsertRows");
#endif
if (mBCInfo) {
int32_t count = mBCInfo->mIEndBorders.Length();
if (aFirstRowIndex < count) {
for (int32_t rowX = aFirstRowIndex; rowX < aFirstRowIndex + numNewRows; rowX++) {
mBCInfo->mIEndBorders.InsertElementAt(rowX);
}
}
else {
GetIEndMostBorder(aFirstRowIndex); // this will create missing entries
for (int32_t rowX = aFirstRowIndex + 1; rowX < aFirstRowIndex + numNewRows; rowX++) {
mBCInfo->mIEndBorders.AppendElement();
}
}
}
return;
}
int32_t rowCount = cellMap->GetRowCount();
rgStartRowIndex += rowCount;
rowIndex -= rowCount;
cellMap = cellMap->GetNextSibling();
}
NS_ERROR("Attempt to insert row into wrong map.");
}
void
nsTableCellMap::RemoveRows(int32_t aFirstRowIndex,
int32_t aNumRowsToRemove,
bool aConsiderSpans,
TableArea& aDamageArea)
{
int32_t rowIndex = aFirstRowIndex;
int32_t rgStartRowIndex = 0;
nsCellMap* cellMap = mFirstMap;
while (cellMap) {
int32_t rowCount = cellMap->GetRowCount();
if (rowCount > rowIndex) {
cellMap->RemoveRows(*this, rowIndex, aNumRowsToRemove, aConsiderSpans,
rgStartRowIndex, aDamageArea);
if (mBCInfo) {
for (int32_t rowX = aFirstRowIndex + aNumRowsToRemove - 1; rowX >= aFirstRowIndex; rowX--) {
if (uint32_t(rowX) < mBCInfo->mIEndBorders.Length()) {
mBCInfo->mIEndBorders.RemoveElementAt(rowX);
}
}
}
break;
}
rgStartRowIndex += rowCount;
rowIndex -= rowCount;
cellMap = cellMap->GetNextSibling();
}
#ifdef DEBUG_TABLE_CELLMAP
Dump("after RemoveRows");
#endif
}
CellData*
nsTableCellMap::AppendCell(nsTableCellFrame& aCellFrame,
int32_t aRowIndex,
bool aRebuildIfNecessary,
TableArea& aDamageArea)
{
MOZ_ASSERT(&aCellFrame == aCellFrame.FirstInFlow(),
"invalid call on continuing frame");
nsIFrame* rgFrame = aCellFrame.GetParent(); // get the row
if (!rgFrame) return 0;
rgFrame = rgFrame->GetParent(); // get the row group
if (!rgFrame) return 0;
CellData* result = nullptr;
int32_t rowIndex = aRowIndex;
int32_t rgStartRowIndex = 0;
nsCellMap* cellMap = mFirstMap;
while (cellMap) {
if (cellMap->GetRowGroup() == rgFrame) {
result = cellMap->AppendCell(*this, &aCellFrame, rowIndex,
aRebuildIfNecessary, rgStartRowIndex,
aDamageArea);
break;
}
int32_t rowCount = cellMap->GetRowCount();
rgStartRowIndex += rowCount;
rowIndex -= rowCount;
cellMap = cellMap->GetNextSibling();
}
#ifdef DEBUG_TABLE_CELLMAP
Dump("after AppendCell");
#endif
return result;
}
void
nsTableCellMap::InsertCells(nsTArray<nsTableCellFrame*>& aCellFrames,
int32_t aRowIndex,
int32_t aColIndexBefore,
TableArea& aDamageArea)
{
int32_t rowIndex = aRowIndex;
int32_t rgStartRowIndex = 0;
nsCellMap* cellMap = mFirstMap;
while (cellMap) {
int32_t rowCount = cellMap->GetRowCount();
if (rowCount > rowIndex) {
cellMap->InsertCells(*this, aCellFrames, rowIndex, aColIndexBefore,
rgStartRowIndex, aDamageArea);
break;
}
rgStartRowIndex += rowCount;
rowIndex -= rowCount;
cellMap = cellMap->GetNextSibling();
}
#ifdef DEBUG_TABLE_CELLMAP
Dump("after InsertCells");
#endif
}
void
nsTableCellMap::RemoveCell(nsTableCellFrame* aCellFrame,
int32_t aRowIndex,
TableArea& aDamageArea)
{
if (!aCellFrame) ABORT0();
MOZ_ASSERT(aCellFrame == aCellFrame->FirstInFlow(),
"invalid call on continuing frame");
int32_t rowIndex = aRowIndex;
int32_t rgStartRowIndex = 0;
nsCellMap* cellMap = mFirstMap;
while (cellMap) {
int32_t rowCount = cellMap->GetRowCount();
if (rowCount > rowIndex) {
cellMap->RemoveCell(*this, aCellFrame, rowIndex, rgStartRowIndex,
aDamageArea);
#ifdef DEBUG_TABLE_CELLMAP
Dump("after RemoveCell");
#endif
return;
}
rgStartRowIndex += rowCount;
rowIndex -= rowCount;
cellMap = cellMap->GetNextSibling();
}
// if we reach this point - the cell did not get removed, the caller of this routine
// will delete the cell and the cellmap will probably hold a reference to
// the deleted cell which will cause a subsequent crash when this cell is
// referenced later
NS_ERROR("nsTableCellMap::RemoveCell - could not remove cell");
}
void
nsTableCellMap::RebuildConsideringCells(nsCellMap* aCellMap,
nsTArray<nsTableCellFrame*>* aCellFrames,
int32_t aRowIndex,
int32_t aColIndex,
bool aInsert,
TableArea& aDamageArea)
{
int32_t numOrigCols = GetColCount();
ClearCols();
nsCellMap* cellMap = mFirstMap;
int32_t rowCount = 0;
while (cellMap) {
if (cellMap == aCellMap) {
cellMap->RebuildConsideringCells(*this, numOrigCols, aCellFrames,
aRowIndex, aColIndex, aInsert);
}
else {
cellMap->RebuildConsideringCells(*this, numOrigCols, nullptr, -1, 0,
false);
}
rowCount += cellMap->GetRowCount();
cellMap = cellMap->GetNextSibling();
}
SetDamageArea(0, 0, GetColCount(), rowCount, aDamageArea);
}
void
nsTableCellMap::RebuildConsideringRows(nsCellMap* aCellMap,
int32_t aStartRowIndex,
nsTArray<nsTableRowFrame*>* aRowsToInsert,
int32_t aNumRowsToRemove,
TableArea& aDamageArea)
{
NS_PRECONDITION(!aRowsToInsert || aNumRowsToRemove == 0,
"Can't handle both removing and inserting rows at once");
int32_t numOrigCols = GetColCount();
ClearCols();
nsCellMap* cellMap = mFirstMap;
int32_t rowCount = 0;
while (cellMap) {
if (cellMap == aCellMap) {
cellMap->RebuildConsideringRows(*this, aStartRowIndex, aRowsToInsert,
aNumRowsToRemove);
}
else {
cellMap->RebuildConsideringCells(*this, numOrigCols, nullptr, -1, 0,
false);
}
rowCount += cellMap->GetRowCount();
cellMap = cellMap->GetNextSibling();
}
SetDamageArea(0, 0, GetColCount(), rowCount, aDamageArea);
}
int32_t
nsTableCellMap::GetNumCellsOriginatingInCol(int32_t aColIndex) const
{
int32_t colCount = mCols.Length();
if ((aColIndex >= 0) && (aColIndex < colCount)) {
return mCols.ElementAt(aColIndex).mNumCellsOrig;
}
else {
NS_ERROR("nsCellMap::GetNumCellsOriginatingInCol - bad col index");
return 0;
}
}
#ifdef DEBUG
void
nsTableCellMap::Dump(char* aString) const
{
if (aString)
printf("%s \n", aString);
printf("***** START TABLE CELL MAP DUMP ***** %p\n", (void*)this);
// output col info
int32_t colCount = mCols.Length();
printf ("cols array orig/span-> %p", (void*)this);
for (int32_t colX = 0; colX < colCount; colX++) {
const nsColInfo& colInfo = mCols.ElementAt(colX);
printf ("%d=%d/%d ", colX, colInfo.mNumCellsOrig, colInfo.mNumCellsSpan);
}
printf(" cols in cache %d\n", int(mTableFrame.GetColCache().Length()));
nsCellMap* cellMap = mFirstMap;
while (cellMap) {
cellMap->Dump(nullptr != mBCInfo);
cellMap = cellMap->GetNextSibling();
}
if (nullptr != mBCInfo) {
printf("***** block-end borders *****\n");
nscoord size;
BCBorderOwner owner;
LogicalSide side;
bool segStart;
bool bevel;
int32_t colIndex;
int32_t numCols = mBCInfo->mBEndBorders.Length();
for (int32_t i = 0; i <= 2; i++) {
printf("\n ");
for (colIndex = 0; colIndex < numCols; colIndex++) {
BCData& cd = mBCInfo->mBEndBorders.ElementAt(colIndex);
if (0 == i) {
size = cd.GetBStartEdge(owner, segStart);
printf("t=%d%X%d ", int32_t(size), owner, segStart);
}
else if (1 == i) {
size = cd.GetIStartEdge(owner, segStart);
printf("l=%d%X%d ", int32_t(size), owner, segStart);
}
else {
size = cd.GetCorner(side, bevel);
printf("c=%d%X%d ", int32_t(size), side, bevel);
}
}
BCData& cd = mBCInfo->mBEndIEndCorner;
if (0 == i) {
size = cd.GetBStartEdge(owner, segStart);
printf("t=%d%X%d ", int32_t(size), owner, segStart);
}
else if (1 == i) {
size = cd.GetIStartEdge(owner, segStart);
printf("l=%d%X%d ", int32_t(size), owner, segStart);
}
else {
size = cd.GetCorner(side, bevel);
printf("c=%d%X%d ", int32_t(size), side, bevel);
}
}
printf("\n");
}
printf("***** END TABLE CELL MAP DUMP *****\n");
}
#endif
nsTableCellFrame*
nsTableCellMap::GetCellInfoAt(int32_t aRowIndex,
int32_t aColIndex,
bool* aOriginates,
int32_t* aColSpan) const
{
int32_t rowIndex = aRowIndex;
nsCellMap* cellMap = mFirstMap;
while (cellMap) {
if (cellMap->GetRowCount() > rowIndex) {
return cellMap->GetCellInfoAt(*this, rowIndex, aColIndex, aOriginates, aColSpan);
}
rowIndex -= cellMap->GetRowCount();
cellMap = cellMap->GetNextSibling();
}
return nullptr;
}
int32_t
nsTableCellMap::GetIndexByRowAndColumn(int32_t aRow, int32_t aColumn) const
{
int32_t index = 0;
int32_t colCount = mCols.Length();
int32_t rowIndex = aRow;
nsCellMap* cellMap = mFirstMap;
while (cellMap) {
int32_t rowCount = cellMap->GetRowCount();
if (rowIndex >= rowCount) {
// If the rowCount is less than the rowIndex, this means that the index is
// not within the current map. If so, get the index of the last cell in
// the last row.
rowIndex -= rowCount;
int32_t cellMapIdx = cellMap->GetHighestIndex(colCount);
if (cellMapIdx != -1)
index += cellMapIdx + 1;
} else {
// Index is in valid range for this cellmap, so get the index of rowIndex
// and aColumn.
int32_t cellMapIdx = cellMap->GetIndexByRowAndColumn(colCount, rowIndex,
aColumn);
if (cellMapIdx == -1)
return -1; // no cell at the given row and column.
index += cellMapIdx;
return index; // no need to look through further maps here
}
cellMap = cellMap->GetNextSibling();
}
return -1;
}
void
nsTableCellMap::GetRowAndColumnByIndex(int32_t aIndex,
int32_t *aRow, int32_t *aColumn) const
{
*aRow = -1;
*aColumn = -1;
int32_t colCount = mCols.Length();
int32_t previousRows = 0;
int32_t index = aIndex;
nsCellMap* cellMap = mFirstMap;
while (cellMap) {
int32_t rowCount = cellMap->GetRowCount();
// Determine the highest possible index in this map to see
// if wanted index is in here.
int32_t cellMapIdx = cellMap->GetHighestIndex(colCount);
if (cellMapIdx == -1) {
// The index is not within this map, increase the total row index
// accordingly.
previousRows += rowCount;
} else {
if (index > cellMapIdx) {
// The index is not within this map, so decrease it by the cellMapIdx
// determined index and increase the total row index accordingly.
index -= cellMapIdx + 1;
previousRows += rowCount;
} else {
cellMap->GetRowAndColumnByIndex(colCount, index, aRow, aColumn);
// If there were previous indexes, take them into account.
*aRow += previousRows;
return; // no need to look any further.
}
}
cellMap = cellMap->GetNextSibling();
}
}
bool nsTableCellMap::RowIsSpannedInto(int32_t aRowIndex,
int32_t aNumEffCols) const
{
int32_t rowIndex = aRowIndex;
nsCellMap* cellMap = mFirstMap;
while (cellMap) {
if (cellMap->GetRowCount() > rowIndex) {
return cellMap->RowIsSpannedInto(rowIndex, aNumEffCols);
}
rowIndex -= cellMap->GetRowCount();
cellMap = cellMap->GetNextSibling();
}
return false;
}
bool nsTableCellMap::RowHasSpanningCells(int32_t aRowIndex,
int32_t aNumEffCols) const
{
int32_t rowIndex = aRowIndex;
nsCellMap* cellMap = mFirstMap;
while (cellMap) {
if (cellMap->GetRowCount() > rowIndex) {
return cellMap->RowHasSpanningCells(rowIndex, aNumEffCols);
}
rowIndex -= cellMap->GetRowCount();
cellMap = cellMap->GetNextSibling();
}
return false;
}
void
nsTableCellMap::ResetBStartStart(LogicalSide aSide,
nsCellMap& aCellMap,
uint32_t aRowIndex,
uint32_t aColIndex,
bool aIsBEndIEnd)
{
if (!mBCInfo || aIsBEndIEnd) ABORT0();
BCCellData* cellData;
BCData* bcData = nullptr;
switch(aSide) {
case eLogicalSideBEnd:
aRowIndex++;
MOZ_FALLTHROUGH;
case eLogicalSideBStart:
cellData = (BCCellData*)aCellMap.GetDataAt(aRowIndex, aColIndex);
if (cellData) {
bcData = &cellData->mData;
}
else {
NS_ASSERTION(aSide == eLogicalSideBEnd, "program error");
// try the next row group
nsCellMap* cellMap = aCellMap.GetNextSibling();
if (cellMap) {
cellData = (BCCellData*)cellMap->GetDataAt(0, aColIndex);
if (cellData) {
bcData = &cellData->mData;
}
else {
bcData = GetBEndMostBorder(aColIndex);
}
}
}
break;
case eLogicalSideIEnd:
aColIndex++;
MOZ_FALLTHROUGH;
case eLogicalSideIStart:
cellData = (BCCellData*)aCellMap.GetDataAt(aRowIndex, aColIndex);
if (cellData) {
bcData = &cellData->mData;
}
else {
NS_ASSERTION(aSide == eLogicalSideIEnd, "program error");
bcData = GetIEndMostBorder(aRowIndex);
}
break;
}
if (bcData) {
bcData->SetBStartStart(false);
}
}
// store the aSide border segment at coord = (aRowIndex, aColIndex). For bStart/iStart, store
// the info at coord. For bEnd/iStart store it at the adjacent location so that it is
// bStart/iStart at that location. If the new location is at the iEnd or bEnd edge of the
// table, then store it one of the special arrays (iEnd-most borders, bEnd-most borders).
void
nsTableCellMap::SetBCBorderEdge(LogicalSide aSide,
nsCellMap& aCellMap,
uint32_t aCellMapStart,
uint32_t aRowIndex,
uint32_t aColIndex,
uint32_t aLength,
BCBorderOwner aOwner,
nscoord aSize,
bool aChanged)
{
if (!mBCInfo) ABORT0();
BCCellData* cellData;
int32_t lastIndex, xIndex, yIndex;
int32_t xPos = aColIndex;
int32_t yPos = aRowIndex;
int32_t rgYPos = aRowIndex - aCellMapStart;
bool changed;
switch(aSide) {
case eLogicalSideBEnd:
rgYPos++;
yPos++;
MOZ_FALLTHROUGH;
case eLogicalSideBStart:
lastIndex = xPos + aLength - 1;
for (xIndex = xPos; xIndex <= lastIndex; xIndex++) {
changed = aChanged && (xIndex == xPos);
BCData* bcData = nullptr;
cellData = (BCCellData*)aCellMap.GetDataAt(rgYPos, xIndex);
if (!cellData) {
int32_t numRgRows = aCellMap.GetRowCount();
if (yPos < numRgRows) { // add a dead cell data
TableArea damageArea;
cellData = (BCCellData*)aCellMap.AppendCell(*this, nullptr, rgYPos,
false, 0, damageArea);
if (!cellData) ABORT0();
}
else {
NS_ASSERTION(aSide == eLogicalSideBEnd, "program error");