forked from mozilla-firefox/firefox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflowInput.cpp
3047 lines (2766 loc) · 127 KB
/
ReflowInput.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: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 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/. */
/* struct containing the input to nsIFrame::Reflow */
#include "mozilla/ReflowInput.h"
#include <algorithm>
#include "CounterStyleManager.h"
#include "LayoutLogging.h"
#include "mozilla/dom/HTMLInputElement.h"
#include "mozilla/ScrollContainerFrame.h"
#include "mozilla/WritingModes.h"
#include "nsBlockFrame.h"
#include "nsFlexContainerFrame.h"
#include "nsFontInflationData.h"
#include "nsFontMetrics.h"
#include "nsGkAtoms.h"
#include "nsGridContainerFrame.h"
#include "nsIContent.h"
#include "nsIFrame.h"
#include "nsIFrameInlines.h"
#include "nsImageFrame.h"
#include "nsIPercentBSizeObserver.h"
#include "nsLayoutUtils.h"
#include "nsLineBox.h"
#include "nsPresContext.h"
#include "nsStyleConsts.h"
#include "nsTableFrame.h"
#include "StickyScrollContainer.h"
using namespace mozilla;
using namespace mozilla::css;
using namespace mozilla::dom;
using namespace mozilla::layout;
static bool CheckNextInFlowParenthood(nsIFrame* aFrame, nsIFrame* aParent) {
nsIFrame* frameNext = aFrame->GetNextInFlow();
nsIFrame* parentNext = aParent->GetNextInFlow();
return frameNext && parentNext && frameNext->GetParent() == parentNext;
}
/**
* Adjusts the margin for a list (ol, ul), if necessary, depending on
* font inflation settings. Unfortunately, because bullets from a list are
* placed in the margin area, we only have ~40px in which to place the
* bullets. When they are inflated, however, this causes problems, since
* the text takes up more space than is available in the margin.
*
* This method will return a small amount (in app units) by which the
* margin can be adjusted, so that the space is available for list
* bullets to be rendered with font inflation enabled.
*/
static nscoord FontSizeInflationListMarginAdjustment(const nsIFrame* aFrame) {
// As an optimization we check this block frame specific bit up front before
// we even check if the frame is a block frame. That's only valid so long as
// we also have the `IsBlockFrameOrSubclass()` call below. Calling that is
// expensive though, and we want to avoid it if we know `HasMarker()` would
// return false.
if (!aFrame->HasAnyStateBits(NS_BLOCK_HAS_MARKER)) {
return 0;
}
// On desktop font inflation is disabled, so this will always early exit
// quickly, but checking the frame state bit is still quicker then this call
// and very likely to early exit on its own so we check this second.
float inflation = nsLayoutUtils::FontSizeInflationFor(aFrame);
if (inflation <= 1.0f) {
return 0;
}
if (!aFrame->IsBlockFrameOrSubclass()) {
return 0;
}
// We only want to adjust the margins if we're dealing with an ordered list.
// We already checked this above.
MOZ_ASSERT(static_cast<const nsBlockFrame*>(aFrame)->HasMarker());
const auto* list = aFrame->StyleList();
if (list->mListStyleType.IsNone()) {
return 0;
}
// The HTML spec states that the default padding for ordered lists
// begins at 40px, indicating that we have 40px of space to place a
// bullet. When performing font inflation calculations, we add space
// equivalent to this, but simply inflated at the same amount as the
// text, in app units.
auto margin = nsPresContext::CSSPixelsToAppUnits(40) * (inflation - 1);
if (!list->mListStyleType.IsName()) {
return margin;
}
nsAtom* type = list->mListStyleType.AsName().AsAtom();
if (type != nsGkAtoms::disc && type != nsGkAtoms::circle &&
type != nsGkAtoms::square && type != nsGkAtoms::disclosure_closed &&
type != nsGkAtoms::disclosure_open) {
return margin;
}
return 0;
}
SizeComputationInput::SizeComputationInput(nsIFrame* aFrame,
gfxContext* aRenderingContext)
: mFrame(aFrame),
mRenderingContext(aRenderingContext),
mWritingMode(aFrame->GetWritingMode()),
mIsThemed(aFrame->IsThemed()),
mComputedMargin(mWritingMode),
mComputedBorderPadding(mWritingMode),
mComputedPadding(mWritingMode) {
MOZ_ASSERT(mFrame);
}
SizeComputationInput::SizeComputationInput(
nsIFrame* aFrame, gfxContext* aRenderingContext,
WritingMode aContainingBlockWritingMode, nscoord aContainingBlockISize,
const Maybe<LogicalMargin>& aBorder, const Maybe<LogicalMargin>& aPadding)
: SizeComputationInput(aFrame, aRenderingContext) {
MOZ_ASSERT(!mFrame->IsTableColFrame());
InitOffsets(aContainingBlockWritingMode, aContainingBlockISize,
mFrame->Type(), {}, aBorder, aPadding);
}
// Initialize a <b>root</b> reflow input with a rendering context to
// use for measuring things.
ReflowInput::ReflowInput(nsPresContext* aPresContext, nsIFrame* aFrame,
gfxContext* aRenderingContext,
const LogicalSize& aAvailableSpace, InitFlags aFlags)
: SizeComputationInput(aFrame, aRenderingContext),
mAvailableSize(aAvailableSpace) {
MOZ_ASSERT(aRenderingContext, "no rendering context");
MOZ_ASSERT(aPresContext, "no pres context");
MOZ_ASSERT(aFrame, "no frame");
MOZ_ASSERT(aPresContext == aFrame->PresContext(), "wrong pres context");
if (aFlags.contains(InitFlag::DummyParentReflowInput)) {
mFlags.mDummyParentReflowInput = true;
}
if (aFlags.contains(InitFlag::StaticPosIsCBOrigin)) {
mFlags.mStaticPosIsCBOrigin = true;
}
if (!aFlags.contains(InitFlag::CallerWillInit)) {
Init(aPresContext);
}
// When we encounter a PageContent frame this will be set to true.
mFlags.mCanHaveClassABreakpoints = false;
}
static nsSize GetICBSize(const nsPresContext* aPresContext,
const nsIFrame* aFrame) {
if (!aPresContext->IsPaginated()) {
return aPresContext->GetVisibleArea().Size();
}
for (const nsIFrame* f = aFrame->GetParent(); f; f = f->GetParent()) {
if (f->IsPageContentFrame()) {
return f->GetSize();
}
}
return aPresContext->GetPageSize();
}
// Initialize a reflow input for a child frame's reflow. Some state
// is copied from the parent reflow input; the remaining state is
// computed.
ReflowInput::ReflowInput(nsPresContext* aPresContext,
const ReflowInput& aParentReflowInput,
nsIFrame* aFrame, const LogicalSize& aAvailableSpace,
const Maybe<LogicalSize>& aContainingBlockSize,
InitFlags aFlags,
const StyleSizeOverrides& aSizeOverrides,
ComputeSizeFlags aComputeSizeFlags)
: SizeComputationInput(aFrame, aParentReflowInput.mRenderingContext),
mParentReflowInput(&aParentReflowInput),
mFloatManager(aParentReflowInput.mFloatManager),
mLineLayout(mFrame->IsLineParticipant() ? aParentReflowInput.mLineLayout
: nullptr),
mBreakType(aParentReflowInput.mBreakType),
mPercentBSizeObserver(
(aParentReflowInput.mPercentBSizeObserver &&
aParentReflowInput.mPercentBSizeObserver->NeedsToObserve(*this))
? aParentReflowInput.mPercentBSizeObserver
: nullptr),
mFlags(aParentReflowInput.mFlags),
mStyleSizeOverrides(aSizeOverrides),
mComputeSizeFlags(aComputeSizeFlags),
mReflowDepth(aParentReflowInput.mReflowDepth + 1),
mAvailableSize(aAvailableSpace) {
MOZ_ASSERT(aPresContext, "no pres context");
MOZ_ASSERT(aFrame, "no frame");
MOZ_ASSERT(aPresContext == aFrame->PresContext(), "wrong pres context");
MOZ_ASSERT(!mFlags.mSpecialBSizeReflow || !aFrame->IsSubtreeDirty(),
"frame should be clean when getting special bsize reflow");
if (mWritingMode.IsOrthogonalTo(mParentReflowInput->GetWritingMode())) {
// If the block establishes an orthogonal flow, set up its AvailableISize
// per https://drafts.csswg.org/css-writing-modes/#orthogonal-auto
auto GetISizeConstraint = [this](const nsIFrame* aFrame) -> nscoord {
nscoord limit = NS_UNCONSTRAINEDSIZE;
const auto* pos = aFrame->StylePosition();
if (auto size =
nsLayoutUtils::GetAbsoluteSize(pos->ISize(mWritingMode))) {
limit = size.value();
} else if (auto maxSize = nsLayoutUtils::GetAbsoluteSize(
pos->MaxISize(mWritingMode))) {
limit = maxSize.value();
}
if (limit != NS_UNCONSTRAINEDSIZE) {
if (auto minSize =
nsLayoutUtils::GetAbsoluteSize(pos->MinISize(mWritingMode))) {
limit = std::max(limit, minSize.value());
}
}
return limit;
};
// See if the containing block has a fixed size we should respect:
const nsIFrame* cb = mFrame->GetContainingBlock();
nscoord cbLimit = GetISizeConstraint(cb);
nscoord scLimit = NS_UNCONSTRAINEDSIZE;
// If the containing block was not a scroll container itself, look up the
// parent chain for a scroller size that we should respect.
// XXX Could maybe use nsLayoutUtils::GetNearestScrollContainerFrame here,
// but unsure if we need the additional complexity it supports?
if (!cb->IsScrollContainerFrame()) {
for (const nsIFrame* p = mFrame->GetParent(); p; p = p->GetParent()) {
if (p->IsScrollContainerFrame()) {
scLimit = GetISizeConstraint(p);
// Only the closest ancestor scroller is relevant, so quit as soon as
// we've found one (whether or not it had fixed sizing).
break;
}
}
}
LogicalSize icbSize(mWritingMode, GetICBSize(aPresContext, mFrame));
nscoord icbLimit = icbSize.ISize(mWritingMode);
SetAvailableISize(std::min(icbLimit, std::min(scLimit, cbLimit)));
}
// Note: mFlags was initialized as a copy of aParentReflowInput.mFlags up in
// this constructor's init list, so the only flags that we need to explicitly
// initialize here are those that may need a value other than our parent's.
mFlags.mNextInFlowUntouched =
aParentReflowInput.mFlags.mNextInFlowUntouched &&
CheckNextInFlowParenthood(aFrame, aParentReflowInput.mFrame);
mFlags.mAssumingHScrollbar = mFlags.mAssumingVScrollbar = false;
mFlags.mIsColumnBalancing = false;
mFlags.mColumnSetWrapperHasNoBSizeLeft = false;
mFlags.mTreatBSizeAsIndefinite = false;
mFlags.mDummyParentReflowInput = false;
mFlags.mStaticPosIsCBOrigin = aFlags.contains(InitFlag::StaticPosIsCBOrigin);
mFlags.mIOffsetsNeedCSSAlign = mFlags.mBOffsetsNeedCSSAlign = false;
// aPresContext->IsPaginated() and the named pages pref should have been
// checked when constructing the root ReflowInput.
if (aParentReflowInput.mFlags.mCanHaveClassABreakpoints) {
MOZ_ASSERT(aPresContext->IsPaginated(),
"mCanHaveClassABreakpoints set during non-paginated reflow.");
}
{
switch (mFrame->Type()) {
case LayoutFrameType::PageContent:
// PageContent requires paginated reflow.
MOZ_ASSERT(aPresContext->IsPaginated(),
"nsPageContentFrame should not be in non-paginated reflow");
MOZ_ASSERT(!mFlags.mCanHaveClassABreakpoints,
"mFlags.mCanHaveClassABreakpoints should have been "
"initalized to false before we found nsPageContentFrame");
mFlags.mCanHaveClassABreakpoints = true;
break;
case LayoutFrameType::Block: // FALLTHROUGH
case LayoutFrameType::Canvas: // FALLTHROUGH
case LayoutFrameType::FlexContainer: // FALLTHROUGH
case LayoutFrameType::GridContainer:
if (mFrame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
// Never allow breakpoints inside of out-of-flow frames.
mFlags.mCanHaveClassABreakpoints = false;
break;
}
// This frame type can have class A breakpoints, inherit this flag
// from the parent (this is done for all flags during construction).
// This also includes Canvas frames, as each PageContent frame always
// has exactly one child which is a Canvas frame.
// Do NOT include the subclasses of BlockFrame here, as the ones for
// which this could be applicable (ColumnSetWrapper and the MathML
// frames) cannot have class A breakpoints.
MOZ_ASSERT(mFlags.mCanHaveClassABreakpoints ==
aParentReflowInput.mFlags.mCanHaveClassABreakpoints);
break;
default:
mFlags.mCanHaveClassABreakpoints = false;
break;
}
}
if (aFlags.contains(InitFlag::DummyParentReflowInput) ||
(mParentReflowInput->mFlags.mDummyParentReflowInput &&
mFrame->IsTableFrame())) {
mFlags.mDummyParentReflowInput = true;
}
if (!aFlags.contains(InitFlag::CallerWillInit)) {
Init(aPresContext, aContainingBlockSize);
}
}
template <typename SizeOrMaxSize>
nscoord SizeComputationInput::ComputeISizeValue(
const LogicalSize& aContainingBlockSize, StyleBoxSizing aBoxSizing,
const SizeOrMaxSize& aSize) const {
WritingMode wm = GetWritingMode();
const auto borderPadding = ComputedLogicalBorderPadding(wm);
const LogicalSize contentEdgeToBoxSizing =
aBoxSizing == StyleBoxSizing::Border ? borderPadding.Size(wm)
: LogicalSize(wm);
const nscoord boxSizingToMarginEdgeISize =
borderPadding.IStartEnd(wm) + ComputedLogicalMargin(wm).IStartEnd(wm) -
contentEdgeToBoxSizing.ISize(wm);
return mFrame
->ComputeISizeValue(mRenderingContext, wm, aContainingBlockSize,
contentEdgeToBoxSizing, boxSizingToMarginEdgeISize,
aSize, mFrame->StylePosition()->BSize(wm),
mFrame->GetAspectRatio())
.mISize;
}
template <typename SizeOrMaxSize>
nscoord SizeComputationInput::ComputeBSizeValueHandlingStretch(
nscoord aContainingBlockBSize, StyleBoxSizing aBoxSizing,
const SizeOrMaxSize& aSize) const {
if (aSize.BehavesLikeStretchOnBlockAxis()) {
WritingMode wm = GetWritingMode();
return nsLayoutUtils::ComputeStretchContentBoxBSize(
aContainingBlockBSize, ComputedLogicalMargin(wm).Size(wm).BSize(wm),
ComputedLogicalBorderPadding(wm).Size(wm).BSize(wm));
}
return ComputeBSizeValue(aContainingBlockBSize, aBoxSizing,
aSize.AsLengthPercentage());
}
nscoord SizeComputationInput::ComputeBSizeValue(
nscoord aContainingBlockBSize, StyleBoxSizing aBoxSizing,
const LengthPercentage& aSize) const {
WritingMode wm = GetWritingMode();
nscoord inside = 0;
if (aBoxSizing == StyleBoxSizing::Border) {
inside = ComputedLogicalBorderPadding(wm).BStartEnd(wm);
}
return nsLayoutUtils::ComputeBSizeValue(aContainingBlockBSize, inside, aSize);
}
WritingMode ReflowInput::GetCBWritingMode() const {
return mCBReflowInput ? mCBReflowInput->GetWritingMode()
: mFrame->GetContainingBlock()->GetWritingMode();
}
nsSize ReflowInput::ComputedSizeAsContainerIfConstrained() const {
LogicalSize size = ComputedSize();
if (size.ISize(mWritingMode) == NS_UNCONSTRAINEDSIZE) {
size.ISize(mWritingMode) = 0;
} else {
size.ISize(mWritingMode) += mComputedBorderPadding.IStartEnd(mWritingMode);
}
if (size.BSize(mWritingMode) == NS_UNCONSTRAINEDSIZE) {
size.BSize(mWritingMode) = 0;
} else {
size.BSize(mWritingMode) += mComputedBorderPadding.BStartEnd(mWritingMode);
}
return size.GetPhysicalSize(mWritingMode);
}
bool ReflowInput::ShouldReflowAllKids() const {
// Note that we could make a stronger optimization for IsBResize if
// we use it in a ShouldReflowChild test that replaces the current
// checks of NS_FRAME_IS_DIRTY | NS_FRAME_HAS_DIRTY_CHILDREN, if it
// were tested there along with NS_FRAME_CONTAINS_RELATIVE_BSIZE.
// This would need to be combined with a slight change in which
// frames NS_FRAME_CONTAINS_RELATIVE_BSIZE is marked on.
return mFrame->HasAnyStateBits(NS_FRAME_IS_DIRTY) || IsIResize() ||
(IsBResize() &&
mFrame->HasAnyStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE)) ||
mFlags.mIsInLastColumnBalancingReflow;
}
void ReflowInput::SetComputedISize(nscoord aComputedISize,
ResetResizeFlags aFlags) {
// It'd be nice to assert that |frame| is not in reflow, but this fails
// because viewport frames reset the computed isize on a copy of their reflow
// input when reflowing fixed-pos kids. In that case we actually don't want
// to mess with the resize flags, because comparing the frame's rect to the
// munged computed isize is pointless.
NS_WARNING_ASSERTION(aComputedISize >= 0, "Invalid computed inline-size!");
if (ComputedISize() != aComputedISize) {
mComputedSize.ISize(mWritingMode) = std::max(0, aComputedISize);
if (aFlags == ResetResizeFlags::Yes) {
InitResizeFlags(mFrame->PresContext(), mFrame->Type());
}
}
}
void ReflowInput::SetComputedBSize(nscoord aComputedBSize,
ResetResizeFlags aFlags) {
// It'd be nice to assert that |frame| is not in reflow, but this fails
// for the same reason as above.
NS_WARNING_ASSERTION(aComputedBSize >= 0, "Invalid computed block-size!");
if (ComputedBSize() != aComputedBSize) {
mComputedSize.BSize(mWritingMode) = std::max(0, aComputedBSize);
if (aFlags == ResetResizeFlags::Yes) {
InitResizeFlags(mFrame->PresContext(), mFrame->Type());
}
}
}
void ReflowInput::Init(nsPresContext* aPresContext,
const Maybe<LogicalSize>& aContainingBlockSize,
const Maybe<LogicalMargin>& aBorder,
const Maybe<LogicalMargin>& aPadding) {
LAYOUT_WARN_IF_FALSE(AvailableISize() != NS_UNCONSTRAINEDSIZE,
"have unconstrained inline-size; this should only "
"result from very large sizes, not attempts at "
"intrinsic inline-size calculation");
mStylePosition = mFrame->StylePosition();
mStyleDisplay = mFrame->StyleDisplay();
mStyleBorder = mFrame->StyleBorder();
mStyleMargin = mFrame->StyleMargin();
InitCBReflowInput();
LayoutFrameType type = mFrame->Type();
if (type == LayoutFrameType::Placeholder) {
// Placeholders have a no-op Reflow method that doesn't need the rest of
// this initialization, so we bail out early.
mComputedSize.SizeTo(mWritingMode, 0, 0);
return;
}
mFlags.mIsReplaced = mFrame->IsReplaced();
InitConstraints(aPresContext, aContainingBlockSize, aBorder, aPadding, type);
InitResizeFlags(aPresContext, type);
InitDynamicReflowRoot();
nsIFrame* parent = mFrame->GetParent();
if (parent && parent->HasAnyStateBits(NS_FRAME_IN_CONSTRAINED_BSIZE) &&
!(parent->IsScrollContainerFrame() &&
parent->StyleDisplay()->mOverflowY != StyleOverflow::Hidden)) {
mFrame->AddStateBits(NS_FRAME_IN_CONSTRAINED_BSIZE);
} else if (type == LayoutFrameType::SVGForeignObject) {
// An SVG foreignObject frame is inherently constrained block-size.
mFrame->AddStateBits(NS_FRAME_IN_CONSTRAINED_BSIZE);
} else {
const auto& bSizeCoord = mStylePosition->BSize(mWritingMode);
const auto& maxBSizeCoord = mStylePosition->MaxBSize(mWritingMode);
if ((!bSizeCoord.BehavesLikeInitialValueOnBlockAxis() ||
!maxBSizeCoord.BehavesLikeInitialValueOnBlockAxis()) &&
// Don't set NS_FRAME_IN_CONSTRAINED_BSIZE on body or html elements.
(mFrame->GetContent() && !(mFrame->GetContent()->IsAnyOfHTMLElements(
nsGkAtoms::body, nsGkAtoms::html)))) {
// If our block-size was specified as a percentage, then this could
// actually resolve to 'auto', based on:
// http://www.w3.org/TR/CSS21/visudet.html#the-height-property
nsIFrame* containingBlk = mFrame;
while (containingBlk) {
const nsStylePosition* stylePos = containingBlk->StylePosition();
const auto& bSizeCoord = stylePos->BSize(mWritingMode);
const auto& maxBSizeCoord = stylePos->MaxBSize(mWritingMode);
if ((bSizeCoord.IsLengthPercentage() && !bSizeCoord.HasPercent()) ||
(maxBSizeCoord.IsLengthPercentage() &&
!maxBSizeCoord.HasPercent())) {
mFrame->AddStateBits(NS_FRAME_IN_CONSTRAINED_BSIZE);
break;
} else if (bSizeCoord.HasPercent() || maxBSizeCoord.HasPercent()) {
if (!(containingBlk = containingBlk->GetContainingBlock())) {
// If we've reached the top of the tree, then we don't have
// a constrained block-size.
mFrame->RemoveStateBits(NS_FRAME_IN_CONSTRAINED_BSIZE);
break;
}
continue;
} else {
mFrame->RemoveStateBits(NS_FRAME_IN_CONSTRAINED_BSIZE);
break;
}
}
} else {
mFrame->RemoveStateBits(NS_FRAME_IN_CONSTRAINED_BSIZE);
}
}
if (mParentReflowInput &&
mParentReflowInput->GetWritingMode().IsOrthogonalTo(mWritingMode)) {
// Orthogonal frames are always reflowed with an unconstrained
// dimension to avoid incomplete reflow across an orthogonal
// boundary. Normally this is the block-size, but for column sets
// with auto-height it's the inline-size, so that they can add
// columns in the container's block direction
if (type == LayoutFrameType::ColumnSet &&
mStylePosition->ISize(mWritingMode).IsAuto()) {
SetComputedISize(NS_UNCONSTRAINEDSIZE, ResetResizeFlags::No);
} else {
SetAvailableBSize(NS_UNCONSTRAINEDSIZE);
}
}
if (mFrame->GetContainSizeAxes().mBContained) {
// In the case that a box is size contained in block axis, we want to ensure
// that it is also monolithic. We do this by setting AvailableBSize() to an
// unconstrained size to avoid fragmentation.
SetAvailableBSize(NS_UNCONSTRAINEDSIZE);
}
LAYOUT_WARN_IF_FALSE(
(mStyleDisplay->IsInlineOutsideStyle() && !mFrame->IsReplaced()) ||
type == LayoutFrameType::Text ||
ComputedISize() != NS_UNCONSTRAINEDSIZE,
"have unconstrained inline-size; this should only "
"result from very large sizes, not attempts at "
"intrinsic inline-size calculation");
}
static bool MightBeContainingBlockFor(nsIFrame* aMaybeContainingBlock,
nsIFrame* aFrame,
const nsStyleDisplay* aStyleDisplay) {
// Keep this in sync with nsIFrame::GetContainingBlock.
if (aFrame->IsAbsolutelyPositioned(aStyleDisplay) &&
aMaybeContainingBlock == aFrame->GetParent()) {
return true;
}
return aMaybeContainingBlock->IsBlockContainer();
}
void ReflowInput::InitCBReflowInput() {
if (!mParentReflowInput) {
mCBReflowInput = nullptr;
return;
}
if (mParentReflowInput->mFlags.mDummyParentReflowInput) {
mCBReflowInput = mParentReflowInput;
return;
}
// To avoid a long walk up the frame tree check if the parent frame can be a
// containing block for mFrame.
if (MightBeContainingBlockFor(mParentReflowInput->mFrame, mFrame,
mStyleDisplay) &&
mParentReflowInput->mFrame ==
mFrame->GetContainingBlock(0, mStyleDisplay)) {
// Inner table frames need to use the containing block of the outer
// table frame.
if (mFrame->IsTableFrame()) {
mCBReflowInput = mParentReflowInput->mCBReflowInput;
} else {
mCBReflowInput = mParentReflowInput;
}
} else {
mCBReflowInput = mParentReflowInput->mCBReflowInput;
}
}
/* Check whether CalcQuirkContainingBlockHeight would stop on the
* given reflow input, using its block as a height. (essentially
* returns false for any case in which CalcQuirkContainingBlockHeight
* has a "continue" in its main loop.)
*
* XXX Maybe refactor CalcQuirkContainingBlockHeight so it uses
* this function as well
*/
static bool IsQuirkContainingBlockHeight(const ReflowInput* rs,
LayoutFrameType aFrameType) {
if (LayoutFrameType::Block == aFrameType ||
LayoutFrameType::ScrollContainer == aFrameType) {
// Note: This next condition could change due to a style change,
// but that would cause a style reflow anyway, which means we're ok.
if (NS_UNCONSTRAINEDSIZE == rs->ComputedHeight()) {
if (!rs->mFrame->IsAbsolutelyPositioned(rs->mStyleDisplay)) {
return false;
}
}
}
return true;
}
void ReflowInput::InitResizeFlags(nsPresContext* aPresContext,
LayoutFrameType aFrameType) {
SetIResize(false);
SetBResize(false);
SetBResizeForPercentages(false);
const WritingMode wm = mWritingMode; // just a shorthand
// We should report that we have a resize in the inline dimension if
// *either* the border-box size or the content-box size in that
// dimension has changed. It might not actually be necessary to do
// this if the border-box size has changed and the content-box size
// has not changed, but since we've historically used the flag to mean
// border-box size change, continue to do that. It's possible for
// the content-box size to change without a border-box size change or
// a style change given (1) a fixed width (possibly fixed by max-width
// or min-width), box-sizing:border-box, and percentage padding;
// (2) box-sizing:content-box, M% width, and calc(Npx - M%) padding.
//
// However, we don't actually have the information at this point to tell
// whether the content-box size has changed, since both style data and the
// UsedPaddingProperty() have already been updated in
// SizeComputationInput::InitOffsets(). So, we check the HasPaddingChange()
// bit for the cases where it's possible for the content-box size to have
// changed without either (a) a change in the border-box size or (b) an
// nsChangeHint_NeedDirtyReflow change hint due to change in border or
// padding.
//
// We don't clear the HasPaddingChange() bit here, since sometimes we
// construct reflow input (e.g. in nsBlockFrame::ReflowBlockFrame to compute
// margin collapsing) without reflowing the frame. Instead, we clear it in
// nsIFrame::DidReflow().
bool isIResize =
// is the border-box resizing?
mFrame->ISize(wm) !=
ComputedISize() + ComputedLogicalBorderPadding(wm).IStartEnd(wm) ||
// or is the content-box resizing? (see comment above)
mFrame->HasPaddingChange();
if (mFrame->HasAnyStateBits(NS_FRAME_FONT_INFLATION_FLOW_ROOT) &&
nsLayoutUtils::FontSizeInflationEnabled(aPresContext)) {
// Create our font inflation data if we don't have it already, and
// give it our current width information.
bool dirty = nsFontInflationData::UpdateFontInflationDataISizeFor(*this) &&
// Avoid running this at the box-to-block interface
// (where we shouldn't be inflating anyway, and where
// reflow input construction is probably to construct a
// dummy parent reflow input anyway).
!mFlags.mDummyParentReflowInput;
if (dirty || (!mFrame->GetParent() && isIResize)) {
// When font size inflation is enabled, a change in either:
// * the effective width of a font inflation flow root
// * the width of the frame
// needs to cause a dirty reflow since they change the font size
// inflation calculations, which in turn change the size of text,
// line-heights, etc. This is relatively similar to a classic
// case of style change reflow, except that because inflation
// doesn't affect the intrinsic sizing codepath, there's no need
// to invalidate intrinsic sizes.
//
// Note that this makes horizontal resizing a good bit more
// expensive. However, font size inflation is targeted at a set of
// devices (zoom-and-pan devices) where the main use case for
// horizontal resizing needing to be efficient (window resizing) is
// not present. It does still increase the cost of dynamic changes
// caused by script where a style or content change in one place
// causes a resize in another (e.g., rebalancing a table).
// FIXME: This isn't so great for the cases where
// ReflowInput::SetComputedWidth is called, if the first time
// we go through InitResizeFlags we set IsHResize() to true, and then
// the second time we'd set it to false even without the
// NS_FRAME_IS_DIRTY bit already set.
if (mFrame->IsSVGForeignObjectFrame()) {
// Foreign object frames use dirty bits in a special way.
mFrame->AddStateBits(NS_FRAME_HAS_DIRTY_CHILDREN);
nsIFrame* kid = mFrame->PrincipalChildList().FirstChild();
if (kid) {
kid->MarkSubtreeDirty();
}
} else {
mFrame->MarkSubtreeDirty();
}
// Mark intrinsic widths on all descendants dirty. We need to do
// this (1) since we're changing the size of text and need to
// clear text runs on text frames and (2) since we actually are
// changing some intrinsic widths, but only those that live inside
// of containers.
// It makes sense to do this for descendants but not ancestors
// (which is unusual) because we're only changing the unusual
// inflation-dependent intrinsic widths (i.e., ones computed with
// nsPresContext::mInflationDisabledForShrinkWrap set to false),
// which should never affect anything outside of their inflation
// flow root (or, for that matter, even their inflation
// container).
// This is also different from what PresShell::FrameNeedsReflow
// does because it doesn't go through placeholders. It doesn't
// need to because we're actually doing something that cares about
// frame tree geometry (the width on an ancestor) rather than
// style.
AutoTArray<nsIFrame*, 32> stack;
stack.AppendElement(mFrame);
do {
nsIFrame* f = stack.PopLastElement();
for (const auto& childList : f->ChildLists()) {
for (nsIFrame* kid : childList.mList) {
kid->MarkIntrinsicISizesDirty();
stack.AppendElement(kid);
}
}
} while (stack.Length() != 0);
}
}
SetIResize(!mFrame->HasAnyStateBits(NS_FRAME_IS_DIRTY) && isIResize);
// XXX Should we really need to null check mCBReflowInput? (We do for
// at least nsBoxFrame).
if (mFrame->HasBSizeChange()) {
// When we have an nsChangeHint_UpdateComputedBSize, we'll set a bit
// on the frame to indicate we're resizing. This might catch cases,
// such as a change between auto and a length, where the box doesn't
// actually resize but children with percentages resize (since those
// percentages become auto if their containing block is auto).
SetBResize(true);
SetBResizeForPercentages(true);
// We don't clear the HasBSizeChange state here, since sometimes we
// construct a ReflowInput (e.g. in nsBlockFrame::ReflowBlockFrame to
// compute margin collapsing) without reflowing the frame. Instead, we
// clear it in nsIFrame::DidReflow.
} else if (mCBReflowInput &&
mCBReflowInput->IsBResizeForPercentagesForWM(wm) &&
(mStylePosition->BSize(wm).HasPercent() ||
mStylePosition->MinBSize(wm).HasPercent() ||
mStylePosition->MaxBSize(wm).HasPercent())) {
// We have a percentage (or calc-with-percentage) block-size, and the
// value it's relative to has changed.
SetBResize(true);
SetBResizeForPercentages(true);
} else if (aFrameType == LayoutFrameType::TableCell &&
(mFlags.mSpecialBSizeReflow ||
mFrame->FirstInFlow()->HasAnyStateBits(
NS_TABLE_CELL_HAD_SPECIAL_REFLOW)) &&
mFrame->HasAnyStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE)) {
// Need to set the bit on the cell so that
// mCBReflowInput->IsBResize() is set correctly below when
// reflowing descendant.
SetBResize(true);
SetBResizeForPercentages(true);
} else if (mCBReflowInput && mFrame->IsBlockWrapper()) {
// XXX Is this problematic for relatively positioned inlines acting
// as containing block for absolutely positioned elements?
// Possibly; in that case we should at least be checking
// IsSubtreeDirty(), I'd think.
SetBResize(mCBReflowInput->IsBResizeForWM(wm));
SetBResizeForPercentages(mCBReflowInput->IsBResizeForPercentagesForWM(wm));
} else if (ComputedBSize() == NS_UNCONSTRAINEDSIZE) {
// We have an 'auto' block-size.
if (eCompatibility_NavQuirks == aPresContext->CompatibilityMode() &&
mCBReflowInput) {
// FIXME: This should probably also check IsIResize().
SetBResize(mCBReflowInput->IsBResizeForWM(wm));
} else {
SetBResize(IsIResize());
}
SetBResize(IsBResize() || mFrame->IsSubtreeDirty());
} else {
// We have a non-'auto' block-size, i.e., a length. Set the BResize
// flag to whether the size is actually different.
SetBResize(mFrame->BSize(wm) !=
ComputedBSize() +
ComputedLogicalBorderPadding(wm).BStartEnd(wm));
}
const auto positionProperty = mStyleDisplay->mPosition;
bool dependsOnCBBSize =
(mStylePosition->BSizeDependsOnContainer(wm) &&
// FIXME: condition this on not-abspos?
!mStylePosition->BSize(wm).IsAuto()) ||
mStylePosition->MinBSizeDependsOnContainer(wm) ||
mStylePosition->MaxBSizeDependsOnContainer(wm) ||
mStylePosition
->GetAnchorResolvedInset(LogicalSide::BStart, wm, positionProperty)
->HasPercent() ||
!mStylePosition
->GetAnchorResolvedInset(LogicalSide::BEnd, wm, positionProperty)
->IsAuto();
// If mFrame is a flex item, and mFrame's block axis is the flex container's
// main axis (e.g. in a column-oriented flex container with same
// writing-mode), then its block-size depends on its CB size, if its
// flex-basis has a percentage.
if (mFrame->IsFlexItem() &&
!nsFlexContainerFrame::IsItemInlineAxisMainAxis(mFrame)) {
const auto& flexBasis = mStylePosition->mFlexBasis;
dependsOnCBBSize |= (flexBasis.IsSize() && flexBasis.AsSize().HasPercent());
}
if (mFrame->StyleFont()->mLineHeight.IsMozBlockHeight()) {
// line-height depends on block bsize
mFrame->AddStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE);
// but only on containing blocks if this frame is not a suitable block
dependsOnCBBSize |= !nsLayoutUtils::IsNonWrapperBlock(mFrame);
}
// If we're the descendant of a table cell that performs special bsize
// reflows and we could be the child that requires them, always set
// the block-axis resize in case this is the first pass before the
// special bsize reflow. However, don't do this if it actually is
// the special bsize reflow, since in that case it will already be
// set correctly above if we need it set.
if (!IsBResize() && mCBReflowInput &&
(mCBReflowInput->mFrame->IsTableCellFrame() ||
mCBReflowInput->mFlags.mHeightDependsOnAncestorCell) &&
!mCBReflowInput->mFlags.mSpecialBSizeReflow && dependsOnCBBSize) {
SetBResize(true);
mFlags.mHeightDependsOnAncestorCell = true;
}
// Set NS_FRAME_CONTAINS_RELATIVE_BSIZE if it's needed.
// It would be nice to check that |ComputedBSize != NS_UNCONSTRAINEDSIZE|
// &&ed with the percentage bsize check. However, this doesn't get
// along with table special bsize reflows, since a special bsize
// reflow (a quirk that makes such percentage height work on children
// of table cells) can cause not just a single percentage height to
// become fixed, but an entire descendant chain of percentage height
// to become fixed.
if (dependsOnCBBSize && mCBReflowInput) {
const ReflowInput* rs = this;
bool hitCBReflowInput = false;
do {
rs = rs->mParentReflowInput;
if (!rs) {
break;
}
if (rs->mFrame->HasAnyStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE)) {
break; // no need to go further
}
rs->mFrame->AddStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE);
// Keep track of whether we've hit the containing block, because
// we need to go at least that far.
if (rs == mCBReflowInput) {
hitCBReflowInput = true;
}
// XXX What about orthogonal flows? It doesn't make sense to
// keep propagating this bit across an orthogonal boundary,
// where the meaning of BSize changes. Bug 1175517.
} while (!hitCBReflowInput ||
(eCompatibility_NavQuirks == aPresContext->CompatibilityMode() &&
!IsQuirkContainingBlockHeight(rs, rs->mFrame->Type())));
// Note: We actually don't need to set the
// NS_FRAME_CONTAINS_RELATIVE_BSIZE bit for the cases
// where we hit the early break statements in
// CalcQuirkContainingBlockHeight. But it doesn't hurt
// us to set the bit in these cases.
}
if (mFrame->HasAnyStateBits(NS_FRAME_IS_DIRTY)) {
// If we're reflowing everything, then we'll find out if we need
// to re-set this.
mFrame->RemoveStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE);
}
}
void ReflowInput::InitDynamicReflowRoot() {
if (mFrame->CanBeDynamicReflowRoot()) {
mFrame->AddStateBits(NS_FRAME_DYNAMIC_REFLOW_ROOT);
} else {
mFrame->RemoveStateBits(NS_FRAME_DYNAMIC_REFLOW_ROOT);
}
}
bool ReflowInput::ShouldApplyAutomaticMinimumOnBlockAxis() const {
MOZ_ASSERT(!mFrame->HasReplacedSizing());
return mFlags.mIsBSizeSetByAspectRatio &&
!mStyleDisplay->IsScrollableOverflow() &&
mStylePosition->MinBSize(GetWritingMode()).IsAuto();
}
bool ReflowInput::IsInFragmentedContext() const {
// We consider mFrame with a prev-in-flow being in a fragmented context
// because nsColumnSetFrame can reflow its last column with an unconstrained
// available block-size.
return AvailableBSize() != NS_UNCONSTRAINEDSIZE || mFrame->GetPrevInFlow();
}
/* static */
LogicalMargin ReflowInput::ComputeRelativeOffsets(WritingMode aWM,
nsIFrame* aFrame,
const LogicalSize& aCBSize) {
// In relative positioning, anchor functions are always invalid;
// anchor-resolved insets should no longer contain any reference to anchor
// functions.
LogicalMargin offsets(aWM);
const nsStylePosition* position = aFrame->StylePosition();
const auto positionProperty = aFrame->StyleDisplay()->mPosition;
// Compute the 'inlineStart' and 'inlineEnd' values. 'inlineStart'
// moves the boxes to the end of the line, and 'inlineEnd' moves the
// boxes to the start of the line. The computed values are always:
// inlineStart=-inlineEnd
const auto inlineStart = position->GetAnchorResolvedInset(
LogicalSide::IStart, aWM, positionProperty);
const auto inlineEnd = position->GetAnchorResolvedInset(
LogicalSide::IEnd, aWM, positionProperty);
bool inlineStartIsAuto = inlineStart->IsAuto();
bool inlineEndIsAuto = inlineEnd->IsAuto();
// If neither 'inlineStart' nor 'inlineEnd' is auto, then we're
// over-constrained and we ignore one of them
if (!inlineStartIsAuto && !inlineEndIsAuto) {
inlineEndIsAuto = true;
}
if (inlineStartIsAuto) {
if (inlineEndIsAuto) {
// If both are 'auto' (their initial values), the computed values are 0
offsets.IStart(aWM) = offsets.IEnd(aWM) = 0;
} else {
// 'inlineEnd' isn't being treated as 'auto' so compute its value
offsets.IEnd(aWM) =
inlineEnd->IsAuto()
? 0
: nsLayoutUtils::ComputeCBDependentValue(
aCBSize.ISize(aWM),
ToStylePhysicalAxis(aWM.PhysicalAxis(LogicalAxis::Inline)),
positionProperty, inlineEnd);
// Computed value for 'inlineStart' is minus the value of 'inlineEnd'
offsets.IStart(aWM) = -offsets.IEnd(aWM);
}
} else {
NS_ASSERTION(inlineEndIsAuto, "unexpected specified constraint");
// 'InlineStart' isn't 'auto' so compute its value
offsets.IStart(aWM) = nsLayoutUtils::ComputeCBDependentValue(
aCBSize.ISize(aWM),
ToStylePhysicalAxis(aWM.PhysicalAxis(LogicalAxis::Inline)),
positionProperty, inlineStart);
// Computed value for 'inlineEnd' is minus the value of 'inlineStart'
offsets.IEnd(aWM) = -offsets.IStart(aWM);
}
// Compute the 'blockStart' and 'blockEnd' values. The 'blockStart'
// and 'blockEnd' properties move relatively positioned elements in
// the block progression direction. They also must be each other's
// negative
const auto blockStart = position->GetAnchorResolvedInset(
LogicalSide::BStart, aWM, positionProperty);
const auto blockEnd = position->GetAnchorResolvedInset(LogicalSide::BEnd, aWM,
positionProperty);
bool blockStartIsAuto = blockStart->IsAuto();
bool blockEndIsAuto = blockEnd->IsAuto();
// Check for percentage based values and a containing block block-size
// that depends on the content block-size. Treat them like 'auto'
if (NS_UNCONSTRAINEDSIZE == aCBSize.BSize(aWM)) {
if (blockStart->HasPercent()) {
blockStartIsAuto = true;
}
if (blockEnd->HasPercent()) {
blockEndIsAuto = true;
}
}
// If neither is 'auto', 'block-end' is ignored
if (!blockStartIsAuto && !blockEndIsAuto) {
blockEndIsAuto = true;
}
if (blockStartIsAuto) {
if (blockEndIsAuto) {
// If both are 'auto' (their initial values), the computed values are 0
offsets.BStart(aWM) = offsets.BEnd(aWM) = 0;
} else {
// 'blockEnd' isn't being treated as 'auto' so compute its value
offsets.BEnd(aWM) =
blockEnd->IsAuto()
? 0
: nsLayoutUtils::ComputeCBDependentValue(
aCBSize.BSize(aWM),
ToStylePhysicalAxis(aWM.PhysicalAxis(LogicalAxis::Block)),
positionProperty, blockEnd);
// Computed value for 'blockStart' is minus the value of 'blockEnd'
offsets.BStart(aWM) = -offsets.BEnd(aWM);
}
} else {
NS_ASSERTION(blockEndIsAuto, "unexpected specified constraint");
// 'blockStart' isn't 'auto' so compute its value