Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderTable.h
1 /*
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.
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #ifndef RenderTable_h
26 #define RenderTable_h
27
28 #include "core/CSSPropertyNames.h"
29 #include "core/rendering/RenderBlock.h"
30 #include "core/rendering/style/CollapsedBorderValue.h"
31 #include "wtf/Vector.h"
32
33 namespace blink {
34
35 class RenderTableCol;
36 class RenderTableCaption;
37 class RenderTableCell;
38 class RenderTableSection;
39 class TableLayout;
40
41 enum SkipEmptySectionsValue { DoNotSkipEmptySections, SkipEmptySections };
42
43 class RenderTable final : public RenderBlock {
44 public:
45     explicit RenderTable(Element*);
46     virtual ~RenderTable();
47
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; }
52
53     bool collapseBorders() const { return style()->borderCollapse(); }
54
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;
59
60     virtual int borderLeft() const override
61     {
62         if (style()->isHorizontalWritingMode())
63             return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
64         return style()->slowIsFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
65     }
66
67     virtual int borderRight() const override
68     {
69         if (style()->isHorizontalWritingMode())
70             return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
71         return style()->slowIsFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
72     }
73
74     virtual int borderTop() const override
75     {
76         if (style()->isHorizontalWritingMode())
77             return style()->slowIsFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
78         return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
79     }
80
81     virtual int borderBottom() const override
82     {
83         if (style()->isHorizontalWritingMode())
84             return style()->slowIsFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
85         return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
86     }
87
88     int outerBorderBefore() const;
89     int outerBorderAfter() const;
90     int outerBorderStart() const;
91     int outerBorderEnd() const;
92
93     int outerBorderLeft() const
94     {
95         if (style()->isHorizontalWritingMode())
96             return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
97         return style()->slowIsFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
98     }
99
100     int outerBorderRight() const
101     {
102         if (style()->isHorizontalWritingMode())
103             return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
104         return style()->slowIsFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
105     }
106
107     int outerBorderTop() const
108     {
109         if (style()->isHorizontalWritingMode())
110             return style()->slowIsFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
111         return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
112     }
113
114     int outerBorderBottom() const
115     {
116         if (style()->isHorizontalWritingMode())
117             return style()->slowIsFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
118         return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
119     }
120
121     int calcBorderStart() const;
122     int calcBorderEnd() const;
123     void recalcBordersInRowDirection();
124
125     virtual void addChild(RenderObject* child, RenderObject* beforeChild = 0) override;
126
127     struct ColumnStruct {
128         explicit ColumnStruct(unsigned initialSpan = 1)
129             : span(initialSpan)
130         {
131         }
132
133         unsigned span;
134     };
135
136     void forceSectionsRecalc()
137     {
138         setNeedsSectionRecalc();
139         recalcSections();
140     }
141
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)
145     {
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;
150     }
151
152     RenderTableSection* header() const { return m_head; }
153     RenderTableSection* footer() const { return m_foot; }
154     RenderTableSection* firstBody() const { return m_firstBody; }
155
156     // This function returns 0 if the table has no section.
157     RenderTableSection* topSection() const;
158     RenderTableSection* bottomSection() const;
159
160     // This function returns 0 if the table has no non-empty sections.
161     RenderTableSection* topNonEmptySection() const;
162
163     unsigned lastColumnIndex() const { return numEffCols() - 1; }
164
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; }
169
170     unsigned colToEffCol(unsigned column) const
171     {
172         if (!m_hasCellColspanThatDeterminesTableWidth)
173             return column;
174
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;
179         return effColumn;
180     }
181
182     unsigned effColToCol(unsigned effCol) const
183     {
184         if (!m_hasCellColspanThatDeterminesTableWidth)
185             return effCol;
186
187         unsigned c = 0;
188         for (unsigned i = 0; i < effCol; i++)
189             c += m_columns[i].span;
190         return c;
191     }
192
193     LayoutUnit borderSpacingInRowDirection() const
194     {
195         if (unsigned effectiveColumnCount = numEffCols())
196             return static_cast<LayoutUnit>(effectiveColumnCount + 1) * hBorderSpacing();
197
198         return 0;
199     }
200
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()); }
204
205     LayoutUnit bordersPaddingAndSpacingInRowDirection() const
206     {
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()));
209     }
210
211     // Return the first column or column-group.
212     RenderTableCol* firstColumn() const;
213
214     RenderTableCol* colElement(unsigned col, bool* startEdge = 0, bool* endEdge = 0) const
215     {
216         // The common case is to not have columns, make that case fast.
217         if (!m_hasColElements)
218             return 0;
219         return slowColElement(col, startEdge, endEdge);
220     }
221
222     bool needsSectionRecalc() const { return m_needsSectionRecalc; }
223     void setNeedsSectionRecalc()
224     {
225         if (documentBeingDestroyed())
226             return;
227         m_needsSectionRecalc = true;
228         setNeedsLayoutAndFullPaintInvalidation();
229     }
230
231     RenderTableSection* sectionAbove(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
232     RenderTableSection* sectionBelow(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
233
234     RenderTableCell* cellAbove(const RenderTableCell*) const;
235     RenderTableCell* cellBelow(const RenderTableCell*) const;
236     RenderTableCell* cellBefore(const RenderTableCell*) const;
237     RenderTableCell* cellAfter(const RenderTableCell*) const;
238
239     typedef Vector<CollapsedBorderValue> CollapsedBorderValues;
240     void invalidateCollapsedBorders()
241     {
242         m_collapsedBordersValid = false;
243         m_collapsedBorders.clear();
244     }
245
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; }
249
250     bool hasSections() const { return m_head || m_foot || m_firstBody; }
251
252     void recalcSectionsIfNeeded() const
253     {
254         if (m_needsSectionRecalc)
255             recalcSections();
256     }
257
258     static RenderTable* createAnonymousWithParentRenderer(const RenderObject*);
259     virtual RenderBox* createAnonymousBoxWithSameTypeAs(const RenderObject* parent) const override
260     {
261         return createAnonymousWithParentRenderer(parent);
262     }
263
264     const BorderValue& tableStartBorderAdjoiningCell(const RenderTableCell*) const;
265     const BorderValue& tableEndBorderAdjoiningCell(const RenderTableCell*) const;
266
267     void addCaption(const RenderTableCaption*);
268     void removeCaption(const RenderTableCaption*);
269     void addColumn(const RenderTableCol*);
270     void removeColumn(const RenderTableCol*);
271
272     // FIXME: this method should be moved into TablePainter.
273     virtual void paintBoxDecorationBackground(PaintInfo&, const LayoutPoint&) override;
274
275     virtual void paintMask(PaintInfo&, const LayoutPoint&) override;
276
277     const CollapsedBorderValues& collapsedBorders() { return m_collapsedBorders; }
278     void subtractCaptionRect(LayoutRect&) const;
279     void recalcCollapsedBorders();
280
281 protected:
282     virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
283     virtual void simplifiedNormalFlowLayout() override;
284
285 private:
286     virtual const char* renderName() const override { return "RenderTable"; }
287
288     virtual bool isOfType(RenderObjectType type) const override { return type == RenderObjectTable || RenderBlock::isOfType(type); }
289
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;
296
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;
300
301     RenderTableCol* slowColElement(unsigned col, bool* startEdge, bool* endEdge) const;
302
303     void updateColumnCache() const;
304     void invalidateCachedColumns();
305
306     virtual void updateLogicalWidth() override;
307
308     LayoutUnit convertStyleLogicalWidthToComputedWidth(const Length& styleLogicalWidth, LayoutUnit availableWidth);
309     LayoutUnit convertStyleLogicalHeightToComputedHeight(const Length& styleLogicalHeight);
310
311     virtual LayoutRect overflowClipRect(const LayoutPoint& location, OverlayScrollbarSizeRelevancy = IgnoreOverlayScrollbarSize) override;
312
313     virtual void addOverflowFromChildren() override;
314
315     void recalcSections() const;
316     void layoutCaption(RenderTableCaption*);
317
318     void distributeExtraLogicalHeight(int extraLogicalHeight);
319
320     mutable Vector<int> m_columnPos;
321     mutable Vector<ColumnStruct> m_columns;
322     mutable Vector<RenderTableCaption*> m_captions;
323     mutable Vector<RenderTableCol*> m_columnRenderers;
324
325     mutable RenderTableSection* m_head;
326     mutable RenderTableSection* m_foot;
327     mutable RenderTableSection* m_firstBody;
328
329     OwnPtr<TableLayout> m_tableLayout;
330
331     CollapsedBorderValues m_collapsedBorders;
332     const CollapsedBorderValue* m_currentBorder;
333     bool m_collapsedBordersValid : 1;
334
335     mutable bool m_hasColElements : 1;
336     mutable bool m_needsSectionRecalc : 1;
337
338     bool m_columnLogicalWidthChanged : 1;
339     mutable bool m_columnRenderersValid: 1;
340     mutable bool m_hasCellColspanThatDeterminesTableWidth : 1;
341     bool hasCellColspanThatDeterminesTableWidth() const
342     {
343         for (unsigned c = 0; c < numEffCols(); c++) {
344             if (m_columns[c].span > 1)
345                 return true;
346         }
347         return false;
348     }
349
350     short m_hSpacing;
351     short m_vSpacing;
352     int m_borderStart;
353     int m_borderEnd;
354 };
355
356 inline RenderTableSection* RenderTable::topSection() const
357 {
358     ASSERT(!needsSectionRecalc());
359     if (m_head)
360         return m_head;
361     if (m_firstBody)
362         return m_firstBody;
363     return m_foot;
364 }
365
366 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderTable, isTable());
367
368 } // namespace blink
369
370 #endif // RenderTable_h