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 "nsLineLayout.h"
19 #include "nsBlockFrame.h"
20 #include "nsLayoutUtils.h"
21 #include "nsPlaceholderFrame.h"
22 #include "nsGkAtoms.h"
23 #include "nsPresContext.h"
24 #include "nsPresContextInlines.h"
25 #include "nsCSSAnonBoxes.h"
26 #include "nsDisplayList.h"
27 #include "nsStyleChangeList.h"
33 using namespace mozilla
;
34 using namespace mozilla::layout
;
36 //////////////////////////////////////////////////////////////////////
38 // Basic nsInlineFrame methods
40 nsInlineFrame
* NS_NewInlineFrame(PresShell
* aPresShell
, ComputedStyle
* aStyle
) {
41 return new (aPresShell
) nsInlineFrame(aStyle
, aPresShell
->GetPresContext());
44 NS_IMPL_FRAMEARENA_HELPERS(nsInlineFrame
)
46 NS_QUERYFRAME_HEAD(nsInlineFrame
)
47 NS_QUERYFRAME_ENTRY(nsInlineFrame
)
48 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame
)
50 #ifdef DEBUG_FRAME_DUMP
51 nsresult
nsInlineFrame::GetFrameName(nsAString
& aResult
) const {
52 return MakeFrameName(u
"Inline"_ns
, aResult
);
56 void nsInlineFrame::InvalidateFrame(uint32_t aDisplayItemKey
,
57 bool aRebuildDisplayItems
) {
58 if (IsInSVGTextSubtree()) {
59 nsIFrame
* svgTextFrame
= nsLayoutUtils::GetClosestFrameOfType(
60 GetParent(), LayoutFrameType::SVGText
);
61 svgTextFrame
->InvalidateFrame();
64 nsContainerFrame::InvalidateFrame(aDisplayItemKey
, aRebuildDisplayItems
);
67 void nsInlineFrame::InvalidateFrameWithRect(const nsRect
& aRect
,
68 uint32_t aDisplayItemKey
,
69 bool aRebuildDisplayItems
) {
70 if (IsInSVGTextSubtree()) {
71 nsIFrame
* svgTextFrame
= nsLayoutUtils::GetClosestFrameOfType(
72 GetParent(), LayoutFrameType::SVGText
);
73 svgTextFrame
->InvalidateFrame();
76 nsContainerFrame::InvalidateFrameWithRect(aRect
, aDisplayItemKey
,
77 aRebuildDisplayItems
);
80 static inline bool IsMarginZero(const LengthPercentageOrAuto
& aLength
) {
81 return aLength
.IsAuto() ||
82 nsLayoutUtils::IsMarginZero(aLength
.AsLengthPercentage());
86 bool nsInlineFrame::IsSelfEmpty() {
88 // I used to think inline frames worked this way, but it seems they
89 // don't. At least not in our codebase.
90 if (GetPresContext()->CompatibilityMode() == eCompatibility_FullStandards
) {
94 const nsStyleMargin
* margin
= StyleMargin();
95 const nsStyleBorder
* border
= StyleBorder();
96 const nsStylePadding
* padding
= StylePadding();
97 // Block-start and -end ignored, since they shouldn't affect things, but this
98 // doesn't really match with nsLineLayout.cpp's setting of
99 // ZeroEffectiveSpanBox, anymore, so what should this really be?
100 WritingMode wm
= GetWritingMode();
101 bool haveStart
, haveEnd
;
103 auto HaveSide
= [&](mozilla::Side aSide
) -> bool {
104 return border
->GetComputedBorderWidth(aSide
) != 0 ||
105 !nsLayoutUtils::IsPaddingZero(padding
->mPadding
.Get(aSide
)) ||
106 !IsMarginZero(margin
->mMargin
.Get(aSide
));
108 // Initially set up haveStart and haveEnd in terms of visual (LTR/TTB)
109 // coordinates; we'll exchange them later if bidi-RTL is in effect to
110 // get logical start and end flags.
111 if (wm
.IsVertical()) {
112 haveStart
= HaveSide(eSideTop
);
113 haveEnd
= HaveSide(eSideBottom
);
115 haveStart
= HaveSide(eSideLeft
);
116 haveEnd
= HaveSide(eSideRight
);
118 if (haveStart
|| haveEnd
) {
119 // We skip this block and return false for box-decoration-break:clone since
120 // in that case all the continuations will have the border/padding/margin.
121 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT
) &&
122 StyleBorder()->mBoxDecorationBreak
== StyleBoxDecorationBreak::Slice
) {
123 // When direction=rtl, we need to consider logical rather than visual
124 // start and end, so swap the flags.
125 if (wm
.IsBidiRTL()) {
126 std::swap(haveStart
, haveEnd
);
128 // For ib-split frames, ignore things we know we'll skip in GetSkipSides.
129 // XXXbz should we be doing this for non-ib-split frames too, in a more
132 // Get the first continuation eagerly, as a performance optimization, to
133 // avoid having to get it twice..
134 nsIFrame
* firstCont
= FirstContinuation();
135 return (!haveStart
|| firstCont
->FrameIsNonFirstInIBSplit()) &&
136 (!haveEnd
|| firstCont
->FrameIsNonLastInIBSplit());
143 bool nsInlineFrame::IsEmpty() {
144 if (!IsSelfEmpty()) {
148 for (nsIFrame
* kid
: mFrames
) {
149 if (!kid
->IsEmpty()) return false;
155 nsIFrame::FrameSearchResult
nsInlineFrame::PeekOffsetCharacter(
156 bool aForward
, int32_t* aOffset
, PeekOffsetCharacterOptions aOptions
) {
157 // Override the implementation in nsFrame, to skip empty inline frames
158 NS_ASSERTION(aOffset
&& *aOffset
<= 1, "aOffset out of range");
159 int32_t startOffset
= *aOffset
;
160 if (startOffset
< 0) startOffset
= 1;
161 if (aForward
== (startOffset
== 0)) {
162 // We're before the frame and moving forward, or after it and moving
163 // backwards: skip to the other side, but keep going.
164 *aOffset
= 1 - startOffset
;
169 void nsInlineFrame::Destroy(DestroyContext
& aContext
) {
170 nsFrameList
* overflowFrames
= GetOverflowFrames();
171 if (overflowFrames
) {
172 // Fixup the parent pointers for any child frames on the OverflowList.
173 // nsIFrame::DestroyFrom depends on that to find the sticky scroll
174 // container (an ancestor).
175 overflowFrames
->ApplySetParent(this);
177 nsContainerFrame::Destroy(aContext
);
180 void nsInlineFrame::StealFrame(nsIFrame
* aChild
) {
181 if (MaybeStealOverflowContainerFrame(aChild
)) {
185 nsInlineFrame
* parent
= this;
187 if (parent
->mFrames
.StartRemoveFrame(aChild
)) {
191 // We didn't find the child in our principal child list.
192 // Maybe it's on the overflow list?
193 nsFrameList
* frameList
= parent
->GetOverflowFrames();
194 if (frameList
&& frameList
->ContinueRemoveFrame(aChild
)) {
195 if (frameList
->IsEmpty()) {
196 parent
->DestroyOverflowList();
201 // Due to our "lazy reparenting" optimization 'aChild' might not actually
202 // be on any of our child lists, but instead in one of our next-in-flows.
203 parent
= static_cast<nsInlineFrame
*>(parent
->GetNextInFlow());
206 MOZ_ASSERT_UNREACHABLE("nsInlineFrame::StealFrame: can't find aChild");
209 void nsInlineFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
210 const nsDisplayListSet
& aLists
) {
211 BuildDisplayListForInline(aBuilder
, aLists
);
213 // The sole purpose of this is to trigger display of the selection
214 // window for Named Anchors, which don't have any children and
215 // normally don't have any size, but in Editor we use CSS to display
216 // an image to represent this "hidden" element.
217 if (!mFrames
.FirstChild()) {
218 DisplaySelectionOverlay(aBuilder
, aLists
.Content());
222 //////////////////////////////////////////////////////////////////////
226 void nsInlineFrame::AddInlineMinISize(gfxContext
* aRenderingContext
,
227 nsIFrame::InlineMinISizeData
* aData
) {
228 DoInlineMinISize(aRenderingContext
, aData
);
232 void nsInlineFrame::AddInlinePrefISize(gfxContext
* aRenderingContext
,
233 nsIFrame::InlinePrefISizeData
* aData
) {
234 DoInlinePrefISize(aRenderingContext
, aData
);
238 nsIFrame::SizeComputationResult
nsInlineFrame::ComputeSize(
239 gfxContext
* aRenderingContext
, WritingMode aWM
, const LogicalSize
& aCBSize
,
240 nscoord aAvailableISize
, const LogicalSize
& aMargin
,
241 const LogicalSize
& aBorderPadding
, const StyleSizeOverrides
& aSizeOverrides
,
242 ComputeSizeFlags aFlags
) {
243 // Inlines and text don't compute size before reflow.
244 return {LogicalSize(aWM
, NS_UNCONSTRAINEDSIZE
, NS_UNCONSTRAINEDSIZE
),
245 AspectRatioUsage::None
};
248 nsRect
nsInlineFrame::ComputeTightBounds(DrawTarget
* aDrawTarget
) const {
250 if (Style()->HasTextDecorationLines()) {
251 return InkOverflowRect();
253 return ComputeSimpleTightBounds(aDrawTarget
);
256 static void ReparentChildListStyle(nsPresContext
* aPresContext
,
257 const nsFrameList::Slice
& aFrames
,
258 nsIFrame
* aParentFrame
) {
259 RestyleManager
* restyleManager
= aPresContext
->RestyleManager();
261 for (nsIFrame
* f
: aFrames
) {
262 NS_ASSERTION(f
->GetParent() == aParentFrame
, "Bogus parentage");
263 restyleManager
->ReparentComputedStyleForFirstLine(f
);
264 nsLayoutUtils::MarkDescendantsDirty(f
);
268 void nsInlineFrame::Reflow(nsPresContext
* aPresContext
, ReflowOutput
& aMetrics
,
269 const ReflowInput
& aReflowInput
,
270 nsReflowStatus
& aStatus
) {
272 DO_GLOBAL_REFLOW_COUNT("nsInlineFrame");
273 MOZ_ASSERT(aStatus
.IsEmpty(), "Caller should pass a fresh reflow status!");
275 if (!aReflowInput
.mLineLayout
) {
276 NS_ERROR("must have non-null aReflowInput.mLineLayout");
279 if (IsFrameTreeTooDeep(aReflowInput
, aMetrics
, aStatus
)) {
283 bool lazilySetParentPointer
= false;
285 // Check for an overflow list with our prev-in-flow
286 nsInlineFrame
* prevInFlow
= (nsInlineFrame
*)GetPrevInFlow();
288 AutoFrameListPtr
prevOverflowFrames(aPresContext
,
289 prevInFlow
->StealOverflowFrames());
290 if (prevOverflowFrames
) {
291 // When pushing and pulling frames we need to check for whether any
292 // views need to be reparented.
293 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames
, prevInFlow
,
296 // Check if we should do the lazilySetParentPointer optimization.
297 // Only do it in simple cases where we're being reflowed for the
298 // first time, nothing (e.g. bidi resolution) has already given
299 // us children, and there's no next-in-flow, so all our frames
300 // will be taken from prevOverflowFrames.
301 if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW
) && mFrames
.IsEmpty() &&
303 // If our child list is empty, just put the new frames into it.
304 // Note that we don't set the parent pointer for the new frames. Instead
305 // wait to do this until we actually reflow the frame. If the overflow
306 // list contains thousands of frames this is a big performance issue
308 mFrames
= std::move(*prevOverflowFrames
);
309 lazilySetParentPointer
= true;
311 // Insert the new frames at the beginning of the child list
312 // and set their parent pointer
313 const nsFrameList::Slice
& newFrames
=
314 mFrames
.InsertFrames(this, nullptr, std::move(*prevOverflowFrames
));
315 // If our prev in flow was under the first continuation of a first-line
316 // frame then we need to reparent the ComputedStyles to remove the
317 // the special first-line styling. In the lazilySetParentPointer case
318 // we reparent the ComputedStyles when we set their parents in
319 // nsInlineFrame::ReflowFrames and nsInlineFrame::ReflowInlineFrame.
320 if (aReflowInput
.mLineLayout
->GetInFirstLine()) {
321 ReparentChildListStyle(aPresContext
, newFrames
, this);
327 // It's also possible that we have an overflow list for ourselves
329 if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW
)) {
330 // If it's our initial reflow, then we should not have an overflow list.
331 // However, add an assertion in case we get reflowed more than once with
332 // the initial reflow reason
333 nsFrameList
* overflowFrames
= GetOverflowFrames();
334 NS_ASSERTION(!overflowFrames
|| overflowFrames
->IsEmpty(),
335 "overflow list is not empty for initial reflow");
338 if (!HasAnyStateBits(NS_FRAME_FIRST_REFLOW
)) {
339 DrainSelfOverflowListInternal(aReflowInput
.mLineLayout
->GetInFirstLine());
342 // Set our own reflow input (additional state above and beyond aReflowInput).
343 InlineReflowInput irs
;
344 irs
.mPrevFrame
= nullptr;
345 irs
.mLineContainer
= aReflowInput
.mLineLayout
->LineContainerFrame();
346 irs
.mLineLayout
= aReflowInput
.mLineLayout
;
347 irs
.mNextInFlow
= (nsInlineFrame
*)GetNextInFlow();
348 irs
.mSetParentPointer
= lazilySetParentPointer
;
350 if (mFrames
.IsEmpty()) {
351 // Try to pull over one frame before starting so that we know
352 // whether we have an anonymous block or not.
353 Unused
<< PullOneFrame(aPresContext
, irs
);
356 ReflowFrames(aPresContext
, aReflowInput
, irs
, aMetrics
, aStatus
);
358 ReflowAbsoluteFrames(aPresContext
, aMetrics
, aReflowInput
, aStatus
);
360 // Note: the line layout code will properly compute our
361 // overflow-rect state for us.
364 nsresult
nsInlineFrame::AttributeChanged(int32_t aNameSpaceID
,
365 nsAtom
* aAttribute
, int32_t aModType
) {
367 nsContainerFrame::AttributeChanged(aNameSpaceID
, aAttribute
, aModType
);
373 if (IsInSVGTextSubtree()) {
374 SVGTextFrame
* f
= static_cast<SVGTextFrame
*>(
375 nsLayoutUtils::GetClosestFrameOfType(this, LayoutFrameType::SVGText
));
376 f
->HandleAttributeChangeInDescendant(mContent
->AsElement(), aNameSpaceID
,
383 bool nsInlineFrame::DrainSelfOverflowListInternal(bool aInFirstLine
) {
384 AutoFrameListPtr
overflowFrames(PresContext(), StealOverflowFrames());
385 if (!overflowFrames
|| overflowFrames
->IsEmpty()) {
389 // The frames on our own overflowlist may have been pushed by a
390 // previous lazilySetParentPointer Reflow so we need to ensure the
391 // correct parent pointer. This is sometimes skipped by Reflow.
392 nsIFrame
* firstChild
= overflowFrames
->FirstChild();
393 RestyleManager
* restyleManager
= PresContext()->RestyleManager();
394 for (nsIFrame
* f
= firstChild
; f
; f
= f
->GetNextSibling()) {
396 if (MOZ_UNLIKELY(aInFirstLine
)) {
397 restyleManager
->ReparentComputedStyleForFirstLine(f
);
398 nsLayoutUtils::MarkDescendantsDirty(f
);
401 mFrames
.AppendFrames(nullptr, std::move(*overflowFrames
));
406 bool nsInlineFrame::DrainSelfOverflowList() {
407 nsIFrame
* lineContainer
= nsLayoutUtils::FindNearestBlockAncestor(this);
408 // Add the eInFirstLine flag if we have a ::first-line ancestor frame.
409 // No need to look further than the nearest line container though.
410 bool inFirstLine
= false;
411 for (nsIFrame
* p
= GetParent(); p
!= lineContainer
; p
= p
->GetParent()) {
412 if (p
->IsLineFrame()) {
417 return DrainSelfOverflowListInternal(inFirstLine
);
421 bool nsInlineFrame::CanContinueTextRun() const {
422 // We can continue a text run through an inline frame
427 void nsInlineFrame::PullOverflowsFromPrevInFlow() {
428 nsInlineFrame
* prevInFlow
= static_cast<nsInlineFrame
*>(GetPrevInFlow());
430 nsPresContext
* presContext
= PresContext();
431 AutoFrameListPtr
prevOverflowFrames(presContext
,
432 prevInFlow
->StealOverflowFrames());
433 if (prevOverflowFrames
) {
434 // Assume that our prev-in-flow has the same line container that we do.
435 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames
, prevInFlow
,
437 mFrames
.InsertFrames(this, nullptr, std::move(*prevOverflowFrames
));
442 void nsInlineFrame::ReflowFrames(nsPresContext
* aPresContext
,
443 const ReflowInput
& aReflowInput
,
444 InlineReflowInput
& irs
, ReflowOutput
& aMetrics
,
445 nsReflowStatus
& aStatus
) {
446 MOZ_ASSERT(aStatus
.IsEmpty(), "Caller should pass a fresh reflow status!");
448 nsLineLayout
* lineLayout
= aReflowInput
.mLineLayout
;
449 bool inFirstLine
= aReflowInput
.mLineLayout
->GetInFirstLine();
450 RestyleManager
* restyleManager
= aPresContext
->RestyleManager();
451 WritingMode frameWM
= aReflowInput
.GetWritingMode();
452 WritingMode lineWM
= aReflowInput
.mLineLayout
->mRootSpan
->mWritingMode
;
453 LogicalMargin framePadding
=
454 aReflowInput
.ComputedLogicalBorderPadding(frameWM
);
455 nscoord startEdge
= 0;
456 const bool boxDecorationBreakClone
= MOZ_UNLIKELY(
457 StyleBorder()->mBoxDecorationBreak
== StyleBoxDecorationBreak::Clone
);
458 // Don't offset by our start borderpadding if we have a prev continuation or
459 // if we're in a part of an {ib} split other than the first one. For
460 // box-decoration-break:clone we always offset our start since all
461 // continuations have border/padding.
462 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
463 boxDecorationBreakClone
) {
464 startEdge
= framePadding
.IStart(frameWM
);
466 nscoord availableISize
= aReflowInput
.AvailableISize();
467 NS_ASSERTION(availableISize
!= NS_UNCONSTRAINEDSIZE
,
468 "should no longer use available widths");
469 // Subtract off inline axis border+padding from availableISize
470 availableISize
-= startEdge
;
471 availableISize
-= framePadding
.IEnd(frameWM
);
472 lineLayout
->BeginSpan(this, &aReflowInput
, startEdge
,
473 startEdge
+ availableISize
, &mBaseline
);
475 // First reflow our principal children.
476 nsIFrame
* frame
= mFrames
.FirstChild();
479 // Check if we should lazily set the child frame's parent pointer.
480 if (irs
.mSetParentPointer
) {
481 nsIFrame
* child
= frame
;
483 child
->SetParent(this);
485 restyleManager
->ReparentComputedStyleForFirstLine(child
);
486 nsLayoutUtils::MarkDescendantsDirty(child
);
488 // We also need to do the same for |frame|'s next-in-flows that are in
489 // the sibling list. Otherwise, if we reflow |frame| and it's complete
490 // we'll crash when trying to delete its next-in-flow.
491 // This scenario doesn't happen often, but it can happen.
492 nsIFrame
* nextSibling
= child
->GetNextSibling();
493 child
= child
->GetNextInFlow();
494 if (MOZ_UNLIKELY(child
)) {
495 while (child
!= nextSibling
&& nextSibling
) {
496 nextSibling
= nextSibling
->GetNextSibling();
502 MOZ_ASSERT(!child
|| mFrames
.ContainsFrame(child
));
505 // Fix the parent pointer for ::first-letter child frame next-in-flows,
506 // so nsFirstLetterFrame::Reflow can destroy them safely (bug 401042).
507 nsIFrame
* realFrame
= nsPlaceholderFrame::GetRealFrameFor(frame
);
508 if (realFrame
->IsLetterFrame()) {
509 nsIFrame
* child
= realFrame
->PrincipalChildList().FirstChild();
511 NS_ASSERTION(child
->IsTextFrame(), "unexpected frame type");
512 nsIFrame
* nextInFlow
= child
->GetNextInFlow();
513 for (; nextInFlow
; nextInFlow
= nextInFlow
->GetNextInFlow()) {
514 NS_ASSERTION(nextInFlow
->IsTextFrame(), "unexpected frame type");
515 if (mFrames
.ContainsFrame(nextInFlow
)) {
516 nextInFlow
->SetParent(this);
518 restyleManager
->ReparentComputedStyleForFirstLine(nextInFlow
);
519 nsLayoutUtils::MarkDescendantsDirty(nextInFlow
);
523 // Once we find a next-in-flow that isn't ours none of the
524 // remaining next-in-flows should be either.
525 for (; nextInFlow
; nextInFlow
= nextInFlow
->GetNextInFlow()) {
526 NS_ASSERTION(!mFrames
.ContainsFrame(nextInFlow
),
527 "unexpected letter frame flow");
536 MOZ_ASSERT(frame
->GetParent() == this);
539 bool reflowingFirstLetter
= lineLayout
->GetFirstLetterStyleOK();
540 ReflowInlineFrame(aPresContext
, aReflowInput
, irs
, frame
, aStatus
);
541 done
= aStatus
.IsInlineBreak() ||
542 (!reflowingFirstLetter
&& aStatus
.IsIncomplete());
544 if (!irs
.mSetParentPointer
) {
547 // Keep reparenting the remaining siblings, but don't reflow them.
548 nsFrameList
* pushedFrames
= GetOverflowFrames();
549 if (pushedFrames
&& pushedFrames
->FirstChild() == frame
) {
550 // Don't bother if |frame| was pushed to our overflow list.
554 irs
.mPrevFrame
= frame
;
557 frame
= frame
->GetNextSibling();
560 // Attempt to pull frames from our next-in-flow until we can't
561 if (!done
&& GetNextInFlow()) {
563 bool reflowingFirstLetter
= lineLayout
->GetFirstLetterStyleOK();
564 if (!frame
) { // Could be non-null if we pulled a first-letter frame and
565 // it created a continuation, since we don't push those.
566 frame
= PullOneFrame(aPresContext
, irs
);
569 printf("%p pulled up %p\n", this, frame
);
574 ReflowInlineFrame(aPresContext
, aReflowInput
, irs
, frame
, aStatus
);
575 if (aStatus
.IsInlineBreak() ||
576 (!reflowingFirstLetter
&& aStatus
.IsIncomplete())) {
579 irs
.mPrevFrame
= frame
;
580 frame
= frame
->GetNextSibling();
584 NS_ASSERTION(!aStatus
.IsComplete() || !GetOverflowFrames(),
585 "We can't be complete AND have overflow frames!");
587 // If after reflowing our children they take up no area then make
588 // sure that we don't either.
590 // Note: CSS demands that empty inline elements still affect the
591 // line-height calculations. However, continuations of an inline
592 // that are empty we force to empty so that things like collapsed
593 // whitespace in an inline element don't affect the line-height.
594 aMetrics
.ISize(lineWM
) = lineLayout
->EndSpan(this);
596 // Compute final width.
598 // XXX Note that that the padding start and end are in the frame's
599 // writing mode, but the metrics' inline-size is in the line's
600 // writing mode. This makes sense if the line and frame are both
601 // vertical or both horizontal, but what should happen with
602 // orthogonal inlines?
604 // Make sure to not include our start border and padding if we have a prev
605 // continuation or if we're in a part of an {ib} split other than the first
606 // one. For box-decoration-break:clone we always include our start border
607 // and padding since all continuations have them.
608 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
609 boxDecorationBreakClone
) {
610 aMetrics
.ISize(lineWM
) += framePadding
.IStart(frameWM
);
614 * We want to only apply the end border and padding if we're the last
615 * continuation and either not in an {ib} split or the last part of it. To
616 * be the last continuation we have to be complete (so that we won't get a
617 * next-in-flow) and have no non-fluid continuations on our continuation
618 * chain. For box-decoration-break:clone we always apply the end border and
619 * padding since all continuations have them.
621 if ((aStatus
.IsComplete() && !LastInFlow()->GetNextContinuation() &&
622 !FrameIsNonLastInIBSplit()) ||
623 boxDecorationBreakClone
) {
624 aMetrics
.ISize(lineWM
) += framePadding
.IEnd(frameWM
);
627 nsLayoutUtils::SetBSizeFromFontMetrics(this, aMetrics
, framePadding
, lineWM
,
630 // For now our overflow area is zero. The real value will be
631 // computed in |nsLineLayout::RelativePositionFrames|.
632 aMetrics
.mOverflowAreas
.Clear();
634 #ifdef NOISY_FINAL_SIZE
636 printf(": metrics=%d,%d ascent=%d\n", aMetrics
.Width(), aMetrics
.Height(),
637 aMetrics
.BlockStartAscent());
641 // Returns whether there's any remaining frame to pull.
643 bool nsInlineFrame::HasFramesToPull(nsInlineFrame
* aNextInFlow
) {
644 while (aNextInFlow
) {
645 if (!aNextInFlow
->mFrames
.IsEmpty()) {
648 if (const nsFrameList
* overflow
= aNextInFlow
->GetOverflowFrames()) {
649 if (!overflow
->IsEmpty()) {
653 aNextInFlow
= static_cast<nsInlineFrame
*>(aNextInFlow
->GetNextInFlow());
658 void nsInlineFrame::ReflowInlineFrame(nsPresContext
* aPresContext
,
659 const ReflowInput
& aReflowInput
,
660 InlineReflowInput
& irs
, nsIFrame
* aFrame
,
661 nsReflowStatus
& aStatus
) {
662 nsLineLayout
* lineLayout
= aReflowInput
.mLineLayout
;
663 bool reflowingFirstLetter
= lineLayout
->GetFirstLetterStyleOK();
666 lineLayout
->ReflowFrame(aFrame
, aStatus
, nullptr, pushedFrame
);
668 if (aStatus
.IsInlineBreakBefore()) {
669 if (aFrame
!= mFrames
.FirstChild()) {
670 // Change break-before status into break-after since we have
671 // already placed at least one child frame. This preserves the
672 // break-type so that it can be propagated upward.
673 StyleClear oldClearType
= aStatus
.FloatClearType();
675 aStatus
.SetIncomplete();
676 aStatus
.SetInlineLineBreakAfter(oldClearType
);
677 PushFrames(aPresContext
, aFrame
, irs
.mPrevFrame
, irs
);
679 // Preserve reflow status when breaking-before our first child
680 // and propagate it upward without modification.
685 // Create a next-in-flow if needed.
686 if (!aStatus
.IsFullyComplete()) {
687 CreateNextInFlow(aFrame
);
690 if (aStatus
.IsInlineBreakAfter()) {
691 nsIFrame
* nextFrame
= aFrame
->GetNextSibling();
693 aStatus
.SetIncomplete();
694 PushFrames(aPresContext
, nextFrame
, aFrame
, irs
);
696 // We must return an incomplete status if there are more child
697 // frames remaining in a next-in-flow that follows this frame.
698 if (HasFramesToPull(static_cast<nsInlineFrame
*>(GetNextInFlow()))) {
699 aStatus
.SetIncomplete();
705 if (!aStatus
.IsFullyComplete() && !reflowingFirstLetter
) {
706 nsIFrame
* nextFrame
= aFrame
->GetNextSibling();
708 PushFrames(aPresContext
, nextFrame
, aFrame
, irs
);
713 nsIFrame
* nsInlineFrame::PullOneFrame(nsPresContext
* aPresContext
,
714 InlineReflowInput
& irs
) {
715 nsIFrame
* frame
= nullptr;
716 nsInlineFrame
* nextInFlow
= irs
.mNextInFlow
;
719 bool willPull
= HasFramesToPull(nextInFlow
);
723 frame
= nextInFlow
->mFrames
.FirstChild();
725 // The nextInFlow's principal list has no frames, try its overflow list.
726 nsFrameList
* overflowFrames
= nextInFlow
->GetOverflowFrames();
727 if (overflowFrames
) {
728 frame
= overflowFrames
->RemoveFirstChild();
729 if (overflowFrames
->IsEmpty()) {
730 // We're stealing the only frame - delete the overflow list.
731 nextInFlow
->DestroyOverflowList();
733 // We leave the remaining frames on the overflow list (rather than
734 // putting them on nextInFlow's principal list) so we don't have to
735 // set up the parent for them.
737 // ReparentFloatsForInlineChild needs it to be on a child list -
738 // we remove it again below.
739 nextInFlow
->mFrames
= nsFrameList(frame
, frame
);
744 // If our block has no next continuation, then any floats belonging to
745 // the pulled frame must belong to our block already. This check ensures
746 // we do no extra work in the common non-vertical-breaking case.
747 if (irs
.mLineContainer
&& irs
.mLineContainer
->GetNextContinuation()) {
748 // The blockChildren.ContainsFrame check performed by
749 // ReparentFloatsForInlineChild will be fast because frame's ancestor
750 // will be the first child of its containing block.
751 ReparentFloatsForInlineChild(irs
.mLineContainer
, frame
, false);
753 nextInFlow
->mFrames
.RemoveFirstChild();
754 // nsFirstLineFrame::PullOneFrame calls ReparentComputedStyle.
756 mFrames
.InsertFrame(this, irs
.mPrevFrame
, frame
);
757 if (irs
.mLineLayout
) {
758 irs
.mLineLayout
->SetDirtyNextLine();
760 nsContainerFrame::ReparentFrameView(frame
, nextInFlow
, this);
763 nextInFlow
= static_cast<nsInlineFrame
*>(nextInFlow
->GetNextInFlow());
764 irs
.mNextInFlow
= nextInFlow
;
767 MOZ_ASSERT(!!frame
== willPull
);
771 void nsInlineFrame::PushFrames(nsPresContext
* aPresContext
,
772 nsIFrame
* aFromChild
, nsIFrame
* aPrevSibling
,
773 InlineReflowInput
& aState
) {
775 printf("%p pushing aFromChild %p, disconnecting from prev sib %p\n", this,
776 aFromChild
, aPrevSibling
);
779 PushChildrenToOverflow(aFromChild
, aPrevSibling
);
780 if (aState
.mLineLayout
) {
781 aState
.mLineLayout
->SetDirtyNextLine();
785 //////////////////////////////////////////////////////////////////////
787 LogicalSides
nsInlineFrame::GetLogicalSkipSides() const {
788 LogicalSides
skip(mWritingMode
);
789 if (MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak
==
790 StyleBoxDecorationBreak::Clone
)) {
795 nsInlineFrame
* prev
= (nsInlineFrame
*)GetPrevContinuation();
796 if (HasAnyStateBits(NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET
) ||
797 (prev
&& (prev
->mRect
.height
|| prev
->mRect
.width
))) {
798 // Prev continuation is not empty therefore we don't render our start
800 skip
+= LogicalSide::IStart
;
802 // If the prev continuation is empty, then go ahead and let our start
803 // edge border render.
807 nsInlineFrame
* next
= (nsInlineFrame
*)GetNextContinuation();
808 if (HasAnyStateBits(NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET
) ||
809 (next
&& (next
->mRect
.height
|| next
->mRect
.width
))) {
810 // Next continuation is not empty therefore we don't render our end
812 skip
+= LogicalSide::IEnd
;
814 // If the next continuation is empty, then go ahead and let our end
815 // edge border render.
819 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT
)) {
820 // All but the last part of an {ib} split should skip the "end" side (as
821 // determined by this frame's direction) and all but the first part of such
822 // a split should skip the "start" side. But figuring out which part of
823 // the split we are involves getting our first continuation, which might be
824 // expensive. So don't bother if we already have the relevant bits set.
825 if (skip
!= LogicalSides(mWritingMode
, LogicalSides::IBoth
)) {
826 // We're missing one of the skip bits, so check whether we need to set it.
827 // Only get the first continuation once, as an optimization.
828 nsIFrame
* firstContinuation
= FirstContinuation();
829 if (firstContinuation
->FrameIsNonLastInIBSplit()) {
830 skip
+= LogicalSide::IEnd
;
832 if (firstContinuation
->FrameIsNonFirstInIBSplit()) {
833 skip
+= LogicalSide::IStart
;
841 Maybe
<nscoord
> nsInlineFrame::GetNaturalBaselineBOffset(
842 WritingMode aWM
, BaselineSharingGroup aBaselineGroup
,
843 BaselineExportContext
) const {
844 if (aBaselineGroup
== BaselineSharingGroup::Last
) {
847 return Some(mBaseline
);
851 a11y::AccType
nsInlineFrame::AccessibleType() {
852 // FIXME(emilio): This is broken, if the image has its default `display` value
853 // overridden. Should be somewhere else.
854 if (mContent
->IsHTMLElement(
855 nsGkAtoms::img
)) // Create accessible for broken <img>
856 return a11y::eHyperTextType
;
858 return a11y::eNoType
;
862 void nsInlineFrame::UpdateStyleOfOwnedAnonBoxesForIBSplit(
863 ServoRestyleState
& aRestyleState
) {
864 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_OWNS_ANON_BOXES
),
865 "Why did we get called?");
866 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT
),
867 "Why did we have the NS_FRAME_OWNS_ANON_BOXES bit set?");
868 // Note: this assert _looks_ expensive, but it's cheap in all the cases when
870 MOZ_ASSERT(nsLayoutUtils::FirstContinuationOrIBSplitSibling(this) == this,
871 "Only the primary frame of the inline in a block-inside-inline "
872 "split should have NS_FRAME_OWNS_ANON_BOXES");
873 MOZ_ASSERT(mContent
->GetPrimaryFrame() == this,
874 "We should be the primary frame for our element");
876 nsIFrame
* blockFrame
= GetProperty(nsIFrame::IBSplitSibling());
877 MOZ_ASSERT(blockFrame
, "Why did we have an IB split?");
879 // The later inlines need to get our style.
880 ComputedStyle
* ourStyle
= Style();
882 // The anonymous block's style inherits from ours, and we already have our new
884 RefPtr
<ComputedStyle
> newContext
=
885 aRestyleState
.StyleSet().ResolveInheritingAnonymousBoxStyle(
886 PseudoStyleType::mozBlockInsideInlineWrapper
, ourStyle
);
888 // We're guaranteed that newContext only differs from the old ComputedStyle on
889 // the block in things they might inherit from us. And changehint processing
890 // guarantees walking the continuation and ib-sibling chains, so our existing
891 // changehint being in aChangeList is good enough. So we don't need to touch
892 // aChangeList at all here.
895 MOZ_ASSERT(!blockFrame
->GetPrevContinuation(),
896 "Must be first continuation");
898 MOZ_ASSERT(blockFrame
->Style()->GetPseudoType() ==
899 PseudoStyleType::mozBlockInsideInlineWrapper
,
900 "Unexpected kind of ComputedStyle");
902 for (nsIFrame
* cont
= blockFrame
; cont
;
903 cont
= cont
->GetNextContinuation()) {
904 cont
->SetComputedStyle(newContext
);
907 nsIFrame
* nextInline
= blockFrame
->GetProperty(nsIFrame::IBSplitSibling());
909 // This check is here due to bug 1431232. Please remove it once
910 // that bug is fixed.
911 if (MOZ_UNLIKELY(!nextInline
)) {
915 MOZ_ASSERT(nextInline
, "There is always a trailing inline in an IB split");
917 for (nsIFrame
* cont
= nextInline
; cont
;
918 cont
= cont
->GetNextContinuation()) {
919 cont
->SetComputedStyle(ourStyle
);
921 blockFrame
= nextInline
->GetProperty(nsIFrame::IBSplitSibling());
925 //////////////////////////////////////////////////////////////////////
927 // nsLineFrame implementation
929 nsFirstLineFrame
* NS_NewFirstLineFrame(PresShell
* aPresShell
,
930 ComputedStyle
* aStyle
) {
931 return new (aPresShell
)
932 nsFirstLineFrame(aStyle
, aPresShell
->GetPresContext());
935 NS_IMPL_FRAMEARENA_HELPERS(nsFirstLineFrame
)
937 void nsFirstLineFrame::Init(nsIContent
* aContent
, nsContainerFrame
* aParent
,
938 nsIFrame
* aPrevInFlow
) {
939 nsInlineFrame::Init(aContent
, aParent
, aPrevInFlow
);
941 MOZ_ASSERT(Style()->GetPseudoType() == PseudoStyleType::firstLine
);
945 // This frame is a continuation - fixup the computed style if aPrevInFlow
946 // is the first-in-flow (the only one with a ::first-line pseudo).
947 if (aPrevInFlow
->Style()->GetPseudoType() == PseudoStyleType::firstLine
) {
948 MOZ_ASSERT(FirstInFlow() == aPrevInFlow
);
949 // Create a new ComputedStyle that is a child of the parent
950 // ComputedStyle thus removing the ::first-line style. This way
951 // we behave as if an anonymous (unstyled) span was the child
952 // of the parent frame.
953 ComputedStyle
* parentContext
= aParent
->Style();
954 RefPtr
<ComputedStyle
> newSC
=
955 PresContext()->StyleSet()->ResolveInheritingAnonymousBoxStyle(
956 PseudoStyleType::mozLineFrame
, parentContext
);
957 SetComputedStyle(newSC
);
959 MOZ_ASSERT(FirstInFlow() != aPrevInFlow
);
960 MOZ_ASSERT(aPrevInFlow
->Style()->GetPseudoType() ==
961 PseudoStyleType::mozLineFrame
);
965 #ifdef DEBUG_FRAME_DUMP
966 nsresult
nsFirstLineFrame::GetFrameName(nsAString
& aResult
) const {
967 return MakeFrameName(u
"Line"_ns
, aResult
);
971 nsIFrame
* nsFirstLineFrame::PullOneFrame(nsPresContext
* aPresContext
,
972 InlineReflowInput
& irs
) {
973 nsIFrame
* frame
= nsInlineFrame::PullOneFrame(aPresContext
, irs
);
974 if (frame
&& !GetPrevInFlow()) {
975 // We are a first-line frame. Fixup the child frames
976 // style-context that we just pulled.
977 NS_ASSERTION(frame
->GetParent() == this, "Incorrect parent?");
978 aPresContext
->RestyleManager()->ReparentComputedStyleForFirstLine(frame
);
979 nsLayoutUtils::MarkDescendantsDirty(frame
);
984 void nsFirstLineFrame::Reflow(nsPresContext
* aPresContext
,
985 ReflowOutput
& aMetrics
,
986 const ReflowInput
& aReflowInput
,
987 nsReflowStatus
& aStatus
) {
989 MOZ_ASSERT(aStatus
.IsEmpty(), "Caller should pass a fresh reflow status!");
991 if (nullptr == aReflowInput
.mLineLayout
) {
992 return; // XXX does this happen? why?
995 // Check for an overflow list with our prev-in-flow
996 nsFirstLineFrame
* prevInFlow
= (nsFirstLineFrame
*)GetPrevInFlow();
998 AutoFrameListPtr
prevOverflowFrames(aPresContext
,
999 prevInFlow
->StealOverflowFrames());
1000 if (prevOverflowFrames
) {
1001 // Reparent the new frames and their ComputedStyles.
1002 const nsFrameList::Slice
& newFrames
=
1003 mFrames
.InsertFrames(this, nullptr, std::move(*prevOverflowFrames
));
1004 ReparentChildListStyle(aPresContext
, newFrames
, this);
1008 // It's also possible that we have an overflow list for ourselves.
1009 DrainSelfOverflowList();
1011 // Set our own reflow input (additional state above and beyond aReflowInput).
1012 InlineReflowInput irs
;
1013 irs
.mPrevFrame
= nullptr;
1014 irs
.mLineContainer
= aReflowInput
.mLineLayout
->LineContainerFrame();
1015 irs
.mLineLayout
= aReflowInput
.mLineLayout
;
1016 irs
.mNextInFlow
= (nsInlineFrame
*)GetNextInFlow();
1018 bool wasEmpty
= mFrames
.IsEmpty();
1020 // Try to pull over one frame before starting so that we know
1021 // whether we have an anonymous block or not.
1022 PullOneFrame(aPresContext
, irs
);
1025 if (nullptr == GetPrevInFlow()) {
1026 // XXX This is pretty sick, but what we do here is to pull-up, in
1027 // advance, all of the next-in-flows children. We re-resolve their
1028 // style while we are at at it so that when we reflow they have
1031 // All of this is so that text-runs reflow properly.
1032 irs
.mPrevFrame
= mFrames
.LastChild();
1034 nsIFrame
* frame
= PullOneFrame(aPresContext
, irs
);
1038 irs
.mPrevFrame
= frame
;
1040 irs
.mPrevFrame
= nullptr;
1043 NS_ASSERTION(!aReflowInput
.mLineLayout
->GetInFirstLine(),
1044 "Nested first-line frames? BOGUS");
1045 aReflowInput
.mLineLayout
->SetInFirstLine(true);
1046 ReflowFrames(aPresContext
, aReflowInput
, irs
, aMetrics
, aStatus
);
1047 aReflowInput
.mLineLayout
->SetInFirstLine(false);
1049 ReflowAbsoluteFrames(aPresContext
, aMetrics
, aReflowInput
, aStatus
);
1051 // Note: the line layout code will properly compute our overflow state for us
1055 void nsFirstLineFrame::PullOverflowsFromPrevInFlow() {
1056 nsFirstLineFrame
* prevInFlow
=
1057 static_cast<nsFirstLineFrame
*>(GetPrevInFlow());
1059 nsPresContext
* presContext
= PresContext();
1060 AutoFrameListPtr
prevOverflowFrames(presContext
,
1061 prevInFlow
->StealOverflowFrames());
1062 if (prevOverflowFrames
) {
1063 // Assume that our prev-in-flow has the same line container that we do.
1064 const nsFrameList::Slice
& newFrames
=
1065 mFrames
.InsertFrames(this, nullptr, std::move(*prevOverflowFrames
));
1066 ReparentChildListStyle(presContext
, newFrames
, this);
1072 bool nsFirstLineFrame::DrainSelfOverflowList() {
1073 AutoFrameListPtr
overflowFrames(PresContext(), StealOverflowFrames());
1074 if (overflowFrames
) {
1075 bool result
= !overflowFrames
->IsEmpty();
1076 const nsFrameList::Slice
& newFrames
=
1077 mFrames
.AppendFrames(nullptr, std::move(*overflowFrames
));
1078 ReparentChildListStyle(PresContext(), newFrames
, this);