Bumping manifests a=b2g-bump
[gecko.git] / layout / generic / nsInlineFrame.cpp
blobb53f97e5c944552648b5df1ca5afcfa8435b8e56
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"
22 #ifdef DEBUG
23 #undef NOISY_PUSHING
24 #endif
26 using namespace mozilla;
27 using namespace mozilla::layout;
30 //////////////////////////////////////////////////////////////////////
32 // Basic nsInlineFrame methods
34 nsInlineFrame*
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
47 nsresult
48 nsInlineFrame::GetFrameName(nsAString& aResult) const
50 return MakeFrameName(NS_LITERAL_STRING("Inline"), aResult);
52 #endif
54 nsIAtom*
55 nsInlineFrame::GetType() const
57 return nsGkAtoms::inlineFrame;
60 void
61 nsInlineFrame::InvalidateFrame(uint32_t aDisplayItemKey)
63 if (IsSVGText()) {
64 nsIFrame* svgTextFrame =
65 nsLayoutUtils::GetClosestFrameOfType(GetParent(),
66 nsGkAtoms::svgTextFrame);
67 svgTextFrame->InvalidateFrame();
68 return;
70 nsInlineFrameBase::InvalidateFrame(aDisplayItemKey);
73 void
74 nsInlineFrame::InvalidateFrameWithRect(const nsRect& aRect, uint32_t aDisplayItemKey)
76 if (IsSVGText()) {
77 nsIFrame* svgTextFrame =
78 nsLayoutUtils::GetClosestFrameOfType(GetParent(),
79 nsGkAtoms::svgTextFrame);
80 svgTextFrame->InvalidateFrame();
81 return;
83 nsInlineFrameBase::InvalidateFrameWithRect(aRect, aDisplayItemKey);
86 static inline bool
87 IsMarginZero(const nsStyleCoord &aCoord)
89 return aCoord.GetUnit() == eStyleUnit_Auto ||
90 nsLayoutUtils::IsMarginZero(aCoord);
93 /* virtual */ bool
94 nsInlineFrame::IsSelfEmpty()
96 #if 0
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) {
100 return false;
102 #endif
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?
109 bool haveRight =
110 border->GetComputedBorderWidth(NS_SIDE_RIGHT) != 0 ||
111 !nsLayoutUtils::IsPaddingZero(padding->mPadding.GetRight()) ||
112 !IsMarginZero(margin->mMargin.GetRight());
113 bool haveLeft =
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;
126 haveEnd = haveRight;
127 } else {
128 haveStart = haveRight;
129 haveEnd = haveLeft;
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
133 // general way?
135 // Get the first continuation eagerly, as a performance optimization, to
136 // avoid having to get it twice..
137 nsIFrame* firstCont = FirstContinuation();
138 return
139 (!haveStart || firstCont->FrameIsNonFirstInIBSplit()) &&
140 (!haveEnd || firstCont->FrameIsNonLastInIBSplit());
142 return false;
144 return true;
147 bool
148 nsInlineFrame::IsEmpty()
150 if (!IsSelfEmpty()) {
151 return false;
154 for (nsIFrame *kid = mFrames.FirstChild(); kid; kid = kid->GetNextSibling()) {
155 if (!kid->IsEmpty())
156 return false;
159 return true;
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;
169 if (startOffset < 0)
170 startOffset = 1;
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;
176 return CONTINUE;
179 void
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);
193 void
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 //////////////////////////////////////////////////////////////////////
210 // Reflow methods
212 /* virtual */ void
213 nsInlineFrame::AddInlineMinISize(nsRenderingContext *aRenderingContext,
214 nsIFrame::InlineMinISizeData *aData)
216 DoInlineIntrinsicISize(aRenderingContext, aData, nsLayoutUtils::MIN_ISIZE);
219 /* virtual */ void
220 nsInlineFrame::AddInlinePrefISize(nsRenderingContext *aRenderingContext,
221 nsIFrame::InlinePrefISizeData *aData)
223 DoInlineIntrinsicISize(aRenderingContext, aData, nsLayoutUtils::PREF_ISIZE);
226 /* virtual */
227 LogicalSize
228 nsInlineFrame::ComputeSize(nsRenderingContext *aRenderingContext,
229 WritingMode aWM,
230 const LogicalSize& aCBSize,
231 nscoord aAvailableISize,
232 const LogicalSize& aMargin,
233 const LogicalSize& aBorder,
234 const LogicalSize& aPadding,
235 uint32_t aFlags)
237 // Inlines and text don't compute size before reflow.
238 return LogicalSize(aWM, NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
241 nsRect
242 nsInlineFrame::ComputeTightBounds(gfxContext* aContext) const
244 // be conservative
245 if (StyleContext()->HasTextDecorationLines()) {
246 return GetVisualOverflowRect();
248 return ComputeSimpleTightBounds(aContext);
251 void
252 nsInlineFrame::ReparentFloatsForInlineChild(nsIFrame* aOurLineContainer,
253 nsIFrame* aFrame,
254 bool aReparentSiblings)
256 // XXXbz this would be better if it took a nsFrameList or a frame
257 // list slice....
258 NS_ASSERTION(aOurLineContainer->GetNextContinuation() ||
259 aOurLineContainer->GetPrevContinuation(),
260 "Don't call this when we have no continuation, it's a waste");
261 if (!aFrame) {
262 NS_ASSERTION(aReparentSiblings, "Why did we get called?");
263 return;
266 nsIFrame* ancestor = aFrame;
267 do {
268 ancestor = ancestor->GetParent();
269 if (!ancestor)
270 return;
271 } while (!ancestor->IsFloatContainingBlock());
273 if (ancestor == aOurLineContainer)
274 return;
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");
281 while (true) {
282 ourBlock->ReparentFloats(aFrame, frameBlock, false);
284 if (!aReparentSiblings)
285 return;
286 nsIFrame* next = aFrame->GetNextSibling();
287 if (!next)
288 return;
289 if (next->GetParent() == aFrame->GetParent()) {
290 aFrame = next;
291 continue;
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);
298 return;
302 static void
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());
316 void
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");
326 return;
328 if (IsFrameTreeTooDeep(aReflowState, aMetrics, aStatus)) {
329 return;
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();
338 if (prevInFlow) {
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,
345 this);
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() &&
353 !GetNextInFlow()) {
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;
360 } else {
361 // Assign all floats to our block if necessary
362 if (lineContainer && lineContainer->GetPrevContinuation()) {
363 ReparentFloatsForInlineChild(lineContainer,
364 prevOverflowFrames->FirstChild(),
365 true);
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
384 #ifdef DEBUG
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");
393 #endif
394 if (!(GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
395 DrainFlags flags =
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
404 // aReflowState)
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.
415 bool complete;
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);
429 bool
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()) {
448 f->SetParent(this);
449 if (doReparentSC) {
450 restyleManager->ReparentStyleContext(f);
451 nsLayoutUtils::MarkDescendantsDirty(f);
455 bool result = !overflowFrames->IsEmpty();
456 mFrames.AppendFrames(nullptr, *overflowFrames);
457 return result;
459 return false;
462 /* virtual */ bool
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);
472 break;
475 return DrainSelfOverflowListInternal(flags, lineContainer);
478 /* virtual */ bool
479 nsInlineFrame::CanContinueTextRun() const
481 // We can continue a text run through an inline frame
482 return true;
485 /* virtual */ void
486 nsInlineFrame::PullOverflowsFromPrevInFlow()
488 nsInlineFrame* prevInFlow = static_cast<nsInlineFrame*>(GetPrevInFlow());
489 if (prevInFlow) {
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,
496 this);
497 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
502 void
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();
540 bool done = false;
541 while (frame) {
542 // Check if we should lazily set the child frame's parent pointer.
543 if (irs.mSetParentPointer) {
544 bool havePrevBlock =
545 irs.mLineContainer && irs.mLineContainer->GetPrevContinuation();
546 nsIFrame* child = frame;
547 do {
548 // If our block is the first in flow, then any floats under the pulled
549 // frame must already belong to our block.
550 if (havePrevBlock) {
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);
562 if (inFirstLine) {
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();
576 if (!nextSibling) {
577 child = nullptr;
580 MOZ_ASSERT(!child || mFrames.ContainsFrame(child));
581 } while (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();
588 if (child) {
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);
597 if (inFirstLine) {
598 restyleManager->ReparentStyleContext(nextInFlow);
599 nsLayoutUtils::MarkDescendantsDirty(nextInFlow);
602 else {
603 #ifdef DEBUG
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");
610 #endif
611 break;
617 MOZ_ASSERT(frame->GetParent() == this);
619 if (!done) {
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));
624 if (done) {
625 if (!irs.mSetParentPointer) {
626 break;
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.
632 break;
634 } else {
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()) {
643 while (true) {
644 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
645 bool isComplete;
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);
650 #ifdef NOISY_PUSHING
651 printf("%p pulled up %p\n", this, frame);
652 #endif
653 if (nullptr == frame) {
654 if (!isComplete) {
655 aStatus = NS_FRAME_NOT_COMPLETE;
657 break;
659 ReflowInlineFrame(aPresContext, aReflowState, irs, frame, aStatus);
660 if (NS_INLINE_IS_BREAK(aStatus) ||
661 (!reflowingFirstLetter && NS_FRAME_IS_NOT_COMPLETE(aStatus))) {
662 break;
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
721 ListTag(stdout);
722 printf(": metrics=%d,%d ascent=%d\n",
723 aMetrics.Width(), aMetrics.Height(), aMetrics.TopAscent());
724 #endif
727 void
728 nsInlineFrame::ReflowInlineFrame(nsPresContext* aPresContext,
729 const nsHTMLReflowState& aReflowState,
730 InlineReflowState& irs,
731 nsIFrame* aFrame,
732 nsReflowStatus& aStatus)
734 nsLineLayout* lineLayout = aReflowState.mLineLayout;
735 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
736 bool pushedFrame;
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);
749 else {
750 // Preserve reflow status when breaking-before our first child
751 // and propagate it upward without modification.
753 return;
756 // Create a next-in-flow if needed.
757 if (!NS_FRAME_IS_FULLY_COMPLETE(aStatus)) {
758 nsIFrame* newFrame;
759 nsresult rv = CreateNextInFlow(aFrame, newFrame);
760 if (NS_FAILED(rv)) {
761 return;
765 if (NS_INLINE_IS_BREAK_AFTER(aStatus)) {
766 nsIFrame* nextFrame = aFrame->GetNextSibling();
767 if (nextFrame) {
768 NS_FRAME_SET_INCOMPLETE(aStatus);
769 PushFrames(aPresContext, nextFrame, aFrame, irs);
771 else {
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());
775 while (nextInFlow) {
776 if (nextInFlow->mFrames.NotEmpty()) {
777 NS_FRAME_SET_INCOMPLETE(aStatus);
778 break;
780 nextInFlow = static_cast<nsInlineFrame*>(nextInFlow->GetNextInFlow());
783 return;
786 if (!NS_FRAME_IS_FULLY_COMPLETE(aStatus) && !reflowingFirstLetter) {
787 nsIFrame* nextFrame = aFrame->GetNextSibling();
788 if (nextFrame) {
789 PushFrames(aPresContext, nextFrame, aFrame, irs);
794 nsIFrame*
795 nsInlineFrame::PullOneFrame(nsPresContext* aPresContext,
796 InlineReflowState& irs,
797 bool* aIsComplete)
799 bool isComplete = true;
801 nsIFrame* frame = nullptr;
802 nsInlineFrame* nextInFlow = irs.mNextInFlow;
803 while (nextInFlow) {
804 frame = nextInFlow->mFrames.FirstChild();
805 if (!frame) {
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();
813 } else {
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);
824 if (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);
838 isComplete = false;
839 if (irs.mLineLayout) {
840 irs.mLineLayout->SetDirtyNextLine();
842 nsContainerFrame::ReparentFrameView(frame, nextInFlow, this);
843 break;
845 nextInFlow = static_cast<nsInlineFrame*>(nextInFlow->GetNextInFlow());
846 irs.mNextInFlow = nextInFlow;
849 *aIsComplete = isComplete;
850 return frame;
853 void
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");
863 #ifdef NOISY_PUSHING
864 printf("%p pushing aFromChild %p, disconnecting from prev sib %p\n",
865 this, aFromChild, aPrevSibling);
866 #endif
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();
887 LogicalSides skip;
888 if (!IsFirst()) {
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
893 // border edge.
894 skip |= eLogicalSideBitsIStart;
896 else {
897 // If the prev continuation is empty, then go ahead and let our start
898 // edge border render.
901 if (!IsLast()) {
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
906 // border edge.
907 skip |= eLogicalSideBitsIEnd;
909 else {
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;
934 return skip;
937 nscoord
938 nsInlineFrame::GetLogicalBaseline(mozilla::WritingMode aWritingMode) const
940 return mBaseline;
943 #ifdef ACCESSIBILITY
944 a11y::AccType
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;
957 #endif
959 //////////////////////////////////////////////////////////////////////
961 // nsLineFrame implementation
963 nsFirstLineFrame*
964 NS_NewFirstLineFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
966 return new (aPresShell) nsFirstLineFrame(aContext);
969 NS_IMPL_FRAMEARENA_HELPERS(nsFirstLineFrame)
971 void
972 nsFirstLineFrame::Init(nsIContent* aContent,
973 nsContainerFrame* aParent,
974 nsIFrame* aPrevInFlow)
976 nsInlineFrame::Init(aContent, aParent, aPrevInFlow);
977 if (!aPrevInFlow) {
978 MOZ_ASSERT(StyleContext()->GetPseudo() == nsCSSPseudoElements::firstLine);
979 return;
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);
994 } else {
995 MOZ_ASSERT(FirstInFlow() != aPrevInFlow);
996 MOZ_ASSERT(aPrevInFlow->StyleContext()->GetPseudo() ==
997 nsCSSAnonBoxes::mozLineFrame);
1001 #ifdef DEBUG_FRAME_DUMP
1002 nsresult
1003 nsFirstLineFrame::GetFrameName(nsAString& aResult) const
1005 return MakeFrameName(NS_LITERAL_STRING("Line"), aResult);
1007 #endif
1009 nsIAtom*
1010 nsFirstLineFrame::GetType() const
1012 return nsGkAtoms::lineFrame;
1015 nsIFrame*
1016 nsFirstLineFrame::PullOneFrame(nsPresContext* aPresContext, InlineReflowState& irs,
1017 bool* aIsComplete)
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);
1027 return frame;
1030 void
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();
1044 if (prevInFlow) {
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(),
1052 true);
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
1064 // aReflowState)
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();
1072 if (wasEmpty) {
1073 // Try to pull over one frame before starting so that we know
1074 // whether we have an anonymous block or not.
1075 bool complete;
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
1083 // the right style.
1085 // All of this is so that text-runs reflow properly.
1086 irs.mPrevFrame = mFrames.LastChild();
1087 for (;;) {
1088 bool complete;
1089 nsIFrame* frame = PullOneFrame(aPresContext, irs, &complete);
1090 if (!frame) {
1091 break;
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
1109 /* virtual */ void
1110 nsFirstLineFrame::PullOverflowsFromPrevInFlow()
1112 nsFirstLineFrame* prevInFlow = static_cast<nsFirstLineFrame*>(GetPrevInFlow());
1113 if (prevInFlow) {
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);
1126 /* virtual */ bool
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);
1137 return result;
1139 return false;