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()->isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
67 virtual int borderRight() const OVERRIDE
69 if (style()->isHorizontalWritingMode())
70 return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
71 return style()->isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
74 virtual int borderTop() const OVERRIDE
76 if (style()->isHorizontalWritingMode())
77 return style()->isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
78 return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
81 virtual int borderBottom() const OVERRIDE
83 if (style()->isHorizontalWritingMode())
84 return style()->isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
85 return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
88 Color bgColor() const { return resolveColor(CSSPropertyBackgroundColor); }
90 int outerBorderBefore() const;
91 int outerBorderAfter() const;
92 int outerBorderStart() const;
93 int outerBorderEnd() const;
95 int outerBorderLeft() const
97 if (style()->isHorizontalWritingMode())
98 return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
99 return style()->isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
102 int outerBorderRight() const
104 if (style()->isHorizontalWritingMode())
105 return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
106 return style()->isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
109 int outerBorderTop() const
111 if (style()->isHorizontalWritingMode())
112 return style()->isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
113 return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
116 int outerBorderBottom() const
118 if (style()->isHorizontalWritingMode())
119 return style()->isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
120 return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
123 int calcBorderStart() const;
124 int calcBorderEnd() const;
125 void recalcBordersInRowDirection();
127 virtual void addChild(RenderObject* child, RenderObject* beforeChild = 0) OVERRIDE;
129 struct ColumnStruct {
130 explicit ColumnStruct(unsigned initialSpan = 1)
138 void forceSectionsRecalc()
140 setNeedsSectionRecalc();
144 const Vector<ColumnStruct>& columns() const { return m_columns; }
145 const Vector<int>& columnPositions() const { return m_columnPos; }
146 void setColumnPosition(unsigned index, int position)
148 // Note that if our horizontal border-spacing changed, our position will change but not
149 // our column's width. In practice, horizontal border-spacing won't change often.
150 m_columnLogicalWidthChanged |= m_columnPos[index] != position;
151 m_columnPos[index] = position;
154 RenderTableSection* header() const { return m_head; }
155 RenderTableSection* footer() const { return m_foot; }
156 RenderTableSection* firstBody() const { return m_firstBody; }
158 // This function returns 0 if the table has no section.
159 RenderTableSection* topSection() const;
160 RenderTableSection* bottomSection() const;
162 // This function returns 0 if the table has no non-empty sections.
163 RenderTableSection* topNonEmptySection() const;
165 unsigned lastColumnIndex() const { return numEffCols() - 1; }
167 void splitColumn(unsigned position, unsigned firstSpan);
168 void appendColumn(unsigned span);
169 unsigned numEffCols() const { return m_columns.size(); }
170 unsigned spanOfEffCol(unsigned effCol) const { return m_columns[effCol].span; }
172 unsigned colToEffCol(unsigned column) const
174 if (!m_hasCellColspanThatDeterminesTableWidth)
177 unsigned effColumn = 0;
178 unsigned numColumns = numEffCols();
179 for (unsigned c = 0; effColumn < numColumns && c + m_columns[effColumn].span - 1 < column; ++effColumn)
180 c += m_columns[effColumn].span;
184 unsigned effColToCol(unsigned effCol) const
186 if (!m_hasCellColspanThatDeterminesTableWidth)
190 for (unsigned i = 0; i < effCol; i++)
191 c += m_columns[i].span;
195 LayoutUnit borderSpacingInRowDirection() const
197 if (unsigned effectiveColumnCount = numEffCols())
198 return static_cast<LayoutUnit>(effectiveColumnCount + 1) * hBorderSpacing();
203 // Override paddingStart/End to return pixel values to match behavor of RenderTableCell.
204 virtual LayoutUnit paddingEnd() const OVERRIDE { return static_cast<int>(RenderBlock::paddingEnd()); }
205 virtual LayoutUnit paddingStart() const OVERRIDE { return static_cast<int>(RenderBlock::paddingStart()); }
207 LayoutUnit bordersPaddingAndSpacingInRowDirection() const
209 // 'border-spacing' only applies to separate borders (see 17.6.1 The separated borders model).
210 return borderStart() + borderEnd() + (collapseBorders() ? LayoutUnit() : (paddingStart() + paddingEnd() + borderSpacingInRowDirection()));
213 // Return the first column or column-group.
214 RenderTableCol* firstColumn() const;
216 RenderTableCol* colElement(unsigned col, bool* startEdge = 0, bool* endEdge = 0) const
218 // The common case is to not have columns, make that case fast.
219 if (!m_hasColElements)
221 return slowColElement(col, startEdge, endEdge);
224 bool needsSectionRecalc() const { return m_needsSectionRecalc; }
225 void setNeedsSectionRecalc()
227 if (documentBeingDestroyed())
229 m_needsSectionRecalc = true;
230 setNeedsLayoutAndFullPaintInvalidation();
233 RenderTableSection* sectionAbove(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
234 RenderTableSection* sectionBelow(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
236 RenderTableCell* cellAbove(const RenderTableCell*) const;
237 RenderTableCell* cellBelow(const RenderTableCell*) const;
238 RenderTableCell* cellBefore(const RenderTableCell*) const;
239 RenderTableCell* cellAfter(const RenderTableCell*) const;
241 typedef Vector<CollapsedBorderValue> CollapsedBorderValues;
242 void invalidateCollapsedBorders()
244 m_collapsedBordersValid = false;
245 m_collapsedBorders.clear();
247 const CollapsedBorderValue* currentBorderValue() const { return m_currentBorder; }
249 bool hasSections() const { return m_head || m_foot || m_firstBody; }
251 void recalcSectionsIfNeeded() const
253 if (m_needsSectionRecalc)
257 static RenderTable* createAnonymousWithParentRenderer(const RenderObject*);
258 virtual RenderBox* createAnonymousBoxWithSameTypeAs(const RenderObject* parent) const OVERRIDE
260 return createAnonymousWithParentRenderer(parent);
263 const BorderValue& tableStartBorderAdjoiningCell(const RenderTableCell*) const;
264 const BorderValue& tableEndBorderAdjoiningCell(const RenderTableCell*) const;
266 void addCaption(const RenderTableCaption*);
267 void removeCaption(const RenderTableCaption*);
268 void addColumn(const RenderTableCol*);
269 void removeColumn(const RenderTableCol*);
272 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) OVERRIDE;
273 virtual void simplifiedNormalFlowLayout() OVERRIDE;
276 virtual const char* renderName() const OVERRIDE { return "RenderTable"; }
278 virtual bool isTable() const OVERRIDE { return true; }
280 virtual void paint(PaintInfo&, const LayoutPoint&) OVERRIDE;
281 virtual void paintObject(PaintInfo&, const LayoutPoint&) OVERRIDE;
282 virtual void paintBoxDecorationBackground(PaintInfo&, const LayoutPoint&) OVERRIDE;
283 virtual void paintMask(PaintInfo&, const LayoutPoint&) OVERRIDE;
284 virtual void layout() OVERRIDE;
285 virtual void computeIntrinsicLogicalWidths(LayoutUnit& minWidth, LayoutUnit& maxWidth) const OVERRIDE;
286 virtual void computePreferredLogicalWidths() OVERRIDE;
287 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) OVERRIDE;
289 virtual int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const OVERRIDE;
290 virtual int firstLineBoxBaseline() const OVERRIDE;
291 virtual int inlineBlockBaseline(LineDirectionMode) const OVERRIDE;
293 RenderTableCol* slowColElement(unsigned col, bool* startEdge, bool* endEdge) const;
295 void updateColumnCache() const;
296 void invalidateCachedColumns();
298 virtual void updateLogicalWidth() OVERRIDE;
300 LayoutUnit convertStyleLogicalWidthToComputedWidth(const Length& styleLogicalWidth, LayoutUnit availableWidth);
301 LayoutUnit convertStyleLogicalHeightToComputedHeight(const Length& styleLogicalHeight);
303 virtual LayoutRect overflowClipRect(const LayoutPoint& location, OverlayScrollbarSizeRelevancy = IgnoreOverlayScrollbarSize) OVERRIDE;
305 virtual void addOverflowFromChildren() OVERRIDE;
307 void subtractCaptionRect(LayoutRect&) const;
309 void recalcCollapsedBorders();
310 void recalcSections() const;
311 void layoutCaption(RenderTableCaption*);
313 void distributeExtraLogicalHeight(int extraLogicalHeight);
315 mutable Vector<int> m_columnPos;
316 mutable Vector<ColumnStruct> m_columns;
317 mutable Vector<RenderTableCaption*> m_captions;
318 mutable Vector<RenderTableCol*> m_columnRenderers;
320 mutable RenderTableSection* m_head;
321 mutable RenderTableSection* m_foot;
322 mutable RenderTableSection* m_firstBody;
324 OwnPtr<TableLayout> m_tableLayout;
326 CollapsedBorderValues m_collapsedBorders;
327 const CollapsedBorderValue* m_currentBorder;
328 bool m_collapsedBordersValid : 1;
330 mutable bool m_hasColElements : 1;
331 mutable bool m_needsSectionRecalc : 1;
333 bool m_columnLogicalWidthChanged : 1;
334 mutable bool m_columnRenderersValid: 1;
335 mutable bool m_hasCellColspanThatDeterminesTableWidth : 1;
336 bool hasCellColspanThatDeterminesTableWidth() const
338 for (unsigned c = 0; c < numEffCols(); c++) {
339 if (m_columns[c].span > 1)
351 inline RenderTableSection* RenderTable::topSection() const
353 ASSERT(!needsSectionRecalc());
361 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderTable, isTable());
365 #endif // RenderTable_h