Child not placed correctly when beforeChild (table part)
[WebKit.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
83 void RenderTableRow::addChild(RenderObject* child, RenderObject* beforeChild)
84 {
85     // Make sure we don't append things after :after-generated content if we have it.
86     if (!beforeChild) {
87         if (RenderObject* afterContentRenderer = findAfterContentRenderer())
88             beforeChild = anonymousContainer(afterContentRenderer);
89     }
90
91     if (!child->isTableCell()) {
92         RenderObject* last = beforeChild;
93         if (!last)
94             last = lastChild();
95         if (last && last->isAnonymous() && last->isTableCell() && !last->isBeforeOrAfterContent()) {
96             if (beforeChild == last)
97                 beforeChild = last->firstChild();
98             last->addChild(child, beforeChild);
99             return;
100         }
101
102         // If beforeChild is inside an anonymous cell, insert into the cell.
103         if (last && !last->isTableCell() && last->parent() && last->parent()->isAnonymous() && !last->parent()->isBeforeOrAfterContent()) {
104             last->parent()->addChild(child, beforeChild);
105             return;
106         }
107
108         RenderTableCell* cell = new (renderArena()) RenderTableCell(document() /* anonymous object */);
109         RefPtr<RenderStyle> newStyle = RenderStyle::create();
110         newStyle->inheritFrom(style());
111         newStyle->setDisplay(TABLE_CELL);
112         cell->setStyle(newStyle.release());
113         addChild(cell, beforeChild);
114         cell->addChild(child);
115         return;
116     } 
117     
118     // If the next renderer is actually wrapped in an anonymous table cell, we need to go up and find that.
119     while (beforeChild && beforeChild->parent() != this)
120         beforeChild = beforeChild->parent();
121
122     RenderTableCell* cell = toRenderTableCell(child);
123
124     // Generated content can result in us having a null section so make sure to null check our parent.
125     if (parent())
126         section()->addCell(cell, this);
127
128     ASSERT(!beforeChild || beforeChild->isTableCell());
129     RenderBox::addChild(cell, beforeChild);
130
131     if (beforeChild || nextSibling())
132         section()->setNeedsCellRecalc();
133 }
134
135 void RenderTableRow::layout()
136 {
137     ASSERT(needsLayout());
138
139     // Table rows do not add translation.
140     LayoutStateMaintainer statePusher(view(), this, LayoutSize(), style()->isFlippedBlocksWritingMode());
141
142     bool paginated = view()->layoutState()->isPaginated();
143                 
144     for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
145         if (child->isTableCell()) {
146             RenderTableCell* cell = toRenderTableCell(child);
147             if (!cell->needsLayout() && paginated && view()->layoutState()->pageLogicalHeight() && view()->layoutState()->pageLogicalOffset(cell->logicalTop()) != cell->pageLogicalOffset())
148                 cell->setChildNeedsLayout(true, false);
149
150             if (child->needsLayout()) {
151                 cell->computeBlockDirectionMargins(table());
152                 cell->layout();
153             }
154         }
155     }
156
157     // We only ever need to repaint if our cells didn't, which menas that they didn't need
158     // layout, so we know that our bounds didn't change. This code is just making up for
159     // the fact that we did not repaint in setStyle() because we had a layout hint.
160     // We cannot call repaint() because our clippedOverflowRectForRepaint() is taken from the
161     // parent table, and being mid-layout, that is invalid. Instead, we repaint our cells.
162     if (selfNeedsLayout() && checkForRepaintDuringLayout()) {
163         for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
164             if (child->isTableCell())
165                 child->repaint();
166         }
167     }
168
169     statePusher.pop();
170     // RenderTableSection::layoutRows will set our logical height and width later, so it calls updateLayerTransform().
171     setNeedsLayout(false);
172 }
173
174 IntRect RenderTableRow::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer) const
175 {
176     ASSERT(parent());
177
178     if (repaintContainer == this)
179         return RenderBox::clippedOverflowRectForRepaint(repaintContainer);
180
181     // For now, just repaint the whole table.
182     // FIXME: Find a better way to do this, e.g., need to repaint all the cells that we
183     // might have propagated a background color into.
184     // FIXME: do repaintContainer checks here
185     if (RenderTable* parentTable = table())
186         return parentTable->clippedOverflowRectForRepaint(repaintContainer);
187
188     return IntRect();
189 }
190
191 // Hit Testing
192 bool RenderTableRow::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
193 {
194     // Table rows cannot ever be hit tested.  Effectively they do not exist.
195     // Just forward to our children always.
196     for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
197         // FIXME: We have to skip over inline flows, since they can show up inside table rows
198         // at the moment (a demoted inline <form> for example). If we ever implement a
199         // table-specific hit-test method (which we should do for performance reasons anyway),
200         // then we can remove this check.
201         if (child->isTableCell() && !toRenderBox(child)->hasSelfPaintingLayer()) {
202             LayoutPoint cellPoint = flipForWritingMode(toRenderTableCell(child), accumulatedOffset, ParentToChildFlippingAdjustment);
203             if (child->nodeAtPoint(request, result, pointInContainer, cellPoint, action)) {
204                 updateHitTestResult(result, pointInContainer - toLayoutSize(cellPoint));
205                 return true;
206             }
207         }
208     }
209     
210     return false;
211 }
212
213 void RenderTableRow::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
214 {
215     ASSERT(hasSelfPaintingLayer());
216     if (!layer())
217         return;
218     for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
219         if (child->isTableCell()) {
220             // Paint the row background behind the cell.
221             if (paintInfo.phase == PaintPhaseBlockBackground || paintInfo.phase == PaintPhaseChildBlockBackground) {
222                 RenderTableCell* cell = toRenderTableCell(child);
223                 cell->paintBackgroundsBehindCell(paintInfo, paintOffset, this);
224             }
225             if (!toRenderBox(child)->hasSelfPaintingLayer())
226                 child->paint(paintInfo, paintOffset);
227         }
228     }
229 }
230
231 void RenderTableRow::imageChanged(WrappedImagePtr, const IntRect*)
232 {
233     // FIXME: Examine cells and repaint only the rect the image paints in.
234     repaint();
235 }
236
237 } // namespace WebCore