Bumping manifests a=b2g-bump
[gecko.git] / layout / generic / nsInlineFrame.cpp
blob069e2c45a0146f6efad1e7181900c1ec22d8227e
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"
21 #include "SVGTextFrame.h"
23 #ifdef DEBUG
24 #undef NOISY_PUSHING
25 #endif
27 using namespace mozilla;
28 using namespace mozilla::layout;
31 //////////////////////////////////////////////////////////////////////
33 // Basic nsInlineFrame methods
35 nsInlineFrame*
36 NS_NewInlineFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
38 return new (aPresShell) nsInlineFrame(aContext);
41 NS_IMPL_FRAMEARENA_HELPERS(nsInlineFrame)
43 NS_QUERYFRAME_HEAD(nsInlineFrame)
44 NS_QUERYFRAME_ENTRY(nsInlineFrame)
45 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
47 #ifdef DEBUG_FRAME_DUMP
48 nsresult
49 nsInlineFrame::GetFrameName(nsAString& aResult) const
51 return MakeFrameName(NS_LITERAL_STRING("Inline"), aResult);
53 #endif
55 nsIAtom*
56 nsInlineFrame::GetType() const
58 return nsGkAtoms::inlineFrame;
61 void
62 nsInlineFrame::InvalidateFrame(uint32_t aDisplayItemKey)
64 if (IsSVGText()) {
65 nsIFrame* svgTextFrame =
66 nsLayoutUtils::GetClosestFrameOfType(GetParent(),
67 nsGkAtoms::svgTextFrame);
68 svgTextFrame->InvalidateFrame();
69 return;
71 nsInlineFrameBase::InvalidateFrame(aDisplayItemKey);
74 void
75 nsInlineFrame::InvalidateFrameWithRect(const nsRect& aRect, uint32_t aDisplayItemKey)
77 if (IsSVGText()) {
78 nsIFrame* svgTextFrame =
79 nsLayoutUtils::GetClosestFrameOfType(GetParent(),
80 nsGkAtoms::svgTextFrame);
81 svgTextFrame->InvalidateFrame();
82 return;
84 nsInlineFrameBase::InvalidateFrameWithRect(aRect, aDisplayItemKey);
87 static inline bool
88 IsMarginZero(const nsStyleCoord &aCoord)
90 return aCoord.GetUnit() == eStyleUnit_Auto ||
91 nsLayoutUtils::IsMarginZero(aCoord);
94 /* virtual */ bool
95 nsInlineFrame::IsSelfEmpty()
97 #if 0
98 // I used to think inline frames worked this way, but it seems they
99 // don't. At least not in our codebase.
100 if (GetPresContext()->CompatibilityMode() == eCompatibility_FullStandards) {
101 return false;
103 #endif
104 const nsStyleMargin* margin = StyleMargin();
105 const nsStyleBorder* border = StyleBorder();
106 const nsStylePadding* padding = StylePadding();
107 // XXX Top and bottom removed, since they shouldn't affect things, but this
108 // doesn't really match with nsLineLayout.cpp's setting of
109 // ZeroEffectiveSpanBox, anymore, so what should this really be?
110 bool haveRight =
111 border->GetComputedBorderWidth(NS_SIDE_RIGHT) != 0 ||
112 !nsLayoutUtils::IsPaddingZero(padding->mPadding.GetRight()) ||
113 !IsMarginZero(margin->mMargin.GetRight());
114 bool haveLeft =
115 border->GetComputedBorderWidth(NS_SIDE_LEFT) != 0 ||
116 !nsLayoutUtils::IsPaddingZero(padding->mPadding.GetLeft()) ||
117 !IsMarginZero(margin->mMargin.GetLeft());
118 if (haveLeft || haveRight) {
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 ((GetStateBits() & NS_FRAME_PART_OF_IBSPLIT) &&
122 StyleBorder()->mBoxDecorationBreak ==
123 NS_STYLE_BOX_DECORATION_BREAK_SLICE) {
124 bool haveStart, haveEnd;
125 if (NS_STYLE_DIRECTION_LTR == StyleVisibility()->mDirection) {
126 haveStart = haveLeft;
127 haveEnd = haveRight;
128 } else {
129 haveStart = haveRight;
130 haveEnd = haveLeft;
132 // For ib-split frames, ignore things we know we'll skip in GetSkipSides.
133 // XXXbz should we be doing this for non-ib-split frames too, in a more
134 // general way?
136 // Get the first continuation eagerly, as a performance optimization, to
137 // avoid having to get it twice..
138 nsIFrame* firstCont = FirstContinuation();
139 return
140 (!haveStart || firstCont->FrameIsNonFirstInIBSplit()) &&
141 (!haveEnd || firstCont->FrameIsNonLastInIBSplit());
143 return false;
145 return true;
148 bool
149 nsInlineFrame::IsEmpty()
151 if (!IsSelfEmpty()) {
152 return false;
155 for (nsIFrame *kid = mFrames.FirstChild(); kid; kid = kid->GetNextSibling()) {
156 if (!kid->IsEmpty())
157 return false;
160 return true;
163 nsIFrame::FrameSearchResult
164 nsInlineFrame::PeekOffsetCharacter(bool aForward, int32_t* aOffset,
165 bool aRespectClusters)
167 // Override the implementation in nsFrame, to skip empty inline frames
168 NS_ASSERTION (aOffset && *aOffset <= 1, "aOffset out of range");
169 int32_t startOffset = *aOffset;
170 if (startOffset < 0)
171 startOffset = 1;
172 if (aForward == (startOffset == 0)) {
173 // We're before the frame and moving forward, or after it and moving backwards:
174 // skip to the other side, but keep going.
175 *aOffset = 1 - startOffset;
177 return CONTINUE;
180 void
181 nsInlineFrame::DestroyFrom(nsIFrame* aDestructRoot)
183 nsFrameList* overflowFrames = GetOverflowFrames();
184 if (overflowFrames) {
185 // Fixup the parent pointers for any child frames on the OverflowList.
186 // nsIFrame::DestroyFrom depends on that to find the sticky scroll
187 // container (an ancestor).
188 nsIFrame* lineContainer = nsLayoutUtils::FindNearestBlockAncestor(this);
189 DrainSelfOverflowListInternal(eForDestroy, lineContainer);
191 nsContainerFrame::DestroyFrom(aDestructRoot);
194 nsresult
195 nsInlineFrame::StealFrame(nsIFrame* aChild,
196 bool aForceNormal)
198 if (aChild->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER) &&
199 !aForceNormal) {
200 return nsContainerFrame::StealFrame(aChild, aForceNormal);
203 nsInlineFrame* parent = this;
204 bool removed = false;
205 do {
206 removed = parent->mFrames.StartRemoveFrame(aChild);
207 if (removed) {
208 break;
211 // We didn't find the child in our principal child list.
212 // Maybe it's on the overflow list?
213 nsFrameList* frameList = parent->GetOverflowFrames();
214 if (frameList) {
215 removed = frameList->ContinueRemoveFrame(aChild);
216 if (frameList->IsEmpty()) {
217 parent->DestroyOverflowList();
219 if (removed) {
220 break;
224 // Due to our "lazy reparenting" optimization 'aChild' might not actually
225 // be on any of our child lists, but instead in one of our next-in-flows.
226 parent = static_cast<nsInlineFrame*>(parent->GetNextInFlow());
227 } while (parent);
229 MOZ_ASSERT(removed, "nsInlineFrame::StealFrame: can't find aChild");
230 return removed ? NS_OK : NS_ERROR_UNEXPECTED;
233 void
234 nsInlineFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
235 const nsRect& aDirtyRect,
236 const nsDisplayListSet& aLists)
238 BuildDisplayListForInline(aBuilder, aDirtyRect, aLists);
240 // The sole purpose of this is to trigger display of the selection
241 // window for Named Anchors, which don't have any children and
242 // normally don't have any size, but in Editor we use CSS to display
243 // an image to represent this "hidden" element.
244 if (!mFrames.FirstChild()) {
245 DisplaySelectionOverlay(aBuilder, aLists.Content());
249 //////////////////////////////////////////////////////////////////////
250 // Reflow methods
252 /* virtual */ void
253 nsInlineFrame::AddInlineMinISize(nsRenderingContext *aRenderingContext,
254 nsIFrame::InlineMinISizeData *aData)
256 DoInlineIntrinsicISize(aRenderingContext, aData, nsLayoutUtils::MIN_ISIZE);
259 /* virtual */ void
260 nsInlineFrame::AddInlinePrefISize(nsRenderingContext *aRenderingContext,
261 nsIFrame::InlinePrefISizeData *aData)
263 DoInlineIntrinsicISize(aRenderingContext, aData, nsLayoutUtils::PREF_ISIZE);
266 /* virtual */
267 LogicalSize
268 nsInlineFrame::ComputeSize(nsRenderingContext *aRenderingContext,
269 WritingMode aWM,
270 const LogicalSize& aCBSize,
271 nscoord aAvailableISize,
272 const LogicalSize& aMargin,
273 const LogicalSize& aBorder,
274 const LogicalSize& aPadding,
275 ComputeSizeFlags aFlags)
277 // Inlines and text don't compute size before reflow.
278 return LogicalSize(aWM, NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
281 nsRect
282 nsInlineFrame::ComputeTightBounds(gfxContext* aContext) const
284 // be conservative
285 if (StyleContext()->HasTextDecorationLines()) {
286 return GetVisualOverflowRect();
288 return ComputeSimpleTightBounds(aContext);
291 void
292 nsInlineFrame::ReparentFloatsForInlineChild(nsIFrame* aOurLineContainer,
293 nsIFrame* aFrame,
294 bool aReparentSiblings)
296 // XXXbz this would be better if it took a nsFrameList or a frame
297 // list slice....
298 NS_ASSERTION(aOurLineContainer->GetNextContinuation() ||
299 aOurLineContainer->GetPrevContinuation(),
300 "Don't call this when we have no continuation, it's a waste");
301 if (!aFrame) {
302 NS_ASSERTION(aReparentSiblings, "Why did we get called?");
303 return;
306 nsIFrame* ancestor = aFrame;
307 do {
308 ancestor = ancestor->GetParent();
309 if (!ancestor)
310 return;
311 } while (!ancestor->IsFloatContainingBlock());
313 if (ancestor == aOurLineContainer)
314 return;
316 nsBlockFrame* ourBlock = nsLayoutUtils::GetAsBlock(aOurLineContainer);
317 NS_ASSERTION(ourBlock, "Not a block, but broke vertically?");
318 nsBlockFrame* frameBlock = nsLayoutUtils::GetAsBlock(ancestor);
319 NS_ASSERTION(frameBlock, "ancestor not a block");
321 while (true) {
322 ourBlock->ReparentFloats(aFrame, frameBlock, false);
324 if (!aReparentSiblings)
325 return;
326 nsIFrame* next = aFrame->GetNextSibling();
327 if (!next)
328 return;
329 if (next->GetParent() == aFrame->GetParent()) {
330 aFrame = next;
331 continue;
333 // This is paranoid and will hardly ever get hit ... but we can't actually
334 // trust that the frames in the sibling chain all have the same parent,
335 // because lazy reparenting may be going on. If we find a different
336 // parent we need to redo our analysis.
337 ReparentFloatsForInlineChild(aOurLineContainer, next, aReparentSiblings);
338 return;
342 static void
343 ReparentChildListStyle(nsPresContext* aPresContext,
344 const nsFrameList::Slice& aFrames,
345 nsIFrame* aParentFrame)
347 RestyleManager* restyleManager = aPresContext->RestyleManager();
349 for (nsFrameList::Enumerator e(aFrames); !e.AtEnd(); e.Next()) {
350 NS_ASSERTION(e.get()->GetParent() == aParentFrame, "Bogus parentage");
351 restyleManager->ReparentStyleContext(e.get());
352 nsLayoutUtils::MarkDescendantsDirty(e.get());
356 void
357 nsInlineFrame::Reflow(nsPresContext* aPresContext,
358 nsHTMLReflowMetrics& aMetrics,
359 const nsHTMLReflowState& aReflowState,
360 nsReflowStatus& aStatus)
362 DO_GLOBAL_REFLOW_COUNT("nsInlineFrame");
363 DISPLAY_REFLOW(aPresContext, this, aReflowState, aMetrics, aStatus);
364 if (nullptr == aReflowState.mLineLayout) {
365 NS_ERROR("must have non-null aReflowState.mLineLayout");
366 return;
368 if (IsFrameTreeTooDeep(aReflowState, aMetrics, aStatus)) {
369 return;
372 bool lazilySetParentPointer = false;
374 nsIFrame* lineContainer = aReflowState.mLineLayout->LineContainerFrame();
376 // Check for an overflow list with our prev-in-flow
377 nsInlineFrame* prevInFlow = (nsInlineFrame*)GetPrevInFlow();
378 if (prevInFlow) {
379 AutoFrameListPtr prevOverflowFrames(aPresContext,
380 prevInFlow->StealOverflowFrames());
381 if (prevOverflowFrames) {
382 // When pushing and pulling frames we need to check for whether any
383 // views need to be reparented.
384 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
385 this);
387 // Check if we should do the lazilySetParentPointer optimization.
388 // Only do it in simple cases where we're being reflowed for the
389 // first time, nothing (e.g. bidi resolution) has already given
390 // us children, and there's no next-in-flow, so all our frames
391 // will be taken from prevOverflowFrames.
392 if ((GetStateBits() & NS_FRAME_FIRST_REFLOW) && mFrames.IsEmpty() &&
393 !GetNextInFlow()) {
394 // If our child list is empty, just put the new frames into it.
395 // Note that we don't set the parent pointer for the new frames. Instead wait
396 // to do this until we actually reflow the frame. If the overflow list contains
397 // thousands of frames this is a big performance issue (see bug #5588)
398 mFrames.SetFrames(*prevOverflowFrames);
399 lazilySetParentPointer = true;
400 } else {
401 // Assign all floats to our block if necessary
402 if (lineContainer && lineContainer->GetPrevContinuation()) {
403 ReparentFloatsForInlineChild(lineContainer,
404 prevOverflowFrames->FirstChild(),
405 true);
407 // Insert the new frames at the beginning of the child list
408 // and set their parent pointer
409 const nsFrameList::Slice& newFrames =
410 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
411 // If our prev in flow was under the first continuation of a first-line
412 // frame then we need to reparent the style contexts to remove the
413 // the special first-line styling. In the lazilySetParentPointer case
414 // we reparent the style contexts when we set their parents in
415 // nsInlineFrame::ReflowFrames and nsInlineFrame::ReflowInlineFrame.
416 if (aReflowState.mLineLayout->GetInFirstLine()) {
417 ReparentChildListStyle(aPresContext, newFrames, this);
423 // It's also possible that we have an overflow list for ourselves
424 #ifdef DEBUG
425 if (GetStateBits() & NS_FRAME_FIRST_REFLOW) {
426 // If it's our initial reflow, then we should not have an overflow list.
427 // However, add an assertion in case we get reflowed more than once with
428 // the initial reflow reason
429 nsFrameList* overflowFrames = GetOverflowFrames();
430 NS_ASSERTION(!overflowFrames || overflowFrames->IsEmpty(),
431 "overflow list is not empty for initial reflow");
433 #endif
434 if (!(GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
435 DrainFlags flags =
436 lazilySetParentPointer ? eDontReparentFrames : DrainFlags(0);
437 if (aReflowState.mLineLayout->GetInFirstLine()) {
438 flags = DrainFlags(flags | eInFirstLine);
440 DrainSelfOverflowListInternal(flags, lineContainer);
443 // Set our own reflow state (additional state above and beyond
444 // aReflowState)
445 InlineReflowState irs;
446 irs.mPrevFrame = nullptr;
447 irs.mLineContainer = lineContainer;
448 irs.mLineLayout = aReflowState.mLineLayout;
449 irs.mNextInFlow = (nsInlineFrame*) GetNextInFlow();
450 irs.mSetParentPointer = lazilySetParentPointer;
452 if (mFrames.IsEmpty()) {
453 // Try to pull over one frame before starting so that we know
454 // whether we have an anonymous block or not.
455 bool complete;
456 (void) PullOneFrame(aPresContext, irs, &complete);
459 ReflowFrames(aPresContext, aReflowState, irs, aMetrics, aStatus);
461 ReflowAbsoluteFrames(aPresContext, aMetrics, aReflowState, aStatus);
463 // Note: the line layout code will properly compute our
464 // overflow-rect state for us.
466 NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aMetrics);
469 nsresult
470 nsInlineFrame::AttributeChanged(int32_t aNameSpaceID,
471 nsIAtom* aAttribute,
472 int32_t aModType)
474 nsresult rv =
475 nsInlineFrameBase::AttributeChanged(aNameSpaceID, aAttribute, aModType);
477 if (NS_FAILED(rv)) {
478 return rv;
481 if (IsSVGText()) {
482 SVGTextFrame* f = static_cast<SVGTextFrame*>(
483 nsLayoutUtils::GetClosestFrameOfType(this, nsGkAtoms::svgTextFrame));
484 f->HandleAttributeChangeInDescendant(mContent->AsElement(),
485 aNameSpaceID, aAttribute);
488 return NS_OK;
491 bool
492 nsInlineFrame::DrainSelfOverflowListInternal(DrainFlags aFlags,
493 nsIFrame* aLineContainer)
495 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
496 if (overflowFrames) {
497 // The frames on our own overflowlist may have been pushed by a
498 // previous lazilySetParentPointer Reflow so we need to ensure the
499 // correct parent pointer. This is sometimes skipped by Reflow.
500 if (!(aFlags & eDontReparentFrames)) {
501 nsIFrame* firstChild = overflowFrames->FirstChild();
502 if (aLineContainer && aLineContainer->GetPrevContinuation()) {
503 ReparentFloatsForInlineChild(aLineContainer, firstChild, true);
505 const bool doReparentSC =
506 (aFlags & eInFirstLine) && !(aFlags & eForDestroy);
507 RestyleManager* restyleManager = PresContext()->RestyleManager();
508 for (nsIFrame* f = firstChild; f; f = f->GetNextSibling()) {
509 f->SetParent(this);
510 if (doReparentSC) {
511 restyleManager->ReparentStyleContext(f);
512 nsLayoutUtils::MarkDescendantsDirty(f);
516 bool result = !overflowFrames->IsEmpty();
517 mFrames.AppendFrames(nullptr, *overflowFrames);
518 return result;
520 return false;
523 /* virtual */ bool
524 nsInlineFrame::DrainSelfOverflowList()
526 nsIFrame* lineContainer = nsLayoutUtils::FindNearestBlockAncestor(this);
527 // Add the eInFirstLine flag if we have a ::first-line ancestor frame.
528 // No need to look further than the nearest line container though.
529 DrainFlags flags = DrainFlags(0);
530 for (nsIFrame* p = GetParent(); p != lineContainer; p = p->GetParent()) {
531 if (p->GetType() == nsGkAtoms::lineFrame) {
532 flags = DrainFlags(flags | eInFirstLine);
533 break;
536 return DrainSelfOverflowListInternal(flags, lineContainer);
539 /* virtual */ bool
540 nsInlineFrame::CanContinueTextRun() const
542 // We can continue a text run through an inline frame
543 return true;
546 /* virtual */ void
547 nsInlineFrame::PullOverflowsFromPrevInFlow()
549 nsInlineFrame* prevInFlow = static_cast<nsInlineFrame*>(GetPrevInFlow());
550 if (prevInFlow) {
551 nsPresContext* presContext = PresContext();
552 AutoFrameListPtr prevOverflowFrames(presContext,
553 prevInFlow->StealOverflowFrames());
554 if (prevOverflowFrames) {
555 // Assume that our prev-in-flow has the same line container that we do.
556 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
557 this);
558 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
563 void
564 nsInlineFrame::ReflowFrames(nsPresContext* aPresContext,
565 const nsHTMLReflowState& aReflowState,
566 InlineReflowState& irs,
567 nsHTMLReflowMetrics& aMetrics,
568 nsReflowStatus& aStatus)
570 aStatus = NS_FRAME_COMPLETE;
572 nsLineLayout* lineLayout = aReflowState.mLineLayout;
573 bool inFirstLine = aReflowState.mLineLayout->GetInFirstLine();
574 RestyleManager* restyleManager = aPresContext->RestyleManager();
575 WritingMode frameWM = aReflowState.GetWritingMode();
576 WritingMode lineWM = aReflowState.mLineLayout->mRootSpan->mWritingMode;
577 LogicalMargin framePadding = aReflowState.ComputedLogicalBorderPadding();
578 nscoord startEdge = 0;
579 const bool boxDecorationBreakClone =
580 MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak ==
581 NS_STYLE_BOX_DECORATION_BREAK_CLONE);
582 // Don't offset by our start borderpadding if we have a prev continuation or
583 // if we're in a part of an {ib} split other than the first one. For
584 // box-decoration-break:clone we always offset our start since all
585 // continuations have border/padding.
586 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
587 boxDecorationBreakClone) {
588 startEdge = framePadding.IStart(frameWM);
590 nscoord availableISize = aReflowState.AvailableISize();
591 NS_ASSERTION(availableISize != NS_UNCONSTRAINEDSIZE,
592 "should no longer use available widths");
593 // Subtract off inline axis border+padding from availableISize
594 availableISize -= startEdge;
595 availableISize -= framePadding.IEnd(frameWM);
596 lineLayout->BeginSpan(this, &aReflowState, startEdge,
597 startEdge + availableISize, &mBaseline);
599 // First reflow our principal children.
600 nsIFrame* frame = mFrames.FirstChild();
601 bool done = false;
602 while (frame) {
603 // Check if we should lazily set the child frame's parent pointer.
604 if (irs.mSetParentPointer) {
605 bool havePrevBlock =
606 irs.mLineContainer && irs.mLineContainer->GetPrevContinuation();
607 nsIFrame* child = frame;
608 do {
609 // If our block is the first in flow, then any floats under the pulled
610 // frame must already belong to our block.
611 if (havePrevBlock) {
612 // This has to happen before we update frame's parent; we need to
613 // know frame's ancestry under its old block.
614 // The blockChildren.ContainsFrame check performed by
615 // ReparentFloatsForInlineChild here may be slow, but we can't
616 // easily avoid it because we don't know where 'frame' originally
617 // came from. If we really really have to optimize this we could
618 // cache whether frame->GetParent() is under its containing blocks
619 // overflowList or not.
620 ReparentFloatsForInlineChild(irs.mLineContainer, child, false);
622 child->SetParent(this);
623 if (inFirstLine) {
624 restyleManager->ReparentStyleContext(child);
625 nsLayoutUtils::MarkDescendantsDirty(child);
627 // We also need to do the same for |frame|'s next-in-flows that are in
628 // the sibling list. Otherwise, if we reflow |frame| and it's complete
629 // we'll crash when trying to delete its next-in-flow.
630 // This scenario doesn't happen often, but it can happen.
631 nsIFrame* nextSibling = child->GetNextSibling();
632 child = child->GetNextInFlow();
633 if (MOZ_UNLIKELY(child)) {
634 while (child != nextSibling && nextSibling) {
635 nextSibling = nextSibling->GetNextSibling();
637 if (!nextSibling) {
638 child = nullptr;
641 MOZ_ASSERT(!child || mFrames.ContainsFrame(child));
642 } while (child);
644 // Fix the parent pointer for ::first-letter child frame next-in-flows,
645 // so nsFirstLetterFrame::Reflow can destroy them safely (bug 401042).
646 nsIFrame* realFrame = nsPlaceholderFrame::GetRealFrameFor(frame);
647 if (realFrame->GetType() == nsGkAtoms::letterFrame) {
648 nsIFrame* child = realFrame->GetFirstPrincipalChild();
649 if (child) {
650 NS_ASSERTION(child->GetType() == nsGkAtoms::textFrame,
651 "unexpected frame type");
652 nsIFrame* nextInFlow = child->GetNextInFlow();
653 for ( ; nextInFlow; nextInFlow = nextInFlow->GetNextInFlow()) {
654 NS_ASSERTION(nextInFlow->GetType() == nsGkAtoms::textFrame,
655 "unexpected frame type");
656 if (mFrames.ContainsFrame(nextInFlow)) {
657 nextInFlow->SetParent(this);
658 if (inFirstLine) {
659 restyleManager->ReparentStyleContext(nextInFlow);
660 nsLayoutUtils::MarkDescendantsDirty(nextInFlow);
663 else {
664 #ifdef DEBUG
665 // Once we find a next-in-flow that isn't ours none of the
666 // remaining next-in-flows should be either.
667 for ( ; nextInFlow; nextInFlow = nextInFlow->GetNextInFlow()) {
668 NS_ASSERTION(!mFrames.ContainsFrame(nextInFlow),
669 "unexpected letter frame flow");
671 #endif
672 break;
678 MOZ_ASSERT(frame->GetParent() == this);
680 if (!done) {
681 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
682 ReflowInlineFrame(aPresContext, aReflowState, irs, frame, aStatus);
683 done = NS_INLINE_IS_BREAK(aStatus) ||
684 (!reflowingFirstLetter && NS_FRAME_IS_NOT_COMPLETE(aStatus));
685 if (done) {
686 if (!irs.mSetParentPointer) {
687 break;
689 // Keep reparenting the remaining siblings, but don't reflow them.
690 nsFrameList* pushedFrames = GetOverflowFrames();
691 if (pushedFrames && pushedFrames->FirstChild() == frame) {
692 // Don't bother if |frame| was pushed to our overflow list.
693 break;
695 } else {
696 irs.mPrevFrame = frame;
699 frame = frame->GetNextSibling();
702 // Attempt to pull frames from our next-in-flow until we can't
703 if (!done && GetNextInFlow()) {
704 while (true) {
705 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
706 bool isComplete;
707 if (!frame) { // Could be non-null if we pulled a first-letter frame and
708 // it created a continuation, since we don't push those.
709 frame = PullOneFrame(aPresContext, irs, &isComplete);
711 #ifdef NOISY_PUSHING
712 printf("%p pulled up %p\n", this, frame);
713 #endif
714 if (nullptr == frame) {
715 if (!isComplete) {
716 aStatus = NS_FRAME_NOT_COMPLETE;
718 break;
720 ReflowInlineFrame(aPresContext, aReflowState, irs, frame, aStatus);
721 if (NS_INLINE_IS_BREAK(aStatus) ||
722 (!reflowingFirstLetter && NS_FRAME_IS_NOT_COMPLETE(aStatus))) {
723 break;
725 irs.mPrevFrame = frame;
726 frame = frame->GetNextSibling();
730 NS_ASSERTION(!NS_FRAME_IS_COMPLETE(aStatus) || !GetOverflowFrames(),
731 "We can't be complete AND have overflow frames!");
733 // If after reflowing our children they take up no area then make
734 // sure that we don't either.
736 // Note: CSS demands that empty inline elements still affect the
737 // line-height calculations. However, continuations of an inline
738 // that are empty we force to empty so that things like collapsed
739 // whitespace in an inline element don't affect the line-height.
740 aMetrics.ISize(lineWM) = lineLayout->EndSpan(this);
742 // Compute final width.
744 // XXX Note that that the padding start and end are in the frame's
745 // writing mode, but the metrics' inline-size is in the line's
746 // writing mode. This makes sense if the line and frame are both
747 // vertical or both horizontal, but what should happen with
748 // orthogonal inlines?
750 // Make sure to not include our start border and padding if we have a prev
751 // continuation or if we're in a part of an {ib} split other than the first
752 // one. For box-decoration-break:clone we always include our start border
753 // and padding since all continuations have them.
754 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
755 boxDecorationBreakClone) {
756 aMetrics.ISize(lineWM) += framePadding.IStart(frameWM);
760 * We want to only apply the end border and padding if we're the last
761 * continuation and either not in an {ib} split or the last part of it. To
762 * be the last continuation we have to be complete (so that we won't get a
763 * next-in-flow) and have no non-fluid continuations on our continuation
764 * chain. For box-decoration-break:clone we always apply the end border and
765 * padding since all continuations have them.
767 if ((NS_FRAME_IS_COMPLETE(aStatus) &&
768 !LastInFlow()->GetNextContinuation() &&
769 !FrameIsNonLastInIBSplit()) ||
770 boxDecorationBreakClone) {
771 aMetrics.ISize(lineWM) += framePadding.IEnd(frameWM);
774 nsLayoutUtils::SetBSizeFromFontMetrics(this, aMetrics, aReflowState,
775 framePadding, lineWM, frameWM);
777 // For now our overflow area is zero. The real value will be
778 // computed in |nsLineLayout::RelativePositionFrames|.
779 aMetrics.mOverflowAreas.Clear();
781 #ifdef NOISY_FINAL_SIZE
782 ListTag(stdout);
783 printf(": metrics=%d,%d ascent=%d\n",
784 aMetrics.Width(), aMetrics.Height(), aMetrics.TopAscent());
785 #endif
788 void
789 nsInlineFrame::ReflowInlineFrame(nsPresContext* aPresContext,
790 const nsHTMLReflowState& aReflowState,
791 InlineReflowState& irs,
792 nsIFrame* aFrame,
793 nsReflowStatus& aStatus)
795 nsLineLayout* lineLayout = aReflowState.mLineLayout;
796 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
797 bool pushedFrame;
798 lineLayout->ReflowFrame(aFrame, aStatus, nullptr, pushedFrame);
800 if (NS_INLINE_IS_BREAK_BEFORE(aStatus)) {
801 if (aFrame != mFrames.FirstChild()) {
802 // Change break-before status into break-after since we have
803 // already placed at least one child frame. This preserves the
804 // break-type so that it can be propagated upward.
805 aStatus = NS_FRAME_NOT_COMPLETE |
806 NS_INLINE_BREAK | NS_INLINE_BREAK_AFTER |
807 (aStatus & NS_INLINE_BREAK_TYPE_MASK);
808 PushFrames(aPresContext, aFrame, irs.mPrevFrame, irs);
810 else {
811 // Preserve reflow status when breaking-before our first child
812 // and propagate it upward without modification.
814 return;
817 // Create a next-in-flow if needed.
818 if (!NS_FRAME_IS_FULLY_COMPLETE(aStatus)) {
819 CreateNextInFlow(aFrame);
822 if (NS_INLINE_IS_BREAK_AFTER(aStatus)) {
823 nsIFrame* nextFrame = aFrame->GetNextSibling();
824 if (nextFrame) {
825 NS_FRAME_SET_INCOMPLETE(aStatus);
826 PushFrames(aPresContext, nextFrame, aFrame, irs);
828 else {
829 // We must return an incomplete status if there are more child
830 // frames remaining in a next-in-flow that follows this frame.
831 nsInlineFrame* nextInFlow = static_cast<nsInlineFrame*>(GetNextInFlow());
832 while (nextInFlow) {
833 if (nextInFlow->mFrames.NotEmpty()) {
834 NS_FRAME_SET_INCOMPLETE(aStatus);
835 break;
837 nextInFlow = static_cast<nsInlineFrame*>(nextInFlow->GetNextInFlow());
840 return;
843 if (!NS_FRAME_IS_FULLY_COMPLETE(aStatus) && !reflowingFirstLetter) {
844 nsIFrame* nextFrame = aFrame->GetNextSibling();
845 if (nextFrame) {
846 PushFrames(aPresContext, nextFrame, aFrame, irs);
851 nsIFrame*
852 nsInlineFrame::PullOneFrame(nsPresContext* aPresContext,
853 InlineReflowState& irs,
854 bool* aIsComplete)
856 bool isComplete = true;
858 nsIFrame* frame = nullptr;
859 nsInlineFrame* nextInFlow = irs.mNextInFlow;
860 while (nextInFlow) {
861 frame = nextInFlow->mFrames.FirstChild();
862 if (!frame) {
863 // The nextInFlow's principal list has no frames, try its overflow list.
864 nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
865 if (overflowFrames) {
866 frame = overflowFrames->RemoveFirstChild();
867 if (overflowFrames->IsEmpty()) {
868 // We're stealing the only frame - delete the overflow list.
869 nextInFlow->DestroyOverflowList();
870 } else {
871 // We leave the remaining frames on the overflow list (rather than
872 // putting them on nextInFlow's principal list) so we don't have to
873 // set up the parent for them.
875 // ReparentFloatsForInlineChild needs it to be on a child list -
876 // we remove it again below.
877 nextInFlow->mFrames.SetFrames(frame);
881 if (frame) {
882 // If our block has no next continuation, then any floats belonging to
883 // the pulled frame must belong to our block already. This check ensures
884 // we do no extra work in the common non-vertical-breaking case.
885 if (irs.mLineContainer && irs.mLineContainer->GetNextContinuation()) {
886 // The blockChildren.ContainsFrame check performed by
887 // ReparentFloatsForInlineChild will be fast because frame's ancestor
888 // will be the first child of its containing block.
889 ReparentFloatsForInlineChild(irs.mLineContainer, frame, false);
891 nextInFlow->mFrames.RemoveFirstChild();
892 // nsFirstLineFrame::PullOneFrame calls ReparentStyleContext.
894 mFrames.InsertFrame(this, irs.mPrevFrame, frame);
895 isComplete = false;
896 if (irs.mLineLayout) {
897 irs.mLineLayout->SetDirtyNextLine();
899 nsContainerFrame::ReparentFrameView(frame, nextInFlow, this);
900 break;
902 nextInFlow = static_cast<nsInlineFrame*>(nextInFlow->GetNextInFlow());
903 irs.mNextInFlow = nextInFlow;
906 *aIsComplete = isComplete;
907 return frame;
910 void
911 nsInlineFrame::PushFrames(nsPresContext* aPresContext,
912 nsIFrame* aFromChild,
913 nsIFrame* aPrevSibling,
914 InlineReflowState& aState)
916 NS_PRECONDITION(aFromChild, "null pointer");
917 NS_PRECONDITION(aPrevSibling, "pushing first child");
918 NS_PRECONDITION(aPrevSibling->GetNextSibling() == aFromChild, "bad prev sibling");
920 #ifdef NOISY_PUSHING
921 printf("%p pushing aFromChild %p, disconnecting from prev sib %p\n",
922 this, aFromChild, aPrevSibling);
923 #endif
925 // Add the frames to our overflow list (let our next in flow drain
926 // our overflow list when it is ready)
927 SetOverflowFrames(mFrames.RemoveFramesAfter(aPrevSibling));
928 if (aState.mLineLayout) {
929 aState.mLineLayout->SetDirtyNextLine();
934 //////////////////////////////////////////////////////////////////////
936 nsIFrame::LogicalSides
937 nsInlineFrame::GetLogicalSkipSides(const nsHTMLReflowState* aReflowState) const
939 if (MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak ==
940 NS_STYLE_BOX_DECORATION_BREAK_CLONE)) {
941 return LogicalSides();
944 LogicalSides skip;
945 if (!IsFirst()) {
946 nsInlineFrame* prev = (nsInlineFrame*) GetPrevContinuation();
947 if ((GetStateBits() & NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
948 (prev && (prev->mRect.height || prev->mRect.width))) {
949 // Prev continuation is not empty therefore we don't render our start
950 // border edge.
951 skip |= eLogicalSideBitsIStart;
953 else {
954 // If the prev continuation is empty, then go ahead and let our start
955 // edge border render.
958 if (!IsLast()) {
959 nsInlineFrame* next = (nsInlineFrame*) GetNextContinuation();
960 if ((GetStateBits() & NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
961 (next && (next->mRect.height || next->mRect.width))) {
962 // Next continuation is not empty therefore we don't render our end
963 // border edge.
964 skip |= eLogicalSideBitsIEnd;
966 else {
967 // If the next continuation is empty, then go ahead and let our end
968 // edge border render.
972 if (GetStateBits() & NS_FRAME_PART_OF_IBSPLIT) {
973 // All but the last part of an {ib} split should skip the "end" side (as
974 // determined by this frame's direction) and all but the first part of such
975 // a split should skip the "start" side. But figuring out which part of
976 // the split we are involves getting our first continuation, which might be
977 // expensive. So don't bother if we already have the relevant bits set.
978 if (skip != LogicalSides(eLogicalSideBitsIBoth)) {
979 // We're missing one of the skip bits, so check whether we need to set it.
980 // Only get the first continuation once, as an optimization.
981 nsIFrame* firstContinuation = FirstContinuation();
982 if (firstContinuation->FrameIsNonLastInIBSplit()) {
983 skip |= eLogicalSideBitsIEnd;
985 if (firstContinuation->FrameIsNonFirstInIBSplit()) {
986 skip |= eLogicalSideBitsIStart;
991 return skip;
994 nscoord
995 nsInlineFrame::GetLogicalBaseline(mozilla::WritingMode aWritingMode) const
997 return mBaseline;
1000 #ifdef ACCESSIBILITY
1001 a11y::AccType
1002 nsInlineFrame::AccessibleType()
1004 // Broken image accessibles are created here, because layout
1005 // replaces the image or image control frame with an inline frame
1006 nsIAtom *tagAtom = mContent->Tag();
1007 if (tagAtom == nsGkAtoms::input) // Broken <input type=image ... />
1008 return a11y::eHTMLButtonType;
1009 if (tagAtom == nsGkAtoms::img) // Create accessible for broken <img>
1010 return a11y::eHyperTextType;
1012 return a11y::eNoType;
1014 #endif
1016 //////////////////////////////////////////////////////////////////////
1018 // nsLineFrame implementation
1020 nsFirstLineFrame*
1021 NS_NewFirstLineFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
1023 return new (aPresShell) nsFirstLineFrame(aContext);
1026 NS_IMPL_FRAMEARENA_HELPERS(nsFirstLineFrame)
1028 void
1029 nsFirstLineFrame::Init(nsIContent* aContent,
1030 nsContainerFrame* aParent,
1031 nsIFrame* aPrevInFlow)
1033 nsInlineFrame::Init(aContent, aParent, aPrevInFlow);
1034 if (!aPrevInFlow) {
1035 MOZ_ASSERT(StyleContext()->GetPseudo() == nsCSSPseudoElements::firstLine);
1036 return;
1039 // This frame is a continuation - fixup the style context if aPrevInFlow
1040 // is the first-in-flow (the only one with a ::first-line pseudo).
1041 if (aPrevInFlow->StyleContext()->GetPseudo() == nsCSSPseudoElements::firstLine) {
1042 MOZ_ASSERT(FirstInFlow() == aPrevInFlow);
1043 // Create a new style context that is a child of the parent
1044 // style context thus removing the ::first-line style. This way
1045 // we behave as if an anonymous (unstyled) span was the child
1046 // of the parent frame.
1047 nsStyleContext* parentContext = aParent->StyleContext();
1048 nsRefPtr<nsStyleContext> newSC = PresContext()->StyleSet()->
1049 ResolveAnonymousBoxStyle(nsCSSAnonBoxes::mozLineFrame, parentContext);
1050 SetStyleContext(newSC);
1051 } else {
1052 MOZ_ASSERT(FirstInFlow() != aPrevInFlow);
1053 MOZ_ASSERT(aPrevInFlow->StyleContext()->GetPseudo() ==
1054 nsCSSAnonBoxes::mozLineFrame);
1058 #ifdef DEBUG_FRAME_DUMP
1059 nsresult
1060 nsFirstLineFrame::GetFrameName(nsAString& aResult) const
1062 return MakeFrameName(NS_LITERAL_STRING("Line"), aResult);
1064 #endif
1066 nsIAtom*
1067 nsFirstLineFrame::GetType() const
1069 return nsGkAtoms::lineFrame;
1072 nsIFrame*
1073 nsFirstLineFrame::PullOneFrame(nsPresContext* aPresContext, InlineReflowState& irs,
1074 bool* aIsComplete)
1076 nsIFrame* frame = nsInlineFrame::PullOneFrame(aPresContext, irs, aIsComplete);
1077 if (frame && !GetPrevInFlow()) {
1078 // We are a first-line frame. Fixup the child frames
1079 // style-context that we just pulled.
1080 NS_ASSERTION(frame->GetParent() == this, "Incorrect parent?");
1081 aPresContext->RestyleManager()->ReparentStyleContext(frame);
1082 nsLayoutUtils::MarkDescendantsDirty(frame);
1084 return frame;
1087 void
1088 nsFirstLineFrame::Reflow(nsPresContext* aPresContext,
1089 nsHTMLReflowMetrics& aMetrics,
1090 const nsHTMLReflowState& aReflowState,
1091 nsReflowStatus& aStatus)
1093 if (nullptr == aReflowState.mLineLayout) {
1094 return; // XXX does this happen? why?
1097 nsIFrame* lineContainer = aReflowState.mLineLayout->LineContainerFrame();
1099 // Check for an overflow list with our prev-in-flow
1100 nsFirstLineFrame* prevInFlow = (nsFirstLineFrame*)GetPrevInFlow();
1101 if (prevInFlow) {
1102 AutoFrameListPtr prevOverflowFrames(aPresContext,
1103 prevInFlow->StealOverflowFrames());
1104 if (prevOverflowFrames) {
1105 // Assign all floats to our block if necessary
1106 if (lineContainer && lineContainer->GetPrevContinuation()) {
1107 ReparentFloatsForInlineChild(lineContainer,
1108 prevOverflowFrames->FirstChild(),
1109 true);
1111 const nsFrameList::Slice& newFrames =
1112 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
1113 ReparentChildListStyle(aPresContext, newFrames, this);
1117 // It's also possible that we have an overflow list for ourselves.
1118 DrainSelfOverflowList();
1120 // Set our own reflow state (additional state above and beyond
1121 // aReflowState)
1122 InlineReflowState irs;
1123 irs.mPrevFrame = nullptr;
1124 irs.mLineContainer = lineContainer;
1125 irs.mLineLayout = aReflowState.mLineLayout;
1126 irs.mNextInFlow = (nsInlineFrame*) GetNextInFlow();
1128 bool wasEmpty = mFrames.IsEmpty();
1129 if (wasEmpty) {
1130 // Try to pull over one frame before starting so that we know
1131 // whether we have an anonymous block or not.
1132 bool complete;
1133 PullOneFrame(aPresContext, irs, &complete);
1136 if (nullptr == GetPrevInFlow()) {
1137 // XXX This is pretty sick, but what we do here is to pull-up, in
1138 // advance, all of the next-in-flows children. We re-resolve their
1139 // style while we are at at it so that when we reflow they have
1140 // the right style.
1142 // All of this is so that text-runs reflow properly.
1143 irs.mPrevFrame = mFrames.LastChild();
1144 for (;;) {
1145 bool complete;
1146 nsIFrame* frame = PullOneFrame(aPresContext, irs, &complete);
1147 if (!frame) {
1148 break;
1150 irs.mPrevFrame = frame;
1152 irs.mPrevFrame = nullptr;
1155 NS_ASSERTION(!aReflowState.mLineLayout->GetInFirstLine(),
1156 "Nested first-line frames? BOGUS");
1157 aReflowState.mLineLayout->SetInFirstLine(true);
1158 ReflowFrames(aPresContext, aReflowState, irs, aMetrics, aStatus);
1159 aReflowState.mLineLayout->SetInFirstLine(false);
1161 ReflowAbsoluteFrames(aPresContext, aMetrics, aReflowState, aStatus);
1163 // Note: the line layout code will properly compute our overflow state for us
1166 /* virtual */ void
1167 nsFirstLineFrame::PullOverflowsFromPrevInFlow()
1169 nsFirstLineFrame* prevInFlow = static_cast<nsFirstLineFrame*>(GetPrevInFlow());
1170 if (prevInFlow) {
1171 nsPresContext* presContext = PresContext();
1172 AutoFrameListPtr prevOverflowFrames(presContext,
1173 prevInFlow->StealOverflowFrames());
1174 if (prevOverflowFrames) {
1175 // Assume that our prev-in-flow has the same line container that we do.
1176 const nsFrameList::Slice& newFrames =
1177 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
1178 ReparentChildListStyle(presContext, newFrames, this);
1183 /* virtual */ bool
1184 nsFirstLineFrame::DrainSelfOverflowList()
1186 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
1187 if (overflowFrames) {
1188 bool result = !overflowFrames->IsEmpty();
1189 const nsFrameList::Slice& newFrames =
1190 mFrames.AppendFrames(nullptr, *overflowFrames);
1191 ReparentChildListStyle(PresContext(), newFrames, this);
1192 return result;
1194 return false;