Bug 1909613 - Enable <details name=''> everywhere, r=emilio
[gecko.git] / layout / generic / nsInlineFrame.cpp
blob44fce51931176b05872aa57f86f69f78a5458c3c
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 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
275 if (!aReflowInput.mLineLayout) {
276 NS_ERROR("must have non-null aReflowInput.mLineLayout");
277 return;
279 if (IsFrameTreeTooDeep(aReflowInput, aMetrics, aStatus)) {
280 return;
283 bool lazilySetParentPointer = false;
285 // Check for an overflow list with our prev-in-flow
286 nsInlineFrame* prevInFlow = (nsInlineFrame*)GetPrevInFlow();
287 if (prevInFlow) {
288 AutoFrameListPtr prevOverflowFrames(aPresContext,
289 prevInFlow->StealOverflowFrames());
290 if (prevOverflowFrames) {
291 // When pushing and pulling frames we need to check for whether any
292 // views need to be reparented.
293 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
294 this);
296 // Check if we should do the lazilySetParentPointer optimization.
297 // Only do it in simple cases where we're being reflowed for the
298 // first time, nothing (e.g. bidi resolution) has already given
299 // us children, and there's no next-in-flow, so all our frames
300 // will be taken from prevOverflowFrames.
301 if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW) && mFrames.IsEmpty() &&
302 !GetNextInFlow()) {
303 // If our child list is empty, just put the new frames into it.
304 // Note that we don't set the parent pointer for the new frames. Instead
305 // wait to do this until we actually reflow the frame. If the overflow
306 // list contains thousands of frames this is a big performance issue
307 // (see bug #5588)
308 mFrames = std::move(*prevOverflowFrames);
309 lazilySetParentPointer = true;
310 } else {
311 // Insert the new frames at the beginning of the child list
312 // and set their parent pointer
313 const nsFrameList::Slice& newFrames =
314 mFrames.InsertFrames(this, nullptr, std::move(*prevOverflowFrames));
315 // If our prev in flow was under the first continuation of a first-line
316 // frame then we need to reparent the ComputedStyles to remove the
317 // the special first-line styling. In the lazilySetParentPointer case
318 // we reparent the ComputedStyles when we set their parents in
319 // nsInlineFrame::ReflowFrames and nsInlineFrame::ReflowInlineFrame.
320 if (aReflowInput.mLineLayout->GetInFirstLine()) {
321 ReparentChildListStyle(aPresContext, newFrames, this);
327 // It's also possible that we have an overflow list for ourselves
328 #ifdef DEBUG
329 if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
330 // If it's our initial reflow, then we should not have an overflow list.
331 // However, add an assertion in case we get reflowed more than once with
332 // the initial reflow reason
333 nsFrameList* overflowFrames = GetOverflowFrames();
334 NS_ASSERTION(!overflowFrames || overflowFrames->IsEmpty(),
335 "overflow list is not empty for initial reflow");
337 #endif
338 if (!HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
339 DrainSelfOverflowListInternal(aReflowInput.mLineLayout->GetInFirstLine());
342 // Set our own reflow input (additional state above and beyond aReflowInput).
343 InlineReflowInput irs;
344 irs.mPrevFrame = nullptr;
345 irs.mLineContainer = aReflowInput.mLineLayout->LineContainerFrame();
346 irs.mLineLayout = aReflowInput.mLineLayout;
347 irs.mNextInFlow = (nsInlineFrame*)GetNextInFlow();
348 irs.mSetParentPointer = lazilySetParentPointer;
350 if (mFrames.IsEmpty()) {
351 // Try to pull over one frame before starting so that we know
352 // whether we have an anonymous block or not.
353 Unused << PullOneFrame(aPresContext, irs);
356 ReflowFrames(aPresContext, aReflowInput, irs, aMetrics, aStatus);
358 ReflowAbsoluteFrames(aPresContext, aMetrics, aReflowInput, aStatus);
360 // Note: the line layout code will properly compute our
361 // overflow-rect state for us.
364 nsresult nsInlineFrame::AttributeChanged(int32_t aNameSpaceID,
365 nsAtom* aAttribute, int32_t aModType) {
366 nsresult rv =
367 nsContainerFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
369 if (NS_FAILED(rv)) {
370 return rv;
373 if (IsInSVGTextSubtree()) {
374 SVGTextFrame* f = static_cast<SVGTextFrame*>(
375 nsLayoutUtils::GetClosestFrameOfType(this, LayoutFrameType::SVGText));
376 f->HandleAttributeChangeInDescendant(mContent->AsElement(), aNameSpaceID,
377 aAttribute);
380 return NS_OK;
383 bool nsInlineFrame::DrainSelfOverflowListInternal(bool aInFirstLine) {
384 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
385 if (!overflowFrames || overflowFrames->IsEmpty()) {
386 return false;
389 // The frames on our own overflowlist may have been pushed by a
390 // previous lazilySetParentPointer Reflow so we need to ensure the
391 // correct parent pointer. This is sometimes skipped by Reflow.
392 nsIFrame* firstChild = overflowFrames->FirstChild();
393 RestyleManager* restyleManager = PresContext()->RestyleManager();
394 for (nsIFrame* f = firstChild; f; f = f->GetNextSibling()) {
395 f->SetParent(this);
396 if (MOZ_UNLIKELY(aInFirstLine)) {
397 restyleManager->ReparentComputedStyleForFirstLine(f);
398 nsLayoutUtils::MarkDescendantsDirty(f);
401 mFrames.AppendFrames(nullptr, std::move(*overflowFrames));
402 return true;
405 /* virtual */
406 bool nsInlineFrame::DrainSelfOverflowList() {
407 nsIFrame* lineContainer = nsLayoutUtils::FindNearestBlockAncestor(this);
408 // Add the eInFirstLine flag if we have a ::first-line ancestor frame.
409 // No need to look further than the nearest line container though.
410 bool inFirstLine = false;
411 for (nsIFrame* p = GetParent(); p != lineContainer; p = p->GetParent()) {
412 if (p->IsLineFrame()) {
413 inFirstLine = true;
414 break;
417 return DrainSelfOverflowListInternal(inFirstLine);
420 /* virtual */
421 bool nsInlineFrame::CanContinueTextRun() const {
422 // We can continue a text run through an inline frame
423 return true;
426 /* virtual */
427 void nsInlineFrame::PullOverflowsFromPrevInFlow() {
428 nsInlineFrame* prevInFlow = static_cast<nsInlineFrame*>(GetPrevInFlow());
429 if (prevInFlow) {
430 nsPresContext* presContext = PresContext();
431 AutoFrameListPtr prevOverflowFrames(presContext,
432 prevInFlow->StealOverflowFrames());
433 if (prevOverflowFrames) {
434 // Assume that our prev-in-flow has the same line container that we do.
435 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
436 this);
437 mFrames.InsertFrames(this, nullptr, std::move(*prevOverflowFrames));
442 void nsInlineFrame::ReflowFrames(nsPresContext* aPresContext,
443 const ReflowInput& aReflowInput,
444 InlineReflowInput& irs, ReflowOutput& aMetrics,
445 nsReflowStatus& aStatus) {
446 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
448 nsLineLayout* lineLayout = aReflowInput.mLineLayout;
449 bool inFirstLine = aReflowInput.mLineLayout->GetInFirstLine();
450 RestyleManager* restyleManager = aPresContext->RestyleManager();
451 WritingMode frameWM = aReflowInput.GetWritingMode();
452 WritingMode lineWM = aReflowInput.mLineLayout->mRootSpan->mWritingMode;
453 LogicalMargin framePadding =
454 aReflowInput.ComputedLogicalBorderPadding(frameWM);
455 nscoord startEdge = 0;
456 const bool boxDecorationBreakClone = MOZ_UNLIKELY(
457 StyleBorder()->mBoxDecorationBreak == StyleBoxDecorationBreak::Clone);
458 // Don't offset by our start borderpadding if we have a prev continuation or
459 // if we're in a part of an {ib} split other than the first one. For
460 // box-decoration-break:clone we always offset our start since all
461 // continuations have border/padding.
462 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
463 boxDecorationBreakClone) {
464 startEdge = framePadding.IStart(frameWM);
466 nscoord availableISize = aReflowInput.AvailableISize();
467 NS_ASSERTION(availableISize != NS_UNCONSTRAINEDSIZE,
468 "should no longer use available widths");
469 // Subtract off inline axis border+padding from availableISize
470 availableISize -= startEdge;
471 availableISize -= framePadding.IEnd(frameWM);
472 lineLayout->BeginSpan(this, &aReflowInput, startEdge,
473 startEdge + availableISize, &mBaseline);
475 // First reflow our principal children.
476 nsIFrame* frame = mFrames.FirstChild();
477 bool done = false;
478 while (frame) {
479 // Check if we should lazily set the child frame's parent pointer.
480 if (irs.mSetParentPointer) {
481 nsIFrame* child = frame;
482 do {
483 child->SetParent(this);
484 if (inFirstLine) {
485 restyleManager->ReparentComputedStyleForFirstLine(child);
486 nsLayoutUtils::MarkDescendantsDirty(child);
488 // We also need to do the same for |frame|'s next-in-flows that are in
489 // the sibling list. Otherwise, if we reflow |frame| and it's complete
490 // we'll crash when trying to delete its next-in-flow.
491 // This scenario doesn't happen often, but it can happen.
492 nsIFrame* nextSibling = child->GetNextSibling();
493 child = child->GetNextInFlow();
494 if (MOZ_UNLIKELY(child)) {
495 while (child != nextSibling && nextSibling) {
496 nextSibling = nextSibling->GetNextSibling();
498 if (!nextSibling) {
499 child = nullptr;
502 MOZ_ASSERT(!child || mFrames.ContainsFrame(child));
503 } while (child);
505 // Fix the parent pointer for ::first-letter child frame next-in-flows,
506 // so nsFirstLetterFrame::Reflow can destroy them safely (bug 401042).
507 nsIFrame* realFrame = nsPlaceholderFrame::GetRealFrameFor(frame);
508 if (realFrame->IsLetterFrame()) {
509 nsIFrame* child = realFrame->PrincipalChildList().FirstChild();
510 if (child) {
511 NS_ASSERTION(child->IsTextFrame(), "unexpected frame type");
512 nsIFrame* nextInFlow = child->GetNextInFlow();
513 for (; nextInFlow; nextInFlow = nextInFlow->GetNextInFlow()) {
514 NS_ASSERTION(nextInFlow->IsTextFrame(), "unexpected frame type");
515 if (mFrames.ContainsFrame(nextInFlow)) {
516 nextInFlow->SetParent(this);
517 if (inFirstLine) {
518 restyleManager->ReparentComputedStyleForFirstLine(nextInFlow);
519 nsLayoutUtils::MarkDescendantsDirty(nextInFlow);
521 } else {
522 #ifdef DEBUG
523 // Once we find a next-in-flow that isn't ours none of the
524 // remaining next-in-flows should be either.
525 for (; nextInFlow; nextInFlow = nextInFlow->GetNextInFlow()) {
526 NS_ASSERTION(!mFrames.ContainsFrame(nextInFlow),
527 "unexpected letter frame flow");
529 #endif
530 break;
536 MOZ_ASSERT(frame->GetParent() == this);
538 if (!done) {
539 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
540 ReflowInlineFrame(aPresContext, aReflowInput, irs, frame, aStatus);
541 done = aStatus.IsInlineBreak() ||
542 (!reflowingFirstLetter && aStatus.IsIncomplete());
543 if (done) {
544 if (!irs.mSetParentPointer) {
545 break;
547 // Keep reparenting the remaining siblings, but don't reflow them.
548 nsFrameList* pushedFrames = GetOverflowFrames();
549 if (pushedFrames && pushedFrames->FirstChild() == frame) {
550 // Don't bother if |frame| was pushed to our overflow list.
551 break;
553 } else {
554 irs.mPrevFrame = frame;
557 frame = frame->GetNextSibling();
560 // Attempt to pull frames from our next-in-flow until we can't
561 if (!done && GetNextInFlow()) {
562 while (true) {
563 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
564 if (!frame) { // Could be non-null if we pulled a first-letter frame and
565 // it created a continuation, since we don't push those.
566 frame = PullOneFrame(aPresContext, irs);
568 #ifdef NOISY_PUSHING
569 printf("%p pulled up %p\n", this, frame);
570 #endif
571 if (!frame) {
572 break;
574 ReflowInlineFrame(aPresContext, aReflowInput, irs, frame, aStatus);
575 if (aStatus.IsInlineBreak() ||
576 (!reflowingFirstLetter && aStatus.IsIncomplete())) {
577 break;
579 irs.mPrevFrame = frame;
580 frame = frame->GetNextSibling();
584 NS_ASSERTION(!aStatus.IsComplete() || !GetOverflowFrames(),
585 "We can't be complete AND have overflow frames!");
587 // If after reflowing our children they take up no area then make
588 // sure that we don't either.
590 // Note: CSS demands that empty inline elements still affect the
591 // line-height calculations. However, continuations of an inline
592 // that are empty we force to empty so that things like collapsed
593 // whitespace in an inline element don't affect the line-height.
594 aMetrics.ISize(lineWM) = lineLayout->EndSpan(this);
596 // Compute final width.
598 // XXX Note that that the padding start and end are in the frame's
599 // writing mode, but the metrics' inline-size is in the line's
600 // writing mode. This makes sense if the line and frame are both
601 // vertical or both horizontal, but what should happen with
602 // orthogonal inlines?
604 // Make sure to not include our start border and padding if we have a prev
605 // continuation or if we're in a part of an {ib} split other than the first
606 // one. For box-decoration-break:clone we always include our start border
607 // and padding since all continuations have them.
608 if ((!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) ||
609 boxDecorationBreakClone) {
610 aMetrics.ISize(lineWM) += framePadding.IStart(frameWM);
614 * We want to only apply the end border and padding if we're the last
615 * continuation and either not in an {ib} split or the last part of it. To
616 * be the last continuation we have to be complete (so that we won't get a
617 * next-in-flow) and have no non-fluid continuations on our continuation
618 * chain. For box-decoration-break:clone we always apply the end border and
619 * padding since all continuations have them.
621 if ((aStatus.IsComplete() && !LastInFlow()->GetNextContinuation() &&
622 !FrameIsNonLastInIBSplit()) ||
623 boxDecorationBreakClone) {
624 aMetrics.ISize(lineWM) += framePadding.IEnd(frameWM);
627 nsLayoutUtils::SetBSizeFromFontMetrics(this, aMetrics, framePadding, lineWM,
628 frameWM);
630 // For now our overflow area is zero. The real value will be
631 // computed in |nsLineLayout::RelativePositionFrames|.
632 aMetrics.mOverflowAreas.Clear();
634 #ifdef NOISY_FINAL_SIZE
635 ListTag(stdout);
636 printf(": metrics=%d,%d ascent=%d\n", aMetrics.Width(), aMetrics.Height(),
637 aMetrics.BlockStartAscent());
638 #endif
641 // Returns whether there's any remaining frame to pull.
642 /* static */
643 bool nsInlineFrame::HasFramesToPull(nsInlineFrame* aNextInFlow) {
644 while (aNextInFlow) {
645 if (!aNextInFlow->mFrames.IsEmpty()) {
646 return true;
648 if (const nsFrameList* overflow = aNextInFlow->GetOverflowFrames()) {
649 if (!overflow->IsEmpty()) {
650 return true;
653 aNextInFlow = static_cast<nsInlineFrame*>(aNextInFlow->GetNextInFlow());
655 return false;
658 void nsInlineFrame::ReflowInlineFrame(nsPresContext* aPresContext,
659 const ReflowInput& aReflowInput,
660 InlineReflowInput& irs, nsIFrame* aFrame,
661 nsReflowStatus& aStatus) {
662 nsLineLayout* lineLayout = aReflowInput.mLineLayout;
663 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
664 bool pushedFrame;
665 aStatus.Reset();
666 lineLayout->ReflowFrame(aFrame, aStatus, nullptr, pushedFrame);
668 if (aStatus.IsInlineBreakBefore()) {
669 if (aFrame != mFrames.FirstChild()) {
670 // Change break-before status into break-after since we have
671 // already placed at least one child frame. This preserves the
672 // break-type so that it can be propagated upward.
673 StyleClear oldClearType = aStatus.FloatClearType();
674 aStatus.Reset();
675 aStatus.SetIncomplete();
676 aStatus.SetInlineLineBreakAfter(oldClearType);
677 PushFrames(aPresContext, aFrame, irs.mPrevFrame, irs);
678 } else {
679 // Preserve reflow status when breaking-before our first child
680 // and propagate it upward without modification.
682 return;
685 // Create a next-in-flow if needed.
686 if (!aStatus.IsFullyComplete()) {
687 CreateNextInFlow(aFrame);
690 if (aStatus.IsInlineBreakAfter()) {
691 nsIFrame* nextFrame = aFrame->GetNextSibling();
692 if (nextFrame) {
693 aStatus.SetIncomplete();
694 PushFrames(aPresContext, nextFrame, aFrame, irs);
695 } else {
696 // We must return an incomplete status if there are more child
697 // frames remaining in a next-in-flow that follows this frame.
698 if (HasFramesToPull(static_cast<nsInlineFrame*>(GetNextInFlow()))) {
699 aStatus.SetIncomplete();
702 return;
705 if (!aStatus.IsFullyComplete() && !reflowingFirstLetter) {
706 nsIFrame* nextFrame = aFrame->GetNextSibling();
707 if (nextFrame) {
708 PushFrames(aPresContext, nextFrame, aFrame, irs);
713 nsIFrame* nsInlineFrame::PullOneFrame(nsPresContext* aPresContext,
714 InlineReflowInput& irs) {
715 nsIFrame* frame = nullptr;
716 nsInlineFrame* nextInFlow = irs.mNextInFlow;
718 #ifdef DEBUG
719 bool willPull = HasFramesToPull(nextInFlow);
720 #endif
722 while (nextInFlow) {
723 frame = nextInFlow->mFrames.FirstChild();
724 if (!frame) {
725 // The nextInFlow's principal list has no frames, try its overflow list.
726 nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
727 if (overflowFrames) {
728 frame = overflowFrames->RemoveFirstChild();
729 if (overflowFrames->IsEmpty()) {
730 // We're stealing the only frame - delete the overflow list.
731 nextInFlow->DestroyOverflowList();
732 } else {
733 // We leave the remaining frames on the overflow list (rather than
734 // putting them on nextInFlow's principal list) so we don't have to
735 // set up the parent for them.
737 // ReparentFloatsForInlineChild needs it to be on a child list -
738 // we remove it again below.
739 nextInFlow->mFrames = nsFrameList(frame, frame);
743 if (frame) {
744 // If our block has no next continuation, then any floats belonging to
745 // the pulled frame must belong to our block already. This check ensures
746 // we do no extra work in the common non-vertical-breaking case.
747 if (irs.mLineContainer && irs.mLineContainer->GetNextContinuation()) {
748 // The blockChildren.ContainsFrame check performed by
749 // ReparentFloatsForInlineChild will be fast because frame's ancestor
750 // will be the first child of its containing block.
751 ReparentFloatsForInlineChild(irs.mLineContainer, frame, false);
753 nextInFlow->mFrames.RemoveFirstChild();
754 // nsFirstLineFrame::PullOneFrame calls ReparentComputedStyle.
756 mFrames.InsertFrame(this, irs.mPrevFrame, frame);
757 if (irs.mLineLayout) {
758 irs.mLineLayout->SetDirtyNextLine();
760 nsContainerFrame::ReparentFrameView(frame, nextInFlow, this);
761 break;
763 nextInFlow = static_cast<nsInlineFrame*>(nextInFlow->GetNextInFlow());
764 irs.mNextInFlow = nextInFlow;
767 MOZ_ASSERT(!!frame == willPull);
768 return frame;
771 void nsInlineFrame::PushFrames(nsPresContext* aPresContext,
772 nsIFrame* aFromChild, nsIFrame* aPrevSibling,
773 InlineReflowInput& aState) {
774 #ifdef NOISY_PUSHING
775 printf("%p pushing aFromChild %p, disconnecting from prev sib %p\n", this,
776 aFromChild, aPrevSibling);
777 #endif
779 PushChildrenToOverflow(aFromChild, aPrevSibling);
780 if (aState.mLineLayout) {
781 aState.mLineLayout->SetDirtyNextLine();
785 //////////////////////////////////////////////////////////////////////
787 LogicalSides nsInlineFrame::GetLogicalSkipSides() const {
788 LogicalSides skip(mWritingMode);
789 if (MOZ_UNLIKELY(StyleBorder()->mBoxDecorationBreak ==
790 StyleBoxDecorationBreak::Clone)) {
791 return skip;
794 if (!IsFirst()) {
795 nsInlineFrame* prev = (nsInlineFrame*)GetPrevContinuation();
796 if (HasAnyStateBits(NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
797 (prev && (prev->mRect.height || prev->mRect.width))) {
798 // Prev continuation is not empty therefore we don't render our start
799 // border edge.
800 skip += LogicalSide::IStart;
801 } else {
802 // If the prev continuation is empty, then go ahead and let our start
803 // edge border render.
806 if (!IsLast()) {
807 nsInlineFrame* next = (nsInlineFrame*)GetNextContinuation();
808 if (HasAnyStateBits(NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
809 (next && (next->mRect.height || next->mRect.width))) {
810 // Next continuation is not empty therefore we don't render our end
811 // border edge.
812 skip += LogicalSide::IEnd;
813 } else {
814 // If the next continuation is empty, then go ahead and let our end
815 // edge border render.
819 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) {
820 // All but the last part of an {ib} split should skip the "end" side (as
821 // determined by this frame's direction) and all but the first part of such
822 // a split should skip the "start" side. But figuring out which part of
823 // the split we are involves getting our first continuation, which might be
824 // expensive. So don't bother if we already have the relevant bits set.
825 if (skip != LogicalSides(mWritingMode, LogicalSides::IBoth)) {
826 // We're missing one of the skip bits, so check whether we need to set it.
827 // Only get the first continuation once, as an optimization.
828 nsIFrame* firstContinuation = FirstContinuation();
829 if (firstContinuation->FrameIsNonLastInIBSplit()) {
830 skip += LogicalSide::IEnd;
832 if (firstContinuation->FrameIsNonFirstInIBSplit()) {
833 skip += LogicalSide::IStart;
838 return skip;
841 Maybe<nscoord> nsInlineFrame::GetNaturalBaselineBOffset(
842 WritingMode aWM, BaselineSharingGroup aBaselineGroup,
843 BaselineExportContext) const {
844 if (aBaselineGroup == BaselineSharingGroup::Last) {
845 return Nothing{};
847 return Some(mBaseline);
850 #ifdef ACCESSIBILITY
851 a11y::AccType nsInlineFrame::AccessibleType() {
852 // FIXME(emilio): This is broken, if the image has its default `display` value
853 // overridden. Should be somewhere else.
854 if (mContent->IsHTMLElement(
855 nsGkAtoms::img)) // Create accessible for broken <img>
856 return a11y::eHyperTextType;
858 return a11y::eNoType;
860 #endif
862 void nsInlineFrame::UpdateStyleOfOwnedAnonBoxesForIBSplit(
863 ServoRestyleState& aRestyleState) {
864 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_OWNS_ANON_BOXES),
865 "Why did we get called?");
866 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT),
867 "Why did we have the NS_FRAME_OWNS_ANON_BOXES bit set?");
868 // Note: this assert _looks_ expensive, but it's cheap in all the cases when
869 // it passes!
870 MOZ_ASSERT(nsLayoutUtils::FirstContinuationOrIBSplitSibling(this) == this,
871 "Only the primary frame of the inline in a block-inside-inline "
872 "split should have NS_FRAME_OWNS_ANON_BOXES");
873 MOZ_ASSERT(mContent->GetPrimaryFrame() == this,
874 "We should be the primary frame for our element");
876 nsIFrame* blockFrame = GetProperty(nsIFrame::IBSplitSibling());
877 MOZ_ASSERT(blockFrame, "Why did we have an IB split?");
879 // The later inlines need to get our style.
880 ComputedStyle* ourStyle = Style();
882 // The anonymous block's style inherits from ours, and we already have our new
883 // ComputedStyle.
884 RefPtr<ComputedStyle> newContext =
885 aRestyleState.StyleSet().ResolveInheritingAnonymousBoxStyle(
886 PseudoStyleType::mozBlockInsideInlineWrapper, ourStyle);
888 // We're guaranteed that newContext only differs from the old ComputedStyle on
889 // the block in things they might inherit from us. And changehint processing
890 // guarantees walking the continuation and ib-sibling chains, so our existing
891 // changehint being in aChangeList is good enough. So we don't need to touch
892 // aChangeList at all here.
894 while (blockFrame) {
895 MOZ_ASSERT(!blockFrame->GetPrevContinuation(),
896 "Must be first continuation");
898 MOZ_ASSERT(blockFrame->Style()->GetPseudoType() ==
899 PseudoStyleType::mozBlockInsideInlineWrapper,
900 "Unexpected kind of ComputedStyle");
902 for (nsIFrame* cont = blockFrame; cont;
903 cont = cont->GetNextContinuation()) {
904 cont->SetComputedStyle(newContext);
907 nsIFrame* nextInline = blockFrame->GetProperty(nsIFrame::IBSplitSibling());
909 // This check is here due to bug 1431232. Please remove it once
910 // that bug is fixed.
911 if (MOZ_UNLIKELY(!nextInline)) {
912 break;
915 MOZ_ASSERT(nextInline, "There is always a trailing inline in an IB split");
917 for (nsIFrame* cont = nextInline; cont;
918 cont = cont->GetNextContinuation()) {
919 cont->SetComputedStyle(ourStyle);
921 blockFrame = nextInline->GetProperty(nsIFrame::IBSplitSibling());
925 //////////////////////////////////////////////////////////////////////
927 // nsLineFrame implementation
929 nsFirstLineFrame* NS_NewFirstLineFrame(PresShell* aPresShell,
930 ComputedStyle* aStyle) {
931 return new (aPresShell)
932 nsFirstLineFrame(aStyle, aPresShell->GetPresContext());
935 NS_IMPL_FRAMEARENA_HELPERS(nsFirstLineFrame)
937 void nsFirstLineFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
938 nsIFrame* aPrevInFlow) {
939 nsInlineFrame::Init(aContent, aParent, aPrevInFlow);
940 if (!aPrevInFlow) {
941 MOZ_ASSERT(Style()->GetPseudoType() == PseudoStyleType::firstLine);
942 return;
945 // This frame is a continuation - fixup the computed style if aPrevInFlow
946 // is the first-in-flow (the only one with a ::first-line pseudo).
947 if (aPrevInFlow->Style()->GetPseudoType() == PseudoStyleType::firstLine) {
948 MOZ_ASSERT(FirstInFlow() == aPrevInFlow);
949 // Create a new ComputedStyle that is a child of the parent
950 // ComputedStyle thus removing the ::first-line style. This way
951 // we behave as if an anonymous (unstyled) span was the child
952 // of the parent frame.
953 ComputedStyle* parentContext = aParent->Style();
954 RefPtr<ComputedStyle> newSC =
955 PresContext()->StyleSet()->ResolveInheritingAnonymousBoxStyle(
956 PseudoStyleType::mozLineFrame, parentContext);
957 SetComputedStyle(newSC);
958 } else {
959 MOZ_ASSERT(FirstInFlow() != aPrevInFlow);
960 MOZ_ASSERT(aPrevInFlow->Style()->GetPseudoType() ==
961 PseudoStyleType::mozLineFrame);
965 #ifdef DEBUG_FRAME_DUMP
966 nsresult nsFirstLineFrame::GetFrameName(nsAString& aResult) const {
967 return MakeFrameName(u"Line"_ns, aResult);
969 #endif
971 nsIFrame* nsFirstLineFrame::PullOneFrame(nsPresContext* aPresContext,
972 InlineReflowInput& irs) {
973 nsIFrame* frame = nsInlineFrame::PullOneFrame(aPresContext, irs);
974 if (frame && !GetPrevInFlow()) {
975 // We are a first-line frame. Fixup the child frames
976 // style-context that we just pulled.
977 NS_ASSERTION(frame->GetParent() == this, "Incorrect parent?");
978 aPresContext->RestyleManager()->ReparentComputedStyleForFirstLine(frame);
979 nsLayoutUtils::MarkDescendantsDirty(frame);
981 return frame;
984 void nsFirstLineFrame::Reflow(nsPresContext* aPresContext,
985 ReflowOutput& aMetrics,
986 const ReflowInput& aReflowInput,
987 nsReflowStatus& aStatus) {
988 MarkInReflow();
989 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
991 if (nullptr == aReflowInput.mLineLayout) {
992 return; // XXX does this happen? why?
995 // Check for an overflow list with our prev-in-flow
996 nsFirstLineFrame* prevInFlow = (nsFirstLineFrame*)GetPrevInFlow();
997 if (prevInFlow) {
998 AutoFrameListPtr prevOverflowFrames(aPresContext,
999 prevInFlow->StealOverflowFrames());
1000 if (prevOverflowFrames) {
1001 // Reparent the new frames and their ComputedStyles.
1002 const nsFrameList::Slice& newFrames =
1003 mFrames.InsertFrames(this, nullptr, std::move(*prevOverflowFrames));
1004 ReparentChildListStyle(aPresContext, newFrames, this);
1008 // It's also possible that we have an overflow list for ourselves.
1009 DrainSelfOverflowList();
1011 // Set our own reflow input (additional state above and beyond aReflowInput).
1012 InlineReflowInput irs;
1013 irs.mPrevFrame = nullptr;
1014 irs.mLineContainer = aReflowInput.mLineLayout->LineContainerFrame();
1015 irs.mLineLayout = aReflowInput.mLineLayout;
1016 irs.mNextInFlow = (nsInlineFrame*)GetNextInFlow();
1018 bool wasEmpty = mFrames.IsEmpty();
1019 if (wasEmpty) {
1020 // Try to pull over one frame before starting so that we know
1021 // whether we have an anonymous block or not.
1022 PullOneFrame(aPresContext, irs);
1025 if (nullptr == GetPrevInFlow()) {
1026 // XXX This is pretty sick, but what we do here is to pull-up, in
1027 // advance, all of the next-in-flows children. We re-resolve their
1028 // style while we are at at it so that when we reflow they have
1029 // the right style.
1031 // All of this is so that text-runs reflow properly.
1032 irs.mPrevFrame = mFrames.LastChild();
1033 for (;;) {
1034 nsIFrame* frame = PullOneFrame(aPresContext, irs);
1035 if (!frame) {
1036 break;
1038 irs.mPrevFrame = frame;
1040 irs.mPrevFrame = nullptr;
1043 NS_ASSERTION(!aReflowInput.mLineLayout->GetInFirstLine(),
1044 "Nested first-line frames? BOGUS");
1045 aReflowInput.mLineLayout->SetInFirstLine(true);
1046 ReflowFrames(aPresContext, aReflowInput, irs, aMetrics, aStatus);
1047 aReflowInput.mLineLayout->SetInFirstLine(false);
1049 ReflowAbsoluteFrames(aPresContext, aMetrics, aReflowInput, aStatus);
1051 // Note: the line layout code will properly compute our overflow state for us
1054 /* virtual */
1055 void nsFirstLineFrame::PullOverflowsFromPrevInFlow() {
1056 nsFirstLineFrame* prevInFlow =
1057 static_cast<nsFirstLineFrame*>(GetPrevInFlow());
1058 if (prevInFlow) {
1059 nsPresContext* presContext = PresContext();
1060 AutoFrameListPtr prevOverflowFrames(presContext,
1061 prevInFlow->StealOverflowFrames());
1062 if (prevOverflowFrames) {
1063 // Assume that our prev-in-flow has the same line container that we do.
1064 const nsFrameList::Slice& newFrames =
1065 mFrames.InsertFrames(this, nullptr, std::move(*prevOverflowFrames));
1066 ReparentChildListStyle(presContext, newFrames, this);
1071 /* virtual */
1072 bool nsFirstLineFrame::DrainSelfOverflowList() {
1073 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
1074 if (overflowFrames) {
1075 bool result = !overflowFrames->IsEmpty();
1076 const nsFrameList::Slice& newFrames =
1077 mFrames.AppendFrames(nullptr, std::move(*overflowFrames));
1078 ReparentChildListStyle(PresContext(), newFrames, this);
1079 return result;
1081 return false;