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, 2007, 2009 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.
25 #ifndef RenderTableCell_h
26 #define RenderTableCell_h
28 #include "RenderBlockFlow.h"
29 #include "RenderTableRow.h"
30 #include "RenderTableSection.h"
34 // These is limited by the size of RenderTableCell::m_column bitfield.
35 static const unsigned unsetColumnIndex = 0x1FFFFFF;
36 static const unsigned maxColumnIndex = 0x1FFFFFE; // 33554430
38 enum IncludeBorderColorOrNot { DoNotIncludeBorderColor, IncludeBorderColor };
40 class RenderTableCell final : public RenderBlockFlow {
42 RenderTableCell(Element&, RenderStyle&&);
43 RenderTableCell(Document&, RenderStyle&&);
45 unsigned colSpan() const;
46 unsigned rowSpan() const;
48 // Called from HTMLTableCellElement.
49 void colSpanOrRowSpanChanged();
51 void setCol(unsigned column);
54 RenderTableCell* nextCell() const;
55 RenderTableCell* previousCell() const;
57 RenderTableRow* row() const { return downcast<RenderTableRow>(parent()); }
58 RenderTableSection* section() const;
59 RenderTable* table() const;
60 unsigned rowIndex() const;
61 Length styleOrColLogicalWidth() const;
62 LayoutUnit logicalHeightForRowSizing() const;
64 void setCellLogicalWidth(LayoutUnit constrainedLogicalWidth);
66 LayoutUnit borderLeft() const override;
67 LayoutUnit borderRight() const override;
68 LayoutUnit borderTop() const override;
69 LayoutUnit borderBottom() const override;
70 LayoutUnit borderStart() const override;
71 LayoutUnit borderEnd() const override;
72 LayoutUnit borderBefore() const override;
73 LayoutUnit borderAfter() const override;
75 void collectBorderValues(RenderTable::CollapsedBorderValues&) const;
76 static void sortBorderValues(RenderTable::CollapsedBorderValues&);
78 void layout() override;
80 void paint(PaintInfo&, const LayoutPoint&) override;
82 void paintCollapsedBorders(PaintInfo&, const LayoutPoint&);
83 void paintBackgroundsBehindCell(PaintInfo&, const LayoutPoint&, RenderElement* backgroundObject);
85 LayoutUnit cellBaselinePosition() const;
86 bool isBaselineAligned() const;
88 void computeIntrinsicPadding(LayoutUnit rowHeight);
89 void clearIntrinsicPadding() { setIntrinsicPadding(0, 0); }
91 LayoutUnit intrinsicPaddingBefore() const { return m_intrinsicPaddingBefore; }
92 LayoutUnit intrinsicPaddingAfter() const { return m_intrinsicPaddingAfter; }
94 LayoutUnit paddingTop() const override;
95 LayoutUnit paddingBottom() const override;
96 LayoutUnit paddingLeft() const override;
97 LayoutUnit paddingRight() const override;
99 // FIXME: For now we just assume the cell has the same block flow direction as the table. It's likely we'll
100 // create an extra anonymous RenderBlock to handle mixing directionality anyway, in which case we can lock
101 // the block flow directionality of the cells to the table's directionality.
102 LayoutUnit paddingBefore() const override;
103 LayoutUnit paddingAfter() const override;
105 void setOverrideLogicalContentHeightFromRowHeight(LayoutUnit);
107 void scrollbarsChanged(bool horizontalScrollbarChanged, bool verticalScrollbarChanged) override;
109 bool cellWidthChanged() const { return m_cellWidthChanged; }
110 void setCellWidthChanged(bool b = true) { m_cellWidthChanged = b; }
112 static RenderTableCell* createAnonymousWithParentRenderer(const RenderObject*);
113 RenderBox* createAnonymousBoxWithSameTypeAs(const RenderObject* parent) const override { return createAnonymousWithParentRenderer(parent); }
115 // This function is used to unify which table part's style we use for computing direction and
116 // writing mode. Writing modes are not allowed on row group and row but direction is.
117 // This means we can safely use the same style in all cases to simplify our code.
118 // FIXME: Eventually this function should replaced by style() once we support direction
119 // on all table parts and writing-mode on cells.
120 const RenderStyle& styleForCellFlow() const { return row()->style(); }
122 const BorderValue& borderAdjoiningTableStart() const;
123 const BorderValue& borderAdjoiningTableEnd() const;
124 const BorderValue& borderAdjoiningCellBefore(const RenderTableCell*);
125 const BorderValue& borderAdjoiningCellAfter(const RenderTableCell*);
127 using RenderBlockFlow::nodeAtPoint;
129 bool isFirstOrLastCellInRow() const { return !table()->cellAfter(this) || !table()->cellBefore(this); }
132 LayoutRect clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const override;
134 void invalidateHasEmptyCollapsedBorders();
135 void setHasEmptyCollapsedBorder(CollapsedBorderSide, bool empty) const;
138 void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
139 void computePreferredLogicalWidths() override;
142 const char* renderName() const override { return (isAnonymous() || isPseudoElement()) ? "RenderTableCell (anonymous)" : "RenderTableCell"; }
144 bool isTableCell() const override { return true; }
146 void willBeRemovedFromTree() override;
148 void updateLogicalWidth() override;
150 void paintBoxDecorations(PaintInfo&, const LayoutPoint&) override;
151 void paintMask(PaintInfo&, const LayoutPoint&) override;
153 bool boxShadowShouldBeAppliedToBackground(const LayoutPoint& paintOffset, BackgroundBleedAvoidance, InlineFlowBox*) const override;
155 LayoutSize offsetFromContainer(RenderElement&, const LayoutPoint&, bool* offsetDependsOnPoint = 0) const override;
156 LayoutRect computeRectForRepaint(const LayoutRect&, const RenderLayerModelObject* repaintContainer, RepaintContext = { }) const override;
158 LayoutUnit borderHalfLeft(bool outer) const;
159 LayoutUnit borderHalfRight(bool outer) const;
160 LayoutUnit borderHalfTop(bool outer) const;
161 LayoutUnit borderHalfBottom(bool outer) const;
163 LayoutUnit borderHalfStart(bool outer) const;
164 LayoutUnit borderHalfEnd(bool outer) const;
165 LayoutUnit borderHalfBefore(bool outer) const;
166 LayoutUnit borderHalfAfter(bool outer) const;
168 void setIntrinsicPaddingBefore(LayoutUnit p) { m_intrinsicPaddingBefore = p; }
169 void setIntrinsicPaddingAfter(LayoutUnit p) { m_intrinsicPaddingAfter = p; }
170 void setIntrinsicPadding(LayoutUnit before, LayoutUnit after) { setIntrinsicPaddingBefore(before); setIntrinsicPaddingAfter(after); }
172 bool hasStartBorderAdjoiningTable() const;
173 bool hasEndBorderAdjoiningTable() const;
175 CollapsedBorderValue collapsedStartBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
176 CollapsedBorderValue collapsedEndBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
177 CollapsedBorderValue collapsedBeforeBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
178 CollapsedBorderValue collapsedAfterBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
180 CollapsedBorderValue cachedCollapsedLeftBorder(const RenderStyle&) const;
181 CollapsedBorderValue cachedCollapsedRightBorder(const RenderStyle&) const;
182 CollapsedBorderValue cachedCollapsedTopBorder(const RenderStyle&) const;
183 CollapsedBorderValue cachedCollapsedBottomBorder(const RenderStyle&) const;
185 CollapsedBorderValue computeCollapsedStartBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
186 CollapsedBorderValue computeCollapsedEndBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
187 CollapsedBorderValue computeCollapsedBeforeBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
188 CollapsedBorderValue computeCollapsedAfterBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
190 Length logicalWidthFromColumns(RenderTableCol* firstColForThisCell, Length widthFromStyle) const;
192 void updateColAndRowSpanFlags();
194 unsigned parseRowSpanFromDOM() const;
195 unsigned parseColSpanFromDOM() const;
197 void nextSibling() const = delete;
198 void previousSibling() const = delete;
200 // Note MSVC will only pack members if they have identical types, hence we use unsigned instead of bool here.
201 unsigned m_column : 25;
202 unsigned m_cellWidthChanged : 1;
203 unsigned m_hasColSpan: 1;
204 unsigned m_hasRowSpan: 1;
205 mutable unsigned m_hasEmptyCollapsedBeforeBorder: 1;
206 mutable unsigned m_hasEmptyCollapsedAfterBorder: 1;
207 mutable unsigned m_hasEmptyCollapsedStartBorder: 1;
208 mutable unsigned m_hasEmptyCollapsedEndBorder: 1;
209 LayoutUnit m_intrinsicPaddingBefore { 0 };
210 LayoutUnit m_intrinsicPaddingAfter { 0 };
213 inline RenderTableCell* RenderTableCell::nextCell() const
215 return downcast<RenderTableCell>(RenderBlockFlow::nextSibling());
218 inline RenderTableCell* RenderTableCell::previousCell() const
220 return downcast<RenderTableCell>(RenderBlockFlow::previousSibling());
223 inline unsigned RenderTableCell::colSpan() const
227 return parseColSpanFromDOM();
230 inline unsigned RenderTableCell::rowSpan() const
234 return parseRowSpanFromDOM();
237 inline void RenderTableCell::setCol(unsigned column)
239 if (UNLIKELY(column > maxColumnIndex))
240 column = maxColumnIndex;
244 inline unsigned RenderTableCell::col() const
246 ASSERT(m_column != unsetColumnIndex);
250 inline RenderTableSection* RenderTableCell::section() const
252 RenderTableRow* row = this->row();
255 return downcast<RenderTableSection>(row->parent());
258 inline RenderTable* RenderTableCell::table() const
260 RenderTableSection* section = this->section();
263 return downcast<RenderTable>(section->parent());
266 inline unsigned RenderTableCell::rowIndex() const
268 // This function shouldn't be called on a detached cell.
270 return row()->rowIndex();
273 inline Length RenderTableCell::styleOrColLogicalWidth() const
275 Length styleWidth = style().logicalWidth();
276 if (!styleWidth.isAuto())
278 if (RenderTableCol* firstColumn = table()->colElement(col()))
279 return logicalWidthFromColumns(firstColumn, styleWidth);
283 inline LayoutUnit RenderTableCell::logicalHeightForRowSizing() const
285 // FIXME: This function does too much work, and is very hot during table layout!
286 LayoutUnit adjustedLogicalHeight = logicalHeight() - (intrinsicPaddingBefore() + intrinsicPaddingAfter());
287 LayoutUnit styleLogicalHeight = valueForLength(style().logicalHeight(), 0);
288 // In strict mode, box-sizing: content-box do the right thing and actually add in the border and padding.
289 // Call computedCSSPadding* directly to avoid including implicitPadding.
290 if (!document().inQuirksMode() && style().boxSizing() != BORDER_BOX)
291 styleLogicalHeight += computedCSSPaddingBefore() + computedCSSPaddingAfter() + borderBefore() + borderAfter();
292 return std::max(styleLogicalHeight, adjustedLogicalHeight);
295 inline bool RenderTableCell::isBaselineAligned() const
297 EVerticalAlign va = style().verticalAlign();
298 return va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB || va == LENGTH;
301 inline const BorderValue& RenderTableCell::borderAdjoiningTableStart() const
303 ASSERT(isFirstOrLastCellInRow());
304 if (section()->hasSameDirectionAs(table()))
305 return style().borderStart();
307 return style().borderEnd();
310 inline const BorderValue& RenderTableCell::borderAdjoiningTableEnd() const
312 ASSERT(isFirstOrLastCellInRow());
313 if (section()->hasSameDirectionAs(table()))
314 return style().borderEnd();
316 return style().borderStart();
319 inline const BorderValue& RenderTableCell::borderAdjoiningCellBefore(const RenderTableCell* cell)
321 ASSERT_UNUSED(cell, table()->cellAfter(cell) == this);
322 // FIXME: https://webkit.org/b/79272 - Add support for mixed directionality at the cell level.
323 return style().borderStart();
326 inline const BorderValue& RenderTableCell::borderAdjoiningCellAfter(const RenderTableCell* cell)
328 ASSERT_UNUSED(cell, table()->cellBefore(cell) == this);
329 // FIXME: https://webkit.org/b/79272 - Add support for mixed directionality at the cell level.
330 return style().borderEnd();
333 inline RenderTableCell* RenderTableRow::firstCell() const
335 return downcast<RenderTableCell>(RenderBox::firstChild());
338 inline RenderTableCell* RenderTableRow::lastCell() const
340 return downcast<RenderTableCell>(RenderBox::lastChild());
343 inline void RenderTableCell::setHasEmptyCollapsedBorder(CollapsedBorderSide side, bool empty) const
347 m_hasEmptyCollapsedAfterBorder = empty;
351 m_hasEmptyCollapsedBeforeBorder = empty;
355 m_hasEmptyCollapsedStartBorder = empty;
359 m_hasEmptyCollapsedEndBorder = empty;
364 table()->collapsedEmptyBorderIsPresent();
367 inline void RenderTableCell::invalidateHasEmptyCollapsedBorders()
369 m_hasEmptyCollapsedBeforeBorder = false;
370 m_hasEmptyCollapsedAfterBorder = false;
371 m_hasEmptyCollapsedStartBorder = false;
372 m_hasEmptyCollapsedEndBorder = false;
375 } // namespace WebCore
377 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderTableCell, isTableCell())
379 #endif // RenderTableCell_h