Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* rendering object for CSS display:inline objects */
8 #include "nsInlineFrame.h"
9 #include "nsLineLayout.h"
10 #include "nsBlockFrame.h"
11 #include "nsPlaceholderFrame.h"
12 #include "nsGkAtoms.h"
13 #include "nsStyleContext.h"
14 #include "nsPresContext.h"
15 #include "nsRenderingContext.h"
16 #include "nsCSSAnonBoxes.h"
17 #include "nsAutoPtr.h"
18 #include "RestyleManager.h"
19 #include "nsDisplayList.h"
20 #include "mozilla/Likely.h"
22 #ifdef DEBUG
23 #undef NOISY_PUSHING
24 #endif
26 using namespace mozilla;
27 using namespace mozilla::layout;
30 //////////////////////////////////////////////////////////////////////
32 // Basic nsInlineFrame methods
34 nsIFrame*
35 NS_NewInlineFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
36 {
37 return new (aPresShell) nsInlineFrame(aContext);
38 }
40 NS_IMPL_FRAMEARENA_HELPERS(nsInlineFrame)
42 NS_QUERYFRAME_HEAD(nsInlineFrame)
43 NS_QUERYFRAME_ENTRY(nsInlineFrame)
44 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
46 #ifdef DEBUG_FRAME_DUMP
47 nsresult
48 nsInlineFrame::GetFrameName(nsAString& aResult) const
49 {
50 return MakeFrameName(NS_LITERAL_STRING("Inline"), aResult);
51 }
52 #endif
54 nsIAtom*
55 nsInlineFrame::GetType() const
56 {
57 return nsGkAtoms::inlineFrame;
58 }
60 void
61 nsInlineFrame::InvalidateFrame(uint32_t aDisplayItemKey)
62 {
63 if (IsSVGText()) {
64 nsIFrame* svgTextFrame =
65 nsLayoutUtils::GetClosestFrameOfType(GetParent(),
66 nsGkAtoms::svgTextFrame);
67 svgTextFrame->InvalidateFrame();
68 return;
69 }
70 nsInlineFrameBase::InvalidateFrame(aDisplayItemKey);
71 }
73 void
74 nsInlineFrame::InvalidateFrameWithRect(const nsRect& aRect, uint32_t aDisplayItemKey)
75 {
76 if (IsSVGText()) {
77 nsIFrame* svgTextFrame =
78 nsLayoutUtils::GetClosestFrameOfType(GetParent(),
79 nsGkAtoms::svgTextFrame);
80 svgTextFrame->InvalidateFrame();
81 return;
82 }
83 nsInlineFrameBase::InvalidateFrameWithRect(aRect, aDisplayItemKey);
84 }
86 static inline bool
87 IsMarginZero(const nsStyleCoord &aCoord)
88 {
89 return aCoord.GetUnit() == eStyleUnit_Auto ||
90 nsLayoutUtils::IsMarginZero(aCoord);
91 }
93 /* virtual */ bool
94 nsInlineFrame::IsSelfEmpty()
95 {
96 #if 0
97 // I used to think inline frames worked this way, but it seems they
98 // don't. At least not in our codebase.
99 if (GetPresContext()->CompatibilityMode() == eCompatibility_FullStandards) {
100 return false;
101 }
102 #endif
103 const nsStyleMargin* margin = StyleMargin();
104 const nsStyleBorder* border = StyleBorder();
105 const nsStylePadding* padding = StylePadding();
106 // XXX Top and bottom removed, since they shouldn't affect things, but this
107 // doesn't really match with nsLineLayout.cpp's setting of
108 // ZeroEffectiveSpanBox, anymore, so what should this really be?
109 bool haveRight =
110 border->GetComputedBorderWidth(NS_SIDE_RIGHT) != 0 ||
111 !nsLayoutUtils::IsPaddingZero(padding->mPadding.GetRight()) ||
112 !IsMarginZero(margin->mMargin.GetRight());
113 bool haveLeft =
114 border->GetComputedBorderWidth(NS_SIDE_LEFT) != 0 ||
115 !nsLayoutUtils::IsPaddingZero(padding->mPadding.GetLeft()) ||
116 !IsMarginZero(margin->mMargin.GetLeft());
117 if (haveLeft || haveRight) {
118 if (GetStateBits() & NS_FRAME_PART_OF_IBSPLIT) {
119 bool haveStart, haveEnd;
120 if (NS_STYLE_DIRECTION_LTR == StyleVisibility()->mDirection) {
121 haveStart = haveLeft;
122 haveEnd = haveRight;
123 } else {
124 haveStart = haveRight;
125 haveEnd = haveLeft;
126 }
127 // For ib-split frames, ignore things we know we'll skip in GetSkipSides.
128 // XXXbz should we be doing this for non-ib-split frames too, in a more
129 // general way?
131 // Get the first continuation eagerly, as a performance optimization, to
132 // avoid having to get it twice..
133 nsIFrame* firstCont = FirstContinuation();
134 return
135 (!haveStart || firstCont->FrameIsNonFirstInIBSplit()) &&
136 (!haveEnd || firstCont->FrameIsNonLastInIBSplit());
137 }
138 return false;
139 }
140 return true;
141 }
143 bool
144 nsInlineFrame::IsEmpty()
145 {
146 if (!IsSelfEmpty()) {
147 return false;
148 }
150 for (nsIFrame *kid = mFrames.FirstChild(); kid; kid = kid->GetNextSibling()) {
151 if (!kid->IsEmpty())
152 return false;
153 }
155 return true;
156 }
158 nsIFrame::FrameSearchResult
159 nsInlineFrame::PeekOffsetCharacter(bool aForward, int32_t* aOffset,
160 bool aRespectClusters)
161 {
162 // Override the implementation in nsFrame, to skip empty inline frames
163 NS_ASSERTION (aOffset && *aOffset <= 1, "aOffset out of range");
164 int32_t startOffset = *aOffset;
165 if (startOffset < 0)
166 startOffset = 1;
167 if (aForward == (startOffset == 0)) {
168 // We're before the frame and moving forward, or after it and moving backwards:
169 // skip to the other side, but keep going.
170 *aOffset = 1 - startOffset;
171 }
172 return CONTINUE;
173 }
175 void
176 nsInlineFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
177 const nsRect& aDirtyRect,
178 const nsDisplayListSet& aLists)
179 {
180 BuildDisplayListForInline(aBuilder, aDirtyRect, aLists);
182 // The sole purpose of this is to trigger display of the selection
183 // window for Named Anchors, which don't have any children and
184 // normally don't have any size, but in Editor we use CSS to display
185 // an image to represent this "hidden" element.
186 if (!mFrames.FirstChild()) {
187 DisplaySelectionOverlay(aBuilder, aLists.Content());
188 }
189 }
191 //////////////////////////////////////////////////////////////////////
192 // Reflow methods
194 /* virtual */ void
195 nsInlineFrame::AddInlineMinWidth(nsRenderingContext *aRenderingContext,
196 nsIFrame::InlineMinWidthData *aData)
197 {
198 DoInlineIntrinsicWidth(aRenderingContext, aData, nsLayoutUtils::MIN_WIDTH);
199 }
201 /* virtual */ void
202 nsInlineFrame::AddInlinePrefWidth(nsRenderingContext *aRenderingContext,
203 nsIFrame::InlinePrefWidthData *aData)
204 {
205 DoInlineIntrinsicWidth(aRenderingContext, aData, nsLayoutUtils::PREF_WIDTH);
206 }
208 /* virtual */ nsSize
209 nsInlineFrame::ComputeSize(nsRenderingContext *aRenderingContext,
210 nsSize aCBSize, nscoord aAvailableWidth,
211 nsSize aMargin, nsSize aBorder, nsSize aPadding,
212 uint32_t aFlags)
213 {
214 // Inlines and text don't compute size before reflow.
215 return nsSize(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
216 }
218 nsRect
219 nsInlineFrame::ComputeTightBounds(gfxContext* aContext) const
220 {
221 // be conservative
222 if (StyleContext()->HasTextDecorationLines()) {
223 return GetVisualOverflowRect();
224 }
225 return ComputeSimpleTightBounds(aContext);
226 }
228 void
229 nsInlineFrame::ReparentFloatsForInlineChild(nsIFrame* aOurLineContainer,
230 nsIFrame* aFrame,
231 bool aReparentSiblings)
232 {
233 // XXXbz this would be better if it took a nsFrameList or a frame
234 // list slice....
235 NS_ASSERTION(aOurLineContainer->GetNextContinuation() ||
236 aOurLineContainer->GetPrevContinuation(),
237 "Don't call this when we have no continuation, it's a waste");
238 if (!aFrame) {
239 NS_ASSERTION(aReparentSiblings, "Why did we get called?");
240 return;
241 }
243 nsIFrame* ancestor = aFrame;
244 do {
245 ancestor = ancestor->GetParent();
246 if (!ancestor)
247 return;
248 } while (!ancestor->IsFloatContainingBlock());
250 if (ancestor == aOurLineContainer)
251 return;
253 nsBlockFrame* ourBlock = nsLayoutUtils::GetAsBlock(aOurLineContainer);
254 NS_ASSERTION(ourBlock, "Not a block, but broke vertically?");
255 nsBlockFrame* frameBlock = nsLayoutUtils::GetAsBlock(ancestor);
256 NS_ASSERTION(frameBlock, "ancestor not a block");
258 while (true) {
259 ourBlock->ReparentFloats(aFrame, frameBlock, false);
261 if (!aReparentSiblings)
262 return;
263 nsIFrame* next = aFrame->GetNextSibling();
264 if (!next)
265 return;
266 if (next->GetParent() == aFrame->GetParent()) {
267 aFrame = next;
268 continue;
269 }
270 // This is paranoid and will hardly ever get hit ... but we can't actually
271 // trust that the frames in the sibling chain all have the same parent,
272 // because lazy reparenting may be going on. If we find a different
273 // parent we need to redo our analysis.
274 ReparentFloatsForInlineChild(aOurLineContainer, next, aReparentSiblings);
275 return;
276 }
277 }
279 static void
280 ReparentChildListStyle(nsPresContext* aPresContext,
281 const nsFrameList::Slice& aFrames,
282 nsIFrame* aParentFrame)
283 {
284 RestyleManager* restyleManager = aPresContext->RestyleManager();
286 for (nsFrameList::Enumerator e(aFrames); !e.AtEnd(); e.Next()) {
287 NS_ASSERTION(e.get()->GetParent() == aParentFrame, "Bogus parentage");
288 restyleManager->ReparentStyleContext(e.get());
289 nsLayoutUtils::MarkDescendantsDirty(e.get());
290 }
291 }
293 nsresult
294 nsInlineFrame::Reflow(nsPresContext* aPresContext,
295 nsHTMLReflowMetrics& aMetrics,
296 const nsHTMLReflowState& aReflowState,
297 nsReflowStatus& aStatus)
298 {
299 DO_GLOBAL_REFLOW_COUNT("nsInlineFrame");
300 DISPLAY_REFLOW(aPresContext, this, aReflowState, aMetrics, aStatus);
301 if (nullptr == aReflowState.mLineLayout) {
302 return NS_ERROR_INVALID_ARG;
303 }
304 if (IsFrameTreeTooDeep(aReflowState, aMetrics, aStatus)) {
305 return NS_OK;
306 }
308 bool lazilySetParentPointer = false;
310 nsIFrame* lineContainer = aReflowState.mLineLayout->LineContainerFrame();
312 // Check for an overflow list with our prev-in-flow
313 nsInlineFrame* prevInFlow = (nsInlineFrame*)GetPrevInFlow();
314 if (prevInFlow) {
315 AutoFrameListPtr prevOverflowFrames(aPresContext,
316 prevInFlow->StealOverflowFrames());
317 if (prevOverflowFrames) {
318 // When pushing and pulling frames we need to check for whether any
319 // views need to be reparented.
320 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
321 this);
323 // Check if we should do the lazilySetParentPointer optimization.
324 // Only do it in simple cases where we're being reflowed for the
325 // first time, nothing (e.g. bidi resolution) has already given
326 // us children, and there's no next-in-flow, so all our frames
327 // will be taken from prevOverflowFrames.
328 if ((GetStateBits() & NS_FRAME_FIRST_REFLOW) && mFrames.IsEmpty() &&
329 !GetNextInFlow()) {
330 // If our child list is empty, just put the new frames into it.
331 // Note that we don't set the parent pointer for the new frames. Instead wait
332 // to do this until we actually reflow the frame. If the overflow list contains
333 // thousands of frames this is a big performance issue (see bug #5588)
334 mFrames.SetFrames(*prevOverflowFrames);
335 lazilySetParentPointer = true;
336 } else {
337 // Assign all floats to our block if necessary
338 if (lineContainer && lineContainer->GetPrevContinuation()) {
339 ReparentFloatsForInlineChild(lineContainer,
340 prevOverflowFrames->FirstChild(),
341 true);
342 }
343 // Insert the new frames at the beginning of the child list
344 // and set their parent pointer
345 const nsFrameList::Slice& newFrames =
346 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
347 // If our prev in flow was under the first continuation of a first-line
348 // frame then we need to reparent the style contexts to remove the
349 // the special first-line styling. In the lazilySetParentPointer case
350 // we reparent the style contexts when we set their parents in
351 // nsInlineFrame::ReflowFrames and nsInlineFrame::ReflowInlineFrame.
352 if (aReflowState.mLineLayout->GetInFirstLine()) {
353 ReparentChildListStyle(aPresContext, newFrames, this);
354 }
355 }
356 }
357 }
359 // It's also possible that we have an overflow list for ourselves
360 #ifdef DEBUG
361 if (GetStateBits() & NS_FRAME_FIRST_REFLOW) {
362 // If it's our initial reflow, then we should not have an overflow list.
363 // However, add an assertion in case we get reflowed more than once with
364 // the initial reflow reason
365 nsFrameList* overflowFrames = GetOverflowFrames();
366 NS_ASSERTION(!overflowFrames || overflowFrames->IsEmpty(),
367 "overflow list is not empty for initial reflow");
368 }
369 #endif
370 if (!(GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
371 DrainFlags flags =
372 lazilySetParentPointer ? eDontReparentFrames : DrainFlags(0);
373 if (aReflowState.mLineLayout->GetInFirstLine()) {
374 flags = DrainFlags(flags | eInFirstLine);
375 }
376 DrainSelfOverflowListInternal(flags, lineContainer);
377 }
379 // Set our own reflow state (additional state above and beyond
380 // aReflowState)
381 InlineReflowState irs;
382 irs.mPrevFrame = nullptr;
383 irs.mLineContainer = lineContainer;
384 irs.mLineLayout = aReflowState.mLineLayout;
385 irs.mNextInFlow = (nsInlineFrame*) GetNextInFlow();
386 irs.mSetParentPointer = lazilySetParentPointer;
388 nsresult rv;
389 if (mFrames.IsEmpty()) {
390 // Try to pull over one frame before starting so that we know
391 // whether we have an anonymous block or not.
392 bool complete;
393 (void) PullOneFrame(aPresContext, irs, &complete);
394 }
396 rv = ReflowFrames(aPresContext, aReflowState, irs, aMetrics, aStatus);
398 ReflowAbsoluteFrames(aPresContext, aMetrics, aReflowState, aStatus);
400 // Note: the line layout code will properly compute our
401 // overflow-rect state for us.
403 NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aMetrics);
404 return rv;
405 }
407 bool
408 nsInlineFrame::DrainSelfOverflowListInternal(DrainFlags aFlags,
409 nsIFrame* aLineContainer)
410 {
411 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
412 if (overflowFrames) {
413 NS_ASSERTION(mFrames.NotEmpty(), "overflow list w/o frames");
414 // The frames on our own overflowlist may have been pushed by a
415 // previous lazilySetParentPointer Reflow so we need to ensure the
416 // correct parent pointer. This is sometimes skipped by Reflow.
417 if (!(aFlags & eDontReparentFrames)) {
418 nsIFrame* firstChild = overflowFrames->FirstChild();
419 if (aLineContainer && aLineContainer->GetPrevContinuation()) {
420 ReparentFloatsForInlineChild(aLineContainer, firstChild, true);
421 }
422 const bool inFirstLine = (aFlags & eInFirstLine);
423 RestyleManager* restyleManager = PresContext()->RestyleManager();
424 for (nsIFrame* f = firstChild; f; f = f->GetNextSibling()) {
425 f->SetParent(this);
426 if (inFirstLine) {
427 restyleManager->ReparentStyleContext(f);
428 nsLayoutUtils::MarkDescendantsDirty(f);
429 }
430 }
431 }
432 bool result = !overflowFrames->IsEmpty();
433 mFrames.AppendFrames(nullptr, *overflowFrames);
434 return result;
435 }
436 return false;
437 }
439 /* virtual */ bool
440 nsInlineFrame::DrainSelfOverflowList()
441 {
442 nsIFrame* lineContainer = nsLayoutUtils::FindNearestBlockAncestor(this);
443 // Add the eInFirstLine flag if we have a ::first-line ancestor frame.
444 // No need to look further than the nearest line container though.
445 DrainFlags flags = DrainFlags(0);
446 for (nsIFrame* p = GetParent(); p != lineContainer; p = p->GetParent()) {
447 if (p->GetType() == nsGkAtoms::lineFrame) {
448 flags = DrainFlags(flags | eInFirstLine);
449 break;
450 }
451 }
452 return DrainSelfOverflowListInternal(flags, lineContainer);
453 }
455 /* virtual */ bool
456 nsInlineFrame::CanContinueTextRun() const
457 {
458 // We can continue a text run through an inline frame
459 return true;
460 }
462 /* virtual */ void
463 nsInlineFrame::PullOverflowsFromPrevInFlow()
464 {
465 nsInlineFrame* prevInFlow = static_cast<nsInlineFrame*>(GetPrevInFlow());
466 if (prevInFlow) {
467 nsPresContext* presContext = PresContext();
468 AutoFrameListPtr prevOverflowFrames(presContext,
469 prevInFlow->StealOverflowFrames());
470 if (prevOverflowFrames) {
471 // Assume that our prev-in-flow has the same line container that we do.
472 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
473 this);
474 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
475 }
476 }
477 }
479 nsresult
480 nsInlineFrame::ReflowFrames(nsPresContext* aPresContext,
481 const nsHTMLReflowState& aReflowState,
482 InlineReflowState& irs,
483 nsHTMLReflowMetrics& aMetrics,
484 nsReflowStatus& aStatus)
485 {
486 nsresult rv = NS_OK;
487 aStatus = NS_FRAME_COMPLETE;
489 nsLineLayout* lineLayout = aReflowState.mLineLayout;
490 bool inFirstLine = aReflowState.mLineLayout->GetInFirstLine();
491 RestyleManager* restyleManager = aPresContext->RestyleManager();
492 WritingMode wm = aReflowState.GetWritingMode();
493 nscoord startEdge = 0;
494 // Don't offset by our start borderpadding if we have a prev continuation or
495 // if we're in a part of an {ib} split other than the first one.
496 if (!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) {
497 startEdge = aReflowState.ComputedLogicalBorderPadding().IStart(wm);
498 }
499 nscoord availableISize = aReflowState.AvailableISize();
500 NS_ASSERTION(availableISize != NS_UNCONSTRAINEDSIZE,
501 "should no longer use available widths");
502 // Subtract off inline axis border+padding from availableISize
503 availableISize -= startEdge;
504 availableISize -= aReflowState.ComputedLogicalBorderPadding().IEnd(wm);
505 lineLayout->BeginSpan(this, &aReflowState, startEdge,
506 startEdge + availableISize, &mBaseline);
508 // First reflow our principal children.
509 nsIFrame* frame = mFrames.FirstChild();
510 bool done = false;
511 while (frame) {
512 // Check if we should lazily set the child frame's parent pointer.
513 if (irs.mSetParentPointer) {
514 bool havePrevBlock =
515 irs.mLineContainer && irs.mLineContainer->GetPrevContinuation();
516 nsIFrame* child = frame;
517 do {
518 // If our block is the first in flow, then any floats under the pulled
519 // frame must already belong to our block.
520 if (havePrevBlock) {
521 // This has to happen before we update frame's parent; we need to
522 // know frame's ancestry under its old block.
523 // The blockChildren.ContainsFrame check performed by
524 // ReparentFloatsForInlineChild here may be slow, but we can't
525 // easily avoid it because we don't know where 'frame' originally
526 // came from. If we really really have to optimize this we could
527 // cache whether frame->GetParent() is under its containing blocks
528 // overflowList or not.
529 ReparentFloatsForInlineChild(irs.mLineContainer, child, false);
530 }
531 child->SetParent(this);
532 if (inFirstLine) {
533 restyleManager->ReparentStyleContext(child);
534 nsLayoutUtils::MarkDescendantsDirty(child);
535 }
536 // We also need to do the same for |frame|'s next-in-flows that are in
537 // the sibling list. Otherwise, if we reflow |frame| and it's complete
538 // we'll crash when trying to delete its next-in-flow.
539 // This scenario doesn't happen often, but it can happen.
540 nsIFrame* nextSibling = child->GetNextSibling();
541 child = child->GetNextInFlow();
542 if (MOZ_UNLIKELY(child)) {
543 while (child != nextSibling && nextSibling) {
544 nextSibling = nextSibling->GetNextSibling();
545 }
546 if (!nextSibling) {
547 child = nullptr;
548 }
549 }
550 MOZ_ASSERT(!child || mFrames.ContainsFrame(child));
551 } while (child);
553 // Fix the parent pointer for ::first-letter child frame next-in-flows,
554 // so nsFirstLetterFrame::Reflow can destroy them safely (bug 401042).
555 nsIFrame* realFrame = nsPlaceholderFrame::GetRealFrameFor(frame);
556 if (realFrame->GetType() == nsGkAtoms::letterFrame) {
557 nsIFrame* child = realFrame->GetFirstPrincipalChild();
558 if (child) {
559 NS_ASSERTION(child->GetType() == nsGkAtoms::textFrame,
560 "unexpected frame type");
561 nsIFrame* nextInFlow = child->GetNextInFlow();
562 for ( ; nextInFlow; nextInFlow = nextInFlow->GetNextInFlow()) {
563 NS_ASSERTION(nextInFlow->GetType() == nsGkAtoms::textFrame,
564 "unexpected frame type");
565 if (mFrames.ContainsFrame(nextInFlow)) {
566 nextInFlow->SetParent(this);
567 if (inFirstLine) {
568 restyleManager->ReparentStyleContext(nextInFlow);
569 nsLayoutUtils::MarkDescendantsDirty(nextInFlow);
570 }
571 }
572 else {
573 #ifdef DEBUG
574 // Once we find a next-in-flow that isn't ours none of the
575 // remaining next-in-flows should be either.
576 for ( ; nextInFlow; nextInFlow = nextInFlow->GetNextInFlow()) {
577 NS_ASSERTION(!mFrames.ContainsFrame(nextInFlow),
578 "unexpected letter frame flow");
579 }
580 #endif
581 break;
582 }
583 }
584 }
585 }
586 }
587 MOZ_ASSERT(frame->GetParent() == this);
589 if (!done) {
590 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
591 rv = ReflowInlineFrame(aPresContext, aReflowState, irs, frame, aStatus);
592 done = NS_FAILED(rv) ||
593 NS_INLINE_IS_BREAK(aStatus) ||
594 (!reflowingFirstLetter && NS_FRAME_IS_NOT_COMPLETE(aStatus));
595 if (done) {
596 if (!irs.mSetParentPointer) {
597 break;
598 }
599 // Keep reparenting the remaining siblings, but don't reflow them.
600 nsFrameList* pushedFrames = GetOverflowFrames();
601 if (pushedFrames && pushedFrames->FirstChild() == frame) {
602 // Don't bother if |frame| was pushed to our overflow list.
603 break;
604 }
605 } else {
606 irs.mPrevFrame = frame;
607 }
608 }
609 frame = frame->GetNextSibling();
610 }
612 // Attempt to pull frames from our next-in-flow until we can't
613 if (!done && GetNextInFlow()) {
614 while (true) {
615 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
616 bool isComplete;
617 if (!frame) { // Could be non-null if we pulled a first-letter frame and
618 // it created a continuation, since we don't push those.
619 frame = PullOneFrame(aPresContext, irs, &isComplete);
620 }
621 #ifdef NOISY_PUSHING
622 printf("%p pulled up %p\n", this, frame);
623 #endif
624 if (nullptr == frame) {
625 if (!isComplete) {
626 aStatus = NS_FRAME_NOT_COMPLETE;
627 }
628 break;
629 }
630 rv = ReflowInlineFrame(aPresContext, aReflowState, irs, frame, aStatus);
631 if (NS_FAILED(rv) ||
632 NS_INLINE_IS_BREAK(aStatus) ||
633 (!reflowingFirstLetter && NS_FRAME_IS_NOT_COMPLETE(aStatus))) {
634 break;
635 }
636 irs.mPrevFrame = frame;
637 frame = frame->GetNextSibling();
638 }
639 }
641 NS_ASSERTION(!NS_FRAME_IS_COMPLETE(aStatus) || !GetOverflowFrames(),
642 "We can't be complete AND have overflow frames!");
644 // If after reflowing our children they take up no area then make
645 // sure that we don't either.
646 //
647 // Note: CSS demands that empty inline elements still affect the
648 // line-height calculations. However, continuations of an inline
649 // that are empty we force to empty so that things like collapsed
650 // whitespace in an inline element don't affect the line-height.
651 aMetrics.ISize() = lineLayout->EndSpan(this);
653 // Compute final width.
655 // Make sure to not include our start border and padding if we have a prev
656 // continuation or if we're in a part of an {ib} split other than the first
657 // one.
658 if (!GetPrevContinuation() && !FrameIsNonFirstInIBSplit()) {
659 aMetrics.ISize() += aReflowState.ComputedLogicalBorderPadding().IStart(wm);
660 }
662 /*
663 * We want to only apply the end border and padding if we're the last
664 * continuation and either not in an {ib} split or the last part of it. To
665 * be the last continuation we have to be complete (so that we won't get a
666 * next-in-flow) and have no non-fluid continuations on our continuation
667 * chain.
668 */
669 if (NS_FRAME_IS_COMPLETE(aStatus) &&
670 !LastInFlow()->GetNextContinuation() &&
671 !FrameIsNonLastInIBSplit()) {
672 aMetrics.Width() += aReflowState.ComputedLogicalBorderPadding().IEnd(wm);
673 }
675 nsRefPtr<nsFontMetrics> fm;
676 float inflation = nsLayoutUtils::FontSizeInflationFor(this);
677 nsLayoutUtils::GetFontMetricsForFrame(this, getter_AddRefs(fm), inflation);
678 aReflowState.rendContext->SetFont(fm);
680 if (fm) {
681 // Compute final height of the frame.
682 //
683 // Do things the standard css2 way -- though it's hard to find it
684 // in the css2 spec! It's actually found in the css1 spec section
685 // 4.4 (you will have to read between the lines to really see
686 // it).
687 //
688 // The height of our box is the sum of our font size plus the top
689 // and bottom border and padding. The height of children do not
690 // affect our height.
691 aMetrics.SetTopAscent(fm->MaxAscent());
692 aMetrics.Height() = fm->MaxHeight();
693 } else {
694 NS_WARNING("Cannot get font metrics - defaulting sizes to 0");
695 aMetrics.SetTopAscent(aMetrics.Height() = 0);
696 }
697 aMetrics.SetTopAscent(aMetrics.TopAscent() + aReflowState.ComputedPhysicalBorderPadding().top);
698 aMetrics.Height() += aReflowState.ComputedPhysicalBorderPadding().top +
699 aReflowState.ComputedPhysicalBorderPadding().bottom;
701 // For now our overflow area is zero. The real value will be
702 // computed in |nsLineLayout::RelativePositionFrames|.
703 aMetrics.mOverflowAreas.Clear();
705 #ifdef NOISY_FINAL_SIZE
706 ListTag(stdout);
707 printf(": metrics=%d,%d ascent=%d\n",
708 aMetrics.Width(), aMetrics.Height(), aMetrics.TopAscent());
709 #endif
711 return rv;
712 }
714 nsresult
715 nsInlineFrame::ReflowInlineFrame(nsPresContext* aPresContext,
716 const nsHTMLReflowState& aReflowState,
717 InlineReflowState& irs,
718 nsIFrame* aFrame,
719 nsReflowStatus& aStatus)
720 {
721 nsLineLayout* lineLayout = aReflowState.mLineLayout;
722 bool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
723 bool pushedFrame;
724 nsresult rv =
725 lineLayout->ReflowFrame(aFrame, aStatus, nullptr, pushedFrame);
727 if (NS_FAILED(rv)) {
728 return rv;
729 }
731 if (NS_INLINE_IS_BREAK_BEFORE(aStatus)) {
732 if (aFrame != mFrames.FirstChild()) {
733 // Change break-before status into break-after since we have
734 // already placed at least one child frame. This preserves the
735 // break-type so that it can be propagated upward.
736 aStatus = NS_FRAME_NOT_COMPLETE |
737 NS_INLINE_BREAK | NS_INLINE_BREAK_AFTER |
738 (aStatus & NS_INLINE_BREAK_TYPE_MASK);
739 PushFrames(aPresContext, aFrame, irs.mPrevFrame, irs);
740 }
741 else {
742 // Preserve reflow status when breaking-before our first child
743 // and propagate it upward without modification.
744 }
745 return NS_OK;
746 }
748 // Create a next-in-flow if needed.
749 if (!NS_FRAME_IS_FULLY_COMPLETE(aStatus)) {
750 nsIFrame* newFrame;
751 rv = CreateNextInFlow(aFrame, newFrame);
752 if (NS_FAILED(rv)) {
753 return rv;
754 }
755 }
757 if (NS_INLINE_IS_BREAK_AFTER(aStatus)) {
758 nsIFrame* nextFrame = aFrame->GetNextSibling();
759 if (nextFrame) {
760 NS_FRAME_SET_INCOMPLETE(aStatus);
761 PushFrames(aPresContext, nextFrame, aFrame, irs);
762 }
763 else {
764 // We must return an incomplete status if there are more child
765 // frames remaining in a next-in-flow that follows this frame.
766 nsInlineFrame* nextInFlow = static_cast<nsInlineFrame*>(GetNextInFlow());
767 while (nextInFlow) {
768 if (nextInFlow->mFrames.NotEmpty()) {
769 NS_FRAME_SET_INCOMPLETE(aStatus);
770 break;
771 }
772 nextInFlow = static_cast<nsInlineFrame*>(nextInFlow->GetNextInFlow());
773 }
774 }
775 return NS_OK;
776 }
778 if (!NS_FRAME_IS_FULLY_COMPLETE(aStatus) && !reflowingFirstLetter) {
779 nsIFrame* nextFrame = aFrame->GetNextSibling();
780 if (nextFrame) {
781 PushFrames(aPresContext, nextFrame, aFrame, irs);
782 }
783 }
784 return NS_OK;
785 }
787 nsIFrame*
788 nsInlineFrame::PullOneFrame(nsPresContext* aPresContext,
789 InlineReflowState& irs,
790 bool* aIsComplete)
791 {
792 bool isComplete = true;
794 nsIFrame* frame = nullptr;
795 nsInlineFrame* nextInFlow = irs.mNextInFlow;
796 while (nextInFlow) {
797 frame = nextInFlow->mFrames.FirstChild();
798 if (!frame) {
799 // The nextInFlow's principal list has no frames, try its overflow list.
800 nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
801 if (overflowFrames) {
802 frame = overflowFrames->RemoveFirstChild();
803 if (overflowFrames->IsEmpty()) {
804 // We're stealing the only frame - delete the overflow list.
805 nextInFlow->DestroyOverflowList();
806 } else {
807 // We leave the remaining frames on the overflow list (rather than
808 // putting them on nextInFlow's principal list) so we don't have to
809 // set up the parent for them.
810 }
811 // ReparentFloatsForInlineChild needs it to be on a child list -
812 // we remove it again below.
813 nextInFlow->mFrames.SetFrames(frame);
814 }
815 }
817 if (frame) {
818 // If our block has no next continuation, then any floats belonging to
819 // the pulled frame must belong to our block already. This check ensures
820 // we do no extra work in the common non-vertical-breaking case.
821 if (irs.mLineContainer && irs.mLineContainer->GetNextContinuation()) {
822 // The blockChildren.ContainsFrame check performed by
823 // ReparentFloatsForInlineChild will be fast because frame's ancestor
824 // will be the first child of its containing block.
825 ReparentFloatsForInlineChild(irs.mLineContainer, frame, false);
826 }
827 nextInFlow->mFrames.RemoveFirstChild();
828 // nsFirstLineFrame::PullOneFrame calls ReparentStyleContext.
830 mFrames.InsertFrame(this, irs.mPrevFrame, frame);
831 isComplete = false;
832 if (irs.mLineLayout) {
833 irs.mLineLayout->SetDirtyNextLine();
834 }
835 nsContainerFrame::ReparentFrameView(frame, nextInFlow, this);
836 break;
837 }
838 nextInFlow = static_cast<nsInlineFrame*>(nextInFlow->GetNextInFlow());
839 irs.mNextInFlow = nextInFlow;
840 }
842 *aIsComplete = isComplete;
843 return frame;
844 }
846 void
847 nsInlineFrame::PushFrames(nsPresContext* aPresContext,
848 nsIFrame* aFromChild,
849 nsIFrame* aPrevSibling,
850 InlineReflowState& aState)
851 {
852 NS_PRECONDITION(aFromChild, "null pointer");
853 NS_PRECONDITION(aPrevSibling, "pushing first child");
854 NS_PRECONDITION(aPrevSibling->GetNextSibling() == aFromChild, "bad prev sibling");
856 #ifdef NOISY_PUSHING
857 printf("%p pushing aFromChild %p, disconnecting from prev sib %p\n",
858 this, aFromChild, aPrevSibling);
859 #endif
861 // Add the frames to our overflow list (let our next in flow drain
862 // our overflow list when it is ready)
863 SetOverflowFrames(mFrames.RemoveFramesAfter(aPrevSibling));
864 if (aState.mLineLayout) {
865 aState.mLineLayout->SetDirtyNextLine();
866 }
867 }
870 //////////////////////////////////////////////////////////////////////
872 int
873 nsInlineFrame::GetLogicalSkipSides(const nsHTMLReflowState* aReflowState) const
874 {
875 int skip = 0;
876 if (!IsFirst()) {
877 nsInlineFrame* prev = (nsInlineFrame*) GetPrevContinuation();
878 if ((GetStateBits() & NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
879 (prev && (prev->mRect.height || prev->mRect.width))) {
880 // Prev continuation is not empty therefore we don't render our start
881 // border edge.
882 skip |= LOGICAL_SIDE_I_START;
883 }
884 else {
885 // If the prev continuation is empty, then go ahead and let our start
886 // edge border render.
887 }
888 }
889 if (!IsLast()) {
890 nsInlineFrame* next = (nsInlineFrame*) GetNextContinuation();
891 if ((GetStateBits() & NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
892 (next && (next->mRect.height || next->mRect.width))) {
893 // Next continuation is not empty therefore we don't render our end
894 // border edge.
895 skip |= LOGICAL_SIDE_I_END;
896 }
897 else {
898 // If the next continuation is empty, then go ahead and let our end
899 // edge border render.
900 }
901 }
903 if (GetStateBits() & NS_FRAME_PART_OF_IBSPLIT) {
904 // All but the last part of an {ib} split should skip the "end" side (as
905 // determined by this frame's direction) and all but the first part of such
906 // a split should skip the "start" side. But figuring out which part of
907 // the split we are involves getting our first continuation, which might be
908 // expensive. So don't bother if we already have the relevant bits set.
909 if (skip != LOGICAL_SIDES_I_BOTH) {
910 // We're missing one of the skip bits, so check whether we need to set it.
911 // Only get the first continuation once, as an optimization.
912 nsIFrame* firstContinuation = FirstContinuation();
913 if (firstContinuation->FrameIsNonLastInIBSplit()) {
914 skip |= LOGICAL_SIDE_I_END;
915 }
916 if (firstContinuation->FrameIsNonFirstInIBSplit()) {
917 skip |= LOGICAL_SIDE_I_START;
918 }
919 }
920 }
922 return skip;
923 }
925 nscoord
926 nsInlineFrame::GetBaseline() const
927 {
928 return mBaseline;
929 }
931 #ifdef ACCESSIBILITY
932 a11y::AccType
933 nsInlineFrame::AccessibleType()
934 {
935 // Broken image accessibles are created here, because layout
936 // replaces the image or image control frame with an inline frame
937 nsIAtom *tagAtom = mContent->Tag();
938 if (tagAtom == nsGkAtoms::input) // Broken <input type=image ... />
939 return a11y::eHTMLButtonType;
940 if (tagAtom == nsGkAtoms::img) // Create accessible for broken <img>
941 return a11y::eHyperTextType;
943 return a11y::eNoType;
944 }
945 #endif
947 //////////////////////////////////////////////////////////////////////
949 // nsLineFrame implementation
951 nsIFrame*
952 NS_NewFirstLineFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
953 {
954 return new (aPresShell) nsFirstLineFrame(aContext);
955 }
957 NS_IMPL_FRAMEARENA_HELPERS(nsFirstLineFrame)
959 void
960 nsFirstLineFrame::Init(nsIContent* aContent, nsIFrame* aParent,
961 nsIFrame* aPrevInFlow)
962 {
963 nsInlineFrame::Init(aContent, aParent, aPrevInFlow);
964 if (!aPrevInFlow) {
965 MOZ_ASSERT(StyleContext()->GetPseudo() == nsCSSPseudoElements::firstLine);
966 return;
967 }
969 // This frame is a continuation - fixup the style context if aPrevInFlow
970 // is the first-in-flow (the only one with a ::first-line pseudo).
971 if (aPrevInFlow->StyleContext()->GetPseudo() == nsCSSPseudoElements::firstLine) {
972 MOZ_ASSERT(FirstInFlow() == aPrevInFlow);
973 // Create a new style context that is a child of the parent
974 // style context thus removing the ::first-line style. This way
975 // we behave as if an anonymous (unstyled) span was the child
976 // of the parent frame.
977 nsStyleContext* parentContext = aParent->StyleContext();
978 nsRefPtr<nsStyleContext> newSC = PresContext()->StyleSet()->
979 ResolveAnonymousBoxStyle(nsCSSAnonBoxes::mozLineFrame, parentContext);
980 SetStyleContext(newSC);
981 } else {
982 MOZ_ASSERT(FirstInFlow() != aPrevInFlow);
983 MOZ_ASSERT(aPrevInFlow->StyleContext()->GetPseudo() ==
984 nsCSSAnonBoxes::mozLineFrame);
985 }
986 }
988 #ifdef DEBUG_FRAME_DUMP
989 nsresult
990 nsFirstLineFrame::GetFrameName(nsAString& aResult) const
991 {
992 return MakeFrameName(NS_LITERAL_STRING("Line"), aResult);
993 }
994 #endif
996 nsIAtom*
997 nsFirstLineFrame::GetType() const
998 {
999 return nsGkAtoms::lineFrame;
1000 }
1002 nsIFrame*
1003 nsFirstLineFrame::PullOneFrame(nsPresContext* aPresContext, InlineReflowState& irs,
1004 bool* aIsComplete)
1005 {
1006 nsIFrame* frame = nsInlineFrame::PullOneFrame(aPresContext, irs, aIsComplete);
1007 if (frame && !GetPrevInFlow()) {
1008 // We are a first-line frame. Fixup the child frames
1009 // style-context that we just pulled.
1010 NS_ASSERTION(frame->GetParent() == this, "Incorrect parent?");
1011 aPresContext->RestyleManager()->ReparentStyleContext(frame);
1012 nsLayoutUtils::MarkDescendantsDirty(frame);
1013 }
1014 return frame;
1015 }
1017 nsresult
1018 nsFirstLineFrame::Reflow(nsPresContext* aPresContext,
1019 nsHTMLReflowMetrics& aMetrics,
1020 const nsHTMLReflowState& aReflowState,
1021 nsReflowStatus& aStatus)
1022 {
1023 if (nullptr == aReflowState.mLineLayout) {
1024 return NS_ERROR_INVALID_ARG;
1025 }
1027 nsIFrame* lineContainer = aReflowState.mLineLayout->LineContainerFrame();
1029 // Check for an overflow list with our prev-in-flow
1030 nsFirstLineFrame* prevInFlow = (nsFirstLineFrame*)GetPrevInFlow();
1031 if (prevInFlow) {
1032 AutoFrameListPtr prevOverflowFrames(aPresContext,
1033 prevInFlow->StealOverflowFrames());
1034 if (prevOverflowFrames) {
1035 // Assign all floats to our block if necessary
1036 if (lineContainer && lineContainer->GetPrevContinuation()) {
1037 ReparentFloatsForInlineChild(lineContainer,
1038 prevOverflowFrames->FirstChild(),
1039 true);
1040 }
1041 const nsFrameList::Slice& newFrames =
1042 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
1043 ReparentChildListStyle(aPresContext, newFrames, this);
1044 }
1045 }
1047 // It's also possible that we have an overflow list for ourselves.
1048 DrainSelfOverflowList();
1050 // Set our own reflow state (additional state above and beyond
1051 // aReflowState)
1052 InlineReflowState irs;
1053 irs.mPrevFrame = nullptr;
1054 irs.mLineContainer = lineContainer;
1055 irs.mLineLayout = aReflowState.mLineLayout;
1056 irs.mNextInFlow = (nsInlineFrame*) GetNextInFlow();
1058 nsresult rv;
1059 bool wasEmpty = mFrames.IsEmpty();
1060 if (wasEmpty) {
1061 // Try to pull over one frame before starting so that we know
1062 // whether we have an anonymous block or not.
1063 bool complete;
1064 PullOneFrame(aPresContext, irs, &complete);
1065 }
1067 if (nullptr == GetPrevInFlow()) {
1068 // XXX This is pretty sick, but what we do here is to pull-up, in
1069 // advance, all of the next-in-flows children. We re-resolve their
1070 // style while we are at at it so that when we reflow they have
1071 // the right style.
1072 //
1073 // All of this is so that text-runs reflow properly.
1074 irs.mPrevFrame = mFrames.LastChild();
1075 for (;;) {
1076 bool complete;
1077 nsIFrame* frame = PullOneFrame(aPresContext, irs, &complete);
1078 if (!frame) {
1079 break;
1080 }
1081 irs.mPrevFrame = frame;
1082 }
1083 irs.mPrevFrame = nullptr;
1084 }
1086 NS_ASSERTION(!aReflowState.mLineLayout->GetInFirstLine(),
1087 "Nested first-line frames? BOGUS");
1088 aReflowState.mLineLayout->SetInFirstLine(true);
1089 rv = ReflowFrames(aPresContext, aReflowState, irs, aMetrics, aStatus);
1090 aReflowState.mLineLayout->SetInFirstLine(false);
1092 ReflowAbsoluteFrames(aPresContext, aMetrics, aReflowState, aStatus);
1094 // Note: the line layout code will properly compute our overflow state for us
1096 return rv;
1097 }
1099 /* virtual */ void
1100 nsFirstLineFrame::PullOverflowsFromPrevInFlow()
1101 {
1102 nsFirstLineFrame* prevInFlow = static_cast<nsFirstLineFrame*>(GetPrevInFlow());
1103 if (prevInFlow) {
1104 nsPresContext* presContext = PresContext();
1105 AutoFrameListPtr prevOverflowFrames(presContext,
1106 prevInFlow->StealOverflowFrames());
1107 if (prevOverflowFrames) {
1108 // Assume that our prev-in-flow has the same line container that we do.
1109 const nsFrameList::Slice& newFrames =
1110 mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
1111 ReparentChildListStyle(presContext, newFrames, this);
1112 }
1113 }
1114 }
1116 /* virtual */ bool
1117 nsFirstLineFrame::DrainSelfOverflowList()
1118 {
1119 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
1120 if (overflowFrames) {
1121 NS_ASSERTION(mFrames.NotEmpty(), "overflow list w/o frames");
1123 bool result = !overflowFrames->IsEmpty();
1124 const nsFrameList::Slice& newFrames =
1125 mFrames.AppendFrames(nullptr, *overflowFrames);
1126 ReparentChildListStyle(PresContext(), newFrames, this);
1127 return result;
1128 }
1129 return false;
1130 }