Bug 1869043 allow a device to be specified with MediaTrackGraph::NotifyWhenDeviceStar...
[gecko.git] / layout / tables / nsTableWrapperFrame.cpp
bloba3e957c4fd97554d40350be4a4a0a49b716d9197
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 DISPLAY_MIN_INLINE_SIZE(this, iSize);
246 if (mCaptionFrames.NotEmpty()) {
247 nscoord capISize = nsLayoutUtils::IntrinsicForContainer(
248 aRenderingContext, mCaptionFrames.FirstChild(),
249 IntrinsicISizeType::MinISize);
250 if (capISize > iSize) {
251 iSize = capISize;
254 return iSize;
257 /* virtual */
258 nscoord nsTableWrapperFrame::GetPrefISize(gfxContext* aRenderingContext) {
259 nscoord maxISize;
260 DISPLAY_PREF_INLINE_SIZE(this, maxISize);
262 maxISize = nsLayoutUtils::IntrinsicForContainer(
263 aRenderingContext, InnerTableFrame(), IntrinsicISizeType::PrefISize);
264 if (mCaptionFrames.NotEmpty()) {
265 // Don't let the caption's pref isize expand the table's pref isize.
266 const nscoord capMinISize = nsLayoutUtils::IntrinsicForContainer(
267 aRenderingContext, mCaptionFrames.FirstChild(),
268 IntrinsicISizeType::MinISize);
269 maxISize = std::max(maxISize, capMinISize);
271 return maxISize;
274 LogicalSize nsTableWrapperFrame::InnerTableShrinkWrapSize(
275 gfxContext* aRenderingContext, nsTableFrame* aTableFrame, WritingMode aWM,
276 const LogicalSize& aCBSize, nscoord aAvailableISize,
277 const StyleSizeOverrides& aSizeOverrides, ComputeSizeFlags aFlags) const {
278 MOZ_ASSERT(InnerTableFrame() == aTableFrame);
280 AutoMaybeDisableFontInflation an(aTableFrame);
282 Maybe<LogicalMargin> collapseBorder;
283 Maybe<LogicalMargin> collapsePadding;
284 aTableFrame->GetCollapsedBorderPadding(collapseBorder, collapsePadding);
286 SizeComputationInput input(aTableFrame, aRenderingContext, aWM,
287 aCBSize.ISize(aWM), collapseBorder,
288 collapsePadding);
289 LogicalSize marginSize(aWM); // Inner table doesn't have any margin
290 LogicalSize bpSize = input.ComputedLogicalBorderPadding(aWM).Size(aWM);
292 // Note that we pass an empty caption-area here (rather than the caption's
293 // actual size). This is fine because:
295 // 1) nsTableWrapperFrame::ComputeSize() only uses the size returned by this
296 // method (indirectly via calling nsTableWrapperFrame::ComputeAutoSize())
297 // if it get a aSizeOverrides arg containing any size overrides with
298 // mApplyOverridesVerbatim=true. The aSizeOverrides arg is passed to this
299 // method without any modifications.
301 // 2) With 1), that means the aSizeOverrides passing into this method should
302 // be applied to the inner table directly, so we don't need to subtract
303 // caption-area when preparing innerOverrides for
304 // nsTableFrame::ComputeSize().
305 StyleSizeOverrides innerOverrides = ComputeSizeOverridesForInnerTable(
306 aTableFrame, aSizeOverrides, bpSize, /* aBSizeOccupiedByCaption = */ 0);
307 auto size =
308 aTableFrame
309 ->ComputeSize(aRenderingContext, aWM, aCBSize, aAvailableISize,
310 marginSize, bpSize, innerOverrides, aFlags)
311 .mLogicalSize;
312 size.ISize(aWM) += bpSize.ISize(aWM);
313 if (size.BSize(aWM) != NS_UNCONSTRAINEDSIZE) {
314 size.BSize(aWM) += bpSize.BSize(aWM);
316 return size;
319 LogicalSize nsTableWrapperFrame::CaptionShrinkWrapSize(
320 gfxContext* aRenderingContext, nsIFrame* aCaptionFrame, WritingMode aWM,
321 const LogicalSize& aCBSize, nscoord aAvailableISize,
322 ComputeSizeFlags aFlags) const {
323 MOZ_ASSERT(aCaptionFrame == mCaptionFrames.FirstChild());
325 AutoMaybeDisableFontInflation an(aCaptionFrame);
327 SizeComputationInput input(aCaptionFrame, aRenderingContext, aWM,
328 aCBSize.ISize(aWM));
329 LogicalSize marginSize = input.ComputedLogicalMargin(aWM).Size(aWM);
330 LogicalSize bpSize = input.ComputedLogicalBorderPadding(aWM).Size(aWM);
332 auto size = aCaptionFrame
333 ->ComputeSize(aRenderingContext, aWM, aCBSize,
334 aAvailableISize, marginSize, bpSize, {}, aFlags)
335 .mLogicalSize;
336 size.ISize(aWM) += (marginSize.ISize(aWM) + bpSize.ISize(aWM));
337 if (size.BSize(aWM) != NS_UNCONSTRAINEDSIZE) {
338 size.BSize(aWM) += (marginSize.BSize(aWM) + bpSize.BSize(aWM));
340 return size;
343 StyleSize nsTableWrapperFrame::ReduceStyleSizeBy(
344 const StyleSize& aStyleSize, const nscoord aAmountToReduce) const {
345 MOZ_ASSERT(aStyleSize.ConvertsToLength(), "Only handles 'Length' StyleSize!");
346 const nscoord size = std::max(0, aStyleSize.ToLength() - aAmountToReduce);
347 return StyleSize::LengthPercentage(LengthPercentage::FromAppUnits(size));
350 StyleSizeOverrides nsTableWrapperFrame::ComputeSizeOverridesForInnerTable(
351 const nsTableFrame* aTableFrame,
352 const StyleSizeOverrides& aWrapperSizeOverrides,
353 const LogicalSize& aBorderPadding, nscoord aBSizeOccupiedByCaption) const {
354 if (aWrapperSizeOverrides.mApplyOverridesVerbatim ||
355 !aWrapperSizeOverrides.HasAnyLengthOverrides()) {
356 // We are asked to apply the size overrides directly to the inner table, or
357 // there's no 'Length' size overrides. No need to tweak the size overrides.
358 return aWrapperSizeOverrides;
361 const auto wm = aTableFrame->GetWritingMode();
362 LogicalSize areaOccupied(wm, 0, aBSizeOccupiedByCaption);
363 if (aTableFrame->StylePosition()->mBoxSizing == StyleBoxSizing::Content) {
364 // If the inner table frame has 'box-sizing: content', enlarge the occupied
365 // area by adding border & padding because they should also be subtracted
366 // from the size overrides.
367 areaOccupied += aBorderPadding;
370 StyleSizeOverrides innerSizeOverrides;
371 const auto& wrapperISize = aWrapperSizeOverrides.mStyleISize;
372 if (wrapperISize) {
373 MOZ_ASSERT(!wrapperISize->HasPercent(),
374 "Table doesn't support size overrides containing percentages!");
375 innerSizeOverrides.mStyleISize.emplace(
376 wrapperISize->ConvertsToLength()
377 ? ReduceStyleSizeBy(*wrapperISize, areaOccupied.ISize(wm))
378 : *wrapperISize);
381 const auto& wrapperBSize = aWrapperSizeOverrides.mStyleBSize;
382 if (wrapperBSize) {
383 MOZ_ASSERT(!wrapperBSize->HasPercent(),
384 "Table doesn't support size overrides containing percentages!");
385 innerSizeOverrides.mStyleBSize.emplace(
386 wrapperBSize->ConvertsToLength()
387 ? ReduceStyleSizeBy(*wrapperBSize, areaOccupied.BSize(wm))
388 : *wrapperBSize);
391 return innerSizeOverrides;
394 /* virtual */
395 nsIFrame::SizeComputationResult nsTableWrapperFrame::ComputeSize(
396 gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
397 nscoord aAvailableISize, const LogicalSize& aMargin,
398 const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
399 ComputeSizeFlags aFlags) {
400 auto result = nsContainerFrame::ComputeSize(
401 aRenderingContext, aWM, aCBSize, aAvailableISize, aMargin, aBorderPadding,
402 aSizeOverrides, aFlags);
404 if (aSizeOverrides.mApplyOverridesVerbatim &&
405 aSizeOverrides.HasAnyOverrides()) {
406 // We are asked to apply the size overrides directly to the inner table, but
407 // we still want ourselves to remain 'auto'-sized and shrink-wrapping our
408 // children's sizes. (Table wrapper frames always have 'auto' inline-size
409 // and block-size, since we don't inherit those properties from inner table,
410 // and authors can't target them with styling.)
411 auto size =
412 ComputeAutoSize(aRenderingContext, aWM, aCBSize, aAvailableISize,
413 aMargin, aBorderPadding, aSizeOverrides, aFlags);
414 result.mLogicalSize = size;
417 return result;
420 /* virtual */
421 LogicalSize nsTableWrapperFrame::ComputeAutoSize(
422 gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
423 nscoord aAvailableISize, const LogicalSize& aMargin,
424 const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
425 ComputeSizeFlags aFlags) {
426 nscoord kidAvailableISize = aAvailableISize - aMargin.ISize(aWM);
427 NS_ASSERTION(aBorderPadding.IsAllZero(),
428 "Table wrapper frames cannot have borders or paddings");
430 // When we're shrink-wrapping, our auto size needs to wrap around the
431 // actual size of the table, which (if it is specified as a percent)
432 // could be something that is not reflected in our GetMinISize and
433 // GetPrefISize. See bug 349457 for an example.
434 const ComputeSizeFlags flags = CreateComputeSizeFlagsForChild();
436 // Match the logic in Reflow() that sets aside space for the caption.
437 Maybe<StyleCaptionSide> captionSide = GetCaptionSide();
439 const LogicalSize innerTableSize = InnerTableShrinkWrapSize(
440 aRenderingContext, InnerTableFrame(), aWM, aCBSize, kidAvailableISize,
441 aSizeOverrides, flags);
442 if (!captionSide) {
443 return innerTableSize;
445 const LogicalSize captionSize =
446 CaptionShrinkWrapSize(aRenderingContext, mCaptionFrames.FirstChild(), aWM,
447 aCBSize, innerTableSize.ISize(aWM), flags);
448 const nscoord iSize =
449 std::max(innerTableSize.ISize(aWM), captionSize.ISize(aWM));
450 nscoord bSize = NS_UNCONSTRAINEDSIZE;
451 if (innerTableSize.BSize(aWM) != NS_UNCONSTRAINEDSIZE &&
452 captionSize.BSize(aWM) != NS_UNCONSTRAINEDSIZE) {
453 bSize = innerTableSize.BSize(aWM) + captionSize.BSize(aWM);
455 return LogicalSize(aWM, iSize, bSize);
458 Maybe<StyleCaptionSide> nsTableWrapperFrame::GetCaptionSide() const {
459 if (mCaptionFrames.IsEmpty()) {
460 return Nothing();
462 return Some(mCaptionFrames.FirstChild()->StyleTableBorder()->mCaptionSide);
465 StyleVerticalAlignKeyword nsTableWrapperFrame::GetCaptionVerticalAlign() const {
466 const auto& va = mCaptionFrames.FirstChild()->StyleDisplay()->mVerticalAlign;
467 return va.IsKeyword() ? va.AsKeyword() : StyleVerticalAlignKeyword::Top;
470 nscoord nsTableWrapperFrame::ComputeFinalBSize(
471 const LogicalSize& aInnerSize, const LogicalSize& aCaptionSize,
472 const LogicalMargin& aCaptionMargin, const WritingMode aWM) const {
473 // negative sizes can upset overflow-area code
474 return std::max(0, aInnerSize.BSize(aWM) +
475 std::max(0, aCaptionSize.BSize(aWM) +
476 aCaptionMargin.BStartEnd(aWM)));
479 void nsTableWrapperFrame::GetCaptionOrigin(StyleCaptionSide aCaptionSide,
480 const LogicalSize& aInnerSize,
481 const LogicalSize& aCaptionSize,
482 LogicalMargin& aCaptionMargin,
483 LogicalPoint& aOrigin,
484 WritingMode aWM) const {
485 aOrigin.I(aWM) = aOrigin.B(aWM) = 0;
486 if ((NS_UNCONSTRAINEDSIZE == aInnerSize.ISize(aWM)) ||
487 (NS_UNCONSTRAINEDSIZE == aInnerSize.BSize(aWM)) ||
488 (NS_UNCONSTRAINEDSIZE == aCaptionSize.ISize(aWM)) ||
489 (NS_UNCONSTRAINEDSIZE == aCaptionSize.BSize(aWM))) {
490 return;
492 if (mCaptionFrames.IsEmpty()) {
493 return;
496 NS_ASSERTION(NS_AUTOMARGIN != aCaptionMargin.IStart(aWM) &&
497 NS_AUTOMARGIN != aCaptionMargin.BStart(aWM) &&
498 NS_AUTOMARGIN != aCaptionMargin.BEnd(aWM),
499 "The computed caption margin is auto?");
501 aOrigin.I(aWM) = aCaptionMargin.IStart(aWM);
503 // block-dir computation
504 switch (aCaptionSide) {
505 case StyleCaptionSide::Bottom:
506 aOrigin.B(aWM) = aInnerSize.BSize(aWM) + aCaptionMargin.BStart(aWM);
507 break;
508 case StyleCaptionSide::Top:
509 aOrigin.B(aWM) = aCaptionMargin.BStart(aWM);
510 break;
514 void nsTableWrapperFrame::GetInnerOrigin(const MaybeCaptionSide& aCaptionSide,
515 const LogicalSize& aCaptionSize,
516 const LogicalMargin& aCaptionMargin,
517 const LogicalSize& aInnerSize,
518 LogicalPoint& aOrigin,
519 WritingMode aWM) const {
520 NS_ASSERTION(NS_AUTOMARGIN != aCaptionMargin.IStart(aWM) &&
521 NS_AUTOMARGIN != aCaptionMargin.IEnd(aWM),
522 "The computed caption margin is auto?");
524 aOrigin.I(aWM) = aOrigin.B(aWM) = 0;
525 if ((NS_UNCONSTRAINEDSIZE == aInnerSize.ISize(aWM)) ||
526 (NS_UNCONSTRAINEDSIZE == aInnerSize.BSize(aWM)) ||
527 (NS_UNCONSTRAINEDSIZE == aCaptionSize.ISize(aWM)) ||
528 (NS_UNCONSTRAINEDSIZE == aCaptionSize.BSize(aWM))) {
529 return;
532 // block-dir computation
533 if (aCaptionSide) {
534 switch (*aCaptionSide) {
535 case StyleCaptionSide::Bottom:
536 // Leave at zero.
537 break;
538 case StyleCaptionSide::Top:
539 aOrigin.B(aWM) =
540 aCaptionSize.BSize(aWM) + aCaptionMargin.BStartEnd(aWM);
541 break;
546 ComputeSizeFlags nsTableWrapperFrame::CreateComputeSizeFlagsForChild() const {
547 // Shrink-wrap child frames by default, except if we're a stretched grid item.
548 if (MOZ_UNLIKELY(IsGridItem())) {
549 auto* gridContainer = static_cast<nsGridContainerFrame*>(GetParent());
550 if (gridContainer->GridItemShouldStretch(this, eLogicalAxisInline)) {
551 return {};
554 return {ComputeSizeFlag::ShrinkWrap};
557 void nsTableWrapperFrame::CreateReflowInputForInnerTable(
558 nsPresContext* aPresContext, nsTableFrame* aTableFrame,
559 const ReflowInput& aOuterRI, Maybe<ReflowInput>& aChildRI,
560 const nscoord aAvailISize, nscoord aBSizeOccupiedByCaption) const {
561 MOZ_ASSERT(InnerTableFrame() == aTableFrame);
563 const WritingMode wm = aTableFrame->GetWritingMode();
564 // If we have a caption occupied our content-box area, reduce the available
565 // block-size by the amount.
566 nscoord availBSize = aOuterRI.AvailableBSize();
567 if (availBSize != NS_UNCONSTRAINEDSIZE) {
568 availBSize = std::max(0, availBSize - aBSizeOccupiedByCaption);
570 const LogicalSize availSize(wm, aAvailISize, availBSize);
572 // For inner table frames, the containing block is the same as for the outer
573 // table frame.
574 Maybe<LogicalSize> cbSize = Some(aOuterRI.mContainingBlockSize);
576 // However, if we are a grid item, the CB size needs to subtract our margins
577 // and the area occupied by the caption.
579 // Note that inner table computed margins are always zero, they're inherited
580 // by the table wrapper, so we need to get our margin from aOuterRI.
581 if (IsGridItem()) {
582 const LogicalMargin margin = aOuterRI.ComputedLogicalMargin(wm);
583 cbSize->ISize(wm) = std::max(0, cbSize->ISize(wm) - margin.IStartEnd(wm));
584 if (cbSize->BSize(wm) != NS_UNCONSTRAINEDSIZE) {
585 cbSize->BSize(wm) = std::max(0, cbSize->BSize(wm) - margin.BStartEnd(wm) -
586 aBSizeOccupiedByCaption);
590 ComputeSizeFlags csFlags = CreateComputeSizeFlagsForChild();
591 if (!aTableFrame->IsBorderCollapse() &&
592 !aOuterRI.mStyleSizeOverrides.HasAnyOverrides()) {
593 // We are not border-collapsed and not given any size overrides. It's
594 // sufficient to call the standard ReflowInput constructor.
595 aChildRI.emplace(aPresContext, aOuterRI, aTableFrame, availSize, cbSize,
596 ReflowInput::InitFlags{}, StyleSizeOverrides{}, csFlags);
597 return;
600 Maybe<LogicalMargin> borderPadding;
601 Maybe<LogicalMargin> padding;
603 // Compute inner table frame's border & padding because we may need to
604 // reduce the size for inner table's size overrides. We won't waste time if
605 // they are not used, because we can use them directly by passing them into
606 // ReflowInput::Init().
607 Maybe<LogicalMargin> collapseBorder;
608 Maybe<LogicalMargin> collapsePadding;
609 aTableFrame->GetCollapsedBorderPadding(collapseBorder, collapsePadding);
610 SizeComputationInput input(aTableFrame, aOuterRI.mRenderingContext, wm,
611 cbSize->ISize(wm), collapseBorder,
612 collapsePadding);
613 borderPadding.emplace(input.ComputedLogicalBorderPadding(wm));
614 padding.emplace(input.ComputedLogicalPadding(wm));
617 StyleSizeOverrides innerOverrides = ComputeSizeOverridesForInnerTable(
618 aTableFrame, aOuterRI.mStyleSizeOverrides, borderPadding->Size(wm),
619 aBSizeOccupiedByCaption);
621 aChildRI.emplace(aPresContext, aOuterRI, aTableFrame, availSize, Nothing(),
622 ReflowInput::InitFlag::CallerWillInit, innerOverrides,
623 csFlags);
624 aChildRI->Init(aPresContext, cbSize, Some(*borderPadding - *padding),
625 padding);
628 void nsTableWrapperFrame::CreateReflowInputForCaption(
629 nsPresContext* aPresContext, nsIFrame* aCaptionFrame,
630 const ReflowInput& aOuterRI, Maybe<ReflowInput>& aChildRI,
631 const nscoord aAvailISize) const {
632 MOZ_ASSERT(aCaptionFrame == mCaptionFrames.FirstChild());
634 const WritingMode wm = aCaptionFrame->GetWritingMode();
636 // Use unconstrained available block-size so that the caption is always
637 // fully-complete.
638 const LogicalSize availSize(wm, aAvailISize, NS_UNCONSTRAINEDSIZE);
639 aChildRI.emplace(aPresContext, aOuterRI, aCaptionFrame, availSize);
641 // See if we need to reset mIsTopOfPage flag.
642 if (aChildRI->mFlags.mIsTopOfPage) {
643 if (auto captionSide = GetCaptionSide()) {
644 if (*captionSide == StyleCaptionSide::Bottom) {
645 aChildRI->mFlags.mIsTopOfPage = false;
651 void nsTableWrapperFrame::ReflowChild(nsPresContext* aPresContext,
652 nsIFrame* aChildFrame,
653 const ReflowInput& aChildRI,
654 ReflowOutput& aMetrics,
655 nsReflowStatus& aStatus) {
656 // Using zero as containerSize here because we want consistency between
657 // the GetLogicalPosition and ReflowChild calls, to avoid unnecessarily
658 // changing the frame's coordinates; but we don't yet know its final
659 // position anyway so the actual value is unimportant.
660 const nsSize zeroCSize;
661 WritingMode wm = aChildRI.GetWritingMode();
663 // Use the current position as a best guess for placement.
664 LogicalPoint childPt = aChildFrame->GetLogicalPosition(wm, zeroCSize);
665 ReflowChildFlags flags = ReflowChildFlags::NoMoveFrame;
667 // We don't want to delete our next-in-flow's child if it's an inner table
668 // frame, because table wrapper frames always assume that their inner table
669 // frames don't go away. If a table wrapper frame is removed because it is
670 // a next-in-flow of an already complete table wrapper frame, then it will
671 // take care of removing it's inner table frame.
672 if (aChildFrame == InnerTableFrame()) {
673 flags |= ReflowChildFlags::NoDeleteNextInFlowChild;
676 nsContainerFrame::ReflowChild(aChildFrame, aPresContext, aMetrics, aChildRI,
677 wm, childPt, zeroCSize, flags, aStatus);
680 void nsTableWrapperFrame::UpdateOverflowAreas(ReflowOutput& aMet) {
681 aMet.SetOverflowAreasToDesiredBounds();
682 ConsiderChildOverflow(aMet.mOverflowAreas, InnerTableFrame());
683 if (mCaptionFrames.NotEmpty()) {
684 ConsiderChildOverflow(aMet.mOverflowAreas, mCaptionFrames.FirstChild());
688 void nsTableWrapperFrame::Reflow(nsPresContext* aPresContext,
689 ReflowOutput& aDesiredSize,
690 const ReflowInput& aOuterRI,
691 nsReflowStatus& aStatus) {
692 MarkInReflow();
693 DO_GLOBAL_REFLOW_COUNT("nsTableWrapperFrame");
694 DISPLAY_REFLOW(aPresContext, this, aOuterRI, aDesiredSize, aStatus);
695 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
697 // Initialize out parameters
698 aDesiredSize.ClearSize();
700 if (!HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) {
701 // Set up our kids. They're already present, on an overflow list,
702 // or there are none so we'll create them now
703 MoveOverflowToChildList();
706 Maybe<ReflowInput> captionRI;
707 Maybe<ReflowInput> innerRI;
709 nsRect origCaptionRect;
710 nsRect origCaptionInkOverflow;
711 bool captionFirstReflow = false;
712 if (mCaptionFrames.NotEmpty()) {
713 origCaptionRect = mCaptionFrames.FirstChild()->GetRect();
714 origCaptionInkOverflow = mCaptionFrames.FirstChild()->InkOverflowRect();
715 captionFirstReflow =
716 mCaptionFrames.FirstChild()->HasAnyStateBits(NS_FRAME_FIRST_REFLOW);
719 // ComputeAutoSize has to match this logic.
720 WritingMode wm = aOuterRI.GetWritingMode();
721 Maybe<StyleCaptionSide> captionSide = GetCaptionSide();
722 const nscoord contentBoxISize = aOuterRI.ComputedSize(wm).ISize(wm);
724 MOZ_ASSERT(mCaptionFrames.NotEmpty() == captionSide.isSome());
726 // Compute the table's size first, and then prevent the caption from
727 // being larger in the inline dir unless it has to be.
729 // Note that CSS 2.1 (but not 2.0) says:
730 // The width of the anonymous box is the border-edge width of the
731 // table box inside it
732 // We don't actually make our anonymous box that isize (if we did,
733 // it would break 'auto' margins), but this effectively does that.
734 CreateReflowInputForInnerTable(aPresContext, InnerTableFrame(), aOuterRI,
735 innerRI, contentBoxISize);
737 // First reflow the caption.
738 ReflowOutput captionMet(wm);
739 LogicalSize captionSize(wm);
740 LogicalMargin captionMargin(wm);
741 if (captionSide) {
742 // It's good that CSS 2.1 says not to include margins, since we can't, since
743 // they already been converted so they exactly fill the available isize
744 // (ignoring the margin on one side if neither are auto). (We take
745 // advantage of that later when we call GetCaptionOrigin, though.)
746 nscoord innerBorderISize =
747 innerRI->ComputedSizeWithBorderPadding(wm).ISize(wm);
748 CreateReflowInputForCaption(aPresContext, mCaptionFrames.FirstChild(),
749 aOuterRI, captionRI, innerBorderISize);
751 // We intentionally don't merge capStatus into aStatus, since we currently
752 // can't handle caption continuations, but we probably should.
753 nsReflowStatus capStatus;
754 ReflowChild(aPresContext, mCaptionFrames.FirstChild(), *captionRI,
755 captionMet, capStatus);
756 captionSize = captionMet.Size(wm);
757 captionMargin = captionRI->ComputedLogicalMargin(wm);
758 nscoord bSizeOccupiedByCaption =
759 captionSize.BSize(wm) + captionMargin.BStartEnd(wm);
760 if (bSizeOccupiedByCaption) {
761 // Reset the inner table's ReflowInput to reduce various sizes because of
762 // the area occupied by caption.
763 innerRI.reset();
764 CreateReflowInputForInnerTable(aPresContext, InnerTableFrame(), aOuterRI,
765 innerRI, contentBoxISize,
766 bSizeOccupiedByCaption);
770 // Now we know how much to reduce the block-size for the inner table to
771 // account for captions. Reflow the inner table.
772 ReflowOutput innerMet(innerRI->GetWritingMode());
773 ReflowChild(aPresContext, InnerTableFrame(), *innerRI, innerMet, aStatus);
774 LogicalSize innerSize(wm, innerMet.ISize(wm), innerMet.BSize(wm));
776 // Now that we've reflowed both we can place them.
777 // Compute the desiredSize so that we can use it as the containerSize
778 // for the FinishReflowChild calls below.
779 LogicalSize desiredSize(wm);
781 // We have zero border and padding, so content-box inline-size is our desired
782 // border-box inline-size.
783 desiredSize.ISize(wm) = contentBoxISize;
784 desiredSize.BSize(wm) =
785 ComputeFinalBSize(innerSize, captionSize, captionMargin, wm);
787 aDesiredSize.SetSize(wm, desiredSize);
788 nsSize containerSize = aDesiredSize.PhysicalSize();
790 MOZ_ASSERT(mCaptionFrames.NotEmpty() == captionSide.isSome());
791 if (mCaptionFrames.NotEmpty()) {
792 LogicalPoint captionOrigin(wm);
793 GetCaptionOrigin(*captionSide, innerSize, captionSize, captionMargin,
794 captionOrigin, wm);
795 FinishReflowChild(mCaptionFrames.FirstChild(), aPresContext, captionMet,
796 captionRI.ptr(), wm, captionOrigin, containerSize,
797 ReflowChildFlags::ApplyRelativePositioning);
798 captionRI.reset();
800 // XXX If the bsize is constrained then we need to check whether
801 // everything still fits...
803 LogicalPoint innerOrigin(wm);
804 GetInnerOrigin(captionSide, captionSize, captionMargin, innerSize,
805 innerOrigin, wm);
806 // NOTE: Relative positioning on the table applies to the whole table wrapper.
807 FinishReflowChild(InnerTableFrame(), aPresContext, innerMet, innerRI.ptr(),
808 wm, innerOrigin, containerSize, ReflowChildFlags::Default);
809 innerRI.reset();
811 if (mCaptionFrames.NotEmpty()) {
812 nsTableFrame::InvalidateTableFrame(mCaptionFrames.FirstChild(),
813 origCaptionRect, origCaptionInkOverflow,
814 captionFirstReflow);
817 UpdateOverflowAreas(aDesiredSize);
819 if (GetPrevInFlow()) {
820 ReflowOverflowContainerChildren(aPresContext, aOuterRI,
821 aDesiredSize.mOverflowAreas,
822 ReflowChildFlags::Default, aStatus);
825 FinishReflowWithAbsoluteFrames(aPresContext, aDesiredSize, aOuterRI, aStatus);
828 /* ----- global methods ----- */
830 nsIContent* nsTableWrapperFrame::GetCellAt(uint32_t aRowIdx,
831 uint32_t aColIdx) const {
832 nsTableCellMap* cellMap = InnerTableFrame()->GetCellMap();
833 if (!cellMap) {
834 return nullptr;
837 nsTableCellFrame* cell = cellMap->GetCellInfoAt(aRowIdx, aColIdx);
838 if (!cell) {
839 return nullptr;
842 return cell->GetContent();
845 nsTableWrapperFrame* NS_NewTableWrapperFrame(PresShell* aPresShell,
846 ComputedStyle* aStyle) {
847 return new (aPresShell)
848 nsTableWrapperFrame(aStyle, aPresShell->GetPresContext());
851 NS_IMPL_FRAMEARENA_HELPERS(nsTableWrapperFrame)
853 #ifdef DEBUG_FRAME_DUMP
854 nsresult nsTableWrapperFrame::GetFrameName(nsAString& aResult) const {
855 return MakeFrameName(u"TableWrapper"_ns, aResult);
857 #endif