Crash in WebCore::RenderTableSection::addChild due to assert failure
[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 "CachedImage.h"
29 #include "Document.h"
30 #include "HTMLNames.h"
31 #include "PaintInfo.h"
32 #include "RenderTableCell.h"
33 #include "RenderView.h"
34
35 namespace WebCore {
36
37 using namespace HTMLNames;
38
39 RenderTableRow::RenderTableRow(Node* node)
40     : RenderBox(node)
41 {
42     // init RenderObject attributes
43     setInline(false);   // our object is not Inline
44 }
45
46 void RenderTableRow::willBeDestroyed()
47 {
48     RenderTableSection* recalcSection = section();
49     
50     RenderBox::willBeDestroyed();
51     
52     if (recalcSection)
53         recalcSection->setNeedsCellRecalc();
54 }
55
56 void RenderTableRow::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
57 {
58     if (section() && style() && style()->logicalHeight() != newStyle->logicalHeight())
59         section()->setNeedsCellRecalc();
60
61     ASSERT(newStyle->display() == TABLE_ROW);
62
63     RenderBox::styleWillChange(diff, newStyle);
64 }
65
66 void RenderTableRow::updateBeforeAndAfterContent()
67 {
68     if (!isAnonymous() && document()->usesBeforeAfterRules()) {
69         children()->updateBeforeAfterContent(this, BEFORE);
70         children()->updateBeforeAfterContent(this, AFTER);
71     }
72 }
73
74 void RenderTableRow::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
75 {
76     RenderBox::styleDidChange(diff, oldStyle);
77     propagateStyleToAnonymousChildren();
78
79     if (parent())
80         updateBeforeAndAfterContent();
81
82     // If border was changed, notify table.
83     if (parent()) {
84         RenderTable* table = this->table();
85         if (table && !table->selfNeedsLayout() && !table->normalChildNeedsLayout() && oldStyle && oldStyle->border() != style()->border())
86             table->invalidateCollapsedBorders();
87     }
88 }
89
90 void RenderTableRow::addChild(RenderObject* child, RenderObject* beforeChild)
91 {
92     // Make sure we don't append things after :after-generated content if we have it.
93     if (!beforeChild)
94         beforeChild = findAfterContentRenderer();
95
96     if (!child->isTableCell()) {
97         RenderObject* last = beforeChild;
98         if (!last)
99             last = lastChild();
100         if (last && last->isAnonymous() && last->isTableCell() && !last->isBeforeOrAfterContent()) {
101             if (beforeChild == last)
102                 beforeChild = last->firstChild();
103             last->addChild(child, beforeChild);
104             return;
105         }
106
107         if (beforeChild && !beforeChild->isAnonymous() && beforeChild->parent() == this) {
108             RenderObject* cell = beforeChild->previousSibling();
109             if (cell && cell->isTableCell() && cell->isAnonymous()) {
110                 cell->addChild(child);
111                 return;
112             }
113         }
114
115         // If beforeChild is inside an anonymous cell, insert into the cell.
116         if (last && !last->isTableCell() && last->parent() && last->parent()->isAnonymous() && !last->parent()->isBeforeOrAfterContent()) {
117             last->parent()->addChild(child, beforeChild);
118             return;
119         }
120
121         RenderTableCell* cell = new (renderArena()) RenderTableCell(document() /* anonymous object */);
122         RefPtr<RenderStyle> newStyle = RenderStyle::create();
123         newStyle->inheritFrom(style());
124         newStyle->setDisplay(TABLE_CELL);
125         cell->setStyle(newStyle.release());
126         addChild(cell, beforeChild);
127         cell->addChild(child);
128         return;
129     } 
130     
131     // If the next renderer is actually wrapped in an anonymous table cell, we need to go up and find that.
132     while (beforeChild && beforeChild->parent() != this)
133         beforeChild = beforeChild->parent();
134
135     RenderTableCell* cell = toRenderTableCell(child);
136
137     // Generated content can result in us having a null section so make sure to null check our parent.
138     if (parent())
139         section()->addCell(cell, this);
140
141     ASSERT(!beforeChild || beforeChild->isTableCell());
142     RenderBox::addChild(cell, beforeChild);
143
144     if (beforeChild || nextSibling())
145         section()->setNeedsCellRecalc();
146 }
147
148 void RenderTableRow::layout()
149 {
150     ASSERT(needsLayout());
151
152     // Table rows do not add translation.
153     LayoutStateMaintainer statePusher(view(), this, LayoutSize(), style()->isFlippedBlocksWritingMode());
154
155     bool paginated = view()->layoutState()->isPaginated();
156                 
157     for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
158         if (child->isTableCell()) {
159             RenderTableCell* cell = toRenderTableCell(child);
160             if (!cell->needsLayout() && paginated && view()->layoutState()->pageLogicalHeight() && view()->layoutState()->pageLogicalOffset(cell->logicalTop()) != cell->pageLogicalOffset())
161                 cell->setChildNeedsLayout(true, false);
162
163             if (child->needsLayout()) {
164                 cell->computeBlockDirectionMargins(table());
165                 cell->layout();
166             }
167         }
168     }
169
170     // We only ever need to repaint if our cells didn't, which menas that they didn't need
171     // layout, so we know that our bounds didn't change. This code is just making up for
172     // the fact that we did not repaint in setStyle() because we had a layout hint.
173     // We cannot call repaint() because our clippedOverflowRectForRepaint() is taken from the
174     // parent table, and being mid-layout, that is invalid. Instead, we repaint our cells.
175     if (selfNeedsLayout() && checkForRepaintDuringLayout()) {
176         for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
177             if (child->isTableCell())
178                 child->repaint();
179         }
180     }
181
182     statePusher.pop();
183     // RenderTableSection::layoutRows will set our logical height and width later, so it calls updateLayerTransform().
184     setNeedsLayout(false);
185 }
186
187 IntRect RenderTableRow::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer) const
188 {
189     ASSERT(parent());
190
191     if (repaintContainer == this)
192         return RenderBox::clippedOverflowRectForRepaint(repaintContainer);
193
194     // For now, just repaint the whole table.
195     // FIXME: Find a better way to do this, e.g., need to repaint all the cells that we
196     // might have propagated a background color into.
197     // FIXME: do repaintContainer checks here
198     if (RenderTable* parentTable = table())
199         return parentTable->clippedOverflowRectForRepaint(repaintContainer);
200
201     return IntRect();
202 }
203
204 // Hit Testing
205 bool RenderTableRow::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
206 {
207     // Table rows cannot ever be hit tested.  Effectively they do not exist.
208     // Just forward to our children always.
209     for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
210         // FIXME: We have to skip over inline flows, since they can show up inside table rows
211         // at the moment (a demoted inline <form> for example). If we ever implement a
212         // table-specific hit-test method (which we should do for performance reasons anyway),
213         // then we can remove this check.
214         if (child->isTableCell() && !toRenderBox(child)->hasSelfPaintingLayer()) {
215             LayoutPoint cellPoint = flipForWritingModeForChild(toRenderTableCell(child), accumulatedOffset);
216             if (child->nodeAtPoint(request, result, pointInContainer, cellPoint, action)) {
217                 updateHitTestResult(result, pointInContainer - toLayoutSize(cellPoint));
218                 return true;
219             }
220         }
221     }
222     
223     return false;
224 }
225
226 void RenderTableRow::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
227 {
228     ASSERT(hasSelfPaintingLayer());
229     if (!layer())
230         return;
231     for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
232         if (child->isTableCell()) {
233             // Paint the row background behind the cell.
234             if (paintInfo.phase == PaintPhaseBlockBackground || paintInfo.phase == PaintPhaseChildBlockBackground) {
235                 RenderTableCell* cell = toRenderTableCell(child);
236                 cell->paintBackgroundsBehindCell(paintInfo, paintOffset, this);
237             }
238             if (!toRenderBox(child)->hasSelfPaintingLayer())
239                 child->paint(paintInfo, paintOffset);
240         }
241     }
242 }
243
244 void RenderTableRow::imageChanged(WrappedImagePtr, const IntRect*)
245 {
246     // FIXME: Examine cells and repaint only the rect the image paints in.
247     repaint();
248 }
249
250 } // namespace WebCore