Bug 1760439 [wpt PR 33220] - Implement FedCM permission delegates in content_shell...
[gecko.git] / layout / generic / nsInlineFrame.cpp
blob20e2eab934d80526d0d078c61afbb7050477523d
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 "mozilla/SVGUtils.h"
19 #include "nsLineLayout.h"
20 #include "nsBlockFrame.h"
21 #include "nsLayoutUtils.h"
22 #include "nsPlaceholderFrame.h"
23 #include "nsGkAtoms.h"
24 #include "nsPresContext.h"
25 #include "nsPresContextInlines.h"
26 #include "nsCSSAnonBoxes.h"
27 #include "nsDisplayList.h"
28 #include "nsStyleChangeList.h"
30 #ifdef DEBUG
31 # undef NOISY_PUSHING
32 #endif
34 using namespace mozilla;
35 using namespace mozilla::layout;
37 //////////////////////////////////////////////////////////////////////
39 // Basic nsInlineFrame methods
41 nsInlineFrame* NS_NewInlineFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
42 return new (aPresShell) nsInlineFrame(aStyle, aPresShell->GetPresContext());
45 NS_IMPL_FRAMEARENA_HELPERS(nsInlineFrame)
47 NS_QUERYFRAME_HEAD(nsInlineFrame)
48 NS_QUERYFRAME_ENTRY(nsInlineFrame)
49 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
51 #ifdef DEBUG_FRAME_DUMP
52 nsresult nsInlineFrame::GetFrameName(nsAString& aResult) const {
53 return MakeFrameName(u"Inline"_ns, aResult);
55 #endif
57 void nsInlineFrame::InvalidateFrame(uint32_t aDisplayItemKey,
58 bool aRebuildDisplayItems) {
59 if (SVGUtils::IsInSVGTextSubtree(this)) {
60 nsIFrame* svgTextFrame = nsLayoutUtils::GetClosestFrameOfType(
61 GetParent(), LayoutFrameType::SVGText);
62 svgTextFrame->InvalidateFrame();
63 return;
65 nsContainerFrame::InvalidateFrame(aDisplayItemKey, aRebuildDisplayItems);
68 void nsInlineFrame::InvalidateFrameWithRect(const nsRect& aRect,
69 uint32_t aDisplayItemKey,
70 bool aRebuildDisplayItems) {
71 if (SVGUtils::IsInSVGTextSubtree(this)) {
72 nsIFrame* svgTextFrame = nsLayoutUtils::GetClosestFrameOfType(
73 GetParent(), LayoutFrameType::SVGText);
74 svgTextFrame->InvalidateFrame();
75 return;
77 nsContainerFrame::InvalidateFrameWithRect(aRect, aDisplayItemKey,
78 aRebuildDisplayItems);
81 static inline bool IsMarginZero(const LengthPercentageOrAuto& aLength) {
82 return aLength.IsAuto() ||
83 nsLayoutUtils::IsMarginZero(aLength.AsLengthPercentage());
86 /* virtual */
87 bool nsInlineFrame::IsSelfEmpty() {
88 #if 0
89 // I used to think inline frames worked this way, but it seems they
90 // don't. At least not in our codebase.
91 if (GetPresContext()->CompatibilityMode() == eCompatibility_FullStandards) {
92 return false;
94 #endif
95 const nsStyleMargin* margin = StyleMargin();
96 const nsStyleBorder* border = StyleBorder();
97 const nsStylePadding* padding = StylePadding();
98 // Block-start and -end ignored, since they shouldn't affect things, but this
99 // doesn't really match with nsLineLayout.cpp's setting of
100 // ZeroEffectiveSpanBox, anymore, so what should this really be?
101 WritingMode wm = GetWritingMode();
102 bool haveStart, haveEnd;
104 auto HaveSide = [&](mozilla::Side aSide) -> bool {
105 return border->GetComputedBorderWidth(aSide) != 0 ||
106 !nsLayoutUtils::IsPaddingZero(padding->mPadding.Get(aSide)) ||
107 !IsMarginZero(margin->mMargin.Get(aSide));
109 // Initially set up haveStart and haveEnd in terms of visual (LTR/TTB)
110 // coordinates; we'll exchange them later if bidi-RTL is in effect to
111 // get logical start and end flags.
112 if (wm.IsVertical()) {
113 haveStart = HaveSide(eSideTop);
114 haveEnd = HaveSide(eSideBottom);
115 } else {
116 haveStart = HaveSide(eSideLeft);
117 haveEnd = HaveSide(eSideRight);
119 if (haveStart || haveEnd) {
120 // We skip this block and return false for box-decoration-break:clone since
121 // in that case all the continuations will have the border/padding/margin.
122 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT) &&
123 StyleBorder()->mBoxDecorationBreak == StyleBoxDecorationBreak::Slice) {
124 // When direction=rtl, we need to consider logical rather than visual
125 // start and end, so swap the flags.
126 if (wm.IsBidiRTL()) {
127 std::swap(haveStart, haveEnd);
129 // For ib-split frames, ignore things we know we'll skip in GetSkipSides.
130 // XXXbz should we be doing this for non-ib-split frames too, in a more
131 // general way?
133 // Get the first continuation eagerly, as a performance optimization, to
134 // avoid having to get it twice..
135 nsIFrame* firstCont = FirstContinuation();
136 return (!haveStart || firstCont->FrameIsNonFirstInIBSplit()) &&
137 (!haveEnd || firstCont->FrameIsNonLastInIBSplit());
139 return false;
141 return true;
144 bool nsInlineFrame::IsEmpty() {
145 if (!IsSelfEmpty()) {
146 return false;
149 for (nsIFrame* kid : mFrames) {
150 if (!kid->IsEmpty()) return false;
153 return true;
156 nsIFrame::FrameSearchResult nsInlineFrame::PeekOffsetCharacter(
157 bool aForward, int32_t* aOffset, PeekOffsetCharacterOptions aOptions) {
158 // Override the implementation in nsFrame, to skip empty inline frames
159 NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
160 int32_t startOffset = *aOffset;
161 if (startOffset < 0) startOffset = 1;
162 if (aForward == (startOffset == 0)) {
163 // We're before the frame and moving forward, or after it and moving
164 // backwards: skip to the other side, but keep going.
165 *aOffset = 1 - startOffset;
167 return CONTINUE;
170 void nsInlineFrame::DestroyFrom(nsIFrame* aDestructRoot,
171 PostDestroyData& aPostDestroyData) {
172 nsFrameList* overflowFrames = GetOverflowFrames();
173 if (overflowFrames) {
174 // Fixup the parent pointers for any child frames on the OverflowList.
175 // nsIFrame::DestroyFrom depends on that to find the sticky scroll
176 // container (an ancestor).
177 overflowFrames->ApplySetParent(this);
179 nsContainerFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
182 void nsInlineFrame::StealFrame(nsIFrame* aChild) {
183 if (MaybeStealOverflowContainerFrame(aChild)) {
184 return;
187 nsInlineFrame* parent = this;
188 do {
189 if (parent->mFrames.StartRemoveFrame(aChild)) {
190 return;
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 && frameList->ContinueRemoveFrame(aChild)) {
197 if (frameList->IsEmpty()) {
198 parent->DestroyOverflowList();
200 return;
203 // Due to our "lazy reparenting" optimization 'aChild' might not actually
204 // be on any of our child lists, but instead in one of our next-in-flows.
205 parent = static_cast<nsInlineFrame*>(parent->GetNextInFlow());
206 } while (parent);
208 MOZ_ASSERT_UNREACHABLE("nsInlineFrame::StealFrame: can't find aChild");
211 void nsInlineFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
212 const nsDisplayListSet& aLists) {
213 BuildDisplayListForInline(aBuilder, aLists);
215 // The sole purpose of this is to trigger display of the selection
216 // window for Named Anchors, which don't have any children and
217 // normally don't have any size, but in Editor we use CSS to display
218 // an image to represent this "hidden" element.
219 if (!mFrames.FirstChild()) {
220 DisplaySelectionOverlay(aBuilder, aLists.Content());
224 //////////////////////////////////////////////////////////////////////
225 // Reflow methods
227 /* virtual */
228 void nsInlineFrame::AddInlineMinISize(gfxContext* aRenderingContext,
229 nsIFrame::InlineMinISizeData* aData) {
230 DoInlineMinISize(aRenderingContext, aData);
233 /* virtual */
234 void nsInlineFrame::AddInlinePrefISize(gfxContext* aRenderingContext,
235 nsIFrame::InlinePrefISizeData* aData) {
236 DoInlinePrefISize(aRenderingContext, aData);
239 /* virtual */
240 nsIFrame::SizeComputationResult nsInlineFrame::ComputeSize(
241 gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
242 nscoord aAvailableISize, const LogicalSize& aMargin,
243 const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
244 ComputeSizeFlags aFlags) {
245 // Inlines and text don't compute size before reflow.
246 return {LogicalSize(aWM, NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE),
247 AspectRatioUsage::None};
250 nsRect nsInlineFrame::ComputeTightBounds(DrawTarget* aDrawTarget) const {
251 // be conservative
252 if (Style()->HasTextDecorationLines()) {
253 return InkOverflowRect();
255 return ComputeSimpleTightBounds(aDrawTarget);
258 static void ReparentChildListStyle(nsPresContext* aPresContext,
259 const nsFrameList::Slice& aFrames,
260 nsIFrame* aParentFrame) {
261 RestyleManager* restyleManager = aPresContext->RestyleManager();
263 for (nsFrameList::Enumerator e(aFrames); !e.AtEnd(); e.Next()) {
264 NS_ASSERTION(e.get()->GetParent() == aParentFrame, "Bogus parentage");
265 restyleManager->ReparentComputedStyleForFirstLine(e.get());
266 nsLayoutUtils::MarkDescendantsDirty(e.get());
270 void nsInlineFrame::Reflow(nsPresContext* aPresContext, ReflowOutput& aMetrics,
271 const ReflowInput& aReflowInput,
272 nsReflowStatus& aStatus) {
273 MarkInReflow();
274 DO_GLOBAL_REFLOW_COUNT("nsInlineFrame");
275 DISPLAY_REFLOW(aPresContext, this, aReflowInput, aMetrics, aStatus);
276 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
278 if (nullptr == aReflowInput.mLineLayout) {
279 NS_ERROR("must have non-null aReflowInput.mLineLayout");
280 return;
282 if (IsFrameTreeTooDeep(aReflowInput, aMetrics, aStatus)) {
283 return;
286 bool lazilySetParentPointer = false;
288 // Check for an overflow list with our prev-in-flow
289 nsInlineFrame* prevInFlow = (nsInlineFrame*)GetPrevInFlow();
290 if (prevInFlow) {
291 AutoFrameListPtr prevOverflowFrames(aPresContext,
292 prevInFlow->StealOverflowFrames());
293 if (prevOverflowFrames) {
294 // When pushing and pulling frames we need to check for whether any
295 // views need to be reparented.
296 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
297 this);
299 // Check if we should do the lazilySetParentPointer optimization.
300 // Only do it in simple cases where we're being reflowed for the
301 // first time, nothing (e.g. bidi resolution) has already given
302 // us children, and there's no next-in-flow, so all our frames
303 // will be taken from prevOverflowFrames.
304 if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW) && mFrames.IsEmpty() &&
305 !GetNextInFlow()) {
306 // If our child list is empty, just put the new frames into it.
307 // Note that we don't set the parent pointer for the new frames. Instead
308 // wait to do this until we actually reflow the frame. If the overflow
309 // list contains thousands of frames this is a big performance issue
310 // (see bug #5588)
311 mFrames.SetFrames(*prevOverflowFrames);
312 lazilySetParentPointer = true;
313 } else {
314 // Insert the new frames at the beginning of the child list
315 // and set their parent pointer
316 const nsFrameList::Slice& newFrames =
317 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
318 // If our prev in flow was under the first continuation of a first-line
319 // frame then we need to reparent the ComputedStyles to remove the
320 // the special first-line styling. In the lazilySetParentPointer case
321 // we reparent the ComputedStyles when we set their parents in
322 // nsInlineFrame::ReflowFrames and nsInlineFrame::ReflowInlineFrame.
323 if (aReflowInput.mLineLayout->GetInFirstLine()) {
324 ReparentChildListStyle(aPresContext, newFrames, this);
330 // It's also possible that we have an overflow list for ourselves
331 #ifdef DEBUG
332 if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
333 // If it's our initial reflow, then we should not have an overflow list.
334 // However, add an assertion in case we get reflowed more than once with
335 // the initial reflow reason
336 nsFrameList* overflowFrames = GetOverflowFrames();
337 NS_ASSERTION(!overflowFrames || overflowFrames->IsEmpty(),
338 "overflow list is not empty for initial reflow");
340 #endif
341 if (!HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
342 DrainSelfOverflowListInternal(aReflowInput.mLineLayout->GetInFirstLine());
345 // Set our own reflow input (additional state above and beyond aReflowInput).
346 InlineReflowInput irs;
347 irs.mPrevFrame = nullptr;
348 irs.mLineContainer = aReflowInput.mLineLayout->LineContainerFrame();
349 irs.mLineLayout = aReflowInput.mLineLayout;
350 irs.mNextInFlow = (nsInlineFrame*)GetNextInFlow();
351 irs.mSetParentPointer = lazilySetParentPointer;
353 if (mFrames.IsEmpty()) {
354 // Try to pull over one frame before starting so that we know
355 // whether we have an anonymous block or not.
356 Unused << PullOneFrame(aPresContext, irs);
359 ReflowFrames(aPresContext, aReflowInput, irs, aMetrics, aStatus);
361 ReflowAbsoluteFrames(aPresContext, aMetrics, aReflowInput, aStatus);
363 // Note: the line layout code will properly compute our
364 // overflow-rect state for us.
366 NS_FRAME_SET_TRUNCATION(aStatus, aReflowInput, aMetrics);
369 nsresult nsInlineFrame::AttributeChanged(int32_t aNameSpaceID,
370 nsAtom* aAttribute, int32_t aModType) {
371 nsresult rv =
372 nsContainerFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
374 if (NS_FAILED(rv)) {
375 return rv;
378 if (SVGUtils::IsInSVGTextSubtree(this)) {
379 SVGTextFrame* f = static_cast<SVGTextFrame*>(
380 nsLayoutUtils::GetClosestFrameOfType(this, LayoutFrameType::SVGText));
381 f->HandleAttributeChangeInDescendant(mContent->AsElement(), aNameSpaceID,
382 aAttribute);
385 return NS_OK;
388 bool nsInlineFrame::DrainSelfOverflowListInternal(bool aInFirstLine) {
389 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
390 if (!overflowFrames || overflowFrames->IsEmpty()) {
391 return false;
394 // The frames on our own overflowlist may have been pushed by a
395 // previous lazilySetParentPointer Reflow so we need to ensure the
396 // correct parent pointer. This is sometimes skipped by Reflow.
397 nsIFrame* firstChild = overflowFrames->FirstChild();
398 RestyleManager* restyleManager = PresContext()->RestyleManager();
399 for (nsIFrame* f = firstChild; f; f = f->GetNextSibling()) {
400 f->SetParent(this);
401 if (MOZ_UNLIKELY(aInFirstLine)) {
402 restyleManager->ReparentComputedStyleForFirstLine(f);
403 nsLayoutUtils::MarkDescendantsDirty(f);
406 mFrames.AppendFrames(nullptr, *overflowFrames);
407 return true;
410 /* virtual */
411 bool nsInlineFrame::DrainSelfOverflowList() {
412 nsIFrame* lineContainer = nsLayoutUtils::FindNearestBlockAncestor(this);
413 // Add the eInFirstLine flag if we have a ::first-line ancestor frame.
414 // No need to look further than the nearest line container though.
415 bool inFirstLine = false;
416 for (nsIFrame* p = GetParent(); p != lineContainer; p = p->GetParent()) {
417 if (p->IsLineFrame()) {
418 inFirstLine = true;
419 break;
422 return DrainSelfOverflowListInternal(inFirstLine);
425 /* virtual */
426 bool nsInlineFrame::CanContinueTextRun() const {
427 // We can continue a text run through an inline frame
428 return true;
431 /* virtual */
432 void nsInlineFrame::PullOverflowsFromPrevInFlow() {
433 nsInlineFrame* prevInFlow = static_cast<nsInlineFrame*>(GetPrevInFlow());
434 if (prevInFlow) {
435 nsPresContext* presContext = PresContext();
436 AutoFrameListPtr prevOverflowFrames(presContext,
437 prevInFlow->StealOverflowFrames());
438 if (prevOverflowFrames) {
439 // Assume that our prev-in-flow has the same line container that we do.
440 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
441 this);
442 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
447 void nsInlineFrame::ReflowFrames(nsPresContext* aPresContext,
448 const ReflowInput& aReflowInput,
449 InlineReflowInput& irs, ReflowOutput& aMetrics,
450 nsReflowStatus& aStatus) {
451 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
453 nsLineLayout* lineLayout = aReflowInput.mLineLayout;
454 bool inFirstLine = aReflowInput.mLineLayout->GetInFirstLine();
455 RestyleManager* restyleManager = aPresContext->RestyleManager();
456 WritingMode frameWM = aReflowInput.GetWritingMode();
457 WritingMode lineWM = aReflowInput.mLineLayout->mRootSpan->mWritingMode;
458 LogicalMargin framePadding =
459 aReflowInput.ComputedLogicalBorderPadding(frameWM);
460 nscoord startEdge = 0;
461 const bool boxDecorationBreakClone = MOZ_UNLIKELY(
462 StyleBorder()->mBoxDecorationBreak == StyleBoxDecorationBreak::Clone);
463 // Don't offset by our start borderpadding if we have a prev continuation or
464 // if we're in a part of an {ib} split other than the first one. For
465 // box-decoration-break:clone we always offset our start since all
466 // continuations have border/padding.
467 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
468 boxDecorationBreakClone) {
469 startEdge = framePadding.IStart(frameWM);
471 nscoord availableISize = aReflowInput.AvailableISize();
472 NS_ASSERTION(availableISize != NS_UNCONSTRAINEDSIZE,
473 "should no longer use available widths");
474 // Subtract off inline axis border+padding from availableISize
475 availableISize -= startEdge;
476 availableISize -= framePadding.IEnd(frameWM);
477 lineLayout->BeginSpan(this, &aReflowInput, startEdge,
478 startEdge + availableISize, &mBaseline);
480 // First reflow our principal children.
481 nsIFrame* frame = mFrames.FirstChild();
482 bool done = false;
483 while (frame) {
484 // Check if we should lazily set the child frame's parent pointer.
485 if (irs.mSetParentPointer) {
486 nsIFrame* child = frame;
487 do {
488 child->SetParent(this);
489 if (inFirstLine) {
490 restyleManager->ReparentComputedStyleForFirstLine(child);
491 nsLayoutUtils::MarkDescendantsDirty(child);
493 // We also need to do the same for |frame|'s next-in-flows that are in
494 // the sibling list. Otherwise, if we reflow |frame| and it's complete
495 // we'll crash when trying to delete its next-in-flow.
496 // This scenario doesn't happen often, but it can happen.
497 nsIFrame* nextSibling = child->GetNextSibling();
498 child = child->GetNextInFlow();
499 if (MOZ_UNLIKELY(child)) {
500 while (child != nextSibling && nextSibling) {
501 nextSibling = nextSibling->GetNextSibling();
503 if (!nextSibling) {
504 child = nullptr;
507 MOZ_ASSERT(!child || mFrames.ContainsFrame(child));
508 } while (child);
510 // Fix the parent pointer for ::first-letter child frame next-in-flows,
511 // so nsFirstLetterFrame::Reflow can destroy them safely (bug 401042).
512 nsIFrame* realFrame = nsPlaceholderFrame::GetRealFrameFor(frame);
513 if (realFrame->IsLetterFrame()) {
514 nsIFrame* child = realFrame->PrincipalChildList().FirstChild();
515 if (child) {
516 NS_ASSERTION(child->IsTextFrame(), "unexpected frame type");
517 nsIFrame* nextInFlow = child->GetNextInFlow();
518 for (; nextInFlow; nextInFlow = nextInFlow->GetNextInFlow()) {
519 NS_ASSERTION(nextInFlow->IsTextFrame(), "unexpected frame type");
520 if (mFrames.ContainsFrame(nextInFlow)) {
521 nextInFlow->SetParent(this);
522 if (inFirstLine) {
523 restyleManager->ReparentComputedStyleForFirstLine(nextInFlow);
524 nsLayoutUtils::MarkDescendantsDirty(nextInFlow);
526 } else {
527 #ifdef DEBUG
528 // Once we find a next-in-flow that isn't ours none of the
529 // remaining next-in-flows should be either.
530 for (; nextInFlow; nextInFlow = nextInFlow->GetNextInFlow()) {
531 NS_ASSERTION(!mFrames.ContainsFrame(nextInFlow),
532 "unexpected letter frame flow");
534 #endif
535 break;
541 MOZ_ASSERT(frame->GetParent() == this);
543 if (!done) {
544 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
545 ReflowInlineFrame(aPresContext, aReflowInput, irs, frame, aStatus);
546 done = aStatus.IsInlineBreak() ||
547 (!reflowingFirstLetter && aStatus.IsIncomplete());
548 if (done) {
549 if (!irs.mSetParentPointer) {
550 break;
552 // Keep reparenting the remaining siblings, but don't reflow them.
553 nsFrameList* pushedFrames = GetOverflowFrames();
554 if (pushedFrames && pushedFrames->FirstChild() == frame) {
555 // Don't bother if |frame| was pushed to our overflow list.
556 break;
558 } else {
559 irs.mPrevFrame = frame;
562 frame = frame->GetNextSibling();
565 // Attempt to pull frames from our next-in-flow until we can't
566 if (!done && GetNextInFlow()) {
567 while (true) {
568 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
569 if (!frame) { // Could be non-null if we pulled a first-letter frame and
570 // it created a continuation, since we don't push those.
571 frame = PullOneFrame(aPresContext, irs);
573 #ifdef NOISY_PUSHING
574 printf("%p pulled up %p\n", this, frame);
575 #endif
576 if (!frame) {
577 break;
579 ReflowInlineFrame(aPresContext, aReflowInput, irs, frame, aStatus);
580 if (aStatus.IsInlineBreak() ||
581 (!reflowingFirstLetter && aStatus.IsIncomplete())) {
582 break;
584 irs.mPrevFrame = frame;
585 frame = frame->GetNextSibling();
589 NS_ASSERTION(!aStatus.IsComplete() || !GetOverflowFrames(),
590 "We can't be complete AND have overflow frames!");
592 // If after reflowing our children they take up no area then make
593 // sure that we don't either.
595 // Note: CSS demands that empty inline elements still affect the
596 // line-height calculations. However, continuations of an inline
597 // that are empty we force to empty so that things like collapsed
598 // whitespace in an inline element don't affect the line-height.
599 aMetrics.ISize(lineWM) = lineLayout->EndSpan(this);
601 // Compute final width.
603 // XXX Note that that the padding start and end are in the frame's
604 // writing mode, but the metrics' inline-size is in the line's
605 // writing mode. This makes sense if the line and frame are both
606 // vertical or both horizontal, but what should happen with
607 // orthogonal inlines?
609 // Make sure to not include our start border and padding if we have a prev
610 // continuation or if we're in a part of an {ib} split other than the first
611 // one. For box-decoration-break:clone we always include our start border
612 // and padding since all continuations have them.
613 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
614 boxDecorationBreakClone) {
615 aMetrics.ISize(lineWM) += framePadding.IStart(frameWM);
619 * We want to only apply the end border and padding if we're the last
620 * continuation and either not in an {ib} split or the last part of it. To
621 * be the last continuation we have to be complete (so that we won't get a
622 * next-in-flow) and have no non-fluid continuations on our continuation
623 * chain. For box-decoration-break:clone we always apply the end border and
624 * padding since all continuations have them.
626 if ((aStatus.IsComplete() && !LastInFlow()->GetNextContinuation() &&
627 !FrameIsNonLastInIBSplit()) ||
628 boxDecorationBreakClone) {
629 aMetrics.ISize(lineWM) += framePadding.IEnd(frameWM);
632 nsLayoutUtils::SetBSizeFromFontMetrics(this, aMetrics, framePadding, lineWM,
633 frameWM);
635 // For now our overflow area is zero. The real value will be
636 // computed in |nsLineLayout::RelativePositionFrames|.
637 aMetrics.mOverflowAreas.Clear();
639 #ifdef NOISY_FINAL_SIZE
640 ListTag(stdout);
641 printf(": metrics=%d,%d ascent=%d\n", aMetrics.Width(), aMetrics.Height(),
642 aMetrics.TopAscent());
643 #endif
646 // Returns whether there's any remaining frame to pull.
647 /* static */
648 bool nsInlineFrame::HasFramesToPull(nsInlineFrame* aNextInFlow) {
649 while (aNextInFlow) {
650 if (!aNextInFlow->mFrames.IsEmpty()) {
651 return true;
653 if (const nsFrameList* overflow = aNextInFlow->GetOverflowFrames()) {
654 if (!overflow->IsEmpty()) {
655 return true;
658 aNextInFlow = static_cast<nsInlineFrame*>(aNextInFlow->GetNextInFlow());
660 return false;
663 void nsInlineFrame::ReflowInlineFrame(nsPresContext* aPresContext,
664 const ReflowInput& aReflowInput,
665 InlineReflowInput& irs, nsIFrame* aFrame,
666 nsReflowStatus& aStatus) {
667 nsLineLayout* lineLayout = aReflowInput.mLineLayout;
668 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
669 bool pushedFrame;
670 aStatus.Reset();
671 lineLayout->ReflowFrame(aFrame, aStatus, nullptr, pushedFrame);
673 if (aStatus.IsInlineBreakBefore()) {
674 if (aFrame != mFrames.FirstChild()) {
675 // Change break-before status into break-after since we have
676 // already placed at least one child frame. This preserves the
677 // break-type so that it can be propagated upward.
678 StyleClear oldBreakType = aStatus.BreakType();
679 aStatus.Reset();
680 aStatus.SetIncomplete();
681 aStatus.SetInlineLineBreakAfter(oldBreakType);
682 PushFrames(aPresContext, aFrame, irs.mPrevFrame, irs);
683 } else {
684 // Preserve reflow status when breaking-before our first child
685 // and propagate it upward without modification.
687 return;
690 // Create a next-in-flow if needed.
691 if (!aStatus.IsFullyComplete()) {
692 CreateNextInFlow(aFrame);
695 if (aStatus.IsInlineBreakAfter()) {
696 nsIFrame* nextFrame = aFrame->GetNextSibling();
697 if (nextFrame) {
698 aStatus.SetIncomplete();
699 PushFrames(aPresContext, nextFrame, aFrame, irs);
700 } else {
701 // We must return an incomplete status if there are more child
702 // frames remaining in a next-in-flow that follows this frame.
703 if (HasFramesToPull(static_cast<nsInlineFrame*>(GetNextInFlow()))) {
704 aStatus.SetIncomplete();
707 return;
710 if (!aStatus.IsFullyComplete() && !reflowingFirstLetter) {
711 nsIFrame* nextFrame = aFrame->GetNextSibling();
712 if (nextFrame) {
713 PushFrames(aPresContext, nextFrame, aFrame, irs);
718 nsIFrame* nsInlineFrame::PullOneFrame(nsPresContext* aPresContext,
719 InlineReflowInput& irs) {
720 nsIFrame* frame = nullptr;
721 nsInlineFrame* nextInFlow = irs.mNextInFlow;
723 #ifdef DEBUG
724 bool willPull = HasFramesToPull(nextInFlow);
725 #endif
727 while (nextInFlow) {
728 frame = nextInFlow->mFrames.FirstChild();
729 if (!frame) {
730 // The nextInFlow's principal list has no frames, try its overflow list.
731 nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
732 if (overflowFrames) {
733 frame = overflowFrames->RemoveFirstChild();
734 if (overflowFrames->IsEmpty()) {
735 // We're stealing the only frame - delete the overflow list.
736 nextInFlow->DestroyOverflowList();
737 } else {
738 // We leave the remaining frames on the overflow list (rather than
739 // putting them on nextInFlow's principal list) so we don't have to
740 // set up the parent for them.
742 // ReparentFloatsForInlineChild needs it to be on a child list -
743 // we remove it again below.
744 nextInFlow->mFrames.SetFrames(frame);
748 if (frame) {
749 // If our block has no next continuation, then any floats belonging to
750 // the pulled frame must belong to our block already. This check ensures
751 // we do no extra work in the common non-vertical-breaking case.
752 if (irs.mLineContainer && irs.mLineContainer->GetNextContinuation()) {
753 // The blockChildren.ContainsFrame check performed by
754 // ReparentFloatsForInlineChild will be fast because frame's ancestor
755 // will be the first child of its containing block.
756 ReparentFloatsForInlineChild(irs.mLineContainer, frame, false);
758 nextInFlow->mFrames.RemoveFirstChild();
759 // nsFirstLineFrame::PullOneFrame calls ReparentComputedStyle.
761 mFrames.InsertFrame(this, irs.mPrevFrame, frame);
762 if (irs.mLineLayout) {
763 irs.mLineLayout->SetDirtyNextLine();
765 nsContainerFrame::ReparentFrameView(frame, nextInFlow, this);
766 break;
768 nextInFlow = static_cast<nsInlineFrame*>(nextInFlow->GetNextInFlow());
769 irs.mNextInFlow = nextInFlow;
772 MOZ_ASSERT(!!frame == willPull);
773 return frame;
776 void nsInlineFrame::PushFrames(nsPresContext* aPresContext,
777 nsIFrame* aFromChild, nsIFrame* aPrevSibling,
778 InlineReflowInput& aState) {
779 #ifdef NOISY_PUSHING
780 printf("%p pushing aFromChild %p, disconnecting from prev sib %p\n", this,
781 aFromChild, aPrevSibling);
782 #endif
784 PushChildrenToOverflow(aFromChild, aPrevSibling);
785 if (aState.mLineLayout) {
786 aState.mLineLayout->SetDirtyNextLine();
790 //////////////////////////////////////////////////////////////////////
792 LogicalSides nsInlineFrame::GetLogicalSkipSides() const {
793 LogicalSides skip(mWritingMode);
794 if (MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak ==
795 StyleBoxDecorationBreak::Clone)) {
796 return skip;
799 if (!IsFirst()) {
800 nsInlineFrame* prev = (nsInlineFrame*)GetPrevContinuation();
801 if (HasAnyStateBits(NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
802 (prev && (prev->mRect.height || prev->mRect.width))) {
803 // Prev continuation is not empty therefore we don't render our start
804 // border edge.
805 skip |= eLogicalSideBitsIStart;
806 } else {
807 // If the prev continuation is empty, then go ahead and let our start
808 // edge border render.
811 if (!IsLast()) {
812 nsInlineFrame* next = (nsInlineFrame*)GetNextContinuation();
813 if (HasAnyStateBits(NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
814 (next && (next->mRect.height || next->mRect.width))) {
815 // Next continuation is not empty therefore we don't render our end
816 // border edge.
817 skip |= eLogicalSideBitsIEnd;
818 } else {
819 // If the next continuation is empty, then go ahead and let our end
820 // edge border render.
824 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) {
825 // All but the last part of an {ib} split should skip the "end" side (as
826 // determined by this frame's direction) and all but the first part of such
827 // a split should skip the "start" side. But figuring out which part of
828 // the split we are involves getting our first continuation, which might be
829 // expensive. So don't bother if we already have the relevant bits set.
830 if (skip != LogicalSides(mWritingMode, eLogicalSideBitsIBoth)) {
831 // We're missing one of the skip bits, so check whether we need to set it.
832 // Only get the first continuation once, as an optimization.
833 nsIFrame* firstContinuation = FirstContinuation();
834 if (firstContinuation->FrameIsNonLastInIBSplit()) {
835 skip |= eLogicalSideBitsIEnd;
837 if (firstContinuation->FrameIsNonFirstInIBSplit()) {
838 skip |= eLogicalSideBitsIStart;
843 return skip;
846 nscoord nsInlineFrame::GetLogicalBaseline(
847 mozilla::WritingMode aWritingMode) const {
848 return 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, *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, *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, *overflowFrames);
1079 ReparentChildListStyle(PresContext(), newFrames, this);
1080 return result;
1082 return false;