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/CoreExport.h"
30 #include "core/layout/LayoutBlock.h"
31 #include "core/style/CollapsedBorderValue.h"
32 #include "wtf/Vector.h"
37 class LayoutTableCaption
;
38 class LayoutTableCell
;
39 class LayoutTableSection
;
40 class TableLayoutAlgorithm
;
42 enum SkipEmptySectionsValue
{ DoNotSkipEmptySections
, SkipEmptySections
};
44 // LayoutTable is the LayoutObject associated with
45 // display: table or inline-table.
47 // LayoutTable is the master coordinator for determining the overall table
48 // structure. The reason is that LayoutTableSection children have a local
49 // view over what their structure is but don't account for other
50 // LayoutTableSection. Thus LayoutTable helps keep consistency across
51 // LayoutTableSection. See e.g. m_column below.
53 // LayoutTable expects only 3 types of children:
54 // - zero or more LayoutTableCol
55 // - zero or more LayoutTableCaption
56 // - zero or more LayoutTableSection
57 // This is aligned with what HTML5 expects:
58 // https://html.spec.whatwg.org/multipage/tables.html#the-table-element
59 // with one difference: we allow more than one caption as we follow what
60 // CSS expects (https://bugs.webkit.org/show_bug.cgi?id=69773).
61 // Those expectations are enforced by LayoutTable::addChild, that wraps unknown
62 // children into an anonymous LayoutTableSection. This is what the "generate
63 // missing child wrapper" step in CSS mandates in
64 // http://www.w3.org/TR/CSS21/tables.html#anonymous-boxes.
66 // TODO(jchaffraix): Explain more of the class structure.
67 class CORE_EXPORT LayoutTable final
: public LayoutBlock
{
69 explicit LayoutTable(Element
*);
70 ~LayoutTable() override
;
72 // Per CSS 3 writing-mode: "The first and second values of the 'border-spacing' property represent spacing between columns
73 // and rows respectively, not necessarily the horizontal and vertical spacing respectively".
74 int hBorderSpacing() const { return m_hSpacing
; }
75 int vBorderSpacing() const { return m_vSpacing
; }
77 bool collapseBorders() const { return style()->borderCollapse(); }
79 int borderStart() const override
{ return m_borderStart
; }
80 int borderEnd() const override
{ return m_borderEnd
; }
81 int borderBefore() const override
;
82 int borderAfter() const override
;
84 int borderLeft() const override
86 if (style()->isHorizontalWritingMode())
87 return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
88 return style()->isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
91 int borderRight() const override
93 if (style()->isHorizontalWritingMode())
94 return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
95 return style()->isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
98 int borderTop() const override
100 if (style()->isHorizontalWritingMode())
101 return style()->isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
102 return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
105 int borderBottom() const override
107 if (style()->isHorizontalWritingMode())
108 return style()->isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
109 return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
112 int outerBorderBefore() const;
113 int outerBorderAfter() const;
114 int outerBorderStart() const;
115 int outerBorderEnd() const;
117 int outerBorderLeft() const
119 if (style()->isHorizontalWritingMode())
120 return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
121 return style()->isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
124 int outerBorderRight() const
126 if (style()->isHorizontalWritingMode())
127 return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
128 return style()->isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
131 int outerBorderTop() const
133 if (style()->isHorizontalWritingMode())
134 return style()->isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
135 return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
138 int outerBorderBottom() const
140 if (style()->isHorizontalWritingMode())
141 return style()->isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
142 return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
145 int calcBorderStart() const;
146 int calcBorderEnd() const;
147 void recalcBordersInRowDirection();
149 void addChild(LayoutObject
* child
, LayoutObject
* beforeChild
= nullptr) override
;
150 void addChildIgnoringContinuation(LayoutObject
* newChild
, LayoutObject
* beforeChild
= nullptr) override
;
152 struct ColumnStruct
{
153 ALLOW_ONLY_INLINE_ALLOCATION();
154 explicit ColumnStruct(unsigned initialSpan
= 1)
162 void forceSectionsRecalc()
164 setNeedsSectionRecalc();
168 const Vector
<ColumnStruct
>& columns() const { return m_columns
; }
169 const Vector
<int>& columnPositions() const { return m_columnPos
; }
170 void setColumnPosition(unsigned index
, int position
)
172 // Note that if our horizontal border-spacing changed, our position will change but not
173 // our column's width. In practice, horizontal border-spacing won't change often.
174 m_columnLogicalWidthChanged
|= m_columnPos
[index
] != position
;
175 m_columnPos
[index
] = position
;
178 LayoutTableSection
* header() const { return m_head
; }
179 LayoutTableSection
* footer() const { return m_foot
; }
180 LayoutTableSection
* firstBody() const { return m_firstBody
; }
182 // This function returns 0 if the table has no section.
183 LayoutTableSection
* topSection() const;
184 LayoutTableSection
* bottomSection() const;
186 // This function returns 0 if the table has no non-empty sections.
187 LayoutTableSection
* topNonEmptySection() const;
189 unsigned lastColumnIndex() const { return numEffCols() - 1; }
191 void splitColumn(unsigned position
, unsigned firstSpan
);
192 void appendColumn(unsigned span
);
193 unsigned numEffCols() const { return m_columns
.size(); }
194 unsigned spanOfEffCol(unsigned effCol
) const { return m_columns
[effCol
].span
; }
196 unsigned colToEffCol(unsigned column
) const
198 if (!m_hasCellColspanThatDeterminesTableWidth
)
201 unsigned effColumn
= 0;
202 unsigned numColumns
= numEffCols();
203 for (unsigned c
= 0; effColumn
< numColumns
&& c
+ m_columns
[effColumn
].span
- 1 < column
; ++effColumn
)
204 c
+= m_columns
[effColumn
].span
;
208 unsigned effColToCol(unsigned effCol
) const
210 if (!m_hasCellColspanThatDeterminesTableWidth
)
214 for (unsigned i
= 0; i
< effCol
; i
++)
215 c
+= m_columns
[i
].span
;
219 LayoutUnit
borderSpacingInRowDirection() const
221 if (unsigned effectiveColumnCount
= numEffCols())
222 return static_cast<LayoutUnit
>(effectiveColumnCount
+ 1) * hBorderSpacing();
227 // Override paddingStart/End to return pixel values to match behavor of LayoutTableCell.
228 LayoutUnit
paddingEnd() const override
{ return static_cast<int>(LayoutBlock::paddingEnd()); }
229 LayoutUnit
paddingStart() const override
{ return static_cast<int>(LayoutBlock::paddingStart()); }
231 LayoutUnit
bordersPaddingAndSpacingInRowDirection() const
233 // 'border-spacing' only applies to separate borders (see 17.6.1 The separated borders model).
234 return borderStart() + borderEnd() + (collapseBorders() ? LayoutUnit() : (paddingStart() + paddingEnd() + borderSpacingInRowDirection()));
237 // Return the first column or column-group.
238 LayoutTableCol
* firstColumn() const;
240 LayoutTableCol
* colElement(unsigned col
, bool* startEdge
= nullptr, bool* endEdge
= nullptr) const
242 // The common case is to not have columns, make that case fast.
243 if (!m_hasColElements
)
245 return slowColElement(col
, startEdge
, endEdge
);
248 bool needsSectionRecalc() const { return m_needsSectionRecalc
; }
249 void setNeedsSectionRecalc()
251 if (documentBeingDestroyed())
253 m_needsSectionRecalc
= true;
254 setNeedsLayoutAndFullPaintInvalidation(LayoutInvalidationReason::TableChanged
);
257 LayoutTableSection
* sectionAbove(const LayoutTableSection
*, SkipEmptySectionsValue
= DoNotSkipEmptySections
) const;
258 LayoutTableSection
* sectionBelow(const LayoutTableSection
*, SkipEmptySectionsValue
= DoNotSkipEmptySections
) const;
260 LayoutTableCell
* cellAbove(const LayoutTableCell
*) const;
261 LayoutTableCell
* cellBelow(const LayoutTableCell
*) const;
262 LayoutTableCell
* cellBefore(const LayoutTableCell
*) const;
263 LayoutTableCell
* cellAfter(const LayoutTableCell
*) const;
265 typedef Vector
<CollapsedBorderValue
> CollapsedBorderValues
;
266 void invalidateCollapsedBorders();
268 // FIXME: This method should be moved into TablePainter.
269 const CollapsedBorderValue
* currentBorderValue() const { return m_currentBorder
; }
270 void setCurrentBorderValue(const CollapsedBorderValue
* val
) { m_currentBorder
= val
; }
272 bool hasSections() const { return m_head
|| m_foot
|| m_firstBody
; }
274 void recalcSectionsIfNeeded() const
276 if (m_needsSectionRecalc
)
280 static LayoutTable
* createAnonymousWithParent(const LayoutObject
*);
281 LayoutBox
* createAnonymousBoxWithSameTypeAs(const LayoutObject
* parent
) const override
283 return createAnonymousWithParent(parent
);
286 const BorderValue
& tableStartBorderAdjoiningCell(const LayoutTableCell
*) const;
287 const BorderValue
& tableEndBorderAdjoiningCell(const LayoutTableCell
*) const;
289 void addCaption(const LayoutTableCaption
*);
290 void removeCaption(const LayoutTableCaption
*);
291 void addColumn(const LayoutTableCol
*);
292 void removeColumn(const LayoutTableCol
*);
294 void paintBoxDecorationBackground(const PaintInfo
&, const LayoutPoint
&) final
;
296 void paintMask(const PaintInfo
&, const LayoutPoint
&) final
;
298 const CollapsedBorderValues
& collapsedBorders()
300 ASSERT(m_collapsedBordersValid
);
301 return m_collapsedBorders
;
304 void subtractCaptionRect(LayoutRect
&) const;
306 const char* name() const override
{ return "LayoutTable"; }
309 void styleDidChange(StyleDifference
, const ComputedStyle
* oldStyle
) override
;
310 void simplifiedNormalFlowLayout() override
;
311 PaintInvalidationReason
invalidatePaintIfNeeded(PaintInvalidationState
&, const LayoutBoxModelObject
& paintInvalidationContainer
) override
;
312 void invalidatePaintOfSubtreesIfNeeded(PaintInvalidationState
&) override
;
315 bool isOfType(LayoutObjectType type
) const override
{ return type
== LayoutObjectTable
|| LayoutBlock::isOfType(type
); }
317 void paintObject(const PaintInfo
&, const LayoutPoint
&) override
;
318 void layout() override
;
319 void computeIntrinsicLogicalWidths(LayoutUnit
& minWidth
, LayoutUnit
& maxWidth
) const override
;
320 void computePreferredLogicalWidths() override
;
321 bool nodeAtPoint(HitTestResult
&, const HitTestLocation
& locationInContainer
, const LayoutPoint
& accumulatedOffset
, HitTestAction
) override
;
323 int baselinePosition(FontBaseline
, bool firstLine
, LineDirectionMode
, LinePositionMode
= PositionOnContainingLine
) const override
;
324 int firstLineBoxBaseline() const override
;
325 int inlineBlockBaseline(LineDirectionMode
) const override
;
327 LayoutTableCol
* slowColElement(unsigned col
, bool* startEdge
, bool* endEdge
) const;
329 void updateColumnCache() const;
330 void invalidateCachedColumns();
332 void updateLogicalWidth() override
;
334 LayoutUnit
convertStyleLogicalWidthToComputedWidth(const Length
& styleLogicalWidth
, LayoutUnit availableWidth
);
335 LayoutUnit
convertStyleLogicalHeightToComputedHeight(const Length
& styleLogicalHeight
);
337 LayoutRect
overflowClipRect(const LayoutPoint
& location
, OverlayScrollbarSizeRelevancy
= IgnoreOverlayScrollbarSize
) const override
;
339 void addOverflowFromChildren() override
;
341 void recalcSections() const;
342 void layoutCaption(LayoutTableCaption
&);
344 void distributeExtraLogicalHeight(int extraLogicalHeight
);
346 void recalcCollapsedBordersIfNeeded();
348 mutable Vector
<int> m_columnPos
;
350 // This Vector holds the columns counts over the entire table.
352 // To save memory at the expense of massive code complexity, the code tries
353 // to coalesce columns. This means that we try to the wider column grouping
354 // seen over the LayoutTableSections.
356 // Note that this is also a defensive pattern as <td colspan="6666666666">
357 // only allocates a single entry in this Vector. This argument is weak
358 // though as we cap colspans in HTMLTableCellElement.
360 // The following example would have 2 ColumnStruct [ 3, 2 ]:
363 // <td colspan="3"></td>
364 // <td colspan="2"></td>
368 // Columns can be split if we add a row with a different colspan structure.
369 // See splitColumn and appendColumn for operations over |m_columns|.
371 // See colToEffCol for converting an absolute column index into an
372 // index into |m_columns|.
373 mutable Vector
<ColumnStruct
> m_columns
;
375 // The captions associated with this object.
376 mutable Vector
<LayoutTableCaption
*> m_captions
;
378 mutable Vector
<LayoutTableCol
*> m_columnLayoutObjects
;
380 mutable LayoutTableSection
* m_head
;
381 mutable LayoutTableSection
* m_foot
;
382 mutable LayoutTableSection
* m_firstBody
;
384 // The layout algorithm used by this table.
386 // CSS 2.1 defines 2 types of table layouts toggled with 'table-layout':
387 // fixed (TableLayoutAlgorithmFixed) and auto (TableLayoutAlgorithmAuto).
388 // See http://www.w3.org/TR/CSS21/tables.html#width-layout.
390 // The layout algorithm is delegated to TableLayoutAlgorithm. This enables
391 // changing 'table-layout' without having to reattach the <table>.
393 // As the algorithm is dependent on the style, this field is nullptr before
394 // the first style is applied in styleDidChange().
395 OwnPtr
<TableLayoutAlgorithm
> m_tableLayout
;
397 // A sorted list of all unique border values that we want to paint.
398 CollapsedBorderValues m_collapsedBorders
;
399 const CollapsedBorderValue
* m_currentBorder
;
400 bool m_collapsedBordersValid
: 1;
402 mutable bool m_hasColElements
: 1;
403 mutable bool m_needsSectionRecalc
: 1;
405 bool m_columnLogicalWidthChanged
: 1;
406 mutable bool m_columnLayoutObjectsValid
: 1;
407 mutable bool m_hasCellColspanThatDeterminesTableWidth
: 1;
408 bool hasCellColspanThatDeterminesTableWidth() const
410 for (unsigned c
= 0; c
< numEffCols(); c
++) {
411 if (m_columns
[c
].span
> 1)
423 inline LayoutTableSection
* LayoutTable::topSection() const
425 ASSERT(!needsSectionRecalc());
433 DEFINE_LAYOUT_OBJECT_TYPE_CASTS(LayoutTable
, isTable());
437 #endif // LayoutTable_h