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 pathnsTableRowFrame.cpp
1420 lines (1269 loc) · 50.1 KB
/
nsTableRowFrame.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 "nsTableRowFrame.h"
#include "nsTableRowGroupFrame.h"
#include "nsIPresShell.h"
#include "nsPresContext.h"
#include "nsStyleContext.h"
#include "nsStyleConsts.h"
#include "nsGkAtoms.h"
#include "nsIContent.h"
#include "nsTableFrame.h"
#include "nsTableCellFrame.h"
#include "nsCSSRendering.h"
#include "nsHTMLParts.h"
#include "nsTableColGroupFrame.h"
#include "nsTableColFrame.h"
#include "nsCOMPtr.h"
#include "nsDisplayList.h"
#include <algorithm>
using namespace mozilla;
struct nsTableCellReflowState : public nsHTMLReflowState
{
nsTableCellReflowState(nsPresContext* aPresContext,
const nsHTMLReflowState& aParentReflowState,
nsIFrame* aFrame,
const nsSize& aAvailableSpace,
bool aInit = true)
: nsHTMLReflowState(aPresContext, aParentReflowState, aFrame,
aAvailableSpace, -1, -1, aInit)
{
}
void FixUp(const nsSize& aAvailSpace);
};
void nsTableCellReflowState::FixUp(const nsSize& aAvailSpace)
{
// fix the mComputed values during a pass 2 reflow since the cell can be a percentage base
NS_WARN_IF_FALSE(NS_UNCONSTRAINEDSIZE != aAvailSpace.width,
"have unconstrained width; this should only result from "
"very large sizes, not attempts at intrinsic width "
"calculation");
if (NS_UNCONSTRAINEDSIZE != ComputedWidth()) {
nscoord computedWidth =
aAvailSpace.width - mComputedBorderPadding.LeftRight();
computedWidth = std::max(0, computedWidth);
SetComputedWidth(computedWidth);
}
if (NS_UNCONSTRAINEDSIZE != ComputedHeight() &&
NS_UNCONSTRAINEDSIZE != aAvailSpace.height) {
nscoord computedHeight =
aAvailSpace.height - mComputedBorderPadding.TopBottom();
computedHeight = std::max(0, computedHeight);
SetComputedHeight(computedHeight);
}
}
void
nsTableRowFrame::InitChildReflowState(nsPresContext& aPresContext,
const nsSize& aAvailSize,
bool aBorderCollapse,
nsTableCellReflowState& aReflowState)
{
nsMargin collapseBorder;
nsMargin* pCollapseBorder = nullptr;
if (aBorderCollapse) {
// we only reflow cells, so don't need to check frame type
nsBCTableCellFrame* bcCellFrame = (nsBCTableCellFrame*)aReflowState.frame;
if (bcCellFrame) {
pCollapseBorder = bcCellFrame->GetBorderWidth(collapseBorder);
}
}
aReflowState.Init(&aPresContext, -1, -1, pCollapseBorder);
aReflowState.FixUp(aAvailSize);
}
void
nsTableRowFrame::SetFixedHeight(nscoord aValue)
{
nscoord height = std::max(0, aValue);
if (HasFixedHeight()) {
if (height > mStyleFixedHeight) {
mStyleFixedHeight = height;
}
}
else {
mStyleFixedHeight = height;
if (height > 0) {
SetHasFixedHeight(true);
}
}
}
void
nsTableRowFrame::SetPctHeight(float aPctValue,
bool aForce)
{
nscoord height = std::max(0, NSToCoordRound(aPctValue * 100.0f));
if (HasPctHeight()) {
if ((height > mStylePctHeight) || aForce) {
mStylePctHeight = height;
}
}
else {
mStylePctHeight = height;
if (height > 0) {
SetHasPctHeight(true);
}
}
}
/* ----------- nsTableRowFrame ---------- */
NS_QUERYFRAME_HEAD(nsTableRowFrame)
NS_QUERYFRAME_ENTRY(nsTableRowFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
nsTableRowFrame::nsTableRowFrame(nsStyleContext* aContext)
: nsContainerFrame(aContext)
{
mBits.mRowIndex = mBits.mFirstInserted = 0;
ResetHeight(0);
}
nsTableRowFrame::~nsTableRowFrame()
{
}
void
nsTableRowFrame::Init(nsIContent* aContent,
nsIFrame* aParent,
nsIFrame* aPrevInFlow)
{
// Let the base class do its initialization
nsContainerFrame::Init(aContent, aParent, aPrevInFlow);
NS_ASSERTION(NS_STYLE_DISPLAY_TABLE_ROW == StyleDisplay()->mDisplay,
"wrong display on table row frame");
if (aPrevInFlow) {
// Set the row index
nsTableRowFrame* rowFrame = (nsTableRowFrame*)aPrevInFlow;
SetRowIndex(rowFrame->GetRowIndex());
}
}
/* virtual */ void
nsTableRowFrame::DidSetStyleContext(nsStyleContext* aOldStyleContext)
{
nsContainerFrame::DidSetStyleContext(aOldStyleContext);
if (!aOldStyleContext) //avoid this on init
return;
nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(this);
if (tableFrame->IsBorderCollapse() &&
tableFrame->BCRecalcNeeded(aOldStyleContext, StyleContext())) {
nsIntRect damageArea(0, GetRowIndex(), tableFrame->GetColCount(), 1);
tableFrame->AddBCDamageArea(damageArea);
}
}
NS_IMETHODIMP
nsTableRowFrame::AppendFrames(ChildListID aListID,
nsFrameList& aFrameList)
{
NS_ASSERTION(aListID == kPrincipalList, "unexpected child list");
const nsFrameList::Slice& newCells = mFrames.AppendFrames(nullptr, aFrameList);
// Add the new cell frames to the table
nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(this);
for (nsFrameList::Enumerator e(newCells) ; !e.AtEnd(); e.Next()) {
nsIFrame *childFrame = e.get();
NS_ASSERTION(IS_TABLE_CELL(childFrame->GetType()),"Not a table cell frame/pseudo frame construction failure");
tableFrame->AppendCell(static_cast<nsTableCellFrame&>(*childFrame), GetRowIndex());
}
PresContext()->PresShell()->FrameNeedsReflow(this, nsIPresShell::eTreeChange,
NS_FRAME_HAS_DIRTY_CHILDREN);
tableFrame->SetGeometryDirty();
return NS_OK;
}
NS_IMETHODIMP
nsTableRowFrame::InsertFrames(ChildListID aListID,
nsIFrame* aPrevFrame,
nsFrameList& aFrameList)
{
NS_ASSERTION(aListID == kPrincipalList, "unexpected child list");
NS_ASSERTION(!aPrevFrame || aPrevFrame->GetParent() == this,
"inserting after sibling frame with different parent");
//Insert Frames in the frame list
const nsFrameList::Slice& newCells = mFrames.InsertFrames(nullptr, aPrevFrame, aFrameList);
// Get the table frame
nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(this);
nsIAtom* cellFrameType = tableFrame->IsBorderCollapse() ? nsGkAtoms::bcTableCellFrame : nsGkAtoms::tableCellFrame;
nsTableCellFrame* prevCellFrame = (nsTableCellFrame *)nsTableFrame::GetFrameAtOrBefore(this, aPrevFrame, cellFrameType);
nsTArray<nsTableCellFrame*> cellChildren;
for (nsFrameList::Enumerator e(newCells); !e.AtEnd(); e.Next()) {
nsIFrame *childFrame = e.get();
NS_ASSERTION(IS_TABLE_CELL(childFrame->GetType()),"Not a table cell frame/pseudo frame construction failure");
cellChildren.AppendElement(static_cast<nsTableCellFrame*>(childFrame));
}
// insert the cells into the cell map
int32_t colIndex = -1;
if (prevCellFrame) {
prevCellFrame->GetColIndex(colIndex);
}
tableFrame->InsertCells(cellChildren, GetRowIndex(), colIndex);
PresContext()->PresShell()->FrameNeedsReflow(this, nsIPresShell::eTreeChange,
NS_FRAME_HAS_DIRTY_CHILDREN);
tableFrame->SetGeometryDirty();
return NS_OK;
}
NS_IMETHODIMP
nsTableRowFrame::RemoveFrame(ChildListID aListID,
nsIFrame* aOldFrame)
{
NS_ASSERTION(aListID == kPrincipalList, "unexpected child list");
nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(this);
nsTableCellFrame *cellFrame = do_QueryFrame(aOldFrame);
if (cellFrame) {
int32_t colIndex;
cellFrame->GetColIndex(colIndex);
// remove the cell from the cell map
tableFrame->RemoveCell(cellFrame, GetRowIndex());
// Remove the frame and destroy it
mFrames.DestroyFrame(aOldFrame);
PresContext()->PresShell()->
FrameNeedsReflow(this, nsIPresShell::eTreeChange,
NS_FRAME_HAS_DIRTY_CHILDREN);
tableFrame->SetGeometryDirty();
}
else {
NS_ERROR("unexpected frame type");
return NS_ERROR_INVALID_ARG;
}
return NS_OK;
}
/* virtual */ nsMargin
nsTableRowFrame::GetUsedMargin() const
{
return nsMargin(0,0,0,0);
}
/* virtual */ nsMargin
nsTableRowFrame::GetUsedBorder() const
{
return nsMargin(0,0,0,0);
}
/* virtual */ nsMargin
nsTableRowFrame::GetUsedPadding() const
{
return nsMargin(0,0,0,0);
}
nscoord
GetHeightOfRowsSpannedBelowFirst(nsTableCellFrame& aTableCellFrame,
nsTableFrame& aTableFrame)
{
nscoord height = 0;
nscoord cellSpacingY = aTableFrame.GetCellSpacingY();
int32_t rowSpan = aTableFrame.GetEffectiveRowSpan(aTableCellFrame);
// add in height of rows spanned beyond the 1st one
nsIFrame* nextRow = aTableCellFrame.GetParent()->GetNextSibling();
for (int32_t rowX = 1; ((rowX < rowSpan) && nextRow);) {
if (nsGkAtoms::tableRowFrame == nextRow->GetType()) {
height += nextRow->GetSize().height;
rowX++;
}
height += cellSpacingY;
nextRow = nextRow->GetNextSibling();
}
return height;
}
nsTableCellFrame*
nsTableRowFrame::GetFirstCell()
{
nsIFrame* childFrame = mFrames.FirstChild();
while (childFrame) {
nsTableCellFrame *cellFrame = do_QueryFrame(childFrame);
if (cellFrame) {
return cellFrame;
}
childFrame = childFrame->GetNextSibling();
}
return nullptr;
}
/**
* Post-reflow hook. This is where the table row does its post-processing
*/
void
nsTableRowFrame::DidResize()
{
// Resize and re-align the cell frames based on our row height
nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(this);
nsTableIterator iter(*this);
nsIFrame* childFrame = iter.First();
nsHTMLReflowMetrics desiredSize;
desiredSize.width = mRect.width;
desiredSize.height = mRect.height;
desiredSize.SetOverflowAreasToDesiredBounds();
while (childFrame) {
nsTableCellFrame *cellFrame = do_QueryFrame(childFrame);
if (cellFrame) {
nscoord cellHeight = mRect.height + GetHeightOfRowsSpannedBelowFirst(*cellFrame, *tableFrame);
// resize the cell's height
nsRect cellRect = cellFrame->GetRect();
nsRect cellVisualOverflow = cellFrame->GetVisualOverflowRect();
if (cellRect.height != cellHeight)
{
cellFrame->SetSize(nsSize(cellRect.width, cellHeight));
nsTableFrame::InvalidateTableFrame(cellFrame, cellRect,
cellVisualOverflow,
false);
}
// realign cell content based on the new height. We might be able to
// skip this if the height didn't change... maybe. Hard to tell.
cellFrame->VerticallyAlignChild(mMaxCellAscent);
// Always store the overflow, even if the height didn't change, since
// we'll lose part of our overflow area otherwise.
ConsiderChildOverflow(desiredSize.mOverflowAreas, cellFrame);
// Note that if the cell's *content* needs to change in response
// to this height, it will get a special height reflow.
}
// Get the next child
childFrame = iter.Next();
}
FinishAndStoreOverflow(&desiredSize);
if (HasView()) {
nsContainerFrame::SyncFrameViewAfterReflow(PresContext(), this, GetView(),
desiredSize.VisualOverflow(), 0);
}
// Let our base class do the usual work
}
// returns max-ascent amongst all cells that have 'vertical-align: baseline'
// *including* cells with rowspans
nscoord nsTableRowFrame::GetMaxCellAscent() const
{
return mMaxCellAscent;
}
nscoord nsTableRowFrame::GetRowBaseline()
{
if(mMaxCellAscent)
return mMaxCellAscent;
// If we don't have a baseline on any of the cells we go for the lowest
// content edge of the inner block frames.
// Every table cell has a cell frame with its border and padding. Inside
// the cell is a block frame. The cell is as high as the tallest cell in
// the parent row. As a consequence the block frame might not touch both
// the top and the bottom padding of it parent cell frame at the same time.
//
// bbbbbbbbbbbbbbbbbb cell border: b
// bppppppppppppppppb cell padding: p
// bpxxxxxxxxxxxxxxpb inner block: x
// bpx xpb
// bpx xpb
// bpx xpb
// bpxxxxxxxxxxxxxxpb base line
// bp pb
// bp pb
// bppppppppppppppppb
// bbbbbbbbbbbbbbbbbb
nsTableIterator iter(*this);
nsIFrame* childFrame = iter.First();
nscoord ascent = 0;
while (childFrame) {
if (IS_TABLE_CELL(childFrame->GetType())) {
nsIFrame* firstKid = childFrame->GetFirstPrincipalChild();
ascent = std::max(ascent, firstKid->GetRect().YMost());
}
// Get the next child
childFrame = iter.Next();
}
return ascent;
}
nscoord
nsTableRowFrame::GetHeight(nscoord aPctBasis) const
{
nscoord height = 0;
if ((aPctBasis > 0) && HasPctHeight()) {
height = NSToCoordRound(GetPctHeight() * (float)aPctBasis);
}
if (HasFixedHeight()) {
height = std::max(height, GetFixedHeight());
}
return std::max(height, GetContentHeight());
}
void
nsTableRowFrame::ResetHeight(nscoord aFixedHeight)
{
SetHasFixedHeight(false);
SetHasPctHeight(false);
SetFixedHeight(0);
SetPctHeight(0);
SetContentHeight(0);
if (aFixedHeight > 0) {
SetFixedHeight(aFixedHeight);
}
mMaxCellAscent = 0;
mMaxCellDescent = 0;
}
void
nsTableRowFrame::UpdateHeight(nscoord aHeight,
nscoord aAscent,
nscoord aDescent,
nsTableFrame* aTableFrame,
nsTableCellFrame* aCellFrame)
{
if (!aTableFrame || !aCellFrame) {
NS_ASSERTION(false , "invalid call");
return;
}
if (aHeight != NS_UNCONSTRAINEDSIZE) {
if (!(aCellFrame->HasVerticalAlignBaseline())) { // only the cell's height matters
if (GetHeight() < aHeight) {
int32_t rowSpan = aTableFrame->GetEffectiveRowSpan(*aCellFrame);
if (rowSpan == 1) {
SetContentHeight(aHeight);
}
}
}
else { // the alignment on the baseline can change the height
NS_ASSERTION((aAscent != NS_UNCONSTRAINEDSIZE) && (aDescent != NS_UNCONSTRAINEDSIZE), "invalid call");
// see if this is a long ascender
if (mMaxCellAscent < aAscent) {
mMaxCellAscent = aAscent;
}
// see if this is a long descender and without rowspan
if (mMaxCellDescent < aDescent) {
int32_t rowSpan = aTableFrame->GetEffectiveRowSpan(*aCellFrame);
if (rowSpan == 1) {
mMaxCellDescent = aDescent;
}
}
// keep the tallest height in sync
if (GetHeight() < mMaxCellAscent + mMaxCellDescent) {
SetContentHeight(mMaxCellAscent + mMaxCellDescent);
}
}
}
}
nscoord
nsTableRowFrame::CalcHeight(const nsHTMLReflowState& aReflowState)
{
nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(this);
nscoord computedHeight = (NS_UNCONSTRAINEDSIZE == aReflowState.ComputedHeight())
? 0 : aReflowState.ComputedHeight();
ResetHeight(computedHeight);
const nsStylePosition* position = StylePosition();
if (position->mHeight.ConvertsToLength()) {
SetFixedHeight(nsRuleNode::ComputeCoordPercentCalc(position->mHeight, 0));
}
else if (eStyleUnit_Percent == position->mHeight.GetUnit()) {
SetPctHeight(position->mHeight.GetPercentValue());
}
// calc() with percentages is treated like 'auto' on table rows.
for (nsIFrame* kidFrame = mFrames.FirstChild(); kidFrame;
kidFrame = kidFrame->GetNextSibling()) {
nsTableCellFrame *cellFrame = do_QueryFrame(kidFrame);
if (cellFrame) {
nsSize desSize = cellFrame->GetDesiredSize();
if ((NS_UNCONSTRAINEDSIZE == aReflowState.availableHeight) && !GetPrevInFlow()) {
CalculateCellActualHeight(cellFrame, desSize.height);
}
// height may have changed, adjust descent to absorb any excess difference
nscoord ascent;
if (!kidFrame->GetFirstPrincipalChild()->GetFirstPrincipalChild())
ascent = desSize.height;
else
ascent = cellFrame->GetCellBaseline();
nscoord descent = desSize.height - ascent;
UpdateHeight(desSize.height, ascent, descent, tableFrame, cellFrame);
}
}
return GetHeight();
}
/**
* We need a custom display item for table row backgrounds. This is only used
* when the table row is the root of a stacking context (e.g., has 'opacity').
* Table row backgrounds can extend beyond the row frame bounds, when
* the row contains row-spanning cells.
*/
class nsDisplayTableRowBackground : public nsDisplayTableItem {
public:
nsDisplayTableRowBackground(nsDisplayListBuilder* aBuilder,
nsTableRowFrame* aFrame) :
nsDisplayTableItem(aBuilder, aFrame) {
MOZ_COUNT_CTOR(nsDisplayTableRowBackground);
}
#ifdef NS_BUILD_REFCNT_LOGGING
virtual ~nsDisplayTableRowBackground() {
MOZ_COUNT_DTOR(nsDisplayTableRowBackground);
}
#endif
virtual void Paint(nsDisplayListBuilder* aBuilder,
nsRenderingContext* aCtx);
NS_DISPLAY_DECL_NAME("TableRowBackground", TYPE_TABLE_ROW_BACKGROUND)
};
void
nsDisplayTableRowBackground::Paint(nsDisplayListBuilder* aBuilder,
nsRenderingContext* aCtx)
{
nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(mFrame);
TableBackgroundPainter painter(tableFrame,
TableBackgroundPainter::eOrigin_TableRow,
mFrame->PresContext(), *aCtx,
mVisibleRect, ToReferenceFrame(),
aBuilder->GetBackgroundPaintFlags());
painter.PaintRow(static_cast<nsTableRowFrame*>(mFrame));
}
void
nsTableRowFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsRect& aDirtyRect,
const nsDisplayListSet& aLists)
{
nsDisplayTableItem* item = nullptr;
if (IsVisibleInSelection(aBuilder)) {
bool isRoot = aBuilder->IsAtRootOfPseudoStackingContext();
if (isRoot) {
// This background is created regardless of whether this frame is
// visible or not. Visibility decisions are delegated to the
// table background painter.
// We would use nsDisplayGeneric for this rare case except that we
// need the background to be larger than the row frame in some
// cases.
item = new (aBuilder) nsDisplayTableRowBackground(aBuilder, this);
aLists.BorderBackground()->AppendNewToTop(item);
}
}
nsTableFrame::DisplayGenericTablePart(aBuilder, this, aDirtyRect, aLists, item);
}
int
nsTableRowFrame::GetSkipSides() const
{
int skip = 0;
if (nullptr != GetPrevInFlow()) {
skip |= 1 << NS_SIDE_TOP;
}
if (nullptr != GetNextInFlow()) {
skip |= 1 << NS_SIDE_BOTTOM;
}
return skip;
}
// Calculate the cell's actual height given its pass2 height.
// Takes into account the specified height (in the style).
// Modifies the desired height that is passed in.
nsresult
nsTableRowFrame::CalculateCellActualHeight(nsTableCellFrame* aCellFrame,
nscoord& aDesiredHeight)
{
nscoord specifiedHeight = 0;
// Get the height specified in the style information
const nsStylePosition* position = aCellFrame->StylePosition();
nsTableFrame* tableFrame = nsTableFrame::GetTableFrame(this);
int32_t rowSpan = tableFrame->GetEffectiveRowSpan(*aCellFrame);
switch (position->mHeight.GetUnit()) {
case eStyleUnit_Calc: {
if (position->mHeight.CalcHasPercent()) {
// Treat this like "auto"
break;
}
// Fall through to the coord case
}
case eStyleUnit_Coord: {
nscoord outsideBoxSizing = 0;
// In quirks mode, table cell width should be content-box, but height
// should be border-box.
// Because of this historic anomaly, we do not use quirk.css
// (since we can't specify one value of box-sizing for width and another
// for height)
if (PresContext()->CompatibilityMode() != eCompatibility_NavQuirks) {
switch (position->mBoxSizing) {
case NS_STYLE_BOX_SIZING_CONTENT:
outsideBoxSizing = aCellFrame->GetUsedBorderAndPadding().TopBottom();
break;
case NS_STYLE_BOX_SIZING_PADDING:
outsideBoxSizing = aCellFrame->GetUsedBorder().TopBottom();
break;
default:
// NS_STYLE_BOX_SIZING_BORDER
break;
}
}
specifiedHeight =
nsRuleNode::ComputeCoordPercentCalc(position->mHeight, 0) +
outsideBoxSizing;
if (1 == rowSpan)
SetFixedHeight(specifiedHeight);
break;
}
case eStyleUnit_Percent: {
if (1 == rowSpan)
SetPctHeight(position->mHeight.GetPercentValue());
// pct heights are handled when all of the cells are finished, so don't set specifiedHeight
break;
}
case eStyleUnit_Auto:
default:
break;
}
// If the specified height is greater than the desired height, then use the specified height
if (specifiedHeight > aDesiredHeight)
aDesiredHeight = specifiedHeight;
return NS_OK;
}
// Calculates the available width for the table cell based on the known
// column widths taking into account column spans and column spacing
static nscoord
CalcAvailWidth(nsTableFrame& aTableFrame,
nsTableCellFrame& aCellFrame,
nscoord aCellSpacingX)
{
nscoord cellAvailWidth = 0;
int32_t colIndex;
aCellFrame.GetColIndex(colIndex);
int32_t colspan = aTableFrame.GetEffectiveColSpan(aCellFrame);
NS_ASSERTION(colspan > 0, "effective colspan should be positive");
for (int32_t spanX = 0; spanX < colspan; spanX++) {
cellAvailWidth += aTableFrame.GetColumnWidth(colIndex + spanX);
if (spanX > 0 &&
aTableFrame.ColumnHasCellSpacingBefore(colIndex + spanX)) {
cellAvailWidth += aCellSpacingX;
}
}
return cellAvailWidth;
}
nscoord
GetSpaceBetween(int32_t aPrevColIndex,
int32_t aColIndex,
int32_t aColSpan,
nsTableFrame& aTableFrame,
nscoord aCellSpacingX,
bool aIsLeftToRight,
bool aCheckVisibility)
{
nscoord space = 0;
int32_t colX;
if (aIsLeftToRight) {
for (colX = aPrevColIndex + 1; aColIndex > colX; colX++) {
bool isCollapsed = false;
if (!aCheckVisibility) {
space += aTableFrame.GetColumnWidth(colX);
}
else {
nsTableColFrame* colFrame = aTableFrame.GetColFrame(colX);
const nsStyleVisibility* colVis = colFrame->StyleVisibility();
bool collapseCol = (NS_STYLE_VISIBILITY_COLLAPSE == colVis->mVisible);
nsIFrame* cgFrame = colFrame->GetParent();
const nsStyleVisibility* groupVis = cgFrame->StyleVisibility();
bool collapseGroup = (NS_STYLE_VISIBILITY_COLLAPSE ==
groupVis->mVisible);
isCollapsed = collapseCol || collapseGroup;
if (!isCollapsed)
space += aTableFrame.GetColumnWidth(colX);
}
if (!isCollapsed && aTableFrame.ColumnHasCellSpacingBefore(colX)) {
space += aCellSpacingX;
}
}
}
else {
int32_t lastCol = aColIndex + aColSpan - 1;
for (colX = aPrevColIndex - 1; colX > lastCol; colX--) {
bool isCollapsed = false;
if (!aCheckVisibility) {
space += aTableFrame.GetColumnWidth(colX);
}
else {
nsTableColFrame* colFrame = aTableFrame.GetColFrame(colX);
const nsStyleVisibility* colVis = colFrame->StyleVisibility();
bool collapseCol = (NS_STYLE_VISIBILITY_COLLAPSE == colVis->mVisible);
nsIFrame* cgFrame = colFrame->GetParent();
const nsStyleVisibility* groupVis = cgFrame->StyleVisibility();
bool collapseGroup = (NS_STYLE_VISIBILITY_COLLAPSE ==
groupVis->mVisible);
isCollapsed = collapseCol || collapseGroup;
if (!isCollapsed)
space += aTableFrame.GetColumnWidth(colX);
}
if (!isCollapsed && aTableFrame.ColumnHasCellSpacingBefore(colX)) {
space += aCellSpacingX;
}
}
}
return space;
}
// subtract the heights of aRow's prev in flows from the unpaginated height
static
nscoord CalcHeightFromUnpaginatedHeight(nsPresContext* aPresContext,
nsTableRowFrame& aRow)
{
nscoord height = 0;
nsTableRowFrame* firstInFlow =
static_cast<nsTableRowFrame*>(aRow.GetFirstInFlow());
if (firstInFlow->HasUnpaginatedHeight()) {
height = firstInFlow->GetUnpaginatedHeight(aPresContext);
for (nsIFrame* prevInFlow = aRow.GetPrevInFlow(); prevInFlow;
prevInFlow = prevInFlow->GetPrevInFlow()) {
height -= prevInFlow->GetSize().height;
}
}
return std::max(height, 0);
}
nsresult
nsTableRowFrame::ReflowChildren(nsPresContext* aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState,
nsTableFrame& aTableFrame,
nsReflowStatus& aStatus)
{
aStatus = NS_FRAME_COMPLETE;
// XXXldb Should we be checking constrained height instead?
const bool isPaginated = aPresContext->IsPaginated();
const bool borderCollapse = aTableFrame.IsBorderCollapse();
nsresult rv = NS_OK;
nscoord cellSpacingX = aTableFrame.GetCellSpacingX();
int32_t cellColSpan = 1; // must be defined here so it's set properly for non-cell kids
nsTableIterator iter(*this);
// remember the col index of the previous cell to handle rowspans into this row
int32_t firstPrevColIndex = (iter.IsLeftToRight()) ? -1 : aTableFrame.GetColCount();
int32_t prevColIndex = firstPrevColIndex;
nscoord x = 0; // running total of children x offset
// This computes the max of all cell heights
nscoord cellMaxHeight = 0;
// Reflow each of our existing cell frames
for (nsIFrame* kidFrame = iter.First(); kidFrame; kidFrame = iter.Next()) {
nsTableCellFrame *cellFrame = do_QueryFrame(kidFrame);
if (!cellFrame) {
// XXXldb nsCSSFrameConstructor needs to enforce this!
NS_NOTREACHED("yikes, a non-row child");
// it's an unknown frame type, give it a generic reflow and ignore the results
nsTableCellReflowState kidReflowState(aPresContext, aReflowState,
kidFrame, nsSize(0,0), false);
InitChildReflowState(*aPresContext, nsSize(0,0), false, kidReflowState);
nsHTMLReflowMetrics desiredSize;
nsReflowStatus status;
ReflowChild(kidFrame, aPresContext, desiredSize, kidReflowState, 0, 0, 0, status);
kidFrame->DidReflow(aPresContext, nullptr, nsDidReflowStatus::FINISHED);
continue;
}
// See if we should only reflow the dirty child frames
bool doReflowChild = true;
if (!aReflowState.ShouldReflowAllKids() &&
!aTableFrame.IsGeometryDirty() &&
!NS_SUBTREE_DIRTY(kidFrame)) {
if (!aReflowState.mFlags.mSpecialHeightReflow)
doReflowChild = false;
}
else if ((NS_UNCONSTRAINEDSIZE != aReflowState.availableHeight)) {
// We don't reflow a rowspan >1 cell here with a constrained height.
// That happens in nsTableRowGroupFrame::SplitSpanningCells.
if (aTableFrame.GetEffectiveRowSpan(*cellFrame) > 1) {
doReflowChild = false;
}
}
if (aReflowState.mFlags.mSpecialHeightReflow) {
if (!isPaginated && !(cellFrame->GetStateBits() &
NS_FRAME_CONTAINS_RELATIVE_HEIGHT)) {
continue;
}
}
int32_t cellColIndex;
cellFrame->GetColIndex(cellColIndex);
cellColSpan = aTableFrame.GetEffectiveColSpan(*cellFrame);
// If the adjacent cell is in a prior row (because of a rowspan) add in the space
if ((iter.IsLeftToRight() && (prevColIndex != (cellColIndex - 1))) ||
(!iter.IsLeftToRight() && (prevColIndex != cellColIndex + cellColSpan))) {
x += GetSpaceBetween(prevColIndex, cellColIndex, cellColSpan, aTableFrame,
cellSpacingX, iter.IsLeftToRight(), false);
}
// remember the rightmost (ltr) or leftmost (rtl) column this cell spans into
prevColIndex = (iter.IsLeftToRight()) ? cellColIndex + (cellColSpan - 1) : cellColIndex;
// Reflow the child frame
nsRect kidRect = kidFrame->GetRect();
nsRect kidVisualOverflow = kidFrame->GetVisualOverflowRect();
bool firstReflow =
(kidFrame->GetStateBits() & NS_FRAME_FIRST_REFLOW) != 0;
if (doReflowChild) {
// Calculate the available width for the table cell using the known column widths
nscoord availCellWidth =
CalcAvailWidth(aTableFrame, *cellFrame, cellSpacingX);
nsHTMLReflowMetrics desiredSize;
// If the avail width is not the same as last time we reflowed the cell or
// the cell wants to be bigger than what was available last time or
// it is a style change reflow or we are printing, then we must reflow the
// cell. Otherwise we can skip the reflow.
// XXXldb Why is this condition distinct from doReflowChild above?
nsSize cellDesiredSize = cellFrame->GetDesiredSize();
if ((availCellWidth != cellFrame->GetPriorAvailWidth()) ||
(cellDesiredSize.width > cellFrame->GetPriorAvailWidth()) ||
(GetStateBits() & NS_FRAME_IS_DIRTY) ||
isPaginated ||
NS_SUBTREE_DIRTY(cellFrame) ||
// See if it needs a special reflow, or if it had one that we need to undo.
(cellFrame->GetStateBits() & NS_FRAME_CONTAINS_RELATIVE_HEIGHT) ||
HasPctHeight()) {
// Reflow the cell to fit the available width, height
// XXX The old IR_ChildIsDirty code used availCellWidth here.
nsSize kidAvailSize(availCellWidth, aReflowState.availableHeight);
// Reflow the child
nsTableCellReflowState kidReflowState(aPresContext, aReflowState,
kidFrame, kidAvailSize, false);
InitChildReflowState(*aPresContext, kidAvailSize, borderCollapse,
kidReflowState);
nsReflowStatus status;
rv = ReflowChild(kidFrame, aPresContext, desiredSize, kidReflowState,
x, 0, 0, status);
// allow the table to determine if/how the table needs to be rebalanced
// If any of the cells are not complete, then we're not complete
if (NS_FRAME_IS_NOT_COMPLETE(status)) {
aStatus = NS_FRAME_NOT_COMPLETE;
}
}
else {
if (x != kidRect.x) {
kidFrame->InvalidateFrameSubtree();
}
desiredSize.width = cellDesiredSize.width;
desiredSize.height = cellDesiredSize.height;
desiredSize.mOverflowAreas = cellFrame->GetOverflowAreas();
// if we are in a floated table, our position is not yet established, so we cannot reposition our views
// the containing block will do this for us after positioning the table
if (!aTableFrame.IsFloating()) {
// Because we may have moved the frame we need to make sure any views are
// positioned properly. We have to do this, because any one of our parent
// frames could have moved and we have no way of knowing...
nsTableFrame::RePositionViews(kidFrame);
}
}
if (NS_UNCONSTRAINEDSIZE == aReflowState.availableHeight) {
if (!GetPrevInFlow()) {
// Calculate the cell's actual height given its pass2 height. This
// function takes into account the specified height (in the style)
CalculateCellActualHeight(cellFrame, desiredSize.height);
}
// height may have changed, adjust descent to absorb any excess difference
nscoord ascent;
if (!kidFrame->GetFirstPrincipalChild()->GetFirstPrincipalChild())
ascent = desiredSize.height;
else
ascent = ((nsTableCellFrame *)kidFrame)->GetCellBaseline();
nscoord descent = desiredSize.height - ascent;
UpdateHeight(desiredSize.height, ascent, descent, &aTableFrame, cellFrame);
}
else {
cellMaxHeight = std::max(cellMaxHeight, desiredSize.height);
int32_t rowSpan = aTableFrame.GetEffectiveRowSpan((nsTableCellFrame&)*kidFrame);
if (1 == rowSpan) {
SetContentHeight(cellMaxHeight);
}
}
// Place the child
desiredSize.width = availCellWidth;
FinishReflowChild(kidFrame, aPresContext, nullptr, desiredSize, x, 0, 0);
nsTableFrame::InvalidateTableFrame(kidFrame, kidRect, kidVisualOverflow,
firstReflow);
x += desiredSize.width;
}
else {
if (kidRect.x != x) {
// Invalidate the old position
kidFrame->InvalidateFrameSubtree();
// move to the new position
kidFrame->SetPosition(nsPoint(x, kidRect.y));
nsTableFrame::RePositionViews(kidFrame);
// invalidate the new position
kidFrame->InvalidateFrameSubtree();
}
// we need to account for the cell's width even if it isn't reflowed
x += kidRect.width;
if (kidFrame->GetNextInFlow()) {
aStatus = NS_FRAME_NOT_COMPLETE;
}
}
ConsiderChildOverflow(aDesiredSize.mOverflowAreas, kidFrame);
x += cellSpacingX;
}
// just set our width to what was available. The table will calculate the width and not use our value.
aDesiredSize.width = aReflowState.availableWidth;
if (aReflowState.mFlags.mSpecialHeightReflow) {
aDesiredSize.height = mRect.height;
}
else if (NS_UNCONSTRAINEDSIZE == aReflowState.availableHeight) {
aDesiredSize.height = CalcHeight(aReflowState);
if (GetPrevInFlow()) {
nscoord height = CalcHeightFromUnpaginatedHeight(aPresContext, *this);
aDesiredSize.height = std::max(aDesiredSize.height, height);
}
else {
if (isPaginated && HasStyleHeight()) {
// set the unpaginated height so next in flows can try to honor it
SetHasUnpaginatedHeight(true);
SetUnpaginatedHeight(aPresContext, aDesiredSize.height);
}
if (isPaginated && HasUnpaginatedHeight()) {
aDesiredSize.height = std::max(aDesiredSize.height, GetUnpaginatedHeight(aPresContext));
}
}
}
else { // constrained height, paginated
// Compute the height we should have from style (subtracting the
// height from our prev-in-flows from the style height)
nscoord styleHeight = CalcHeightFromUnpaginatedHeight(aPresContext, *this);
if (styleHeight > aReflowState.availableHeight) {
styleHeight = aReflowState.availableHeight;
NS_FRAME_SET_INCOMPLETE(aStatus);
}
aDesiredSize.height = std::max(cellMaxHeight, styleHeight);
}
aDesiredSize.UnionOverflowAreasWithDesiredBounds();
FinishAndStoreOverflow(&aDesiredSize);
return rv;
}
/** Layout the entire row.
* This method stacks cells horizontally according to HTML 4.0 rules.