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, 2013 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 "core/rendering/RenderBlockFlow.h"
29 #include "core/rendering/RenderTableRow.h"
30 #include "core/rendering/RenderTableSection.h"
31 #include "platform/LengthFunctions.h"
35 static const unsigned unsetColumnIndex = 0x1FFFFFFF;
36 static const unsigned maxColumnIndex = 0x1FFFFFFE; // 536,870,910
38 enum IncludeBorderColorOrNot { DoNotIncludeBorderColor, IncludeBorderColor };
40 class SubtreeLayoutScope;
42 class RenderTableCell FINAL : public RenderBlockFlow {
44 explicit RenderTableCell(Element*);
46 unsigned colSpan() const
50 return parseColSpanFromDOM();
52 unsigned rowSpan() const
56 return parseRowSpanFromDOM();
59 // Called from HTMLTableCellElement.
60 void colSpanOrRowSpanChanged();
62 void setCol(unsigned column)
64 if (UNLIKELY(column > maxColumnIndex))
72 ASSERT(m_column != unsetColumnIndex);
76 RenderTableRow* row() const { return toRenderTableRow(parent()); }
77 RenderTableSection* section() const { return toRenderTableSection(parent()->parent()); }
78 RenderTable* table() const { return toRenderTable(parent()->parent()->parent()); }
80 RenderTableCell* previousCell() const;
81 RenderTableCell* nextCell() const;
83 unsigned rowIndex() const
85 // This function shouldn't be called on a detached cell.
87 return row()->rowIndex();
90 Length styleOrColLogicalWidth() const
92 Length styleWidth = style()->logicalWidth();
93 if (!styleWidth.isAuto())
95 if (RenderTableCol* firstColumn = table()->colElement(col()))
96 return logicalWidthFromColumns(firstColumn, styleWidth);
100 int logicalHeightForRowSizing() const
102 // FIXME: This function does too much work, and is very hot during table layout!
103 int adjustedLogicalHeight = pixelSnappedLogicalHeight() - (intrinsicPaddingBefore() + intrinsicPaddingAfter());
104 int styleLogicalHeight = valueForLength(style()->logicalHeight(), 0);
105 // In strict mode, box-sizing: content-box do the right thing and actually add in the border and padding.
106 // Call computedCSSPadding* directly to avoid including implicitPadding.
107 if (!document().inQuirksMode() && style()->boxSizing() != BORDER_BOX)
108 styleLogicalHeight += (computedCSSPaddingBefore() + computedCSSPaddingAfter()).floor() + borderBefore() + borderAfter();
109 return max(styleLogicalHeight, adjustedLogicalHeight);
113 void setCellLogicalWidth(int constrainedLogicalWidth, SubtreeLayoutScope&);
115 virtual int borderLeft() const OVERRIDE;
116 virtual int borderRight() const OVERRIDE;
117 virtual int borderTop() const OVERRIDE;
118 virtual int borderBottom() const OVERRIDE;
119 virtual int borderStart() const OVERRIDE;
120 virtual int borderEnd() const OVERRIDE;
121 virtual int borderBefore() const OVERRIDE;
122 virtual int borderAfter() const OVERRIDE;
124 void collectBorderValues(RenderTable::CollapsedBorderValues&) const;
125 static void sortBorderValues(RenderTable::CollapsedBorderValues&);
127 virtual void layout() OVERRIDE;
129 virtual void paint(PaintInfo&, const LayoutPoint&) OVERRIDE;
131 void paintCollapsedBorders(PaintInfo&, const LayoutPoint&);
132 void paintBackgroundsBehindCell(PaintInfo&, const LayoutPoint&, RenderObject* backgroundObject);
134 LayoutUnit cellBaselinePosition() const;
135 bool isBaselineAligned() const
137 EVerticalAlign va = style()->verticalAlign();
138 return va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB || va == LENGTH;
141 void computeIntrinsicPadding(int rowHeight, SubtreeLayoutScope&);
142 void clearIntrinsicPadding() { setIntrinsicPadding(0, 0); }
144 int intrinsicPaddingBefore() const { return m_intrinsicPaddingBefore; }
145 int intrinsicPaddingAfter() const { return m_intrinsicPaddingAfter; }
147 virtual LayoutUnit paddingTop() const OVERRIDE;
148 virtual LayoutUnit paddingBottom() const OVERRIDE;
149 virtual LayoutUnit paddingLeft() const OVERRIDE;
150 virtual LayoutUnit paddingRight() const OVERRIDE;
152 // FIXME: For now we just assume the cell has the same block flow direction as the table. It's likely we'll
153 // create an extra anonymous RenderBlock to handle mixing directionality anyway, in which case we can lock
154 // the block flow directionality of the cells to the table's directionality.
155 virtual LayoutUnit paddingBefore() const OVERRIDE;
156 virtual LayoutUnit paddingAfter() const OVERRIDE;
158 void setOverrideLogicalContentHeightFromRowHeight(LayoutUnit);
160 virtual void scrollbarsChanged(bool horizontalScrollbarChanged, bool verticalScrollbarChanged) OVERRIDE;
162 bool cellWidthChanged() const { return m_cellWidthChanged; }
163 void setCellWidthChanged(bool b = true) { m_cellWidthChanged = b; }
165 static RenderTableCell* createAnonymous(Document*);
166 static RenderTableCell* createAnonymousWithParentRenderer(const RenderObject*);
167 virtual RenderBox* createAnonymousBoxWithSameTypeAs(const RenderObject* parent) const OVERRIDE
169 return createAnonymousWithParentRenderer(parent);
172 // This function is used to unify which table part's style we use for computing direction and
173 // writing mode. Writing modes are not allowed on row group and row but direction is.
174 // This means we can safely use the same style in all cases to simplify our code.
175 // FIXME: Eventually this function should replaced by style() once we support direction
176 // on all table parts and writing-mode on cells.
177 const RenderStyle* styleForCellFlow() const
179 return row()->style();
182 const BorderValue& borderAdjoiningTableStart() const
184 ASSERT(isFirstOrLastCellInRow());
185 if (section()->hasSameDirectionAs(table()))
186 return style()->borderStart();
188 return style()->borderEnd();
191 const BorderValue& borderAdjoiningTableEnd() const
193 ASSERT(isFirstOrLastCellInRow());
194 if (section()->hasSameDirectionAs(table()))
195 return style()->borderEnd();
197 return style()->borderStart();
200 const BorderValue& borderAdjoiningCellBefore(const RenderTableCell* cell)
202 ASSERT_UNUSED(cell, table()->cellAfter(cell) == this);
203 // FIXME: https://webkit.org/b/79272 - Add support for mixed directionality at the cell level.
204 return style()->borderStart();
207 const BorderValue& borderAdjoiningCellAfter(const RenderTableCell* cell)
209 ASSERT_UNUSED(cell, table()->cellBefore(cell) == this);
210 // FIXME: https://webkit.org/b/79272 - Add support for mixed directionality at the cell level.
211 return style()->borderEnd();
215 bool isFirstOrLastCellInRow() const
217 return !table()->cellAfter(this) || !table()->cellBefore(this);
221 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) OVERRIDE;
222 virtual void computePreferredLogicalWidths() OVERRIDE;
224 virtual void addLayerHitTestRects(LayerHitTestRects&, const RenderLayer* currentCompositedLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const OVERRIDE;
227 virtual const char* renderName() const OVERRIDE { return (isAnonymous() || isPseudoElement()) ? "RenderTableCell (anonymous)" : "RenderTableCell"; }
229 virtual bool isTableCell() const OVERRIDE { return true; }
231 virtual void willBeRemovedFromTree() OVERRIDE;
233 virtual void updateLogicalWidth() OVERRIDE;
235 virtual void paintBoxDecorationBackground(PaintInfo&, const LayoutPoint&) OVERRIDE;
236 virtual void paintMask(PaintInfo&, const LayoutPoint&) OVERRIDE;
238 virtual bool boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance, InlineFlowBox*) const OVERRIDE;
240 virtual LayoutSize offsetFromContainer(const RenderObject*, const LayoutPoint&, bool* offsetDependsOnPoint = 0) const OVERRIDE;
241 virtual LayoutRect clippedOverflowRectForPaintInvalidation(const RenderLayerModelObject* paintInvalidationContainer, const PaintInvalidationState* = 0) const OVERRIDE;
242 virtual void mapRectToPaintInvalidationBacking(const RenderLayerModelObject* paintInvalidationContainer, LayoutRect&, ViewportConstrainedPosition, const PaintInvalidationState*) const OVERRIDE;
244 int borderHalfLeft(bool outer) const;
245 int borderHalfRight(bool outer) const;
246 int borderHalfTop(bool outer) const;
247 int borderHalfBottom(bool outer) const;
249 int borderHalfStart(bool outer) const;
250 int borderHalfEnd(bool outer) const;
251 int borderHalfBefore(bool outer) const;
252 int borderHalfAfter(bool outer) const;
254 void setIntrinsicPaddingBefore(int p) { m_intrinsicPaddingBefore = p; }
255 void setIntrinsicPaddingAfter(int p) { m_intrinsicPaddingAfter = p; }
256 void setIntrinsicPadding(int before, int after) { setIntrinsicPaddingBefore(before); setIntrinsicPaddingAfter(after); }
258 bool hasStartBorderAdjoiningTable() const;
259 bool hasEndBorderAdjoiningTable() const;
261 CollapsedBorderValue collapsedStartBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
262 CollapsedBorderValue collapsedEndBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
263 CollapsedBorderValue collapsedBeforeBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
264 CollapsedBorderValue collapsedAfterBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
266 CollapsedBorderValue cachedCollapsedLeftBorder(const RenderStyle*) const;
267 CollapsedBorderValue cachedCollapsedRightBorder(const RenderStyle*) const;
268 CollapsedBorderValue cachedCollapsedTopBorder(const RenderStyle*) const;
269 CollapsedBorderValue cachedCollapsedBottomBorder(const RenderStyle*) const;
271 CollapsedBorderValue computeCollapsedStartBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
272 CollapsedBorderValue computeCollapsedEndBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
273 CollapsedBorderValue computeCollapsedBeforeBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
274 CollapsedBorderValue computeCollapsedAfterBorder(IncludeBorderColorOrNot = IncludeBorderColor) const;
276 Length logicalWidthFromColumns(RenderTableCol* firstColForThisCell, Length widthFromStyle) const;
278 void updateColAndRowSpanFlags();
280 unsigned parseRowSpanFromDOM() const;
281 unsigned parseColSpanFromDOM() const;
283 void nextSibling() const WTF_DELETED_FUNCTION;
284 void previousSibling() const WTF_DELETED_FUNCTION;
286 // Note MSVC will only pack members if they have identical types, hence we use unsigned instead of bool here.
287 unsigned m_column : 29;
288 unsigned m_cellWidthChanged : 1;
289 unsigned m_hasColSpan: 1;
290 unsigned m_hasRowSpan: 1;
291 int m_intrinsicPaddingBefore;
292 int m_intrinsicPaddingAfter;
295 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderTableCell, isTableCell());
297 inline RenderTableCell* RenderTableCell::previousCell() const
299 return toRenderTableCell(RenderObject::previousSibling());
302 inline RenderTableCell* RenderTableCell::nextCell() const
304 return toRenderTableCell(RenderObject::nextSibling());
307 inline RenderTableCell* RenderTableRow::firstCell() const
309 ASSERT(children() == virtualChildren());
310 return toRenderTableCell(children()->firstChild());
313 inline RenderTableCell* RenderTableRow::lastCell() const
315 ASSERT(children() == virtualChildren());
316 return toRenderTableCell(children()->lastChild());
321 #endif // RenderTableCell_h