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 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 "core/CSSPropertyNames.h"
29 #include "core/rendering/RenderBlock.h"
30 #include "core/rendering/style/CollapsedBorderValue.h"
31 #include "wtf/Vector.h"
36 class RenderTableCaption;
37 class RenderTableCell;
38 class RenderTableSection;
41 enum SkipEmptySectionsValue { DoNotSkipEmptySections, SkipEmptySections };
43 class RenderTable final : public RenderBlock {
45 explicit RenderTable(Element*);
46 virtual ~RenderTable();
48 // Per CSS 3 writing-mode: "The first and second values of the 'border-spacing' property represent spacing between columns
49 // and rows respectively, not necessarily the horizontal and vertical spacing respectively".
50 int hBorderSpacing() const { return m_hSpacing; }
51 int vBorderSpacing() const { return m_vSpacing; }
53 bool collapseBorders() const { return style()->borderCollapse(); }
55 virtual int borderStart() const override { return m_borderStart; }
56 virtual int borderEnd() const override { return m_borderEnd; }
57 virtual int borderBefore() const override;
58 virtual int borderAfter() const override;
60 virtual int borderLeft() const override
62 if (style()->isHorizontalWritingMode())
63 return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
64 return style()->slowIsFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
67 virtual int borderRight() const override
69 if (style()->isHorizontalWritingMode())
70 return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
71 return style()->slowIsFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
74 virtual int borderTop() const override
76 if (style()->isHorizontalWritingMode())
77 return style()->slowIsFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
78 return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
81 virtual int borderBottom() const override
83 if (style()->isHorizontalWritingMode())
84 return style()->slowIsFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
85 return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
88 int outerBorderBefore() const;
89 int outerBorderAfter() const;
90 int outerBorderStart() const;
91 int outerBorderEnd() const;
93 int outerBorderLeft() const
95 if (style()->isHorizontalWritingMode())
96 return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
97 return style()->slowIsFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
100 int outerBorderRight() const
102 if (style()->isHorizontalWritingMode())
103 return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
104 return style()->slowIsFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
107 int outerBorderTop() const
109 if (style()->isHorizontalWritingMode())
110 return style()->slowIsFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
111 return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
114 int outerBorderBottom() const
116 if (style()->isHorizontalWritingMode())
117 return style()->slowIsFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
118 return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
121 int calcBorderStart() const;
122 int calcBorderEnd() const;
123 void recalcBordersInRowDirection();
125 virtual void addChild(RenderObject* child, RenderObject* beforeChild = 0) override;
127 struct ColumnStruct {
128 explicit ColumnStruct(unsigned initialSpan = 1)
136 void forceSectionsRecalc()
138 setNeedsSectionRecalc();
142 const Vector<ColumnStruct>& columns() const { return m_columns; }
143 const Vector<int>& columnPositions() const { return m_columnPos; }
144 void setColumnPosition(unsigned index, int position)
146 // Note that if our horizontal border-spacing changed, our position will change but not
147 // our column's width. In practice, horizontal border-spacing won't change often.
148 m_columnLogicalWidthChanged |= m_columnPos[index] != position;
149 m_columnPos[index] = position;
152 RenderTableSection* header() const { return m_head; }
153 RenderTableSection* footer() const { return m_foot; }
154 RenderTableSection* firstBody() const { return m_firstBody; }
156 // This function returns 0 if the table has no section.
157 RenderTableSection* topSection() const;
158 RenderTableSection* bottomSection() const;
160 // This function returns 0 if the table has no non-empty sections.
161 RenderTableSection* topNonEmptySection() const;
163 unsigned lastColumnIndex() const { return numEffCols() - 1; }
165 void splitColumn(unsigned position, unsigned firstSpan);
166 void appendColumn(unsigned span);
167 unsigned numEffCols() const { return m_columns.size(); }
168 unsigned spanOfEffCol(unsigned effCol) const { return m_columns[effCol].span; }
170 unsigned colToEffCol(unsigned column) const
172 if (!m_hasCellColspanThatDeterminesTableWidth)
175 unsigned effColumn = 0;
176 unsigned numColumns = numEffCols();
177 for (unsigned c = 0; effColumn < numColumns && c + m_columns[effColumn].span - 1 < column; ++effColumn)
178 c += m_columns[effColumn].span;
182 unsigned effColToCol(unsigned effCol) const
184 if (!m_hasCellColspanThatDeterminesTableWidth)
188 for (unsigned i = 0; i < effCol; i++)
189 c += m_columns[i].span;
193 LayoutUnit borderSpacingInRowDirection() const
195 if (unsigned effectiveColumnCount = numEffCols())
196 return static_cast<LayoutUnit>(effectiveColumnCount + 1) * hBorderSpacing();
201 // Override paddingStart/End to return pixel values to match behavor of RenderTableCell.
202 virtual LayoutUnit paddingEnd() const override { return static_cast<int>(RenderBlock::paddingEnd()); }
203 virtual LayoutUnit paddingStart() const override { return static_cast<int>(RenderBlock::paddingStart()); }
205 LayoutUnit bordersPaddingAndSpacingInRowDirection() const
207 // 'border-spacing' only applies to separate borders (see 17.6.1 The separated borders model).
208 return borderStart() + borderEnd() + (collapseBorders() ? LayoutUnit() : (paddingStart() + paddingEnd() + borderSpacingInRowDirection()));
211 // Return the first column or column-group.
212 RenderTableCol* firstColumn() const;
214 RenderTableCol* colElement(unsigned col, bool* startEdge = 0, bool* endEdge = 0) const
216 // The common case is to not have columns, make that case fast.
217 if (!m_hasColElements)
219 return slowColElement(col, startEdge, endEdge);
222 bool needsSectionRecalc() const { return m_needsSectionRecalc; }
223 void setNeedsSectionRecalc()
225 if (documentBeingDestroyed())
227 m_needsSectionRecalc = true;
228 setNeedsLayoutAndFullPaintInvalidation();
231 RenderTableSection* sectionAbove(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
232 RenderTableSection* sectionBelow(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
234 RenderTableCell* cellAbove(const RenderTableCell*) const;
235 RenderTableCell* cellBelow(const RenderTableCell*) const;
236 RenderTableCell* cellBefore(const RenderTableCell*) const;
237 RenderTableCell* cellAfter(const RenderTableCell*) const;
239 typedef Vector<CollapsedBorderValue> CollapsedBorderValues;
240 void invalidateCollapsedBorders()
242 m_collapsedBordersValid = false;
243 m_collapsedBorders.clear();
246 // FIXME: This method should be moved into TablePainter.
247 const CollapsedBorderValue* currentBorderValue() const { return m_currentBorder; }
248 void setCurrentBorderValue(const CollapsedBorderValue* val) { m_currentBorder = val; }
250 bool hasSections() const { return m_head || m_foot || m_firstBody; }
252 void recalcSectionsIfNeeded() const
254 if (m_needsSectionRecalc)
258 static RenderTable* createAnonymousWithParentRenderer(const RenderObject*);
259 virtual RenderBox* createAnonymousBoxWithSameTypeAs(const RenderObject* parent) const override
261 return createAnonymousWithParentRenderer(parent);
264 const BorderValue& tableStartBorderAdjoiningCell(const RenderTableCell*) const;
265 const BorderValue& tableEndBorderAdjoiningCell(const RenderTableCell*) const;
267 void addCaption(const RenderTableCaption*);
268 void removeCaption(const RenderTableCaption*);
269 void addColumn(const RenderTableCol*);
270 void removeColumn(const RenderTableCol*);
272 // FIXME: this method should be moved into TablePainter.
273 virtual void paintBoxDecorationBackground(PaintInfo&, const LayoutPoint&) override;
275 virtual void paintMask(PaintInfo&, const LayoutPoint&) override;
277 const CollapsedBorderValues& collapsedBorders() { return m_collapsedBorders; }
278 void subtractCaptionRect(LayoutRect&) const;
279 void recalcCollapsedBorders();
282 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
283 virtual void simplifiedNormalFlowLayout() override;
286 virtual const char* renderName() const override { return "RenderTable"; }
288 virtual bool isOfType(RenderObjectType type) const override { return type == RenderObjectTable || RenderBlock::isOfType(type); }
290 virtual void paint(PaintInfo&, const LayoutPoint&) override;
291 virtual void paintObject(PaintInfo&, const LayoutPoint&) override;
292 virtual void layout() override;
293 virtual void computeIntrinsicLogicalWidths(LayoutUnit& minWidth, LayoutUnit& maxWidth) const override;
294 virtual void computePreferredLogicalWidths() override;
295 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) override;
297 virtual int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const override;
298 virtual int firstLineBoxBaseline() const override;
299 virtual int inlineBlockBaseline(LineDirectionMode) const override;
301 RenderTableCol* slowColElement(unsigned col, bool* startEdge, bool* endEdge) const;
303 void updateColumnCache() const;
304 void invalidateCachedColumns();
306 virtual void updateLogicalWidth() override;
308 LayoutUnit convertStyleLogicalWidthToComputedWidth(const Length& styleLogicalWidth, LayoutUnit availableWidth);
309 LayoutUnit convertStyleLogicalHeightToComputedHeight(const Length& styleLogicalHeight);
311 virtual LayoutRect overflowClipRect(const LayoutPoint& location, OverlayScrollbarSizeRelevancy = IgnoreOverlayScrollbarSize) override;
313 virtual void addOverflowFromChildren() override;
315 void recalcSections() const;
316 void layoutCaption(RenderTableCaption*);
318 void distributeExtraLogicalHeight(int extraLogicalHeight);
320 mutable Vector<int> m_columnPos;
321 mutable Vector<ColumnStruct> m_columns;
322 mutable Vector<RenderTableCaption*> m_captions;
323 mutable Vector<RenderTableCol*> m_columnRenderers;
325 mutable RenderTableSection* m_head;
326 mutable RenderTableSection* m_foot;
327 mutable RenderTableSection* m_firstBody;
329 OwnPtr<TableLayout> m_tableLayout;
331 CollapsedBorderValues m_collapsedBorders;
332 const CollapsedBorderValue* m_currentBorder;
333 bool m_collapsedBordersValid : 1;
335 mutable bool m_hasColElements : 1;
336 mutable bool m_needsSectionRecalc : 1;
338 bool m_columnLogicalWidthChanged : 1;
339 mutable bool m_columnRenderersValid: 1;
340 mutable bool m_hasCellColspanThatDeterminesTableWidth : 1;
341 bool hasCellColspanThatDeterminesTableWidth() const
343 for (unsigned c = 0; c < numEffCols(); c++) {
344 if (m_columns[c].span > 1)
356 inline RenderTableSection* RenderTable::topSection() const
358 ASSERT(!needsSectionRecalc());
366 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderTable, isTable());
370 #endif // RenderTable_h