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 "CSSPropertyNames.h"
29 #include "CollapsedBorderValue.h"
30 #include "RenderBlock.h"
31 #include <wtf/Vector.h>
36 class RenderTableCaption;
37 class RenderTableCell;
38 class RenderTableSection;
41 enum SkipEmptySectionsValue { DoNotSkipEmptySections, SkipEmptySections };
43 class RenderTable : 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 int borderStart() const { return m_borderStart; }
56 int borderEnd() const { return m_borderEnd; }
57 int borderBefore() const;
58 int borderAfter() const;
60 int borderLeft() const
62 if (style()->isHorizontalWritingMode())
63 return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
64 return style()->isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
67 int borderRight() const
69 if (style()->isHorizontalWritingMode())
70 return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
71 return style()->isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
76 if (style()->isHorizontalWritingMode())
77 return style()->isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
78 return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
81 int borderBottom() const
83 if (style()->isHorizontalWritingMode())
84 return style()->isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
85 return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
88 Color bgColor() const { return style()->visitedDependentColor(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);
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 unsigned effColumn = 0;
175 unsigned numColumns = numEffCols();
176 for (unsigned c = 0; effColumn < numColumns && c + m_columns[effColumn].span - 1 < column; ++effColumn)
177 c += m_columns[effColumn].span;
181 unsigned effColToCol(unsigned effCol) const
184 for (unsigned i = 0; i < effCol; i++)
185 c += m_columns[i].span;
189 LayoutUnit borderSpacingInRowDirection() const
191 if (unsigned effectiveColumnCount = numEffCols())
192 return static_cast<LayoutUnit>(effectiveColumnCount + 1) * hBorderSpacing();
197 // Override paddingStart/End to return pixel values to match behavor of RenderTableCell.
198 virtual LayoutUnit paddingEnd() const OVERRIDE { return static_cast<int>(RenderBlock::paddingEnd()); }
199 virtual LayoutUnit paddingStart() const OVERRIDE { return static_cast<int>(RenderBlock::paddingStart()); }
201 LayoutUnit bordersPaddingAndSpacingInRowDirection() const
203 // 'border-spacing' only applies to separate borders (see 17.6.1 The separated borders model).
204 return borderStart() + borderEnd() + (collapseBorders() ? LayoutUnit() : (paddingStart() + paddingEnd() + borderSpacingInRowDirection()));
207 // Return the first column or column-group.
208 RenderTableCol* firstColumn() const;
210 RenderTableCol* colElement(unsigned col, bool* startEdge = 0, bool* endEdge = 0) const
212 // The common case is to not have columns, make that case fast.
213 if (!m_hasColElements)
215 return slowColElement(col, startEdge, endEdge);
218 bool needsSectionRecalc() const { return m_needsSectionRecalc; }
219 void setNeedsSectionRecalc()
221 if (documentBeingDestroyed())
223 m_needsSectionRecalc = true;
224 setNeedsLayout(true);
227 RenderTableSection* sectionAbove(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
228 RenderTableSection* sectionBelow(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
230 RenderTableCell* cellAbove(const RenderTableCell*) const;
231 RenderTableCell* cellBelow(const RenderTableCell*) const;
232 RenderTableCell* cellBefore(const RenderTableCell*) const;
233 RenderTableCell* cellAfter(const RenderTableCell*) const;
235 typedef Vector<CollapsedBorderValue> CollapsedBorderValues;
236 void invalidateCollapsedBorders()
238 m_collapsedBordersValid = false;
239 m_collapsedBorders.clear();
241 const CollapsedBorderValue* currentBorderValue() const { return m_currentBorder; }
243 bool hasSections() const { return m_head || m_foot || m_firstBody; }
245 void recalcSectionsIfNeeded() const
247 if (m_needsSectionRecalc)
251 static RenderTable* createAnonymousWithParentRenderer(const RenderObject*);
252 virtual RenderBox* createAnonymousBoxWithSameTypeAs(const RenderObject* parent) const OVERRIDE
254 return createAnonymousWithParentRenderer(parent);
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*);
266 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
267 virtual void simplifiedNormalFlowLayout();
270 virtual const char* renderName() const { return "RenderTable"; }
272 virtual bool isTable() const { return true; }
274 virtual bool avoidsFloats() const { return true; }
276 virtual void paint(PaintInfo&, const LayoutPoint&);
277 virtual void paintObject(PaintInfo&, const LayoutPoint&);
278 virtual void paintBoxDecorations(PaintInfo&, const LayoutPoint&);
279 virtual void paintMask(PaintInfo&, const LayoutPoint&);
280 virtual void layout();
281 virtual void computeIntrinsicLogicalWidths(LayoutUnit& minWidth, LayoutUnit& maxWidth) const OVERRIDE;
282 virtual void computePreferredLogicalWidths() OVERRIDE;
283 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) OVERRIDE;
285 virtual int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const OVERRIDE;
286 virtual int firstLineBoxBaseline() const OVERRIDE;
287 virtual int inlineBlockBaseline(LineDirectionMode) const OVERRIDE;
289 RenderTableCol* slowColElement(unsigned col, bool* startEdge, bool* endEdge) const;
291 void updateColumnCache() const;
292 void invalidateCachedColumns();
294 virtual RenderBlock* firstLineBlock() const;
295 virtual void updateFirstLetter();
297 virtual void updateLogicalWidth() OVERRIDE;
299 LayoutUnit convertStyleLogicalWidthToComputedWidth(const Length& styleLogicalWidth, LayoutUnit availableWidth);
300 LayoutUnit convertStyleLogicalHeightToComputedHeight(const Length& styleLogicalHeight);
302 virtual LayoutRect overflowClipRect(const LayoutPoint& location, RenderRegion*, OverlayScrollbarSizeRelevancy = IgnoreOverlayScrollbarSize, PaintPhase = PaintPhaseBlockBackground);
303 virtual LayoutRect overflowClipRectForChildLayers(const LayoutPoint& location, RenderRegion* region, OverlayScrollbarSizeRelevancy relevancy) { return RenderBox::overflowClipRect(location, region, relevancy); }
305 virtual void addOverflowFromChildren();
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;
342 inline RenderTableSection* RenderTable::topSection() const
344 ASSERT(!needsSectionRecalc());
352 inline RenderTable* toRenderTable(RenderObject* object)
354 ASSERT_WITH_SECURITY_IMPLICATION(!object || object->isTable());
355 return static_cast<RenderTable*>(object);
358 inline const RenderTable* toRenderTable(const RenderObject* object)
360 ASSERT_WITH_SECURITY_IMPLICATION(!object || object->isTable());
361 return static_cast<const RenderTable*>(object);
364 // This will catch anyone doing an unnecessary cast.
365 void toRenderTable(const RenderTable*);
367 } // namespace WebCore
369 #endif // RenderTable_h