Introduce RenderTreeBuilder
[WebKit-https.git] / Source / WebCore / rendering / RenderTableRow.cpp
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, 2007, 2008, 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 #include "config.h"
26 #include "RenderTableRow.h"
27
28 #include "Document.h"
29 #include "HTMLNames.h"
30 #include "HitTestResult.h"
31 #include "LayoutState.h"
32 #include "PaintInfo.h"
33 #include "RenderTableCell.h"
34 #include "RenderTreeBuilder.h"
35 #include "RenderView.h"
36 #include "StyleInheritedData.h"
37 #include <wtf/IsoMallocInlines.h>
38 #include <wtf/StackStats.h>
39
40 namespace WebCore {
41
42 using namespace HTMLNames;
43
44 WTF_MAKE_ISO_ALLOCATED_IMPL(RenderTableRow);
45
46 RenderTableRow::RenderTableRow(Element& element, RenderStyle&& style)
47     : RenderBox(element, WTFMove(style), 0)
48     , m_rowIndex(unsetRowIndex)
49 {
50     setInline(false);
51 }
52
53 RenderTableRow::RenderTableRow(Document& document, RenderStyle&& style)
54     : RenderBox(document, WTFMove(style), 0)
55     , m_rowIndex(unsetRowIndex)
56 {
57     setInline(false);
58 }
59
60 void RenderTableRow::willBeRemovedFromTree()
61 {
62     RenderBox::willBeRemovedFromTree();
63
64     section()->setNeedsCellRecalc();
65 }
66
67 static bool borderWidthChanged(const RenderStyle* oldStyle, const RenderStyle* newStyle)
68 {
69     return oldStyle->borderLeftWidth() != newStyle->borderLeftWidth()
70         || oldStyle->borderTopWidth() != newStyle->borderTopWidth()
71         || oldStyle->borderRightWidth() != newStyle->borderRightWidth()
72         || oldStyle->borderBottomWidth() != newStyle->borderBottomWidth();
73 }
74
75 void RenderTableRow::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
76 {
77     ASSERT(style().display() == TABLE_ROW);
78
79     RenderBox::styleDidChange(diff, oldStyle);
80     propagateStyleToAnonymousChildren(PropagateToAllChildren);
81
82     if (section() && oldStyle && style().logicalHeight() != oldStyle->logicalHeight())
83         section()->rowLogicalHeightChanged(rowIndex());
84
85     // If border was changed, notify table.
86     if (RenderTable* table = this->table()) {
87         if (oldStyle && oldStyle->border() != style().border())
88             table->invalidateCollapsedBorders();
89
90         if (oldStyle && diff == StyleDifferenceLayout && needsLayout() && table->collapseBorders() && borderWidthChanged(oldStyle, &style())) {
91             // If the border width changes on a row, we need to make sure the cells in the row know to lay out again.
92             // This only happens when borders are collapsed, since they end up affecting the border sides of the cell
93             // itself.
94             for (RenderTableCell* cell = firstCell(); cell; cell = cell->nextCell())
95                 cell->setChildNeedsLayout(MarkOnlyThis);
96         }
97     }
98 }
99
100 const BorderValue& RenderTableRow::borderAdjoiningStartCell(const RenderTableCell& cell) const
101 {
102     ASSERT_UNUSED(cell, cell.isFirstOrLastCellInRow());
103     // FIXME: https://webkit.org/b/79272 - Add support for mixed directionality at the cell level.
104     return style().borderStart();
105 }
106
107 const BorderValue& RenderTableRow::borderAdjoiningEndCell(const RenderTableCell& cell) const
108 {
109     ASSERT_UNUSED(cell, cell.isFirstOrLastCellInRow());
110     // FIXME: https://webkit.org/b/79272 - Add support for mixed directionality at the cell level.
111     return style().borderEnd();
112 }
113
114 void RenderTableRow::addChild(RenderTreeBuilder& builder, RenderPtr<RenderObject> child, RenderObject* beforeChild)
115 {
116     if (!is<RenderTableCell>(*child)) {
117         RenderObject* last = beforeChild;
118         if (!last)
119             last = lastCell();
120         if (last && last->isAnonymous() && is<RenderTableCell>(*last) && !last->isBeforeOrAfterContent()) {
121             RenderTableCell& cell = downcast<RenderTableCell>(*last);
122             if (beforeChild == &cell)
123                 beforeChild = cell.firstChild();
124             builder.insertChild(cell, WTFMove(child), beforeChild);
125             return;
126         }
127
128         if (beforeChild && !beforeChild->isAnonymous() && beforeChild->parent() == this) {
129             RenderObject* cell = beforeChild->previousSibling();
130             if (is<RenderTableCell>(cell) && cell->isAnonymous()) {
131                 builder.insertChild(downcast<RenderTableCell>(*cell), WTFMove(child));
132                 return;
133             }
134         }
135
136         // Try to find an anonymous container for the child.
137         if (last && last->parent() && last->parent()->isAnonymous() && !last->parent()->isBeforeOrAfterContent()) {
138             // If beforeChild is inside an anonymous cell, insert into the cell.
139             if (!is<RenderTableCell>(*last)) {
140                 builder.insertChild(*last->parent(), WTFMove(child), beforeChild);
141                 return;
142             }
143             // If beforeChild is inside an anonymous row, insert into the row.
144             auto& parent = *last->parent();
145             if (is<RenderTableRow>(parent)) {
146                 auto newCell = RenderTableCell::createAnonymousWithParentRenderer(*this);
147                 auto& cell = *newCell;
148                 builder.insertChild(parent, WTFMove(newCell), beforeChild);
149                 builder.insertChild(cell, WTFMove(child));
150                 return;
151             }
152         }
153         auto newCell = RenderTableCell::createAnonymousWithParentRenderer(*this);
154         auto& cell = *newCell;
155         builder.insertChild(*this, WTFMove(newCell), beforeChild);
156         builder.insertChild(cell, WTFMove(child));
157         return;
158     } 
159
160     if (beforeChild && beforeChild->parent() != this)
161         beforeChild = splitAnonymousBoxesAroundChild(beforeChild);    
162
163     RenderTableCell& cell = downcast<RenderTableCell>(*child);
164
165     // Generated content can result in us having a null section so make sure to null check our parent.
166     if (RenderTableSection* section = this->section())
167         section->addCell(&cell, this);
168
169     ASSERT(!beforeChild || is<RenderTableCell>(*beforeChild));
170     RenderBox::addChild(builder, WTFMove(child), beforeChild);
171
172     if (beforeChild || nextRow())
173         section()->setNeedsCellRecalc();
174     if (RenderTable* table = this->table())
175         table->invalidateCollapsedBorders();
176 }
177
178 void RenderTableRow::layout()
179 {
180     StackStats::LayoutCheckPoint layoutCheckPoint;
181     ASSERT(needsLayout());
182
183     // Table rows do not add translation.
184     LayoutStateMaintainer statePusher(*this, LayoutSize(), hasTransform() || hasReflection() || style().isFlippedBlocksWritingMode());
185
186     auto* layoutState = view().frameView().layoutContext().layoutState();
187     bool paginated = layoutState->isPaginated();
188                 
189     for (RenderTableCell* cell = firstCell(); cell; cell = cell->nextCell()) {
190         if (!cell->needsLayout() && paginated && (layoutState->pageLogicalHeightChanged() || (layoutState->pageLogicalHeight() && layoutState->pageLogicalOffset(cell, cell->logicalTop()) != cell->pageLogicalOffset())))
191             cell->setChildNeedsLayout(MarkOnlyThis);
192
193         if (cell->needsLayout()) {
194             cell->computeAndSetBlockDirectionMargins(*table());
195             cell->layout();
196         }
197     }
198
199     clearOverflow();
200     addVisualEffectOverflow();
201     // We only ever need to repaint if our cells didn't, which menas that they didn't need
202     // layout, so we know that our bounds didn't change. This code is just making up for
203     // the fact that we did not repaint in setStyle() because we had a layout hint.
204     // We cannot call repaint() because our clippedOverflowRectForRepaint() is taken from the
205     // parent table, and being mid-layout, that is invalid. Instead, we repaint our cells.
206     if (selfNeedsLayout() && checkForRepaintDuringLayout()) {
207         for (RenderTableCell* cell = firstCell(); cell; cell = cell->nextCell())
208             cell->repaint();
209     }
210
211     // RenderTableSection::layoutRows will set our logical height and width later, so it calls updateLayerTransform().
212     clearNeedsLayout();
213 }
214
215 LayoutRect RenderTableRow::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
216 {
217     ASSERT(parent());
218     // Rows and cells are in the same coordinate space. We need to both compute our overflow rect (which
219     // will accommodate a row outline and any visual effects on the row itself), but we also need to add in
220     // the repaint rects of cells.
221     LayoutRect result = RenderBox::clippedOverflowRectForRepaint(repaintContainer);
222     for (RenderTableCell* cell = firstCell(); cell; cell = cell->nextCell()) {
223         // Even if a cell is a repaint container, it's the row that paints the background behind it.
224         // So we don't care if a cell is a repaintContainer here.
225         result.uniteIfNonZero(cell->clippedOverflowRectForRepaint(repaintContainer));
226     }
227     return result;
228 }
229
230 // Hit Testing
231 bool RenderTableRow::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
232 {
233     // Table rows cannot ever be hit tested.  Effectively they do not exist.
234     // Just forward to our children always.
235     for (RenderTableCell* cell = lastCell(); cell; cell = cell->previousCell()) {
236         // FIXME: We have to skip over inline flows, since they can show up inside table rows
237         // at the moment (a demoted inline <form> for example). If we ever implement a
238         // table-specific hit-test method (which we should do for performance reasons anyway),
239         // then we can remove this check.
240         if (!cell->hasSelfPaintingLayer()) {
241             LayoutPoint cellPoint = flipForWritingModeForChild(cell, accumulatedOffset);
242             if (cell->nodeAtPoint(request, result, locationInContainer, cellPoint, action)) {
243                 updateHitTestResult(result, locationInContainer.point() - toLayoutSize(cellPoint));
244                 return true;
245             }
246         }
247     }
248
249     return false;
250 }
251
252 void RenderTableRow::paintOutlineForRowIfNeeded(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
253 {
254     LayoutPoint adjustedPaintOffset = paintOffset + location();
255     PaintPhase paintPhase = paintInfo.phase;
256     if ((paintPhase == PaintPhaseOutline || paintPhase == PaintPhaseSelfOutline) && style().visibility() == VISIBLE)
257         paintOutline(paintInfo, LayoutRect(adjustedPaintOffset, size()));
258 }
259
260 void RenderTableRow::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
261 {
262     ASSERT(hasSelfPaintingLayer());
263
264     paintOutlineForRowIfNeeded(paintInfo, paintOffset);
265     for (RenderTableCell* cell = firstCell(); cell; cell = cell->nextCell()) {
266         // Paint the row background behind the cell.
267         if (paintInfo.phase == PaintPhaseBlockBackground || paintInfo.phase == PaintPhaseChildBlockBackground)
268             cell->paintBackgroundsBehindCell(paintInfo, paintOffset, this);
269         if (!cell->hasSelfPaintingLayer())
270             cell->paint(paintInfo, paintOffset);
271     }
272 }
273
274 void RenderTableRow::imageChanged(WrappedImagePtr, const IntRect*)
275 {
276     // FIXME: Examine cells and repaint only the rect the image paints in.
277     repaint();
278 }
279
280 RenderPtr<RenderTableRow> RenderTableRow::createTableRowWithStyle(Document& document, const RenderStyle& style)
281 {
282     auto row = createRenderer<RenderTableRow>(document, RenderStyle::createAnonymousStyleWithDisplay(style, TABLE_ROW));
283     row->initializeStyle();
284     return row;
285 }
286
287 RenderPtr<RenderTableRow> RenderTableRow::createAnonymousWithParentRenderer(const RenderTableSection& parent)
288 {
289     return RenderTableRow::createTableRowWithStyle(parent.document(), parent.style());
290 }
291
292 void RenderTableRow::collapseAndDestroyAnonymousSiblingRows()
293 {
294     auto* section = this->section();
295     if (!section)
296         return;
297
298     // All siblings generated?
299     for (auto* current = section->firstRow(); current; current = current->nextRow()) {
300         if (current == this)
301             continue;
302         if (!current->isAnonymous())
303             return;
304     }
305
306     RenderTableRow* rowToInsertInto = nullptr;
307     auto* currentRow = section->firstRow();
308     while (currentRow) {
309         if (currentRow == this) {
310             currentRow = currentRow->nextRow();
311             continue;
312         }
313         if (!rowToInsertInto) {
314             rowToInsertInto = currentRow;
315             currentRow = currentRow->nextRow();
316             continue;
317         }
318         currentRow->moveAllChildrenTo(rowToInsertInto, RenderBoxModelObject::NormalizeAfterInsertion::No);
319         auto toDestroy = section->takeChild(*currentRow);
320         currentRow = currentRow->nextRow();
321     }
322     if (rowToInsertInto)
323         rowToInsertInto->setNeedsLayout();
324 }
325
326 } // namespace WebCore