Bug 1474771 - Revert all changes from bug 1459937 since they are no longer needed...
[gecko.git] / layout / generic / nsInlineFrame.cpp
blob6c201509731731b7d85a0323d00e722b0cf8aaa0
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* rendering object for CSS display:inline objects */
9 #include "nsInlineFrame.h"
11 #include "gfxContext.h"
12 #include "mozilla/ComputedStyle.h"
13 #include "mozilla/Likely.h"
14 #include "mozilla/PresShell.h"
15 #include "mozilla/RestyleManager.h"
16 #include "mozilla/ServoStyleSet.h"
17 #include "nsLineLayout.h"
18 #include "nsBlockFrame.h"
19 #include "nsPlaceholderFrame.h"
20 #include "nsGkAtoms.h"
21 #include "nsPresContext.h"
22 #include "nsPresContextInlines.h"
23 #include "nsCSSAnonBoxes.h"
24 #include "nsDisplayList.h"
25 #include "SVGTextFrame.h"
26 #include "nsStyleChangeList.h"
28 #ifdef DEBUG
29 # undef NOISY_PUSHING
30 #endif
32 using namespace mozilla;
33 using namespace mozilla::layout;
35 //////////////////////////////////////////////////////////////////////
37 // Basic nsInlineFrame methods
39 nsInlineFrame* NS_NewInlineFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
40 return new (aPresShell) nsInlineFrame(aStyle, aPresShell->GetPresContext());
43 NS_IMPL_FRAMEARENA_HELPERS(nsInlineFrame)
45 NS_QUERYFRAME_HEAD(nsInlineFrame)
46 NS_QUERYFRAME_ENTRY(nsInlineFrame)
47 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
49 #ifdef DEBUG_FRAME_DUMP
50 nsresult nsInlineFrame::GetFrameName(nsAString& aResult) const {
51 return MakeFrameName(NS_LITERAL_STRING("Inline"), aResult);
53 #endif
55 void nsInlineFrame::InvalidateFrame(uint32_t aDisplayItemKey,
56 bool aRebuildDisplayItems) {
57 if (nsSVGUtils::IsInSVGTextSubtree(this)) {
58 nsIFrame* svgTextFrame = nsLayoutUtils::GetClosestFrameOfType(
59 GetParent(), LayoutFrameType::SVGText);
60 svgTextFrame->InvalidateFrame();
61 return;
63 nsContainerFrame::InvalidateFrame(aDisplayItemKey, aRebuildDisplayItems);
66 void nsInlineFrame::InvalidateFrameWithRect(const nsRect& aRect,
67 uint32_t aDisplayItemKey,
68 bool aRebuildDisplayItems) {
69 if (nsSVGUtils::IsInSVGTextSubtree(this)) {
70 nsIFrame* svgTextFrame = nsLayoutUtils::GetClosestFrameOfType(
71 GetParent(), LayoutFrameType::SVGText);
72 svgTextFrame->InvalidateFrame();
73 return;
75 nsContainerFrame::InvalidateFrameWithRect(aRect, aDisplayItemKey,
76 aRebuildDisplayItems);
79 static inline bool IsMarginZero(const LengthPercentageOrAuto& aLength) {
80 return aLength.IsAuto() ||
81 nsLayoutUtils::IsMarginZero(aLength.AsLengthPercentage());
84 /* virtual */
85 bool nsInlineFrame::IsSelfEmpty() {
86 #if 0
87 // I used to think inline frames worked this way, but it seems they
88 // don't. At least not in our codebase.
89 if (GetPresContext()->CompatibilityMode() == eCompatibility_FullStandards) {
90 return false;
92 #endif
93 const nsStyleMargin* margin = StyleMargin();
94 const nsStyleBorder* border = StyleBorder();
95 const nsStylePadding* padding = StylePadding();
96 // Block-start and -end ignored, since they shouldn't affect things, but this
97 // doesn't really match with nsLineLayout.cpp's setting of
98 // ZeroEffectiveSpanBox, anymore, so what should this really be?
99 WritingMode wm = GetWritingMode();
100 bool haveStart, haveEnd;
102 auto HaveSide = [&](mozilla::Side aSide) -> bool {
103 return border->GetComputedBorderWidth(aSide) != 0 ||
104 !nsLayoutUtils::IsPaddingZero(padding->mPadding.Get(aSide)) ||
105 !IsMarginZero(margin->mMargin.Get(aSide));
107 // Initially set up haveStart and haveEnd in terms of visual (LTR/TTB)
108 // coordinates; we'll exchange them later if bidi-RTL is in effect to
109 // get logical start and end flags.
110 if (wm.IsVertical()) {
111 haveStart = HaveSide(eSideTop);
112 haveEnd = HaveSide(eSideBottom);
113 } else {
114 haveStart = HaveSide(eSideLeft);
115 haveEnd = HaveSide(eSideRight);
117 if (haveStart || haveEnd) {
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 == StyleBoxDecorationBreak::Slice) {
122 // When direction=rtl, we need to consider logical rather than visual
123 // start and end, so swap the flags.
124 if (!wm.IsBidiLTR()) {
125 Swap(haveStart, haveEnd);
127 // For ib-split frames, ignore things we know we'll skip in GetSkipSides.
128 // XXXbz should we be doing this for non-ib-split frames too, in a more
129 // general way?
131 // Get the first continuation eagerly, as a performance optimization, to
132 // avoid having to get it twice..
133 nsIFrame* firstCont = FirstContinuation();
134 return (!haveStart || firstCont->FrameIsNonFirstInIBSplit()) &&
135 (!haveEnd || firstCont->FrameIsNonLastInIBSplit());
137 return false;
139 return true;
142 bool nsInlineFrame::IsEmpty() {
143 if (!IsSelfEmpty()) {
144 return false;
147 for (nsIFrame* kid : mFrames) {
148 if (!kid->IsEmpty()) return false;
151 return true;
154 nsIFrame::FrameSearchResult nsInlineFrame::PeekOffsetCharacter(
155 bool aForward, int32_t* aOffset, PeekOffsetCharacterOptions aOptions) {
156 // Override the implementation in nsFrame, to skip empty inline frames
157 NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
158 int32_t startOffset = *aOffset;
159 if (startOffset < 0) startOffset = 1;
160 if (aForward == (startOffset == 0)) {
161 // We're before the frame and moving forward, or after it and moving
162 // backwards: skip to the other side, but keep going.
163 *aOffset = 1 - startOffset;
165 return CONTINUE;
168 void nsInlineFrame::DestroyFrom(nsIFrame* aDestructRoot,
169 PostDestroyData& aPostDestroyData) {
170 nsFrameList* overflowFrames = GetOverflowFrames();
171 if (overflowFrames) {
172 // Fixup the parent pointers for any child frames on the OverflowList.
173 // nsIFrame::DestroyFrom depends on that to find the sticky scroll
174 // container (an ancestor).
175 overflowFrames->ApplySetParent(this);
177 nsContainerFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
180 nsresult nsInlineFrame::StealFrame(nsIFrame* aChild) {
181 if (MaybeStealOverflowContainerFrame(aChild)) {
182 return NS_OK;
185 nsInlineFrame* parent = this;
186 bool removed = false;
187 do {
188 removed = parent->mFrames.StartRemoveFrame(aChild);
189 if (removed) {
190 break;
193 // We didn't find the child in our principal child list.
194 // Maybe it's on the overflow list?
195 nsFrameList* frameList = parent->GetOverflowFrames();
196 if (frameList) {
197 removed = frameList->ContinueRemoveFrame(aChild);
198 if (frameList->IsEmpty()) {
199 parent->DestroyOverflowList();
201 if (removed) {
202 break;
206 // Due to our "lazy reparenting" optimization 'aChild' might not actually
207 // be on any of our child lists, but instead in one of our next-in-flows.
208 parent = static_cast<nsInlineFrame*>(parent->GetNextInFlow());
209 } while (parent);
211 MOZ_ASSERT(removed, "nsInlineFrame::StealFrame: can't find aChild");
212 return removed ? NS_OK : NS_ERROR_UNEXPECTED;
215 void nsInlineFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
216 const nsDisplayListSet& aLists) {
217 BuildDisplayListForInline(aBuilder, aLists);
219 // The sole purpose of this is to trigger display of the selection
220 // window for Named Anchors, which don't have any children and
221 // normally don't have any size, but in Editor we use CSS to display
222 // an image to represent this "hidden" element.
223 if (!mFrames.FirstChild()) {
224 DisplaySelectionOverlay(aBuilder, aLists.Content());
228 //////////////////////////////////////////////////////////////////////
229 // Reflow methods
231 /* virtual */
232 void nsInlineFrame::AddInlineMinISize(gfxContext* aRenderingContext,
233 nsIFrame::InlineMinISizeData* aData) {
234 DoInlineIntrinsicISize(aRenderingContext, aData, nsLayoutUtils::MIN_ISIZE);
237 /* virtual */
238 void nsInlineFrame::AddInlinePrefISize(gfxContext* aRenderingContext,
239 nsIFrame::InlinePrefISizeData* aData) {
240 DoInlineIntrinsicISize(aRenderingContext, aData, nsLayoutUtils::PREF_ISIZE);
241 aData->mLineIsEmpty = false;
244 /* virtual */
245 LogicalSize nsInlineFrame::ComputeSize(
246 gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
247 nscoord aAvailableISize, const LogicalSize& aMargin,
248 const LogicalSize& aBorder, const LogicalSize& aPadding,
249 ComputeSizeFlags aFlags) {
250 // Inlines and text don't compute size before reflow.
251 return LogicalSize(aWM, NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
254 nsRect nsInlineFrame::ComputeTightBounds(DrawTarget* aDrawTarget) const {
255 // be conservative
256 if (Style()->HasTextDecorationLines()) {
257 return GetVisualOverflowRect();
259 return ComputeSimpleTightBounds(aDrawTarget);
262 static void ReparentChildListStyle(nsPresContext* aPresContext,
263 const nsFrameList::Slice& aFrames,
264 nsIFrame* aParentFrame) {
265 RestyleManager* restyleManager = aPresContext->RestyleManager();
267 for (nsFrameList::Enumerator e(aFrames); !e.AtEnd(); e.Next()) {
268 NS_ASSERTION(e.get()->GetParent() == aParentFrame, "Bogus parentage");
269 restyleManager->ReparentComputedStyleForFirstLine(e.get());
270 nsLayoutUtils::MarkDescendantsDirty(e.get());
274 void nsInlineFrame::Reflow(nsPresContext* aPresContext, ReflowOutput& aMetrics,
275 const ReflowInput& aReflowInput,
276 nsReflowStatus& aStatus) {
277 MarkInReflow();
278 DO_GLOBAL_REFLOW_COUNT("nsInlineFrame");
279 DISPLAY_REFLOW(aPresContext, this, aReflowInput, aMetrics, aStatus);
280 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
282 if (nullptr == aReflowInput.mLineLayout) {
283 NS_ERROR("must have non-null aReflowInput.mLineLayout");
284 return;
286 if (IsFrameTreeTooDeep(aReflowInput, aMetrics, aStatus)) {
287 return;
290 bool lazilySetParentPointer = false;
292 // Check for an overflow list with our prev-in-flow
293 nsInlineFrame* prevInFlow = (nsInlineFrame*)GetPrevInFlow();
294 if (prevInFlow) {
295 AutoFrameListPtr prevOverflowFrames(aPresContext,
296 prevInFlow->StealOverflowFrames());
297 if (prevOverflowFrames) {
298 // When pushing and pulling frames we need to check for whether any
299 // views need to be reparented.
300 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
301 this);
303 // Check if we should do the lazilySetParentPointer optimization.
304 // Only do it in simple cases where we're being reflowed for the
305 // first time, nothing (e.g. bidi resolution) has already given
306 // us children, and there's no next-in-flow, so all our frames
307 // will be taken from prevOverflowFrames.
308 if ((GetStateBits() & NS_FRAME_FIRST_REFLOW) && mFrames.IsEmpty() &&
309 !GetNextInFlow()) {
310 // If our child list is empty, just put the new frames into it.
311 // Note that we don't set the parent pointer for the new frames. Instead
312 // wait to do this until we actually reflow the frame. If the overflow
313 // list contains thousands of frames this is a big performance issue
314 // (see bug #5588)
315 mFrames.SetFrames(*prevOverflowFrames);
316 lazilySetParentPointer = true;
317 } else {
318 // Insert the new frames at the beginning of the child list
319 // and set their parent pointer
320 const nsFrameList::Slice& newFrames =
321 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
322 // If our prev in flow was under the first continuation of a first-line
323 // frame then we need to reparent the ComputedStyles to remove the
324 // the special first-line styling. In the lazilySetParentPointer case
325 // we reparent the ComputedStyles when we set their parents in
326 // nsInlineFrame::ReflowFrames and nsInlineFrame::ReflowInlineFrame.
327 if (aReflowInput.mLineLayout->GetInFirstLine()) {
328 ReparentChildListStyle(aPresContext, newFrames, this);
334 // It's also possible that we have an overflow list for ourselves
335 #ifdef DEBUG
336 if (GetStateBits() & NS_FRAME_FIRST_REFLOW) {
337 // If it's our initial reflow, then we should not have an overflow list.
338 // However, add an assertion in case we get reflowed more than once with
339 // the initial reflow reason
340 nsFrameList* overflowFrames = GetOverflowFrames();
341 NS_ASSERTION(!overflowFrames || overflowFrames->IsEmpty(),
342 "overflow list is not empty for initial reflow");
344 #endif
345 if (!(GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
346 DrainSelfOverflowListInternal(aReflowInput.mLineLayout->GetInFirstLine());
349 // Set our own reflow input (additional state above and beyond aReflowInput).
350 InlineReflowInput irs;
351 irs.mPrevFrame = nullptr;
352 irs.mLineContainer = aReflowInput.mLineLayout->LineContainerFrame();
353 irs.mLineLayout = aReflowInput.mLineLayout;
354 irs.mNextInFlow = (nsInlineFrame*)GetNextInFlow();
355 irs.mSetParentPointer = lazilySetParentPointer;
357 if (mFrames.IsEmpty()) {
358 // Try to pull over one frame before starting so that we know
359 // whether we have an anonymous block or not.
360 Unused << PullOneFrame(aPresContext, irs);
363 ReflowFrames(aPresContext, aReflowInput, irs, aMetrics, aStatus);
365 ReflowAbsoluteFrames(aPresContext, aMetrics, aReflowInput, aStatus);
367 // Note: the line layout code will properly compute our
368 // overflow-rect state for us.
370 NS_FRAME_SET_TRUNCATION(aStatus, aReflowInput, aMetrics);
373 nsresult nsInlineFrame::AttributeChanged(int32_t aNameSpaceID,
374 nsAtom* aAttribute, int32_t aModType) {
375 nsresult rv =
376 nsContainerFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
378 if (NS_FAILED(rv)) {
379 return rv;
382 if (nsSVGUtils::IsInSVGTextSubtree(this)) {
383 SVGTextFrame* f = static_cast<SVGTextFrame*>(
384 nsLayoutUtils::GetClosestFrameOfType(this, LayoutFrameType::SVGText));
385 f->HandleAttributeChangeInDescendant(mContent->AsElement(), aNameSpaceID,
386 aAttribute);
389 return NS_OK;
392 bool nsInlineFrame::DrainSelfOverflowListInternal(bool aInFirstLine) {
393 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
394 if (!overflowFrames || overflowFrames->IsEmpty()) {
395 return false;
398 // The frames on our own overflowlist may have been pushed by a
399 // previous lazilySetParentPointer Reflow so we need to ensure the
400 // correct parent pointer. This is sometimes skipped by Reflow.
401 nsIFrame* firstChild = overflowFrames->FirstChild();
402 RestyleManager* restyleManager = PresContext()->RestyleManager();
403 for (nsIFrame* f = firstChild; f; f = f->GetNextSibling()) {
404 f->SetParent(this);
405 if (MOZ_UNLIKELY(aInFirstLine)) {
406 restyleManager->ReparentComputedStyleForFirstLine(f);
407 nsLayoutUtils::MarkDescendantsDirty(f);
410 mFrames.AppendFrames(nullptr, *overflowFrames);
411 return true;
414 /* virtual */
415 bool nsInlineFrame::DrainSelfOverflowList() {
416 nsIFrame* lineContainer = nsLayoutUtils::FindNearestBlockAncestor(this);
417 // Add the eInFirstLine flag if we have a ::first-line ancestor frame.
418 // No need to look further than the nearest line container though.
419 bool inFirstLine = false;
420 for (nsIFrame* p = GetParent(); p != lineContainer; p = p->GetParent()) {
421 if (p->IsLineFrame()) {
422 inFirstLine = true;
423 break;
426 return DrainSelfOverflowListInternal(inFirstLine);
429 /* virtual */
430 bool nsInlineFrame::CanContinueTextRun() const {
431 // We can continue a text run through an inline frame
432 return true;
435 /* virtual */
436 void nsInlineFrame::PullOverflowsFromPrevInFlow() {
437 nsInlineFrame* prevInFlow = static_cast<nsInlineFrame*>(GetPrevInFlow());
438 if (prevInFlow) {
439 nsPresContext* presContext = PresContext();
440 AutoFrameListPtr prevOverflowFrames(presContext,
441 prevInFlow->StealOverflowFrames());
442 if (prevOverflowFrames) {
443 // Assume that our prev-in-flow has the same line container that we do.
444 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
445 this);
446 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
451 void nsInlineFrame::ReflowFrames(nsPresContext* aPresContext,
452 const ReflowInput& aReflowInput,
453 InlineReflowInput& irs, ReflowOutput& aMetrics,
454 nsReflowStatus& aStatus) {
455 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
457 nsLineLayout* lineLayout = aReflowInput.mLineLayout;
458 bool inFirstLine = aReflowInput.mLineLayout->GetInFirstLine();
459 RestyleManager* restyleManager = aPresContext->RestyleManager();
460 WritingMode frameWM = aReflowInput.GetWritingMode();
461 WritingMode lineWM = aReflowInput.mLineLayout->mRootSpan->mWritingMode;
462 LogicalMargin framePadding = aReflowInput.ComputedLogicalBorderPadding();
463 nscoord startEdge = 0;
464 const bool boxDecorationBreakClone = MOZ_UNLIKELY(
465 StyleBorder()->mBoxDecorationBreak == StyleBoxDecorationBreak::Clone);
466 // Don't offset by our start borderpadding if we have a prev continuation or
467 // if we're in a part of an {ib} split other than the first one. For
468 // box-decoration-break:clone we always offset our start since all
469 // continuations have border/padding.
470 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
471 boxDecorationBreakClone) {
472 startEdge = framePadding.IStart(frameWM);
474 nscoord availableISize = aReflowInput.AvailableISize();
475 NS_ASSERTION(availableISize != NS_UNCONSTRAINEDSIZE,
476 "should no longer use available widths");
477 // Subtract off inline axis border+padding from availableISize
478 availableISize -= startEdge;
479 availableISize -= framePadding.IEnd(frameWM);
480 lineLayout->BeginSpan(this, &aReflowInput, startEdge,
481 startEdge + availableISize, &mBaseline);
483 // First reflow our principal children.
484 nsIFrame* frame = mFrames.FirstChild();
485 bool done = false;
486 while (frame) {
487 // Check if we should lazily set the child frame's parent pointer.
488 if (irs.mSetParentPointer) {
489 nsIFrame* child = frame;
490 do {
491 child->SetParent(this);
492 if (inFirstLine) {
493 restyleManager->ReparentComputedStyleForFirstLine(child);
494 nsLayoutUtils::MarkDescendantsDirty(child);
496 // We also need to do the same for |frame|'s next-in-flows that are in
497 // the sibling list. Otherwise, if we reflow |frame| and it's complete
498 // we'll crash when trying to delete its next-in-flow.
499 // This scenario doesn't happen often, but it can happen.
500 nsIFrame* nextSibling = child->GetNextSibling();
501 child = child->GetNextInFlow();
502 if (MOZ_UNLIKELY(child)) {
503 while (child != nextSibling && nextSibling) {
504 nextSibling = nextSibling->GetNextSibling();
506 if (!nextSibling) {
507 child = nullptr;
510 MOZ_ASSERT(!child || mFrames.ContainsFrame(child));
511 } while (child);
513 // Fix the parent pointer for ::first-letter child frame next-in-flows,
514 // so nsFirstLetterFrame::Reflow can destroy them safely (bug 401042).
515 nsIFrame* realFrame = nsPlaceholderFrame::GetRealFrameFor(frame);
516 if (realFrame->IsLetterFrame()) {
517 nsIFrame* child = realFrame->PrincipalChildList().FirstChild();
518 if (child) {
519 NS_ASSERTION(child->IsTextFrame(), "unexpected frame type");
520 nsIFrame* nextInFlow = child->GetNextInFlow();
521 for (; nextInFlow; nextInFlow = nextInFlow->GetNextInFlow()) {
522 NS_ASSERTION(nextInFlow->IsTextFrame(), "unexpected frame type");
523 if (mFrames.ContainsFrame(nextInFlow)) {
524 nextInFlow->SetParent(this);
525 if (inFirstLine) {
526 restyleManager->ReparentComputedStyleForFirstLine(nextInFlow);
527 nsLayoutUtils::MarkDescendantsDirty(nextInFlow);
529 } else {
530 #ifdef DEBUG
531 // Once we find a next-in-flow that isn't ours none of the
532 // remaining next-in-flows should be either.
533 for (; nextInFlow; nextInFlow = nextInFlow->GetNextInFlow()) {
534 NS_ASSERTION(!mFrames.ContainsFrame(nextInFlow),
535 "unexpected letter frame flow");
537 #endif
538 break;
544 MOZ_ASSERT(frame->GetParent() == this);
546 if (!done) {
547 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
548 ReflowInlineFrame(aPresContext, aReflowInput, irs, frame, aStatus);
549 done = aStatus.IsInlineBreak() ||
550 (!reflowingFirstLetter && aStatus.IsIncomplete());
551 if (done) {
552 if (!irs.mSetParentPointer) {
553 break;
555 // Keep reparenting the remaining siblings, but don't reflow them.
556 nsFrameList* pushedFrames = GetOverflowFrames();
557 if (pushedFrames && pushedFrames->FirstChild() == frame) {
558 // Don't bother if |frame| was pushed to our overflow list.
559 break;
561 } else {
562 irs.mPrevFrame = frame;
565 frame = frame->GetNextSibling();
568 // Attempt to pull frames from our next-in-flow until we can't
569 if (!done && GetNextInFlow()) {
570 while (true) {
571 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
572 if (!frame) { // Could be non-null if we pulled a first-letter frame and
573 // it created a continuation, since we don't push those.
574 frame = PullOneFrame(aPresContext, irs);
576 #ifdef NOISY_PUSHING
577 printf("%p pulled up %p\n", this, frame);
578 #endif
579 if (!frame) {
580 break;
582 ReflowInlineFrame(aPresContext, aReflowInput, irs, frame, aStatus);
583 if (aStatus.IsInlineBreak() ||
584 (!reflowingFirstLetter && aStatus.IsIncomplete())) {
585 break;
587 irs.mPrevFrame = frame;
588 frame = frame->GetNextSibling();
592 NS_ASSERTION(!aStatus.IsComplete() || !GetOverflowFrames(),
593 "We can't be complete AND have overflow frames!");
595 // If after reflowing our children they take up no area then make
596 // sure that we don't either.
598 // Note: CSS demands that empty inline elements still affect the
599 // line-height calculations. However, continuations of an inline
600 // that are empty we force to empty so that things like collapsed
601 // whitespace in an inline element don't affect the line-height.
602 aMetrics.ISize(lineWM) = lineLayout->EndSpan(this);
604 // Compute final width.
606 // XXX Note that that the padding start and end are in the frame's
607 // writing mode, but the metrics' inline-size is in the line's
608 // writing mode. This makes sense if the line and frame are both
609 // vertical or both horizontal, but what should happen with
610 // orthogonal inlines?
612 // Make sure to not include our start border and padding if we have a prev
613 // continuation or if we're in a part of an {ib} split other than the first
614 // one. For box-decoration-break:clone we always include our start border
615 // and padding since all continuations have them.
616 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
617 boxDecorationBreakClone) {
618 aMetrics.ISize(lineWM) += framePadding.IStart(frameWM);
622 * We want to only apply the end border and padding if we're the last
623 * continuation and either not in an {ib} split or the last part of it. To
624 * be the last continuation we have to be complete (so that we won't get a
625 * next-in-flow) and have no non-fluid continuations on our continuation
626 * chain. For box-decoration-break:clone we always apply the end border and
627 * padding since all continuations have them.
629 if ((aStatus.IsComplete() && !LastInFlow()->GetNextContinuation() &&
630 !FrameIsNonLastInIBSplit()) ||
631 boxDecorationBreakClone) {
632 aMetrics.ISize(lineWM) += framePadding.IEnd(frameWM);
635 nsLayoutUtils::SetBSizeFromFontMetrics(this, aMetrics, framePadding, lineWM,
636 frameWM);
638 // For now our overflow area is zero. The real value will be
639 // computed in |nsLineLayout::RelativePositionFrames|.
640 aMetrics.mOverflowAreas.Clear();
642 #ifdef NOISY_FINAL_SIZE
643 ListTag(stdout);
644 printf(": metrics=%d,%d ascent=%d\n", aMetrics.Width(), aMetrics.Height(),
645 aMetrics.TopAscent());
646 #endif
649 // Returns whether there's any remaining frame to pull.
650 /* static */
651 bool nsInlineFrame::HasFramesToPull(nsInlineFrame* aNextInFlow) {
652 while (aNextInFlow) {
653 if (!aNextInFlow->mFrames.IsEmpty()) {
654 return true;
656 if (const nsFrameList* overflow = aNextInFlow->GetOverflowFrames()) {
657 if (!overflow->IsEmpty()) {
658 return true;
661 aNextInFlow = static_cast<nsInlineFrame*>(aNextInFlow->GetNextInFlow());
663 return false;
666 void nsInlineFrame::ReflowInlineFrame(nsPresContext* aPresContext,
667 const ReflowInput& aReflowInput,
668 InlineReflowInput& irs, nsIFrame* aFrame,
669 nsReflowStatus& aStatus) {
670 nsLineLayout* lineLayout = aReflowInput.mLineLayout;
671 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
672 bool pushedFrame;
673 aStatus.Reset();
674 lineLayout->ReflowFrame(aFrame, aStatus, nullptr, pushedFrame);
676 if (aStatus.IsInlineBreakBefore()) {
677 if (aFrame != mFrames.FirstChild()) {
678 // Change break-before status into break-after since we have
679 // already placed at least one child frame. This preserves the
680 // break-type so that it can be propagated upward.
681 StyleClear oldBreakType = aStatus.BreakType();
682 aStatus.Reset();
683 aStatus.SetIncomplete();
684 aStatus.SetInlineLineBreakAfter(oldBreakType);
685 PushFrames(aPresContext, aFrame, irs.mPrevFrame, irs);
686 } else {
687 // Preserve reflow status when breaking-before our first child
688 // and propagate it upward without modification.
690 return;
693 // Create a next-in-flow if needed.
694 if (!aStatus.IsFullyComplete()) {
695 CreateNextInFlow(aFrame);
698 if (aStatus.IsInlineBreakAfter()) {
699 nsIFrame* nextFrame = aFrame->GetNextSibling();
700 if (nextFrame) {
701 aStatus.SetIncomplete();
702 PushFrames(aPresContext, nextFrame, aFrame, irs);
703 } else {
704 // We must return an incomplete status if there are more child
705 // frames remaining in a next-in-flow that follows this frame.
706 if (HasFramesToPull(static_cast<nsInlineFrame*>(GetNextInFlow()))) {
707 aStatus.SetIncomplete();
710 return;
713 if (!aStatus.IsFullyComplete() && !reflowingFirstLetter) {
714 nsIFrame* nextFrame = aFrame->GetNextSibling();
715 if (nextFrame) {
716 PushFrames(aPresContext, nextFrame, aFrame, irs);
721 nsIFrame* nsInlineFrame::PullOneFrame(nsPresContext* aPresContext,
722 InlineReflowInput& irs) {
723 nsIFrame* frame = nullptr;
724 nsInlineFrame* nextInFlow = irs.mNextInFlow;
726 #ifdef DEBUG
727 bool willPull = HasFramesToPull(nextInFlow);
728 #endif
730 while (nextInFlow) {
731 frame = nextInFlow->mFrames.FirstChild();
732 if (!frame) {
733 // The nextInFlow's principal list has no frames, try its overflow list.
734 nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
735 if (overflowFrames) {
736 frame = overflowFrames->RemoveFirstChild();
737 if (overflowFrames->IsEmpty()) {
738 // We're stealing the only frame - delete the overflow list.
739 nextInFlow->DestroyOverflowList();
740 } else {
741 // We leave the remaining frames on the overflow list (rather than
742 // putting them on nextInFlow's principal list) so we don't have to
743 // set up the parent for them.
745 // ReparentFloatsForInlineChild needs it to be on a child list -
746 // we remove it again below.
747 nextInFlow->mFrames.SetFrames(frame);
751 if (frame) {
752 // If our block has no next continuation, then any floats belonging to
753 // the pulled frame must belong to our block already. This check ensures
754 // we do no extra work in the common non-vertical-breaking case.
755 if (irs.mLineContainer && irs.mLineContainer->GetNextContinuation()) {
756 // The blockChildren.ContainsFrame check performed by
757 // ReparentFloatsForInlineChild will be fast because frame's ancestor
758 // will be the first child of its containing block.
759 ReparentFloatsForInlineChild(irs.mLineContainer, frame, false);
761 nextInFlow->mFrames.RemoveFirstChild();
762 // nsFirstLineFrame::PullOneFrame calls ReparentComputedStyle.
764 mFrames.InsertFrame(this, irs.mPrevFrame, frame);
765 if (irs.mLineLayout) {
766 irs.mLineLayout->SetDirtyNextLine();
768 nsContainerFrame::ReparentFrameView(frame, nextInFlow, this);
769 break;
771 nextInFlow = static_cast<nsInlineFrame*>(nextInFlow->GetNextInFlow());
772 irs.mNextInFlow = nextInFlow;
775 MOZ_ASSERT(!!frame == willPull);
776 return frame;
779 void nsInlineFrame::PushFrames(nsPresContext* aPresContext,
780 nsIFrame* aFromChild, nsIFrame* aPrevSibling,
781 InlineReflowInput& aState) {
782 #ifdef NOISY_PUSHING
783 printf("%p pushing aFromChild %p, disconnecting from prev sib %p\n", this,
784 aFromChild, aPrevSibling);
785 #endif
787 PushChildrenToOverflow(aFromChild, aPrevSibling);
788 if (aState.mLineLayout) {
789 aState.mLineLayout->SetDirtyNextLine();
793 //////////////////////////////////////////////////////////////////////
795 nsIFrame::LogicalSides nsInlineFrame::GetLogicalSkipSides(
796 const ReflowInput* aReflowInput) const {
797 if (MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak ==
798 StyleBoxDecorationBreak::Clone)) {
799 return LogicalSides();
802 LogicalSides skip;
803 if (!IsFirst()) {
804 nsInlineFrame* prev = (nsInlineFrame*)GetPrevContinuation();
805 if ((GetStateBits() & NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
806 (prev && (prev->mRect.height || prev->mRect.width))) {
807 // Prev continuation is not empty therefore we don't render our start
808 // border edge.
809 skip |= eLogicalSideBitsIStart;
810 } else {
811 // If the prev continuation is empty, then go ahead and let our start
812 // edge border render.
815 if (!IsLast()) {
816 nsInlineFrame* next = (nsInlineFrame*)GetNextContinuation();
817 if ((GetStateBits() & NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
818 (next && (next->mRect.height || next->mRect.width))) {
819 // Next continuation is not empty therefore we don't render our end
820 // border edge.
821 skip |= eLogicalSideBitsIEnd;
822 } else {
823 // If the next continuation is empty, then go ahead and let our end
824 // edge border render.
828 if (GetStateBits() & NS_FRAME_PART_OF_IBSPLIT) {
829 // All but the last part of an {ib} split should skip the "end" side (as
830 // determined by this frame's direction) and all but the first part of such
831 // a split should skip the "start" side. But figuring out which part of
832 // the split we are involves getting our first continuation, which might be
833 // expensive. So don't bother if we already have the relevant bits set.
834 if (skip != LogicalSides(eLogicalSideBitsIBoth)) {
835 // We're missing one of the skip bits, so check whether we need to set it.
836 // Only get the first continuation once, as an optimization.
837 nsIFrame* firstContinuation = FirstContinuation();
838 if (firstContinuation->FrameIsNonLastInIBSplit()) {
839 skip |= eLogicalSideBitsIEnd;
841 if (firstContinuation->FrameIsNonFirstInIBSplit()) {
842 skip |= eLogicalSideBitsIStart;
847 return skip;
850 nscoord nsInlineFrame::GetLogicalBaseline(
851 mozilla::WritingMode aWritingMode) const {
852 return mBaseline;
855 #ifdef ACCESSIBILITY
856 a11y::AccType nsInlineFrame::AccessibleType() {
857 // FIXME(emilio): This is broken, if the image has its default `display` value
858 // overridden. Should be somewhere else.
859 if (mContent->IsHTMLElement(
860 nsGkAtoms::img)) // Create accessible for broken <img>
861 return a11y::eHyperTextType;
863 return a11y::eNoType;
865 #endif
867 void nsInlineFrame::UpdateStyleOfOwnedAnonBoxesForIBSplit(
868 ServoRestyleState& aRestyleState) {
869 MOZ_ASSERT(GetStateBits() & NS_FRAME_OWNS_ANON_BOXES,
870 "Why did we get called?");
871 MOZ_ASSERT(GetStateBits() & NS_FRAME_PART_OF_IBSPLIT,
872 "Why did we have the NS_FRAME_OWNS_ANON_BOXES bit set?");
873 // Note: this assert _looks_ expensive, but it's cheap in all the cases when
874 // it passes!
875 MOZ_ASSERT(nsLayoutUtils::FirstContinuationOrIBSplitSibling(this) == this,
876 "Only the primary frame of the inline in a block-inside-inline "
877 "split should have NS_FRAME_OWNS_ANON_BOXES");
878 MOZ_ASSERT(mContent->GetPrimaryFrame() == this,
879 "We should be the primary frame for our element");
881 nsIFrame* blockFrame = GetProperty(nsIFrame::IBSplitSibling());
882 MOZ_ASSERT(blockFrame, "Why did we have an IB split?");
884 // The later inlines need to get our style.
885 ComputedStyle* ourStyle = Style();
887 // The anonymous block's style inherits from ours, and we already have our new
888 // ComputedStyle.
889 RefPtr<ComputedStyle> newContext =
890 aRestyleState.StyleSet().ResolveInheritingAnonymousBoxStyle(
891 PseudoStyleType::mozBlockInsideInlineWrapper, ourStyle);
893 // We're guaranteed that newContext only differs from the old ComputedStyle on
894 // the block in things they might inherit from us. And changehint processing
895 // guarantees walking the continuation and ib-sibling chains, so our existing
896 // changehint being in aChangeList is good enough. So we don't need to touch
897 // aChangeList at all here.
899 while (blockFrame) {
900 MOZ_ASSERT(!blockFrame->GetPrevContinuation(),
901 "Must be first continuation");
903 MOZ_ASSERT(blockFrame->Style()->GetPseudoType() ==
904 PseudoStyleType::mozBlockInsideInlineWrapper,
905 "Unexpected kind of ComputedStyle");
907 for (nsIFrame* cont = blockFrame; cont;
908 cont = cont->GetNextContinuation()) {
909 cont->SetComputedStyle(newContext);
912 nsIFrame* nextInline = blockFrame->GetProperty(nsIFrame::IBSplitSibling());
914 // This check is here due to bug 1431232. Please remove it once
915 // that bug is fixed.
916 if (MOZ_UNLIKELY(!nextInline)) {
917 break;
920 MOZ_ASSERT(nextInline, "There is always a trailing inline in an IB split");
922 for (nsIFrame* cont = nextInline; cont;
923 cont = cont->GetNextContinuation()) {
924 cont->SetComputedStyle(ourStyle);
926 blockFrame = nextInline->GetProperty(nsIFrame::IBSplitSibling());
930 //////////////////////////////////////////////////////////////////////
932 // nsLineFrame implementation
934 nsFirstLineFrame* NS_NewFirstLineFrame(PresShell* aPresShell,
935 ComputedStyle* aStyle) {
936 return new (aPresShell)
937 nsFirstLineFrame(aStyle, aPresShell->GetPresContext());
940 NS_IMPL_FRAMEARENA_HELPERS(nsFirstLineFrame)
942 void nsFirstLineFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
943 nsIFrame* aPrevInFlow) {
944 nsInlineFrame::Init(aContent, aParent, aPrevInFlow);
945 if (!aPrevInFlow) {
946 MOZ_ASSERT(Style()->GetPseudoType() == PseudoStyleType::firstLine);
947 return;
950 // This frame is a continuation - fixup the computed style if aPrevInFlow
951 // is the first-in-flow (the only one with a ::first-line pseudo).
952 if (aPrevInFlow->Style()->GetPseudoType() == PseudoStyleType::firstLine) {
953 MOZ_ASSERT(FirstInFlow() == aPrevInFlow);
954 // Create a new ComputedStyle that is a child of the parent
955 // ComputedStyle thus removing the ::first-line style. This way
956 // we behave as if an anonymous (unstyled) span was the child
957 // of the parent frame.
958 ComputedStyle* parentContext = aParent->Style();
959 RefPtr<ComputedStyle> newSC =
960 PresContext()->StyleSet()->ResolveInheritingAnonymousBoxStyle(
961 PseudoStyleType::mozLineFrame, parentContext);
962 SetComputedStyle(newSC);
963 } else {
964 MOZ_ASSERT(FirstInFlow() != aPrevInFlow);
965 MOZ_ASSERT(aPrevInFlow->Style()->GetPseudoType() ==
966 PseudoStyleType::mozLineFrame);
970 #ifdef DEBUG_FRAME_DUMP
971 nsresult nsFirstLineFrame::GetFrameName(nsAString& aResult) const {
972 return MakeFrameName(NS_LITERAL_STRING("Line"), aResult);
974 #endif
976 nsIFrame* nsFirstLineFrame::PullOneFrame(nsPresContext* aPresContext,
977 InlineReflowInput& irs) {
978 nsIFrame* frame = nsInlineFrame::PullOneFrame(aPresContext, irs);
979 if (frame && !GetPrevInFlow()) {
980 // We are a first-line frame. Fixup the child frames
981 // style-context that we just pulled.
982 NS_ASSERTION(frame->GetParent() == this, "Incorrect parent?");
983 aPresContext->RestyleManager()->ReparentComputedStyleForFirstLine(frame);
984 nsLayoutUtils::MarkDescendantsDirty(frame);
986 return frame;
989 void nsFirstLineFrame::Reflow(nsPresContext* aPresContext,
990 ReflowOutput& aMetrics,
991 const ReflowInput& aReflowInput,
992 nsReflowStatus& aStatus) {
993 MarkInReflow();
994 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
996 if (nullptr == aReflowInput.mLineLayout) {
997 return; // XXX does this happen? why?
1000 // Check for an overflow list with our prev-in-flow
1001 nsFirstLineFrame* prevInFlow = (nsFirstLineFrame*)GetPrevInFlow();
1002 if (prevInFlow) {
1003 AutoFrameListPtr prevOverflowFrames(aPresContext,
1004 prevInFlow->StealOverflowFrames());
1005 if (prevOverflowFrames) {
1006 // Reparent the new frames and their ComputedStyles.
1007 const nsFrameList::Slice& newFrames =
1008 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
1009 ReparentChildListStyle(aPresContext, newFrames, this);
1013 // It's also possible that we have an overflow list for ourselves.
1014 DrainSelfOverflowList();
1016 // Set our own reflow input (additional state above and beyond aReflowInput).
1017 InlineReflowInput irs;
1018 irs.mPrevFrame = nullptr;
1019 irs.mLineContainer = aReflowInput.mLineLayout->LineContainerFrame();
1020 irs.mLineLayout = aReflowInput.mLineLayout;
1021 irs.mNextInFlow = (nsInlineFrame*)GetNextInFlow();
1023 bool wasEmpty = mFrames.IsEmpty();
1024 if (wasEmpty) {
1025 // Try to pull over one frame before starting so that we know
1026 // whether we have an anonymous block or not.
1027 PullOneFrame(aPresContext, irs);
1030 if (nullptr == GetPrevInFlow()) {
1031 // XXX This is pretty sick, but what we do here is to pull-up, in
1032 // advance, all of the next-in-flows children. We re-resolve their
1033 // style while we are at at it so that when we reflow they have
1034 // the right style.
1036 // All of this is so that text-runs reflow properly.
1037 irs.mPrevFrame = mFrames.LastChild();
1038 for (;;) {
1039 nsIFrame* frame = PullOneFrame(aPresContext, irs);
1040 if (!frame) {
1041 break;
1043 irs.mPrevFrame = frame;
1045 irs.mPrevFrame = nullptr;
1048 NS_ASSERTION(!aReflowInput.mLineLayout->GetInFirstLine(),
1049 "Nested first-line frames? BOGUS");
1050 aReflowInput.mLineLayout->SetInFirstLine(true);
1051 ReflowFrames(aPresContext, aReflowInput, irs, aMetrics, aStatus);
1052 aReflowInput.mLineLayout->SetInFirstLine(false);
1054 ReflowAbsoluteFrames(aPresContext, aMetrics, aReflowInput, aStatus);
1056 // Note: the line layout code will properly compute our overflow state for us
1059 /* virtual */
1060 void nsFirstLineFrame::PullOverflowsFromPrevInFlow() {
1061 nsFirstLineFrame* prevInFlow =
1062 static_cast<nsFirstLineFrame*>(GetPrevInFlow());
1063 if (prevInFlow) {
1064 nsPresContext* presContext = PresContext();
1065 AutoFrameListPtr prevOverflowFrames(presContext,
1066 prevInFlow->StealOverflowFrames());
1067 if (prevOverflowFrames) {
1068 // Assume that our prev-in-flow has the same line container that we do.
1069 const nsFrameList::Slice& newFrames =
1070 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
1071 ReparentChildListStyle(presContext, newFrames, this);
1076 /* virtual */
1077 bool nsFirstLineFrame::DrainSelfOverflowList() {
1078 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
1079 if (overflowFrames) {
1080 bool result = !overflowFrames->IsEmpty();
1081 const nsFrameList::Slice& newFrames =
1082 mFrames.AppendFrames(nullptr, *overflowFrames);
1083 ReparentChildListStyle(PresContext(), newFrames, this);
1084 return result;
1086 return false;