Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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, 2013 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 "core/rendering/RenderTableRow.h"
27
28 #include "core/HTMLNames.h"
29 #include "core/fetch/ImageResource.h"
30 #include "core/rendering/GraphicsContextAnnotator.h"
31 #include "core/rendering/HitTestResult.h"
32 #include "core/rendering/PaintInfo.h"
33 #include "core/rendering/RenderTableCell.h"
34 #include "core/rendering/RenderView.h"
35 #include "core/rendering/SubtreeLayoutScope.h"
36 #include "core/rendering/style/StyleInheritedData.h"
37
38 namespace blink {
39
40 using namespace HTMLNames;
41
42 RenderTableRow::RenderTableRow(Element* element)
43     : RenderBox(element)
44     , m_rowIndex(unsetRowIndex)
45 {
46     // init RenderObject attributes
47     setInline(false);   // our object is not Inline
48 }
49
50 void RenderTableRow::trace(Visitor* visitor)
51 {
52     visitor->trace(m_children);
53     RenderBox::trace(visitor);
54 }
55
56 void RenderTableRow::willBeRemovedFromTree()
57 {
58     RenderBox::willBeRemovedFromTree();
59
60     section()->setNeedsCellRecalc();
61 }
62
63 static bool borderWidthChanged(const RenderStyle* oldStyle, const RenderStyle* newStyle)
64 {
65     return oldStyle->borderLeftWidth() != newStyle->borderLeftWidth()
66         || oldStyle->borderTopWidth() != newStyle->borderTopWidth()
67         || oldStyle->borderRightWidth() != newStyle->borderRightWidth()
68         || oldStyle->borderBottomWidth() != newStyle->borderBottomWidth();
69 }
70
71 void RenderTableRow::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
72 {
73     ASSERT(style()->display() == TABLE_ROW);
74
75     RenderBox::styleDidChange(diff, oldStyle);
76     propagateStyleToAnonymousChildren();
77
78     if (section() && oldStyle && style()->logicalHeight() != oldStyle->logicalHeight())
79         section()->rowLogicalHeightChanged(rowIndex());
80
81     // If border was changed, notify table.
82     if (parent()) {
83         RenderTable* table = this->table();
84         if (table && !table->selfNeedsLayout() && !table->normalChildNeedsLayout() && oldStyle && oldStyle->border() != style()->border())
85             table->invalidateCollapsedBorders();
86
87         if (table && oldStyle && diff.needsFullLayout() && needsLayout() && table->collapseBorders() && borderWidthChanged(oldStyle, style())) {
88             // If the border width changes on a row, we need to make sure the cells in the row know to lay out again.
89             // This only happens when borders are collapsed, since they end up affecting the border sides of the cell
90             // itself.
91             for (RenderBox* childBox = firstChildBox(); childBox; childBox = childBox->nextSiblingBox()) {
92                 if (!childBox->isTableCell())
93                     continue;
94                 childBox->setChildNeedsLayout();
95             }
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(RenderObject* child, RenderObject* beforeChild)
115 {
116     if (!child->isTableCell()) {
117         RenderObject* last = beforeChild;
118         if (!last)
119             last = lastCell();
120         if (last && last->isAnonymous() && last->isTableCell() && !last->isBeforeOrAfterContent()) {
121             RenderTableCell* lastCell = toRenderTableCell(last);
122             if (beforeChild == lastCell)
123                 beforeChild = lastCell->firstChild();
124             lastCell->addChild(child, beforeChild);
125             return;
126         }
127
128         if (beforeChild && !beforeChild->isAnonymous() && beforeChild->parent() == this) {
129             RenderObject* cell = beforeChild->previousSibling();
130             if (cell && cell->isTableCell() && cell->isAnonymous()) {
131                 cell->addChild(child);
132                 return;
133             }
134         }
135
136         // If beforeChild is inside an anonymous cell, insert into the cell.
137         if (last && !last->isTableCell() && last->parent() && last->parent()->isAnonymous() && !last->parent()->isBeforeOrAfterContent()) {
138             last->parent()->addChild(child, beforeChild);
139             return;
140         }
141
142         RenderTableCell* cell = RenderTableCell::createAnonymousWithParentRenderer(this);
143         addChild(cell, beforeChild);
144         cell->addChild(child);
145         return;
146     }
147
148     if (beforeChild && beforeChild->parent() != this)
149         beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
150
151     RenderTableCell* cell = toRenderTableCell(child);
152
153     // Generated content can result in us having a null section so make sure to null check our parent.
154     if (parent())
155         section()->addCell(cell, this);
156
157     ASSERT(!beforeChild || beforeChild->isTableCell());
158     RenderBox::addChild(cell, beforeChild);
159
160     if (beforeChild || nextRow())
161         section()->setNeedsCellRecalc();
162 }
163
164 void RenderTableRow::layout()
165 {
166     ASSERT(needsLayout());
167
168     // Table rows do not add translation.
169     LayoutState state(*this, LayoutSize());
170
171     for (RenderTableCell* cell = firstCell(); cell; cell = cell->nextCell()) {
172         SubtreeLayoutScope layouter(*cell);
173         if (!cell->needsLayout())
174             cell->markForPaginationRelayoutIfNeeded(layouter);
175         if (cell->needsLayout()) {
176             cell->computeAndSetBlockDirectionMargins(table());
177             cell->layout();
178         }
179     }
180
181     m_overflow.clear();
182     addVisualEffectOverflow();
183
184     // We only ever need to issue paint invalidations if our cells didn't, which means that they didn't need
185     // layout, so we know that our bounds didn't change. This code is just making up for
186     // the fact that we did not invalidate paints in setStyle() because we had a layout hint.
187     // We cannot call repaint() because our clippedOverflowRectForPaintInvalidation() is taken from the
188     // parent table, and being mid-layout, that is invalid. Instead, we issue paint invalidations for our cells.
189     if (selfNeedsLayout() && checkForPaintInvalidation()) {
190         for (RenderTableCell* cell = firstCell(); cell; cell = cell->nextCell()) {
191             // FIXME: Is this needed with Repaint After Layout?
192             cell->setShouldDoFullPaintInvalidation(true);
193         }
194     }
195
196     // RenderTableSection::layoutRows will set our logical height and width later, so it calls updateLayerTransform().
197     clearNeedsLayout();
198 }
199
200 // Hit Testing
201 bool RenderTableRow::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
202 {
203     // Table rows cannot ever be hit tested.  Effectively they do not exist.
204     // Just forward to our children always.
205     for (RenderTableCell* cell = lastCell(); cell; cell = cell->previousCell()) {
206         // FIXME: We have to skip over inline flows, since they can show up inside table rows
207         // at the moment (a demoted inline <form> for example). If we ever implement a
208         // table-specific hit-test method (which we should do for performance reasons anyway),
209         // then we can remove this check.
210         if (!cell->hasSelfPaintingLayer()) {
211             LayoutPoint cellPoint = flipForWritingModeForChild(cell, accumulatedOffset);
212             if (cell->nodeAtPoint(request, result, locationInContainer, cellPoint, action)) {
213                 updateHitTestResult(result, locationInContainer.point() - toLayoutSize(cellPoint));
214                 return true;
215             }
216         }
217     }
218
219     return false;
220 }
221
222 void RenderTableRow::paintOutlineForRowIfNeeded(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
223 {
224     LayoutPoint adjustedPaintOffset = paintOffset + location();
225     PaintPhase paintPhase = paintInfo.phase;
226     if ((paintPhase == PaintPhaseOutline || paintPhase == PaintPhaseSelfOutline) && style()->visibility() == VISIBLE)
227         paintOutline(paintInfo, LayoutRect(adjustedPaintOffset, size()));
228 }
229
230 void RenderTableRow::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
231 {
232     ASSERT(hasSelfPaintingLayer());
233     ANNOTATE_GRAPHICS_CONTEXT(paintInfo, this);
234
235     paintOutlineForRowIfNeeded(paintInfo, paintOffset);
236     for (RenderTableCell* cell = firstCell(); cell; cell = cell->nextCell()) {
237         // Paint the row background behind the cell.
238         if (paintInfo.phase == PaintPhaseBlockBackground || paintInfo.phase == PaintPhaseChildBlockBackground)
239             cell->paintBackgroundsBehindCell(paintInfo, paintOffset, this);
240         if (!cell->hasSelfPaintingLayer())
241             cell->paint(paintInfo, paintOffset);
242     }
243 }
244
245 void RenderTableRow::imageChanged(WrappedImagePtr, const IntRect*)
246 {
247     // FIXME: Examine cells and repaint only the rect the image paints in.
248     paintInvalidationForWholeRenderer();
249 }
250
251 RenderTableRow* RenderTableRow::createAnonymous(Document* document)
252 {
253     RenderTableRow* renderer = new RenderTableRow(0);
254     renderer->setDocumentForAnonymous(document);
255     return renderer;
256 }
257
258 RenderTableRow* RenderTableRow::createAnonymousWithParentRenderer(const RenderObject* parent)
259 {
260     RenderTableRow* newRow = RenderTableRow::createAnonymous(&parent->document());
261     RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(parent->style(), TABLE_ROW);
262     newRow->setStyle(newStyle.release());
263     return newRow;
264 }
265
266 } // namespace blink