1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* rendering object for CSS display:inline objects */
8 #include "nsInlineFrame.h"
9 #include "nsLineLayout.h"
10 #include "nsBlockFrame.h"
11 #include "nsPlaceholderFrame.h"
12 #include "nsGkAtoms.h"
13 #include "nsStyleContext.h"
14 #include "nsPresContext.h"
15 #include "nsRenderingContext.h"
16 #include "nsCSSAnonBoxes.h"
17 #include "nsAutoPtr.h"
18 #include "RestyleManager.h"
19 #include "nsDisplayList.h"
20 #include "mozilla/Likely.h"
26 using namespace mozilla
;
27 using namespace mozilla::layout
;
30 //////////////////////////////////////////////////////////////////////
32 // Basic nsInlineFrame methods
35 NS_NewInlineFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
37 return new (aPresShell
) nsInlineFrame(aContext
);
40 NS_IMPL_FRAMEARENA_HELPERS(nsInlineFrame
)
42 NS_QUERYFRAME_HEAD(nsInlineFrame
)
43 NS_QUERYFRAME_ENTRY(nsInlineFrame
)
44 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame
)
46 #ifdef DEBUG_FRAME_DUMP
48 nsInlineFrame::GetFrameName(nsAString
& aResult
) const
50 return MakeFrameName(NS_LITERAL_STRING("Inline"), aResult
);
55 nsInlineFrame::GetType() const
57 return nsGkAtoms::inlineFrame
;
61 nsInlineFrame::InvalidateFrame(uint32_t aDisplayItemKey
)
64 nsIFrame
* svgTextFrame
=
65 nsLayoutUtils::GetClosestFrameOfType(GetParent(),
66 nsGkAtoms::svgTextFrame
);
67 svgTextFrame
->InvalidateFrame();
70 nsInlineFrameBase::InvalidateFrame(aDisplayItemKey
);
74 nsInlineFrame::InvalidateFrameWithRect(const nsRect
& aRect
, uint32_t aDisplayItemKey
)
77 nsIFrame
* svgTextFrame
=
78 nsLayoutUtils::GetClosestFrameOfType(GetParent(),
79 nsGkAtoms::svgTextFrame
);
80 svgTextFrame
->InvalidateFrame();
83 nsInlineFrameBase::InvalidateFrameWithRect(aRect
, aDisplayItemKey
);
87 IsMarginZero(const nsStyleCoord
&aCoord
)
89 return aCoord
.GetUnit() == eStyleUnit_Auto
||
90 nsLayoutUtils::IsMarginZero(aCoord
);
94 nsInlineFrame::IsSelfEmpty()
97 // I used to think inline frames worked this way, but it seems they
98 // don't. At least not in our codebase.
99 if (GetPresContext()->CompatibilityMode() == eCompatibility_FullStandards
) {
103 const nsStyleMargin
* margin
= StyleMargin();
104 const nsStyleBorder
* border
= StyleBorder();
105 const nsStylePadding
* padding
= StylePadding();
106 // XXX Top and bottom removed, since they shouldn't affect things, but this
107 // doesn't really match with nsLineLayout.cpp's setting of
108 // ZeroEffectiveSpanBox, anymore, so what should this really be?
110 border
->GetComputedBorderWidth(NS_SIDE_RIGHT
) != 0 ||
111 !nsLayoutUtils::IsPaddingZero(padding
->mPadding
.GetRight()) ||
112 !IsMarginZero(margin
->mMargin
.GetRight());
114 border
->GetComputedBorderWidth(NS_SIDE_LEFT
) != 0 ||
115 !nsLayoutUtils::IsPaddingZero(padding
->mPadding
.GetLeft()) ||
116 !IsMarginZero(margin
->mMargin
.GetLeft());
117 if (haveLeft
|| haveRight
) {
118 // We skip this block and return false for box-decoration-break:clone since
119 // in that case all the continuations will have the border/padding/margin.
120 if ((GetStateBits() & NS_FRAME_PART_OF_IBSPLIT
) &&
121 StyleBorder()->mBoxDecorationBreak
==
122 NS_STYLE_BOX_DECORATION_BREAK_SLICE
) {
123 bool haveStart
, haveEnd
;
124 if (NS_STYLE_DIRECTION_LTR
== StyleVisibility()->mDirection
) {
125 haveStart
= haveLeft
;
128 haveStart
= haveRight
;
131 // For ib-split frames, ignore things we know we'll skip in GetSkipSides.
132 // XXXbz should we be doing this for non-ib-split frames too, in a more
135 // Get the first continuation eagerly, as a performance optimization, to
136 // avoid having to get it twice..
137 nsIFrame
* firstCont
= FirstContinuation();
139 (!haveStart
|| firstCont
->FrameIsNonFirstInIBSplit()) &&
140 (!haveEnd
|| firstCont
->FrameIsNonLastInIBSplit());
148 nsInlineFrame::IsEmpty()
150 if (!IsSelfEmpty()) {
154 for (nsIFrame
*kid
= mFrames
.FirstChild(); kid
; kid
= kid
->GetNextSibling()) {
162 nsIFrame::FrameSearchResult
163 nsInlineFrame::PeekOffsetCharacter(bool aForward
, int32_t* aOffset
,
164 bool aRespectClusters
)
166 // Override the implementation in nsFrame, to skip empty inline frames
167 NS_ASSERTION (aOffset
&& *aOffset
<= 1, "aOffset out of range");
168 int32_t startOffset
= *aOffset
;
171 if (aForward
== (startOffset
== 0)) {
172 // We're before the frame and moving forward, or after it and moving backwards:
173 // skip to the other side, but keep going.
174 *aOffset
= 1 - startOffset
;
180 nsInlineFrame::DestroyFrom(nsIFrame
* aDestructRoot
)
182 nsFrameList
* overflowFrames
= GetOverflowFrames();
183 if (overflowFrames
) {
184 // Fixup the parent pointers for any child frames on the OverflowList.
185 // nsIFrame::DestroyFrom depends on that to find the sticky scroll
186 // container (an ancestor).
187 nsIFrame
* lineContainer
= nsLayoutUtils::FindNearestBlockAncestor(this);
188 DrainSelfOverflowListInternal(eForDestroy
, lineContainer
);
190 nsContainerFrame::DestroyFrom(aDestructRoot
);
194 nsInlineFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
195 const nsRect
& aDirtyRect
,
196 const nsDisplayListSet
& aLists
)
198 BuildDisplayListForInline(aBuilder
, aDirtyRect
, aLists
);
200 // The sole purpose of this is to trigger display of the selection
201 // window for Named Anchors, which don't have any children and
202 // normally don't have any size, but in Editor we use CSS to display
203 // an image to represent this "hidden" element.
204 if (!mFrames
.FirstChild()) {
205 DisplaySelectionOverlay(aBuilder
, aLists
.Content());
209 //////////////////////////////////////////////////////////////////////
213 nsInlineFrame::AddInlineMinISize(nsRenderingContext
*aRenderingContext
,
214 nsIFrame::InlineMinISizeData
*aData
)
216 DoInlineIntrinsicISize(aRenderingContext
, aData
, nsLayoutUtils::MIN_ISIZE
);
220 nsInlineFrame::AddInlinePrefISize(nsRenderingContext
*aRenderingContext
,
221 nsIFrame::InlinePrefISizeData
*aData
)
223 DoInlineIntrinsicISize(aRenderingContext
, aData
, nsLayoutUtils::PREF_ISIZE
);
228 nsInlineFrame::ComputeSize(nsRenderingContext
*aRenderingContext
,
230 const LogicalSize
& aCBSize
,
231 nscoord aAvailableISize
,
232 const LogicalSize
& aMargin
,
233 const LogicalSize
& aBorder
,
234 const LogicalSize
& aPadding
,
237 // Inlines and text don't compute size before reflow.
238 return LogicalSize(aWM
, NS_UNCONSTRAINEDSIZE
, NS_UNCONSTRAINEDSIZE
);
242 nsInlineFrame::ComputeTightBounds(gfxContext
* aContext
) const
245 if (StyleContext()->HasTextDecorationLines()) {
246 return GetVisualOverflowRect();
248 return ComputeSimpleTightBounds(aContext
);
252 nsInlineFrame::ReparentFloatsForInlineChild(nsIFrame
* aOurLineContainer
,
254 bool aReparentSiblings
)
256 // XXXbz this would be better if it took a nsFrameList or a frame
258 NS_ASSERTION(aOurLineContainer
->GetNextContinuation() ||
259 aOurLineContainer
->GetPrevContinuation(),
260 "Don't call this when we have no continuation, it's a waste");
262 NS_ASSERTION(aReparentSiblings
, "Why did we get called?");
266 nsIFrame
* ancestor
= aFrame
;
268 ancestor
= ancestor
->GetParent();
271 } while (!ancestor
->IsFloatContainingBlock());
273 if (ancestor
== aOurLineContainer
)
276 nsBlockFrame
* ourBlock
= nsLayoutUtils::GetAsBlock(aOurLineContainer
);
277 NS_ASSERTION(ourBlock
, "Not a block, but broke vertically?");
278 nsBlockFrame
* frameBlock
= nsLayoutUtils::GetAsBlock(ancestor
);
279 NS_ASSERTION(frameBlock
, "ancestor not a block");
282 ourBlock
->ReparentFloats(aFrame
, frameBlock
, false);
284 if (!aReparentSiblings
)
286 nsIFrame
* next
= aFrame
->GetNextSibling();
289 if (next
->GetParent() == aFrame
->GetParent()) {
293 // This is paranoid and will hardly ever get hit ... but we can't actually
294 // trust that the frames in the sibling chain all have the same parent,
295 // because lazy reparenting may be going on. If we find a different
296 // parent we need to redo our analysis.
297 ReparentFloatsForInlineChild(aOurLineContainer
, next
, aReparentSiblings
);
303 ReparentChildListStyle(nsPresContext
* aPresContext
,
304 const nsFrameList::Slice
& aFrames
,
305 nsIFrame
* aParentFrame
)
307 RestyleManager
* restyleManager
= aPresContext
->RestyleManager();
309 for (nsFrameList::Enumerator
e(aFrames
); !e
.AtEnd(); e
.Next()) {
310 NS_ASSERTION(e
.get()->GetParent() == aParentFrame
, "Bogus parentage");
311 restyleManager
->ReparentStyleContext(e
.get());
312 nsLayoutUtils::MarkDescendantsDirty(e
.get());
317 nsInlineFrame::Reflow(nsPresContext
* aPresContext
,
318 nsHTMLReflowMetrics
& aMetrics
,
319 const nsHTMLReflowState
& aReflowState
,
320 nsReflowStatus
& aStatus
)
322 DO_GLOBAL_REFLOW_COUNT("nsInlineFrame");
323 DISPLAY_REFLOW(aPresContext
, this, aReflowState
, aMetrics
, aStatus
);
324 if (nullptr == aReflowState
.mLineLayout
) {
325 NS_ERROR("must have non-null aReflowState.mLineLayout");
328 if (IsFrameTreeTooDeep(aReflowState
, aMetrics
, aStatus
)) {
332 bool lazilySetParentPointer
= false;
334 nsIFrame
* lineContainer
= aReflowState
.mLineLayout
->LineContainerFrame();
336 // Check for an overflow list with our prev-in-flow
337 nsInlineFrame
* prevInFlow
= (nsInlineFrame
*)GetPrevInFlow();
339 AutoFrameListPtr
prevOverflowFrames(aPresContext
,
340 prevInFlow
->StealOverflowFrames());
341 if (prevOverflowFrames
) {
342 // When pushing and pulling frames we need to check for whether any
343 // views need to be reparented.
344 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames
, prevInFlow
,
347 // Check if we should do the lazilySetParentPointer optimization.
348 // Only do it in simple cases where we're being reflowed for the
349 // first time, nothing (e.g. bidi resolution) has already given
350 // us children, and there's no next-in-flow, so all our frames
351 // will be taken from prevOverflowFrames.
352 if ((GetStateBits() & NS_FRAME_FIRST_REFLOW
) && mFrames
.IsEmpty() &&
354 // If our child list is empty, just put the new frames into it.
355 // Note that we don't set the parent pointer for the new frames. Instead wait
356 // to do this until we actually reflow the frame. If the overflow list contains
357 // thousands of frames this is a big performance issue (see bug #5588)
358 mFrames
.SetFrames(*prevOverflowFrames
);
359 lazilySetParentPointer
= true;
361 // Assign all floats to our block if necessary
362 if (lineContainer
&& lineContainer
->GetPrevContinuation()) {
363 ReparentFloatsForInlineChild(lineContainer
,
364 prevOverflowFrames
->FirstChild(),
367 // Insert the new frames at the beginning of the child list
368 // and set their parent pointer
369 const nsFrameList::Slice
& newFrames
=
370 mFrames
.InsertFrames(this, nullptr, *prevOverflowFrames
);
371 // If our prev in flow was under the first continuation of a first-line
372 // frame then we need to reparent the style contexts to remove the
373 // the special first-line styling. In the lazilySetParentPointer case
374 // we reparent the style contexts when we set their parents in
375 // nsInlineFrame::ReflowFrames and nsInlineFrame::ReflowInlineFrame.
376 if (aReflowState
.mLineLayout
->GetInFirstLine()) {
377 ReparentChildListStyle(aPresContext
, newFrames
, this);
383 // It's also possible that we have an overflow list for ourselves
385 if (GetStateBits() & NS_FRAME_FIRST_REFLOW
) {
386 // If it's our initial reflow, then we should not have an overflow list.
387 // However, add an assertion in case we get reflowed more than once with
388 // the initial reflow reason
389 nsFrameList
* overflowFrames
= GetOverflowFrames();
390 NS_ASSERTION(!overflowFrames
|| overflowFrames
->IsEmpty(),
391 "overflow list is not empty for initial reflow");
394 if (!(GetStateBits() & NS_FRAME_FIRST_REFLOW
)) {
396 lazilySetParentPointer
? eDontReparentFrames
: DrainFlags(0);
397 if (aReflowState
.mLineLayout
->GetInFirstLine()) {
398 flags
= DrainFlags(flags
| eInFirstLine
);
400 DrainSelfOverflowListInternal(flags
, lineContainer
);
403 // Set our own reflow state (additional state above and beyond
405 InlineReflowState irs
;
406 irs
.mPrevFrame
= nullptr;
407 irs
.mLineContainer
= lineContainer
;
408 irs
.mLineLayout
= aReflowState
.mLineLayout
;
409 irs
.mNextInFlow
= (nsInlineFrame
*) GetNextInFlow();
410 irs
.mSetParentPointer
= lazilySetParentPointer
;
412 if (mFrames
.IsEmpty()) {
413 // Try to pull over one frame before starting so that we know
414 // whether we have an anonymous block or not.
416 (void) PullOneFrame(aPresContext
, irs
, &complete
);
419 ReflowFrames(aPresContext
, aReflowState
, irs
, aMetrics
, aStatus
);
421 ReflowAbsoluteFrames(aPresContext
, aMetrics
, aReflowState
, aStatus
);
423 // Note: the line layout code will properly compute our
424 // overflow-rect state for us.
426 NS_FRAME_SET_TRUNCATION(aStatus
, aReflowState
, aMetrics
);
430 nsInlineFrame::DrainSelfOverflowListInternal(DrainFlags aFlags
,
431 nsIFrame
* aLineContainer
)
433 AutoFrameListPtr
overflowFrames(PresContext(), StealOverflowFrames());
434 if (overflowFrames
) {
435 NS_ASSERTION(mFrames
.NotEmpty(), "overflow list w/o frames");
436 // The frames on our own overflowlist may have been pushed by a
437 // previous lazilySetParentPointer Reflow so we need to ensure the
438 // correct parent pointer. This is sometimes skipped by Reflow.
439 if (!(aFlags
& eDontReparentFrames
)) {
440 nsIFrame
* firstChild
= overflowFrames
->FirstChild();
441 if (aLineContainer
&& aLineContainer
->GetPrevContinuation()) {
442 ReparentFloatsForInlineChild(aLineContainer
, firstChild
, true);
444 const bool doReparentSC
=
445 (aFlags
& eInFirstLine
) && !(aFlags
& eForDestroy
);
446 RestyleManager
* restyleManager
= PresContext()->RestyleManager();
447 for (nsIFrame
* f
= firstChild
; f
; f
= f
->GetNextSibling()) {
450 restyleManager
->ReparentStyleContext(f
);
451 nsLayoutUtils::MarkDescendantsDirty(f
);
455 bool result
= !overflowFrames
->IsEmpty();
456 mFrames
.AppendFrames(nullptr, *overflowFrames
);
463 nsInlineFrame::DrainSelfOverflowList()
465 nsIFrame
* lineContainer
= nsLayoutUtils::FindNearestBlockAncestor(this);
466 // Add the eInFirstLine flag if we have a ::first-line ancestor frame.
467 // No need to look further than the nearest line container though.
468 DrainFlags flags
= DrainFlags(0);
469 for (nsIFrame
* p
= GetParent(); p
!= lineContainer
; p
= p
->GetParent()) {
470 if (p
->GetType() == nsGkAtoms::lineFrame
) {
471 flags
= DrainFlags(flags
| eInFirstLine
);
475 return DrainSelfOverflowListInternal(flags
, lineContainer
);
479 nsInlineFrame::CanContinueTextRun() const
481 // We can continue a text run through an inline frame
486 nsInlineFrame::PullOverflowsFromPrevInFlow()
488 nsInlineFrame
* prevInFlow
= static_cast<nsInlineFrame
*>(GetPrevInFlow());
490 nsPresContext
* presContext
= PresContext();
491 AutoFrameListPtr
prevOverflowFrames(presContext
,
492 prevInFlow
->StealOverflowFrames());
493 if (prevOverflowFrames
) {
494 // Assume that our prev-in-flow has the same line container that we do.
495 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames
, prevInFlow
,
497 mFrames
.InsertFrames(this, nullptr, *prevOverflowFrames
);
503 nsInlineFrame::ReflowFrames(nsPresContext
* aPresContext
,
504 const nsHTMLReflowState
& aReflowState
,
505 InlineReflowState
& irs
,
506 nsHTMLReflowMetrics
& aMetrics
,
507 nsReflowStatus
& aStatus
)
509 aStatus
= NS_FRAME_COMPLETE
;
511 nsLineLayout
* lineLayout
= aReflowState
.mLineLayout
;
512 bool inFirstLine
= aReflowState
.mLineLayout
->GetInFirstLine();
513 RestyleManager
* restyleManager
= aPresContext
->RestyleManager();
514 WritingMode frameWM
= aReflowState
.GetWritingMode();
515 WritingMode lineWM
= aReflowState
.mLineLayout
->mRootSpan
->mWritingMode
;
516 LogicalMargin framePadding
= aReflowState
.ComputedLogicalBorderPadding();
517 nscoord startEdge
= 0;
518 const bool boxDecorationBreakClone
=
519 MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak
==
520 NS_STYLE_BOX_DECORATION_BREAK_CLONE
);
521 // Don't offset by our start borderpadding if we have a prev continuation or
522 // if we're in a part of an {ib} split other than the first one. For
523 // box-decoration-break:clone we always offset our start since all
524 // continuations have border/padding.
525 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
526 boxDecorationBreakClone
) {
527 startEdge
= framePadding
.IStart(frameWM
);
529 nscoord availableISize
= aReflowState
.AvailableISize();
530 NS_ASSERTION(availableISize
!= NS_UNCONSTRAINEDSIZE
,
531 "should no longer use available widths");
532 // Subtract off inline axis border+padding from availableISize
533 availableISize
-= startEdge
;
534 availableISize
-= framePadding
.IEnd(frameWM
);
535 lineLayout
->BeginSpan(this, &aReflowState
, startEdge
,
536 startEdge
+ availableISize
, &mBaseline
);
538 // First reflow our principal children.
539 nsIFrame
* frame
= mFrames
.FirstChild();
542 // Check if we should lazily set the child frame's parent pointer.
543 if (irs
.mSetParentPointer
) {
545 irs
.mLineContainer
&& irs
.mLineContainer
->GetPrevContinuation();
546 nsIFrame
* child
= frame
;
548 // If our block is the first in flow, then any floats under the pulled
549 // frame must already belong to our block.
551 // This has to happen before we update frame's parent; we need to
552 // know frame's ancestry under its old block.
553 // The blockChildren.ContainsFrame check performed by
554 // ReparentFloatsForInlineChild here may be slow, but we can't
555 // easily avoid it because we don't know where 'frame' originally
556 // came from. If we really really have to optimize this we could
557 // cache whether frame->GetParent() is under its containing blocks
558 // overflowList or not.
559 ReparentFloatsForInlineChild(irs
.mLineContainer
, child
, false);
561 child
->SetParent(this);
563 restyleManager
->ReparentStyleContext(child
);
564 nsLayoutUtils::MarkDescendantsDirty(child
);
566 // We also need to do the same for |frame|'s next-in-flows that are in
567 // the sibling list. Otherwise, if we reflow |frame| and it's complete
568 // we'll crash when trying to delete its next-in-flow.
569 // This scenario doesn't happen often, but it can happen.
570 nsIFrame
* nextSibling
= child
->GetNextSibling();
571 child
= child
->GetNextInFlow();
572 if (MOZ_UNLIKELY(child
)) {
573 while (child
!= nextSibling
&& nextSibling
) {
574 nextSibling
= nextSibling
->GetNextSibling();
580 MOZ_ASSERT(!child
|| mFrames
.ContainsFrame(child
));
583 // Fix the parent pointer for ::first-letter child frame next-in-flows,
584 // so nsFirstLetterFrame::Reflow can destroy them safely (bug 401042).
585 nsIFrame
* realFrame
= nsPlaceholderFrame::GetRealFrameFor(frame
);
586 if (realFrame
->GetType() == nsGkAtoms::letterFrame
) {
587 nsIFrame
* child
= realFrame
->GetFirstPrincipalChild();
589 NS_ASSERTION(child
->GetType() == nsGkAtoms::textFrame
,
590 "unexpected frame type");
591 nsIFrame
* nextInFlow
= child
->GetNextInFlow();
592 for ( ; nextInFlow
; nextInFlow
= nextInFlow
->GetNextInFlow()) {
593 NS_ASSERTION(nextInFlow
->GetType() == nsGkAtoms::textFrame
,
594 "unexpected frame type");
595 if (mFrames
.ContainsFrame(nextInFlow
)) {
596 nextInFlow
->SetParent(this);
598 restyleManager
->ReparentStyleContext(nextInFlow
);
599 nsLayoutUtils::MarkDescendantsDirty(nextInFlow
);
604 // Once we find a next-in-flow that isn't ours none of the
605 // remaining next-in-flows should be either.
606 for ( ; nextInFlow
; nextInFlow
= nextInFlow
->GetNextInFlow()) {
607 NS_ASSERTION(!mFrames
.ContainsFrame(nextInFlow
),
608 "unexpected letter frame flow");
617 MOZ_ASSERT(frame
->GetParent() == this);
620 bool reflowingFirstLetter
= lineLayout
->GetFirstLetterStyleOK();
621 ReflowInlineFrame(aPresContext
, aReflowState
, irs
, frame
, aStatus
);
622 done
= NS_INLINE_IS_BREAK(aStatus
) ||
623 (!reflowingFirstLetter
&& NS_FRAME_IS_NOT_COMPLETE(aStatus
));
625 if (!irs
.mSetParentPointer
) {
628 // Keep reparenting the remaining siblings, but don't reflow them.
629 nsFrameList
* pushedFrames
= GetOverflowFrames();
630 if (pushedFrames
&& pushedFrames
->FirstChild() == frame
) {
631 // Don't bother if |frame| was pushed to our overflow list.
635 irs
.mPrevFrame
= frame
;
638 frame
= frame
->GetNextSibling();
641 // Attempt to pull frames from our next-in-flow until we can't
642 if (!done
&& GetNextInFlow()) {
644 bool reflowingFirstLetter
= lineLayout
->GetFirstLetterStyleOK();
646 if (!frame
) { // Could be non-null if we pulled a first-letter frame and
647 // it created a continuation, since we don't push those.
648 frame
= PullOneFrame(aPresContext
, irs
, &isComplete
);
651 printf("%p pulled up %p\n", this, frame
);
653 if (nullptr == frame
) {
655 aStatus
= NS_FRAME_NOT_COMPLETE
;
659 ReflowInlineFrame(aPresContext
, aReflowState
, irs
, frame
, aStatus
);
660 if (NS_INLINE_IS_BREAK(aStatus
) ||
661 (!reflowingFirstLetter
&& NS_FRAME_IS_NOT_COMPLETE(aStatus
))) {
664 irs
.mPrevFrame
= frame
;
665 frame
= frame
->GetNextSibling();
669 NS_ASSERTION(!NS_FRAME_IS_COMPLETE(aStatus
) || !GetOverflowFrames(),
670 "We can't be complete AND have overflow frames!");
672 // If after reflowing our children they take up no area then make
673 // sure that we don't either.
675 // Note: CSS demands that empty inline elements still affect the
676 // line-height calculations. However, continuations of an inline
677 // that are empty we force to empty so that things like collapsed
678 // whitespace in an inline element don't affect the line-height.
679 aMetrics
.ISize(lineWM
) = lineLayout
->EndSpan(this);
681 // Compute final width.
683 // XXX Note that that the padding start and end are in the frame's
684 // writing mode, but the metrics' inline-size is in the line's
685 // writing mode. This makes sense if the line and frame are both
686 // vertical or both horizontal, but what should happen with
687 // orthogonal inlines?
689 // Make sure to not include our start border and padding if we have a prev
690 // continuation or if we're in a part of an {ib} split other than the first
691 // one. For box-decoration-break:clone we always include our start border
692 // and padding since all continuations have them.
693 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
694 boxDecorationBreakClone
) {
695 aMetrics
.ISize(lineWM
) += framePadding
.IStart(frameWM
);
699 * We want to only apply the end border and padding if we're the last
700 * continuation and either not in an {ib} split or the last part of it. To
701 * be the last continuation we have to be complete (so that we won't get a
702 * next-in-flow) and have no non-fluid continuations on our continuation
703 * chain. For box-decoration-break:clone we always apply the end border and
704 * padding since all continuations have them.
706 if ((NS_FRAME_IS_COMPLETE(aStatus
) &&
707 !LastInFlow()->GetNextContinuation() &&
708 !FrameIsNonLastInIBSplit()) ||
709 boxDecorationBreakClone
) {
710 aMetrics
.ISize(lineWM
) += framePadding
.IEnd(frameWM
);
713 nsLayoutUtils::SetBSizeFromFontMetrics(this, aMetrics
, aReflowState
,
714 framePadding
, lineWM
, frameWM
);
716 // For now our overflow area is zero. The real value will be
717 // computed in |nsLineLayout::RelativePositionFrames|.
718 aMetrics
.mOverflowAreas
.Clear();
720 #ifdef NOISY_FINAL_SIZE
722 printf(": metrics=%d,%d ascent=%d\n",
723 aMetrics
.Width(), aMetrics
.Height(), aMetrics
.TopAscent());
728 nsInlineFrame::ReflowInlineFrame(nsPresContext
* aPresContext
,
729 const nsHTMLReflowState
& aReflowState
,
730 InlineReflowState
& irs
,
732 nsReflowStatus
& aStatus
)
734 nsLineLayout
* lineLayout
= aReflowState
.mLineLayout
;
735 bool reflowingFirstLetter
= lineLayout
->GetFirstLetterStyleOK();
737 lineLayout
->ReflowFrame(aFrame
, aStatus
, nullptr, pushedFrame
);
739 if (NS_INLINE_IS_BREAK_BEFORE(aStatus
)) {
740 if (aFrame
!= mFrames
.FirstChild()) {
741 // Change break-before status into break-after since we have
742 // already placed at least one child frame. This preserves the
743 // break-type so that it can be propagated upward.
744 aStatus
= NS_FRAME_NOT_COMPLETE
|
745 NS_INLINE_BREAK
| NS_INLINE_BREAK_AFTER
|
746 (aStatus
& NS_INLINE_BREAK_TYPE_MASK
);
747 PushFrames(aPresContext
, aFrame
, irs
.mPrevFrame
, irs
);
750 // Preserve reflow status when breaking-before our first child
751 // and propagate it upward without modification.
756 // Create a next-in-flow if needed.
757 if (!NS_FRAME_IS_FULLY_COMPLETE(aStatus
)) {
759 nsresult rv
= CreateNextInFlow(aFrame
, newFrame
);
765 if (NS_INLINE_IS_BREAK_AFTER(aStatus
)) {
766 nsIFrame
* nextFrame
= aFrame
->GetNextSibling();
768 NS_FRAME_SET_INCOMPLETE(aStatus
);
769 PushFrames(aPresContext
, nextFrame
, aFrame
, irs
);
772 // We must return an incomplete status if there are more child
773 // frames remaining in a next-in-flow that follows this frame.
774 nsInlineFrame
* nextInFlow
= static_cast<nsInlineFrame
*>(GetNextInFlow());
776 if (nextInFlow
->mFrames
.NotEmpty()) {
777 NS_FRAME_SET_INCOMPLETE(aStatus
);
780 nextInFlow
= static_cast<nsInlineFrame
*>(nextInFlow
->GetNextInFlow());
786 if (!NS_FRAME_IS_FULLY_COMPLETE(aStatus
) && !reflowingFirstLetter
) {
787 nsIFrame
* nextFrame
= aFrame
->GetNextSibling();
789 PushFrames(aPresContext
, nextFrame
, aFrame
, irs
);
795 nsInlineFrame::PullOneFrame(nsPresContext
* aPresContext
,
796 InlineReflowState
& irs
,
799 bool isComplete
= true;
801 nsIFrame
* frame
= nullptr;
802 nsInlineFrame
* nextInFlow
= irs
.mNextInFlow
;
804 frame
= nextInFlow
->mFrames
.FirstChild();
806 // The nextInFlow's principal list has no frames, try its overflow list.
807 nsFrameList
* overflowFrames
= nextInFlow
->GetOverflowFrames();
808 if (overflowFrames
) {
809 frame
= overflowFrames
->RemoveFirstChild();
810 if (overflowFrames
->IsEmpty()) {
811 // We're stealing the only frame - delete the overflow list.
812 nextInFlow
->DestroyOverflowList();
814 // We leave the remaining frames on the overflow list (rather than
815 // putting them on nextInFlow's principal list) so we don't have to
816 // set up the parent for them.
818 // ReparentFloatsForInlineChild needs it to be on a child list -
819 // we remove it again below.
820 nextInFlow
->mFrames
.SetFrames(frame
);
825 // If our block has no next continuation, then any floats belonging to
826 // the pulled frame must belong to our block already. This check ensures
827 // we do no extra work in the common non-vertical-breaking case.
828 if (irs
.mLineContainer
&& irs
.mLineContainer
->GetNextContinuation()) {
829 // The blockChildren.ContainsFrame check performed by
830 // ReparentFloatsForInlineChild will be fast because frame's ancestor
831 // will be the first child of its containing block.
832 ReparentFloatsForInlineChild(irs
.mLineContainer
, frame
, false);
834 nextInFlow
->mFrames
.RemoveFirstChild();
835 // nsFirstLineFrame::PullOneFrame calls ReparentStyleContext.
837 mFrames
.InsertFrame(this, irs
.mPrevFrame
, frame
);
839 if (irs
.mLineLayout
) {
840 irs
.mLineLayout
->SetDirtyNextLine();
842 nsContainerFrame::ReparentFrameView(frame
, nextInFlow
, this);
845 nextInFlow
= static_cast<nsInlineFrame
*>(nextInFlow
->GetNextInFlow());
846 irs
.mNextInFlow
= nextInFlow
;
849 *aIsComplete
= isComplete
;
854 nsInlineFrame::PushFrames(nsPresContext
* aPresContext
,
855 nsIFrame
* aFromChild
,
856 nsIFrame
* aPrevSibling
,
857 InlineReflowState
& aState
)
859 NS_PRECONDITION(aFromChild
, "null pointer");
860 NS_PRECONDITION(aPrevSibling
, "pushing first child");
861 NS_PRECONDITION(aPrevSibling
->GetNextSibling() == aFromChild
, "bad prev sibling");
864 printf("%p pushing aFromChild %p, disconnecting from prev sib %p\n",
865 this, aFromChild
, aPrevSibling
);
868 // Add the frames to our overflow list (let our next in flow drain
869 // our overflow list when it is ready)
870 SetOverflowFrames(mFrames
.RemoveFramesAfter(aPrevSibling
));
871 if (aState
.mLineLayout
) {
872 aState
.mLineLayout
->SetDirtyNextLine();
877 //////////////////////////////////////////////////////////////////////
879 nsIFrame::LogicalSides
880 nsInlineFrame::GetLogicalSkipSides(const nsHTMLReflowState
* aReflowState
) const
882 if (MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak
==
883 NS_STYLE_BOX_DECORATION_BREAK_CLONE
)) {
884 return LogicalSides();
889 nsInlineFrame
* prev
= (nsInlineFrame
*) GetPrevContinuation();
890 if ((GetStateBits() & NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET
) ||
891 (prev
&& (prev
->mRect
.height
|| prev
->mRect
.width
))) {
892 // Prev continuation is not empty therefore we don't render our start
894 skip
|= eLogicalSideBitsIStart
;
897 // If the prev continuation is empty, then go ahead and let our start
898 // edge border render.
902 nsInlineFrame
* next
= (nsInlineFrame
*) GetNextContinuation();
903 if ((GetStateBits() & NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET
) ||
904 (next
&& (next
->mRect
.height
|| next
->mRect
.width
))) {
905 // Next continuation is not empty therefore we don't render our end
907 skip
|= eLogicalSideBitsIEnd
;
910 // If the next continuation is empty, then go ahead and let our end
911 // edge border render.
915 if (GetStateBits() & NS_FRAME_PART_OF_IBSPLIT
) {
916 // All but the last part of an {ib} split should skip the "end" side (as
917 // determined by this frame's direction) and all but the first part of such
918 // a split should skip the "start" side. But figuring out which part of
919 // the split we are involves getting our first continuation, which might be
920 // expensive. So don't bother if we already have the relevant bits set.
921 if (skip
!= LogicalSides(eLogicalSideBitsIBoth
)) {
922 // We're missing one of the skip bits, so check whether we need to set it.
923 // Only get the first continuation once, as an optimization.
924 nsIFrame
* firstContinuation
= FirstContinuation();
925 if (firstContinuation
->FrameIsNonLastInIBSplit()) {
926 skip
|= eLogicalSideBitsIEnd
;
928 if (firstContinuation
->FrameIsNonFirstInIBSplit()) {
929 skip
|= eLogicalSideBitsIStart
;
938 nsInlineFrame::GetLogicalBaseline(mozilla::WritingMode aWritingMode
) const
945 nsInlineFrame::AccessibleType()
947 // Broken image accessibles are created here, because layout
948 // replaces the image or image control frame with an inline frame
949 nsIAtom
*tagAtom
= mContent
->Tag();
950 if (tagAtom
== nsGkAtoms::input
) // Broken <input type=image ... />
951 return a11y::eHTMLButtonType
;
952 if (tagAtom
== nsGkAtoms::img
) // Create accessible for broken <img>
953 return a11y::eHyperTextType
;
955 return a11y::eNoType
;
959 //////////////////////////////////////////////////////////////////////
961 // nsLineFrame implementation
964 NS_NewFirstLineFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
966 return new (aPresShell
) nsFirstLineFrame(aContext
);
969 NS_IMPL_FRAMEARENA_HELPERS(nsFirstLineFrame
)
972 nsFirstLineFrame::Init(nsIContent
* aContent
,
973 nsContainerFrame
* aParent
,
974 nsIFrame
* aPrevInFlow
)
976 nsInlineFrame::Init(aContent
, aParent
, aPrevInFlow
);
978 MOZ_ASSERT(StyleContext()->GetPseudo() == nsCSSPseudoElements::firstLine
);
982 // This frame is a continuation - fixup the style context if aPrevInFlow
983 // is the first-in-flow (the only one with a ::first-line pseudo).
984 if (aPrevInFlow
->StyleContext()->GetPseudo() == nsCSSPseudoElements::firstLine
) {
985 MOZ_ASSERT(FirstInFlow() == aPrevInFlow
);
986 // Create a new style context that is a child of the parent
987 // style context thus removing the ::first-line style. This way
988 // we behave as if an anonymous (unstyled) span was the child
989 // of the parent frame.
990 nsStyleContext
* parentContext
= aParent
->StyleContext();
991 nsRefPtr
<nsStyleContext
> newSC
= PresContext()->StyleSet()->
992 ResolveAnonymousBoxStyle(nsCSSAnonBoxes::mozLineFrame
, parentContext
);
993 SetStyleContext(newSC
);
995 MOZ_ASSERT(FirstInFlow() != aPrevInFlow
);
996 MOZ_ASSERT(aPrevInFlow
->StyleContext()->GetPseudo() ==
997 nsCSSAnonBoxes::mozLineFrame
);
1001 #ifdef DEBUG_FRAME_DUMP
1003 nsFirstLineFrame::GetFrameName(nsAString
& aResult
) const
1005 return MakeFrameName(NS_LITERAL_STRING("Line"), aResult
);
1010 nsFirstLineFrame::GetType() const
1012 return nsGkAtoms::lineFrame
;
1016 nsFirstLineFrame::PullOneFrame(nsPresContext
* aPresContext
, InlineReflowState
& irs
,
1019 nsIFrame
* frame
= nsInlineFrame::PullOneFrame(aPresContext
, irs
, aIsComplete
);
1020 if (frame
&& !GetPrevInFlow()) {
1021 // We are a first-line frame. Fixup the child frames
1022 // style-context that we just pulled.
1023 NS_ASSERTION(frame
->GetParent() == this, "Incorrect parent?");
1024 aPresContext
->RestyleManager()->ReparentStyleContext(frame
);
1025 nsLayoutUtils::MarkDescendantsDirty(frame
);
1031 nsFirstLineFrame::Reflow(nsPresContext
* aPresContext
,
1032 nsHTMLReflowMetrics
& aMetrics
,
1033 const nsHTMLReflowState
& aReflowState
,
1034 nsReflowStatus
& aStatus
)
1036 if (nullptr == aReflowState
.mLineLayout
) {
1037 return; // XXX does this happen? why?
1040 nsIFrame
* lineContainer
= aReflowState
.mLineLayout
->LineContainerFrame();
1042 // Check for an overflow list with our prev-in-flow
1043 nsFirstLineFrame
* prevInFlow
= (nsFirstLineFrame
*)GetPrevInFlow();
1045 AutoFrameListPtr
prevOverflowFrames(aPresContext
,
1046 prevInFlow
->StealOverflowFrames());
1047 if (prevOverflowFrames
) {
1048 // Assign all floats to our block if necessary
1049 if (lineContainer
&& lineContainer
->GetPrevContinuation()) {
1050 ReparentFloatsForInlineChild(lineContainer
,
1051 prevOverflowFrames
->FirstChild(),
1054 const nsFrameList::Slice
& newFrames
=
1055 mFrames
.InsertFrames(this, nullptr, *prevOverflowFrames
);
1056 ReparentChildListStyle(aPresContext
, newFrames
, this);
1060 // It's also possible that we have an overflow list for ourselves.
1061 DrainSelfOverflowList();
1063 // Set our own reflow state (additional state above and beyond
1065 InlineReflowState irs
;
1066 irs
.mPrevFrame
= nullptr;
1067 irs
.mLineContainer
= lineContainer
;
1068 irs
.mLineLayout
= aReflowState
.mLineLayout
;
1069 irs
.mNextInFlow
= (nsInlineFrame
*) GetNextInFlow();
1071 bool wasEmpty
= mFrames
.IsEmpty();
1073 // Try to pull over one frame before starting so that we know
1074 // whether we have an anonymous block or not.
1076 PullOneFrame(aPresContext
, irs
, &complete
);
1079 if (nullptr == GetPrevInFlow()) {
1080 // XXX This is pretty sick, but what we do here is to pull-up, in
1081 // advance, all of the next-in-flows children. We re-resolve their
1082 // style while we are at at it so that when we reflow they have
1085 // All of this is so that text-runs reflow properly.
1086 irs
.mPrevFrame
= mFrames
.LastChild();
1089 nsIFrame
* frame
= PullOneFrame(aPresContext
, irs
, &complete
);
1093 irs
.mPrevFrame
= frame
;
1095 irs
.mPrevFrame
= nullptr;
1098 NS_ASSERTION(!aReflowState
.mLineLayout
->GetInFirstLine(),
1099 "Nested first-line frames? BOGUS");
1100 aReflowState
.mLineLayout
->SetInFirstLine(true);
1101 ReflowFrames(aPresContext
, aReflowState
, irs
, aMetrics
, aStatus
);
1102 aReflowState
.mLineLayout
->SetInFirstLine(false);
1104 ReflowAbsoluteFrames(aPresContext
, aMetrics
, aReflowState
, aStatus
);
1106 // Note: the line layout code will properly compute our overflow state for us
1110 nsFirstLineFrame::PullOverflowsFromPrevInFlow()
1112 nsFirstLineFrame
* prevInFlow
= static_cast<nsFirstLineFrame
*>(GetPrevInFlow());
1114 nsPresContext
* presContext
= PresContext();
1115 AutoFrameListPtr
prevOverflowFrames(presContext
,
1116 prevInFlow
->StealOverflowFrames());
1117 if (prevOverflowFrames
) {
1118 // Assume that our prev-in-flow has the same line container that we do.
1119 const nsFrameList::Slice
& newFrames
=
1120 mFrames
.InsertFrames(this, nullptr, *prevOverflowFrames
);
1121 ReparentChildListStyle(presContext
, newFrames
, this);
1127 nsFirstLineFrame::DrainSelfOverflowList()
1129 AutoFrameListPtr
overflowFrames(PresContext(), StealOverflowFrames());
1130 if (overflowFrames
) {
1131 NS_ASSERTION(mFrames
.NotEmpty(), "overflow list w/o frames");
1133 bool result
= !overflowFrames
->IsEmpty();
1134 const nsFrameList::Slice
& newFrames
=
1135 mFrames
.AppendFrames(nullptr, *overflowFrames
);
1136 ReparentChildListStyle(PresContext(), newFrames
, this);