1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* rendering object for CSS display:inline objects */
9 #include "nsInlineFrame.h"
11 #include "gfxContext.h"
12 #include "mozilla/ComputedStyle.h"
13 #include "mozilla/Likely.h"
14 #include "mozilla/PresShell.h"
15 #include "mozilla/RestyleManager.h"
16 #include "mozilla/ServoStyleSet.h"
17 #include "mozilla/SVGTextFrame.h"
18 #include "mozilla/SVGUtils.h"
19 #include "nsLineLayout.h"
20 #include "nsBlockFrame.h"
21 #include "nsLayoutUtils.h"
22 #include "nsPlaceholderFrame.h"
23 #include "nsGkAtoms.h"
24 #include "nsPresContext.h"
25 #include "nsPresContextInlines.h"
26 #include "nsCSSAnonBoxes.h"
27 #include "nsDisplayList.h"
28 #include "nsStyleChangeList.h"
34 using namespace mozilla
;
35 using namespace mozilla::layout
;
37 //////////////////////////////////////////////////////////////////////
39 // Basic nsInlineFrame methods
41 nsInlineFrame
* NS_NewInlineFrame(PresShell
* aPresShell
, ComputedStyle
* aStyle
) {
42 return new (aPresShell
) nsInlineFrame(aStyle
, aPresShell
->GetPresContext());
45 NS_IMPL_FRAMEARENA_HELPERS(nsInlineFrame
)
47 NS_QUERYFRAME_HEAD(nsInlineFrame
)
48 NS_QUERYFRAME_ENTRY(nsInlineFrame
)
49 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame
)
51 #ifdef DEBUG_FRAME_DUMP
52 nsresult
nsInlineFrame::GetFrameName(nsAString
& aResult
) const {
53 return MakeFrameName(u
"Inline"_ns
, aResult
);
57 void nsInlineFrame::InvalidateFrame(uint32_t aDisplayItemKey
,
58 bool aRebuildDisplayItems
) {
59 if (SVGUtils::IsInSVGTextSubtree(this)) {
60 nsIFrame
* svgTextFrame
= nsLayoutUtils::GetClosestFrameOfType(
61 GetParent(), LayoutFrameType::SVGText
);
62 svgTextFrame
->InvalidateFrame();
65 nsContainerFrame::InvalidateFrame(aDisplayItemKey
, aRebuildDisplayItems
);
68 void nsInlineFrame::InvalidateFrameWithRect(const nsRect
& aRect
,
69 uint32_t aDisplayItemKey
,
70 bool aRebuildDisplayItems
) {
71 if (SVGUtils::IsInSVGTextSubtree(this)) {
72 nsIFrame
* svgTextFrame
= nsLayoutUtils::GetClosestFrameOfType(
73 GetParent(), LayoutFrameType::SVGText
);
74 svgTextFrame
->InvalidateFrame();
77 nsContainerFrame::InvalidateFrameWithRect(aRect
, aDisplayItemKey
,
78 aRebuildDisplayItems
);
81 static inline bool IsMarginZero(const LengthPercentageOrAuto
& aLength
) {
82 return aLength
.IsAuto() ||
83 nsLayoutUtils::IsMarginZero(aLength
.AsLengthPercentage());
87 bool nsInlineFrame::IsSelfEmpty() {
89 // I used to think inline frames worked this way, but it seems they
90 // don't. At least not in our codebase.
91 if (GetPresContext()->CompatibilityMode() == eCompatibility_FullStandards
) {
95 const nsStyleMargin
* margin
= StyleMargin();
96 const nsStyleBorder
* border
= StyleBorder();
97 const nsStylePadding
* padding
= StylePadding();
98 // Block-start and -end ignored, since they shouldn't affect things, but this
99 // doesn't really match with nsLineLayout.cpp's setting of
100 // ZeroEffectiveSpanBox, anymore, so what should this really be?
101 WritingMode wm
= GetWritingMode();
102 bool haveStart
, haveEnd
;
104 auto HaveSide
= [&](mozilla::Side aSide
) -> bool {
105 return border
->GetComputedBorderWidth(aSide
) != 0 ||
106 !nsLayoutUtils::IsPaddingZero(padding
->mPadding
.Get(aSide
)) ||
107 !IsMarginZero(margin
->mMargin
.Get(aSide
));
109 // Initially set up haveStart and haveEnd in terms of visual (LTR/TTB)
110 // coordinates; we'll exchange them later if bidi-RTL is in effect to
111 // get logical start and end flags.
112 if (wm
.IsVertical()) {
113 haveStart
= HaveSide(eSideTop
);
114 haveEnd
= HaveSide(eSideBottom
);
116 haveStart
= HaveSide(eSideLeft
);
117 haveEnd
= HaveSide(eSideRight
);
119 if (haveStart
|| haveEnd
) {
120 // We skip this block and return false for box-decoration-break:clone since
121 // in that case all the continuations will have the border/padding/margin.
122 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT
) &&
123 StyleBorder()->mBoxDecorationBreak
== StyleBoxDecorationBreak::Slice
) {
124 // When direction=rtl, we need to consider logical rather than visual
125 // start and end, so swap the flags.
126 if (wm
.IsBidiRTL()) {
127 std::swap(haveStart
, haveEnd
);
129 // For ib-split frames, ignore things we know we'll skip in GetSkipSides.
130 // XXXbz should we be doing this for non-ib-split frames too, in a more
133 // Get the first continuation eagerly, as a performance optimization, to
134 // avoid having to get it twice..
135 nsIFrame
* firstCont
= FirstContinuation();
136 return (!haveStart
|| firstCont
->FrameIsNonFirstInIBSplit()) &&
137 (!haveEnd
|| firstCont
->FrameIsNonLastInIBSplit());
144 bool nsInlineFrame::IsEmpty() {
145 if (!IsSelfEmpty()) {
149 for (nsIFrame
* kid
: mFrames
) {
150 if (!kid
->IsEmpty()) return false;
156 nsIFrame::FrameSearchResult
nsInlineFrame::PeekOffsetCharacter(
157 bool aForward
, int32_t* aOffset
, PeekOffsetCharacterOptions aOptions
) {
158 // Override the implementation in nsFrame, to skip empty inline frames
159 NS_ASSERTION(aOffset
&& *aOffset
<= 1, "aOffset out of range");
160 int32_t startOffset
= *aOffset
;
161 if (startOffset
< 0) startOffset
= 1;
162 if (aForward
== (startOffset
== 0)) {
163 // We're before the frame and moving forward, or after it and moving
164 // backwards: skip to the other side, but keep going.
165 *aOffset
= 1 - startOffset
;
170 void nsInlineFrame::DestroyFrom(nsIFrame
* aDestructRoot
,
171 PostDestroyData
& aPostDestroyData
) {
172 nsFrameList
* overflowFrames
= GetOverflowFrames();
173 if (overflowFrames
) {
174 // Fixup the parent pointers for any child frames on the OverflowList.
175 // nsIFrame::DestroyFrom depends on that to find the sticky scroll
176 // container (an ancestor).
177 overflowFrames
->ApplySetParent(this);
179 nsContainerFrame::DestroyFrom(aDestructRoot
, aPostDestroyData
);
182 void nsInlineFrame::StealFrame(nsIFrame
* aChild
) {
183 if (MaybeStealOverflowContainerFrame(aChild
)) {
187 nsInlineFrame
* parent
= this;
189 if (parent
->mFrames
.StartRemoveFrame(aChild
)) {
193 // We didn't find the child in our principal child list.
194 // Maybe it's on the overflow list?
195 nsFrameList
* frameList
= parent
->GetOverflowFrames();
196 if (frameList
&& frameList
->ContinueRemoveFrame(aChild
)) {
197 if (frameList
->IsEmpty()) {
198 parent
->DestroyOverflowList();
203 // Due to our "lazy reparenting" optimization 'aChild' might not actually
204 // be on any of our child lists, but instead in one of our next-in-flows.
205 parent
= static_cast<nsInlineFrame
*>(parent
->GetNextInFlow());
208 MOZ_ASSERT_UNREACHABLE("nsInlineFrame::StealFrame: can't find aChild");
211 void nsInlineFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
212 const nsDisplayListSet
& aLists
) {
213 BuildDisplayListForInline(aBuilder
, aLists
);
215 // The sole purpose of this is to trigger display of the selection
216 // window for Named Anchors, which don't have any children and
217 // normally don't have any size, but in Editor we use CSS to display
218 // an image to represent this "hidden" element.
219 if (!mFrames
.FirstChild()) {
220 DisplaySelectionOverlay(aBuilder
, aLists
.Content());
224 //////////////////////////////////////////////////////////////////////
228 void nsInlineFrame::AddInlineMinISize(gfxContext
* aRenderingContext
,
229 nsIFrame::InlineMinISizeData
* aData
) {
230 DoInlineMinISize(aRenderingContext
, aData
);
234 void nsInlineFrame::AddInlinePrefISize(gfxContext
* aRenderingContext
,
235 nsIFrame::InlinePrefISizeData
* aData
) {
236 DoInlinePrefISize(aRenderingContext
, aData
);
240 nsIFrame::SizeComputationResult
nsInlineFrame::ComputeSize(
241 gfxContext
* aRenderingContext
, WritingMode aWM
, const LogicalSize
& aCBSize
,
242 nscoord aAvailableISize
, const LogicalSize
& aMargin
,
243 const LogicalSize
& aBorderPadding
, const StyleSizeOverrides
& aSizeOverrides
,
244 ComputeSizeFlags aFlags
) {
245 // Inlines and text don't compute size before reflow.
246 return {LogicalSize(aWM
, NS_UNCONSTRAINEDSIZE
, NS_UNCONSTRAINEDSIZE
),
247 AspectRatioUsage::None
};
250 nsRect
nsInlineFrame::ComputeTightBounds(DrawTarget
* aDrawTarget
) const {
252 if (Style()->HasTextDecorationLines()) {
253 return InkOverflowRect();
255 return ComputeSimpleTightBounds(aDrawTarget
);
258 static void ReparentChildListStyle(nsPresContext
* aPresContext
,
259 const nsFrameList::Slice
& aFrames
,
260 nsIFrame
* aParentFrame
) {
261 RestyleManager
* restyleManager
= aPresContext
->RestyleManager();
263 for (nsFrameList::Enumerator
e(aFrames
); !e
.AtEnd(); e
.Next()) {
264 NS_ASSERTION(e
.get()->GetParent() == aParentFrame
, "Bogus parentage");
265 restyleManager
->ReparentComputedStyleForFirstLine(e
.get());
266 nsLayoutUtils::MarkDescendantsDirty(e
.get());
270 void nsInlineFrame::Reflow(nsPresContext
* aPresContext
, ReflowOutput
& aMetrics
,
271 const ReflowInput
& aReflowInput
,
272 nsReflowStatus
& aStatus
) {
274 DO_GLOBAL_REFLOW_COUNT("nsInlineFrame");
275 DISPLAY_REFLOW(aPresContext
, this, aReflowInput
, aMetrics
, aStatus
);
276 MOZ_ASSERT(aStatus
.IsEmpty(), "Caller should pass a fresh reflow status!");
278 if (nullptr == aReflowInput
.mLineLayout
) {
279 NS_ERROR("must have non-null aReflowInput.mLineLayout");
282 if (IsFrameTreeTooDeep(aReflowInput
, aMetrics
, aStatus
)) {
286 bool lazilySetParentPointer
= false;
288 // Check for an overflow list with our prev-in-flow
289 nsInlineFrame
* prevInFlow
= (nsInlineFrame
*)GetPrevInFlow();
291 AutoFrameListPtr
prevOverflowFrames(aPresContext
,
292 prevInFlow
->StealOverflowFrames());
293 if (prevOverflowFrames
) {
294 // When pushing and pulling frames we need to check for whether any
295 // views need to be reparented.
296 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames
, prevInFlow
,
299 // Check if we should do the lazilySetParentPointer optimization.
300 // Only do it in simple cases where we're being reflowed for the
301 // first time, nothing (e.g. bidi resolution) has already given
302 // us children, and there's no next-in-flow, so all our frames
303 // will be taken from prevOverflowFrames.
304 if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW
) && mFrames
.IsEmpty() &&
306 // If our child list is empty, just put the new frames into it.
307 // Note that we don't set the parent pointer for the new frames. Instead
308 // wait to do this until we actually reflow the frame. If the overflow
309 // list contains thousands of frames this is a big performance issue
311 mFrames
.SetFrames(*prevOverflowFrames
);
312 lazilySetParentPointer
= true;
314 // Insert the new frames at the beginning of the child list
315 // and set their parent pointer
316 const nsFrameList::Slice
& newFrames
=
317 mFrames
.InsertFrames(this, nullptr, *prevOverflowFrames
);
318 // If our prev in flow was under the first continuation of a first-line
319 // frame then we need to reparent the ComputedStyles to remove the
320 // the special first-line styling. In the lazilySetParentPointer case
321 // we reparent the ComputedStyles when we set their parents in
322 // nsInlineFrame::ReflowFrames and nsInlineFrame::ReflowInlineFrame.
323 if (aReflowInput
.mLineLayout
->GetInFirstLine()) {
324 ReparentChildListStyle(aPresContext
, newFrames
, this);
330 // It's also possible that we have an overflow list for ourselves
332 if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW
)) {
333 // If it's our initial reflow, then we should not have an overflow list.
334 // However, add an assertion in case we get reflowed more than once with
335 // the initial reflow reason
336 nsFrameList
* overflowFrames
= GetOverflowFrames();
337 NS_ASSERTION(!overflowFrames
|| overflowFrames
->IsEmpty(),
338 "overflow list is not empty for initial reflow");
341 if (!HasAnyStateBits(NS_FRAME_FIRST_REFLOW
)) {
342 DrainSelfOverflowListInternal(aReflowInput
.mLineLayout
->GetInFirstLine());
345 // Set our own reflow input (additional state above and beyond aReflowInput).
346 InlineReflowInput irs
;
347 irs
.mPrevFrame
= nullptr;
348 irs
.mLineContainer
= aReflowInput
.mLineLayout
->LineContainerFrame();
349 irs
.mLineLayout
= aReflowInput
.mLineLayout
;
350 irs
.mNextInFlow
= (nsInlineFrame
*)GetNextInFlow();
351 irs
.mSetParentPointer
= lazilySetParentPointer
;
353 if (mFrames
.IsEmpty()) {
354 // Try to pull over one frame before starting so that we know
355 // whether we have an anonymous block or not.
356 Unused
<< PullOneFrame(aPresContext
, irs
);
359 ReflowFrames(aPresContext
, aReflowInput
, irs
, aMetrics
, aStatus
);
361 ReflowAbsoluteFrames(aPresContext
, aMetrics
, aReflowInput
, aStatus
);
363 // Note: the line layout code will properly compute our
364 // overflow-rect state for us.
366 NS_FRAME_SET_TRUNCATION(aStatus
, aReflowInput
, aMetrics
);
369 nsresult
nsInlineFrame::AttributeChanged(int32_t aNameSpaceID
,
370 nsAtom
* aAttribute
, int32_t aModType
) {
372 nsContainerFrame::AttributeChanged(aNameSpaceID
, aAttribute
, aModType
);
378 if (SVGUtils::IsInSVGTextSubtree(this)) {
379 SVGTextFrame
* f
= static_cast<SVGTextFrame
*>(
380 nsLayoutUtils::GetClosestFrameOfType(this, LayoutFrameType::SVGText
));
381 f
->HandleAttributeChangeInDescendant(mContent
->AsElement(), aNameSpaceID
,
388 bool nsInlineFrame::DrainSelfOverflowListInternal(bool aInFirstLine
) {
389 AutoFrameListPtr
overflowFrames(PresContext(), StealOverflowFrames());
390 if (!overflowFrames
|| overflowFrames
->IsEmpty()) {
394 // The frames on our own overflowlist may have been pushed by a
395 // previous lazilySetParentPointer Reflow so we need to ensure the
396 // correct parent pointer. This is sometimes skipped by Reflow.
397 nsIFrame
* firstChild
= overflowFrames
->FirstChild();
398 RestyleManager
* restyleManager
= PresContext()->RestyleManager();
399 for (nsIFrame
* f
= firstChild
; f
; f
= f
->GetNextSibling()) {
401 if (MOZ_UNLIKELY(aInFirstLine
)) {
402 restyleManager
->ReparentComputedStyleForFirstLine(f
);
403 nsLayoutUtils::MarkDescendantsDirty(f
);
406 mFrames
.AppendFrames(nullptr, *overflowFrames
);
411 bool nsInlineFrame::DrainSelfOverflowList() {
412 nsIFrame
* lineContainer
= nsLayoutUtils::FindNearestBlockAncestor(this);
413 // Add the eInFirstLine flag if we have a ::first-line ancestor frame.
414 // No need to look further than the nearest line container though.
415 bool inFirstLine
= false;
416 for (nsIFrame
* p
= GetParent(); p
!= lineContainer
; p
= p
->GetParent()) {
417 if (p
->IsLineFrame()) {
422 return DrainSelfOverflowListInternal(inFirstLine
);
426 bool nsInlineFrame::CanContinueTextRun() const {
427 // We can continue a text run through an inline frame
432 void nsInlineFrame::PullOverflowsFromPrevInFlow() {
433 nsInlineFrame
* prevInFlow
= static_cast<nsInlineFrame
*>(GetPrevInFlow());
435 nsPresContext
* presContext
= PresContext();
436 AutoFrameListPtr
prevOverflowFrames(presContext
,
437 prevInFlow
->StealOverflowFrames());
438 if (prevOverflowFrames
) {
439 // Assume that our prev-in-flow has the same line container that we do.
440 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames
, prevInFlow
,
442 mFrames
.InsertFrames(this, nullptr, *prevOverflowFrames
);
447 void nsInlineFrame::ReflowFrames(nsPresContext
* aPresContext
,
448 const ReflowInput
& aReflowInput
,
449 InlineReflowInput
& irs
, ReflowOutput
& aMetrics
,
450 nsReflowStatus
& aStatus
) {
451 MOZ_ASSERT(aStatus
.IsEmpty(), "Caller should pass a fresh reflow status!");
453 nsLineLayout
* lineLayout
= aReflowInput
.mLineLayout
;
454 bool inFirstLine
= aReflowInput
.mLineLayout
->GetInFirstLine();
455 RestyleManager
* restyleManager
= aPresContext
->RestyleManager();
456 WritingMode frameWM
= aReflowInput
.GetWritingMode();
457 WritingMode lineWM
= aReflowInput
.mLineLayout
->mRootSpan
->mWritingMode
;
458 LogicalMargin framePadding
=
459 aReflowInput
.ComputedLogicalBorderPadding(frameWM
);
460 nscoord startEdge
= 0;
461 const bool boxDecorationBreakClone
= MOZ_UNLIKELY(
462 StyleBorder()->mBoxDecorationBreak
== StyleBoxDecorationBreak::Clone
);
463 // Don't offset by our start borderpadding if we have a prev continuation or
464 // if we're in a part of an {ib} split other than the first one. For
465 // box-decoration-break:clone we always offset our start since all
466 // continuations have border/padding.
467 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
468 boxDecorationBreakClone
) {
469 startEdge
= framePadding
.IStart(frameWM
);
471 nscoord availableISize
= aReflowInput
.AvailableISize();
472 NS_ASSERTION(availableISize
!= NS_UNCONSTRAINEDSIZE
,
473 "should no longer use available widths");
474 // Subtract off inline axis border+padding from availableISize
475 availableISize
-= startEdge
;
476 availableISize
-= framePadding
.IEnd(frameWM
);
477 lineLayout
->BeginSpan(this, &aReflowInput
, startEdge
,
478 startEdge
+ availableISize
, &mBaseline
);
480 // First reflow our principal children.
481 nsIFrame
* frame
= mFrames
.FirstChild();
484 // Check if we should lazily set the child frame's parent pointer.
485 if (irs
.mSetParentPointer
) {
486 nsIFrame
* child
= frame
;
488 child
->SetParent(this);
490 restyleManager
->ReparentComputedStyleForFirstLine(child
);
491 nsLayoutUtils::MarkDescendantsDirty(child
);
493 // We also need to do the same for |frame|'s next-in-flows that are in
494 // the sibling list. Otherwise, if we reflow |frame| and it's complete
495 // we'll crash when trying to delete its next-in-flow.
496 // This scenario doesn't happen often, but it can happen.
497 nsIFrame
* nextSibling
= child
->GetNextSibling();
498 child
= child
->GetNextInFlow();
499 if (MOZ_UNLIKELY(child
)) {
500 while (child
!= nextSibling
&& nextSibling
) {
501 nextSibling
= nextSibling
->GetNextSibling();
507 MOZ_ASSERT(!child
|| mFrames
.ContainsFrame(child
));
510 // Fix the parent pointer for ::first-letter child frame next-in-flows,
511 // so nsFirstLetterFrame::Reflow can destroy them safely (bug 401042).
512 nsIFrame
* realFrame
= nsPlaceholderFrame::GetRealFrameFor(frame
);
513 if (realFrame
->IsLetterFrame()) {
514 nsIFrame
* child
= realFrame
->PrincipalChildList().FirstChild();
516 NS_ASSERTION(child
->IsTextFrame(), "unexpected frame type");
517 nsIFrame
* nextInFlow
= child
->GetNextInFlow();
518 for (; nextInFlow
; nextInFlow
= nextInFlow
->GetNextInFlow()) {
519 NS_ASSERTION(nextInFlow
->IsTextFrame(), "unexpected frame type");
520 if (mFrames
.ContainsFrame(nextInFlow
)) {
521 nextInFlow
->SetParent(this);
523 restyleManager
->ReparentComputedStyleForFirstLine(nextInFlow
);
524 nsLayoutUtils::MarkDescendantsDirty(nextInFlow
);
528 // Once we find a next-in-flow that isn't ours none of the
529 // remaining next-in-flows should be either.
530 for (; nextInFlow
; nextInFlow
= nextInFlow
->GetNextInFlow()) {
531 NS_ASSERTION(!mFrames
.ContainsFrame(nextInFlow
),
532 "unexpected letter frame flow");
541 MOZ_ASSERT(frame
->GetParent() == this);
544 bool reflowingFirstLetter
= lineLayout
->GetFirstLetterStyleOK();
545 ReflowInlineFrame(aPresContext
, aReflowInput
, irs
, frame
, aStatus
);
546 done
= aStatus
.IsInlineBreak() ||
547 (!reflowingFirstLetter
&& aStatus
.IsIncomplete());
549 if (!irs
.mSetParentPointer
) {
552 // Keep reparenting the remaining siblings, but don't reflow them.
553 nsFrameList
* pushedFrames
= GetOverflowFrames();
554 if (pushedFrames
&& pushedFrames
->FirstChild() == frame
) {
555 // Don't bother if |frame| was pushed to our overflow list.
559 irs
.mPrevFrame
= frame
;
562 frame
= frame
->GetNextSibling();
565 // Attempt to pull frames from our next-in-flow until we can't
566 if (!done
&& GetNextInFlow()) {
568 bool reflowingFirstLetter
= lineLayout
->GetFirstLetterStyleOK();
569 if (!frame
) { // Could be non-null if we pulled a first-letter frame and
570 // it created a continuation, since we don't push those.
571 frame
= PullOneFrame(aPresContext
, irs
);
574 printf("%p pulled up %p\n", this, frame
);
579 ReflowInlineFrame(aPresContext
, aReflowInput
, irs
, frame
, aStatus
);
580 if (aStatus
.IsInlineBreak() ||
581 (!reflowingFirstLetter
&& aStatus
.IsIncomplete())) {
584 irs
.mPrevFrame
= frame
;
585 frame
= frame
->GetNextSibling();
589 NS_ASSERTION(!aStatus
.IsComplete() || !GetOverflowFrames(),
590 "We can't be complete AND have overflow frames!");
592 // If after reflowing our children they take up no area then make
593 // sure that we don't either.
595 // Note: CSS demands that empty inline elements still affect the
596 // line-height calculations. However, continuations of an inline
597 // that are empty we force to empty so that things like collapsed
598 // whitespace in an inline element don't affect the line-height.
599 aMetrics
.ISize(lineWM
) = lineLayout
->EndSpan(this);
601 // Compute final width.
603 // XXX Note that that the padding start and end are in the frame's
604 // writing mode, but the metrics' inline-size is in the line's
605 // writing mode. This makes sense if the line and frame are both
606 // vertical or both horizontal, but what should happen with
607 // orthogonal inlines?
609 // Make sure to not include our start border and padding if we have a prev
610 // continuation or if we're in a part of an {ib} split other than the first
611 // one. For box-decoration-break:clone we always include our start border
612 // and padding since all continuations have them.
613 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
614 boxDecorationBreakClone
) {
615 aMetrics
.ISize(lineWM
) += framePadding
.IStart(frameWM
);
619 * We want to only apply the end border and padding if we're the last
620 * continuation and either not in an {ib} split or the last part of it. To
621 * be the last continuation we have to be complete (so that we won't get a
622 * next-in-flow) and have no non-fluid continuations on our continuation
623 * chain. For box-decoration-break:clone we always apply the end border and
624 * padding since all continuations have them.
626 if ((aStatus
.IsComplete() && !LastInFlow()->GetNextContinuation() &&
627 !FrameIsNonLastInIBSplit()) ||
628 boxDecorationBreakClone
) {
629 aMetrics
.ISize(lineWM
) += framePadding
.IEnd(frameWM
);
632 nsLayoutUtils::SetBSizeFromFontMetrics(this, aMetrics
, framePadding
, lineWM
,
635 // For now our overflow area is zero. The real value will be
636 // computed in |nsLineLayout::RelativePositionFrames|.
637 aMetrics
.mOverflowAreas
.Clear();
639 #ifdef NOISY_FINAL_SIZE
641 printf(": metrics=%d,%d ascent=%d\n", aMetrics
.Width(), aMetrics
.Height(),
642 aMetrics
.TopAscent());
646 // Returns whether there's any remaining frame to pull.
648 bool nsInlineFrame::HasFramesToPull(nsInlineFrame
* aNextInFlow
) {
649 while (aNextInFlow
) {
650 if (!aNextInFlow
->mFrames
.IsEmpty()) {
653 if (const nsFrameList
* overflow
= aNextInFlow
->GetOverflowFrames()) {
654 if (!overflow
->IsEmpty()) {
658 aNextInFlow
= static_cast<nsInlineFrame
*>(aNextInFlow
->GetNextInFlow());
663 void nsInlineFrame::ReflowInlineFrame(nsPresContext
* aPresContext
,
664 const ReflowInput
& aReflowInput
,
665 InlineReflowInput
& irs
, nsIFrame
* aFrame
,
666 nsReflowStatus
& aStatus
) {
667 nsLineLayout
* lineLayout
= aReflowInput
.mLineLayout
;
668 bool reflowingFirstLetter
= lineLayout
->GetFirstLetterStyleOK();
671 lineLayout
->ReflowFrame(aFrame
, aStatus
, nullptr, pushedFrame
);
673 if (aStatus
.IsInlineBreakBefore()) {
674 if (aFrame
!= mFrames
.FirstChild()) {
675 // Change break-before status into break-after since we have
676 // already placed at least one child frame. This preserves the
677 // break-type so that it can be propagated upward.
678 StyleClear oldBreakType
= aStatus
.BreakType();
680 aStatus
.SetIncomplete();
681 aStatus
.SetInlineLineBreakAfter(oldBreakType
);
682 PushFrames(aPresContext
, aFrame
, irs
.mPrevFrame
, irs
);
684 // Preserve reflow status when breaking-before our first child
685 // and propagate it upward without modification.
690 // Create a next-in-flow if needed.
691 if (!aStatus
.IsFullyComplete()) {
692 CreateNextInFlow(aFrame
);
695 if (aStatus
.IsInlineBreakAfter()) {
696 nsIFrame
* nextFrame
= aFrame
->GetNextSibling();
698 aStatus
.SetIncomplete();
699 PushFrames(aPresContext
, nextFrame
, aFrame
, irs
);
701 // We must return an incomplete status if there are more child
702 // frames remaining in a next-in-flow that follows this frame.
703 if (HasFramesToPull(static_cast<nsInlineFrame
*>(GetNextInFlow()))) {
704 aStatus
.SetIncomplete();
710 if (!aStatus
.IsFullyComplete() && !reflowingFirstLetter
) {
711 nsIFrame
* nextFrame
= aFrame
->GetNextSibling();
713 PushFrames(aPresContext
, nextFrame
, aFrame
, irs
);
718 nsIFrame
* nsInlineFrame::PullOneFrame(nsPresContext
* aPresContext
,
719 InlineReflowInput
& irs
) {
720 nsIFrame
* frame
= nullptr;
721 nsInlineFrame
* nextInFlow
= irs
.mNextInFlow
;
724 bool willPull
= HasFramesToPull(nextInFlow
);
728 frame
= nextInFlow
->mFrames
.FirstChild();
730 // The nextInFlow's principal list has no frames, try its overflow list.
731 nsFrameList
* overflowFrames
= nextInFlow
->GetOverflowFrames();
732 if (overflowFrames
) {
733 frame
= overflowFrames
->RemoveFirstChild();
734 if (overflowFrames
->IsEmpty()) {
735 // We're stealing the only frame - delete the overflow list.
736 nextInFlow
->DestroyOverflowList();
738 // We leave the remaining frames on the overflow list (rather than
739 // putting them on nextInFlow's principal list) so we don't have to
740 // set up the parent for them.
742 // ReparentFloatsForInlineChild needs it to be on a child list -
743 // we remove it again below.
744 nextInFlow
->mFrames
.SetFrames(frame
);
749 // If our block has no next continuation, then any floats belonging to
750 // the pulled frame must belong to our block already. This check ensures
751 // we do no extra work in the common non-vertical-breaking case.
752 if (irs
.mLineContainer
&& irs
.mLineContainer
->GetNextContinuation()) {
753 // The blockChildren.ContainsFrame check performed by
754 // ReparentFloatsForInlineChild will be fast because frame's ancestor
755 // will be the first child of its containing block.
756 ReparentFloatsForInlineChild(irs
.mLineContainer
, frame
, false);
758 nextInFlow
->mFrames
.RemoveFirstChild();
759 // nsFirstLineFrame::PullOneFrame calls ReparentComputedStyle.
761 mFrames
.InsertFrame(this, irs
.mPrevFrame
, frame
);
762 if (irs
.mLineLayout
) {
763 irs
.mLineLayout
->SetDirtyNextLine();
765 nsContainerFrame::ReparentFrameView(frame
, nextInFlow
, this);
768 nextInFlow
= static_cast<nsInlineFrame
*>(nextInFlow
->GetNextInFlow());
769 irs
.mNextInFlow
= nextInFlow
;
772 MOZ_ASSERT(!!frame
== willPull
);
776 void nsInlineFrame::PushFrames(nsPresContext
* aPresContext
,
777 nsIFrame
* aFromChild
, nsIFrame
* aPrevSibling
,
778 InlineReflowInput
& aState
) {
780 printf("%p pushing aFromChild %p, disconnecting from prev sib %p\n", this,
781 aFromChild
, aPrevSibling
);
784 PushChildrenToOverflow(aFromChild
, aPrevSibling
);
785 if (aState
.mLineLayout
) {
786 aState
.mLineLayout
->SetDirtyNextLine();
790 //////////////////////////////////////////////////////////////////////
792 LogicalSides
nsInlineFrame::GetLogicalSkipSides() const {
793 LogicalSides
skip(mWritingMode
);
794 if (MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak
==
795 StyleBoxDecorationBreak::Clone
)) {
800 nsInlineFrame
* prev
= (nsInlineFrame
*)GetPrevContinuation();
801 if (HasAnyStateBits(NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET
) ||
802 (prev
&& (prev
->mRect
.height
|| prev
->mRect
.width
))) {
803 // Prev continuation is not empty therefore we don't render our start
805 skip
|= eLogicalSideBitsIStart
;
807 // If the prev continuation is empty, then go ahead and let our start
808 // edge border render.
812 nsInlineFrame
* next
= (nsInlineFrame
*)GetNextContinuation();
813 if (HasAnyStateBits(NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET
) ||
814 (next
&& (next
->mRect
.height
|| next
->mRect
.width
))) {
815 // Next continuation is not empty therefore we don't render our end
817 skip
|= eLogicalSideBitsIEnd
;
819 // If the next continuation is empty, then go ahead and let our end
820 // edge border render.
824 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT
)) {
825 // All but the last part of an {ib} split should skip the "end" side (as
826 // determined by this frame's direction) and all but the first part of such
827 // a split should skip the "start" side. But figuring out which part of
828 // the split we are involves getting our first continuation, which might be
829 // expensive. So don't bother if we already have the relevant bits set.
830 if (skip
!= LogicalSides(mWritingMode
, eLogicalSideBitsIBoth
)) {
831 // We're missing one of the skip bits, so check whether we need to set it.
832 // Only get the first continuation once, as an optimization.
833 nsIFrame
* firstContinuation
= FirstContinuation();
834 if (firstContinuation
->FrameIsNonLastInIBSplit()) {
835 skip
|= eLogicalSideBitsIEnd
;
837 if (firstContinuation
->FrameIsNonFirstInIBSplit()) {
838 skip
|= eLogicalSideBitsIStart
;
846 nscoord
nsInlineFrame::GetLogicalBaseline(
847 mozilla::WritingMode aWritingMode
) const {
852 a11y::AccType
nsInlineFrame::AccessibleType() {
853 // FIXME(emilio): This is broken, if the image has its default `display` value
854 // overridden. Should be somewhere else.
855 if (mContent
->IsHTMLElement(
856 nsGkAtoms::img
)) // Create accessible for broken <img>
857 return a11y::eHyperTextType
;
859 return a11y::eNoType
;
863 void nsInlineFrame::UpdateStyleOfOwnedAnonBoxesForIBSplit(
864 ServoRestyleState
& aRestyleState
) {
865 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_OWNS_ANON_BOXES
),
866 "Why did we get called?");
867 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT
),
868 "Why did we have the NS_FRAME_OWNS_ANON_BOXES bit set?");
869 // Note: this assert _looks_ expensive, but it's cheap in all the cases when
871 MOZ_ASSERT(nsLayoutUtils::FirstContinuationOrIBSplitSibling(this) == this,
872 "Only the primary frame of the inline in a block-inside-inline "
873 "split should have NS_FRAME_OWNS_ANON_BOXES");
874 MOZ_ASSERT(mContent
->GetPrimaryFrame() == this,
875 "We should be the primary frame for our element");
877 nsIFrame
* blockFrame
= GetProperty(nsIFrame::IBSplitSibling());
878 MOZ_ASSERT(blockFrame
, "Why did we have an IB split?");
880 // The later inlines need to get our style.
881 ComputedStyle
* ourStyle
= Style();
883 // The anonymous block's style inherits from ours, and we already have our new
885 RefPtr
<ComputedStyle
> newContext
=
886 aRestyleState
.StyleSet().ResolveInheritingAnonymousBoxStyle(
887 PseudoStyleType::mozBlockInsideInlineWrapper
, ourStyle
);
889 // We're guaranteed that newContext only differs from the old ComputedStyle on
890 // the block in things they might inherit from us. And changehint processing
891 // guarantees walking the continuation and ib-sibling chains, so our existing
892 // changehint being in aChangeList is good enough. So we don't need to touch
893 // aChangeList at all here.
896 MOZ_ASSERT(!blockFrame
->GetPrevContinuation(),
897 "Must be first continuation");
899 MOZ_ASSERT(blockFrame
->Style()->GetPseudoType() ==
900 PseudoStyleType::mozBlockInsideInlineWrapper
,
901 "Unexpected kind of ComputedStyle");
903 for (nsIFrame
* cont
= blockFrame
; cont
;
904 cont
= cont
->GetNextContinuation()) {
905 cont
->SetComputedStyle(newContext
);
908 nsIFrame
* nextInline
= blockFrame
->GetProperty(nsIFrame::IBSplitSibling());
910 // This check is here due to bug 1431232. Please remove it once
911 // that bug is fixed.
912 if (MOZ_UNLIKELY(!nextInline
)) {
916 MOZ_ASSERT(nextInline
, "There is always a trailing inline in an IB split");
918 for (nsIFrame
* cont
= nextInline
; cont
;
919 cont
= cont
->GetNextContinuation()) {
920 cont
->SetComputedStyle(ourStyle
);
922 blockFrame
= nextInline
->GetProperty(nsIFrame::IBSplitSibling());
926 //////////////////////////////////////////////////////////////////////
928 // nsLineFrame implementation
930 nsFirstLineFrame
* NS_NewFirstLineFrame(PresShell
* aPresShell
,
931 ComputedStyle
* aStyle
) {
932 return new (aPresShell
)
933 nsFirstLineFrame(aStyle
, aPresShell
->GetPresContext());
936 NS_IMPL_FRAMEARENA_HELPERS(nsFirstLineFrame
)
938 void nsFirstLineFrame::Init(nsIContent
* aContent
, nsContainerFrame
* aParent
,
939 nsIFrame
* aPrevInFlow
) {
940 nsInlineFrame::Init(aContent
, aParent
, aPrevInFlow
);
942 MOZ_ASSERT(Style()->GetPseudoType() == PseudoStyleType::firstLine
);
946 // This frame is a continuation - fixup the computed style if aPrevInFlow
947 // is the first-in-flow (the only one with a ::first-line pseudo).
948 if (aPrevInFlow
->Style()->GetPseudoType() == PseudoStyleType::firstLine
) {
949 MOZ_ASSERT(FirstInFlow() == aPrevInFlow
);
950 // Create a new ComputedStyle that is a child of the parent
951 // ComputedStyle thus removing the ::first-line style. This way
952 // we behave as if an anonymous (unstyled) span was the child
953 // of the parent frame.
954 ComputedStyle
* parentContext
= aParent
->Style();
955 RefPtr
<ComputedStyle
> newSC
=
956 PresContext()->StyleSet()->ResolveInheritingAnonymousBoxStyle(
957 PseudoStyleType::mozLineFrame
, parentContext
);
958 SetComputedStyle(newSC
);
960 MOZ_ASSERT(FirstInFlow() != aPrevInFlow
);
961 MOZ_ASSERT(aPrevInFlow
->Style()->GetPseudoType() ==
962 PseudoStyleType::mozLineFrame
);
966 #ifdef DEBUG_FRAME_DUMP
967 nsresult
nsFirstLineFrame::GetFrameName(nsAString
& aResult
) const {
968 return MakeFrameName(u
"Line"_ns
, aResult
);
972 nsIFrame
* nsFirstLineFrame::PullOneFrame(nsPresContext
* aPresContext
,
973 InlineReflowInput
& irs
) {
974 nsIFrame
* frame
= nsInlineFrame::PullOneFrame(aPresContext
, irs
);
975 if (frame
&& !GetPrevInFlow()) {
976 // We are a first-line frame. Fixup the child frames
977 // style-context that we just pulled.
978 NS_ASSERTION(frame
->GetParent() == this, "Incorrect parent?");
979 aPresContext
->RestyleManager()->ReparentComputedStyleForFirstLine(frame
);
980 nsLayoutUtils::MarkDescendantsDirty(frame
);
985 void nsFirstLineFrame::Reflow(nsPresContext
* aPresContext
,
986 ReflowOutput
& aMetrics
,
987 const ReflowInput
& aReflowInput
,
988 nsReflowStatus
& aStatus
) {
990 MOZ_ASSERT(aStatus
.IsEmpty(), "Caller should pass a fresh reflow status!");
992 if (nullptr == aReflowInput
.mLineLayout
) {
993 return; // XXX does this happen? why?
996 // Check for an overflow list with our prev-in-flow
997 nsFirstLineFrame
* prevInFlow
= (nsFirstLineFrame
*)GetPrevInFlow();
999 AutoFrameListPtr
prevOverflowFrames(aPresContext
,
1000 prevInFlow
->StealOverflowFrames());
1001 if (prevOverflowFrames
) {
1002 // Reparent the new frames and their ComputedStyles.
1003 const nsFrameList::Slice
& newFrames
=
1004 mFrames
.InsertFrames(this, nullptr, *prevOverflowFrames
);
1005 ReparentChildListStyle(aPresContext
, newFrames
, this);
1009 // It's also possible that we have an overflow list for ourselves.
1010 DrainSelfOverflowList();
1012 // Set our own reflow input (additional state above and beyond aReflowInput).
1013 InlineReflowInput irs
;
1014 irs
.mPrevFrame
= nullptr;
1015 irs
.mLineContainer
= aReflowInput
.mLineLayout
->LineContainerFrame();
1016 irs
.mLineLayout
= aReflowInput
.mLineLayout
;
1017 irs
.mNextInFlow
= (nsInlineFrame
*)GetNextInFlow();
1019 bool wasEmpty
= mFrames
.IsEmpty();
1021 // Try to pull over one frame before starting so that we know
1022 // whether we have an anonymous block or not.
1023 PullOneFrame(aPresContext
, irs
);
1026 if (nullptr == GetPrevInFlow()) {
1027 // XXX This is pretty sick, but what we do here is to pull-up, in
1028 // advance, all of the next-in-flows children. We re-resolve their
1029 // style while we are at at it so that when we reflow they have
1032 // All of this is so that text-runs reflow properly.
1033 irs
.mPrevFrame
= mFrames
.LastChild();
1035 nsIFrame
* frame
= PullOneFrame(aPresContext
, irs
);
1039 irs
.mPrevFrame
= frame
;
1041 irs
.mPrevFrame
= nullptr;
1044 NS_ASSERTION(!aReflowInput
.mLineLayout
->GetInFirstLine(),
1045 "Nested first-line frames? BOGUS");
1046 aReflowInput
.mLineLayout
->SetInFirstLine(true);
1047 ReflowFrames(aPresContext
, aReflowInput
, irs
, aMetrics
, aStatus
);
1048 aReflowInput
.mLineLayout
->SetInFirstLine(false);
1050 ReflowAbsoluteFrames(aPresContext
, aMetrics
, aReflowInput
, aStatus
);
1052 // Note: the line layout code will properly compute our overflow state for us
1056 void nsFirstLineFrame::PullOverflowsFromPrevInFlow() {
1057 nsFirstLineFrame
* prevInFlow
=
1058 static_cast<nsFirstLineFrame
*>(GetPrevInFlow());
1060 nsPresContext
* presContext
= PresContext();
1061 AutoFrameListPtr
prevOverflowFrames(presContext
,
1062 prevInFlow
->StealOverflowFrames());
1063 if (prevOverflowFrames
) {
1064 // Assume that our prev-in-flow has the same line container that we do.
1065 const nsFrameList::Slice
& newFrames
=
1066 mFrames
.InsertFrames(this, nullptr, *prevOverflowFrames
);
1067 ReparentChildListStyle(presContext
, newFrames
, this);
1073 bool nsFirstLineFrame::DrainSelfOverflowList() {
1074 AutoFrameListPtr
overflowFrames(PresContext(), StealOverflowFrames());
1075 if (overflowFrames
) {
1076 bool result
= !overflowFrames
->IsEmpty();
1077 const nsFrameList::Slice
& newFrames
=
1078 mFrames
.AppendFrames(nullptr, *overflowFrames
);
1079 ReparentChildListStyle(PresContext(), newFrames
, this);