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, 2010, 2014 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.
28 #include "CSSPropertyNames.h"
29 #include "CollapsedBorderValue.h"
30 #include "RenderBlock.h"
32 #include <wtf/HashMap.h>
33 #include <wtf/Vector.h>
38 class RenderTableCaption;
39 class RenderTableCell;
40 class RenderTableSection;
43 enum SkipEmptySectionsValue { DoNotSkipEmptySections, SkipEmptySections };
45 class RenderTable : public RenderBlock {
47 RenderTable(Element&, RenderStyle&&);
48 RenderTable(Document&, RenderStyle&&);
49 virtual ~RenderTable();
51 // Per CSS 3 writing-mode: "The first and second values of the 'border-spacing' property represent spacing between columns
52 // and rows respectively, not necessarily the horizontal and vertical spacing respectively".
53 LayoutUnit hBorderSpacing() const { return m_hSpacing; }
54 LayoutUnit vBorderSpacing() const { return m_vSpacing; }
56 bool collapseBorders() const { return style().borderCollapse(); }
58 LayoutUnit borderStart() const override { return m_borderStart; }
59 LayoutUnit borderEnd() const override { return m_borderEnd; }
60 LayoutUnit borderBefore() const override;
61 LayoutUnit borderAfter() const override;
63 LayoutUnit borderLeft() const override
65 if (style().isHorizontalWritingMode())
66 return style().isLeftToRightDirection() ? borderStart() : borderEnd();
67 return style().isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
70 LayoutUnit borderRight() const override
72 if (style().isHorizontalWritingMode())
73 return style().isLeftToRightDirection() ? borderEnd() : borderStart();
74 return style().isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
77 LayoutUnit borderTop() const override
79 if (style().isHorizontalWritingMode())
80 return style().isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
81 return style().isLeftToRightDirection() ? borderStart() : borderEnd();
84 LayoutUnit borderBottom() const override
86 if (style().isHorizontalWritingMode())
87 return style().isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
88 return style().isLeftToRightDirection() ? borderEnd() : borderStart();
91 Color bgColor() const { return style().visitedDependentColor(CSSPropertyBackgroundColor); }
93 LayoutUnit outerBorderBefore() const;
94 LayoutUnit outerBorderAfter() const;
95 LayoutUnit outerBorderStart() const;
96 LayoutUnit outerBorderEnd() const;
98 LayoutUnit outerBorderLeft() const
100 if (style().isHorizontalWritingMode())
101 return style().isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
102 return style().isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
105 LayoutUnit outerBorderRight() const
107 if (style().isHorizontalWritingMode())
108 return style().isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
109 return style().isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
112 LayoutUnit outerBorderTop() const
114 if (style().isHorizontalWritingMode())
115 return style().isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
116 return style().isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
119 LayoutUnit outerBorderBottom() const
121 if (style().isHorizontalWritingMode())
122 return style().isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
123 return style().isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
126 LayoutUnit calcBorderStart() const;
127 LayoutUnit calcBorderEnd() const;
128 void recalcBordersInRowDirection();
130 void addChild(RenderObject* child, RenderObject* beforeChild = 0) final;
132 struct ColumnStruct {
133 explicit ColumnStruct(unsigned initialSpan = 1)
141 void forceSectionsRecalc()
143 setNeedsSectionRecalc();
147 const Vector<ColumnStruct>& columns() const { return m_columns; }
148 const Vector<LayoutUnit>& columnPositions() const { return m_columnPos; }
149 void setColumnPosition(unsigned index, LayoutUnit position)
151 // Note that if our horizontal border-spacing changed, our position will change but not
152 // our column's width. In practice, horizontal border-spacing won't change often.
153 m_columnLogicalWidthChanged |= m_columnPos[index] != position;
154 m_columnPos[index] = position;
157 RenderTableSection* header() const { return m_head; }
158 RenderTableSection* footer() const { return m_foot; }
159 RenderTableSection* firstBody() const { return m_firstBody; }
161 // This function returns 0 if the table has no section.
162 RenderTableSection* topSection() const;
163 RenderTableSection* bottomSection() const;
165 // This function returns 0 if the table has no non-empty sections.
166 RenderTableSection* topNonEmptySection() const;
168 unsigned lastColumnIndex() const { return numEffCols() - 1; }
170 void splitColumn(unsigned position, unsigned firstSpan);
171 void appendColumn(unsigned span);
172 unsigned numEffCols() const { return m_columns.size(); }
173 unsigned spanOfEffCol(unsigned effCol) const { return m_columns[effCol].span; }
175 unsigned colToEffCol(unsigned column) const
177 if (!m_hasCellColspanThatDeterminesTableWidth)
180 unsigned effColumn = 0;
181 unsigned numColumns = numEffCols();
182 for (unsigned c = 0; effColumn < numColumns && c + m_columns[effColumn].span - 1 < column; ++effColumn)
183 c += m_columns[effColumn].span;
187 unsigned effColToCol(unsigned effCol) const
189 if (!m_hasCellColspanThatDeterminesTableWidth)
193 for (unsigned i = 0; i < effCol; i++)
194 c += m_columns[i].span;
198 LayoutUnit borderSpacingInRowDirection() const
200 if (unsigned effectiveColumnCount = numEffCols())
201 return (effectiveColumnCount + 1) * hBorderSpacing();
206 LayoutUnit bordersPaddingAndSpacingInRowDirection() const
208 // 'border-spacing' only applies to separate borders (see 17.6.1 The separated borders model).
209 return borderStart() + borderEnd() + (collapseBorders() ? LayoutUnit() : (paddingStart() + paddingEnd() + borderSpacingInRowDirection()));
212 // Return the first column or column-group.
213 RenderTableCol* firstColumn() const;
215 RenderTableCol* colElement(unsigned col, bool* startEdge = 0, bool* endEdge = 0) const
217 // The common case is to not have columns, make that case fast.
218 if (!m_hasColElements)
220 return slowColElement(col, startEdge, endEdge);
223 bool needsSectionRecalc() const { return m_needsSectionRecalc; }
224 void setNeedsSectionRecalc()
226 if (documentBeingDestroyed())
228 m_needsSectionRecalc = true;
232 RenderTableSection* sectionAbove(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
233 RenderTableSection* sectionBelow(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
235 RenderTableCell* cellAbove(const RenderTableCell*) const;
236 RenderTableCell* cellBelow(const RenderTableCell*) const;
237 RenderTableCell* cellBefore(const RenderTableCell*) const;
238 RenderTableCell* cellAfter(const RenderTableCell*) const;
240 typedef Vector<CollapsedBorderValue> CollapsedBorderValues;
241 bool collapsedBordersAreValid() const { return m_collapsedBordersValid; }
242 void invalidateCollapsedBorders(RenderTableCell* cellWithStyleChange = nullptr);
243 void collapsedEmptyBorderIsPresent() { m_collapsedEmptyBorderIsPresent = true; }
244 const CollapsedBorderValue* currentBorderValue() const { return m_currentBorder; }
246 bool hasSections() const { return m_head || m_foot || m_firstBody; }
248 void recalcSectionsIfNeeded() const
250 if (m_needsSectionRecalc)
254 static std::unique_ptr<RenderTable> createAnonymousWithParentRenderer(const RenderElement&);
255 std::unique_ptr<RenderBox> createAnonymousBoxWithSameTypeAs(const RenderBox& renderer) const override;
257 const BorderValue& tableStartBorderAdjoiningCell(const RenderTableCell&) const;
258 const BorderValue& tableEndBorderAdjoiningCell(const RenderTableCell&) const;
260 void addCaption(const RenderTableCaption*);
261 void removeCaption(const RenderTableCaption*);
262 void addColumn(const RenderTableCol*);
263 void removeColumn(const RenderTableCol*);
265 LayoutUnit offsetTopForColumn(const RenderTableCol&) const;
266 LayoutUnit offsetLeftForColumn(const RenderTableCol&) const;
267 LayoutUnit offsetWidthForColumn(const RenderTableCol&) const;
268 LayoutUnit offsetHeightForColumn(const RenderTableCol&) const;
270 void markForPaginationRelayoutIfNeeded() final;
273 void styleDidChange(StyleDifference, const RenderStyle* oldStyle) final;
274 void simplifiedNormalFlowLayout() final;
277 static std::unique_ptr<RenderTable> createTableWithStyle(Document&, const RenderStyle&);
279 const char* renderName() const override { return "RenderTable"; }
281 bool isTable() const final { return true; }
283 bool avoidsFloats() const final { return true; }
285 void paint(PaintInfo&, const LayoutPoint&) final;
286 void paintObject(PaintInfo&, const LayoutPoint&) final;
287 void paintBoxDecorations(PaintInfo&, const LayoutPoint&) final;
288 void paintMask(PaintInfo&, const LayoutPoint&) final;
290 void computeIntrinsicLogicalWidths(LayoutUnit& minWidth, LayoutUnit& maxWidth) const final;
291 void computePreferredLogicalWidths() override;
292 bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) override;
294 int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const final;
295 Optional<int> firstLineBaseline() const override;
296 Optional<int> inlineBlockBaseline(LineDirectionMode) const final;
298 RenderTableCol* slowColElement(unsigned col, bool* startEdge, bool* endEdge) const;
300 void updateColumnCache() const;
301 void invalidateCachedColumns();
303 void invalidateCachedColumnOffsets();
305 RenderBlock* firstLineBlock() const final;
306 void updateFirstLetter() final;
308 void updateLogicalWidth() final;
310 LayoutUnit convertStyleLogicalWidthToComputedWidth(const Length& styleLogicalWidth, LayoutUnit availableWidth);
311 LayoutUnit convertStyleLogicalHeightToComputedHeight(const Length& styleLogicalHeight);
313 LayoutRect overflowClipRect(const LayoutPoint& location, RenderRegion*, OverlayScrollbarSizeRelevancy = IgnoreOverlayScrollbarSize, PaintPhase = PaintPhaseBlockBackground) final;
314 LayoutRect overflowClipRectForChildLayers(const LayoutPoint& location, RenderRegion* region, OverlayScrollbarSizeRelevancy relevancy) override { return RenderBox::overflowClipRect(location, region, relevancy); }
316 void addOverflowFromChildren() final;
318 void subtractCaptionRect(LayoutRect&) const;
320 void recalcCollapsedBorders();
321 void recalcSections() const;
322 void layoutCaption(RenderTableCaption*);
324 void distributeExtraLogicalHeight(LayoutUnit extraLogicalHeight);
326 mutable Vector<LayoutUnit> m_columnPos;
327 mutable Vector<ColumnStruct> m_columns;
328 mutable Vector<RenderTableCaption*> m_captions;
329 mutable Vector<RenderTableCol*> m_columnRenderers;
331 unsigned effectiveIndexOfColumn(const RenderTableCol&) const;
332 typedef HashMap<const RenderTableCol*, unsigned> EffectiveColumnIndexMap;
333 mutable EffectiveColumnIndexMap m_effectiveColumnIndexMap;
335 mutable RenderTableSection* m_head;
336 mutable RenderTableSection* m_foot;
337 mutable RenderTableSection* m_firstBody;
339 std::unique_ptr<TableLayout> m_tableLayout;
341 CollapsedBorderValues m_collapsedBorders;
342 const CollapsedBorderValue* m_currentBorder;
343 bool m_collapsedBordersValid : 1;
344 bool m_collapsedEmptyBorderIsPresent : 1;
346 mutable bool m_hasColElements : 1;
347 mutable bool m_needsSectionRecalc : 1;
349 bool m_columnLogicalWidthChanged : 1;
350 mutable bool m_columnRenderersValid: 1;
351 mutable bool m_hasCellColspanThatDeterminesTableWidth : 1;
353 bool hasCellColspanThatDeterminesTableWidth() const
355 for (unsigned c = 0; c < numEffCols(); c++) {
356 if (m_columns[c].span > 1)
362 LayoutUnit m_hSpacing;
363 LayoutUnit m_vSpacing;
364 LayoutUnit m_borderStart;
365 LayoutUnit m_borderEnd;
366 mutable LayoutUnit m_columnOffsetTop;
367 mutable LayoutUnit m_columnOffsetHeight;
370 inline RenderTableSection* RenderTable::topSection() const
372 ASSERT(!needsSectionRecalc());
380 inline bool isDirectionSame(const RenderBox* tableItem, const RenderBox* otherTableItem) { return tableItem && otherTableItem ? tableItem->style().direction() == otherTableItem->style().direction() : true; }
382 inline std::unique_ptr<RenderBox> RenderTable::createAnonymousBoxWithSameTypeAs(const RenderBox& renderer) const
384 return RenderTable::createTableWithStyle(renderer.document(), renderer.style());
387 } // namespace WebCore
389 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderTable, isTable())
391 #endif // RenderTable_h