summaryrefslogtreecommitdiffstats
path: root/layout/generic/ColumnSetWrapperFrame.cpp
blob: 7717f14df8ec3085353c67f06e0099ed2e72292e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "ColumnSetWrapperFrame.h"

#include "mozilla/ColumnUtils.h"
#include "mozilla/PresShell.h"
#include "nsContentUtils.h"
#include "nsIFrame.h"
#include "nsIFrameInlines.h"

using namespace mozilla;

nsBlockFrame* NS_NewColumnSetWrapperFrame(PresShell* aPresShell,
                                          ComputedStyle* aStyle,
                                          nsFrameState aStateFlags) {
  ColumnSetWrapperFrame* frame = new (aPresShell)
      ColumnSetWrapperFrame(aStyle, aPresShell->GetPresContext());
  frame->AddStateBits(aStateFlags);
  return frame;
}

NS_IMPL_FRAMEARENA_HELPERS(ColumnSetWrapperFrame)

NS_QUERYFRAME_HEAD(ColumnSetWrapperFrame)
  NS_QUERYFRAME_ENTRY(ColumnSetWrapperFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsBlockFrame)

ColumnSetWrapperFrame::ColumnSetWrapperFrame(ComputedStyle* aStyle,
                                             nsPresContext* aPresContext)
    : nsBlockFrame(aStyle, aPresContext, kClassID) {}

void ColumnSetWrapperFrame::Init(nsIContent* aContent,
                                 nsContainerFrame* aParent,
                                 nsIFrame* aPrevInFlow) {
  nsBlockFrame::Init(aContent, aParent, aPrevInFlow);

  // ColumnSetWrapperFrame doesn't need to call ResolveBidi().
  RemoveStateBits(NS_BLOCK_NEEDS_BIDI_RESOLUTION);
}

nsContainerFrame* ColumnSetWrapperFrame::GetContentInsertionFrame() {
  nsIFrame* columnSet = PrincipalChildList().OnlyChild();
  if (columnSet) {
    // We have only one child, which means we don't have any column-span
    // descendants. Thus we can safely return our only ColumnSet child's
    // insertion frame as ours.
    MOZ_ASSERT(columnSet->IsColumnSetFrame());
    return columnSet->GetContentInsertionFrame();
  }

  // We have column-span descendants. Return ourselves as the insertion
  // frame to let nsCSSFrameConstructor::WipeContainingBlock() figure out
  // what to do.
  return this;
}

void ColumnSetWrapperFrame::AppendDirectlyOwnedAnonBoxes(
    nsTArray<OwnedAnonBox>& aResult) {
  MOZ_ASSERT(!GetPrevContinuation(),
             "Who set NS_FRAME_OWNS_ANON_BOXES on our continuations?");

  // It's sufficient to append the first ColumnSet child, which is the first
  // continuation of all the other ColumnSets.
  //
  // We don't need to append -moz-column-span-wrapper children because
  // they're non-inheriting anon boxes, and they cannot have any directly
  // owned anon boxes nor generate any native anonymous content themselves.
  // Thus, no need to restyle them. AssertColumnSpanWrapperSubtreeIsSane()
  // asserts all the conditions above which allow us to skip appending
  // -moz-column-span-wrappers.
  auto FindFirstChildInChildLists = [this]() -> nsIFrame* {
    const ChildListID listIDs[] = {FrameChildListID::Principal,
                                   FrameChildListID::Overflow};
    for (nsIFrame* frag = this; frag; frag = frag->GetNextInFlow()) {
      for (ChildListID id : listIDs) {
        const nsFrameList& list = frag->GetChildList(id);
        if (nsIFrame* firstChild = list.FirstChild()) {
          return firstChild;
        }
      }
    }
    return nullptr;
  };

  nsIFrame* columnSet = FindFirstChildInChildLists();
  MOZ_ASSERT(columnSet && columnSet->IsColumnSetFrame(),
             "The first child should always be ColumnSet!");
  aResult.AppendElement(OwnedAnonBox(columnSet));
}

#ifdef DEBUG_FRAME_DUMP
nsresult ColumnSetWrapperFrame::GetFrameName(nsAString& aResult) const {
  return MakeFrameName(u"ColumnSetWrapper"_ns, aResult);
}
#endif

// Disallow any append, insert, or remove operations after building the
// column hierarchy since any change to the column hierarchy in the column
// sub-tree need to be re-created.
void ColumnSetWrapperFrame::AppendFrames(ChildListID aListID,
                                         nsFrameList&& aFrameList) {
#ifdef DEBUG
  MOZ_ASSERT(!mFinishedBuildingColumns, "Should only call once!");
  mFinishedBuildingColumns = true;
#endif

  nsBlockFrame::AppendFrames(aListID, std::move(aFrameList));

#ifdef DEBUG
  nsIFrame* firstColumnSet = PrincipalChildList().FirstChild();
  for (nsIFrame* child : PrincipalChildList()) {
    if (child->IsColumnSpan()) {
      AssertColumnSpanWrapperSubtreeIsSane(child);
    } else if (child != firstColumnSet) {
      // All the other ColumnSets are the continuation of the first ColumnSet.
      MOZ_ASSERT(child->IsColumnSetFrame() && child->GetPrevContinuation(),
                 "ColumnSet's prev-continuation is not set properly?");
    }
  }
#endif
}

void ColumnSetWrapperFrame::InsertFrames(
    ChildListID aListID, nsIFrame* aPrevFrame,
    const nsLineList::iterator* aPrevFrameLine, nsFrameList&& aFrameList) {
  MOZ_ASSERT_UNREACHABLE("Unsupported operation!");
  nsBlockFrame::InsertFrames(aListID, aPrevFrame, aPrevFrameLine,
                             std::move(aFrameList));
}

void ColumnSetWrapperFrame::RemoveFrame(DestroyContext& aContext,
                                        ChildListID aListID,
                                        nsIFrame* aOldFrame) {
  MOZ_ASSERT_UNREACHABLE("Unsupported operation!");
  nsBlockFrame::RemoveFrame(aContext, aListID, aOldFrame);
}

void ColumnSetWrapperFrame::MarkIntrinsicISizesDirty() {
  nsBlockFrame::MarkIntrinsicISizesDirty();

  // The parent's method adds NS_BLOCK_NEEDS_BIDI_RESOLUTION to all our
  // continuations. Clear the bit because we don't want to call ResolveBidi().
  for (nsIFrame* f = FirstContinuation(); f; f = f->GetNextContinuation()) {
    f->RemoveStateBits(NS_BLOCK_NEEDS_BIDI_RESOLUTION);
  }
}

nscoord ColumnSetWrapperFrame::GetMinISize(gfxContext* aRenderingContext) {
  nscoord iSize = 0;
  DISPLAY_MIN_INLINE_SIZE(this, iSize);

  if (Maybe<nscoord> containISize =
          ContainIntrinsicISize(NS_UNCONSTRAINEDSIZE)) {
    // If we're size-contained in inline axis and contain-intrinsic-inline-size
    // is not 'none', then use that size.
    if (*containISize != NS_UNCONSTRAINEDSIZE) {
      return *containISize;
    }

    // In the 'none' case, we determine our minimum intrinsic size purely from
    // our column styling, as if we had no descendants. This should match what
    // happens in nsColumnSetFrame::GetMinISize in an actual no-descendants
    // scenario.
    const nsStyleColumn* colStyle = StyleColumn();
    if (colStyle->mColumnWidth.IsLength()) {
      // As available inline size reduces to zero, our number of columns reduces
      // to one, so no column gaps contribute to our minimum intrinsic size.
      // Also, column-width doesn't set a lower bound on our minimum intrinsic
      // size, either. Just use 0 because we're size-contained.
      iSize = 0;
    } else {
      MOZ_ASSERT(colStyle->mColumnCount != nsStyleColumn::kColumnCountAuto,
                 "column-count and column-width can't both be auto!");
      // As available inline size reduces to zero, we still have mColumnCount
      // columns, so compute our minimum intrinsic size based on N zero-width
      // columns, with specified gap size between them.
      const nscoord colGap =
          ColumnUtils::GetColumnGap(this, NS_UNCONSTRAINEDSIZE);
      iSize = ColumnUtils::IntrinsicISize(colStyle->mColumnCount, colGap, 0);
    }
  } else {
    for (nsIFrame* f : PrincipalChildList()) {
      iSize = std::max(iSize, f->GetMinISize(aRenderingContext));
    }
  }

  return iSize;
}

nscoord ColumnSetWrapperFrame::GetPrefISize(gfxContext* aRenderingContext) {
  nscoord iSize = 0;
  DISPLAY_PREF_INLINE_SIZE(this, iSize);

  if (Maybe<nscoord> containISize =
          ContainIntrinsicISize(NS_UNCONSTRAINEDSIZE)) {
    if (*containISize != NS_UNCONSTRAINEDSIZE) {
      return *containISize;
    }

    const nsStyleColumn* colStyle = StyleColumn();
    nscoord colISize;
    if (colStyle->mColumnWidth.IsLength()) {
      colISize =
          ColumnUtils::ClampUsedColumnWidth(colStyle->mColumnWidth.AsLength());
    } else {
      MOZ_ASSERT(colStyle->mColumnCount != nsStyleColumn::kColumnCountAuto,
                 "column-count and column-width can't both be auto!");
      colISize = 0;
    }

    // If column-count is auto, assume one column.
    const uint32_t numColumns =
        colStyle->mColumnCount == nsStyleColumn::kColumnCountAuto
            ? 1
            : colStyle->mColumnCount;
    const nscoord colGap =
        ColumnUtils::GetColumnGap(this, NS_UNCONSTRAINEDSIZE);
    iSize = ColumnUtils::IntrinsicISize(numColumns, colGap, colISize);
  } else {
    for (nsIFrame* f : PrincipalChildList()) {
      iSize = std::max(iSize, f->GetPrefISize(aRenderingContext));
    }
  }

  return iSize;
}

template <typename Iterator>
Maybe<nscoord> ColumnSetWrapperFrame::GetBaselineBOffset(
    Iterator aStart, Iterator aEnd, WritingMode aWM,
    BaselineSharingGroup aBaselineGroup,
    BaselineExportContext aExportContext) const {
  // Either forward iterator + first baseline, or reverse iterator + last
  // baseline
  MOZ_ASSERT((*aStart == PrincipalChildList().FirstChild() &&
              aBaselineGroup == BaselineSharingGroup::First) ||
                 (*aStart == PrincipalChildList().LastChild() &&
                  aBaselineGroup == BaselineSharingGroup::Last),
             "Iterator direction must match baseline sharing group.");
  if (StyleDisplay()->IsContainLayout()) {
    return Nothing{};
  }

  // Start from start/end of principal child list, and use the first valid
  // baseline.
  for (auto itr = aStart; itr != aEnd; ++itr) {
    const nsIFrame* kid = *itr;
    auto kidBaseline =
        kid->GetNaturalBaselineBOffset(aWM, aBaselineGroup, aExportContext);
    if (!kidBaseline) {
      continue;
    }
    // Baseline is offset from the kid's rectangle, so find the offset to the
    // kid's rectangle.
    LogicalRect kidRect{aWM, kid->GetLogicalNormalPosition(aWM, GetSize()),
                        kid->GetLogicalSize(aWM)};
    if (aBaselineGroup == BaselineSharingGroup::First) {
      *kidBaseline += kidRect.BStart(aWM);
    } else {
      *kidBaseline += (GetLogicalSize().BSize(aWM) - kidRect.BEnd(aWM));
    }
    return kidBaseline;
  }
  return Nothing{};
}

Maybe<nscoord> ColumnSetWrapperFrame::GetNaturalBaselineBOffset(
    WritingMode aWM, BaselineSharingGroup aBaselineGroup,
    BaselineExportContext aExportContext) const {
  if (aBaselineGroup == BaselineSharingGroup::First) {
    return GetBaselineBOffset(PrincipalChildList().cbegin(),
                              PrincipalChildList().cend(), aWM, aBaselineGroup,
                              aExportContext);
  }
  return GetBaselineBOffset(PrincipalChildList().crbegin(),
                            PrincipalChildList().crend(), aWM, aBaselineGroup,
                            aExportContext);
}

#ifdef DEBUG

/* static */
void ColumnSetWrapperFrame::AssertColumnSpanWrapperSubtreeIsSane(
    const nsIFrame* aFrame) {
  MOZ_ASSERT(aFrame->IsColumnSpan(), "aFrame is not column-span?");

  if (!nsLayoutUtils::GetStyleFrame(const_cast<nsIFrame*>(aFrame))
           ->Style()
           ->IsAnonBox()) {
    // aFrame's style frame has "column-span: all". Traverse no further.
    return;
  }

  MOZ_ASSERT(
      aFrame->Style()->GetPseudoType() == PseudoStyleType::columnSpanWrapper,
      "aFrame should be ::-moz-column-span-wrapper");

  MOZ_ASSERT(!aFrame->HasAnyStateBits(NS_FRAME_OWNS_ANON_BOXES),
             "::-moz-column-span-wrapper anonymous blocks cannot own "
             "other types of anonymous blocks!");

  for (const nsIFrame* child : aFrame->PrincipalChildList()) {
    AssertColumnSpanWrapperSubtreeIsSane(child);
  }
}

#endif