Bug 1874684 - Part 33: Defer allocation of options object for CalendarDateFromFields...
[gecko.git] / layout / tables / nsTableWrapperFrame.cpp
blobaa898ddf6f34a966c21889326977880f6660522c
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 #include "nsTableWrapperFrame.h"
8 #include "LayoutConstants.h"
9 #include "mozilla/ComputedStyle.h"
10 #include "mozilla/PresShell.h"
11 #include "nsFrameManager.h"
12 #include "nsGridContainerFrame.h"
13 #include "nsTableFrame.h"
14 #include "nsTableCellFrame.h"
15 #include "nsStyleConsts.h"
16 #include "nsPresContext.h"
17 #include "nsCSSRendering.h"
18 #include "nsIContent.h"
19 #include "prinrval.h"
20 #include "nsGkAtoms.h"
21 #include "nsHTMLParts.h"
22 #include "nsDisplayList.h"
23 #include "nsLayoutUtils.h"
24 #include "nsIFrameInlines.h"
25 #include <algorithm>
27 using namespace mozilla;
28 using namespace mozilla::layout;
30 nscoord nsTableWrapperFrame::SynthesizeFallbackBaseline(
31 mozilla::WritingMode aWM, BaselineSharingGroup aBaselineGroup) const {
32 const auto marginBlockEnd = GetLogicalUsedMargin(aWM).BEnd(aWM);
33 if (aWM.IsCentralBaseline()) {
34 return (BSize(aWM) + marginBlockEnd) / 2;
36 // Our fallback baseline is the block-end margin-edge, with respect to the
37 // given writing mode.
38 if (aBaselineGroup == BaselineSharingGroup::Last) {
39 return -marginBlockEnd;
41 return BSize(aWM) + marginBlockEnd;
44 Maybe<nscoord> nsTableWrapperFrame::GetNaturalBaselineBOffset(
45 WritingMode aWM, BaselineSharingGroup aBaselineGroup,
46 BaselineExportContext aExportContext) const {
47 // Baseline is determined by row
48 // (https://drafts.csswg.org/css-align-3/#baseline-export). If the row
49 // direction is going to be orthogonal to the parent's writing mode, the
50 // resulting baseline wouldn't be valid, so we use the fallback baseline
51 // instead.
52 if (StyleDisplay()->IsContainLayout() ||
53 GetWritingMode().IsOrthogonalTo(aWM)) {
54 return Nothing{};
56 auto* innerTable = InnerTableFrame();
57 return innerTable
58 ->GetNaturalBaselineBOffset(aWM, aBaselineGroup, aExportContext)
59 .map([this, aWM, aBaselineGroup, innerTable](nscoord aBaseline) {
60 auto bStart = innerTable->BStart(aWM, mRect.Size());
61 if (aBaselineGroup == BaselineSharingGroup::First) {
62 return aBaseline + bStart;
64 auto bEnd = bStart + innerTable->BSize(aWM);
65 return BSize(aWM) - (bEnd - aBaseline);
66 });
69 nsTableWrapperFrame::nsTableWrapperFrame(ComputedStyle* aStyle,
70 nsPresContext* aPresContext,
71 ClassID aID)
72 : nsContainerFrame(aStyle, aPresContext, aID) {}
74 nsTableWrapperFrame::~nsTableWrapperFrame() = default;
76 NS_QUERYFRAME_HEAD(nsTableWrapperFrame)
77 NS_QUERYFRAME_ENTRY(nsTableWrapperFrame)
78 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
80 #ifdef ACCESSIBILITY
81 a11y::AccType nsTableWrapperFrame::AccessibleType() {
82 return a11y::eHTMLTableType;
84 #endif
86 void nsTableWrapperFrame::Destroy(DestroyContext& aContext) {
87 DestroyAbsoluteFrames(aContext);
88 mCaptionFrames.DestroyFrames(aContext);
89 nsContainerFrame::Destroy(aContext);
92 const nsFrameList& nsTableWrapperFrame::GetChildList(
93 ChildListID aListID) const {
94 if (aListID == FrameChildListID::Caption) {
95 return mCaptionFrames;
98 return nsContainerFrame::GetChildList(aListID);
101 void nsTableWrapperFrame::GetChildLists(nsTArray<ChildList>* aLists) const {
102 nsContainerFrame::GetChildLists(aLists);
103 mCaptionFrames.AppendIfNonempty(aLists, FrameChildListID::Caption);
106 void nsTableWrapperFrame::SetInitialChildList(ChildListID aListID,
107 nsFrameList&& aChildList) {
108 if (FrameChildListID::Caption == aListID) {
109 #ifdef DEBUG
110 nsIFrame::VerifyDirtyBitSet(aChildList);
111 for (nsIFrame* f : aChildList) {
112 MOZ_ASSERT(f->GetParent() == this, "Unexpected parent");
114 #endif
115 // the frame constructor already checked for table-caption display type
116 MOZ_ASSERT(mCaptionFrames.IsEmpty(),
117 "already have child frames in CaptionList");
118 mCaptionFrames = std::move(aChildList);
119 } else {
120 MOZ_ASSERT(FrameChildListID::Principal != aListID ||
121 (aChildList.FirstChild() &&
122 aChildList.FirstChild() == aChildList.LastChild() &&
123 aChildList.FirstChild()->IsTableFrame()),
124 "expected a single table frame in principal child list");
125 nsContainerFrame::SetInitialChildList(aListID, std::move(aChildList));
129 void nsTableWrapperFrame::AppendFrames(ChildListID aListID,
130 nsFrameList&& aFrameList) {
131 // We only have two child frames: the inner table and a caption frame.
132 // The inner frame is provided when we're initialized, and it cannot change
133 MOZ_ASSERT(FrameChildListID::Caption == aListID, "unexpected child list");
134 MOZ_ASSERT(aFrameList.IsEmpty() || aFrameList.FirstChild()->IsTableCaption(),
135 "appending non-caption frame to captionList");
136 mCaptionFrames.AppendFrames(nullptr, std::move(aFrameList));
138 // Reflow the new caption frame. It's already marked dirty, so
139 // just tell the pres shell.
140 PresShell()->FrameNeedsReflow(this, IntrinsicDirty::FrameAndAncestors,
141 NS_FRAME_HAS_DIRTY_CHILDREN);
142 // The presence of caption frames makes us sort our display
143 // list differently, so mark us as changed for the new
144 // ordering.
145 MarkNeedsDisplayItemRebuild();
148 void nsTableWrapperFrame::InsertFrames(
149 ChildListID aListID, nsIFrame* aPrevFrame,
150 const nsLineList::iterator* aPrevFrameLine, nsFrameList&& aFrameList) {
151 MOZ_ASSERT(FrameChildListID::Caption == aListID, "unexpected child list");
152 MOZ_ASSERT(aFrameList.IsEmpty() || aFrameList.FirstChild()->IsTableCaption(),
153 "inserting non-caption frame into captionList");
154 MOZ_ASSERT(!aPrevFrame || aPrevFrame->GetParent() == this,
155 "inserting after sibling frame with different parent");
156 mCaptionFrames.InsertFrames(nullptr, aPrevFrame, std::move(aFrameList));
158 // Reflow the new caption frame. It's already marked dirty, so
159 // just tell the pres shell.
160 PresShell()->FrameNeedsReflow(this, IntrinsicDirty::FrameAndAncestors,
161 NS_FRAME_HAS_DIRTY_CHILDREN);
162 MarkNeedsDisplayItemRebuild();
165 void nsTableWrapperFrame::RemoveFrame(DestroyContext& aContext,
166 ChildListID aListID,
167 nsIFrame* aOldFrame) {
168 // We only have two child frames: the inner table and one caption frame.
169 // The inner frame can't be removed so this should be the caption
170 MOZ_ASSERT(FrameChildListID::Caption == aListID, "can't remove inner frame");
172 // Remove the frame and destroy it
173 mCaptionFrames.DestroyFrame(aContext, aOldFrame);
175 PresShell()->FrameNeedsReflow(this, IntrinsicDirty::FrameAndAncestors,
176 NS_FRAME_HAS_DIRTY_CHILDREN);
177 MarkNeedsDisplayItemRebuild();
180 void nsTableWrapperFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
181 const nsDisplayListSet& aLists) {
182 // No border or background is painted because they belong to the inner table.
183 // The outline belongs to the wrapper frame so it can contain the caption.
185 // If there's no caption, take a short cut to avoid having to create
186 // the special display list set and then sort it.
187 if (mCaptionFrames.IsEmpty()) {
188 BuildDisplayListForInnerTable(aBuilder, aLists);
189 DisplayOutline(aBuilder, aLists);
190 return;
193 nsDisplayListCollection set(aBuilder);
194 BuildDisplayListForInnerTable(aBuilder, set);
196 nsDisplayListSet captionSet(set, set.BlockBorderBackgrounds());
197 BuildDisplayListForChild(aBuilder, mCaptionFrames.FirstChild(), captionSet);
199 // Now we have to sort everything by content order, since the caption
200 // may be somewhere inside the table.
201 // We don't sort BlockBorderBackgrounds and BorderBackgrounds because the
202 // display items in those lists should stay out of content order in order to
203 // follow the rules in https://www.w3.org/TR/CSS21/zindex.html#painting-order
204 // and paint the caption background after all of the rest.
205 set.Floats()->SortByContentOrder(GetContent());
206 set.Content()->SortByContentOrder(GetContent());
207 set.PositionedDescendants()->SortByContentOrder(GetContent());
208 set.Outlines()->SortByContentOrder(GetContent());
209 set.MoveTo(aLists);
211 DisplayOutline(aBuilder, aLists);
214 void nsTableWrapperFrame::BuildDisplayListForInnerTable(
215 nsDisplayListBuilder* aBuilder, const nsDisplayListSet& aLists) {
216 // Just paint the regular children, but the children's background is our
217 // true background (there should only be one, the real table)
218 nsIFrame* kid = mFrames.FirstChild();
219 // The children should be in content order
220 while (kid) {
221 BuildDisplayListForChild(aBuilder, kid, aLists);
222 kid = kid->GetNextSibling();
226 ComputedStyle* nsTableWrapperFrame::GetParentComputedStyle(
227 nsIFrame** aProviderFrame) const {
228 // The table wrapper frame and the (inner) table frame split the style
229 // data by giving the table frame the ComputedStyle associated with
230 // the table content node and creating a ComputedStyle for the wrapper
231 // frame that is a *child* of the table frame's ComputedStyle,
232 // matching the ::-moz-table-wrapper pseudo-element. html.css has a
233 // rule that causes that pseudo-element (and thus the wrapper table)
234 // to inherit *some* style properties from the table frame. The
235 // children of the table inherit directly from the inner table, and
236 // the table wrapper's ComputedStyle is a leaf.
238 return (*aProviderFrame = InnerTableFrame())->Style();
241 /* virtual */
242 nscoord nsTableWrapperFrame::GetMinISize(gfxContext* aRenderingContext) {
243 nscoord iSize = nsLayoutUtils::IntrinsicForContainer(
244 aRenderingContext, InnerTableFrame(), IntrinsicISizeType::MinISize);
245 if (mCaptionFrames.NotEmpty()) {
246 nscoord capISize = nsLayoutUtils::IntrinsicForContainer(
247 aRenderingContext, mCaptionFrames.FirstChild(),
248 IntrinsicISizeType::MinISize);
249 if (capISize > iSize) {
250 iSize = capISize;
253 return iSize;
256 /* virtual */
257 nscoord nsTableWrapperFrame::GetPrefISize(gfxContext* aRenderingContext) {
258 nscoord maxISize = nsLayoutUtils::IntrinsicForContainer(
259 aRenderingContext, InnerTableFrame(), IntrinsicISizeType::PrefISize);
260 if (mCaptionFrames.NotEmpty()) {
261 // Don't let the caption's pref isize expand the table's pref isize.
262 const nscoord capMinISize = nsLayoutUtils::IntrinsicForContainer(
263 aRenderingContext, mCaptionFrames.FirstChild(),
264 IntrinsicISizeType::MinISize);
265 maxISize = std::max(maxISize, capMinISize);
267 return maxISize;
270 LogicalSize nsTableWrapperFrame::InnerTableShrinkWrapSize(
271 gfxContext* aRenderingContext, nsTableFrame* aTableFrame, WritingMode aWM,
272 const LogicalSize& aCBSize, nscoord aAvailableISize,
273 const StyleSizeOverrides& aSizeOverrides, ComputeSizeFlags aFlags) const {
274 MOZ_ASSERT(InnerTableFrame() == aTableFrame);
276 AutoMaybeDisableFontInflation an(aTableFrame);
278 Maybe<LogicalMargin> collapseBorder;
279 Maybe<LogicalMargin> collapsePadding;
280 aTableFrame->GetCollapsedBorderPadding(collapseBorder, collapsePadding);
282 SizeComputationInput input(aTableFrame, aRenderingContext, aWM,
283 aCBSize.ISize(aWM), collapseBorder,
284 collapsePadding);
285 LogicalSize marginSize(aWM); // Inner table doesn't have any margin
286 LogicalSize bpSize = input.ComputedLogicalBorderPadding(aWM).Size(aWM);
288 // Note that we pass an empty caption-area here (rather than the caption's
289 // actual size). This is fine because:
291 // 1) nsTableWrapperFrame::ComputeSize() only uses the size returned by this
292 // method (indirectly via calling nsTableWrapperFrame::ComputeAutoSize())
293 // if it get a aSizeOverrides arg containing any size overrides with
294 // mApplyOverridesVerbatim=true. The aSizeOverrides arg is passed to this
295 // method without any modifications.
297 // 2) With 1), that means the aSizeOverrides passing into this method should
298 // be applied to the inner table directly, so we don't need to subtract
299 // caption-area when preparing innerOverrides for
300 // nsTableFrame::ComputeSize().
301 StyleSizeOverrides innerOverrides = ComputeSizeOverridesForInnerTable(
302 aTableFrame, aSizeOverrides, bpSize, /* aBSizeOccupiedByCaption = */ 0);
303 auto size =
304 aTableFrame
305 ->ComputeSize(aRenderingContext, aWM, aCBSize, aAvailableISize,
306 marginSize, bpSize, innerOverrides, aFlags)
307 .mLogicalSize;
308 size.ISize(aWM) += bpSize.ISize(aWM);
309 if (size.BSize(aWM) != NS_UNCONSTRAINEDSIZE) {
310 size.BSize(aWM) += bpSize.BSize(aWM);
312 return size;
315 LogicalSize nsTableWrapperFrame::CaptionShrinkWrapSize(
316 gfxContext* aRenderingContext, nsIFrame* aCaptionFrame, WritingMode aWM,
317 const LogicalSize& aCBSize, nscoord aAvailableISize,
318 ComputeSizeFlags aFlags) const {
319 MOZ_ASSERT(aCaptionFrame == mCaptionFrames.FirstChild());
321 AutoMaybeDisableFontInflation an(aCaptionFrame);
323 SizeComputationInput input(aCaptionFrame, aRenderingContext, aWM,
324 aCBSize.ISize(aWM));
325 LogicalSize marginSize = input.ComputedLogicalMargin(aWM).Size(aWM);
326 LogicalSize bpSize = input.ComputedLogicalBorderPadding(aWM).Size(aWM);
328 auto size = aCaptionFrame
329 ->ComputeSize(aRenderingContext, aWM, aCBSize,
330 aAvailableISize, marginSize, bpSize, {}, aFlags)
331 .mLogicalSize;
332 size.ISize(aWM) += (marginSize.ISize(aWM) + bpSize.ISize(aWM));
333 if (size.BSize(aWM) != NS_UNCONSTRAINEDSIZE) {
334 size.BSize(aWM) += (marginSize.BSize(aWM) + bpSize.BSize(aWM));
336 return size;
339 StyleSize nsTableWrapperFrame::ReduceStyleSizeBy(
340 const StyleSize& aStyleSize, const nscoord aAmountToReduce) const {
341 MOZ_ASSERT(aStyleSize.ConvertsToLength(), "Only handles 'Length' StyleSize!");
342 const nscoord size = std::max(0, aStyleSize.ToLength() - aAmountToReduce);
343 return StyleSize::LengthPercentage(LengthPercentage::FromAppUnits(size));
346 StyleSizeOverrides nsTableWrapperFrame::ComputeSizeOverridesForInnerTable(
347 const nsTableFrame* aTableFrame,
348 const StyleSizeOverrides& aWrapperSizeOverrides,
349 const LogicalSize& aBorderPadding, nscoord aBSizeOccupiedByCaption) const {
350 if (aWrapperSizeOverrides.mApplyOverridesVerbatim ||
351 !aWrapperSizeOverrides.HasAnyLengthOverrides()) {
352 // We are asked to apply the size overrides directly to the inner table, or
353 // there's no 'Length' size overrides. No need to tweak the size overrides.
354 return aWrapperSizeOverrides;
357 const auto wm = aTableFrame->GetWritingMode();
358 LogicalSize areaOccupied(wm, 0, aBSizeOccupiedByCaption);
359 if (aTableFrame->StylePosition()->mBoxSizing == StyleBoxSizing::Content) {
360 // If the inner table frame has 'box-sizing: content', enlarge the occupied
361 // area by adding border & padding because they should also be subtracted
362 // from the size overrides.
363 areaOccupied += aBorderPadding;
366 StyleSizeOverrides innerSizeOverrides;
367 const auto& wrapperISize = aWrapperSizeOverrides.mStyleISize;
368 if (wrapperISize) {
369 MOZ_ASSERT(!wrapperISize->HasPercent(),
370 "Table doesn't support size overrides containing percentages!");
371 innerSizeOverrides.mStyleISize.emplace(
372 wrapperISize->ConvertsToLength()
373 ? ReduceStyleSizeBy(*wrapperISize, areaOccupied.ISize(wm))
374 : *wrapperISize);
377 const auto& wrapperBSize = aWrapperSizeOverrides.mStyleBSize;
378 if (wrapperBSize) {
379 MOZ_ASSERT(!wrapperBSize->HasPercent(),
380 "Table doesn't support size overrides containing percentages!");
381 innerSizeOverrides.mStyleBSize.emplace(
382 wrapperBSize->ConvertsToLength()
383 ? ReduceStyleSizeBy(*wrapperBSize, areaOccupied.BSize(wm))
384 : *wrapperBSize);
387 return innerSizeOverrides;
390 /* virtual */
391 nsIFrame::SizeComputationResult nsTableWrapperFrame::ComputeSize(
392 gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
393 nscoord aAvailableISize, const LogicalSize& aMargin,
394 const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
395 ComputeSizeFlags aFlags) {
396 auto result = nsContainerFrame::ComputeSize(
397 aRenderingContext, aWM, aCBSize, aAvailableISize, aMargin, aBorderPadding,
398 aSizeOverrides, aFlags);
400 if (aSizeOverrides.mApplyOverridesVerbatim &&
401 aSizeOverrides.HasAnyOverrides()) {
402 // We are asked to apply the size overrides directly to the inner table, but
403 // we still want ourselves to remain 'auto'-sized and shrink-wrapping our
404 // children's sizes. (Table wrapper frames always have 'auto' inline-size
405 // and block-size, since we don't inherit those properties from inner table,
406 // and authors can't target them with styling.)
407 auto size =
408 ComputeAutoSize(aRenderingContext, aWM, aCBSize, aAvailableISize,
409 aMargin, aBorderPadding, aSizeOverrides, aFlags);
410 result.mLogicalSize = size;
413 return result;
416 /* virtual */
417 LogicalSize nsTableWrapperFrame::ComputeAutoSize(
418 gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
419 nscoord aAvailableISize, const LogicalSize& aMargin,
420 const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
421 ComputeSizeFlags aFlags) {
422 nscoord kidAvailableISize = aAvailableISize - aMargin.ISize(aWM);
423 NS_ASSERTION(aBorderPadding.IsAllZero(),
424 "Table wrapper frames cannot have borders or paddings");
426 // When we're shrink-wrapping, our auto size needs to wrap around the
427 // actual size of the table, which (if it is specified as a percent)
428 // could be something that is not reflected in our GetMinISize and
429 // GetPrefISize. See bug 349457 for an example.
430 const ComputeSizeFlags flags = CreateComputeSizeFlagsForChild();
432 // Match the logic in Reflow() that sets aside space for the caption.
433 Maybe<StyleCaptionSide> captionSide = GetCaptionSide();
435 const LogicalSize innerTableSize = InnerTableShrinkWrapSize(
436 aRenderingContext, InnerTableFrame(), aWM, aCBSize, kidAvailableISize,
437 aSizeOverrides, flags);
438 if (!captionSide) {
439 return innerTableSize;
441 const LogicalSize captionSize =
442 CaptionShrinkWrapSize(aRenderingContext, mCaptionFrames.FirstChild(), aWM,
443 aCBSize, innerTableSize.ISize(aWM), flags);
444 const nscoord iSize =
445 std::max(innerTableSize.ISize(aWM), captionSize.ISize(aWM));
446 nscoord bSize = NS_UNCONSTRAINEDSIZE;
447 if (innerTableSize.BSize(aWM) != NS_UNCONSTRAINEDSIZE &&
448 captionSize.BSize(aWM) != NS_UNCONSTRAINEDSIZE) {
449 bSize = innerTableSize.BSize(aWM) + captionSize.BSize(aWM);
451 return LogicalSize(aWM, iSize, bSize);
454 Maybe<StyleCaptionSide> nsTableWrapperFrame::GetCaptionSide() const {
455 if (mCaptionFrames.IsEmpty()) {
456 return Nothing();
458 return Some(mCaptionFrames.FirstChild()->StyleTableBorder()->mCaptionSide);
461 StyleVerticalAlignKeyword nsTableWrapperFrame::GetCaptionVerticalAlign() const {
462 const auto& va = mCaptionFrames.FirstChild()->StyleDisplay()->mVerticalAlign;
463 return va.IsKeyword() ? va.AsKeyword() : StyleVerticalAlignKeyword::Top;
466 nscoord nsTableWrapperFrame::ComputeFinalBSize(
467 const LogicalSize& aInnerSize, const LogicalSize& aCaptionSize,
468 const LogicalMargin& aCaptionMargin, const WritingMode aWM) const {
469 // negative sizes can upset overflow-area code
470 return std::max(0, aInnerSize.BSize(aWM) +
471 std::max(0, aCaptionSize.BSize(aWM) +
472 aCaptionMargin.BStartEnd(aWM)));
475 void nsTableWrapperFrame::GetCaptionOrigin(StyleCaptionSide aCaptionSide,
476 const LogicalSize& aInnerSize,
477 const LogicalSize& aCaptionSize,
478 LogicalMargin& aCaptionMargin,
479 LogicalPoint& aOrigin,
480 WritingMode aWM) const {
481 aOrigin.I(aWM) = aOrigin.B(aWM) = 0;
482 if ((NS_UNCONSTRAINEDSIZE == aInnerSize.ISize(aWM)) ||
483 (NS_UNCONSTRAINEDSIZE == aInnerSize.BSize(aWM)) ||
484 (NS_UNCONSTRAINEDSIZE == aCaptionSize.ISize(aWM)) ||
485 (NS_UNCONSTRAINEDSIZE == aCaptionSize.BSize(aWM))) {
486 return;
488 if (mCaptionFrames.IsEmpty()) {
489 return;
492 NS_ASSERTION(NS_AUTOMARGIN != aCaptionMargin.IStart(aWM) &&
493 NS_AUTOMARGIN != aCaptionMargin.BStart(aWM) &&
494 NS_AUTOMARGIN != aCaptionMargin.BEnd(aWM),
495 "The computed caption margin is auto?");
497 aOrigin.I(aWM) = aCaptionMargin.IStart(aWM);
499 // block-dir computation
500 switch (aCaptionSide) {
501 case StyleCaptionSide::Bottom:
502 aOrigin.B(aWM) = aInnerSize.BSize(aWM) + aCaptionMargin.BStart(aWM);
503 break;
504 case StyleCaptionSide::Top:
505 aOrigin.B(aWM) = aCaptionMargin.BStart(aWM);
506 break;
510 void nsTableWrapperFrame::GetInnerOrigin(const MaybeCaptionSide& aCaptionSide,
511 const LogicalSize& aCaptionSize,
512 const LogicalMargin& aCaptionMargin,
513 const LogicalSize& aInnerSize,
514 LogicalPoint& aOrigin,
515 WritingMode aWM) const {
516 NS_ASSERTION(NS_AUTOMARGIN != aCaptionMargin.IStart(aWM) &&
517 NS_AUTOMARGIN != aCaptionMargin.IEnd(aWM),
518 "The computed caption margin is auto?");
520 aOrigin.I(aWM) = aOrigin.B(aWM) = 0;
521 if ((NS_UNCONSTRAINEDSIZE == aInnerSize.ISize(aWM)) ||
522 (NS_UNCONSTRAINEDSIZE == aInnerSize.BSize(aWM)) ||
523 (NS_UNCONSTRAINEDSIZE == aCaptionSize.ISize(aWM)) ||
524 (NS_UNCONSTRAINEDSIZE == aCaptionSize.BSize(aWM))) {
525 return;
528 // block-dir computation
529 if (aCaptionSide) {
530 switch (*aCaptionSide) {
531 case StyleCaptionSide::Bottom:
532 // Leave at zero.
533 break;
534 case StyleCaptionSide::Top:
535 aOrigin.B(aWM) =
536 aCaptionSize.BSize(aWM) + aCaptionMargin.BStartEnd(aWM);
537 break;
542 ComputeSizeFlags nsTableWrapperFrame::CreateComputeSizeFlagsForChild() const {
543 // Shrink-wrap child frames by default, except if we're a stretched grid item.
544 if (MOZ_UNLIKELY(IsGridItem())) {
545 auto* gridContainer = static_cast<nsGridContainerFrame*>(GetParent());
546 if (gridContainer->GridItemShouldStretch(this, LogicalAxis::Inline)) {
547 return {};
550 return {ComputeSizeFlag::ShrinkWrap};
553 void nsTableWrapperFrame::CreateReflowInputForInnerTable(
554 nsPresContext* aPresContext, nsTableFrame* aTableFrame,
555 const ReflowInput& aOuterRI, Maybe<ReflowInput>& aChildRI,
556 const nscoord aAvailISize, nscoord aBSizeOccupiedByCaption) const {
557 MOZ_ASSERT(InnerTableFrame() == aTableFrame);
559 const WritingMode wm = aTableFrame->GetWritingMode();
560 // If we have a caption occupied our content-box area, reduce the available
561 // block-size by the amount.
562 nscoord availBSize = aOuterRI.AvailableBSize();
563 if (availBSize != NS_UNCONSTRAINEDSIZE) {
564 availBSize = std::max(0, availBSize - aBSizeOccupiedByCaption);
566 const LogicalSize availSize(wm, aAvailISize, availBSize);
568 // For inner table frames, the containing block is the same as for the outer
569 // table frame.
570 Maybe<LogicalSize> cbSize = Some(aOuterRI.mContainingBlockSize);
572 // However, if we are a grid item, the CB size needs to subtract our margins
573 // and the area occupied by the caption.
575 // Note that inner table computed margins are always zero, they're inherited
576 // by the table wrapper, so we need to get our margin from aOuterRI.
577 if (IsGridItem()) {
578 const LogicalMargin margin = aOuterRI.ComputedLogicalMargin(wm);
579 cbSize->ISize(wm) = std::max(0, cbSize->ISize(wm) - margin.IStartEnd(wm));
580 if (cbSize->BSize(wm) != NS_UNCONSTRAINEDSIZE) {
581 cbSize->BSize(wm) = std::max(0, cbSize->BSize(wm) - margin.BStartEnd(wm) -
582 aBSizeOccupiedByCaption);
586 ComputeSizeFlags csFlags = CreateComputeSizeFlagsForChild();
587 if (!aTableFrame->IsBorderCollapse() &&
588 !aOuterRI.mStyleSizeOverrides.HasAnyOverrides()) {
589 // We are not border-collapsed and not given any size overrides. It's
590 // sufficient to call the standard ReflowInput constructor.
591 aChildRI.emplace(aPresContext, aOuterRI, aTableFrame, availSize, cbSize,
592 ReflowInput::InitFlags{}, StyleSizeOverrides{}, csFlags);
593 return;
596 Maybe<LogicalMargin> borderPadding;
597 Maybe<LogicalMargin> padding;
599 // Compute inner table frame's border & padding because we may need to
600 // reduce the size for inner table's size overrides. We won't waste time if
601 // they are not used, because we can use them directly by passing them into
602 // ReflowInput::Init().
603 Maybe<LogicalMargin> collapseBorder;
604 Maybe<LogicalMargin> collapsePadding;
605 aTableFrame->GetCollapsedBorderPadding(collapseBorder, collapsePadding);
606 SizeComputationInput input(aTableFrame, aOuterRI.mRenderingContext, wm,
607 cbSize->ISize(wm), collapseBorder,
608 collapsePadding);
609 borderPadding.emplace(input.ComputedLogicalBorderPadding(wm));
610 padding.emplace(input.ComputedLogicalPadding(wm));
613 StyleSizeOverrides innerOverrides = ComputeSizeOverridesForInnerTable(
614 aTableFrame, aOuterRI.mStyleSizeOverrides, borderPadding->Size(wm),
615 aBSizeOccupiedByCaption);
617 aChildRI.emplace(aPresContext, aOuterRI, aTableFrame, availSize, Nothing(),
618 ReflowInput::InitFlag::CallerWillInit, innerOverrides,
619 csFlags);
620 aChildRI->Init(aPresContext, cbSize, Some(*borderPadding - *padding),
621 padding);
624 void nsTableWrapperFrame::CreateReflowInputForCaption(
625 nsPresContext* aPresContext, nsIFrame* aCaptionFrame,
626 const ReflowInput& aOuterRI, Maybe<ReflowInput>& aChildRI,
627 const nscoord aAvailISize) const {
628 MOZ_ASSERT(aCaptionFrame == mCaptionFrames.FirstChild());
630 const WritingMode wm = aCaptionFrame->GetWritingMode();
632 // Use unconstrained available block-size so that the caption is always
633 // fully-complete.
634 const LogicalSize availSize(wm, aAvailISize, NS_UNCONSTRAINEDSIZE);
635 aChildRI.emplace(aPresContext, aOuterRI, aCaptionFrame, availSize);
637 // See if we need to reset mIsTopOfPage flag.
638 if (aChildRI->mFlags.mIsTopOfPage) {
639 if (auto captionSide = GetCaptionSide()) {
640 if (*captionSide == StyleCaptionSide::Bottom) {
641 aChildRI->mFlags.mIsTopOfPage = false;
647 void nsTableWrapperFrame::ReflowChild(nsPresContext* aPresContext,
648 nsIFrame* aChildFrame,
649 const ReflowInput& aChildRI,
650 ReflowOutput& aMetrics,
651 nsReflowStatus& aStatus) {
652 // Using zero as containerSize here because we want consistency between
653 // the GetLogicalPosition and ReflowChild calls, to avoid unnecessarily
654 // changing the frame's coordinates; but we don't yet know its final
655 // position anyway so the actual value is unimportant.
656 const nsSize zeroCSize;
657 WritingMode wm = aChildRI.GetWritingMode();
659 // Use the current position as a best guess for placement.
660 LogicalPoint childPt = aChildFrame->GetLogicalPosition(wm, zeroCSize);
661 ReflowChildFlags flags = ReflowChildFlags::NoMoveFrame;
663 // We don't want to delete our next-in-flow's child if it's an inner table
664 // frame, because table wrapper frames always assume that their inner table
665 // frames don't go away. If a table wrapper frame is removed because it is
666 // a next-in-flow of an already complete table wrapper frame, then it will
667 // take care of removing it's inner table frame.
668 if (aChildFrame == InnerTableFrame()) {
669 flags |= ReflowChildFlags::NoDeleteNextInFlowChild;
672 nsContainerFrame::ReflowChild(aChildFrame, aPresContext, aMetrics, aChildRI,
673 wm, childPt, zeroCSize, flags, aStatus);
676 void nsTableWrapperFrame::UpdateOverflowAreas(ReflowOutput& aMet) {
677 aMet.SetOverflowAreasToDesiredBounds();
678 ConsiderChildOverflow(aMet.mOverflowAreas, InnerTableFrame());
679 if (mCaptionFrames.NotEmpty()) {
680 ConsiderChildOverflow(aMet.mOverflowAreas, mCaptionFrames.FirstChild());
684 void nsTableWrapperFrame::Reflow(nsPresContext* aPresContext,
685 ReflowOutput& aDesiredSize,
686 const ReflowInput& aOuterRI,
687 nsReflowStatus& aStatus) {
688 MarkInReflow();
689 DO_GLOBAL_REFLOW_COUNT("nsTableWrapperFrame");
690 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
692 // Initialize out parameters
693 aDesiredSize.ClearSize();
695 if (!HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
696 // Set up our kids. They're already present, on an overflow list,
697 // or there are none so we'll create them now
698 MoveOverflowToChildList();
701 Maybe<ReflowInput> captionRI;
702 Maybe<ReflowInput> innerRI;
704 nsRect origCaptionRect;
705 nsRect origCaptionInkOverflow;
706 bool captionFirstReflow = false;
707 if (mCaptionFrames.NotEmpty()) {
708 origCaptionRect = mCaptionFrames.FirstChild()->GetRect();
709 origCaptionInkOverflow = mCaptionFrames.FirstChild()->InkOverflowRect();
710 captionFirstReflow =
711 mCaptionFrames.FirstChild()->HasAnyStateBits(NS_FRAME_FIRST_REFLOW);
714 // ComputeAutoSize has to match this logic.
715 WritingMode wm = aOuterRI.GetWritingMode();
716 Maybe<StyleCaptionSide> captionSide = GetCaptionSide();
717 const nscoord contentBoxISize = aOuterRI.ComputedSize(wm).ISize(wm);
719 MOZ_ASSERT(mCaptionFrames.NotEmpty() == captionSide.isSome());
721 // Compute the table's size first, and then prevent the caption from
722 // being larger in the inline dir unless it has to be.
724 // Note that CSS 2.1 (but not 2.0) says:
725 // The width of the anonymous box is the border-edge width of the
726 // table box inside it
727 // We don't actually make our anonymous box that isize (if we did,
728 // it would break 'auto' margins), but this effectively does that.
729 CreateReflowInputForInnerTable(aPresContext, InnerTableFrame(), aOuterRI,
730 innerRI, contentBoxISize);
732 // First reflow the caption.
733 ReflowOutput captionMet(wm);
734 LogicalSize captionSize(wm);
735 LogicalMargin captionMargin(wm);
736 if (captionSide) {
737 // It's good that CSS 2.1 says not to include margins, since we can't, since
738 // they already been converted so they exactly fill the available isize
739 // (ignoring the margin on one side if neither are auto). (We take
740 // advantage of that later when we call GetCaptionOrigin, though.)
741 nscoord innerBorderISize =
742 innerRI->ComputedSizeWithBorderPadding(wm).ISize(wm);
743 CreateReflowInputForCaption(aPresContext, mCaptionFrames.FirstChild(),
744 aOuterRI, captionRI, innerBorderISize);
746 // We intentionally don't merge capStatus into aStatus, since we currently
747 // can't handle caption continuations, but we probably should.
748 nsReflowStatus capStatus;
749 ReflowChild(aPresContext, mCaptionFrames.FirstChild(), *captionRI,
750 captionMet, capStatus);
751 captionSize = captionMet.Size(wm);
752 captionMargin = captionRI->ComputedLogicalMargin(wm);
753 nscoord bSizeOccupiedByCaption =
754 captionSize.BSize(wm) + captionMargin.BStartEnd(wm);
755 if (bSizeOccupiedByCaption) {
756 // Reset the inner table's ReflowInput to reduce various sizes because of
757 // the area occupied by caption.
758 innerRI.reset();
759 CreateReflowInputForInnerTable(aPresContext, InnerTableFrame(), aOuterRI,
760 innerRI, contentBoxISize,
761 bSizeOccupiedByCaption);
765 // Now we know how much to reduce the block-size for the inner table to
766 // account for captions. Reflow the inner table.
767 ReflowOutput innerMet(innerRI->GetWritingMode());
768 ReflowChild(aPresContext, InnerTableFrame(), *innerRI, innerMet, aStatus);
769 LogicalSize innerSize(wm, innerMet.ISize(wm), innerMet.BSize(wm));
771 // Now that we've reflowed both we can place them.
772 // Compute the desiredSize so that we can use it as the containerSize
773 // for the FinishReflowChild calls below.
774 LogicalSize desiredSize(wm);
776 // We have zero border and padding, so content-box inline-size is our desired
777 // border-box inline-size.
778 desiredSize.ISize(wm) = contentBoxISize;
779 desiredSize.BSize(wm) =
780 ComputeFinalBSize(innerSize, captionSize, captionMargin, wm);
782 aDesiredSize.SetSize(wm, desiredSize);
783 nsSize containerSize = aDesiredSize.PhysicalSize();
785 MOZ_ASSERT(mCaptionFrames.NotEmpty() == captionSide.isSome());
786 if (mCaptionFrames.NotEmpty()) {
787 LogicalPoint captionOrigin(wm);
788 GetCaptionOrigin(*captionSide, innerSize, captionSize, captionMargin,
789 captionOrigin, wm);
790 FinishReflowChild(mCaptionFrames.FirstChild(), aPresContext, captionMet,
791 captionRI.ptr(), wm, captionOrigin, containerSize,
792 ReflowChildFlags::ApplyRelativePositioning);
793 captionRI.reset();
795 // XXX If the bsize is constrained then we need to check whether
796 // everything still fits...
798 LogicalPoint innerOrigin(wm);
799 GetInnerOrigin(captionSide, captionSize, captionMargin, innerSize,
800 innerOrigin, wm);
801 // NOTE: Relative positioning on the table applies to the whole table wrapper.
802 FinishReflowChild(InnerTableFrame(), aPresContext, innerMet, innerRI.ptr(),
803 wm, innerOrigin, containerSize, ReflowChildFlags::Default);
804 innerRI.reset();
806 if (mCaptionFrames.NotEmpty()) {
807 nsTableFrame::InvalidateTableFrame(mCaptionFrames.FirstChild(),
808 origCaptionRect, origCaptionInkOverflow,
809 captionFirstReflow);
812 UpdateOverflowAreas(aDesiredSize);
814 if (GetPrevInFlow()) {
815 ReflowOverflowContainerChildren(aPresContext, aOuterRI,
816 aDesiredSize.mOverflowAreas,
817 ReflowChildFlags::Default, aStatus);
820 FinishReflowWithAbsoluteFrames(aPresContext, aDesiredSize, aOuterRI, aStatus);
823 /* ----- global methods ----- */
825 nsIContent* nsTableWrapperFrame::GetCellAt(uint32_t aRowIdx,
826 uint32_t aColIdx) const {
827 nsTableCellMap* cellMap = InnerTableFrame()->GetCellMap();
828 if (!cellMap) {
829 return nullptr;
832 nsTableCellFrame* cell = cellMap->GetCellInfoAt(aRowIdx, aColIdx);
833 if (!cell) {
834 return nullptr;
837 return cell->GetContent();
840 nsTableWrapperFrame* NS_NewTableWrapperFrame(PresShell* aPresShell,
841 ComputedStyle* aStyle) {
842 return new (aPresShell)
843 nsTableWrapperFrame(aStyle, aPresShell->GetPresContext());
846 NS_IMPL_FRAMEARENA_HELPERS(nsTableWrapperFrame)
848 #ifdef DEBUG_FRAME_DUMP
849 nsresult nsTableWrapperFrame::GetFrameName(nsAString& aResult) const {
850 return MakeFrameName(u"TableWrapper"_ns, aResult);
852 #endif