-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathnsTableRowFrame.cpp
1307 lines (1134 loc) · 49.9 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 "mozilla/Baseline.h"
#include "mozilla/Maybe.h"
#include "mozilla/PresShell.h"
#include "nsTableRowGroupFrame.h"
#include "nsPresContext.h"
#include "mozilla/ComputedStyle.h"
#include "mozilla/StaticPrefs_layout.h"
#include "nsStyleConsts.h"
#include "nsIContent.h"
#include "nsIFrame.h"
#include "nsIFrameInlines.h"
#include "nsTableFrame.h"
#include "nsTableCellFrame.h"
#include "nsCSSRendering.h"
#include "nsHTMLParts.h"
#include "nsTableColGroupFrame.h"
#include "nsTableColFrame.h"
#include "nsDisplayList.h"
#include <algorithm>
#ifdef ACCESSIBILITY
# include "nsAccessibilityService.h"
#endif
using namespace mozilla;
namespace mozilla {
struct TableCellReflowInput : public ReflowInput {
TableCellReflowInput(nsPresContext* aPresContext,
const ReflowInput& aParentReflowInput, nsIFrame* aFrame,
const LogicalSize& aAvailableSpace,
ReflowInput::InitFlags aFlags = {})
: ReflowInput(aPresContext, aParentReflowInput, aFrame, aAvailableSpace,
Nothing(), aFlags) {}
void FixUp(const LogicalSize& aAvailSpace);
};
} // namespace mozilla
void TableCellReflowInput::FixUp(const LogicalSize& aAvailSpace) {
// fix the mComputed values during a pass 2 reflow since the cell can be a
// percentage base
NS_WARNING_ASSERTION(
NS_UNCONSTRAINEDSIZE != aAvailSpace.ISize(mWritingMode),
"have unconstrained inline-size; this should only result from very large "
"sizes, not attempts at intrinsic inline size calculation");
if (NS_UNCONSTRAINEDSIZE != ComputedISize()) {
nscoord computedISize =
aAvailSpace.ISize(mWritingMode) -
ComputedLogicalBorderPadding(mWritingMode).IStartEnd(mWritingMode);
computedISize = std::max(0, computedISize);
SetComputedISize(computedISize);
}
if (NS_UNCONSTRAINEDSIZE != ComputedBSize() &&
NS_UNCONSTRAINEDSIZE != aAvailSpace.BSize(mWritingMode)) {
nscoord computedBSize =
aAvailSpace.BSize(mWritingMode) -
ComputedLogicalBorderPadding(mWritingMode).BStartEnd(mWritingMode);
computedBSize = std::max(0, computedBSize);
SetComputedBSize(computedBSize);
}
}
void nsTableRowFrame::InitChildReflowInput(nsPresContext& aPresContext,
const LogicalSize& aAvailSize,
bool aBorderCollapse,
TableCellReflowInput& aReflowInput) {
Maybe<LogicalMargin> collapseBorder;
if (aBorderCollapse) {
// we only reflow cells, so don't need to check frame type
nsBCTableCellFrame* bcCellFrame = (nsBCTableCellFrame*)aReflowInput.mFrame;
if (bcCellFrame) {
collapseBorder.emplace(
bcCellFrame->GetBorderWidth(aReflowInput.GetWritingMode()));
}
}
aReflowInput.Init(&aPresContext, Nothing(), collapseBorder);
aReflowInput.FixUp(aAvailSize);
}
void nsTableRowFrame::SetFixedBSize(nscoord aValue) {
nscoord bsize = std::max(0, aValue);
if (HasFixedBSize()) {
if (bsize > mStyleFixedBSize) {
mStyleFixedBSize = bsize;
}
} else {
mStyleFixedBSize = bsize;
if (bsize > 0) {
SetHasFixedBSize(true);
}
}
}
void nsTableRowFrame::SetPctBSize(float aPctValue, bool aForce) {
nscoord bsize = std::max(0, NSToCoordRound(aPctValue * 100.0f));
if (HasPctBSize()) {
if ((bsize > mStylePctBSize) || aForce) {
mStylePctBSize = bsize;
}
} else {
mStylePctBSize = bsize;
if (bsize > 0) {
SetHasPctBSize(true);
}
}
}
/* ----------- nsTableRowFrame ---------- */
NS_QUERYFRAME_HEAD(nsTableRowFrame)
NS_QUERYFRAME_ENTRY(nsTableRowFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
nsTableRowFrame::nsTableRowFrame(ComputedStyle* aStyle,
nsPresContext* aPresContext, ClassID aID)
: nsContainerFrame(aStyle, aPresContext, aID) {
mBits.mRowIndex = 0;
mBits.mHasFixedBSize = 0;
mBits.mHasPctBSize = 0;
mBits.mFirstInserted = 0;
ResetBSize();
}
nsTableRowFrame::~nsTableRowFrame() = default;
void nsTableRowFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
nsIFrame* aPrevInFlow) {
// Let the base class do its initialization
nsContainerFrame::Init(aContent, aParent, aPrevInFlow);
NS_ASSERTION(mozilla::StyleDisplay::TableRow == StyleDisplay()->mDisplay,
"wrong display on table row frame");
if (aPrevInFlow) {
// Set the row index
nsTableRowFrame* rowFrame = (nsTableRowFrame*)aPrevInFlow;
SetRowIndex(rowFrame->GetRowIndex());
} else {
mWritingMode = GetTableFrame()->GetWritingMode();
}
}
void nsTableRowFrame::Destroy(DestroyContext& aContext) {
nsTableFrame::MaybeUnregisterPositionedTablePart(this);
nsContainerFrame::Destroy(aContext);
}
/* virtual */
void nsTableRowFrame::DidSetComputedStyle(ComputedStyle* aOldComputedStyle) {
nsContainerFrame::DidSetComputedStyle(aOldComputedStyle);
nsTableFrame::PositionedTablePartMaybeChanged(this, aOldComputedStyle);
if (!aOldComputedStyle) {
return; // avoid the following on init
}
#ifdef ACCESSIBILITY
if (nsAccessibilityService* accService = GetAccService()) {
// If a table row's background color is now different from
// the background color of its previous row, it is possible our
// table now has alternating row colors. This changes whether or not
// the table is classified as a layout table or data table.
// We invalidate on every background color change to avoid
// walking the tree in search of the nearest row.
if (StyleBackground()->BackgroundColor(this) !=
aOldComputedStyle->StyleBackground()->BackgroundColor(
aOldComputedStyle)) {
// We send a notification here to invalidate the a11y cache on the
// table so the next fetch of IsProbablyLayoutTable() is accurate.
accService->TableLayoutGuessMaybeChanged(PresShell(), mContent);
}
}
#endif
nsTableFrame* tableFrame = GetTableFrame();
if (tableFrame->IsBorderCollapse() &&
tableFrame->BCRecalcNeeded(aOldComputedStyle, Style())) {
TableArea damageArea(0, GetRowIndex(), tableFrame->GetColCount(), 1);
tableFrame->AddBCDamageArea(damageArea);
}
}
void nsTableRowFrame::AppendFrames(ChildListID aListID,
nsFrameList&& aFrameList) {
NS_ASSERTION(aListID == FrameChildListID::Principal, "unexpected child list");
DrainSelfOverflowList(); // ensure the last frame is in mFrames
const nsFrameList::Slice& newCells =
mFrames.AppendFrames(nullptr, std::move(aFrameList));
// Add the new cell frames to the table
nsTableFrame* tableFrame = GetTableFrame();
for (nsIFrame* childFrame : newCells) {
NS_ASSERTION(childFrame->IsTableCellFrame(),
"Not a table cell frame/pseudo frame construction failure");
tableFrame->AppendCell(static_cast<nsTableCellFrame&>(*childFrame),
GetRowIndex());
}
PresShell()->FrameNeedsReflow(this, IntrinsicDirty::FrameAndAncestors,
NS_FRAME_HAS_DIRTY_CHILDREN);
tableFrame->SetGeometryDirty();
}
void nsTableRowFrame::InsertFrames(ChildListID aListID, nsIFrame* aPrevFrame,
const nsLineList::iterator* aPrevFrameLine,
nsFrameList&& aFrameList) {
NS_ASSERTION(aListID == FrameChildListID::Principal, "unexpected child list");
NS_ASSERTION(!aPrevFrame || aPrevFrame->GetParent() == this,
"inserting after sibling frame with different parent");
if (mFrames.IsEmpty() || (aPrevFrame && !aPrevFrame->GetNextSibling())) {
// This is actually an append (though our caller didn't figure that out),
// and our append codepath is both simpler/faster _and_ less buggy.
// https://bugzilla.mozilla.org/show_bug.cgi?id=1388898 tracks the bugginess
AppendFrames(aListID, std::move(aFrameList));
return;
}
DrainSelfOverflowList(); // ensure aPrevFrame is in mFrames
// Insert Frames in the frame list
const nsFrameList::Slice& newCells =
mFrames.InsertFrames(nullptr, aPrevFrame, std::move(aFrameList));
nsTableCellFrame* prevCellFrame =
static_cast<nsTableCellFrame*>(nsTableFrame::GetFrameAtOrBefore(
this, aPrevFrame, LayoutFrameType::TableCell));
nsTArray<nsTableCellFrame*> cellChildren;
for (nsIFrame* childFrame : newCells) {
NS_ASSERTION(childFrame->IsTableCellFrame(),
"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) {
colIndex = prevCellFrame->ColIndex();
}
nsTableFrame* tableFrame = GetTableFrame();
tableFrame->InsertCells(cellChildren, GetRowIndex(), colIndex);
PresShell()->FrameNeedsReflow(this, IntrinsicDirty::FrameAndAncestors,
NS_FRAME_HAS_DIRTY_CHILDREN);
tableFrame->SetGeometryDirty();
}
void nsTableRowFrame::RemoveFrame(DestroyContext& aContext, ChildListID aListID,
nsIFrame* aOldFrame) {
NS_ASSERTION(aListID == FrameChildListID::Principal, "unexpected child list");
MOZ_ASSERT((nsTableCellFrame*)do_QueryFrame(aOldFrame));
auto* cellFrame = static_cast<nsTableCellFrame*>(aOldFrame);
// remove the cell from the cell map
nsTableFrame* tableFrame = GetTableFrame();
tableFrame->RemoveCell(cellFrame, GetRowIndex());
// Remove the frame and destroy it
mFrames.DestroyFrame(aContext, aOldFrame);
PresShell()->FrameNeedsReflow(this, IntrinsicDirty::FrameAndAncestors,
NS_FRAME_HAS_DIRTY_CHILDREN);
tableFrame->SetGeometryDirty();
}
/* 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);
}
static nscoord GetBSizeOfRowsSpannedBelowFirst(
nsTableCellFrame& aTableCellFrame, nsTableFrame& aTableFrame,
const WritingMode aWM) {
nscoord bsize = 0;
int32_t rowSpan = aTableFrame.GetEffectiveRowSpan(aTableCellFrame);
// add in bsize of rows spanned beyond the 1st one
nsIFrame* nextRow = aTableCellFrame.GetParent()->GetNextSibling();
for (int32_t rowX = 1; ((rowX < rowSpan) && nextRow);) {
if (nextRow->IsTableRowFrame()) {
bsize += nextRow->BSize(aWM);
rowX++;
}
bsize += aTableFrame.GetRowSpacing(rowX);
nextRow = nextRow->GetNextSibling();
}
return bsize;
}
/**
* Post-reflow hook. This is where the table row does its post-processing
*/
void nsTableRowFrame::DidResize(ForceAlignTopForTableCell aForceAlignTop) {
// Resize and re-align the cell frames based on our row bsize
nsTableFrame* tableFrame = GetTableFrame();
WritingMode wm = GetWritingMode();
ReflowOutput desiredSize(wm);
desiredSize.SetSize(wm, GetLogicalSize(wm));
desiredSize.SetOverflowAreasToDesiredBounds();
nsSize containerSize = mRect.Size();
for (nsTableCellFrame* cellFrame = GetFirstCell(); cellFrame;
cellFrame = cellFrame->GetNextCell()) {
nscoord cellBSize = BSize(wm) + GetBSizeOfRowsSpannedBelowFirst(
*cellFrame, *tableFrame, wm);
// If the bsize for the cell has changed, we need to reset it;
// and in vertical-rl mode, we need to update the cell's block position
// to account for the containerSize, which may not have been known
// earlier, so we always apply it here.
LogicalSize cellSize = cellFrame->GetLogicalSize(wm);
if (cellSize.BSize(wm) != cellBSize || wm.IsVerticalRL()) {
nsRect cellOldRect = cellFrame->GetRect();
nsRect cellInkOverflow = cellFrame->InkOverflowRect();
if (wm.IsVerticalRL()) {
// Get the old position of the cell, as we want to preserve its
// inline coordinate.
LogicalPoint oldPos = cellFrame->GetLogicalPosition(wm, containerSize);
// The cell should normally be aligned with the row's block-start,
// so set the B component of the position to zero:
LogicalPoint newPos(wm, oldPos.I(wm), 0);
// ...unless relative positioning is in effect, in which case the
// cell may have been moved away from the row's block-start
if (cellFrame->IsRelativelyOrStickyPositioned()) {
// Find out where the cell would have been without relative
// positioning.
LogicalPoint oldNormalPos =
cellFrame->GetLogicalNormalPosition(wm, containerSize);
// The difference (if any) between oldPos and oldNormalPos reflects
// relative positioning that was applied to the cell, and which we
// need to incorporate when resetting the position.
newPos.B(wm) = oldPos.B(wm) - oldNormalPos.B(wm);
}
if (oldPos != newPos) {
cellFrame->SetPosition(wm, newPos, containerSize);
nsTableFrame::RePositionViews(cellFrame);
}
}
cellSize.BSize(wm) = cellBSize;
cellFrame->SetSize(wm, cellSize);
if (tableFrame->IsBorderCollapse()) {
nsTableFrame::InvalidateTableFrame(cellFrame, cellOldRect,
cellInkOverflow, false);
}
}
// realign cell content based on the new bsize. We might be able to
// skip this if the bsize didn't change... maybe. Hard to tell.
cellFrame->AlignChildWithinCell(mMaxCellAscent, aForceAlignTop);
// 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 bsize reflow.
}
FinishAndStoreOverflow(&desiredSize);
if (HasView()) {
nsContainerFrame::SyncFrameViewAfterReflow(PresContext(), this, GetView(),
desiredSize.InkOverflow(),
ReflowChildFlags::Default);
}
// 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; }
Maybe<nscoord> nsTableRowFrame::GetRowBaseline(WritingMode aWM) {
if (mMaxCellAscent) {
return Some(mMaxCellAscent);
}
// If we get here, we don't have a baseline on any of the cells in this row.
if (aWM.IsCentralBaseline()) {
return Nothing{};
}
nscoord ascent = 0;
for (nsIFrame* childFrame : mFrames) {
MOZ_ASSERT(childFrame->IsTableCellFrame());
nscoord s = Baseline::SynthesizeBOffsetFromContentBox(
childFrame, aWM, BaselineSharingGroup::First);
ascent = std::max(ascent, s);
}
return Some(ascent);
}
nscoord nsTableRowFrame::GetInitialBSize(nscoord aPctBasis) const {
nscoord bsize = 0;
if ((aPctBasis > 0) && HasPctBSize()) {
bsize = NSToCoordRound(GetPctBSize() * (float)aPctBasis);
}
if (HasFixedBSize()) {
bsize = std::max(bsize, GetFixedBSize());
}
return std::max(bsize, GetContentBSize());
}
void nsTableRowFrame::ResetBSize() {
SetHasFixedBSize(false);
SetHasPctBSize(false);
SetFixedBSize(0);
SetPctBSize(0);
SetContentBSize(0);
mMaxCellAscent = 0;
mMaxCellDescent = 0;
}
void nsTableRowFrame::UpdateBSize(nscoord aBSize, nsTableFrame* aTableFrame,
nsTableCellFrame* aCellFrame) {
if (!aTableFrame || !aCellFrame) {
MOZ_ASSERT_UNREACHABLE("Invalid call");
return;
}
if (aBSize == NS_UNCONSTRAINEDSIZE) {
return;
}
if (GetInitialBSize() < aBSize &&
aTableFrame->GetEffectiveRowSpan(*aCellFrame) == 1) {
SetContentBSize(aBSize);
}
if (aCellFrame->HasVerticalAlignBaseline()) {
if (auto ascent = aCellFrame->GetCellBaseline()) {
// see if this is a long ascender
if (mMaxCellAscent < *ascent) {
mMaxCellAscent = *ascent;
}
nscoord descent = aBSize - *ascent;
if (mMaxCellDescent < descent &&
aTableFrame->GetEffectiveRowSpan(*aCellFrame) == 1) {
mMaxCellDescent = descent;
}
}
}
}
nscoord nsTableRowFrame::CalcBSize(const ReflowInput& aReflowInput) {
nsTableFrame* tableFrame = GetTableFrame();
ResetBSize();
const nscoord computedBSize = aReflowInput.ComputedBSize();
if (computedBSize != NS_UNCONSTRAINEDSIZE && computedBSize > 0) {
SetFixedBSize(computedBSize);
}
WritingMode wm = aReflowInput.GetWritingMode();
const nsStylePosition* position = StylePosition();
const auto bsizeStyleCoord = position->BSize(wm, StyleDisplay()->mPosition);
if (bsizeStyleCoord->ConvertsToLength()) {
SetFixedBSize(bsizeStyleCoord->ToLength());
} else if (bsizeStyleCoord->ConvertsToPercentage()) {
SetPctBSize(bsizeStyleCoord->ToPercentage());
}
for (nsTableCellFrame* kidFrame = GetFirstCell(); kidFrame;
kidFrame = kidFrame->GetNextCell()) {
MOZ_ASSERT(kidFrame->GetWritingMode() == wm);
LogicalSize desSize = kidFrame->GetDesiredSize();
if (NS_UNCONSTRAINEDSIZE == aReflowInput.AvailableBSize() &&
!GetPrevInFlow()) {
desSize.BSize(wm) = CalcCellActualBSize(kidFrame, desSize.BSize(wm), wm);
}
UpdateBSize(desSize.BSize(wm), tableFrame, kidFrame);
}
return GetInitialBSize();
}
void nsTableRowFrame::PaintCellBackgroundsForFrame(
nsIFrame* aFrame, nsDisplayListBuilder* aBuilder,
const nsDisplayListSet& aLists, const nsPoint& aOffset) {
// Compute background rect by iterating all cell frame.
const nsPoint toReferenceFrame = aBuilder->ToReferenceFrame(aFrame);
for (nsTableCellFrame* cell = GetFirstCell(); cell;
cell = cell->GetNextCell()) {
if (!cell->ShouldPaintBackground(aBuilder)) {
continue;
}
auto cellRect =
cell->GetRectRelativeToSelf() + cell->GetNormalPosition() + aOffset;
if (!aBuilder->GetDirtyRect().Intersects(cellRect)) {
continue;
}
cellRect += toReferenceFrame;
nsDisplayBackgroundImage::AppendBackgroundItemsToTop(
aBuilder, aFrame, cellRect, aLists.BorderBackground(), true,
aFrame->GetRectRelativeToSelf() + toReferenceFrame, cell);
}
}
void nsTableRowFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsDisplayListSet& aLists) {
DisplayOutsetBoxShadow(aBuilder, aLists.BorderBackground());
PaintCellBackgroundsForFrame(this, aBuilder, aLists);
DisplayInsetBoxShadow(aBuilder, aLists.BorderBackground());
DisplayOutline(aBuilder, aLists);
for (nsIFrame* kid : PrincipalChildList()) {
BuildDisplayListForChild(aBuilder, kid, aLists);
}
}
LogicalSides nsTableRowFrame::GetLogicalSkipSides() const {
LogicalSides skip(mWritingMode);
if (MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak ==
StyleBoxDecorationBreak::Clone)) {
return skip;
}
if (GetPrevInFlow()) {
skip += LogicalSide::BStart;
}
if (GetNextInFlow()) {
skip += LogicalSide::BEnd;
}
return skip;
}
nscoord nsTableRowFrame::CalcCellActualBSize(nsTableCellFrame* aCellFrame,
const nscoord& aDesiredBSize,
WritingMode aWM) {
nscoord specifiedBSize = 0;
// Get the bsize specified in the style information
const nsStylePosition* position = aCellFrame->StylePosition();
int32_t rowSpan = GetTableFrame()->GetEffectiveRowSpan(*aCellFrame);
const auto bsizeStyleCoord =
position->BSize(aWM, aCellFrame->StyleDisplay()->mPosition);
if (bsizeStyleCoord->ConvertsToLength()) {
// In quirks mode, table cell bsize should always be border-box.
// https://quirks.spec.whatwg.org/#the-table-cell-height-box-sizing-quirk
specifiedBSize = bsizeStyleCoord->ToLength();
if (PresContext()->CompatibilityMode() != eCompatibility_NavQuirks &&
position->mBoxSizing == StyleBoxSizing::Content) {
specifiedBSize +=
aCellFrame->GetLogicalUsedBorderAndPadding(aWM).BStartEnd(aWM);
}
if (1 == rowSpan) {
SetFixedBSize(specifiedBSize);
}
} else if (bsizeStyleCoord->ConvertsToPercentage()) {
if (1 == rowSpan) {
SetPctBSize(bsizeStyleCoord->ToPercentage());
}
}
// If the specified bsize is greater than the desired bsize,
// then use the specified bsize
return std::max(specifiedBSize, aDesiredBSize);
}
// Calculates the available isize for the table cell based on the known
// column isizes taking into account column spans and column spacing
static nscoord CalcAvailISize(nsTableFrame& aTableFrame,
nsTableCellFrame& aCellFrame) {
nscoord cellAvailISize = 0;
uint32_t colIndex = aCellFrame.ColIndex();
int32_t colspan = aTableFrame.GetEffectiveColSpan(aCellFrame);
NS_ASSERTION(colspan > 0, "effective colspan should be positive");
nsTableFrame* fifTable =
static_cast<nsTableFrame*>(aTableFrame.FirstInFlow());
for (int32_t spanX = 0; spanX < colspan; spanX++) {
cellAvailISize += fifTable->GetColumnISizeFromFirstInFlow(colIndex + spanX);
if (spanX > 0 && aTableFrame.ColumnHasCellSpacingBefore(colIndex + spanX)) {
cellAvailISize += aTableFrame.GetColSpacing(colIndex + spanX - 1);
}
}
return cellAvailISize;
}
static nscoord GetSpaceBetween(int32_t aPrevColIndex, int32_t aColIndex,
int32_t aColSpan, nsTableFrame& aTableFrame,
bool aCheckVisibility) {
nscoord space = 0;
int32_t colIdx;
nsTableFrame* fifTable =
static_cast<nsTableFrame*>(aTableFrame.FirstInFlow());
for (colIdx = aPrevColIndex + 1; aColIndex > colIdx; colIdx++) {
bool isCollapsed = false;
if (!aCheckVisibility) {
space += fifTable->GetColumnISizeFromFirstInFlow(colIdx);
} else {
nsTableColFrame* colFrame = aTableFrame.GetColFrame(colIdx);
const nsStyleVisibility* colVis = colFrame->StyleVisibility();
bool collapseCol = StyleVisibility::Collapse == colVis->mVisible;
nsIFrame* cgFrame = colFrame->GetParent();
const nsStyleVisibility* groupVis = cgFrame->StyleVisibility();
bool collapseGroup = StyleVisibility::Collapse == groupVis->mVisible;
isCollapsed = collapseCol || collapseGroup;
if (!isCollapsed) {
space += fifTable->GetColumnISizeFromFirstInFlow(colIdx);
}
}
if (!isCollapsed && aTableFrame.ColumnHasCellSpacingBefore(colIdx)) {
space += aTableFrame.GetColSpacing(colIdx - 1);
}
}
return space;
}
// subtract the bsizes of aRow's prev in flows from the unpaginated bsize
static nscoord CalcBSizeFromUnpaginatedBSize(nsTableRowFrame& aRow,
WritingMode aWM) {
nscoord bsize = 0;
nsTableRowFrame* firstInFlow =
static_cast<nsTableRowFrame*>(aRow.FirstInFlow());
if (firstInFlow->HasUnpaginatedBSize()) {
bsize = firstInFlow->GetUnpaginatedBSize();
for (nsIFrame* prevInFlow = aRow.GetPrevInFlow(); prevInFlow;
prevInFlow = prevInFlow->GetPrevInFlow()) {
bsize -= prevInFlow->BSize(aWM);
}
}
return std::max(bsize, 0);
}
void nsTableRowFrame::ReflowChildren(nsPresContext* aPresContext,
ReflowOutput& aDesiredSize,
const ReflowInput& aReflowInput,
nsTableFrame& aTableFrame,
nsReflowStatus& aStatus) {
aStatus.Reset();
// XXXldb Should we be checking constrained bsize instead?
const bool isPaginated = aPresContext->IsPaginated();
const bool borderCollapse = aTableFrame.IsBorderCollapse();
int32_t cellColSpan =
1; // must be defined here so it's set properly for non-cell kids
// remember the col index of the previous cell to handle rowspans into this
// row
int32_t prevColIndex = -1;
nscoord iCoord = 0; // running total of children inline-coord offset
// This computes the max of all cell bsizes
nscoord cellMaxBSize = 0;
// Reflow each of our existing cell frames
WritingMode wm = aReflowInput.GetWritingMode();
nsSize containerSize = aReflowInput.ComputedSizeAsContainerIfConstrained();
for (nsTableCellFrame* kidFrame = GetFirstCell(); kidFrame;
kidFrame = kidFrame->GetNextCell()) {
// See if we should only reflow the dirty child frames
bool doReflowChild = true;
if (!aReflowInput.ShouldReflowAllKids() && !aTableFrame.IsGeometryDirty() &&
!kidFrame->IsSubtreeDirty()) {
if (!aReflowInput.mFlags.mSpecialBSizeReflow) {
doReflowChild = false;
}
} else if (NS_UNCONSTRAINEDSIZE != aReflowInput.AvailableBSize()) {
// We don't reflow a rowspan >1 cell here with a constrained bsize.
// That happens in nsTableRowGroupFrame::SplitSpanningCells.
if (aTableFrame.GetEffectiveRowSpan(*kidFrame) > 1) {
doReflowChild = false;
}
}
if (aReflowInput.mFlags.mSpecialBSizeReflow && !isPaginated &&
!kidFrame->HasAnyStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE)) {
continue;
}
uint32_t cellColIndex = kidFrame->ColIndex();
cellColSpan = aTableFrame.GetEffectiveColSpan(*kidFrame);
// If the adjacent cell is in a prior row (because of a rowspan) add in the
// space NOTE: prevColIndex can be -1 here.
if (prevColIndex != (static_cast<int32_t>(cellColIndex) - 1)) {
iCoord += GetSpaceBetween(prevColIndex, cellColIndex, cellColSpan,
aTableFrame, false);
}
// remember the rightmost (ltr) or leftmost (rtl) column this cell spans
// into
prevColIndex = cellColIndex + (cellColSpan - 1);
// Reflow the child frame
nsRect kidRect = kidFrame->GetRect();
LogicalPoint origKidNormalPosition =
kidFrame->GetLogicalNormalPosition(wm, containerSize);
nsRect kidInkOverflow = kidFrame->InkOverflowRect();
LogicalPoint kidPosition(wm, iCoord, 0);
bool firstReflow = kidFrame->HasAnyStateBits(NS_FRAME_FIRST_REFLOW);
if (doReflowChild) {
// Calculate the available isize for the table cell using the known
// column isizes
nscoord availCellISize = CalcAvailISize(aTableFrame, *kidFrame);
Maybe<TableCellReflowInput> kidReflowInput;
ReflowOutput desiredSize(aReflowInput);
// If the avail isize 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?
NS_ASSERTION(kidFrame->GetWritingMode() == wm,
"expected consistent writing-mode within table");
LogicalSize cellDesiredSize = kidFrame->GetDesiredSize();
if (availCellISize != kidFrame->GetPriorAvailISize() ||
cellDesiredSize.ISize(wm) > kidFrame->GetPriorAvailISize() ||
HasAnyStateBits(NS_FRAME_IS_DIRTY) || isPaginated ||
kidFrame->IsSubtreeDirty() ||
// See if it needs a special reflow, or if it had one that we need to
// undo.
kidFrame->HasAnyStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE) ||
kidFrame->BCBordersChanged() || HasPctBSize()) {
// Reflow the cell to fit the available isize, bsize
// XXX The old IR_ChildIsDirty code used availCellISize here.
LogicalSize kidAvailSize(wm, availCellISize,
aReflowInput.AvailableBSize());
// Reflow the child
kidReflowInput.emplace(aPresContext, aReflowInput, kidFrame,
kidAvailSize,
ReflowInput::InitFlag::CallerWillInit);
InitChildReflowInput(*aPresContext, kidAvailSize, borderCollapse,
*kidReflowInput);
nsReflowStatus status;
ReflowChild(kidFrame, aPresContext, desiredSize, *kidReflowInput, wm,
kidPosition, containerSize, ReflowChildFlags::Default,
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 (status.IsIncomplete()) {
aStatus.Reset();
aStatus.SetIncomplete();
}
} else {
if (iCoord != origKidNormalPosition.I(wm)) {
kidFrame->InvalidateFrameSubtree();
}
desiredSize.SetSize(wm, cellDesiredSize);
desiredSize.mOverflowAreas = kidFrame->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 == aReflowInput.AvailableBSize()) {
if (!GetPrevInFlow()) {
desiredSize.BSize(wm) =
CalcCellActualBSize(kidFrame, desiredSize.BSize(wm), wm);
}
// bsize may have changed, adjust descent to absorb any excess
// difference
UpdateBSize(desiredSize.BSize(wm), &aTableFrame, kidFrame);
} else {
cellMaxBSize = std::max(cellMaxBSize, desiredSize.BSize(wm));
int32_t rowSpan = aTableFrame.GetEffectiveRowSpan(*kidFrame);
if (1 == rowSpan) {
SetContentBSize(cellMaxBSize);
}
}
// Place the child
desiredSize.ISize(wm) = availCellISize;
ReflowChildFlags flags = ReflowChildFlags::Default;
if (kidReflowInput) {
// We reflowed. Apply relative positioning in the normal way.
flags = ReflowChildFlags::ApplyRelativePositioning;
} else if (kidFrame->IsRelativelyOrStickyPositioned()) {
// We didn't reflow. Do the positioning part of what
// MovePositionBy does internally. (This codepath should really
// be merged into the else below if we can.)
nsMargin* computedOffsetProp =
kidFrame->GetProperty(nsIFrame::ComputedOffsetProperty());
// On our fist reflow sticky children may not have the property yet (we
// need to reflow the children first to size the scroll frame).
LogicalMargin computedOffsets(
wm, computedOffsetProp ? *computedOffsetProp : nsMargin());
ReflowInput::ApplyRelativePositioning(kidFrame, wm, computedOffsets,
&kidPosition, containerSize);
}
// In vertical-rl mode, we are likely to have containerSize.width = 0
// because ComputedWidth() was NS_UNCONSTRAINEDSIZE.
// For cases where that's wrong, we will fix up the position later.
FinishReflowChild(kidFrame, aPresContext, desiredSize,
kidReflowInput.ptrOr(nullptr), wm, kidPosition,
containerSize, flags);
nsTableFrame* tableFrame = GetTableFrame();
if (tableFrame->IsBorderCollapse()) {
nsTableFrame::InvalidateTableFrame(kidFrame, kidRect, kidInkOverflow,
firstReflow);
}
iCoord += desiredSize.ISize(wm);
} else {
if (iCoord != origKidNormalPosition.I(wm)) {
// Invalidate the old position
kidFrame->InvalidateFrameSubtree();
// Move to the new position. As above, we need to account for relative
// positioning.
kidFrame->MovePositionBy(
wm, LogicalPoint(wm, iCoord - origKidNormalPosition.I(wm), 0));
nsTableFrame::RePositionViews(kidFrame);
// invalidate the new position
kidFrame->InvalidateFrameSubtree();
}
// we need to account for the cell's isize even if it isn't reflowed
iCoord += kidFrame->ISize(wm);
if (kidFrame->GetNextInFlow()) {
aStatus.Reset();
aStatus.SetIncomplete();
}
}
ConsiderChildOverflow(aDesiredSize.mOverflowAreas, kidFrame);
iCoord += aTableFrame.GetColSpacing(cellColIndex);
}
// Just set our isize to what was available.
// The table will calculate the isize and not use our value.
aDesiredSize.ISize(wm) = aReflowInput.AvailableISize();
if (aReflowInput.mFlags.mSpecialBSizeReflow) {
aDesiredSize.BSize(wm) = BSize(wm);
} else if (NS_UNCONSTRAINEDSIZE == aReflowInput.AvailableBSize()) {
aDesiredSize.BSize(wm) = CalcBSize(aReflowInput);
if (GetPrevInFlow()) {
nscoord bsize = CalcBSizeFromUnpaginatedBSize(*this, wm);
aDesiredSize.BSize(wm) = std::max(aDesiredSize.BSize(wm), bsize);
} else {
if (isPaginated && HasStyleBSize()) {
// set the unpaginated bsize so next in flows can try to honor it
SetUnpaginatedBSize(aDesiredSize.BSize(wm));
}
if (isPaginated && HasUnpaginatedBSize()) {
aDesiredSize.BSize(wm) =
std::max(aDesiredSize.BSize(wm), GetUnpaginatedBSize());
}
}
} else { // constrained bsize, paginated
// Compute the bsize we should have from style (subtracting the
// bsize from our prev-in-flows from the style bsize)
nscoord styleBSize = CalcBSizeFromUnpaginatedBSize(*this, wm);
if (styleBSize > aReflowInput.AvailableBSize()) {
styleBSize = aReflowInput.AvailableBSize();
aStatus.SetIncomplete();
}
aDesiredSize.BSize(wm) = std::max(cellMaxBSize, styleBSize);
}
if (wm.IsVerticalRL()) {
// Any children whose width was not the same as our final
// aDesiredSize.BSize will have been misplaced earlier at the
// FinishReflowChild stage. So fix them up now.
for (nsIFrame* kidFrame : mFrames) {
if (kidFrame->BSize(wm) != aDesiredSize.BSize(wm)) {
kidFrame->MovePositionBy(
wm,
LogicalPoint(wm, 0, kidFrame->BSize(wm) - aDesiredSize.BSize(wm)));
nsTableFrame::RePositionViews(kidFrame);
// Do we need to InvalidateFrameSubtree() here?
}
}
}
aDesiredSize.UnionOverflowAreasWithDesiredBounds();
FinishAndStoreOverflow(&aDesiredSize);
}
/** Layout the entire row.
* This method stacks cells in the inline dir according to HTML 4.0 rules.
*/
void nsTableRowFrame::Reflow(nsPresContext* aPresContext,
ReflowOutput& aDesiredSize,
const ReflowInput& aReflowInput,
nsReflowStatus& aStatus) {
MarkInReflow();
DO_GLOBAL_REFLOW_COUNT("nsTableRowFrame");
MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
WritingMode wm = aReflowInput.GetWritingMode();
nsTableFrame* tableFrame = GetTableFrame();
const nsStyleVisibility* rowVis = StyleVisibility();
bool collapseRow = StyleVisibility::Collapse == rowVis->mVisible;
if (collapseRow) {
tableFrame->SetNeedToCollapse(true);
}
// see if a special bsize reflow needs to occur due to having a pct bsize
nsTableFrame::CheckRequestSpecialBSizeReflow(aReflowInput);
// See if we have a cell with specified/pct bsize
InitHasCellWithStyleBSize(tableFrame);
ReflowChildren(aPresContext, aDesiredSize, aReflowInput, *tableFrame,
aStatus);
if (aPresContext->IsPaginated() && !aStatus.IsFullyComplete() &&
ShouldAvoidBreakInside(aReflowInput)) {
aStatus.SetInlineLineBreakBeforeAndReset();
}
// Just set our isize to what was available.
// The table will calculate the isize and not use our value.
aDesiredSize.ISize(wm) = aReflowInput.AvailableISize();
// If our parent is in initial reflow, it'll handle invalidating our
// entire overflow rect.
if (!GetParent()->HasAnyStateBits(NS_FRAME_FIRST_REFLOW) &&
nsSize(aDesiredSize.Width(), aDesiredSize.Height()) != mRect.Size()) {
InvalidateFrame();
}
// Any absolutely-positioned children will get reflowed in
// nsIFrame::FixupPositionedTableParts in another pass, so propagate our
// dirtiness to them before our parent clears our dirty bits.
PushDirtyBitToAbsoluteFrames();
}
nscoord nsTableRowFrame::ReflowCellFrame(nsPresContext* aPresContext,
const ReflowInput& aReflowInput,
bool aIsTopOfPage,
nsTableCellFrame* aCellFrame,
nscoord aAvailableBSize,
nsReflowStatus& aStatus) {
MOZ_ASSERT(aPresContext->IsPaginated(),
"ReflowCellFrame currently supports only paged media!");
MOZ_ASSERT(aAvailableBSize != NS_UNCONSTRAINEDSIZE,
"Why split cell frame if available bsize is unconstrained?");
WritingMode wm = aReflowInput.GetWritingMode();
// Reflow the cell frame with the specified height. Use the existing width
nsSize containerSize = aCellFrame->GetSize();
LogicalRect cellRect = aCellFrame->GetLogicalRect(wm, containerSize);
nsRect cellInkOverflow = aCellFrame->InkOverflowRect();
LogicalSize cellSize = cellRect.Size(wm);
LogicalSize availSize(wm, cellRect.ISize(wm), aAvailableBSize);
bool borderCollapse = GetTableFrame()->IsBorderCollapse();
NS_ASSERTION(aCellFrame->GetWritingMode() == wm,
"expected consistent writing-mode within table");
TableCellReflowInput cellReflowInput(aPresContext, aReflowInput, aCellFrame,
availSize,
ReflowInput::InitFlag::CallerWillInit);
InitChildReflowInput(*aPresContext, availSize, borderCollapse,
cellReflowInput);
cellReflowInput.mFlags.mIsTopOfPage = aIsTopOfPage;
ReflowOutput desiredSize(aReflowInput);
ReflowChild(aCellFrame, aPresContext, desiredSize, cellReflowInput, 0, 0,
ReflowChildFlags::NoMoveFrame, aStatus);