2 * Copyright (C) 1997 Martin Jones (mjones@kde.org)
3 * (C) 1997 Torben Weis (weis@kde.org)
4 * (C) 1998 Waldo Bastian (bastian@kde.org)
5 * (C) 1999 Lars Knoll (knoll@kde.org)
6 * (C) 1999 Antti Koivisto (koivisto@kde.org)
7 * Copyright (C) 2003, 2004, 2005, 2006, 2009 Apple Inc. All rights reserved.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
25 #ifndef RenderTableSection_h
26 #define RenderTableSection_h
28 #include "RenderTable.h"
29 #include <wtf/Vector.h>
33 class RenderTableCell;
36 enum CollapsedBorderSide {
43 // Helper class for paintObject.
46 CellSpan(unsigned start, unsigned end)
56 class RenderTableSection final : public RenderBox {
58 RenderTableSection(Element&, RenderStyle&&);
59 RenderTableSection(Document&, RenderStyle&&);
60 virtual ~RenderTableSection();
62 RenderTableRow* firstRow() const;
63 RenderTableRow* lastRow() const;
65 void addChild(RenderObject* child, RenderObject* beforeChild = 0) override;
67 Optional<int> firstLineBaseline() const override;
69 void addCell(RenderTableCell*, RenderTableRow* row);
71 LayoutUnit calcRowLogicalHeight();
73 void computeOverflowFromCells();
75 RenderTable* table() const { return downcast<RenderTable>(parent()); }
78 Vector<RenderTableCell*, 1> cells;
79 bool inColSpan { false }; // true for columns after the first in a colspan
81 RenderTableCell* primaryCell() { return hasCells() ? cells[cells.size() - 1] : 0; }
82 const RenderTableCell* primaryCell() const { return hasCells() ? cells[cells.size() - 1] : 0; }
83 bool hasCells() const { return cells.size() > 0; }
86 typedef Vector<CellStruct> Row;
89 RenderTableRow* rowRenderer { nullptr };
94 const BorderValue& borderAdjoiningTableStart() const;
95 const BorderValue& borderAdjoiningTableEnd() const;
96 const BorderValue& borderAdjoiningStartCell(const RenderTableCell&) const;
97 const BorderValue& borderAdjoiningEndCell(const RenderTableCell&) const;
99 const RenderTableCell* firstRowCellAdjoiningTableStart() const;
100 const RenderTableCell* firstRowCellAdjoiningTableEnd() const;
102 CellStruct& cellAt(unsigned row, unsigned col);
103 const CellStruct& cellAt(unsigned row, unsigned col) const;
104 RenderTableCell* primaryCellAt(unsigned row, unsigned col);
105 RenderTableRow* rowRendererAt(unsigned row) const;
107 void appendColumn(unsigned pos);
108 void splitColumn(unsigned pos, unsigned first);
110 LayoutUnit calcOuterBorderBefore() const;
111 LayoutUnit calcOuterBorderAfter() const;
112 LayoutUnit calcOuterBorderStart() const;
113 LayoutUnit calcOuterBorderEnd() const;
114 void recalcOuterBorder();
116 LayoutUnit outerBorderBefore() const { return m_outerBorderBefore; }
117 LayoutUnit outerBorderAfter() const { return m_outerBorderAfter; }
118 LayoutUnit outerBorderStart() const { return m_outerBorderStart; }
119 LayoutUnit outerBorderEnd() const { return m_outerBorderEnd; }
121 LayoutUnit outerBorderLeft(const RenderStyle* styleForCellFlow) const;
122 LayoutUnit outerBorderRight(const RenderStyle* styleForCellFlow) const;
123 LayoutUnit outerBorderTop(const RenderStyle* styleForCellFlow) const;
124 LayoutUnit outerBorderBottom(const RenderStyle* styleForCellFlow) const;
126 unsigned numRows() const;
127 unsigned numColumns() const;
129 void recalcCellsIfNeeded();
131 bool needsCellRecalc() const { return m_needsCellRecalc; }
132 void setNeedsCellRecalc();
134 LayoutUnit rowBaseline(unsigned row);
135 void rowLogicalHeightChanged(unsigned rowIndex);
137 void clearCachedCollapsedBorders();
138 void removeCachedCollapsedBorders(const RenderTableCell&);
139 void setCachedCollapsedBorder(const RenderTableCell&, CollapsedBorderSide, CollapsedBorderValue);
140 CollapsedBorderValue cachedCollapsedBorder(const RenderTableCell&, CollapsedBorderSide);
142 // distributeExtraLogicalHeightToRows methods return the *consumed* extra logical height.
143 // FIXME: We may want to introduce a structure holding the in-flux layout information.
144 LayoutUnit distributeExtraLogicalHeightToRows(LayoutUnit extraLogicalHeight);
146 static RenderTableSection* createAnonymousWithParentRenderer(const RenderObject*);
147 RenderBox* createAnonymousBoxWithSameTypeAs(const RenderObject* parent) const override { return createAnonymousWithParentRenderer(parent); }
149 void paint(PaintInfo&, const LayoutPoint&) override;
152 void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
155 enum ShouldIncludeAllIntersectingCells {
156 IncludeAllIntersectingCells,
157 DoNotIncludeAllIntersectingCells
160 const char* renderName() const override { return (isAnonymous() || isPseudoElement()) ? "RenderTableSection (anonymous)" : "RenderTableSection"; }
162 bool canHaveChildren() const override { return true; }
164 bool isTableSection() const override { return true; }
166 void willBeRemovedFromTree() override;
168 void layout() override;
170 void paintCell(RenderTableCell*, PaintInfo&, const LayoutPoint&);
171 void paintObject(PaintInfo&, const LayoutPoint&) override;
172 void paintRowGroupBorder(const PaintInfo&, bool antialias, LayoutRect, BoxSide, CSSPropertyID borderColor, EBorderStyle, EBorderStyle tableBorderStyle);
173 void paintRowGroupBorderIfRequired(const PaintInfo&, const LayoutPoint& paintOffset, unsigned row, unsigned col, BoxSide, RenderTableCell* = 0);
174 LayoutUnit offsetLeftForRowGroupBorder(RenderTableCell*, const LayoutRect& rowGroupRect, unsigned row);
176 LayoutUnit offsetTopForRowGroupBorder(RenderTableCell*, BoxSide borderSide, unsigned row);
177 LayoutUnit verticalRowGroupBorderHeight(RenderTableCell*, const LayoutRect& rowGroupRect, unsigned row);
178 LayoutUnit horizontalRowGroupBorderWidth(RenderTableCell*, const LayoutRect& rowGroupRect, unsigned row, unsigned column);
180 void imageChanged(WrappedImagePtr, const IntRect* = 0) override;
182 bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) override;
184 void ensureRows(unsigned);
186 void distributeExtraLogicalHeightToPercentRows(LayoutUnit& extraLogicalHeight, int totalPercent);
187 void distributeExtraLogicalHeightToAutoRows(LayoutUnit& extraLogicalHeight, unsigned autoRowsCount);
188 void distributeRemainingExtraLogicalHeight(LayoutUnit& extraLogicalHeight);
190 bool hasOverflowingCell() const { return m_overflowingCells.size() || m_forceSlowPaintPathWithOverflowingCell; }
191 void computeOverflowFromCells(unsigned totalRows, unsigned nEffCols);
193 CellSpan fullTableRowSpan() const;
194 CellSpan fullTableColumnSpan() const { return CellSpan(0, table()->columns().size()); }
196 // Flip the rect so it aligns with the coordinates used by the rowPos and columnPos vectors.
197 LayoutRect logicalRectForWritingModeAndDirection(const LayoutRect&) const;
199 CellSpan dirtiedRows(const LayoutRect& repaintRect) const;
200 CellSpan dirtiedColumns(const LayoutRect& repaintRect) const;
202 // These two functions take a rectangle as input that has been flipped by logicalRectForWritingModeAndDirection.
203 // The returned span of rows or columns is end-exclusive, and empty if start==end.
204 // The IncludeAllIntersectingCells argument is used to determine which cells to include when
205 // an edge of the flippedRect lies exactly on a cell boundary. Using IncludeAllIntersectingCells
206 // will return both cells, and using DoNotIncludeAllIntersectingCells will return only the cell
207 // that hittesting should return.
208 CellSpan spannedRows(const LayoutRect& flippedRect, ShouldIncludeAllIntersectingCells) const;
209 CellSpan spannedColumns(const LayoutRect& flippedRect, ShouldIncludeAllIntersectingCells) const;
211 void setLogicalPositionForCell(RenderTableCell*, unsigned effectiveColumn) const;
213 void firstChild() const = delete;
214 void lastChild() const = delete;
216 Vector<RowStruct> m_grid;
217 Vector<LayoutUnit> m_rowPos;
219 // the current insertion position
220 unsigned m_cCol { 0 };
221 unsigned m_cRow { 0 };
223 LayoutUnit m_outerBorderStart;
224 LayoutUnit m_outerBorderEnd;
225 LayoutUnit m_outerBorderBefore;
226 LayoutUnit m_outerBorderAfter;
228 bool m_needsCellRecalc { false };
230 // This HashSet holds the overflowing cells for faster painting.
231 // If we have more than gMaxAllowedOverflowingCellRatio * total cells, it will be empty
232 // and m_forceSlowPaintPathWithOverflowingCell will be set to save memory.
233 HashSet<RenderTableCell*> m_overflowingCells;
234 bool m_forceSlowPaintPathWithOverflowingCell { false };
236 bool m_hasMultipleCellLevels { false };
238 // This map holds the collapsed border values for cells with collapsed borders.
239 // It is held at RenderTableSection level to spare memory consumption by table cells.
240 HashMap<std::pair<const RenderTableCell*, int>, CollapsedBorderValue > m_cellsCollapsedBorders;
243 inline const BorderValue& RenderTableSection::borderAdjoiningTableStart() const
245 if (isDirectionSame(this, table()))
246 return style().borderStart();
247 return style().borderEnd();
250 inline const BorderValue& RenderTableSection::borderAdjoiningTableEnd() const
252 if (isDirectionSame(this, table()))
253 return style().borderEnd();
254 return style().borderStart();
257 inline RenderTableSection::CellStruct& RenderTableSection::cellAt(unsigned row, unsigned col)
259 recalcCellsIfNeeded();
260 return m_grid[row].row[col];
263 inline const RenderTableSection::CellStruct& RenderTableSection::cellAt(unsigned row, unsigned col) const
265 ASSERT(!m_needsCellRecalc);
266 return m_grid[row].row[col];
269 inline RenderTableCell* RenderTableSection::primaryCellAt(unsigned row, unsigned col)
271 recalcCellsIfNeeded();
272 CellStruct& c = m_grid[row].row[col];
273 return c.primaryCell();
276 inline RenderTableRow* RenderTableSection::rowRendererAt(unsigned row) const
278 ASSERT(!m_needsCellRecalc);
279 return m_grid[row].rowRenderer;
282 inline LayoutUnit RenderTableSection::outerBorderLeft(const RenderStyle* styleForCellFlow) const
284 if (styleForCellFlow->isHorizontalWritingMode())
285 return styleForCellFlow->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
286 return styleForCellFlow->isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
289 inline LayoutUnit RenderTableSection::outerBorderRight(const RenderStyle* styleForCellFlow) const
291 if (styleForCellFlow->isHorizontalWritingMode())
292 return styleForCellFlow->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
293 return styleForCellFlow->isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
296 inline LayoutUnit RenderTableSection::outerBorderTop(const RenderStyle* styleForCellFlow) const
298 if (styleForCellFlow->isHorizontalWritingMode())
299 return styleForCellFlow->isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
300 return styleForCellFlow->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
303 inline LayoutUnit RenderTableSection::outerBorderBottom(const RenderStyle* styleForCellFlow) const
305 if (styleForCellFlow->isHorizontalWritingMode())
306 return styleForCellFlow->isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
307 return styleForCellFlow->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
310 inline unsigned RenderTableSection::numRows() const
312 ASSERT(!m_needsCellRecalc);
313 return m_grid.size();
316 inline void RenderTableSection::recalcCellsIfNeeded()
318 if (m_needsCellRecalc)
322 inline LayoutUnit RenderTableSection::rowBaseline(unsigned row)
324 recalcCellsIfNeeded();
325 return m_grid[row].baseline;
328 inline CellSpan RenderTableSection::fullTableRowSpan() const
330 ASSERT(!m_needsCellRecalc);
331 return CellSpan(0, m_grid.size());
334 } // namespace WebCore
336 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderTableSection, isTableSection())
338 #endif // RenderTableSection_h