forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsHTMLReflowState.cpp
2611 lines (2357 loc) · 103 KB
/
nsHTMLReflowState.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/. */
/* struct containing the input to nsIFrame::Reflow */
#include "nsCOMPtr.h"
#include "nsStyleConsts.h"
#include "nsCSSAnonBoxes.h"
#include "nsFrame.h"
#include "nsIContent.h"
#include "nsGkAtoms.h"
#include "nsPresContext.h"
#include "nsIPresShell.h"
#include "nsFontMetrics.h"
#include "nsBlockFrame.h"
#include "nsLineBox.h"
#include "nsFlexContainerFrame.h"
#include "nsImageFrame.h"
#include "nsTableFrame.h"
#include "nsTableCellFrame.h"
#include "nsIServiceManager.h"
#include "nsIPercentHeightObserver.h"
#include "nsLayoutUtils.h"
#include "mozilla/Preferences.h"
#include "nsBidiUtils.h"
#include "nsFontInflationData.h"
#ifdef DEBUG
#undef NOISY_VERTICAL_ALIGN
#else
#undef NOISY_VERTICAL_ALIGN
#endif
using namespace mozilla;
using namespace mozilla::layout;
// Prefs-driven control for |text-decoration: blink|
static bool sPrefIsLoaded = false;
static bool sBlinkIsAllowed = true;
enum eNormalLineHeightControl {
eUninitialized = -1,
eNoExternalLeading = 0, // does not include external leading
eIncludeExternalLeading, // use whatever value font vendor provides
eCompensateLeading // compensate leading if leading provided by font vendor is not enough
};
static eNormalLineHeightControl sNormalLineHeightControl = eUninitialized;
// Initialize a <b>root</b> reflow state with a rendering context to
// use for measuring things.
nsHTMLReflowState::nsHTMLReflowState(nsPresContext* aPresContext,
nsIFrame* aFrame,
nsRenderingContext* aRenderingContext,
const nsSize& aAvailableSpace,
uint32_t aFlags)
: nsCSSOffsetState(aFrame, aRenderingContext)
, mBlockDelta(0)
, mReflowDepth(0)
{
NS_PRECONDITION(aPresContext, "no pres context");
NS_PRECONDITION(aRenderingContext, "no rendering context");
NS_PRECONDITION(aFrame, "no frame");
parentReflowState = nullptr;
availableWidth = aAvailableSpace.width;
availableHeight = aAvailableSpace.height;
mFloatManager = nullptr;
mLineLayout = nullptr;
memset(&mFlags, 0, sizeof(mFlags));
mDiscoveredClearance = nullptr;
mPercentHeightObserver = nullptr;
if (aFlags & DUMMY_PARENT_REFLOW_STATE) {
mFlags.mDummyParentReflowState = true;
}
Init(aPresContext);
}
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)
{
float inflation = nsLayoutUtils::FontSizeInflationFor(aFrame);
if (aFrame->IsFrameOfType(nsIFrame::eBlockFrame)) {
const nsBlockFrame* blockFrame = static_cast<const nsBlockFrame*>(aFrame);
const nsStyleList* styleList = aFrame->GetStyleList();
// We only want to adjust the margins if we're dealing with an ordered
// list.
if (inflation > 1.0f &&
blockFrame->HasBullet() &&
styleList->mListStyleType != NS_STYLE_LIST_STYLE_NONE &&
styleList->mListStyleType != NS_STYLE_LIST_STYLE_DISC &&
styleList->mListStyleType != NS_STYLE_LIST_STYLE_CIRCLE &&
styleList->mListStyleType != NS_STYLE_LIST_STYLE_SQUARE &&
inflation > 1.0f) {
// 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.
return nsPresContext::CSSPixelsToAppUnits(40) * (inflation - 1);
}
}
return 0;
}
// Initialize a reflow state for a child frames reflow. Some state
// is copied from the parent reflow state; the remaining state is
// computed.
nsHTMLReflowState::nsHTMLReflowState(nsPresContext* aPresContext,
const nsHTMLReflowState& aParentReflowState,
nsIFrame* aFrame,
const nsSize& aAvailableSpace,
nscoord aContainingBlockWidth,
nscoord aContainingBlockHeight,
bool aInit)
: nsCSSOffsetState(aFrame, aParentReflowState.rendContext)
, mBlockDelta(0)
, mReflowDepth(aParentReflowState.mReflowDepth + 1)
, mFlags(aParentReflowState.mFlags)
{
NS_PRECONDITION(aPresContext, "no pres context");
NS_PRECONDITION(aFrame, "no frame");
NS_PRECONDITION((aContainingBlockWidth == -1) ==
(aContainingBlockHeight == -1),
"cb width and height should only be non-default together");
NS_PRECONDITION(!mFlags.mSpecialHeightReflow ||
!NS_SUBTREE_DIRTY(aFrame),
"frame should be clean when getting special height reflow");
parentReflowState = &aParentReflowState;
// If the parent is dirty, then the child is as well.
// XXX Are the other cases where the parent reflows a child a second
// time, as a resize?
if (!mFlags.mSpecialHeightReflow)
frame->AddStateBits(parentReflowState->frame->GetStateBits() &
NS_FRAME_IS_DIRTY);
availableWidth = aAvailableSpace.width;
availableHeight = aAvailableSpace.height;
mFloatManager = aParentReflowState.mFloatManager;
if (frame->IsFrameOfType(nsIFrame::eLineParticipant))
mLineLayout = aParentReflowState.mLineLayout;
else
mLineLayout = nullptr;
// Note: mFlags was initialized as a copy of aParentReflowState.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 = aParentReflowState.mFlags.mNextInFlowUntouched &&
CheckNextInFlowParenthood(aFrame, aParentReflowState.frame);
mFlags.mAssumingHScrollbar = mFlags.mAssumingVScrollbar = false;
mFlags.mHasClearance = false;
mFlags.mIsColumnBalancing = false;
mFlags.mIsFlexContainerMeasuringHeight = false;
mFlags.mDummyParentReflowState = false;
mDiscoveredClearance = nullptr;
mPercentHeightObserver = (aParentReflowState.mPercentHeightObserver &&
aParentReflowState.mPercentHeightObserver->NeedsToObserve(*this))
? aParentReflowState.mPercentHeightObserver : nullptr;
if (aInit) {
Init(aPresContext, aContainingBlockWidth, aContainingBlockHeight);
}
}
inline nscoord
nsCSSOffsetState::ComputeWidthValue(nscoord aContainingBlockWidth,
nscoord aContentEdgeToBoxSizing,
nscoord aBoxSizingToMarginEdge,
const nsStyleCoord& aCoord)
{
return nsLayoutUtils::ComputeWidthValue(rendContext, frame,
aContainingBlockWidth,
aContentEdgeToBoxSizing,
aBoxSizingToMarginEdge,
aCoord);
}
nscoord
nsCSSOffsetState::ComputeWidthValue(nscoord aContainingBlockWidth,
uint8_t aBoxSizing,
const nsStyleCoord& aCoord)
{
nscoord inside = 0, outside = mComputedBorderPadding.LeftRight() +
mComputedMargin.LeftRight();
switch (aBoxSizing) {
case NS_STYLE_BOX_SIZING_BORDER:
inside = mComputedBorderPadding.LeftRight();
break;
case NS_STYLE_BOX_SIZING_PADDING:
inside = mComputedPadding.LeftRight();
break;
}
outside -= inside;
return ComputeWidthValue(aContainingBlockWidth, inside,
outside, aCoord);
}
nscoord
nsCSSOffsetState::ComputeHeightValue(nscoord aContainingBlockHeight,
uint8_t aBoxSizing,
const nsStyleCoord& aCoord)
{
nscoord inside = 0;
switch (aBoxSizing) {
case NS_STYLE_BOX_SIZING_BORDER:
inside = mComputedBorderPadding.TopBottom();
break;
case NS_STYLE_BOX_SIZING_PADDING:
inside = mComputedPadding.TopBottom();
break;
}
return nsLayoutUtils::ComputeHeightValue(aContainingBlockHeight,
inside, aCoord);
}
void
nsHTMLReflowState::SetComputedWidth(nscoord aComputedWidth)
{
NS_ASSERTION(frame, "Must have a frame!");
// It'd be nice to assert that |frame| is not in reflow, but this fails for
// two reasons:
//
// 1) Viewport frames reset the computed width on a copy of their reflow
// state 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 width is pointless.
// 2) nsFrame::BoxReflow creates a reflow state for its parent. This reflow
// state is not used to reflow the parent, but just as a parent for the
// frame's own reflow state. So given a nsBoxFrame inside some non-XUL
// (like a text control, for example), we'll end up creating a reflow
// state for the parent while the parent is reflowing.
NS_PRECONDITION(aComputedWidth >= 0, "Invalid computed width");
if (mComputedWidth != aComputedWidth) {
mComputedWidth = aComputedWidth;
nsIAtom* frameType = frame->GetType();
if (frameType != nsGkAtoms::viewportFrame) { // Or check GetParent()?
InitResizeFlags(frame->PresContext(), frameType);
}
}
}
void
nsHTMLReflowState::SetComputedHeight(nscoord aComputedHeight)
{
NS_ASSERTION(frame, "Must have a frame!");
// It'd be nice to assert that |frame| is not in reflow, but this fails
// because:
//
// nsFrame::BoxReflow creates a reflow state for its parent. This reflow
// state is not used to reflow the parent, but just as a parent for the
// frame's own reflow state. So given a nsBoxFrame inside some non-XUL
// (like a text control, for example), we'll end up creating a reflow
// state for the parent while the parent is reflowing.
NS_PRECONDITION(aComputedHeight >= 0, "Invalid computed height");
if (mComputedHeight != aComputedHeight) {
mComputedHeight = aComputedHeight;
InitResizeFlags(frame->PresContext(), frame->GetType());
}
}
void
nsHTMLReflowState::Init(nsPresContext* aPresContext,
nscoord aContainingBlockWidth,
nscoord aContainingBlockHeight,
const nsMargin* aBorder,
const nsMargin* aPadding)
{
NS_WARN_IF_FALSE(availableWidth != NS_UNCONSTRAINEDSIZE,
"have unconstrained width; this should only result from "
"very large sizes, not attempts at intrinsic width "
"calculation");
mStylePosition = frame->GetStylePosition();
mStyleDisplay = frame->GetStyleDisplay();
mStyleVisibility = frame->GetStyleVisibility();
mStyleBorder = frame->GetStyleBorder();
mStyleMargin = frame->GetStyleMargin();
mStylePadding = frame->GetStylePadding();
mStyleText = frame->GetStyleText();
nsIAtom* type = frame->GetType();
InitFrameType(type);
InitCBReflowState();
InitConstraints(aPresContext, aContainingBlockWidth, aContainingBlockHeight,
aBorder, aPadding, type);
InitResizeFlags(aPresContext, type);
nsIFrame *parent = frame->GetParent();
if (parent &&
(parent->GetStateBits() & NS_FRAME_IN_CONSTRAINED_HEIGHT) &&
!(parent->GetType() == nsGkAtoms::scrollFrame &&
parent->GetStyleDisplay()->mOverflowY != NS_STYLE_OVERFLOW_HIDDEN)) {
frame->AddStateBits(NS_FRAME_IN_CONSTRAINED_HEIGHT);
} else if (type == nsGkAtoms::svgForeignObjectFrame) {
// An SVG foreignObject frame is inherently constrained height.
frame->AddStateBits(NS_FRAME_IN_CONSTRAINED_HEIGHT);
} else if ((mStylePosition->mHeight.GetUnit() != eStyleUnit_Auto ||
mStylePosition->mMaxHeight.GetUnit() != eStyleUnit_None) &&
// Don't set NS_FRAME_IN_CONSTRAINED_HEIGHT on body or html
// elements.
(frame->GetContent() &&
!(frame->GetContent()->IsHTML(nsGkAtoms::body) ||
frame->GetContent()->IsHTML(nsGkAtoms::html)))) {
// If our height 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 = frame;
while (containingBlk) {
const nsStylePosition* stylePos = containingBlk->GetStylePosition();
if ((stylePos->mHeight.IsCoordPercentCalcUnit() &&
!stylePos->mHeight.HasPercent()) ||
(stylePos->mMaxHeight.IsCoordPercentCalcUnit() &&
!stylePos->mMaxHeight.HasPercent())) {
frame->AddStateBits(NS_FRAME_IN_CONSTRAINED_HEIGHT);
break;
} else if ((stylePos->mHeight.IsCoordPercentCalcUnit() &&
stylePos->mHeight.HasPercent()) ||
(stylePos->mMaxHeight.IsCoordPercentCalcUnit() &&
stylePos->mMaxHeight.HasPercent())) {
if (!(containingBlk = containingBlk->GetContainingBlock())) {
// If we've reached the top of the tree, then we don't have
// a constrained height.
frame->RemoveStateBits(NS_FRAME_IN_CONSTRAINED_HEIGHT);
break;
}
continue;
} else {
frame->RemoveStateBits(NS_FRAME_IN_CONSTRAINED_HEIGHT);
break;
}
}
} else {
frame->RemoveStateBits(NS_FRAME_IN_CONSTRAINED_HEIGHT);
}
NS_WARN_IF_FALSE((mFrameType == NS_CSS_FRAME_TYPE_INLINE &&
!frame->IsFrameOfType(nsIFrame::eReplaced)) ||
type == nsGkAtoms::textFrame ||
mComputedWidth != NS_UNCONSTRAINEDSIZE,
"have unconstrained width; this should only result from "
"very large sizes, not attempts at intrinsic width "
"calculation");
}
void nsHTMLReflowState::InitCBReflowState()
{
if (!parentReflowState) {
mCBReflowState = nullptr;
return;
}
if (parentReflowState->frame == frame->GetContainingBlock()) {
// Inner table frames need to use the containing block of the outer
// table frame.
if (frame->GetType() == nsGkAtoms::tableFrame) {
mCBReflowState = parentReflowState->mCBReflowState;
} else {
mCBReflowState = parentReflowState;
}
} else {
mCBReflowState = parentReflowState->mCBReflowState;
}
}
/* Check whether CalcQuirkContainingBlockHeight would stop on the
* given reflow state, 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 nsHTMLReflowState* rs, nsIAtom* aFrameType)
{
if (nsGkAtoms::blockFrame == aFrameType ||
#ifdef MOZ_XUL
nsGkAtoms::XULLabelFrame == aFrameType ||
#endif
nsGkAtoms::scrollFrame == 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_AUTOHEIGHT == rs->ComputedHeight()) {
if (!rs->frame->IsAbsolutelyPositioned()) {
return false;
}
}
}
return true;
}
void
nsHTMLReflowState::InitResizeFlags(nsPresContext* aPresContext, nsIAtom* aFrameType)
{
bool isHResize = frame->GetSize().width !=
mComputedWidth + mComputedBorderPadding.LeftRight();
if ((frame->GetStateBits() & 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::UpdateFontInflationDataWidthFor(*this) &&
// Avoid running this at the box-to-block interface
// (where we shouldn't be inflating anyway, and where
// reflow state construction is probably to construct a
// dummy parent reflow state anyway).
!mFlags.mDummyParentReflowState;
if (dirty || (!frame->GetParent() && isHResize)) {
// 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
// nsHTMLReflowState::SetComputedWidth is called, if the first time
// we go through InitResizeFlags we set mHResize to true, and then
// the second time we'd set it to false even without the
// NS_FRAME_IS_DIRTY bit already set.
if (frame->GetType() == nsGkAtoms::svgForeignObjectFrame) {
// Foreign object frames use dirty bits in a special way.
frame->AddStateBits(NS_FRAME_HAS_DIRTY_CHILDREN);
nsIFrame *kid = frame->GetFirstPrincipalChild();
if (kid) {
kid->AddStateBits(NS_FRAME_IS_DIRTY);
}
} else {
frame->AddStateBits(NS_FRAME_IS_DIRTY);
}
// 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.
nsAutoTArray<nsIFrame*, 32> stack;
stack.AppendElement(frame);
do {
nsIFrame *f = stack.ElementAt(stack.Length() - 1);
stack.RemoveElementAt(stack.Length() - 1);
nsIFrame::ChildListIterator lists(f);
for (; !lists.IsDone(); lists.Next()) {
nsFrameList::Enumerator childFrames(lists.CurrentList());
for (; !childFrames.AtEnd(); childFrames.Next()) {
nsIFrame* kid = childFrames.get();
kid->MarkIntrinsicWidthsDirty();
stack.AppendElement(kid);
}
}
} while (stack.Length() != 0);
}
}
mFlags.mHResize = !(frame->GetStateBits() & NS_FRAME_IS_DIRTY) &&
isHResize;
// XXX Should we really need to null check mCBReflowState? (We do for
// at least nsBoxFrame).
if (IS_TABLE_CELL(aFrameType) &&
(mFlags.mSpecialHeightReflow ||
(frame->GetFirstInFlow()->GetStateBits() &
NS_TABLE_CELL_HAD_SPECIAL_REFLOW)) &&
(frame->GetStateBits() & NS_FRAME_CONTAINS_RELATIVE_HEIGHT)) {
// Need to set the bit on the cell so that
// mCBReflowState->mFlags.mVResize is set correctly below when
// reflowing descendant.
mFlags.mVResize = true;
} else if (mCBReflowState && !nsLayoutUtils::IsNonWrapperBlock(frame)) {
// 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
// NS_SUBTREE_DIRTY, I'd think.
mFlags.mVResize = mCBReflowState->mFlags.mVResize;
} else if (mComputedHeight == NS_AUTOHEIGHT) {
if (eCompatibility_NavQuirks == aPresContext->CompatibilityMode() &&
mCBReflowState) {
mFlags.mVResize = mCBReflowState->mFlags.mVResize;
} else {
mFlags.mVResize = mFlags.mHResize;
}
mFlags.mVResize = mFlags.mVResize || NS_SUBTREE_DIRTY(frame);
} else {
// not 'auto' height
mFlags.mVResize = frame->GetSize().height !=
mComputedHeight + mComputedBorderPadding.TopBottom();
}
bool dependsOnCBHeight =
(mStylePosition->HeightDependsOnContainer() &&
// FIXME: condition this on not-abspos?
mStylePosition->mHeight.GetUnit() != eStyleUnit_Auto) ||
mStylePosition->MinHeightDependsOnContainer() ||
mStylePosition->MaxHeightDependsOnContainer() ||
mStylePosition->OffsetHasPercent(NS_SIDE_TOP) ||
mStylePosition->mOffset.GetBottomUnit() != eStyleUnit_Auto ||
frame->IsBoxFrame();
if (mStyleText->mLineHeight.GetUnit() == eStyleUnit_Enumerated) {
NS_ASSERTION(mStyleText->mLineHeight.GetIntValue() ==
NS_STYLE_LINE_HEIGHT_BLOCK_HEIGHT,
"bad line-height value");
// line-height depends on block height
frame->AddStateBits(NS_FRAME_CONTAINS_RELATIVE_HEIGHT);
// but only on containing blocks if this frame is not a suitable block
dependsOnCBHeight |= !nsLayoutUtils::IsNonWrapperBlock(frame);
}
// If we're the descendant of a table cell that performs special height
// reflows and we could be the child that requires them, always set
// the vertical resize in case this is the first pass before the
// special height reflow. However, don't do this if it actually is
// the special height reflow, since in that case it will already be
// set correctly above if we need it set.
if (!mFlags.mVResize && mCBReflowState &&
(IS_TABLE_CELL(mCBReflowState->frame->GetType()) ||
mCBReflowState->mFlags.mHeightDependsOnAncestorCell) &&
!mCBReflowState->mFlags.mSpecialHeightReflow &&
dependsOnCBHeight) {
mFlags.mVResize = true;
mFlags.mHeightDependsOnAncestorCell = true;
}
// Set NS_FRAME_CONTAINS_RELATIVE_HEIGHT if it's needed.
// It would be nice to check that |mComputedHeight != NS_AUTOHEIGHT|
// &&ed with the percentage height check. However, this doesn't get
// along with table special height reflows, since a special height
// reflow (a quirk that makes such percentage heights work on children
// of table cells) can cause not just a single percentage height to
// become fixed, but an entire descendant chain of percentage heights
// to become fixed.
if (dependsOnCBHeight && mCBReflowState) {
const nsHTMLReflowState *rs = this;
bool hitCBReflowState = false;
do {
rs = rs->parentReflowState;
if (!rs) {
break;
}
if (rs->frame->GetStateBits() & NS_FRAME_CONTAINS_RELATIVE_HEIGHT)
break; // no need to go further
rs->frame->AddStateBits(NS_FRAME_CONTAINS_RELATIVE_HEIGHT);
// Keep track of whether we've hit the containing block, because
// we need to go at least that far.
if (rs == mCBReflowState) {
hitCBReflowState = true;
}
} while (!hitCBReflowState ||
(eCompatibility_NavQuirks == aPresContext->CompatibilityMode() &&
!IsQuirkContainingBlockHeight(rs, rs->frame->GetType())));
// Note: We actually don't need to set the
// NS_FRAME_CONTAINS_RELATIVE_HEIGHT 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 (frame->GetStateBits() & NS_FRAME_IS_DIRTY) {
// If we're reflowing everything, then we'll find out if we need
// to re-set this.
frame->RemoveStateBits(NS_FRAME_CONTAINS_RELATIVE_HEIGHT);
}
}
/* static */
nscoord
nsHTMLReflowState::GetContainingBlockContentWidth(const nsHTMLReflowState* aReflowState)
{
const nsHTMLReflowState* rs = aReflowState->mCBReflowState;
if (!rs)
return 0;
return rs->mComputedWidth;
}
void
nsHTMLReflowState::InitFrameType(nsIAtom* aFrameType)
{
const nsStyleDisplay *disp = mStyleDisplay;
nsCSSFrameType frameType;
// Section 9.7 of the CSS2 spec indicates that absolute position
// takes precedence over float which takes precedence over display.
// XXXldb nsRuleNode::ComputeDisplayData should take care of this, right?
// Make sure the frame was actually moved out of the flow, and don't
// just assume what the style says, because we might not have had a
// useful float/absolute containing block
DISPLAY_INIT_TYPE(frame, this);
if (aFrameType == nsGkAtoms::tableFrame) {
mFrameType = NS_CSS_FRAME_TYPE_BLOCK;
return;
}
NS_ASSERTION(frame->GetStyleDisplay()->IsAbsolutelyPositionedStyle() ==
disp->IsAbsolutelyPositionedStyle(),
"Unexpected position style");
NS_ASSERTION(frame->GetStyleDisplay()->IsFloatingStyle() ==
disp->IsFloatingStyle(), "Unexpected float style");
if (frame->GetStateBits() & NS_FRAME_OUT_OF_FLOW) {
if (disp->IsAbsolutelyPositioned(frame)) {
frameType = NS_CSS_FRAME_TYPE_ABSOLUTE;
//XXXfr hack for making frames behave properly when in overflow container lists
// see bug 154892; need to revisit later
if (frame->GetPrevInFlow())
frameType = NS_CSS_FRAME_TYPE_BLOCK;
}
else if (disp->IsFloating(frame)) {
frameType = NS_CSS_FRAME_TYPE_FLOATING;
} else {
NS_ASSERTION(disp->mDisplay == NS_STYLE_DISPLAY_POPUP,
"unknown out of flow frame type");
frameType = NS_CSS_FRAME_TYPE_UNKNOWN;
}
}
else {
switch (GetDisplay()) {
case NS_STYLE_DISPLAY_BLOCK:
case NS_STYLE_DISPLAY_LIST_ITEM:
case NS_STYLE_DISPLAY_TABLE:
case NS_STYLE_DISPLAY_TABLE_CAPTION:
#ifdef MOZ_FLEXBOX
case NS_STYLE_DISPLAY_FLEX:
#endif // MOZ_FLEXBOX
frameType = NS_CSS_FRAME_TYPE_BLOCK;
break;
case NS_STYLE_DISPLAY_INLINE:
case NS_STYLE_DISPLAY_INLINE_BLOCK:
case NS_STYLE_DISPLAY_INLINE_TABLE:
case NS_STYLE_DISPLAY_INLINE_BOX:
case NS_STYLE_DISPLAY_INLINE_GRID:
case NS_STYLE_DISPLAY_INLINE_STACK:
#ifdef MOZ_FLEXBOX
case NS_STYLE_DISPLAY_INLINE_FLEX:
#endif // MOZ_FLEXBOX
frameType = NS_CSS_FRAME_TYPE_INLINE;
break;
case NS_STYLE_DISPLAY_TABLE_CELL:
case NS_STYLE_DISPLAY_TABLE_ROW_GROUP:
case NS_STYLE_DISPLAY_TABLE_COLUMN:
case NS_STYLE_DISPLAY_TABLE_COLUMN_GROUP:
case NS_STYLE_DISPLAY_TABLE_HEADER_GROUP:
case NS_STYLE_DISPLAY_TABLE_FOOTER_GROUP:
case NS_STYLE_DISPLAY_TABLE_ROW:
frameType = NS_CSS_FRAME_TYPE_INTERNAL_TABLE;
break;
case NS_STYLE_DISPLAY_NONE:
default:
frameType = NS_CSS_FRAME_TYPE_UNKNOWN;
break;
}
}
// See if the frame is replaced
if (frame->IsFrameOfType(nsIFrame::eReplacedContainsBlock)) {
frameType = NS_FRAME_REPLACED_CONTAINS_BLOCK(frameType);
} else if (frame->IsFrameOfType(nsIFrame::eReplaced)) {
frameType = NS_FRAME_REPLACED(frameType);
}
mFrameType = frameType;
}
/* static */ void
nsHTMLReflowState::ComputeRelativeOffsets(uint8_t aCBDirection,
nsIFrame* aFrame,
nscoord aContainingBlockWidth,
nscoord aContainingBlockHeight,
nsMargin& aComputedOffsets)
{
const nsStylePosition* position = aFrame->GetStylePosition();
// Compute the 'left' and 'right' values. 'Left' moves the boxes to the right,
// and 'right' moves the boxes to the left. The computed values are always:
// left=-right
bool leftIsAuto = eStyleUnit_Auto == position->mOffset.GetLeftUnit();
bool rightIsAuto = eStyleUnit_Auto == position->mOffset.GetRightUnit();
// If neither 'left' not 'right' are auto, then we're over-constrained and
// we ignore one of them
if (!leftIsAuto && !rightIsAuto) {
if (aCBDirection == NS_STYLE_DIRECTION_RTL) {
leftIsAuto = true;
} else {
rightIsAuto = true;
}
}
if (leftIsAuto) {
if (rightIsAuto) {
// If both are 'auto' (their initial values), the computed values are 0
aComputedOffsets.left = aComputedOffsets.right = 0;
} else {
// 'Right' isn't 'auto' so compute its value
aComputedOffsets.right = nsLayoutUtils::
ComputeWidthDependentValue(aContainingBlockWidth,
position->mOffset.GetRight());
// Computed value for 'left' is minus the value of 'right'
aComputedOffsets.left = -aComputedOffsets.right;
}
} else {
NS_ASSERTION(rightIsAuto, "unexpected specified constraint");
// 'Left' isn't 'auto' so compute its value
aComputedOffsets.left = nsLayoutUtils::
ComputeWidthDependentValue(aContainingBlockWidth,
position->mOffset.GetLeft());
// Computed value for 'right' is minus the value of 'left'
aComputedOffsets.right = -aComputedOffsets.left;
}
// Compute the 'top' and 'bottom' values. The 'top' and 'bottom' properties
// move relatively positioned elements up and down. They also must be each
// other's negative
bool topIsAuto = eStyleUnit_Auto == position->mOffset.GetTopUnit();
bool bottomIsAuto = eStyleUnit_Auto == position->mOffset.GetBottomUnit();
// Check for percentage based values and a containing block height that
// depends on the content height. Treat them like 'auto'
if (NS_AUTOHEIGHT == aContainingBlockHeight) {
if (position->OffsetHasPercent(NS_SIDE_TOP)) {
topIsAuto = true;
}
if (position->OffsetHasPercent(NS_SIDE_BOTTOM)) {
bottomIsAuto = true;
}
}
// If neither is 'auto', 'bottom' is ignored
if (!topIsAuto && !bottomIsAuto) {
bottomIsAuto = true;
}
if (topIsAuto) {
if (bottomIsAuto) {
// If both are 'auto' (their initial values), the computed values are 0
aComputedOffsets.top = aComputedOffsets.bottom = 0;
} else {
// 'Bottom' isn't 'auto' so compute its value
aComputedOffsets.bottom = nsLayoutUtils::
ComputeHeightDependentValue(aContainingBlockHeight,
position->mOffset.GetBottom());
// Computed value for 'top' is minus the value of 'bottom'
aComputedOffsets.top = -aComputedOffsets.bottom;
}
} else {
NS_ASSERTION(bottomIsAuto, "unexpected specified constraint");
// 'Top' isn't 'auto' so compute its value
aComputedOffsets.top = nsLayoutUtils::
ComputeHeightDependentValue(aContainingBlockHeight,
position->mOffset.GetTop());
// Computed value for 'bottom' is minus the value of 'top'
aComputedOffsets.bottom = -aComputedOffsets.top;
}
// Store the offset
FrameProperties props = aFrame->Properties();
nsPoint* offsets = static_cast<nsPoint*>
(props.Get(nsIFrame::ComputedOffsetProperty()));
if (offsets) {
offsets->MoveTo(aComputedOffsets.left, aComputedOffsets.top);
} else {
props.Set(nsIFrame::ComputedOffsetProperty(),
new nsPoint(aComputedOffsets.left, aComputedOffsets.top));
}
}
nsIFrame*
nsHTMLReflowState::GetHypotheticalBoxContainer(nsIFrame* aFrame,
nscoord& aCBLeftEdge,
nscoord& aCBWidth)
{
aFrame = aFrame->GetContainingBlock();
NS_ASSERTION(aFrame != frame, "How did that happen?");
/* Now aFrame is the containing block we want */
/* Check whether the containing block is currently being reflowed.
If so, use the info from the reflow state. */
const nsHTMLReflowState* state;
if (aFrame->GetStateBits() & NS_FRAME_IN_REFLOW) {
for (state = parentReflowState; state && state->frame != aFrame;
state = state->parentReflowState) {
/* do nothing */
}
} else {
state = nullptr;
}
if (state) {
aCBLeftEdge = state->mComputedBorderPadding.left;
aCBWidth = state->mComputedWidth;
} else {
/* Didn't find a reflow state for aFrame. Just compute the information we
want, on the assumption that aFrame already knows its size. This really
ought to be true by now. */
NS_ASSERTION(!(aFrame->GetStateBits() & NS_FRAME_IN_REFLOW),
"aFrame shouldn't be in reflow; we'll lie if it is");
nsMargin borderPadding = aFrame->GetUsedBorderAndPadding();
aCBLeftEdge = borderPadding.left;
aCBWidth = aFrame->GetSize().width - borderPadding.LeftRight();
}
return aFrame;
}
// When determining the hypothetical box that would have been if the element
// had been in the flow we may not be able to exactly determine both the left
// and right edges. For example, if the element is a non-replaced inline-level
// element we would have to reflow it in order to determine it desired width.
// In that case depending on the progression direction either the left or
// right edge would be marked as not being exact
struct nsHypotheticalBox {
// offsets from left edge of containing block (which is a padding edge)
nscoord mLeft, mRight;
// offset from top edge of containing block (which is a padding edge)
nscoord mTop;
#ifdef DEBUG
bool mLeftIsExact, mRightIsExact;
#endif
nsHypotheticalBox() {
#ifdef DEBUG
mLeftIsExact = mRightIsExact = false;
#endif
}
};
static bool
GetIntrinsicSizeFor(nsIFrame* aFrame, nsSize& aIntrinsicSize, nsIAtom* aFrameType)
{
// See if it is an image frame
bool success = false;
// Currently the only type of replaced frame that we can get the intrinsic
// size for is an image frame
// XXX We should add back the GetReflowMetrics() function and one of the
// things should be the intrinsic size...
if (aFrameType == nsGkAtoms::imageFrame) {
nsImageFrame* imageFrame = (nsImageFrame*)aFrame;
if (NS_SUCCEEDED(imageFrame->GetIntrinsicImageSize(aIntrinsicSize))) {
success = (aIntrinsicSize != nsSize(0, 0));
}
}
return success;
}
/**
* aInsideBoxSizing returns the part of the horizontal padding, border,
* and margin that goes inside the edge given by -moz-box-sizing;
* aOutsideBoxSizing returns the rest.
*/
void
nsHTMLReflowState::CalculateHorizBorderPaddingMargin(
nscoord aContainingBlockWidth,
nscoord* aInsideBoxSizing,
nscoord* aOutsideBoxSizing)
{
const nsMargin& border = mStyleBorder->GetComputedBorder();
nsMargin padding, margin;
// See if the style system can provide us the padding directly
if (!mStylePadding->GetPadding(padding)) {
// We have to compute the left and right values
padding.left = nsLayoutUtils::
ComputeWidthDependentValue(aContainingBlockWidth,
mStylePadding->mPadding.GetLeft());
padding.right = nsLayoutUtils::
ComputeWidthDependentValue(aContainingBlockWidth,
mStylePadding->mPadding.GetRight());
}
// See if the style system can provide us the margin directly
if (!mStyleMargin->GetMargin(margin)) {
// We have to compute the left and right values
if (eStyleUnit_Auto == mStyleMargin->mMargin.GetLeftUnit()) {
// XXX FIXME (or does CalculateBlockSideMargins do this?)
margin.left = 0; // just ignore
} else {
margin.left = nsLayoutUtils::
ComputeWidthDependentValue(aContainingBlockWidth,
mStyleMargin->mMargin.GetLeft());
}
if (eStyleUnit_Auto == mStyleMargin->mMargin.GetRightUnit()) {
// XXX FIXME (or does CalculateBlockSideMargins do this?)
margin.right = 0; // just ignore
} else {
margin.right = nsLayoutUtils::
ComputeWidthDependentValue(aContainingBlockWidth,
mStyleMargin->mMargin.GetRight());
}
}
nscoord outside =
padding.LeftRight() + border.LeftRight() + margin.LeftRight();
nscoord inside = 0;
switch (mStylePosition->mBoxSizing) {
case NS_STYLE_BOX_SIZING_BORDER:
inside += border.LeftRight();
// fall through
case NS_STYLE_BOX_SIZING_PADDING:
inside += padding.LeftRight();
}
outside -= inside;
*aInsideBoxSizing = inside;
*aOutsideBoxSizing = outside;
return;
}
/**
* Returns true iff a pre-order traversal of the normal child
* frames rooted at aFrame finds no non-empty frame before aDescendant.
*/
static bool AreAllEarlierInFlowFramesEmpty(nsIFrame* aFrame,
nsIFrame* aDescendant, bool* aFound) {
if (aFrame == aDescendant) {
*aFound = true;
return true;
}
if (!aFrame->IsSelfEmpty()) {