summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/layout_table_section.h
blob: 3ffbae560a9328f44d044237262911e729e2522d (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
 * Copyright (C) 1997 Martin Jones (mjones@kde.org)
 *           (C) 1997 Torben Weis (weis@kde.org)
 *           (C) 1998 Waldo Bastian (bastian@kde.org)
 *           (C) 1999 Lars Knoll (knoll@kde.org)
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
 * Copyright (C) 2003, 2004, 2005, 2006, 2009, 2013 Apple Inc. All rights
 * reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_LAYOUT_TABLE_SECTION_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_LAYOUT_TABLE_SECTION_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/layout/layout_table.h"
#include "third_party/blink/renderer/core/layout/layout_table_box_component.h"
#include "third_party/blink/renderer/core/layout/ng/table/layout_ng_table_section_interface.h"
#include "third_party/blink/renderer/core/layout/table_grid_cell.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace blink {

// Helper class for paintObject.
class CellSpan {
  STACK_ALLOCATED();

 public:
  CellSpan() : start_(0), end_(0) {}
  CellSpan(unsigned start, unsigned end) : start_(start), end_(end) {}

  unsigned Start() const { return start_; }
  unsigned End() const { return end_; }

  void DecreaseStart() { --start_; }
  void IncreaseEnd() { ++end_; }

  void EnsureConsistency(const unsigned);

 private:
  unsigned start_;
  unsigned end_;
};

inline bool operator==(const CellSpan& s1, const CellSpan& s2) {
  return s1.Start() == s2.Start() && s1.End() == s2.End();
}
inline bool operator!=(const CellSpan& s1, const CellSpan& s2) {
  return !(s1 == s2);
}

class LayoutTableCell;
class LayoutTableRow;

// LayoutTableSection is used to represent table row group (display:
// table-row-group), header group (display: table-header-group) and footer group
// (display: table-footer-group).
//
// The object holds the internal representation of the rows (m_grid). See
// recalcCells() below for some extra explanation.
//
// A lot of the complexity in this class is related to handling rowspan, colspan
// or just non-regular tables.
//
// Example of rowspan / colspan leading to overlapping cells (rowspan and
// colspan are overlapping):
// <table>
//   <tr>
//       <td>first row</td>
//       <td rowspan="2">rowspan</td>
//     </tr>
//    <tr>
//        <td colspan="2">colspan</td>
//     </tr>
// </table>
//
// Example of non-regular table (missing one cell in the first row):
// <!DOCTYPE html>
// <table>
//   <tr><td>First row only child.</td></tr>
//   <tr>
//     <td>Second row first child</td>
//     <td>Second row second child</td>
//   </tr>
// </table>
//
// LayoutTableSection is responsible for laying out LayoutTableRows and
// LayoutTableCells (see layoutRows()). However it is not their containing
// block, the enclosing LayoutTable (this object's parent()) is. This is why
// this class inherits from LayoutTableBoxComponent and not LayoutBlock.
class CORE_EXPORT LayoutTableSection final
    : public LayoutTableBoxComponent,
      public LayoutNGTableSectionInterface {
 public:
  explicit LayoutTableSection(Element*);
  ~LayoutTableSection() override;

  LayoutTableRow* FirstRow() const;
  LayoutTableRow* LastRow() const;

  void AddChild(LayoutObject* child,
                LayoutObject* before_child = nullptr) override;

  LayoutUnit FirstLineBoxBaseline() const override;

  void AddCell(LayoutTableCell*, LayoutTableRow*);

  int16_t VBorderSpacingBeforeFirstRow() const;
  int CalcRowLogicalHeight();
  void LayoutRows();
  RecalcLayoutOverflowResult RecalcLayoutOverflow() final;
  void RecalcVisualOverflow() final;

  void MarkAllCellsWidthsDirtyAndOrNeedsLayout(LayoutTable::WhatToMarkAllCells);

  LayoutTable* Table() const final {
    NOT_DESTROYED();
    return To<LayoutTable>(Parent());
  }

  typedef Vector<LayoutTableCell*, 2> SpanningLayoutTableCells;

  struct SpanningRowsHeight {
    STACK_ALLOCATED();

   public:
    SpanningRowsHeight()
        : total_rows_height(0),
          spanning_cell_height_ignoring_border_spacing(0),
          is_any_row_with_only_spanning_cells(false) {}
    SpanningRowsHeight(const SpanningRowsHeight&) = delete;
    SpanningRowsHeight& operator=(const SpanningRowsHeight&) = delete;
    Vector<int> row_height;
    int total_rows_height;
    int spanning_cell_height_ignoring_border_spacing;
    bool is_any_row_with_only_spanning_cells;
  };

  TableGridCell& GridCellAt(unsigned row, unsigned effective_column) {
    NOT_DESTROYED();
    SECURITY_DCHECK(!needs_cell_recalc_);
    return grid_[row].grid_cells[effective_column];
  }
  const TableGridCell& GridCellAt(unsigned row,
                                  unsigned effective_column) const {
    NOT_DESTROYED();
    SECURITY_DCHECK(!needs_cell_recalc_);
    return grid_[row].grid_cells[effective_column];
  }
  LayoutTableCell* PrimaryCellAt(unsigned row, unsigned effective_column) {
    NOT_DESTROYED();
    SECURITY_DCHECK(!needs_cell_recalc_);
    auto& grid_cells = grid_[row].grid_cells;
    if (effective_column >= grid_cells.size())
      return nullptr;
    return grid_cells[effective_column].PrimaryCell();
  }
  const LayoutTableCell* PrimaryCellAt(unsigned row,
                                       unsigned effective_column) const {
    NOT_DESTROYED();
    return const_cast<LayoutTableSection*>(this)->PrimaryCellAt(
        row, effective_column);
  }

  // Returns the primary cell at (row, effectiveColumn) if the cell exists and
  // originates from (instead of spanning into) the grid slot, or nullptr.
  LayoutTableCell* OriginatingCellAt(unsigned row, unsigned effective_column);
  const LayoutTableCell* OriginatingCellAt(unsigned row,
                                           unsigned effective_column) const {
    NOT_DESTROYED();
    return const_cast<LayoutTableSection*>(this)->OriginatingCellAt(
        row, effective_column);
  }

  unsigned NumCols(unsigned row) const final {
    NOT_DESTROYED();
    DCHECK(!NeedsCellRecalc());
    return grid_[row].grid_cells.size();
  }

  // Returns null for cells with a rowspan that exceed the last row. Possibly
  // others.
  LayoutTableRow* RowLayoutObjectAt(unsigned row) {
    NOT_DESTROYED();
    SECURITY_DCHECK(!needs_cell_recalc_);
    return grid_[row].row;
  }
  const LayoutTableRow* RowLayoutObjectAt(unsigned row) const {
    NOT_DESTROYED();
    SECURITY_DCHECK(!needs_cell_recalc_);
    return grid_[row].row;
  }

  void AppendEffectiveColumn(unsigned pos);
  void SplitEffectiveColumn(unsigned pos, unsigned first);

  unsigned NumRows() const final {
    NOT_DESTROYED();
    DCHECK(!NeedsCellRecalc());
    return grid_.size();
  }
  unsigned NumEffectiveColumns() const final;

  // recalcCells() is used when we are not sure about the section's structure
  // and want to do an expensive (but safe) reconstruction of m_grid from
  // scratch.
  // An example of this is inserting a new cell in the middle of an existing
  // row or removing a row.
  //
  // Accessing m_grid when m_needsCellRecalc is set is UNSAFE as pointers can
  // be left dangling. Thus care should be taken in the code to check
  // m_needsCellRecalc before accessing m_grid.
  void RecalcCells();
  void RecalcCellsIfNeeded() {
    NOT_DESTROYED();
    if (needs_cell_recalc_)
      RecalcCells();
  }

  bool NeedsCellRecalc() const {
    NOT_DESTROYED();
    return needs_cell_recalc_;
  }
  void SetNeedsCellRecalc() final;

  LayoutUnit RowBaseline(unsigned row) {
    NOT_DESTROYED();
    return grid_[row].baseline;
  }

  void RowLogicalHeightChanged(LayoutTableRow*);

  // distributeExtraLogicalHeightToRows methods return the *consumed* extra
  // logical height.
  // FIXME: We may want to introduce a structure holding the in-flux layout
  // information.
  int DistributeExtraLogicalHeightToRows(int extra_logical_height);

  LayoutBox* CreateAnonymousBoxWithSameTypeAs(
      const LayoutObject* parent) const override;

  void Paint(const PaintInfo&) const override;

  // Flip the rect so it aligns with the coordinates used by the rowPos and
  // columnPos vectors.
  LayoutRect LogicalRectForWritingModeAndDirection(const PhysicalRect&) const;

  // Sets |rows| and |columns| to cover all cells needing repaint in
  // |damage_rect|.
  void DirtiedRowsAndEffectiveColumns(const LayoutRect& damage_rect,
                                      CellSpan& rows,
                                      CellSpan& columns) const;

  const HashSet<const LayoutTableCell*>& VisuallyOverflowingCells() const {
    NOT_DESTROYED();
    return visually_overflowing_cells_;
  }
  bool HasVisuallyOverflowingCell() const {
    NOT_DESTROYED();
    return visually_overflowing_cells_.size() || force_full_paint_;
  }
  bool HasMultipleCellLevels() const {
    NOT_DESTROYED();
    return has_multiple_cell_levels_;
  }

  const char* GetName() const override {
    NOT_DESTROYED();
    return "LayoutTableSection";
  }

  // Whether a section has opaque background depends on many factors, e.g.
  // border spacing, border collapsing, missing cells, etc. For simplicity,
  // just conservatively assume all table sections are not opaque.
  bool ForegroundIsKnownToBeOpaqueInRect(const PhysicalRect&,
                                         unsigned) const override {
    NOT_DESTROYED();
    return false;
  }
  bool BackgroundIsKnownToBeOpaqueInRect(const PhysicalRect&) const override {
    NOT_DESTROYED();
    return false;
  }

  int PaginationStrutForRow(LayoutTableRow*, LayoutUnit logical_offset) const;

  bool MapToVisualRectInAncestorSpaceInternal(
      const LayoutBoxModelObject* ancestor,
      TransformState&,
      VisualRectFlags = kDefaultVisualRectFlags) const override;

  bool IsRepeatingHeaderGroup() const final {
    NOT_DESTROYED();
    return is_repeating_header_group_;
  }
  bool IsRepeatingFooterGroup() const final {
    NOT_DESTROYED();
    return is_repeating_footer_group_;
  }

  void UpdateLayout() override;

  CellSpan FullSectionRowSpan() const {
    NOT_DESTROYED();
    return CellSpan(0, grid_.size());
  }
  CellSpan FullTableEffectiveColumnSpan() const {
    NOT_DESTROYED();
    return CellSpan(0, Table()->NumEffectiveColumns());
  }

  void DetermineIfHeaderGroupShouldRepeat() {
    NOT_DESTROYED();
    is_repeating_header_group_ = HeaderGroupShouldRepeat();
  }

  // Check whether row or row group has visibility:collapse.
  bool RowHasVisibilityCollapse(unsigned row) const;

  void DetermineIfFooterGroupShouldRepeat() {
    NOT_DESTROYED();
    is_repeating_footer_group_ = FooterGroupShouldRepeat();
  }

  // Update widths of cells affected by collapsed columns and sets whether cells
  // are spanning any collapsed columns.
  void UpdateLogicalWidthForCollapsedCells(
      const Vector<int>& col_collapsed_width);

  void ComputeLayoutOverflowFromDescendants();

  // LayoutNGTableSectionInterface methods start.

  const LayoutNGTableSectionInterface* ToLayoutNGTableSectionInterface()
      const final {
    NOT_DESTROYED();
    return this;
  }
  const LayoutTableSection* ToLayoutTableSection() const final {
    NOT_DESTROYED();
    return this;
  }
  const LayoutObject* ToLayoutObject() const final {
    NOT_DESTROYED();
    return this;
  }
  LayoutObject* ToMutableLayoutObject() final {
    NOT_DESTROYED();
    return this;
  }

  LayoutNGTableInterface* TableInterface() const final {
    NOT_DESTROYED();
    return Table();
  }
  LayoutNGTableRowInterface* FirstRowInterface() const final;
  LayoutNGTableRowInterface* LastRowInterface() const final;

  // LayoutNGTableSectionInterface methods end.
 protected:
  void StyleDidChange(StyleDifference, const ComputedStyle* old_style) override;
  bool NodeAtPoint(HitTestResult&,
                   const HitTestLocation&,
                   const PhysicalOffset& accumulated_offset,
                   HitTestAction) override;

 private:
  MinMaxSizes ComputeIntrinsicLogicalWidths() const final {
    NOT_DESTROYED();
    NOTREACHED();
    return MinMaxSizes();
  }

  void ComputeVisualOverflowFromDescendants();

  bool IsOfType(LayoutObjectType type) const override {
    NOT_DESTROYED();
    return type == kLayoutObjectTableSection || LayoutBox::IsOfType(type);
  }

  void WillBeRemovedFromTree() override;

  int BorderSpacingForRow(unsigned row) const {
    NOT_DESTROYED();
    return grid_[row].row ? Table()->VBorderSpacing() : 0;
  }

  void EnsureRows(unsigned num_rows) {
    NOT_DESTROYED();
    if (num_rows > grid_.size())
      grid_.Grow(num_rows);
  }

  void EnsureCols(unsigned row_index, unsigned num_cols) {
    NOT_DESTROYED();
    if (num_cols > NumCols(row_index))
      grid_[row_index].grid_cells.Grow(num_cols);
  }

  bool RowHasOnlySpanningCells(unsigned);
  unsigned CalcRowHeightHavingOnlySpanningCells(unsigned,
                                                int&,
                                                unsigned,
                                                unsigned&,
                                                Vector<int>&);
  void UpdateRowsHeightHavingOnlySpanningCells(LayoutTableCell*,
                                               struct SpanningRowsHeight&,
                                               unsigned&,
                                               Vector<int>&);

  void PopulateSpanningRowsHeightFromCell(LayoutTableCell*,
                                          struct SpanningRowsHeight&);
  void DistributeExtraRowSpanHeightToPercentRows(LayoutTableCell*,
                                                 float,
                                                 int&,
                                                 Vector<int>&);
  void DistributeWholeExtraRowSpanHeightToPercentRows(LayoutTableCell*,
                                                      float,
                                                      int&,
                                                      Vector<int>&);
  void DistributeExtraRowSpanHeightToAutoRows(LayoutTableCell*,
                                              int,
                                              int&,
                                              Vector<int>&);
  void DistributeExtraRowSpanHeightToRemainingRows(LayoutTableCell*,
                                                   int,
                                                   int&,
                                                   Vector<int>&);
  void DistributeRowSpanHeightToRows(SpanningLayoutTableCells& row_span_cells);

  void DistributeExtraLogicalHeightToPercentRows(int& extra_logical_height,
                                                 int total_percent);
  void DistributeExtraLogicalHeightToAutoRows(int& extra_logical_height,
                                              unsigned auto_rows_count);
  void DistributeRemainingExtraLogicalHeight(int& extra_logical_height);

  void UpdateBaselineForCell(LayoutTableCell*,
                             unsigned row,
                             LayoutUnit& baseline_descent);

  // These two functions take a rectangle as input that has been flipped by
  // logicalRectForWritingModeAndDirection.
  // The returned span of rows or columns is end-exclusive, and empty if
  // start==end.
  CellSpan SpannedRows(const LayoutRect& flipped_rect) const;
  CellSpan SpannedEffectiveColumns(const LayoutRect& flipped_rect) const;

  void SetLogicalPositionForCell(LayoutTableCell*,
                                 unsigned effective_column) const;

  void RelayoutCellIfFlexed(LayoutTableCell&, int row_index, int row_height);

  int LogicalHeightForRow(const LayoutTableRow&) const;

  // Honor breaking restrictions inside the table row, and adjust position and
  // size accordingly.
  void AdjustRowForPagination(LayoutTableRow&, SubtreeLayoutScope&);

  // The offset at which the first row in the section will get positioned to
  // avoid any repeating headers in its table or ancestor tables.
  int OffsetForRepeatedHeader() const;

  bool HeaderGroupShouldRepeat() const {
    NOT_DESTROYED();
    return Table()->Header() == this && GroupShouldRepeat();
  }

  bool FooterGroupShouldRepeat() const {
    NOT_DESTROYED();
    return Table()->Footer() == this && GroupShouldRepeat();
  }

  bool GroupShouldRepeat() const;

  struct TableGridRow {
    DISALLOW_NEW();

   public:
    inline void SetRowLogicalHeightToRowStyleLogicalHeight();
    inline void UpdateLogicalHeightForCell(const LayoutTableCell*);

    // The index is effective column index.
    Vector<TableGridCell> grid_cells;
    LayoutTableRow* row = nullptr;
    LayoutUnit baseline = LayoutUnit(-1);
    Length logical_height;
  };

  // The representation of the rows and their grid cells.
  Vector<TableGridRow> grid_;

  // The logical offset of each row from the top of the section.
  //
  // Note that this Vector has one more entry than the number of rows so that
  // we can keep track of the final size of the section. That is,
  // m_rowPos[m_grid.size()] is a valid entry.
  //
  // To know a row's height at |rowIndex|, use the formula:
  // m_rowPos[rowIndex + 1] - m_rowPos[rowIndex]
  Vector<int> row_pos_;

  // The amount of height collapsed in each row.
  //
  // This is used to adjust the padding of row-spanning cells. The padding
  // should stay the same as if the row were not collapsed.
  Vector<int> row_collapsed_height_;

  // Whether any row in the table section is or has been collapsed.
  bool is_any_row_collapsed_;

  // The current insertion position in the grid.
  // The position is used when inserting a new cell into the section to
  // know where it should be inserted and expand our internal structure.
  //
  // The reason for them is that we process cells as we discover them
  // during parsing or during recalcCells (ie in DOM order). This means
  // that we can discover changes in the structure later (e.g. due to
  // colspans, extra cells, ...).
  //
  // Do not use outside of recalcCells and addChild.
  unsigned c_col_;
  unsigned c_row_;

  bool needs_cell_recalc_;

  // This HashSet holds the overflowing cells for the partial paint path. If we
  // have too many overflowing cells, it will be empty and force_full_paint_
  // will be set to save memory. See ComputeVisualOverflowFromDescendants().
  HashSet<const LayoutTableCell*> visually_overflowing_cells_;
  bool force_full_paint_;

  // This boolean tracks if we have cells overlapping due to rowspan / colspan
  // (see class comment above about when it could appear).
  //
  // The use is to disable a painting optimization where we just paint the
  // invalidated cells.
  bool has_multiple_cell_levels_;

  // Whether any cell spans multiple rows or cols.
  bool has_spanning_cells_;

  // Header group should be painted on every page.
  bool is_repeating_header_group_;

  // Footer group should be painted on every page.
  bool is_repeating_footer_group_;
};

// To<LayoutTableSection>() helper.
template <>
struct DowncastTraits<LayoutTableSection> {
  static bool AllowFrom(const LayoutObject& object) {
    return object.IsTableSection() && !object.IsLayoutNGObject();
  }
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_LAYOUT_TABLE_SECTION_H_