This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnsTableFrame.cpp
7385 lines (6699 loc) · 267 KB
/
nsTableFrame.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 -*- */
/* vim: set ts=2 sw=2 et tw=80: */
/* 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 "mozilla/Likely.h"
#include "mozilla/MathAlgorithms.h"
#include "nsCOMPtr.h"
#include "nsTableFrame.h"
#include "nsRenderingContext.h"
#include "nsStyleContext.h"
#include "nsStyleConsts.h"
#include "nsIContent.h"
#include "nsCellMap.h"
#include "nsTableCellFrame.h"
#include "nsHTMLParts.h"
#include "nsTableColFrame.h"
#include "nsTableColGroupFrame.h"
#include "nsTableRowFrame.h"
#include "nsTableRowGroupFrame.h"
#include "nsTableOuterFrame.h"
#include "nsTablePainter.h"
#include "BasicTableLayoutStrategy.h"
#include "FixedTableLayoutStrategy.h"
#include "nsPresContext.h"
#include "nsContentUtils.h"
#include "nsCSSRendering.h"
#include "nsGkAtoms.h"
#include "nsCSSAnonBoxes.h"
#include "nsIPresShell.h"
#include "nsIDOMElement.h"
#include "nsIDOMHTMLElement.h"
#include "nsIScriptError.h"
#include "nsFrameManager.h"
#include "nsError.h"
#include "nsAutoPtr.h"
#include "nsCSSFrameConstructor.h"
#include "nsStyleSet.h"
#include "nsDisplayList.h"
#include "nsIScrollableFrame.h"
#include "nsCSSProps.h"
#include "RestyleTracker.h"
#include <algorithm>
using namespace mozilla;
using namespace mozilla::layout;
/********************************************************************************
** nsTableReflowState **
********************************************************************************/
struct nsTableReflowState {
// the real reflow state
const nsHTMLReflowState& reflowState;
// The table's available size
nsSize availSize;
// Stationary x-offset
nscoord x;
// Running y-offset
nscoord y;
nsTableReflowState(nsPresContext& aPresContext,
const nsHTMLReflowState& aReflowState,
nsTableFrame& aTableFrame,
nscoord aAvailWidth,
nscoord aAvailHeight)
: reflowState(aReflowState)
{
Init(aPresContext, aTableFrame, aAvailWidth, aAvailHeight);
}
void Init(nsPresContext& aPresContext,
nsTableFrame& aTableFrame,
nscoord aAvailWidth,
nscoord aAvailHeight)
{
nsTableFrame* table = static_cast<nsTableFrame*>(aTableFrame.FirstInFlow());
nsMargin borderPadding = table->GetChildAreaOffset(&reflowState);
nscoord cellSpacingX = table->GetCellSpacingX();
x = borderPadding.left + cellSpacingX;
y = borderPadding.top; //cellspacing added during reflow
availSize.width = aAvailWidth;
if (NS_UNCONSTRAINEDSIZE != availSize.width) {
availSize.width -= borderPadding.left + borderPadding.right
+ (2 * cellSpacingX);
availSize.width = std::max(0, availSize.width);
}
availSize.height = aAvailHeight;
if (NS_UNCONSTRAINEDSIZE != availSize.height) {
availSize.height -= borderPadding.top + borderPadding.bottom
+ (2 * table->GetCellSpacingY());
availSize.height = std::max(0, availSize.height);
}
}
nsTableReflowState(nsPresContext& aPresContext,
const nsHTMLReflowState& aReflowState,
nsTableFrame& aTableFrame)
: reflowState(aReflowState)
{
Init(aPresContext, aTableFrame, aReflowState.AvailableWidth(), aReflowState.AvailableHeight());
}
};
/********************************************************************************
** nsTableFrame **
********************************************************************************/
struct BCPropertyData
{
BCPropertyData() : mTopBorderWidth(0), mRightBorderWidth(0),
mBottomBorderWidth(0), mLeftBorderWidth(0),
mLeftCellBorderWidth(0), mRightCellBorderWidth(0) {}
nsIntRect mDamageArea;
BCPixelSize mTopBorderWidth;
BCPixelSize mRightBorderWidth;
BCPixelSize mBottomBorderWidth;
BCPixelSize mLeftBorderWidth;
BCPixelSize mLeftCellBorderWidth;
BCPixelSize mRightCellBorderWidth;
};
nsIFrame*
nsTableFrame::GetParentStyleContextFrame() const
{
// Since our parent, the table outer frame, returned this frame, we
// must return whatever our parent would normally have returned.
NS_PRECONDITION(mParent, "table constructed without outer table");
if (!mContent->GetParent() && !StyleContext()->GetPseudo()) {
// We're the root. We have no style context parent.
return nullptr;
}
return static_cast<nsFrame*>(GetParent())->DoGetParentStyleContextFrame();
}
nsIAtom*
nsTableFrame::GetType() const
{
return nsGkAtoms::tableFrame;
}
nsTableFrame::nsTableFrame(nsStyleContext* aContext)
: nsContainerFrame(aContext),
mCellMap(nullptr),
mTableLayoutStrategy(nullptr)
{
memset(&mBits, 0, sizeof(mBits));
}
void
nsTableFrame::Init(nsIContent* aContent,
nsIFrame* aParent,
nsIFrame* aPrevInFlow)
{
NS_PRECONDITION(!mCellMap, "Init called twice");
NS_PRECONDITION(!aPrevInFlow ||
aPrevInFlow->GetType() == nsGkAtoms::tableFrame,
"prev-in-flow must be of same type");
// Let the base class do its processing
nsContainerFrame::Init(aContent, aParent, aPrevInFlow);
// see if border collapse is on, if so set it
const nsStyleTableBorder* tableStyle = StyleTableBorder();
bool borderCollapse = (NS_STYLE_BORDER_COLLAPSE == tableStyle->mBorderCollapse);
SetBorderCollapse(borderCollapse);
// Create the cell map if this frame is the first-in-flow.
if (!aPrevInFlow) {
mCellMap = new nsTableCellMap(*this, borderCollapse);
}
if (aPrevInFlow) {
// set my width, because all frames in a table flow are the same width and
// code in nsTableOuterFrame depends on this being set
mRect.width = aPrevInFlow->GetSize().width;
}
else {
NS_ASSERTION(!mTableLayoutStrategy, "strategy was created before Init was called");
// create the strategy
if (IsAutoLayout())
mTableLayoutStrategy = new BasicTableLayoutStrategy(this);
else
mTableLayoutStrategy = new FixedTableLayoutStrategy(this);
}
}
nsTableFrame::~nsTableFrame()
{
delete mCellMap;
delete mTableLayoutStrategy;
}
void
nsTableFrame::DestroyFrom(nsIFrame* aDestructRoot)
{
mColGroups.DestroyFramesFrom(aDestructRoot);
nsContainerFrame::DestroyFrom(aDestructRoot);
}
// Make sure any views are positioned properly
void
nsTableFrame::RePositionViews(nsIFrame* aFrame)
{
nsContainerFrame::PositionFrameView(aFrame);
nsContainerFrame::PositionChildViews(aFrame);
}
static bool
IsRepeatedFrame(nsIFrame* kidFrame)
{
return (kidFrame->GetType() == nsGkAtoms::tableRowFrame ||
kidFrame->GetType() == nsGkAtoms::tableRowGroupFrame) &&
(kidFrame->GetStateBits() & NS_REPEATED_ROW_OR_ROWGROUP);
}
bool
nsTableFrame::PageBreakAfter(nsIFrame* aSourceFrame,
nsIFrame* aNextFrame)
{
const nsStyleDisplay* display = aSourceFrame->StyleDisplay();
nsTableRowGroupFrame* prevRg = do_QueryFrame(aSourceFrame);
// don't allow a page break after a repeated element ...
if ((display->mBreakAfter || (prevRg && prevRg->HasInternalBreakAfter())) &&
!IsRepeatedFrame(aSourceFrame)) {
return !(aNextFrame && IsRepeatedFrame(aNextFrame)); // or before
}
if (aNextFrame) {
display = aNextFrame->StyleDisplay();
// don't allow a page break before a repeated element ...
nsTableRowGroupFrame* nextRg = do_QueryFrame(aNextFrame);
if ((display->mBreakBefore ||
(nextRg && nextRg->HasInternalBreakBefore())) &&
!IsRepeatedFrame(aNextFrame)) {
return !IsRepeatedFrame(aSourceFrame); // or after
}
}
return false;
}
typedef nsTArray<nsIFrame*> FrameTArray;
/* static */ void
nsTableFrame::DestroyPositionedTablePartArray(void* aPropertyValue)
{
auto positionedObjs = static_cast<FrameTArray*>(aPropertyValue);
delete positionedObjs;
}
/* static */ void
nsTableFrame::RegisterPositionedTablePart(nsIFrame* aFrame)
{
// Supporting relative positioning for table parts other than table cells has
// the potential to break sites that apply 'position: relative' to those
// parts, expecting nothing to happen. We warn at the console to make tracking
// down the issue easy.
if (nsGkAtoms::tableCellFrame != aFrame->GetType()) {
nsIContent* content = aFrame->GetContent();
nsPresContext* presContext = aFrame->PresContext();
if (content && !presContext->HasWarnedAboutPositionedTableParts()) {
presContext->SetHasWarnedAboutPositionedTableParts();
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
NS_LITERAL_CSTRING("Layout: Tables"),
content->OwnerDoc(),
nsContentUtils::eLAYOUT_PROPERTIES,
"TablePartRelPosWarning");
}
}
nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(aFrame);
MOZ_ASSERT(tableFrame, "Should have a table frame here");
tableFrame = static_cast<nsTableFrame*>(tableFrame->FirstContinuation());
// Retrieve the positioned parts array for this table.
FrameProperties props = tableFrame->Properties();
auto positionedParts =
static_cast<FrameTArray*>(props.Get(PositionedTablePartArray()));
// Lazily create the array if it doesn't exist yet.
if (!positionedParts) {
positionedParts = new FrameTArray;
props.Set(PositionedTablePartArray(), positionedParts);
}
// Add this frame to the list.
positionedParts->AppendElement(aFrame);
}
/* static */ void
nsTableFrame::UnregisterPositionedTablePart(nsIFrame* aFrame,
nsIFrame* aDestructRoot)
{
// Retrieve the table frame, and ensure that we hit aDestructRoot on the way.
// If we don't, that means that the table frame will be destroyed, so we don't
// need to bother with unregistering this frame.
nsTableFrame* tableFrame = GetTableFramePassingThrough(aDestructRoot, aFrame);
if (!tableFrame) {
return;
}
tableFrame = static_cast<nsTableFrame*>(tableFrame->FirstContinuation());
// Retrieve the positioned parts array for this table.
FrameProperties props = tableFrame->Properties();
auto positionedParts =
static_cast<FrameTArray*>(props.Get(PositionedTablePartArray()));
// Remove the frame.
MOZ_ASSERT(positionedParts &&
positionedParts->IndexOf(aFrame) != FrameTArray::NoIndex,
"Asked to unregister a positioned table part that wasn't registered");
if (positionedParts) {
positionedParts->RemoveElement(aFrame);
}
}
// XXX this needs to be cleaned up so that the frame constructor breaks out col group
// frames into a separate child list, bug 343048.
nsresult
nsTableFrame::SetInitialChildList(ChildListID aListID,
nsFrameList& aChildList)
{
if (!mFrames.IsEmpty() || !mColGroups.IsEmpty()) {
// We already have child frames which means we've already been
// initialized
NS_NOTREACHED("unexpected second call to SetInitialChildList");
return NS_ERROR_UNEXPECTED;
}
if (aListID != kPrincipalList) {
// All we know about is the principal child list.
NS_NOTREACHED("unknown frame list");
return NS_ERROR_INVALID_ARG;
}
// XXXbz the below code is an icky cesspit that's only needed in its current
// form for two reasons:
// 1) Both rowgroups and column groups come in on the principal child list.
while (aChildList.NotEmpty()) {
nsIFrame* childFrame = aChildList.FirstChild();
aChildList.RemoveFirstChild();
const nsStyleDisplay* childDisplay = childFrame->StyleDisplay();
if (NS_STYLE_DISPLAY_TABLE_COLUMN_GROUP == childDisplay->mDisplay) {
NS_ASSERTION(nsGkAtoms::tableColGroupFrame == childFrame->GetType(),
"This is not a colgroup");
mColGroups.AppendFrame(nullptr, childFrame);
}
else { // row groups and unknown frames go on the main list for now
mFrames.AppendFrame(nullptr, childFrame);
}
}
// If we have a prev-in-flow, then we're a table that has been split and
// so don't treat this like an append
if (!GetPrevInFlow()) {
// process col groups first so that real cols get constructed before
// anonymous ones due to cells in rows.
InsertColGroups(0, mColGroups);
InsertRowGroups(mFrames);
// calc collapsing borders
if (IsBorderCollapse()) {
SetFullBCDamageArea();
}
}
return NS_OK;
}
void nsTableFrame::AttributeChangedFor(nsIFrame* aFrame,
nsIContent* aContent,
nsIAtom* aAttribute)
{
nsTableCellFrame *cellFrame = do_QueryFrame(aFrame);
if (cellFrame) {
if ((nsGkAtoms::rowspan == aAttribute) ||
(nsGkAtoms::colspan == aAttribute)) {
nsTableCellMap* cellMap = GetCellMap();
if (cellMap) {
// for now just remove the cell from the map and reinsert it
int32_t rowIndex, colIndex;
cellFrame->GetRowIndex(rowIndex);
cellFrame->GetColIndex(colIndex);
RemoveCell(cellFrame, rowIndex);
nsAutoTArray<nsTableCellFrame*, 1> cells;
cells.AppendElement(cellFrame);
InsertCells(cells, rowIndex, colIndex - 1);
// XXX Should this use eStyleChange? It currently doesn't need
// to, but it might given more optimization.
PresContext()->PresShell()->
FrameNeedsReflow(this, nsIPresShell::eTreeChange, NS_FRAME_IS_DIRTY);
}
}
}
}
/* ****** CellMap methods ******* */
/* return the effective col count */
int32_t nsTableFrame::GetEffectiveColCount() const
{
int32_t colCount = GetColCount();
if (LayoutStrategy()->GetType() == nsITableLayoutStrategy::Auto) {
nsTableCellMap* cellMap = GetCellMap();
if (!cellMap) {
return 0;
}
// don't count cols at the end that don't have originating cells
for (int32_t colX = colCount - 1; colX >= 0; colX--) {
if (cellMap->GetNumCellsOriginatingInCol(colX) > 0) {
break;
}
colCount--;
}
}
return colCount;
}
int32_t nsTableFrame::GetIndexOfLastRealCol()
{
int32_t numCols = mColFrames.Length();
if (numCols > 0) {
for (int32_t colX = numCols - 1; colX >= 0; colX--) {
nsTableColFrame* colFrame = GetColFrame(colX);
if (colFrame) {
if (eColAnonymousCell != colFrame->GetColType()) {
return colX;
}
}
}
}
return -1;
}
nsTableColFrame*
nsTableFrame::GetColFrame(int32_t aColIndex) const
{
NS_ASSERTION(!GetPrevInFlow(), "GetColFrame called on next in flow");
int32_t numCols = mColFrames.Length();
if ((aColIndex >= 0) && (aColIndex < numCols)) {
return mColFrames.ElementAt(aColIndex);
}
else {
NS_ERROR("invalid col index");
return nullptr;
}
}
int32_t nsTableFrame::GetEffectiveRowSpan(int32_t aRowIndex,
const nsTableCellFrame& aCell) const
{
nsTableCellMap* cellMap = GetCellMap();
NS_PRECONDITION (nullptr != cellMap, "bad call, cellMap not yet allocated.");
int32_t colIndex;
aCell.GetColIndex(colIndex);
return cellMap->GetEffectiveRowSpan(aRowIndex, colIndex);
}
int32_t nsTableFrame::GetEffectiveRowSpan(const nsTableCellFrame& aCell,
nsCellMap* aCellMap)
{
nsTableCellMap* tableCellMap = GetCellMap(); if (!tableCellMap) ABORT1(1);
int32_t colIndex, rowIndex;
aCell.GetColIndex(colIndex);
aCell.GetRowIndex(rowIndex);
if (aCellMap)
return aCellMap->GetRowSpan(rowIndex, colIndex, true);
else
return tableCellMap->GetEffectiveRowSpan(rowIndex, colIndex);
}
int32_t nsTableFrame::GetEffectiveColSpan(const nsTableCellFrame& aCell,
nsCellMap* aCellMap) const
{
nsTableCellMap* tableCellMap = GetCellMap(); if (!tableCellMap) ABORT1(1);
int32_t colIndex, rowIndex;
aCell.GetColIndex(colIndex);
aCell.GetRowIndex(rowIndex);
bool ignore;
if (aCellMap)
return aCellMap->GetEffectiveColSpan(*tableCellMap, rowIndex, colIndex, ignore);
else
return tableCellMap->GetEffectiveColSpan(rowIndex, colIndex);
}
bool nsTableFrame::HasMoreThanOneCell(int32_t aRowIndex) const
{
nsTableCellMap* tableCellMap = GetCellMap(); if (!tableCellMap) ABORT1(1);
return tableCellMap->HasMoreThanOneCell(aRowIndex);
}
void nsTableFrame::AdjustRowIndices(int32_t aRowIndex,
int32_t aAdjustment)
{
// Iterate over the row groups and adjust the row indices of all rows
// whose index is >= aRowIndex.
RowGroupArray rowGroups;
OrderRowGroups(rowGroups);
for (uint32_t rgX = 0; rgX < rowGroups.Length(); rgX++) {
rowGroups[rgX]->AdjustRowIndices(aRowIndex, aAdjustment);
}
}
void nsTableFrame::ResetRowIndices(const nsFrameList::Slice& aRowGroupsToExclude)
{
// Iterate over the row groups and adjust the row indices of all rows
// omit the rowgroups that will be inserted later
RowGroupArray rowGroups;
OrderRowGroups(rowGroups);
int32_t rowIndex = 0;
nsTHashtable<nsPtrHashKey<nsTableRowGroupFrame> > excludeRowGroups;
nsFrameList::Enumerator excludeRowGroupsEnumerator(aRowGroupsToExclude);
while (!excludeRowGroupsEnumerator.AtEnd()) {
excludeRowGroups.PutEntry(static_cast<nsTableRowGroupFrame*>(excludeRowGroupsEnumerator.get()));
excludeRowGroupsEnumerator.Next();
}
for (uint32_t rgX = 0; rgX < rowGroups.Length(); rgX++) {
nsTableRowGroupFrame* rgFrame = rowGroups[rgX];
if (!excludeRowGroups.GetEntry(rgFrame)) {
const nsFrameList& rowFrames = rgFrame->PrincipalChildList();
for (nsFrameList::Enumerator rows(rowFrames); !rows.AtEnd(); rows.Next()) {
if (NS_STYLE_DISPLAY_TABLE_ROW==rows.get()->StyleDisplay()->mDisplay) {
((nsTableRowFrame *)rows.get())->SetRowIndex(rowIndex);
rowIndex++;
}
}
}
}
}
void nsTableFrame::InsertColGroups(int32_t aStartColIndex,
const nsFrameList::Slice& aColGroups)
{
int32_t colIndex = aStartColIndex;
nsFrameList::Enumerator colGroups(aColGroups);
for (; !colGroups.AtEnd(); colGroups.Next()) {
MOZ_ASSERT(colGroups.get()->GetType() == nsGkAtoms::tableColGroupFrame);
nsTableColGroupFrame* cgFrame =
static_cast<nsTableColGroupFrame*>(colGroups.get());
cgFrame->SetStartColumnIndex(colIndex);
// XXXbz this sucks. AddColsToTable will actually remove colgroups from
// the list we're traversing! Need to fix things here. :( I guess this is
// why the old code used pointer-to-last-frame as opposed to
// pointer-to-frame-after-last....
// How about dealing with this by storing a const reference to the
// mNextSibling of the framelist's last frame, instead of storing a pointer
// to the first-after-next frame? Will involve making nsFrameList friend
// of nsIFrame, but it's time for that anyway.
cgFrame->AddColsToTable(colIndex, false,
colGroups.get()->PrincipalChildList());
int32_t numCols = cgFrame->GetColCount();
colIndex += numCols;
}
nsFrameList::Enumerator remainingColgroups = colGroups.GetUnlimitedEnumerator();
if (!remainingColgroups.AtEnd()) {
nsTableColGroupFrame::ResetColIndices(
static_cast<nsTableColGroupFrame*>(remainingColgroups.get()), colIndex);
}
}
void nsTableFrame::InsertCol(nsTableColFrame& aColFrame,
int32_t aColIndex)
{
mColFrames.InsertElementAt(aColIndex, &aColFrame);
nsTableColType insertedColType = aColFrame.GetColType();
int32_t numCacheCols = mColFrames.Length();
nsTableCellMap* cellMap = GetCellMap();
if (cellMap) {
int32_t numMapCols = cellMap->GetColCount();
if (numCacheCols > numMapCols) {
bool removedFromCache = false;
if (eColAnonymousCell != insertedColType) {
nsTableColFrame* lastCol = mColFrames.ElementAt(numCacheCols - 1);
if (lastCol) {
nsTableColType lastColType = lastCol->GetColType();
if (eColAnonymousCell == lastColType) {
// remove the col from the cache
mColFrames.RemoveElementAt(numCacheCols - 1);
// remove the col from the eColGroupAnonymousCell col group
nsTableColGroupFrame* lastColGroup = (nsTableColGroupFrame *)mColGroups.LastChild();
if (lastColGroup) {
lastColGroup->RemoveChild(*lastCol, false);
// remove the col group if it is empty
if (lastColGroup->GetColCount() <= 0) {
mColGroups.DestroyFrame((nsIFrame*)lastColGroup);
}
}
removedFromCache = true;
}
}
}
if (!removedFromCache) {
cellMap->AddColsAtEnd(1);
}
}
}
// for now, just bail and recalc all of the collapsing borders
if (IsBorderCollapse()) {
nsIntRect damageArea(aColIndex, 0, 1, GetRowCount());
AddBCDamageArea(damageArea);
}
}
void nsTableFrame::RemoveCol(nsTableColGroupFrame* aColGroupFrame,
int32_t aColIndex,
bool aRemoveFromCache,
bool aRemoveFromCellMap)
{
if (aRemoveFromCache) {
mColFrames.RemoveElementAt(aColIndex);
}
if (aRemoveFromCellMap) {
nsTableCellMap* cellMap = GetCellMap();
if (cellMap) {
AppendAnonymousColFrames(1);
}
}
// for now, just bail and recalc all of the collapsing borders
if (IsBorderCollapse()) {
nsIntRect damageArea(0, 0, GetColCount(), GetRowCount());
AddBCDamageArea(damageArea);
}
}
/** Get the cell map for this table frame. It is not always mCellMap.
* Only the first-in-flow has a legit cell map.
*/
nsTableCellMap*
nsTableFrame::GetCellMap() const
{
return static_cast<nsTableFrame*>(FirstInFlow())->mCellMap;
}
// XXX this needs to be moved to nsCSSFrameConstructor
nsTableColGroupFrame*
nsTableFrame::CreateAnonymousColGroupFrame(nsTableColGroupType aColGroupType)
{
nsIContent* colGroupContent = GetContent();
nsPresContext* presContext = PresContext();
nsIPresShell *shell = presContext->PresShell();
nsRefPtr<nsStyleContext> colGroupStyle;
colGroupStyle = shell->StyleSet()->
ResolveAnonymousBoxStyle(nsCSSAnonBoxes::tableColGroup, mStyleContext);
// Create a col group frame
nsIFrame* newFrame = NS_NewTableColGroupFrame(shell, colGroupStyle);
((nsTableColGroupFrame *)newFrame)->SetColType(aColGroupType);
newFrame->Init(colGroupContent, this, nullptr);
return (nsTableColGroupFrame *)newFrame;
}
void
nsTableFrame::AppendAnonymousColFrames(int32_t aNumColsToAdd)
{
// get the last col group frame
nsTableColGroupFrame* colGroupFrame =
static_cast<nsTableColGroupFrame*>(mColGroups.LastChild());
if (!colGroupFrame ||
(colGroupFrame->GetColType() != eColGroupAnonymousCell)) {
int32_t colIndex = (colGroupFrame) ?
colGroupFrame->GetStartColumnIndex() +
colGroupFrame->GetColCount() : 0;
colGroupFrame = CreateAnonymousColGroupFrame(eColGroupAnonymousCell);
if (!colGroupFrame) {
return;
}
// add the new frame to the child list
mColGroups.AppendFrame(this, colGroupFrame);
colGroupFrame->SetStartColumnIndex(colIndex);
}
AppendAnonymousColFrames(colGroupFrame, aNumColsToAdd, eColAnonymousCell,
true);
}
// XXX this needs to be moved to nsCSSFrameConstructor
// Right now it only creates the col frames at the end
void
nsTableFrame::AppendAnonymousColFrames(nsTableColGroupFrame* aColGroupFrame,
int32_t aNumColsToAdd,
nsTableColType aColType,
bool aAddToTable)
{
NS_PRECONDITION(aColGroupFrame, "null frame");
NS_PRECONDITION(aColType != eColAnonymousCol, "Shouldn't happen");
nsIPresShell *shell = PresContext()->PresShell();
// Get the last col frame
nsFrameList newColFrames;
int32_t startIndex = mColFrames.Length();
int32_t lastIndex = startIndex + aNumColsToAdd - 1;
for (int32_t childX = startIndex; childX <= lastIndex; childX++) {
nsIContent* iContent;
nsRefPtr<nsStyleContext> styleContext;
nsStyleContext* parentStyleContext;
// all anonymous cols that we create here use a pseudo style context of the
// col group
iContent = aColGroupFrame->GetContent();
parentStyleContext = aColGroupFrame->StyleContext();
styleContext = shell->StyleSet()->
ResolveAnonymousBoxStyle(nsCSSAnonBoxes::tableCol, parentStyleContext);
// ASSERTION to check for bug 54454 sneaking back in...
NS_ASSERTION(iContent, "null content in CreateAnonymousColFrames");
// create the new col frame
nsIFrame* colFrame = NS_NewTableColFrame(shell, styleContext);
((nsTableColFrame *) colFrame)->SetColType(aColType);
colFrame->Init(iContent, aColGroupFrame, nullptr);
newColFrames.AppendFrame(nullptr, colFrame);
}
nsFrameList& cols = aColGroupFrame->GetWritableChildList();
nsIFrame* oldLastCol = cols.LastChild();
const nsFrameList::Slice& newCols =
cols.InsertFrames(nullptr, oldLastCol, newColFrames);
if (aAddToTable) {
// get the starting col index in the cache
int32_t startColIndex;
if (oldLastCol) {
startColIndex =
static_cast<nsTableColFrame*>(oldLastCol)->GetColIndex() + 1;
} else {
startColIndex = aColGroupFrame->GetStartColumnIndex();
}
aColGroupFrame->AddColsToTable(startColIndex, true, newCols);
}
}
void
nsTableFrame::MatchCellMapToColCache(nsTableCellMap* aCellMap)
{
int32_t numColsInMap = GetColCount();
int32_t numColsInCache = mColFrames.Length();
int32_t numColsToAdd = numColsInMap - numColsInCache;
if (numColsToAdd > 0) {
// this sets the child list, updates the col cache and cell map
AppendAnonymousColFrames(numColsToAdd);
}
if (numColsToAdd < 0) {
int32_t numColsNotRemoved = DestroyAnonymousColFrames(-numColsToAdd);
// if the cell map has fewer cols than the cache, correct it
if (numColsNotRemoved > 0) {
aCellMap->AddColsAtEnd(numColsNotRemoved);
}
}
if (numColsToAdd && HasZeroColSpans()) {
SetNeedColSpanExpansion(true);
}
if (NeedColSpanExpansion()) {
// This flag can be set in two ways -- either by changing
// the number of columns (that happens in the block above),
// or by adding a cell with colspan="0" to the cellmap. To
// handle the latter case we need to explicitly check the
// flag here -- it may be set even if the number of columns
// did not change.
//
// @see nsCellMap::AppendCell
aCellMap->ExpandZeroColSpans();
}
}
void
nsTableFrame::DidResizeColumns()
{
NS_PRECONDITION(!GetPrevInFlow(),
"should only be called on first-in-flow");
if (mBits.mResizedColumns)
return; // already marked
for (nsTableFrame *f = this; f;
f = static_cast<nsTableFrame*>(f->GetNextInFlow()))
f->mBits.mResizedColumns = true;
}
void
nsTableFrame::AppendCell(nsTableCellFrame& aCellFrame,
int32_t aRowIndex)
{
nsTableCellMap* cellMap = GetCellMap();
if (cellMap) {
nsIntRect damageArea(0,0,0,0);
cellMap->AppendCell(aCellFrame, aRowIndex, true, damageArea);
MatchCellMapToColCache(cellMap);
if (IsBorderCollapse()) {
AddBCDamageArea(damageArea);
}
}
}
void nsTableFrame::InsertCells(nsTArray<nsTableCellFrame*>& aCellFrames,
int32_t aRowIndex,
int32_t aColIndexBefore)
{
nsTableCellMap* cellMap = GetCellMap();
if (cellMap) {
nsIntRect damageArea(0,0,0,0);
cellMap->InsertCells(aCellFrames, aRowIndex, aColIndexBefore, damageArea);
MatchCellMapToColCache(cellMap);
if (IsBorderCollapse()) {
AddBCDamageArea(damageArea);
}
}
}
// this removes the frames from the col group and table, but not the cell map
int32_t
nsTableFrame::DestroyAnonymousColFrames(int32_t aNumFrames)
{
// only remove cols that are of type eTypeAnonymous cell (they are at the end)
int32_t endIndex = mColFrames.Length() - 1;
int32_t startIndex = (endIndex - aNumFrames) + 1;
int32_t numColsRemoved = 0;
for (int32_t colX = endIndex; colX >= startIndex; colX--) {
nsTableColFrame* colFrame = GetColFrame(colX);
if (colFrame && (eColAnonymousCell == colFrame->GetColType())) {
nsTableColGroupFrame* cgFrame =
static_cast<nsTableColGroupFrame*>(colFrame->GetParent());
// remove the frame from the colgroup
cgFrame->RemoveChild(*colFrame, false);
// remove the frame from the cache, but not the cell map
RemoveCol(nullptr, colX, true, false);
numColsRemoved++;
}
else {
break;
}
}
return (aNumFrames - numColsRemoved);
}
void nsTableFrame::RemoveCell(nsTableCellFrame* aCellFrame,
int32_t aRowIndex)
{
nsTableCellMap* cellMap = GetCellMap();
if (cellMap) {
nsIntRect damageArea(0,0,0,0);
cellMap->RemoveCell(aCellFrame, aRowIndex, damageArea);
MatchCellMapToColCache(cellMap);
if (IsBorderCollapse()) {
AddBCDamageArea(damageArea);
}
}
}
int32_t
nsTableFrame::GetStartRowIndex(nsTableRowGroupFrame* aRowGroupFrame)
{
RowGroupArray orderedRowGroups;
OrderRowGroups(orderedRowGroups);
int32_t rowIndex = 0;
for (uint32_t rgIndex = 0; rgIndex < orderedRowGroups.Length(); rgIndex++) {
nsTableRowGroupFrame* rgFrame = orderedRowGroups[rgIndex];
if (rgFrame == aRowGroupFrame) {
break;
}
int32_t numRows = rgFrame->GetRowCount();
rowIndex += numRows;
}
return rowIndex;
}
// this cannot extend beyond a single row group
void nsTableFrame::AppendRows(nsTableRowGroupFrame* aRowGroupFrame,
int32_t aRowIndex,
nsTArray<nsTableRowFrame*>& aRowFrames)
{
nsTableCellMap* cellMap = GetCellMap();
if (cellMap) {
int32_t absRowIndex = GetStartRowIndex(aRowGroupFrame) + aRowIndex;
InsertRows(aRowGroupFrame, aRowFrames, absRowIndex, true);
}
}
// this cannot extend beyond a single row group
int32_t
nsTableFrame::InsertRows(nsTableRowGroupFrame* aRowGroupFrame,
nsTArray<nsTableRowFrame*>& aRowFrames,
int32_t aRowIndex,
bool aConsiderSpans)
{
#ifdef DEBUG_TABLE_CELLMAP
printf("=== insertRowsBefore firstRow=%d \n", aRowIndex);
Dump(true, false, true);
#endif
int32_t numColsToAdd = 0;
nsTableCellMap* cellMap = GetCellMap();
if (cellMap) {
nsIntRect damageArea(0,0,0,0);
int32_t origNumRows = cellMap->GetRowCount();
int32_t numNewRows = aRowFrames.Length();
cellMap->InsertRows(aRowGroupFrame, aRowFrames, aRowIndex, aConsiderSpans, damageArea);
MatchCellMapToColCache(cellMap);
if (aRowIndex < origNumRows) {
AdjustRowIndices(aRowIndex, numNewRows);
}
// assign the correct row indices to the new rows. If they were adjusted above
// it may not have been done correctly because each row is constructed with index 0
for (int32_t rowY = 0; rowY < numNewRows; rowY++) {
nsTableRowFrame* rowFrame = aRowFrames.ElementAt(rowY);
rowFrame->SetRowIndex(aRowIndex + rowY);
}
if (IsBorderCollapse()) {
AddBCDamageArea(damageArea);
}
}
#ifdef DEBUG_TABLE_CELLMAP
printf("=== insertRowsAfter \n");
Dump(true, false, true);
#endif
return numColsToAdd;
}
// this cannot extend beyond a single row group
void nsTableFrame::RemoveRows(nsTableRowFrame& aFirstRowFrame,
int32_t aNumRowsToRemove,
bool aConsiderSpans)
{
#ifdef TBD_OPTIMIZATION
// decide if we need to rebalance. we have to do this here because the row group
// cannot do it when it gets the dirty reflow corresponding to the frame being destroyed
bool stopTelling = false;
for (nsIFrame* kidFrame = aFirstFrame.FirstChild(); (kidFrame && !stopAsking);
kidFrame = kidFrame->GetNextSibling()) {
nsTableCellFrame *cellFrame = do_QueryFrame(kidFrame);
if (cellFrame) {
stopTelling = tableFrame->CellChangedWidth(*cellFrame, cellFrame->GetPass1MaxElementWidth(),
cellFrame->GetMaximumWidth(), true);
}
}
// XXX need to consider what happens if there are cells that have rowspans
// into the deleted row. Need to consider moving rows if a rebalance doesn't happen
#endif
int32_t firstRowIndex = aFirstRowFrame.GetRowIndex();
#ifdef DEBUG_TABLE_CELLMAP
printf("=== removeRowsBefore firstRow=%d numRows=%d\n", firstRowIndex, aNumRowsToRemove);
Dump(true, false, true);
#endif
nsTableCellMap* cellMap = GetCellMap();
if (cellMap) {
nsIntRect damageArea(0,0,0,0);
cellMap->RemoveRows(firstRowIndex, aNumRowsToRemove, aConsiderSpans, damageArea);
MatchCellMapToColCache(cellMap);
if (IsBorderCollapse()) {
AddBCDamageArea(damageArea);
}
}
AdjustRowIndices(firstRowIndex, -aNumRowsToRemove);
#ifdef DEBUG_TABLE_CELLMAP
printf("=== removeRowsAfter\n");
Dump(true, true, true);
#endif
}
// collect the rows ancestors of aFrame
int32_t
nsTableFrame::CollectRows(nsIFrame* aFrame,
nsTArray<nsTableRowFrame*>& aCollection)
{
NS_PRECONDITION(aFrame, "null frame");
int32_t numRows = 0;