Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / layout / generic / nsInlineFrame.cpp
blob7a99ecbe5d32896b36c32b464a439667876068d1
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* rendering object for CSS display:inline objects */
9 #include "nsInlineFrame.h"
11 #include "gfxContext.h"
12 #include "mozilla/ComputedStyle.h"
13 #include "mozilla/Likely.h"
14 #include "mozilla/PresShell.h"
15 #include "mozilla/RestyleManager.h"
16 #include "mozilla/ServoStyleSet.h"
17 #include "mozilla/SVGTextFrame.h"
18 #include "nsLineLayout.h"
19 #include "nsBlockFrame.h"
20 #include "nsLayoutUtils.h"
21 #include "nsPlaceholderFrame.h"
22 #include "nsGkAtoms.h"
23 #include "nsPresContext.h"
24 #include "nsPresContextInlines.h"
25 #include "nsCSSAnonBoxes.h"
26 #include "nsDisplayList.h"
27 #include "nsStyleChangeList.h"
29 #ifdef DEBUG
30 # undef NOISY_PUSHING
31 #endif
33 using namespace mozilla;
34 using namespace mozilla::layout;
36 //////////////////////////////////////////////////////////////////////
38 // Basic nsInlineFrame methods
40 nsInlineFrame* NS_NewInlineFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
41 return new (aPresShell) nsInlineFrame(aStyle, aPresShell->GetPresContext());
44 NS_IMPL_FRAMEARENA_HELPERS(nsInlineFrame)
46 NS_QUERYFRAME_HEAD(nsInlineFrame)
47 NS_QUERYFRAME_ENTRY(nsInlineFrame)
48 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
50 #ifdef DEBUG_FRAME_DUMP
51 nsresult nsInlineFrame::GetFrameName(nsAString& aResult) const {
52 return MakeFrameName(u"Inline"_ns, aResult);
54 #endif
56 void nsInlineFrame::InvalidateFrame(uint32_t aDisplayItemKey,
57 bool aRebuildDisplayItems) {
58 if (IsInSVGTextSubtree()) {
59 nsIFrame* svgTextFrame = nsLayoutUtils::GetClosestFrameOfType(
60 GetParent(), LayoutFrameType::SVGText);
61 svgTextFrame->InvalidateFrame();
62 return;
64 nsContainerFrame::InvalidateFrame(aDisplayItemKey, aRebuildDisplayItems);
67 void nsInlineFrame::InvalidateFrameWithRect(const nsRect& aRect,
68 uint32_t aDisplayItemKey,
69 bool aRebuildDisplayItems) {
70 if (IsInSVGTextSubtree()) {
71 nsIFrame* svgTextFrame = nsLayoutUtils::GetClosestFrameOfType(
72 GetParent(), LayoutFrameType::SVGText);
73 svgTextFrame->InvalidateFrame();
74 return;
76 nsContainerFrame::InvalidateFrameWithRect(aRect, aDisplayItemKey,
77 aRebuildDisplayItems);
80 static inline bool IsMarginZero(const LengthPercentageOrAuto& aLength) {
81 return aLength.IsAuto() ||
82 nsLayoutUtils::IsMarginZero(aLength.AsLengthPercentage());
85 /* virtual */
86 bool nsInlineFrame::IsSelfEmpty() {
87 #if 0
88 // I used to think inline frames worked this way, but it seems they
89 // don't. At least not in our codebase.
90 if (GetPresContext()->CompatibilityMode() == eCompatibility_FullStandards) {
91 return false;
93 #endif
94 const nsStyleMargin* margin = StyleMargin();
95 const nsStyleBorder* border = StyleBorder();
96 const nsStylePadding* padding = StylePadding();
97 // Block-start and -end ignored, since they shouldn't affect things, but this
98 // doesn't really match with nsLineLayout.cpp's setting of
99 // ZeroEffectiveSpanBox, anymore, so what should this really be?
100 WritingMode wm = GetWritingMode();
101 bool haveStart, haveEnd;
103 auto HaveSide = [&](mozilla::Side aSide) -> bool {
104 return border->GetComputedBorderWidth(aSide) != 0 ||
105 !nsLayoutUtils::IsPaddingZero(padding->mPadding.Get(aSide)) ||
106 !IsMarginZero(margin->mMargin.Get(aSide));
108 // Initially set up haveStart and haveEnd in terms of visual (LTR/TTB)
109 // coordinates; we'll exchange them later if bidi-RTL is in effect to
110 // get logical start and end flags.
111 if (wm.IsVertical()) {
112 haveStart = HaveSide(eSideTop);
113 haveEnd = HaveSide(eSideBottom);
114 } else {
115 haveStart = HaveSide(eSideLeft);
116 haveEnd = HaveSide(eSideRight);
118 if (haveStart || haveEnd) {
119 // We skip this block and return false for box-decoration-break:clone since
120 // in that case all the continuations will have the border/padding/margin.
121 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT) &&
122 StyleBorder()->mBoxDecorationBreak == StyleBoxDecorationBreak::Slice) {
123 // When direction=rtl, we need to consider logical rather than visual
124 // start and end, so swap the flags.
125 if (wm.IsBidiRTL()) {
126 std::swap(haveStart, haveEnd);
128 // For ib-split frames, ignore things we know we'll skip in GetSkipSides.
129 // XXXbz should we be doing this for non-ib-split frames too, in a more
130 // general way?
132 // Get the first continuation eagerly, as a performance optimization, to
133 // avoid having to get it twice..
134 nsIFrame* firstCont = FirstContinuation();
135 return (!haveStart || firstCont->FrameIsNonFirstInIBSplit()) &&
136 (!haveEnd || firstCont->FrameIsNonLastInIBSplit());
138 return false;
140 return true;
143 bool nsInlineFrame::IsEmpty() {
144 if (!IsSelfEmpty()) {
145 return false;
148 for (nsIFrame* kid : mFrames) {
149 if (!kid->IsEmpty()) return false;
152 return true;
155 nsIFrame::FrameSearchResult nsInlineFrame::PeekOffsetCharacter(
156 bool aForward, int32_t* aOffset, PeekOffsetCharacterOptions aOptions) {
157 // Override the implementation in nsFrame, to skip empty inline frames
158 NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
159 int32_t startOffset = *aOffset;
160 if (startOffset < 0) startOffset = 1;
161 if (aForward == (startOffset == 0)) {
162 // We're before the frame and moving forward, or after it and moving
163 // backwards: skip to the other side, but keep going.
164 *aOffset = 1 - startOffset;
166 return CONTINUE;
169 void nsInlineFrame::Destroy(DestroyContext& aContext) {
170 nsFrameList* overflowFrames = GetOverflowFrames();
171 if (overflowFrames) {
172 // Fixup the parent pointers for any child frames on the OverflowList.
173 // nsIFrame::DestroyFrom depends on that to find the sticky scroll
174 // container (an ancestor).
175 overflowFrames->ApplySetParent(this);
177 nsContainerFrame::Destroy(aContext);
180 void nsInlineFrame::StealFrame(nsIFrame* aChild) {
181 if (MaybeStealOverflowContainerFrame(aChild)) {
182 return;
185 nsInlineFrame* parent = this;
186 do {
187 if (parent->mFrames.StartRemoveFrame(aChild)) {
188 return;
191 // We didn't find the child in our principal child list.
192 // Maybe it's on the overflow list?
193 nsFrameList* frameList = parent->GetOverflowFrames();
194 if (frameList && frameList->ContinueRemoveFrame(aChild)) {
195 if (frameList->IsEmpty()) {
196 parent->DestroyOverflowList();
198 return;
201 // Due to our "lazy reparenting" optimization 'aChild' might not actually
202 // be on any of our child lists, but instead in one of our next-in-flows.
203 parent = static_cast<nsInlineFrame*>(parent->GetNextInFlow());
204 } while (parent);
206 MOZ_ASSERT_UNREACHABLE("nsInlineFrame::StealFrame: can't find aChild");
209 void nsInlineFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
210 const nsDisplayListSet& aLists) {
211 BuildDisplayListForInline(aBuilder, aLists);
213 // The sole purpose of this is to trigger display of the selection
214 // window for Named Anchors, which don't have any children and
215 // normally don't have any size, but in Editor we use CSS to display
216 // an image to represent this "hidden" element.
217 if (!mFrames.FirstChild()) {
218 DisplaySelectionOverlay(aBuilder, aLists.Content());
222 //////////////////////////////////////////////////////////////////////
223 // Reflow methods
225 /* virtual */
226 void nsInlineFrame::AddInlineMinISize(gfxContext* aRenderingContext,
227 nsIFrame::InlineMinISizeData* aData) {
228 DoInlineMinISize(aRenderingContext, aData);
231 /* virtual */
232 void nsInlineFrame::AddInlinePrefISize(gfxContext* aRenderingContext,
233 nsIFrame::InlinePrefISizeData* aData) {
234 DoInlinePrefISize(aRenderingContext, aData);
237 /* virtual */
238 nsIFrame::SizeComputationResult nsInlineFrame::ComputeSize(
239 gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
240 nscoord aAvailableISize, const LogicalSize& aMargin,
241 const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
242 ComputeSizeFlags aFlags) {
243 // Inlines and text don't compute size before reflow.
244 return {LogicalSize(aWM, NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE),
245 AspectRatioUsage::None};
248 nsRect nsInlineFrame::ComputeTightBounds(DrawTarget* aDrawTarget) const {
249 // be conservative
250 if (Style()->HasTextDecorationLines()) {
251 return InkOverflowRect();
253 return ComputeSimpleTightBounds(aDrawTarget);
256 static void ReparentChildListStyle(nsPresContext* aPresContext,
257 const nsFrameList::Slice& aFrames,
258 nsIFrame* aParentFrame) {
259 RestyleManager* restyleManager = aPresContext->RestyleManager();
261 for (nsIFrame* f : aFrames) {
262 NS_ASSERTION(f->GetParent() == aParentFrame, "Bogus parentage");
263 restyleManager->ReparentComputedStyleForFirstLine(f);
264 nsLayoutUtils::MarkDescendantsDirty(f);
268 void nsInlineFrame::Reflow(nsPresContext* aPresContext, ReflowOutput& aMetrics,
269 const ReflowInput& aReflowInput,
270 nsReflowStatus& aStatus) {
271 MarkInReflow();
272 DO_GLOBAL_REFLOW_COUNT("nsInlineFrame");
273 DISPLAY_REFLOW(aPresContext, this, aReflowInput, aMetrics, aStatus);
274 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
276 if (!aReflowInput.mLineLayout) {
277 NS_ERROR("must have non-null aReflowInput.mLineLayout");
278 return;
280 if (IsFrameTreeTooDeep(aReflowInput, aMetrics, aStatus)) {
281 return;
284 bool lazilySetParentPointer = false;
286 // Check for an overflow list with our prev-in-flow
287 nsInlineFrame* prevInFlow = (nsInlineFrame*)GetPrevInFlow();
288 if (prevInFlow) {
289 AutoFrameListPtr prevOverflowFrames(aPresContext,
290 prevInFlow->StealOverflowFrames());
291 if (prevOverflowFrames) {
292 // When pushing and pulling frames we need to check for whether any
293 // views need to be reparented.
294 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
295 this);
297 // Check if we should do the lazilySetParentPointer optimization.
298 // Only do it in simple cases where we're being reflowed for the
299 // first time, nothing (e.g. bidi resolution) has already given
300 // us children, and there's no next-in-flow, so all our frames
301 // will be taken from prevOverflowFrames.
302 if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW) && mFrames.IsEmpty() &&
303 !GetNextInFlow()) {
304 // If our child list is empty, just put the new frames into it.
305 // Note that we don't set the parent pointer for the new frames. Instead
306 // wait to do this until we actually reflow the frame. If the overflow
307 // list contains thousands of frames this is a big performance issue
308 // (see bug #5588)
309 mFrames = std::move(*prevOverflowFrames);
310 lazilySetParentPointer = true;
311 } else {
312 // Insert the new frames at the beginning of the child list
313 // and set their parent pointer
314 const nsFrameList::Slice& newFrames =
315 mFrames.InsertFrames(this, nullptr, std::move(*prevOverflowFrames));
316 // If our prev in flow was under the first continuation of a first-line
317 // frame then we need to reparent the ComputedStyles to remove the
318 // the special first-line styling. In the lazilySetParentPointer case
319 // we reparent the ComputedStyles when we set their parents in
320 // nsInlineFrame::ReflowFrames and nsInlineFrame::ReflowInlineFrame.
321 if (aReflowInput.mLineLayout->GetInFirstLine()) {
322 ReparentChildListStyle(aPresContext, newFrames, this);
328 // It's also possible that we have an overflow list for ourselves
329 #ifdef DEBUG
330 if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
331 // If it's our initial reflow, then we should not have an overflow list.
332 // However, add an assertion in case we get reflowed more than once with
333 // the initial reflow reason
334 nsFrameList* overflowFrames = GetOverflowFrames();
335 NS_ASSERTION(!overflowFrames || overflowFrames->IsEmpty(),
336 "overflow list is not empty for initial reflow");
338 #endif
339 if (!HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
340 DrainSelfOverflowListInternal(aReflowInput.mLineLayout->GetInFirstLine());
343 // Set our own reflow input (additional state above and beyond aReflowInput).
344 InlineReflowInput irs;
345 irs.mPrevFrame = nullptr;
346 irs.mLineContainer = aReflowInput.mLineLayout->LineContainerFrame();
347 irs.mLineLayout = aReflowInput.mLineLayout;
348 irs.mNextInFlow = (nsInlineFrame*)GetNextInFlow();
349 irs.mSetParentPointer = lazilySetParentPointer;
351 if (mFrames.IsEmpty()) {
352 // Try to pull over one frame before starting so that we know
353 // whether we have an anonymous block or not.
354 Unused << PullOneFrame(aPresContext, irs);
357 ReflowFrames(aPresContext, aReflowInput, irs, aMetrics, aStatus);
359 ReflowAbsoluteFrames(aPresContext, aMetrics, aReflowInput, aStatus);
361 // Note: the line layout code will properly compute our
362 // overflow-rect state for us.
365 nsresult nsInlineFrame::AttributeChanged(int32_t aNameSpaceID,
366 nsAtom* aAttribute, int32_t aModType) {
367 nsresult rv =
368 nsContainerFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
370 if (NS_FAILED(rv)) {
371 return rv;
374 if (IsInSVGTextSubtree()) {
375 SVGTextFrame* f = static_cast<SVGTextFrame*>(
376 nsLayoutUtils::GetClosestFrameOfType(this, LayoutFrameType::SVGText));
377 f->HandleAttributeChangeInDescendant(mContent->AsElement(), aNameSpaceID,
378 aAttribute);
381 return NS_OK;
384 bool nsInlineFrame::DrainSelfOverflowListInternal(bool aInFirstLine) {
385 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
386 if (!overflowFrames || overflowFrames->IsEmpty()) {
387 return false;
390 // The frames on our own overflowlist may have been pushed by a
391 // previous lazilySetParentPointer Reflow so we need to ensure the
392 // correct parent pointer. This is sometimes skipped by Reflow.
393 nsIFrame* firstChild = overflowFrames->FirstChild();
394 RestyleManager* restyleManager = PresContext()->RestyleManager();
395 for (nsIFrame* f = firstChild; f; f = f->GetNextSibling()) {
396 f->SetParent(this);
397 if (MOZ_UNLIKELY(aInFirstLine)) {
398 restyleManager->ReparentComputedStyleForFirstLine(f);
399 nsLayoutUtils::MarkDescendantsDirty(f);
402 mFrames.AppendFrames(nullptr, std::move(*overflowFrames));
403 return true;
406 /* virtual */
407 bool nsInlineFrame::DrainSelfOverflowList() {
408 nsIFrame* lineContainer = nsLayoutUtils::FindNearestBlockAncestor(this);
409 // Add the eInFirstLine flag if we have a ::first-line ancestor frame.
410 // No need to look further than the nearest line container though.
411 bool inFirstLine = false;
412 for (nsIFrame* p = GetParent(); p != lineContainer; p = p->GetParent()) {
413 if (p->IsLineFrame()) {
414 inFirstLine = true;
415 break;
418 return DrainSelfOverflowListInternal(inFirstLine);
421 /* virtual */
422 bool nsInlineFrame::CanContinueTextRun() const {
423 // We can continue a text run through an inline frame
424 return true;
427 /* virtual */
428 void nsInlineFrame::PullOverflowsFromPrevInFlow() {
429 nsInlineFrame* prevInFlow = static_cast<nsInlineFrame*>(GetPrevInFlow());
430 if (prevInFlow) {
431 nsPresContext* presContext = PresContext();
432 AutoFrameListPtr prevOverflowFrames(presContext,
433 prevInFlow->StealOverflowFrames());
434 if (prevOverflowFrames) {
435 // Assume that our prev-in-flow has the same line container that we do.
436 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
437 this);
438 mFrames.InsertFrames(this, nullptr, std::move(*prevOverflowFrames));
443 void nsInlineFrame::ReflowFrames(nsPresContext* aPresContext,
444 const ReflowInput& aReflowInput,
445 InlineReflowInput& irs, ReflowOutput& aMetrics,
446 nsReflowStatus& aStatus) {
447 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
449 nsLineLayout* lineLayout = aReflowInput.mLineLayout;
450 bool inFirstLine = aReflowInput.mLineLayout->GetInFirstLine();
451 RestyleManager* restyleManager = aPresContext->RestyleManager();
452 WritingMode frameWM = aReflowInput.GetWritingMode();
453 WritingMode lineWM = aReflowInput.mLineLayout->mRootSpan->mWritingMode;
454 LogicalMargin framePadding =
455 aReflowInput.ComputedLogicalBorderPadding(frameWM);
456 nscoord startEdge = 0;
457 const bool boxDecorationBreakClone = MOZ_UNLIKELY(
458 StyleBorder()->mBoxDecorationBreak == StyleBoxDecorationBreak::Clone);
459 // Don't offset by our start borderpadding if we have a prev continuation or
460 // if we're in a part of an {ib} split other than the first one. For
461 // box-decoration-break:clone we always offset our start since all
462 // continuations have border/padding.
463 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
464 boxDecorationBreakClone) {
465 startEdge = framePadding.IStart(frameWM);
467 nscoord availableISize = aReflowInput.AvailableISize();
468 NS_ASSERTION(availableISize != NS_UNCONSTRAINEDSIZE,
469 "should no longer use available widths");
470 // Subtract off inline axis border+padding from availableISize
471 availableISize -= startEdge;
472 availableISize -= framePadding.IEnd(frameWM);
473 lineLayout->BeginSpan(this, &aReflowInput, startEdge,
474 startEdge + availableISize, &mBaseline);
476 // First reflow our principal children.
477 nsIFrame* frame = mFrames.FirstChild();
478 bool done = false;
479 while (frame) {
480 // Check if we should lazily set the child frame's parent pointer.
481 if (irs.mSetParentPointer) {
482 nsIFrame* child = frame;
483 do {
484 child->SetParent(this);
485 if (inFirstLine) {
486 restyleManager->ReparentComputedStyleForFirstLine(child);
487 nsLayoutUtils::MarkDescendantsDirty(child);
489 // We also need to do the same for |frame|'s next-in-flows that are in
490 // the sibling list. Otherwise, if we reflow |frame| and it's complete
491 // we'll crash when trying to delete its next-in-flow.
492 // This scenario doesn't happen often, but it can happen.
493 nsIFrame* nextSibling = child->GetNextSibling();
494 child = child->GetNextInFlow();
495 if (MOZ_UNLIKELY(child)) {
496 while (child != nextSibling && nextSibling) {
497 nextSibling = nextSibling->GetNextSibling();
499 if (!nextSibling) {
500 child = nullptr;
503 MOZ_ASSERT(!child || mFrames.ContainsFrame(child));
504 } while (child);
506 // Fix the parent pointer for ::first-letter child frame next-in-flows,
507 // so nsFirstLetterFrame::Reflow can destroy them safely (bug 401042).
508 nsIFrame* realFrame = nsPlaceholderFrame::GetRealFrameFor(frame);
509 if (realFrame->IsLetterFrame()) {
510 nsIFrame* child = realFrame->PrincipalChildList().FirstChild();
511 if (child) {
512 NS_ASSERTION(child->IsTextFrame(), "unexpected frame type");
513 nsIFrame* nextInFlow = child->GetNextInFlow();
514 for (; nextInFlow; nextInFlow = nextInFlow->GetNextInFlow()) {
515 NS_ASSERTION(nextInFlow->IsTextFrame(), "unexpected frame type");
516 if (mFrames.ContainsFrame(nextInFlow)) {
517 nextInFlow->SetParent(this);
518 if (inFirstLine) {
519 restyleManager->ReparentComputedStyleForFirstLine(nextInFlow);
520 nsLayoutUtils::MarkDescendantsDirty(nextInFlow);
522 } else {
523 #ifdef DEBUG
524 // Once we find a next-in-flow that isn't ours none of the
525 // remaining next-in-flows should be either.
526 for (; nextInFlow; nextInFlow = nextInFlow->GetNextInFlow()) {
527 NS_ASSERTION(!mFrames.ContainsFrame(nextInFlow),
528 "unexpected letter frame flow");
530 #endif
531 break;
537 MOZ_ASSERT(frame->GetParent() == this);
539 if (!done) {
540 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
541 ReflowInlineFrame(aPresContext, aReflowInput, irs, frame, aStatus);
542 done = aStatus.IsInlineBreak() ||
543 (!reflowingFirstLetter && aStatus.IsIncomplete());
544 if (done) {
545 if (!irs.mSetParentPointer) {
546 break;
548 // Keep reparenting the remaining siblings, but don't reflow them.
549 nsFrameList* pushedFrames = GetOverflowFrames();
550 if (pushedFrames && pushedFrames->FirstChild() == frame) {
551 // Don't bother if |frame| was pushed to our overflow list.
552 break;
554 } else {
555 irs.mPrevFrame = frame;
558 frame = frame->GetNextSibling();
561 // Attempt to pull frames from our next-in-flow until we can't
562 if (!done && GetNextInFlow()) {
563 while (true) {
564 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
565 if (!frame) { // Could be non-null if we pulled a first-letter frame and
566 // it created a continuation, since we don't push those.
567 frame = PullOneFrame(aPresContext, irs);
569 #ifdef NOISY_PUSHING
570 printf("%p pulled up %p\n", this, frame);
571 #endif
572 if (!frame) {
573 break;
575 ReflowInlineFrame(aPresContext, aReflowInput, irs, frame, aStatus);
576 if (aStatus.IsInlineBreak() ||
577 (!reflowingFirstLetter && aStatus.IsIncomplete())) {
578 break;
580 irs.mPrevFrame = frame;
581 frame = frame->GetNextSibling();
585 NS_ASSERTION(!aStatus.IsComplete() || !GetOverflowFrames(),
586 "We can't be complete AND have overflow frames!");
588 // If after reflowing our children they take up no area then make
589 // sure that we don't either.
591 // Note: CSS demands that empty inline elements still affect the
592 // line-height calculations. However, continuations of an inline
593 // that are empty we force to empty so that things like collapsed
594 // whitespace in an inline element don't affect the line-height.
595 aMetrics.ISize(lineWM) = lineLayout->EndSpan(this);
597 // Compute final width.
599 // XXX Note that that the padding start and end are in the frame's
600 // writing mode, but the metrics' inline-size is in the line's
601 // writing mode. This makes sense if the line and frame are both
602 // vertical or both horizontal, but what should happen with
603 // orthogonal inlines?
605 // Make sure to not include our start border and padding if we have a prev
606 // continuation or if we're in a part of an {ib} split other than the first
607 // one. For box-decoration-break:clone we always include our start border
608 // and padding since all continuations have them.
609 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
610 boxDecorationBreakClone) {
611 aMetrics.ISize(lineWM) += framePadding.IStart(frameWM);
615 * We want to only apply the end border and padding if we're the last
616 * continuation and either not in an {ib} split or the last part of it. To
617 * be the last continuation we have to be complete (so that we won't get a
618 * next-in-flow) and have no non-fluid continuations on our continuation
619 * chain. For box-decoration-break:clone we always apply the end border and
620 * padding since all continuations have them.
622 if ((aStatus.IsComplete() && !LastInFlow()->GetNextContinuation() &&
623 !FrameIsNonLastInIBSplit()) ||
624 boxDecorationBreakClone) {
625 aMetrics.ISize(lineWM) += framePadding.IEnd(frameWM);
628 nsLayoutUtils::SetBSizeFromFontMetrics(this, aMetrics, framePadding, lineWM,
629 frameWM);
631 // For now our overflow area is zero. The real value will be
632 // computed in |nsLineLayout::RelativePositionFrames|.
633 aMetrics.mOverflowAreas.Clear();
635 #ifdef NOISY_FINAL_SIZE
636 ListTag(stdout);
637 printf(": metrics=%d,%d ascent=%d\n", aMetrics.Width(), aMetrics.Height(),
638 aMetrics.BlockStartAscent());
639 #endif
642 // Returns whether there's any remaining frame to pull.
643 /* static */
644 bool nsInlineFrame::HasFramesToPull(nsInlineFrame* aNextInFlow) {
645 while (aNextInFlow) {
646 if (!aNextInFlow->mFrames.IsEmpty()) {
647 return true;
649 if (const nsFrameList* overflow = aNextInFlow->GetOverflowFrames()) {
650 if (!overflow->IsEmpty()) {
651 return true;
654 aNextInFlow = static_cast<nsInlineFrame*>(aNextInFlow->GetNextInFlow());
656 return false;
659 void nsInlineFrame::ReflowInlineFrame(nsPresContext* aPresContext,
660 const ReflowInput& aReflowInput,
661 InlineReflowInput& irs, nsIFrame* aFrame,
662 nsReflowStatus& aStatus) {
663 nsLineLayout* lineLayout = aReflowInput.mLineLayout;
664 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
665 bool pushedFrame;
666 aStatus.Reset();
667 lineLayout->ReflowFrame(aFrame, aStatus, nullptr, pushedFrame);
669 if (aStatus.IsInlineBreakBefore()) {
670 if (aFrame != mFrames.FirstChild()) {
671 // Change break-before status into break-after since we have
672 // already placed at least one child frame. This preserves the
673 // break-type so that it can be propagated upward.
674 StyleClear oldClearType = aStatus.FloatClearType();
675 aStatus.Reset();
676 aStatus.SetIncomplete();
677 aStatus.SetInlineLineBreakAfter(oldClearType);
678 PushFrames(aPresContext, aFrame, irs.mPrevFrame, irs);
679 } else {
680 // Preserve reflow status when breaking-before our first child
681 // and propagate it upward without modification.
683 return;
686 // Create a next-in-flow if needed.
687 if (!aStatus.IsFullyComplete()) {
688 CreateNextInFlow(aFrame);
691 if (aStatus.IsInlineBreakAfter()) {
692 nsIFrame* nextFrame = aFrame->GetNextSibling();
693 if (nextFrame) {
694 aStatus.SetIncomplete();
695 PushFrames(aPresContext, nextFrame, aFrame, irs);
696 } else {
697 // We must return an incomplete status if there are more child
698 // frames remaining in a next-in-flow that follows this frame.
699 if (HasFramesToPull(static_cast<nsInlineFrame*>(GetNextInFlow()))) {
700 aStatus.SetIncomplete();
703 return;
706 if (!aStatus.IsFullyComplete() && !reflowingFirstLetter) {
707 nsIFrame* nextFrame = aFrame->GetNextSibling();
708 if (nextFrame) {
709 PushFrames(aPresContext, nextFrame, aFrame, irs);
714 nsIFrame* nsInlineFrame::PullOneFrame(nsPresContext* aPresContext,
715 InlineReflowInput& irs) {
716 nsIFrame* frame = nullptr;
717 nsInlineFrame* nextInFlow = irs.mNextInFlow;
719 #ifdef DEBUG
720 bool willPull = HasFramesToPull(nextInFlow);
721 #endif
723 while (nextInFlow) {
724 frame = nextInFlow->mFrames.FirstChild();
725 if (!frame) {
726 // The nextInFlow's principal list has no frames, try its overflow list.
727 nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
728 if (overflowFrames) {
729 frame = overflowFrames->RemoveFirstChild();
730 if (overflowFrames->IsEmpty()) {
731 // We're stealing the only frame - delete the overflow list.
732 nextInFlow->DestroyOverflowList();
733 } else {
734 // We leave the remaining frames on the overflow list (rather than
735 // putting them on nextInFlow's principal list) so we don't have to
736 // set up the parent for them.
738 // ReparentFloatsForInlineChild needs it to be on a child list -
739 // we remove it again below.
740 nextInFlow->mFrames = nsFrameList(frame, frame);
744 if (frame) {
745 // If our block has no next continuation, then any floats belonging to
746 // the pulled frame must belong to our block already. This check ensures
747 // we do no extra work in the common non-vertical-breaking case.
748 if (irs.mLineContainer && irs.mLineContainer->GetNextContinuation()) {
749 // The blockChildren.ContainsFrame check performed by
750 // ReparentFloatsForInlineChild will be fast because frame's ancestor
751 // will be the first child of its containing block.
752 ReparentFloatsForInlineChild(irs.mLineContainer, frame, false);
754 nextInFlow->mFrames.RemoveFirstChild();
755 // nsFirstLineFrame::PullOneFrame calls ReparentComputedStyle.
757 mFrames.InsertFrame(this, irs.mPrevFrame, frame);
758 if (irs.mLineLayout) {
759 irs.mLineLayout->SetDirtyNextLine();
761 nsContainerFrame::ReparentFrameView(frame, nextInFlow, this);
762 break;
764 nextInFlow = static_cast<nsInlineFrame*>(nextInFlow->GetNextInFlow());
765 irs.mNextInFlow = nextInFlow;
768 MOZ_ASSERT(!!frame == willPull);
769 return frame;
772 void nsInlineFrame::PushFrames(nsPresContext* aPresContext,
773 nsIFrame* aFromChild, nsIFrame* aPrevSibling,
774 InlineReflowInput& aState) {
775 #ifdef NOISY_PUSHING
776 printf("%p pushing aFromChild %p, disconnecting from prev sib %p\n", this,
777 aFromChild, aPrevSibling);
778 #endif
780 PushChildrenToOverflow(aFromChild, aPrevSibling);
781 if (aState.mLineLayout) {
782 aState.mLineLayout->SetDirtyNextLine();
786 //////////////////////////////////////////////////////////////////////
788 LogicalSides nsInlineFrame::GetLogicalSkipSides() const {
789 LogicalSides skip(mWritingMode);
790 if (MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak ==
791 StyleBoxDecorationBreak::Clone)) {
792 return skip;
795 if (!IsFirst()) {
796 nsInlineFrame* prev = (nsInlineFrame*)GetPrevContinuation();
797 if (HasAnyStateBits(NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
798 (prev && (prev->mRect.height || prev->mRect.width))) {
799 // Prev continuation is not empty therefore we don't render our start
800 // border edge.
801 skip |= eLogicalSideBitsIStart;
802 } else {
803 // If the prev continuation is empty, then go ahead and let our start
804 // edge border render.
807 if (!IsLast()) {
808 nsInlineFrame* next = (nsInlineFrame*)GetNextContinuation();
809 if (HasAnyStateBits(NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
810 (next && (next->mRect.height || next->mRect.width))) {
811 // Next continuation is not empty therefore we don't render our end
812 // border edge.
813 skip |= eLogicalSideBitsIEnd;
814 } else {
815 // If the next continuation is empty, then go ahead and let our end
816 // edge border render.
820 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) {
821 // All but the last part of an {ib} split should skip the "end" side (as
822 // determined by this frame's direction) and all but the first part of such
823 // a split should skip the "start" side. But figuring out which part of
824 // the split we are involves getting our first continuation, which might be
825 // expensive. So don't bother if we already have the relevant bits set.
826 if (skip != LogicalSides(mWritingMode, eLogicalSideBitsIBoth)) {
827 // We're missing one of the skip bits, so check whether we need to set it.
828 // Only get the first continuation once, as an optimization.
829 nsIFrame* firstContinuation = FirstContinuation();
830 if (firstContinuation->FrameIsNonLastInIBSplit()) {
831 skip |= eLogicalSideBitsIEnd;
833 if (firstContinuation->FrameIsNonFirstInIBSplit()) {
834 skip |= eLogicalSideBitsIStart;
839 return skip;
842 Maybe<nscoord> nsInlineFrame::GetNaturalBaselineBOffset(
843 WritingMode aWM, BaselineSharingGroup aBaselineGroup,
844 BaselineExportContext) const {
845 if (aBaselineGroup == BaselineSharingGroup::Last) {
846 return Nothing{};
848 return Some(mBaseline);
851 #ifdef ACCESSIBILITY
852 a11y::AccType nsInlineFrame::AccessibleType() {
853 // FIXME(emilio): This is broken, if the image has its default `display` value
854 // overridden. Should be somewhere else.
855 if (mContent->IsHTMLElement(
856 nsGkAtoms::img)) // Create accessible for broken <img>
857 return a11y::eHyperTextType;
859 return a11y::eNoType;
861 #endif
863 void nsInlineFrame::UpdateStyleOfOwnedAnonBoxesForIBSplit(
864 ServoRestyleState& aRestyleState) {
865 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_OWNS_ANON_BOXES),
866 "Why did we get called?");
867 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT),
868 "Why did we have the NS_FRAME_OWNS_ANON_BOXES bit set?");
869 // Note: this assert _looks_ expensive, but it's cheap in all the cases when
870 // it passes!
871 MOZ_ASSERT(nsLayoutUtils::FirstContinuationOrIBSplitSibling(this) == this,
872 "Only the primary frame of the inline in a block-inside-inline "
873 "split should have NS_FRAME_OWNS_ANON_BOXES");
874 MOZ_ASSERT(mContent->GetPrimaryFrame() == this,
875 "We should be the primary frame for our element");
877 nsIFrame* blockFrame = GetProperty(nsIFrame::IBSplitSibling());
878 MOZ_ASSERT(blockFrame, "Why did we have an IB split?");
880 // The later inlines need to get our style.
881 ComputedStyle* ourStyle = Style();
883 // The anonymous block's style inherits from ours, and we already have our new
884 // ComputedStyle.
885 RefPtr<ComputedStyle> newContext =
886 aRestyleState.StyleSet().ResolveInheritingAnonymousBoxStyle(
887 PseudoStyleType::mozBlockInsideInlineWrapper, ourStyle);
889 // We're guaranteed that newContext only differs from the old ComputedStyle on
890 // the block in things they might inherit from us. And changehint processing
891 // guarantees walking the continuation and ib-sibling chains, so our existing
892 // changehint being in aChangeList is good enough. So we don't need to touch
893 // aChangeList at all here.
895 while (blockFrame) {
896 MOZ_ASSERT(!blockFrame->GetPrevContinuation(),
897 "Must be first continuation");
899 MOZ_ASSERT(blockFrame->Style()->GetPseudoType() ==
900 PseudoStyleType::mozBlockInsideInlineWrapper,
901 "Unexpected kind of ComputedStyle");
903 for (nsIFrame* cont = blockFrame; cont;
904 cont = cont->GetNextContinuation()) {
905 cont->SetComputedStyle(newContext);
908 nsIFrame* nextInline = blockFrame->GetProperty(nsIFrame::IBSplitSibling());
910 // This check is here due to bug 1431232. Please remove it once
911 // that bug is fixed.
912 if (MOZ_UNLIKELY(!nextInline)) {
913 break;
916 MOZ_ASSERT(nextInline, "There is always a trailing inline in an IB split");
918 for (nsIFrame* cont = nextInline; cont;
919 cont = cont->GetNextContinuation()) {
920 cont->SetComputedStyle(ourStyle);
922 blockFrame = nextInline->GetProperty(nsIFrame::IBSplitSibling());
926 //////////////////////////////////////////////////////////////////////
928 // nsLineFrame implementation
930 nsFirstLineFrame* NS_NewFirstLineFrame(PresShell* aPresShell,
931 ComputedStyle* aStyle) {
932 return new (aPresShell)
933 nsFirstLineFrame(aStyle, aPresShell->GetPresContext());
936 NS_IMPL_FRAMEARENA_HELPERS(nsFirstLineFrame)
938 void nsFirstLineFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
939 nsIFrame* aPrevInFlow) {
940 nsInlineFrame::Init(aContent, aParent, aPrevInFlow);
941 if (!aPrevInFlow) {
942 MOZ_ASSERT(Style()->GetPseudoType() == PseudoStyleType::firstLine);
943 return;
946 // This frame is a continuation - fixup the computed style if aPrevInFlow
947 // is the first-in-flow (the only one with a ::first-line pseudo).
948 if (aPrevInFlow->Style()->GetPseudoType() == PseudoStyleType::firstLine) {
949 MOZ_ASSERT(FirstInFlow() == aPrevInFlow);
950 // Create a new ComputedStyle that is a child of the parent
951 // ComputedStyle thus removing the ::first-line style. This way
952 // we behave as if an anonymous (unstyled) span was the child
953 // of the parent frame.
954 ComputedStyle* parentContext = aParent->Style();
955 RefPtr<ComputedStyle> newSC =
956 PresContext()->StyleSet()->ResolveInheritingAnonymousBoxStyle(
957 PseudoStyleType::mozLineFrame, parentContext);
958 SetComputedStyle(newSC);
959 } else {
960 MOZ_ASSERT(FirstInFlow() != aPrevInFlow);
961 MOZ_ASSERT(aPrevInFlow->Style()->GetPseudoType() ==
962 PseudoStyleType::mozLineFrame);
966 #ifdef DEBUG_FRAME_DUMP
967 nsresult nsFirstLineFrame::GetFrameName(nsAString& aResult) const {
968 return MakeFrameName(u"Line"_ns, aResult);
970 #endif
972 nsIFrame* nsFirstLineFrame::PullOneFrame(nsPresContext* aPresContext,
973 InlineReflowInput& irs) {
974 nsIFrame* frame = nsInlineFrame::PullOneFrame(aPresContext, irs);
975 if (frame && !GetPrevInFlow()) {
976 // We are a first-line frame. Fixup the child frames
977 // style-context that we just pulled.
978 NS_ASSERTION(frame->GetParent() == this, "Incorrect parent?");
979 aPresContext->RestyleManager()->ReparentComputedStyleForFirstLine(frame);
980 nsLayoutUtils::MarkDescendantsDirty(frame);
982 return frame;
985 void nsFirstLineFrame::Reflow(nsPresContext* aPresContext,
986 ReflowOutput& aMetrics,
987 const ReflowInput& aReflowInput,
988 nsReflowStatus& aStatus) {
989 MarkInReflow();
990 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
992 if (nullptr == aReflowInput.mLineLayout) {
993 return; // XXX does this happen? why?
996 // Check for an overflow list with our prev-in-flow
997 nsFirstLineFrame* prevInFlow = (nsFirstLineFrame*)GetPrevInFlow();
998 if (prevInFlow) {
999 AutoFrameListPtr prevOverflowFrames(aPresContext,
1000 prevInFlow->StealOverflowFrames());
1001 if (prevOverflowFrames) {
1002 // Reparent the new frames and their ComputedStyles.
1003 const nsFrameList::Slice& newFrames =
1004 mFrames.InsertFrames(this, nullptr, std::move(*prevOverflowFrames));
1005 ReparentChildListStyle(aPresContext, newFrames, this);
1009 // It's also possible that we have an overflow list for ourselves.
1010 DrainSelfOverflowList();
1012 // Set our own reflow input (additional state above and beyond aReflowInput).
1013 InlineReflowInput irs;
1014 irs.mPrevFrame = nullptr;
1015 irs.mLineContainer = aReflowInput.mLineLayout->LineContainerFrame();
1016 irs.mLineLayout = aReflowInput.mLineLayout;
1017 irs.mNextInFlow = (nsInlineFrame*)GetNextInFlow();
1019 bool wasEmpty = mFrames.IsEmpty();
1020 if (wasEmpty) {
1021 // Try to pull over one frame before starting so that we know
1022 // whether we have an anonymous block or not.
1023 PullOneFrame(aPresContext, irs);
1026 if (nullptr == GetPrevInFlow()) {
1027 // XXX This is pretty sick, but what we do here is to pull-up, in
1028 // advance, all of the next-in-flows children. We re-resolve their
1029 // style while we are at at it so that when we reflow they have
1030 // the right style.
1032 // All of this is so that text-runs reflow properly.
1033 irs.mPrevFrame = mFrames.LastChild();
1034 for (;;) {
1035 nsIFrame* frame = PullOneFrame(aPresContext, irs);
1036 if (!frame) {
1037 break;
1039 irs.mPrevFrame = frame;
1041 irs.mPrevFrame = nullptr;
1044 NS_ASSERTION(!aReflowInput.mLineLayout->GetInFirstLine(),
1045 "Nested first-line frames? BOGUS");
1046 aReflowInput.mLineLayout->SetInFirstLine(true);
1047 ReflowFrames(aPresContext, aReflowInput, irs, aMetrics, aStatus);
1048 aReflowInput.mLineLayout->SetInFirstLine(false);
1050 ReflowAbsoluteFrames(aPresContext, aMetrics, aReflowInput, aStatus);
1052 // Note: the line layout code will properly compute our overflow state for us
1055 /* virtual */
1056 void nsFirstLineFrame::PullOverflowsFromPrevInFlow() {
1057 nsFirstLineFrame* prevInFlow =
1058 static_cast<nsFirstLineFrame*>(GetPrevInFlow());
1059 if (prevInFlow) {
1060 nsPresContext* presContext = PresContext();
1061 AutoFrameListPtr prevOverflowFrames(presContext,
1062 prevInFlow->StealOverflowFrames());
1063 if (prevOverflowFrames) {
1064 // Assume that our prev-in-flow has the same line container that we do.
1065 const nsFrameList::Slice& newFrames =
1066 mFrames.InsertFrames(this, nullptr, std::move(*prevOverflowFrames));
1067 ReparentChildListStyle(presContext, newFrames, this);
1072 /* virtual */
1073 bool nsFirstLineFrame::DrainSelfOverflowList() {
1074 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
1075 if (overflowFrames) {
1076 bool result = !overflowFrames->IsEmpty();
1077 const nsFrameList::Slice& newFrames =
1078 mFrames.AppendFrames(nullptr, std::move(*overflowFrames));
1079 ReparentChildListStyle(PresContext(), newFrames, this);
1080 return result;
1082 return false;