Bug 1909121 - [wpt-sync] Update web-platform-tests to 5af3e9c2a2aba76ade00f0dbc3486e5...
[gecko.git] / layout / generic / nsBlockFrame.h
blob04de4f74f4cf7f591a2cdb606d332504d12b5038
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 /*
8 * rendering object for CSS display:block, inline-block, and list-item
9 * boxes, also used for various anonymous boxes
12 #ifndef nsBlockFrame_h___
13 #define nsBlockFrame_h___
15 #include "nsContainerFrame.h"
16 #include "nsHTMLParts.h"
17 #include "nsLineBox.h"
18 #include "nsCSSPseudoElements.h"
19 #include "nsFloatManager.h"
21 enum class LineReflowStatus {
22 // The line was completely reflowed and fit in available width, and we should
23 // try to pull up content from the next line if possible.
24 OK,
25 // The line was completely reflowed and fit in available width, but we should
26 // not try to pull up content from the next line.
27 Stop,
28 // We need to reflow the line again at its current vertical position. The
29 // new reflow should not try to pull up any frames from the next line.
30 RedoNoPull,
31 // We need to reflow the line again using the floats from its height
32 // this reflow, since its height made it hit floats that were not
33 // adjacent to its top.
34 RedoMoreFloats,
35 // We need to reflow the line again at a lower vertical postion where there
36 // may be more horizontal space due to different float configuration.
37 RedoNextBand,
38 // The line did not fit in the available vertical space. Try pushing it to
39 // the next page or column if it's not the first line on the current
40 // page/column.
41 Truncated
44 class nsBlockInFlowLineIterator;
45 namespace mozilla {
46 class BlockReflowState;
47 class PresShell;
48 class ServoRestyleState;
49 class ServoStyleSet;
50 } // namespace mozilla
52 /**
53 * Some invariants:
54 * -- The overflow out-of-flows list contains the out-of-
55 * flow frames whose placeholders are in the overflow list.
56 * -- A given piece of content has at most one placeholder
57 * frame in a block's normal child list.
58 * -- While a block is being reflowed, and from then until
59 * its next-in-flow is reflowed it may have a
60 * PushedFloatProperty frame property that points to
61 * an nsFrameList. This list contains continuations for
62 * floats whose prev-in-flow is in the block's regular float
63 * list and first-in-flows of floats that did not fit, but
64 * whose placeholders are in the block or one of its
65 * prev-in-flows.
66 * -- In all these frame lists, if there are two frames for
67 * the same content appearing in the list, then the frames
68 * appear with the prev-in-flow before the next-in-flow.
69 * -- While reflowing a block, its overflow line list
70 * will usually be empty but in some cases will have lines
71 * (while we reflow the block at its shrink-wrap width).
72 * In this case any new overflowing content must be
73 * prepended to the overflow lines.
77 * Base class for block and inline frames.
78 * The block frame has an additional child list, FrameChildListID::Absolute,
79 * which contains the absolutely positioned frames.
81 class nsBlockFrame : public nsContainerFrame {
82 using BlockReflowState = mozilla::BlockReflowState;
84 public:
85 NS_DECL_FRAMEARENA_HELPERS(nsBlockFrame)
87 typedef nsLineList::iterator LineIterator;
88 typedef nsLineList::const_iterator ConstLineIterator;
89 typedef nsLineList::reverse_iterator ReverseLineIterator;
90 typedef nsLineList::const_reverse_iterator ConstReverseLineIterator;
92 LineIterator LinesBegin() { return mLines.begin(); }
93 LineIterator LinesEnd() { return mLines.end(); }
94 ConstLineIterator LinesBegin() const { return mLines.begin(); }
95 ConstLineIterator LinesEnd() const { return mLines.end(); }
96 ReverseLineIterator LinesRBegin() { return mLines.rbegin(); }
97 ReverseLineIterator LinesREnd() { return mLines.rend(); }
98 ConstReverseLineIterator LinesRBegin() const { return mLines.rbegin(); }
99 ConstReverseLineIterator LinesREnd() const { return mLines.rend(); }
100 LineIterator LinesBeginFrom(nsLineBox* aList) { return mLines.begin(aList); }
101 ReverseLineIterator LinesRBeginFrom(nsLineBox* aList) {
102 return mLines.rbegin(aList);
105 // Methods declared to be used in 'range-based-for-loop'
106 nsLineList& Lines() { return mLines; }
107 const nsLineList& Lines() const { return mLines; }
109 friend nsBlockFrame* NS_NewBlockFrame(mozilla::PresShell* aPresShell,
110 ComputedStyle* aStyle);
112 // nsQueryFrame
113 NS_DECL_QUERYFRAME
115 // nsIFrame
116 void Init(nsIContent* aContent, nsContainerFrame* aParent,
117 nsIFrame* aPrevInFlow) override;
118 void SetInitialChildList(ChildListID aListID,
119 nsFrameList&& aChildList) override;
120 void AppendFrames(ChildListID aListID, nsFrameList&& aFrameList) override;
121 void InsertFrames(ChildListID aListID, nsIFrame* aPrevFrame,
122 const nsLineList::iterator* aPrevFrameLine,
123 nsFrameList&& aFrameList) override;
124 void RemoveFrame(DestroyContext&, ChildListID, nsIFrame* aOldFrame) override;
125 nsContainerFrame* GetContentInsertionFrame() override;
126 void AppendDirectlyOwnedAnonBoxes(nsTArray<OwnedAnonBox>& aResult) override;
127 const nsFrameList& GetChildList(ChildListID aListID) const override;
128 void GetChildLists(nsTArray<ChildList>* aLists) const override;
129 nscoord SynthesizeFallbackBaseline(
130 mozilla::WritingMode aWM,
131 BaselineSharingGroup aBaselineGroup) const override;
132 BaselineSharingGroup GetDefaultBaselineSharingGroup() const override {
133 return BaselineSharingGroup::Last;
135 Maybe<nscoord> GetNaturalBaselineBOffset(
136 mozilla::WritingMode aWM, BaselineSharingGroup aBaselineGroup,
137 BaselineExportContext aExportContext) const override;
138 nscoord GetCaretBaseline() const override;
139 void Destroy(DestroyContext&) override;
141 bool IsFloatContainingBlock() const override;
142 void BuildDisplayList(nsDisplayListBuilder* aBuilder,
143 const nsDisplayListSet& aLists) override;
145 void InvalidateFrame(uint32_t aDisplayItemKey = 0,
146 bool aRebuildDisplayItems = true) override;
147 void InvalidateFrameWithRect(const nsRect& aRect,
148 uint32_t aDisplayItemKey = 0,
149 bool aRebuildDisplayItems = true) override;
151 #ifdef DEBUG_FRAME_DUMP
152 void List(FILE* out = stderr, const char* aPrefix = "",
153 ListFlags aFlags = ListFlags()) const override;
154 nsresult GetFrameName(nsAString& aResult) const override;
155 #endif
157 #ifdef DEBUG
158 const char* LineReflowStatusToString(
159 LineReflowStatus aLineReflowStatus) const;
160 #endif
162 #ifdef ACCESSIBILITY
163 mozilla::a11y::AccType AccessibleType() override;
164 #endif
166 // Line cursor methods to speed up line searching in which one query result
167 // is expected to be close to the next in general. This is mainly for
168 // searching line(s) containing a point. It is also used as a cache for local
169 // computation. The basic idea for the former is that we set the cursor
170 // property if the lines' overflowArea.InkOverflow().ys and
171 // overflowArea.InkOverflow().yMosts are non-decreasing
172 // (considering only non-empty overflowArea.InkOverflow()s; empty
173 // overflowArea.InkOverflow()s never participate in event handling
174 // or painting), and the block has sufficient number of lines. The
175 // cursor property points to a "recently used" line. If we get a
176 // series of requests that work on lines
177 // "near" the cursor, then we can find those nearby lines quickly by
178 // starting our search at the cursor.
180 // We have two independent line cursors, one used for display-list building
181 // and the other for a11y or other frame queries. Either or both may be
182 // present at any given time. When we reflow or otherwise munge the lines,
183 // both cursors will be cleared.
184 // The display cursor is only created and used if the lines satisfy the non-
185 // decreasing y-coordinate condition (see SetupLineCursorForDisplay comment
186 // below), whereas the query cursor may be created for any block. The two
187 // are separated so creating a cursor for a11y queries (eg GetRenderedText)
188 // does not risk confusing the display-list building code.
190 // Clear out line cursors because we're disturbing the lines (i.e., Reflow)
191 void ClearLineCursors() {
192 if (MaybeHasLineCursor()) {
193 ClearLineCursorForDisplay();
194 ClearLineCursorForQuery();
195 RemoveStateBits(NS_BLOCK_HAS_LINE_CURSOR);
197 ClearLineIterator();
199 void ClearLineCursorForDisplay() {
200 RemoveProperty(LineCursorPropertyDisplay());
202 void ClearLineCursorForQuery() { RemoveProperty(LineCursorPropertyQuery()); }
204 // Clear just the line-iterator property; this is used if we need to get a
205 // LineIterator temporarily during reflow, when using a persisted iterator
206 // would be invalid. So we clear the stored property immediately after use.
207 void ClearLineIterator() { RemoveProperty(LineIteratorProperty()); }
209 // Get the first line that might contain y-coord 'y', or nullptr if you must
210 // search all lines. If nonnull is returned then we guarantee that the lines'
211 // combinedArea.ys and combinedArea.yMosts are non-decreasing.
212 // The actual line returned might not contain 'y', but if not, it is
213 // guaranteed to be before any line which does contain 'y'.
214 nsLineBox* GetFirstLineContaining(nscoord y);
216 // Ensure the frame has a display-list line cursor, initializing it to the
217 // first line if it is not already present. (If there's an existing cursor,
218 // it is left untouched.) Only call this if you guarantee that the lines'
219 // combinedArea.ys and combinedArea.yMosts are non-decreasing.
220 void SetupLineCursorForDisplay();
222 // Ensure the frame has a query line cursor, initializing it to the first
223 // line if it is not already present. (If there's an existing cursor, it is
224 // left untouched.)
225 void SetupLineCursorForQuery();
227 void ChildIsDirty(nsIFrame* aChild) override;
229 bool IsEmpty() override;
230 bool CachedIsEmpty() override;
231 bool IsSelfEmpty() override;
233 // Given that we have a ::marker frame, does it actually draw something, i.e.,
234 // do we have either a 'list-style-type' or 'list-style-image' that is
235 // not 'none', and no 'content'?
236 bool MarkerIsEmpty() const;
238 // Return true if this frame has a ::marker frame.
239 bool HasMarker() const { return HasOutsideMarker() || HasInsideMarker(); }
241 // Return true if this frame has an inside ::marker frame.
242 bool HasInsideMarker() const {
243 return HasAnyStateBits(NS_BLOCK_HAS_INSIDE_MARKER);
246 // Return true if this frame has an outside ::marker frame.
247 bool HasOutsideMarker() const {
248 return HasAnyStateBits(NS_BLOCK_HAS_OUTSIDE_MARKER);
252 * @return the first-letter frame or nullptr if we don't have one.
254 nsIFrame* GetFirstLetter() const;
257 * @return the ::first-line frame or nullptr if we don't have one.
259 nsIFrame* GetFirstLineFrame() const;
261 void MarkIntrinsicISizesDirty() override;
263 private:
264 // Whether CSS text-indent should be applied to the given line.
265 bool TextIndentAppliesTo(const LineIterator& aLine) const;
267 void CheckIntrinsicCacheAgainstShrinkWrapState();
269 template <typename LineIteratorType>
270 Maybe<nscoord> GetBaselineBOffset(LineIteratorType aStart,
271 LineIteratorType aEnd,
272 mozilla::WritingMode aWM,
273 BaselineSharingGroup aBaselineGroup,
274 BaselineExportContext aExportContext) const;
276 public:
277 nscoord GetMinISize(gfxContext* aRenderingContext) override;
278 nscoord GetPrefISize(gfxContext* aRenderingContext) override;
280 nsRect ComputeTightBounds(DrawTarget* aDrawTarget) const override;
282 nsresult GetPrefWidthTightBounds(gfxContext* aContext, nscoord* aX,
283 nscoord* aXMost) override;
286 * Compute the final block size of this frame.
288 * @param aState BlockReflowState passed from parent during reflow.
289 * Note: aState.mReflowStatus is mostly an "input" parameter. When this
290 * method is called, it should represent what our status would be as if
291 * we were shrinkwrapping our children's block-size. This method will
292 * then adjust it before returning if our status is different in light
293 * of our actual final block-size and current page/column's available
294 * block-size.
295 * @param aBEndEdgeOfChildren The distance between this frame's block-start
296 * border-edge and the block-end edge of our last child's border-box.
297 * This is effectively our block-start border-padding plus the
298 * block-size of our children, precomputed outside of this function.
299 * @return our final block-size with respect to aReflowInput's writing-mode.
301 nscoord ComputeFinalBSize(BlockReflowState& aState,
302 nscoord aBEndEdgeOfChildren);
304 void Reflow(nsPresContext* aPresContext, ReflowOutput& aDesiredSize,
305 const ReflowInput& aReflowInput,
306 nsReflowStatus& aStatus) override;
309 * Move any frames on our overflow list to the end of our principal list.
310 * @return true if there were any overflow frames
312 bool DrainSelfOverflowList() override;
314 void StealFrame(nsIFrame* aChild) override;
316 void DeleteNextInFlowChild(DestroyContext&, nsIFrame* aNextInFlow,
317 bool aDeletingEmptyFrames) override;
320 * This is a special method that allows a child class of nsBlockFrame to
321 * return a special, customized nsStyleText object to the nsLineLayout
322 * constructor. It is used when the nsBlockFrame child needs to specify its
323 * custom rendering style.
325 virtual const nsStyleText* StyleTextForLineLayout();
328 * Determines whether the collapsed margin carried out of the last
329 * line includes the margin-top of a line with clearance (in which
330 * case we must avoid collapsing that margin with our bottom margin)
332 bool CheckForCollapsedBEndMarginFromClearanceLine();
334 static nsresult GetCurrentLine(BlockReflowState* aState,
335 nsLineBox** aOutCurrentLine);
338 * Determine if this block is a margin root at the top/bottom edges.
340 void IsMarginRoot(bool* aBStartMarginRoot, bool* aBEndMarginRoot);
342 static bool BlockNeedsFloatManager(nsIFrame* aBlock);
345 * Returns whether aFrame is a block frame that will wrap its contents
346 * around floats intruding on it from the outside. (aFrame need not
347 * be a block frame, but if it's not, the result will be false.)
349 * Note: We often use the term "float-avoiding block" to refer to
350 * block-level frames for whom this function returns false.
352 static bool BlockCanIntersectFloats(nsIFrame* aFrame);
355 * Returns the inline size that needs to be cleared past floats for
356 * blocks that avoid (i.e. cannot intersect) floats. aState must already
357 * have GetFloatAvailableSpace called on it for the block-dir position that
358 * we care about (which need not be its current mBCoord)
360 struct FloatAvoidingISizeToClear {
361 // Note that we care about the inline-start margin but can ignore
362 // the inline-end margin.
363 nscoord marginIStart, borderBoxISize;
365 static FloatAvoidingISizeToClear ISizeToClearPastFloats(
366 const BlockReflowState& aState,
367 const mozilla::LogicalRect& aFloatAvailableSpace,
368 nsIFrame* aFloatAvoidingBlock);
371 * Creates a contination for aFloat and adds it to the list of overflow
372 * floats. Also updates aState.mReflowStatus to include the float's
373 * incompleteness. Must only be called while this block frame is in reflow.
374 * aFloatStatus must be the float's true, unmodified reflow status.
376 void SplitFloat(BlockReflowState& aState, nsIFrame* aFloat,
377 const nsReflowStatus& aFloatStatus);
380 * Walks up the frame tree, starting with aCandidate, and returns the first
381 * block frame that it encounters.
383 static nsBlockFrame* GetNearestAncestorBlock(nsIFrame* aCandidate);
385 struct FrameLines {
386 nsLineList mLines;
387 nsFrameList mFrames;
391 * Update the styles of our various pseudo-elements (marker, first-line,
392 * etc, but _not_ first-letter).
394 void UpdatePseudoElementStyles(mozilla::ServoRestyleState& aRestyleState);
396 // Update our first-letter styles during stylo post-traversal. This needs to
397 // be done at a slightly different time than our other pseudo-elements.
398 void UpdateFirstLetterStyle(mozilla::ServoRestyleState& aRestyleState);
400 protected:
401 explicit nsBlockFrame(ComputedStyle* aStyle, nsPresContext* aPresContext,
402 ClassID aID = kClassID)
403 : nsContainerFrame(aStyle, aPresContext, aID) {
404 #ifdef DEBUG
405 InitDebugFlags();
406 #endif
409 virtual ~nsBlockFrame();
411 void DidSetComputedStyle(ComputedStyle* aOldStyle) override;
413 #ifdef DEBUG
414 already_AddRefed<ComputedStyle> GetFirstLetterStyle(
415 nsPresContext* aPresContext);
416 #endif
418 NS_DECLARE_FRAME_PROPERTY_WITHOUT_DTOR(LineCursorPropertyDisplay, nsLineBox)
419 NS_DECLARE_FRAME_PROPERTY_WITHOUT_DTOR(LineCursorPropertyQuery, nsLineBox)
420 // Note that the NS_BLOCK_HAS_LINE_CURSOR flag does not necessarily mean the
421 // cursor is present, as it covers both the "display" and "query" cursors,
422 // but may remain set if they have been separately deleted. In such a case,
423 // the Get* accessors will be slightly more expensive, but will still safely
424 // return null if the cursor is absent.
425 bool MaybeHasLineCursor() {
426 return HasAnyStateBits(NS_BLOCK_HAS_LINE_CURSOR);
428 nsLineBox* GetLineCursorForDisplay() {
429 return MaybeHasLineCursor() ? GetProperty(LineCursorPropertyDisplay())
430 : nullptr;
432 nsLineBox* GetLineCursorForQuery() {
433 return MaybeHasLineCursor() ? GetProperty(LineCursorPropertyQuery())
434 : nullptr;
437 nsLineBox* NewLineBox(nsIFrame* aFrame, bool aIsBlock) {
438 return NS_NewLineBox(PresShell(), aFrame, aIsBlock);
440 nsLineBox* NewLineBox(nsLineBox* aFromLine, nsIFrame* aFrame,
441 int32_t aCount) {
442 return NS_NewLineBox(PresShell(), aFromLine, aFrame, aCount);
444 void FreeLineBox(nsLineBox* aLine) {
445 if (aLine == GetLineCursorForDisplay()) {
446 ClearLineCursorForDisplay();
448 if (aLine == GetLineCursorForQuery()) {
449 ClearLineCursorForQuery();
451 aLine->Destroy(PresShell());
454 * Helper method for StealFrame.
456 void RemoveFrameFromLine(nsIFrame* aChild, nsLineList::iterator aLine,
457 nsFrameList& aFrameList, nsLineList& aLineList);
459 void TryAllLines(nsLineList::iterator* aIterator,
460 nsLineList::iterator* aStartIterator,
461 nsLineList::iterator* aEndIterator, bool* aInOverflowLines,
462 FrameLines** aOverflowLines);
464 /** move the frames contained by aLine by aDeltaBCoord
465 * if aLine is a block, its child floats are added to the state manager
467 void SlideLine(BlockReflowState& aState, nsLineBox* aLine,
468 nscoord aDeltaBCoord);
470 void UpdateLineContainerSize(nsLineBox* aLine,
471 const nsSize& aNewContainerSize);
473 // helper for SlideLine and UpdateLineContainerSize
474 void MoveChildFramesOfLine(nsLineBox* aLine, nscoord aDeltaBCoord);
476 // Returns block-end edge of children.
477 nscoord ComputeFinalSize(const ReflowInput& aReflowInput,
478 BlockReflowState& aState, ReflowOutput& aMetrics);
481 * Calculates the necessary shift to honor 'align-content' and applies it.
483 void AlignContent(BlockReflowState& aState, ReflowOutput& aMetrics,
484 nscoord aBEndEdgeOfChildren);
485 // Stash the effective align-content shift value between reflows
486 NS_DECLARE_FRAME_PROPERTY_SMALL_VALUE(AlignContentShift, nscoord)
489 * Helper method for Reflow(). Computes the overflow areas created by our
490 * children, and includes them into aOverflowAreas.
492 void ComputeOverflowAreas(mozilla::OverflowAreas& aOverflowAreas,
493 nscoord aBEndEdgeOfChildren,
494 const nsStyleDisplay* aDisplay) const;
497 * Helper method for ComputeOverflowAreas(). Incorporates aBEndEdgeOfChildren
498 * into the aOverflowAreas.
500 void ConsiderBlockEndEdgeOfChildren(mozilla::OverflowAreas& aOverflowAreas,
501 nscoord aBEndEdgeOfChildren,
502 const nsStyleDisplay* aDisplay) const;
505 * Add the frames in aFrameList to this block after aPrevSibling.
506 * This block thinks in terms of lines, but the frame construction code
507 * knows nothing about lines at all so we need to find the line that
508 * contains aPrevSibling and add aFrameList after aPrevSibling on that line.
509 * New lines are created as necessary to handle block data in aFrameList.
510 * This function will clear aFrameList.
512 * aPrevSiblingLine, if present, must be the line containing aPrevSibling.
513 * Providing it will make this function faster.
515 void AddFrames(nsFrameList&& aFrameList, nsIFrame* aPrevSibling,
516 const nsLineList::iterator* aPrevSiblingLine);
518 // Return the :-moz-block-ruby-content child frame, if any.
519 // (It's non-null only if this block frame is for 'display:block ruby'.)
520 nsContainerFrame* GetRubyContentPseudoFrame();
523 * Perform Bidi resolution on this frame
525 nsresult ResolveBidi();
528 * Test whether the frame is a form control in a visual Bidi page.
529 * This is necessary for backwards-compatibility, because most visual
530 * pages use logical order for form controls so that they will
531 * display correctly on native widgets in OSs with Bidi support
532 * @param aPresContext the pres context
533 * @return whether the frame is a BIDI form control
535 bool IsVisualFormControl(nsPresContext* aPresContext);
537 /** Whether this block has an effective align-content property */
538 bool IsAligned() const {
539 return StylePosition()->mAlignContent.primary !=
540 mozilla::StyleAlignFlags::NORMAL;
543 nscoord GetAlignContentShift() const {
544 return IsAligned() ? GetProperty(AlignContentShift()) : 0;
548 * For text-wrap:balance, we iteratively try reflowing with adjusted inline
549 * size to find the "best" result (the tightest size that can be applied
550 * without increasing the total line count of the block).
551 * This record is used to manage the state of these "trial reflows", and
552 * return results from the final trial.
554 struct TrialReflowState {
555 // Values pre-computed at start of Reflow(), constant across trials.
556 const nscoord mConsumedBSize;
557 const nscoord mEffectiveContentBoxBSize;
558 bool mNeedFloatManager;
559 // [out] Whether reflowing resulted in use of an overflow-wrap break.
560 bool mUsedOverflowWrap = false;
561 // Settings for the current trial.
562 bool mBalancing = false;
563 nscoord mInset = 0;
564 // Results computed during the trial reflow. Values from the final trial
565 // will be used by the remainder of Reflow().
566 mozilla::OverflowAreas mOcBounds;
567 mozilla::OverflowAreas mFcBounds;
568 nscoord mBlockEndEdgeOfChildren = 0;
569 nscoord mContainerWidth = 0;
571 // Initialize for the initial trial reflow, with zero inset.
572 TrialReflowState(nscoord aConsumedBSize, nscoord aEffectiveContentBoxBSize,
573 bool aNeedFloatManager)
574 : mConsumedBSize(aConsumedBSize),
575 mEffectiveContentBoxBSize(aEffectiveContentBoxBSize),
576 mNeedFloatManager(aNeedFloatManager) {}
578 // Adjust the inset amount, and reset state for a new trial.
579 void ResetForBalance(nscoord aInsetDelta) {
580 // Tells the reflow-lines loop we must consider all lines "dirty" (as we
581 // are modifying the effective inline-size to be used).
582 mBalancing = true;
583 // Adjust inset to apply.
584 mInset += aInsetDelta;
585 // Re-initialize state that the reflow loop will compute.
586 mOcBounds.Clear();
587 mFcBounds.Clear();
588 mBlockEndEdgeOfChildren = 0;
589 mContainerWidth = 0;
590 mUsedOverflowWrap = false;
595 * Internal helper for Reflow(); may be called repeatedly during a single
596 * Reflow() in order to implement text-wrap:balance.
597 * This method applies aTrialState.mInset during line-breaking to reduce
598 * the effective available inline-size (without affecting alignment).
600 nsReflowStatus TrialReflow(nsPresContext* aPresContext,
601 ReflowOutput& aMetrics,
602 const ReflowInput& aReflowInput,
603 TrialReflowState& aTrialState);
605 public:
607 * Helper function for the frame ctor to register a ::marker frame.
609 void SetMarkerFrameForListItem(nsIFrame* aMarkerFrame);
612 * Does all the real work for removing aDeletedFrame
613 * -- finds the line containing aDeletedFrame
614 * -- removes all aDeletedFrame next-in-flows (or all continuations,
615 * if REMOVE_FIXED_CONTINUATIONS is given)
616 * -- marks lines dirty as needed
617 * -- marks textruns dirty (unless FRAMES_ARE_EMPTY is given, in which
618 * case textruns do not need to be dirtied)
619 * -- destroys all removed frames
621 enum { REMOVE_FIXED_CONTINUATIONS = 0x02, FRAMES_ARE_EMPTY = 0x04 };
622 void DoRemoveFrame(DestroyContext&, nsIFrame* aDeletedFrame, uint32_t aFlags);
624 void ReparentFloats(nsIFrame* aFirstFrame, nsBlockFrame* aOldParent,
625 bool aReparentSiblings);
627 bool ComputeCustomOverflow(mozilla::OverflowAreas&) override;
629 void UnionChildOverflow(mozilla::OverflowAreas&) override;
632 * Load all of aFrame's floats into the float manager iff aFrame is not a
633 * block formatting context. Handles all necessary float manager translations;
634 * assumes float manager is in aFrame's parent's coord system.
636 * Safe to call on non-blocks (does nothing).
638 static void RecoverFloatsFor(nsIFrame* aFrame, nsFloatManager& aFloatManager,
639 mozilla::WritingMode aWM,
640 const nsSize& aContainerSize);
643 * Determine if we have any pushed floats from a previous continuation.
645 * @returns true, if any of the floats at the beginning of our floats list
646 * have the NS_FRAME_IS_PUSHED_FLOAT bit set; false otherwise.
648 bool HasPushedFloatsFromPrevContinuation() const;
650 // @see nsIFrame::AddSizeOfExcludingThisForTree
651 void AddSizeOfExcludingThisForTree(nsWindowSizes&) const override;
654 * Clears any -webkit-line-clamp ellipsis on a line in this block or one
655 * of its descendants.
657 void ClearLineClampEllipsis();
660 * Returns whether this block is in a -webkit-line-clamp context. That is,
661 * whether this block is in a block formatting-context whose root block has
662 * -webkit-line-clamp: <n>.
664 bool IsInLineClampContext() const;
667 * @return false iff this block does not have a float on any child list.
668 * This function is O(1).
670 bool MaybeHasFloats() const;
672 protected:
673 /** grab overflow lines from this block's prevInFlow, and make them
674 * part of this block's mLines list.
675 * @return true if any lines were drained.
677 bool DrainOverflowLines();
680 * Moves frames from our PushedFloats list back into our mFloats list.
682 void DrainSelfPushedFloats();
685 * First calls DrainSelfPushedFloats() then grabs pushed floats from this
686 * block's prev-in-flow, and splice them into this block's mFloats list too.
688 void DrainPushedFloats();
690 /** Load all our floats into the float manager (without reflowing them).
691 * Assumes float manager is in our own coordinate system.
693 void RecoverFloats(nsFloatManager& aFloatManager, mozilla::WritingMode aWM,
694 const nsSize& aContainerSize);
696 /** Reflow pushed floats
698 void ReflowPushedFloats(BlockReflowState& aState,
699 mozilla::OverflowAreas& aOverflowAreas);
702 * Find any trailing BR clear from the last line of this block (or from its
703 * prev-in-flows).
705 mozilla::StyleClear FindTrailingClear();
708 * Remove a float from our float list.
710 void RemoveFloat(nsIFrame* aFloat);
712 * Remove a float from the float cache for the line its placeholder is on.
714 void RemoveFloatFromFloatCache(nsIFrame* aFloat);
716 void CollectFloats(nsIFrame* aFrame, nsFrameList& aList,
717 bool aCollectFromSiblings) {
718 if (MaybeHasFloats()) {
719 DoCollectFloats(aFrame, aList, aCollectFromSiblings);
722 void DoCollectFloats(nsIFrame* aFrame, nsFrameList& aList,
723 bool aCollectFromSiblings);
725 // Remove a float, abs, rel positioned frame from the appropriate block's list
726 static void DoRemoveOutOfFlowFrame(DestroyContext&, nsIFrame*);
728 /** set up the conditions necessary for an resize reflow
729 * the primary task is to mark the minimumly sufficient lines dirty.
731 void PrepareResizeReflow(BlockReflowState& aState);
734 * Reflow all lines that have been marked dirty.
735 * Returns whether an overflow-wrap break was used anywhere.
737 bool ReflowDirtyLines(BlockReflowState& aState);
739 /** Mark a given line dirty due to reflow being interrupted on or before it */
740 void MarkLineDirtyForInterrupt(nsLineBox* aLine);
742 //----------------------------------------
743 // Methods for line reflow
745 * Reflow a line.
747 * @param aState
748 * the current reflow input
749 * @param aLine
750 * the line to reflow. can contain a single block frame or contain 1 or
751 * more inline frames.
752 * @param aKeepReflowGoing [OUT]
753 * indicates whether the caller should continue to reflow more lines
754 * @returns
755 * whether an overflow-wrap breakpoint was used
757 bool ReflowLine(BlockReflowState& aState, LineIterator aLine,
758 bool* aKeepReflowGoing);
760 // Return false if it needs another reflow because of reduced space
761 // between floats that are next to it (but not next to its top), and
762 // return true otherwise.
763 bool PlaceLine(BlockReflowState& aState, nsLineLayout& aLineLayout,
764 LineIterator aLine,
765 nsFloatManager::SavedState* aFloatStateBeforeLine,
766 nsFlowAreaRect& aFlowArea, // in-out
767 nscoord& aAvailableSpaceBSize, // in-out
768 bool* aKeepReflowGoing);
771 * If NS_BLOCK_LOOK_FOR_DIRTY_FRAMES is set, call MarkLineDirty
772 * on any line with a child frame that is dirty.
774 void LazyMarkLinesDirty();
777 * Mark |aLine| dirty, and, if necessary because of possible
778 * pull-up, mark the previous line dirty as well. Also invalidates textruns
779 * on those lines because the text in the lines might have changed due to
780 * addition/removal of frames.
781 * @param aLine the line to mark dirty
782 * @param aLineList the line list containing that line
784 void MarkLineDirty(LineIterator aLine, const nsLineList* aLineList);
786 // XXX where to go
787 bool IsLastLine(BlockReflowState& aState, LineIterator aLine);
789 void DeleteLine(BlockReflowState& aState, nsLineList::iterator aLine,
790 nsLineList::iterator aLineEnd);
792 //----------------------------------------
793 // Methods for individual frame reflow
795 bool ShouldApplyBStartMargin(BlockReflowState& aState, nsLineBox* aLine);
797 void ReflowBlockFrame(BlockReflowState& aState, LineIterator aLine,
798 bool* aKeepGoing);
800 // Returns whether an overflow-wrap break was used.
801 bool ReflowInlineFrames(BlockReflowState& aState, LineIterator aLine,
802 bool* aKeepLineGoing);
804 void DoReflowInlineFrames(
805 BlockReflowState& aState, nsLineLayout& aLineLayout, LineIterator aLine,
806 nsFlowAreaRect& aFloatAvailableSpace, nscoord& aAvailableSpaceBSize,
807 nsFloatManager::SavedState* aFloatStateBeforeLine, bool* aKeepReflowGoing,
808 LineReflowStatus* aLineReflowStatus, bool aAllowPullUp);
810 void ReflowInlineFrame(BlockReflowState& aState, nsLineLayout& aLineLayout,
811 LineIterator aLine, nsIFrame* aFrame,
812 LineReflowStatus* aLineReflowStatus);
814 // @param aReflowStatus an incomplete status indicates the float should be
815 // split but only if the available block-size is constrained.
816 void ReflowFloat(BlockReflowState& aState, ReflowInput& aFloatRI,
817 nsIFrame* aFloat, nsReflowStatus& aReflowStatus);
819 //----------------------------------------
820 // Methods for pushing/pulling lines/frames
823 * Create a next-in-flow, if necessary, for aFrame. If a new frame is
824 * created, place it in aLine if aLine is not null.
825 * @param aState the block reflow state
826 * @param aLine where to put a new frame
827 * @param aFrame the frame
828 * @return true if a new frame was created, false if not
830 bool CreateContinuationFor(BlockReflowState& aState, nsLineBox* aLine,
831 nsIFrame* aFrame);
834 * Set line-break-before status in aState.mReflowStatus because aLine cannot
835 * be placed on this page/column and we don't want to break within ourselves.
836 * Also, mark the aLine dirty, and set aKeepReflowGoing to false;
838 void SetBreakBeforeStatusBeforeLine(BlockReflowState& aState,
839 LineIterator aLine,
840 bool* aKeepReflowGoing);
843 * Indicates if we need to compute a page name for the next page when pushing
844 * a truncated line.
846 * Using a value of No saves work when a new page name has already been set
847 * with nsCSSFrameConstructor::SetNextPageContentFramePageName.
849 enum class ComputeNewPageNameIfNeeded : uint8_t { Yes, No };
852 * Push aLine (and any after it), since it cannot be placed on this
853 * page/column. Set aKeepReflowGoing to false and set
854 * flag aState.mReflowStatus as incomplete.
856 void PushTruncatedLine(BlockReflowState& aState, LineIterator aLine,
857 bool* aKeepReflowGoing,
858 ComputeNewPageNameIfNeeded aComputeNewPageName =
859 ComputeNewPageNameIfNeeded::Yes);
861 void SplitLine(BlockReflowState& aState, nsLineLayout& aLineLayout,
862 LineIterator aLine, nsIFrame* aFrame,
863 LineReflowStatus* aLineReflowStatus);
866 * Pull a frame from the next available location (one of our lines or
867 * one of our next-in-flows lines).
868 * @return the pulled frame or nullptr
870 nsIFrame* PullFrame(BlockReflowState& aState, LineIterator aLine);
873 * Try to pull a frame out of a line pointed at by aFromLine.
875 * Note: pulling a frame from a line that is a place-holder frame
876 * doesn't automatically remove the corresponding float from the
877 * line's float array. This happens indirectly: either the line gets
878 * emptied (and destroyed) or the line gets reflowed (because we mark
879 * it dirty) and the code at the top of ReflowLine empties the
880 * array. So eventually, it will be removed, just not right away.
882 * @return the pulled frame or nullptr
884 nsIFrame* PullFrameFrom(nsLineBox* aLine, nsBlockFrame* aFromContainer,
885 nsLineList::iterator aFromLine);
888 * Push the line after aLineBefore to the overflow line list.
889 * @param aLineBefore a line in 'mLines' (or LinesBegin() when
890 * pushing the first line)
892 void PushLines(BlockReflowState& aState, nsLineList::iterator aLineBefore);
894 void PropagateFloatDamage(BlockReflowState& aState, nsLineBox* aLine,
895 nscoord aDeltaBCoord);
897 void CheckFloats(BlockReflowState& aState);
899 //----------------------------------------
900 // List handling kludge
902 void ReflowOutsideMarker(nsIFrame* aMarkerFrame, BlockReflowState& aState,
903 ReflowOutput& aMetrics, nscoord aLineTop);
905 //----------------------------------------
907 NS_DECLARE_FRAME_PROPERTY_DELETABLE(LineIteratorProperty, nsLineIterator);
909 bool CanProvideLineIterator() const final { return true; }
910 nsILineIterator* GetLineIterator() final;
912 public:
913 bool HasOverflowLines() const {
914 return HasAnyStateBits(NS_BLOCK_HAS_OVERFLOW_LINES);
916 FrameLines* GetOverflowLines() const;
918 protected:
919 FrameLines* RemoveOverflowLines();
920 void SetOverflowLines(FrameLines* aOverflowLines);
921 void DestroyOverflowLines();
924 * This class is useful for efficiently modifying the out of flow
925 * overflow list. It gives the client direct writable access to
926 * the frame list temporarily but ensures that property is only
927 * written back if absolutely necessary.
929 struct nsAutoOOFFrameList {
930 nsFrameList mList;
932 explicit nsAutoOOFFrameList(nsBlockFrame* aBlock)
933 : mPropValue(aBlock->GetOverflowOutOfFlows()), mBlock(aBlock) {
934 if (mPropValue) {
935 mList = std::move(*mPropValue);
938 ~nsAutoOOFFrameList() {
939 mBlock->SetOverflowOutOfFlows(std::move(mList), mPropValue);
942 protected:
943 nsFrameList* const mPropValue;
944 nsBlockFrame* const mBlock;
946 friend struct nsAutoOOFFrameList;
948 nsFrameList* GetOverflowOutOfFlows() const;
950 // This takes ownership of the frames in aList.
951 void SetOverflowOutOfFlows(nsFrameList&& aList, nsFrameList* aPropValue);
953 // Return the ::marker frame or nullptr if we don't have one.
954 nsIFrame* GetMarker() const {
955 nsIFrame* outside = GetOutsideMarker();
956 return outside ? outside : GetInsideMarker();
959 // Return the inside ::marker frame or nullptr if we don't have one.
960 nsIFrame* GetInsideMarker() const;
962 // Return the outside ::marker frame or nullptr if we don't have one.
963 nsIFrame* GetOutsideMarker() const;
965 // Return the outside ::marker frame list frame property.
966 nsFrameList* GetOutsideMarkerList() const;
968 // Return true if this frame has floats.
969 bool HasFloats() const;
971 // Get the floats list, or nullptr if there isn't one.
972 nsFrameList* GetFloats() const;
974 // Get the floats list, or if there is not currently one, make a new empty
975 // one.
976 nsFrameList* EnsureFloats() MOZ_NONNULL_RETURN;
978 // Get the float list and remove the property from this frame.
980 // The caller is responsible for deleting the returned list and managing the
981 // ownership of all frames in the list.
982 [[nodiscard]] nsFrameList* StealFloats();
984 // Return true if this frame has pushed floats.
985 bool HasPushedFloats() const;
987 // Get the pushed floats list, or nullptr if there isn't one.
989 // The pushed floats list is used for *temporary* storage of floats during
990 // reflow, between when we decide they don't fit in this block until our next
991 // continuation takes them.
992 nsFrameList* GetPushedFloats() const;
994 // Get the pushed floats list, or if there is not currently one,
995 // make a new empty one.
996 nsFrameList* EnsurePushedFloats() MOZ_NONNULL_RETURN;
998 // Get the pushed float list and remove the property from this frame.
1000 // The caller is responsible for deleting the returned list and managing the
1001 // ownership of all frames in the list.
1002 [[nodiscard]] nsFrameList* StealPushedFloats();
1004 #ifdef DEBUG
1005 void VerifyLines(bool aFinalCheckOK);
1006 void VerifyOverflowSituation();
1007 int32_t GetDepth() const;
1008 #endif
1010 nscoord mCachedMinISize = NS_INTRINSIC_ISIZE_UNKNOWN;
1011 nscoord mCachedPrefISize = NS_INTRINSIC_ISIZE_UNKNOWN;
1013 nsLineList mLines;
1015 friend class mozilla::BlockReflowState;
1016 friend class nsBlockInFlowLineIterator;
1018 #ifdef DEBUG
1019 public:
1020 static bool gLamePaintMetrics;
1021 static bool gLameReflowMetrics;
1022 static bool gNoisy;
1023 static bool gNoisyDamageRepair;
1024 static bool gNoisyIntrinsic;
1025 static bool gNoisyReflow;
1026 static bool gReallyNoisyReflow;
1027 static bool gNoisyFloatManager;
1028 static bool gVerifyLines;
1029 static bool gDisableResizeOpt;
1031 static int32_t gNoiseIndent;
1033 static const char* kReflowCommandType[];
1035 protected:
1036 static void InitDebugFlags();
1037 #endif
1040 #ifdef DEBUG
1041 class AutoNoisyIndenter {
1042 public:
1043 explicit AutoNoisyIndenter(bool aDoIndent) : mIndented(aDoIndent) {
1044 if (mIndented) {
1045 nsBlockFrame::gNoiseIndent++;
1048 ~AutoNoisyIndenter() {
1049 if (mIndented) {
1050 nsBlockFrame::gNoiseIndent--;
1054 private:
1055 bool mIndented;
1057 #endif
1060 * Iterates over all lines in the prev-in-flows/next-in-flows of this block.
1062 class nsBlockInFlowLineIterator {
1063 public:
1064 typedef nsBlockFrame::LineIterator LineIterator;
1066 * Set up the iterator to point to aLine which must be a normal line
1067 * in aFrame (not an overflow line).
1069 nsBlockInFlowLineIterator(nsBlockFrame* aFrame, LineIterator aLine);
1071 * Set up the iterator to point to the first line found starting from
1072 * aFrame. Sets aFoundValidLine to false if there is no such line.
1073 * After aFoundValidLine has returned false, don't call any methods on this
1074 * object again.
1076 nsBlockInFlowLineIterator(nsBlockFrame* aFrame, bool* aFoundValidLine);
1078 * Set up the iterator to point to the line that contains aFindFrame (either
1079 * directly or indirectly). If aFrame is out of flow, or contained in an
1080 * out-of-flow, finds the line containing the out-of-flow's placeholder. If
1081 * the frame is not found, sets aFoundValidLine to false. After
1082 * aFoundValidLine has returned false, don't call any methods on this
1083 * object again.
1085 nsBlockInFlowLineIterator(nsBlockFrame* aFrame, nsIFrame* aFindFrame,
1086 bool* aFoundValidLine);
1088 // Allow to be uninitialized (and then assigned from another object).
1089 nsBlockInFlowLineIterator() : mFrame(nullptr) {}
1091 LineIterator GetLine() { return mLine; }
1092 bool IsLastLineInList();
1093 nsBlockFrame* GetContainer() { return mFrame; }
1094 bool GetInOverflow() { return mLineList != &mFrame->mLines; }
1097 * Returns the current line list we're iterating, null means
1098 * we're iterating |mLines| of the container.
1100 nsLineList* GetLineList() { return mLineList; }
1103 * Returns the end-iterator of whatever line list we're in.
1105 LineIterator End();
1108 * Returns false if there are no more lines. After this has returned false,
1109 * don't call any methods on this object again.
1111 bool Next();
1113 * Returns false if there are no more lines. After this has returned false,
1114 * don't call any methods on this object again.
1116 bool Prev();
1118 // XXX nsBlockFrame uses this internally in one place. Try to remove it.
1119 // XXX uhm, and nsBidiPresUtils::Resolve too.
1120 nsBlockInFlowLineIterator(nsBlockFrame* aFrame, LineIterator aLine,
1121 bool aInOverflow);
1123 private:
1124 nsBlockFrame* mFrame;
1125 LineIterator mLine;
1126 nsLineList* mLineList; // the line list mLine is in
1129 * Moves iterator to next valid line reachable from the current block.
1130 * Returns false if there are no valid lines.
1132 bool FindValidLine();
1135 #endif /* nsBlockFrame_h___ */