blob: a0b6b9e7b1b70c33e75fada975b90ad35b00de74 [file] [log] [blame]
darin@apple.com83833152008-01-14 17:51:10 +00001/*
eseidela043f3d2006-01-20 19:24:53 +00002 * 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)
mitz@apple.com8010bba2010-11-05 02:43:17 +00007 * Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010 Apple Inc. All rights reserved.
ap50b58352006-01-22 20:32:36 +00008 * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
eseidela043f3d2006-01-20 19:24:53 +00009 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
19 *
20 * You should have received a copy of the GNU Library General Public License
21 * along with this library; see the file COPYING.LIB. If not, write to
ddkilzerc8eccec2007-09-26 02:29:57 +000022 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
eseidela043f3d2006-01-20 19:24:53 +000024 */
25
26#include "config.h"
27#include "RenderTableSection.h"
bdashb07ae8d2007-02-08 04:56:20 +000028#include "CachedImage.h"
darinb9481ed2006-03-20 02:57:59 +000029#include "Document.h"
tonikitoo@webkit.org81c027d2010-07-29 13:19:43 +000030#include "HitTestResult.h"
darin98fa8b82006-03-20 08:03:57 +000031#include "HTMLNames.h"
tonyg@chromium.orgb138b1e2011-01-15 00:27:07 +000032#include "PaintInfo.h"
darin91298e52006-06-12 01:10:17 +000033#include "RenderTableCell.h"
34#include "RenderTableCol.h"
35#include "RenderTableRow.h"
weinigfef13632007-04-29 20:09:08 +000036#include "RenderView.h"
darin91298e52006-06-12 01:10:17 +000037#include <limits>
jamesr@google.com36623a32010-07-23 20:22:29 +000038#include <wtf/HashSet.h>
ddkilzer260f3d22007-01-05 05:56:31 +000039#include <wtf/Vector.h>
eseidela043f3d2006-01-20 19:24:53 +000040
darin7bd70952006-04-13 07:07:34 +000041using namespace std;
42
eseidela043f3d2006-01-20 19:24:53 +000043namespace WebCore {
44
45using namespace HTMLNames;
46
mitz@apple.com8010bba2010-11-05 02:43:17 +000047static inline void setRowLogicalHeightToRowStyleLogicalHeightIfNotRelative(RenderTableSection::RowStruct* row)
dbates@webkit.orge1131832009-11-19 00:05:57 +000048{
49 ASSERT(row && row->rowRenderer);
mitz@apple.com8010bba2010-11-05 02:43:17 +000050 row->logicalHeight = row->rowRenderer->style()->logicalHeight();
51 if (row->logicalHeight.isRelative())
52 row->logicalHeight = Length();
dbates@webkit.orge1131832009-11-19 00:05:57 +000053}
54
darinb9481ed2006-03-20 02:57:59 +000055RenderTableSection::RenderTableSection(Node* node)
hyatt@apple.comf073d9b2009-02-05 00:53:38 +000056 : RenderBox(node)
ddkilzer260f3d22007-01-05 05:56:31 +000057 , m_gridRows(0)
58 , m_cCol(0)
59 , m_cRow(-1)
mitz@apple.com8010bba2010-11-05 02:43:17 +000060 , m_outerBorderStart(0)
61 , m_outerBorderEnd(0)
62 , m_outerBorderBefore(0)
63 , m_outerBorderAfter(0)
simon.fraser@apple.com85362502009-05-08 01:23:32 +000064 , m_needsCellRecalc(false)
adeleddfe27f2007-01-05 23:03:50 +000065 , m_hasOverflowingCell(false)
jamesr@google.com36623a32010-07-23 20:22:29 +000066 , m_hasMultipleCellLevels(false)
eseidela043f3d2006-01-20 19:24:53 +000067{
68 // init RenderObject attributes
jamesr@google.com36623a32010-07-23 20:22:29 +000069 setInline(false); // our object is not Inline
eseidela043f3d2006-01-20 19:24:53 +000070}
71
72RenderTableSection::~RenderTableSection()
73{
74 clearGrid();
75}
76
77void RenderTableSection::destroy()
78{
antti078e87f2007-08-16 16:13:28 +000079 RenderTable* recalcTable = table();
80
hyatt@apple.comf073d9b2009-02-05 00:53:38 +000081 RenderBox::destroy();
antti078e87f2007-08-16 16:13:28 +000082
eseidela043f3d2006-01-20 19:24:53 +000083 // recalc cell info because RenderTable has unguarded pointers
84 // stored that point to this RenderTableSection.
antti078e87f2007-08-16 16:13:28 +000085 if (recalcTable)
86 recalcTable->setNeedsSectionRecalc();
eseidela043f3d2006-01-20 19:24:53 +000087}
88
darin316ed062006-01-23 05:29:14 +000089void RenderTableSection::addChild(RenderObject* child, RenderObject* beforeChild)
eseidela043f3d2006-01-20 19:24:53 +000090{
hyatt58028f52007-05-01 04:57:01 +000091 // Make sure we don't append things after :after-generated content if we have it.
92 if (!beforeChild && isAfterContent(lastChild()))
93 beforeChild = lastChild();
94
eseidela043f3d2006-01-20 19:24:53 +000095 if (!child->isTableRow()) {
darin316ed062006-01-23 05:29:14 +000096 RenderObject* last = beforeChild;
97 if (!last)
98 last = lastChild();
99 if (last && last->isAnonymous()) {
inferno@chromium.org947af302011-01-04 02:27:50 +0000100 if (beforeChild == last)
101 beforeChild = last->firstChild();
102 last->addChild(child, beforeChild);
darin316ed062006-01-23 05:29:14 +0000103 return;
104 }
105
darin10ab1352006-01-29 01:29:33 +0000106 // If beforeChild is inside an anonymous cell/row, insert into the cell or into
107 // the anonymous row containing it, if there is one.
darin316ed062006-01-23 05:29:14 +0000108 RenderObject* lastBox = last;
109 while (lastBox && lastBox->parent()->isAnonymous() && !lastBox->isTableRow())
110 lastBox = lastBox->parent();
111 if (lastBox && lastBox->isAnonymous()) {
112 lastBox->addChild(child, beforeChild);
113 return;
114 }
115
eric@webkit.org32963e32009-06-05 03:25:12 +0000116 RenderObject* row = new (renderArena()) RenderTableRow(document() /* anonymous table row */);
hyatt@apple.comdec0cbf22008-10-17 00:25:33 +0000117 RefPtr<RenderStyle> newStyle = RenderStyle::create();
darin316ed062006-01-23 05:29:14 +0000118 newStyle->inheritFrom(style());
119 newStyle->setDisplay(TABLE_ROW);
hyatt@apple.comdec0cbf22008-10-17 00:25:33 +0000120 row->setStyle(newStyle.release());
darin316ed062006-01-23 05:29:14 +0000121 addChild(row, beforeChild);
eseidela043f3d2006-01-20 19:24:53 +0000122 row->addChild(child);
eseidela043f3d2006-01-20 19:24:53 +0000123 return;
124 }
125
126 if (beforeChild)
ddkilzer260f3d22007-01-05 05:56:31 +0000127 setNeedsCellRecalc();
eseidela043f3d2006-01-20 19:24:53 +0000128
ddkilzer260f3d22007-01-05 05:56:31 +0000129 ++m_cRow;
130 m_cCol = 0;
eseidela043f3d2006-01-20 19:24:53 +0000131
larsb5288ad2007-01-13 18:48:29 +0000132 // make sure we have enough rows
133 if (!ensureRows(m_cRow + 1))
134 return;
135
darin@apple.com445c34f2009-08-01 00:40:58 +0000136 m_grid[m_cRow].rowRenderer = toRenderTableRow(child);
eseidela043f3d2006-01-20 19:24:53 +0000137
dbates@webkit.orge1131832009-11-19 00:05:57 +0000138 if (!beforeChild)
mitz@apple.com8010bba2010-11-05 02:43:17 +0000139 setRowLogicalHeightToRowStyleLogicalHeightIfNotRelative(&m_grid[m_cRow]);
eseidela043f3d2006-01-20 19:24:53 +0000140
ddkilzer260f3d22007-01-05 05:56:31 +0000141 // If the next renderer is actually wrapped in an anonymous table row, we need to go up and find that.
mitz@apple.comfdbd51e2008-01-18 21:52:57 +0000142 while (beforeChild && beforeChild->parent() != this)
mjs6666eee72006-07-14 07:45:13 +0000143 beforeChild = beforeChild->parent();
144
bdakin@apple.com40ae9be2009-04-16 20:48:19 +0000145 ASSERT(!beforeChild || beforeChild->isTableRow());
hyatt@apple.comf073d9b2009-02-05 00:53:38 +0000146 RenderBox::addChild(child, beforeChild);
eseidela043f3d2006-01-20 19:24:53 +0000147}
148
weinig@apple.com84974112009-02-03 06:45:48 +0000149void RenderTableSection::removeChild(RenderObject* oldChild)
150{
151 setNeedsCellRecalc();
hyatt@apple.comf073d9b2009-02-05 00:53:38 +0000152 RenderBox::removeChild(oldChild);
weinig@apple.com84974112009-02-03 06:45:48 +0000153}
154
eseidela043f3d2006-01-20 19:24:53 +0000155bool RenderTableSection::ensureRows(int numRows)
156{
ddkilzer260f3d22007-01-05 05:56:31 +0000157 int nRows = m_gridRows;
eseidela043f3d2006-01-20 19:24:53 +0000158 if (numRows > nRows) {
ddkilzer260f3d22007-01-05 05:56:31 +0000159 if (numRows > static_cast<int>(m_grid.size())) {
larsb5288ad2007-01-13 18:48:29 +0000160 size_t maxSize = numeric_limits<size_t>::max() / sizeof(RowStruct);
161 if (static_cast<size_t>(numRows) > maxSize)
eseidela043f3d2006-01-20 19:24:53 +0000162 return false;
darin@apple.com83833152008-01-14 17:51:10 +0000163 m_grid.grow(numRows);
darin91298e52006-06-12 01:10:17 +0000164 }
ddkilzer260f3d22007-01-05 05:56:31 +0000165 m_gridRows = numRows;
bdakin@apple.com5b0a3b22008-09-19 23:02:03 +0000166 int nCols = max(1, table()->numEffCols());
ap50b58352006-01-22 20:32:36 +0000167 for (int r = nRows; r < numRows; r++) {
ddkilzer260f3d22007-01-05 05:56:31 +0000168 m_grid[r].row = new Row(nCols);
ddkilzer260f3d22007-01-05 05:56:31 +0000169 m_grid[r].rowRenderer = 0;
hyatt851433b2007-05-12 06:40:14 +0000170 m_grid[r].baseline = 0;
mitz@apple.com8010bba2010-11-05 02:43:17 +0000171 m_grid[r].logicalHeight = Length();
ap50b58352006-01-22 20:32:36 +0000172 }
eseidela043f3d2006-01-20 19:24:53 +0000173 }
174
175 return true;
176}
177
hyatt@apple.comd885df72009-01-22 02:31:52 +0000178void RenderTableSection::addCell(RenderTableCell* cell, RenderTableRow* row)
eseidela043f3d2006-01-20 19:24:53 +0000179{
180 int rSpan = cell->rowSpan();
181 int cSpan = cell->colSpan();
ddkilzer260f3d22007-01-05 05:56:31 +0000182 Vector<RenderTable::ColumnStruct>& columns = table()->columns();
eseidela043f3d2006-01-20 19:24:53 +0000183 int nCols = columns.size();
184
185 // ### mozilla still seems to do the old HTML way, even for strict DTD
186 // (see the annotation on table cell layouting in the CSS specs and the testcase below:
187 // <TABLE border>
188 // <TR><TD>1 <TD rowspan="2">2 <TD>3 <TD>4
189 // <TR><TD colspan="2">5
190 // </TABLE>
jamesr@google.com36623a32010-07-23 20:22:29 +0000191 while (m_cCol < nCols && (cellAt(m_cRow, m_cCol).hasCells() || cellAt(m_cRow, m_cCol).inColSpan))
ddkilzer260f3d22007-01-05 05:56:31 +0000192 m_cCol++;
jamesr@google.comb4266232010-07-28 20:07:19 +0000193
eseidela043f3d2006-01-20 19:24:53 +0000194 if (rSpan == 1) {
ap50b58352006-01-22 20:32:36 +0000195 // we ignore height settings on rowspan cells
mitz@apple.com8010bba2010-11-05 02:43:17 +0000196 Length logicalHeight = cell->style()->logicalHeight();
197 if (logicalHeight.isPositive() || (logicalHeight.isRelative() && logicalHeight.value() >= 0)) {
198 Length cRowLogicalHeight = m_grid[m_cRow].logicalHeight;
199 switch (logicalHeight.type()) {
eseidela043f3d2006-01-20 19:24:53 +0000200 case Percent:
mitz@apple.com8010bba2010-11-05 02:43:17 +0000201 if (!(cRowLogicalHeight.isPercent()) ||
202 (cRowLogicalHeight.isPercent() && cRowLogicalHeight.rawValue() < logicalHeight.rawValue()))
203 m_grid[m_cRow].logicalHeight = logicalHeight;
eseidela043f3d2006-01-20 19:24:53 +0000204 break;
205 case Fixed:
mitz@apple.com8010bba2010-11-05 02:43:17 +0000206 if (cRowLogicalHeight.type() < Percent ||
207 (cRowLogicalHeight.isFixed() && cRowLogicalHeight.value() < logicalHeight.value()))
208 m_grid[m_cRow].logicalHeight = logicalHeight;
eseidela043f3d2006-01-20 19:24:53 +0000209 break;
210 case Relative:
211 default:
212 break;
ap50b58352006-01-22 20:32:36 +0000213 }
214 }
eseidela043f3d2006-01-20 19:24:53 +0000215 }
216
217 // make sure we have enough rows
ddkilzer260f3d22007-01-05 05:56:31 +0000218 if (!ensureRows(m_cRow + rSpan))
eseidela043f3d2006-01-20 19:24:53 +0000219 return;
220
ddkilzer260f3d22007-01-05 05:56:31 +0000221 m_grid[m_cRow].rowRenderer = row;
hyattd866e592006-03-17 09:50:35 +0000222
ddkilzer260f3d22007-01-05 05:56:31 +0000223 int col = m_cCol;
eseidela043f3d2006-01-20 19:24:53 +0000224 // tell the cell where it is
jamesr@google.com36623a32010-07-23 20:22:29 +0000225 bool inColSpan = false;
eseidela043f3d2006-01-20 19:24:53 +0000226 while (cSpan) {
jamesr@google.comb4266232010-07-28 20:07:19 +0000227 int currentSpan;
228 if (m_cCol >= nCols) {
229 table()->appendColumn(cSpan);
230 currentSpan = cSpan;
231 } else {
232 if (cSpan < (int)columns[m_cCol].span)
233 table()->splitColumn(m_cCol, cSpan);
234 currentSpan = columns[m_cCol].span;
235 }
236 for (int r = 0; r < rSpan; r++) {
237 CellStruct& c = cellAt(m_cRow + r, m_cCol);
238 ASSERT(cell);
239 c.cells.append(cell);
240 // If cells overlap then we take the slow path for painting.
241 if (c.cells.size() > 1)
242 m_hasMultipleCellLevels = true;
243 if (inColSpan)
244 c.inColSpan = true;
245 }
246 m_cCol++;
247 cSpan -= currentSpan;
248 inColSpan = true;
eseidela043f3d2006-01-20 19:24:53 +0000249 }
eric@webkit.org5832ab72009-03-25 22:15:03 +0000250 cell->setRow(m_cRow);
251 cell->setCol(table()->effColToCol(col));
eseidela043f3d2006-01-20 19:24:53 +0000252}
253
mitz@apple.com8010bba2010-11-05 02:43:17 +0000254void RenderTableSection::setCellLogicalWidths()
eseidela043f3d2006-01-20 19:24:53 +0000255{
ddkilzer260f3d22007-01-05 05:56:31 +0000256 Vector<int>& columnPos = table()->columnPositions();
eseidela043f3d2006-01-20 19:24:53 +0000257
simon.fraser@apple.comfeaef2e2008-11-07 19:17:08 +0000258 LayoutStateMaintainer statePusher(view());
259
ddkilzer260f3d22007-01-05 05:56:31 +0000260 for (int i = 0; i < m_gridRows; i++) {
261 Row& row = *m_grid[i].row;
ap50b58352006-01-22 20:32:36 +0000262 int cols = row.size();
263 for (int j = 0; j < cols; j++) {
jamesr@google.com36623a32010-07-23 20:22:29 +0000264 CellStruct& current = row[j];
265 RenderTableCell* cell = current.primaryCell();
jamesr@google.comb4266232010-07-28 20:07:19 +0000266 if (!cell || current.inColSpan)
jamesr@google.com36623a32010-07-23 20:22:29 +0000267 continue;
ap50b58352006-01-22 20:32:36 +0000268 int endCol = j;
269 int cspan = cell->colSpan();
270 while (cspan && endCol < cols) {
jamesr@google.com36623a32010-07-23 20:22:29 +0000271 ASSERT(endCol < (int)table()->columns().size());
ddkilzer260f3d22007-01-05 05:56:31 +0000272 cspan -= table()->columns()[endCol].span;
ap50b58352006-01-22 20:32:36 +0000273 endCol++;
274 }
275 int w = columnPos[endCol] - columnPos[j] - table()->hBorderSpacing();
mitz@apple.com8010bba2010-11-05 02:43:17 +0000276 int oldLogicalWidth = cell->logicalWidth();
277 if (w != oldLogicalWidth) {
ap50b58352006-01-22 20:32:36 +0000278 cell->setNeedsLayout(true);
weinigfef13632007-04-29 20:09:08 +0000279 if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout()) {
simon.fraser@apple.comfeaef2e2008-11-07 19:17:08 +0000280 if (!statePusher.didPush()) {
weinigfef13632007-04-29 20:09:08 +0000281 // Technically, we should also push state for the row, but since
282 // rows don't push a coordinate transform, that's not necessary.
hyatt@apple.coma97f4672009-01-23 03:55:34 +0000283 statePusher.push(this, IntSize(x(), y()));
weinigfef13632007-04-29 20:09:08 +0000284 }
hyattd72625b2007-04-26 19:51:11 +0000285 cell->repaint();
weinigfef13632007-04-29 20:09:08 +0000286 }
mitz@apple.com2722b372010-11-03 18:03:55 +0000287 cell->updateLogicalWidth(w);
ap50b58352006-01-22 20:32:36 +0000288 }
289 }
eseidela043f3d2006-01-20 19:24:53 +0000290 }
weinigfef13632007-04-29 20:09:08 +0000291
jamesr@google.com36623a32010-07-23 20:22:29 +0000292 statePusher.pop(); // only pops if we pushed
eseidela043f3d2006-01-20 19:24:53 +0000293}
294
mitz@apple.com8010bba2010-11-05 02:43:17 +0000295int RenderTableSection::calcRowLogicalHeight()
eseidela043f3d2006-01-20 19:24:53 +0000296{
hyatt@apple.comc93db9b2009-02-04 22:29:04 +0000297#ifndef NDEBUG
298 setNeedsLayoutIsForbidden(true);
299#endif
300
301 ASSERT(!needsLayout());
302
ddkilzer260f3d22007-01-05 05:56:31 +0000303 RenderTableCell* cell;
eseidela043f3d2006-01-20 19:24:53 +0000304
eseidela043f3d2006-01-20 19:24:53 +0000305 int spacing = table()->vBorderSpacing();
simon.fraser@apple.comfeaef2e2008-11-07 19:17:08 +0000306
307 LayoutStateMaintainer statePusher(view());
eseidela043f3d2006-01-20 19:24:53 +0000308
ddkilzer260f3d22007-01-05 05:56:31 +0000309 m_rowPos.resize(m_gridRows + 1);
310 m_rowPos[0] = spacing;
eseidela043f3d2006-01-20 19:24:53 +0000311
ddkilzer260f3d22007-01-05 05:56:31 +0000312 for (int r = 0; r < m_gridRows; r++) {
313 m_rowPos[r + 1] = 0;
hyatt777b31c2007-07-19 20:01:26 +0000314 m_grid[r].baseline = 0;
ap50b58352006-01-22 20:32:36 +0000315 int baseline = 0;
316 int bdesc = 0;
mitz@apple.com8010bba2010-11-05 02:43:17 +0000317 int ch = m_grid[r].logicalHeight.calcMinValue(0);
ddkilzer260f3d22007-01-05 05:56:31 +0000318 int pos = m_rowPos[r] + ch + (m_grid[r].rowRenderer ? spacing : 0);
eseidela043f3d2006-01-20 19:24:53 +0000319
ddkilzer260f3d22007-01-05 05:56:31 +0000320 m_rowPos[r + 1] = max(m_rowPos[r + 1], pos);
eseidela043f3d2006-01-20 19:24:53 +0000321
ddkilzer260f3d22007-01-05 05:56:31 +0000322 Row* row = m_grid[r].row;
ap50b58352006-01-22 20:32:36 +0000323 int totalCols = row->size();
eseidela043f3d2006-01-20 19:24:53 +0000324
ap50b58352006-01-22 20:32:36 +0000325 for (int c = 0; c < totalCols; c++) {
jamesr@google.com36623a32010-07-23 20:22:29 +0000326 CellStruct& current = cellAt(r, c);
327 cell = current.primaryCell();
jamesr@google.comb4266232010-07-28 20:07:19 +0000328
329 if (!cell || current.inColSpan)
ap50b58352006-01-22 20:32:36 +0000330 continue;
jamesr@google.com36623a32010-07-23 20:22:29 +0000331
332 if ((cell->row() + cell->rowSpan() - 1) > r)
ap50b58352006-01-22 20:32:36 +0000333 continue;
eseidela043f3d2006-01-20 19:24:53 +0000334
ddkilzer260f3d22007-01-05 05:56:31 +0000335 int indx = max(r - cell->rowSpan() + 1, 0);
eseidela043f3d2006-01-20 19:24:53 +0000336
337 if (cell->overrideSize() != -1) {
simon.fraser@apple.comfeaef2e2008-11-07 19:17:08 +0000338 if (!statePusher.didPush()) {
weinigfef13632007-04-29 20:09:08 +0000339 // Technically, we should also push state for the row, but since
340 // rows don't push a coordinate transform, that's not necessary.
hyatt@apple.coma97f4672009-01-23 03:55:34 +0000341 statePusher.push(this, IntSize(x(), y()));
weinigfef13632007-04-29 20:09:08 +0000342 }
eseidela043f3d2006-01-20 19:24:53 +0000343 cell->setOverrideSize(-1);
344 cell->setChildNeedsLayout(true, false);
345 cell->layoutIfNeeded();
346 }
mitz@apple.com8010bba2010-11-05 02:43:17 +0000347
348 int adjustedPaddingBefore = cell->paddingBefore() - cell->intrinsicPaddingBefore();
349 int adjustedPaddingAfter = cell->paddingAfter() - cell->intrinsicPaddingAfter();
350 int adjustedLogicalHeight = cell->logicalHeight() - (cell->intrinsicPaddingBefore() + cell->intrinsicPaddingAfter());
351
eseidela043f3d2006-01-20 19:24:53 +0000352 // Explicit heights use the border box in quirks mode. In strict mode do the right
353 // thing and actually add in the border and padding.
mitz@apple.com8010bba2010-11-05 02:43:17 +0000354 ch = cell->style()->logicalHeight().calcValue(0) +
355 (document()->inQuirksMode() ? 0 : (adjustedPaddingBefore + adjustedPaddingAfter +
356 cell->borderBefore() + cell->borderAfter()));
357 ch = max(ch, adjustedLogicalHeight);
eseidela043f3d2006-01-20 19:24:53 +0000358
ddkilzer260f3d22007-01-05 05:56:31 +0000359 pos = m_rowPos[indx] + ch + (m_grid[r].rowRenderer ? spacing : 0);
eseidela043f3d2006-01-20 19:24:53 +0000360
ddkilzer260f3d22007-01-05 05:56:31 +0000361 m_rowPos[r + 1] = max(m_rowPos[r + 1], pos);
eseidela043f3d2006-01-20 19:24:53 +0000362
ap50b58352006-01-22 20:32:36 +0000363 // find out the baseline
364 EVerticalAlign va = cell->style()->verticalAlign();
ddkilzer260f3d22007-01-05 05:56:31 +0000365 if (va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB) {
ap50b58352006-01-22 20:32:36 +0000366 int b = cell->baselinePosition();
mitz@apple.com8010bba2010-11-05 02:43:17 +0000367 if (b > cell->borderBefore() + cell->paddingBefore()) {
hyatt@apple.comd76152f2010-09-21 07:45:08 +0000368 baseline = max(baseline, b - cell->intrinsicPaddingBefore());
369 bdesc = max(bdesc, m_rowPos[indx] + ch - (b - cell->intrinsicPaddingBefore()));
eseidela043f3d2006-01-20 19:24:53 +0000370 }
ap50b58352006-01-22 20:32:36 +0000371 }
372 }
eseidela043f3d2006-01-20 19:24:53 +0000373
jamesr@google.com36623a32010-07-23 20:22:29 +0000374 // do we have baseline aligned elements?
ap50b58352006-01-22 20:32:36 +0000375 if (baseline) {
376 // increase rowheight if baseline requires
ddkilzer260f3d22007-01-05 05:56:31 +0000377 m_rowPos[r + 1] = max(m_rowPos[r + 1], baseline + bdesc + (m_grid[r].rowRenderer ? spacing : 0));
hyatt851433b2007-05-12 06:40:14 +0000378 m_grid[r].baseline = baseline;
ap50b58352006-01-22 20:32:36 +0000379 }
eseidela043f3d2006-01-20 19:24:53 +0000380
ddkilzer260f3d22007-01-05 05:56:31 +0000381 m_rowPos[r + 1] = max(m_rowPos[r + 1], m_rowPos[r]);
eseidela043f3d2006-01-20 19:24:53 +0000382 }
weinigfef13632007-04-29 20:09:08 +0000383
hyatt@apple.comc93db9b2009-02-04 22:29:04 +0000384#ifndef NDEBUG
385 setNeedsLayoutIsForbidden(false);
386#endif
387
388 ASSERT(!needsLayout());
389
simon.fraser@apple.comfeaef2e2008-11-07 19:17:08 +0000390 statePusher.pop();
mitz@apple.com440ac8c2008-04-05 16:05:08 +0000391
392 return m_rowPos[m_gridRows];
eseidela043f3d2006-01-20 19:24:53 +0000393}
394
hyatt@apple.combfda0e32009-01-31 21:20:17 +0000395void RenderTableSection::layout()
396{
397 ASSERT(needsLayout());
398
hyatt@apple.com7a97d272010-11-11 22:17:38 +0000399 LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), style()->isFlippedBlocksWritingMode());
hyatt@apple.combfda0e32009-01-31 21:20:17 +0000400 for (RenderObject* child = children()->firstChild(); child; child = child->nextSibling()) {
401 if (child->isTableRow()) {
402 child->layoutIfNeeded();
403 ASSERT(!child->needsLayout());
404 }
405 }
406 statePusher.pop();
407 setNeedsLayout(false);
408}
409
eseidela043f3d2006-01-20 19:24:53 +0000410int RenderTableSection::layoutRows(int toAdd)
411{
hyatt@apple.comc93db9b2009-02-04 22:29:04 +0000412#ifndef NDEBUG
413 setNeedsLayoutIsForbidden(true);
414#endif
415
416 ASSERT(!needsLayout());
417
eseidela043f3d2006-01-20 19:24:53 +0000418 int rHeight;
419 int rindx;
ddkilzer260f3d22007-01-05 05:56:31 +0000420 int totalRows = m_gridRows;
eseidela043f3d2006-01-20 19:24:53 +0000421
hyattd866e592006-03-17 09:50:35 +0000422 // Set the width of our section now. The rows will also be this width.
mitz@apple.com8010bba2010-11-05 02:43:17 +0000423 setLogicalWidth(table()->contentLogicalWidth());
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000424 m_overflow.clear();
adeleddfe27f2007-01-05 23:03:50 +0000425 m_hasOverflowingCell = false;
426
ddkilzer260f3d22007-01-05 05:56:31 +0000427 if (toAdd && totalRows && (m_rowPos[totalRows] || !nextSibling())) {
428 int totalHeight = m_rowPos[totalRows] + toAdd;
eseidela043f3d2006-01-20 19:24:53 +0000429
430 int dh = toAdd;
ap50b58352006-01-22 20:32:36 +0000431 int totalPercent = 0;
432 int numAuto = 0;
433 for (int r = 0; r < totalRows; r++) {
mitz@apple.com8010bba2010-11-05 02:43:17 +0000434 if (m_grid[r].logicalHeight.isAuto())
ap50b58352006-01-22 20:32:36 +0000435 numAuto++;
mitz@apple.com8010bba2010-11-05 02:43:17 +0000436 else if (m_grid[r].logicalHeight.isPercent())
437 totalPercent += m_grid[r].logicalHeight.rawValue();
ap50b58352006-01-22 20:32:36 +0000438 }
439 if (totalPercent) {
440 // try to satisfy percent
441 int add = 0;
ddkilzer260f3d22007-01-05 05:56:31 +0000442 totalPercent = min(totalPercent, 100 * percentScaleFactor);
443 int rh = m_rowPos[1] - m_rowPos[0];
ap50b58352006-01-22 20:32:36 +0000444 for (int r = 0; r < totalRows; r++) {
mitz@apple.com8010bba2010-11-05 02:43:17 +0000445 if (totalPercent > 0 && m_grid[r].logicalHeight.isPercent()) {
446 int toAdd = min(dh, (totalHeight * m_grid[r].logicalHeight.rawValue() / (100 * percentScaleFactor)) - rh);
eseidela043f3d2006-01-20 19:24:53 +0000447 // If toAdd is negative, then we don't want to shrink the row (this bug
448 // affected Outlook Web Access).
darin7bd70952006-04-13 07:07:34 +0000449 toAdd = max(0, toAdd);
ap50b58352006-01-22 20:32:36 +0000450 add += toAdd;
451 dh -= toAdd;
mitz@apple.com8010bba2010-11-05 02:43:17 +0000452 totalPercent -= m_grid[r].logicalHeight.rawValue();
ap50b58352006-01-22 20:32:36 +0000453 }
454 if (r < totalRows - 1)
ddkilzer260f3d22007-01-05 05:56:31 +0000455 rh = m_rowPos[r + 2] - m_rowPos[r + 1];
456 m_rowPos[r + 1] += add;
ap50b58352006-01-22 20:32:36 +0000457 }
458 }
459 if (numAuto) {
460 // distribute over variable cols
461 int add = 0;
462 for (int r = 0; r < totalRows; r++) {
mitz@apple.com8010bba2010-11-05 02:43:17 +0000463 if (numAuto > 0 && m_grid[r].logicalHeight.isAuto()) {
ddkilzer260f3d22007-01-05 05:56:31 +0000464 int toAdd = dh / numAuto;
ap50b58352006-01-22 20:32:36 +0000465 add += toAdd;
466 dh -= toAdd;
eseidela043f3d2006-01-20 19:24:53 +0000467 numAuto--;
ap50b58352006-01-22 20:32:36 +0000468 }
ddkilzer260f3d22007-01-05 05:56:31 +0000469 m_rowPos[r + 1] += add;
ap50b58352006-01-22 20:32:36 +0000470 }
471 }
ddkilzer260f3d22007-01-05 05:56:31 +0000472 if (dh > 0 && m_rowPos[totalRows]) {
ap50b58352006-01-22 20:32:36 +0000473 // if some left overs, distribute equally.
ddkilzer260f3d22007-01-05 05:56:31 +0000474 int tot = m_rowPos[totalRows];
eseidela043f3d2006-01-20 19:24:53 +0000475 int add = 0;
ddkilzer260f3d22007-01-05 05:56:31 +0000476 int prev = m_rowPos[0];
eseidela043f3d2006-01-20 19:24:53 +0000477 for (int r = 0; r < totalRows; r++) {
jamesr@google.com36623a32010-07-23 20:22:29 +0000478 // weight with the original height
ddkilzer260f3d22007-01-05 05:56:31 +0000479 add += dh * (m_rowPos[r + 1] - prev) / tot;
480 prev = m_rowPos[r + 1];
481 m_rowPos[r + 1] += add;
eseidela043f3d2006-01-20 19:24:53 +0000482 }
483 }
484 }
485
ddkilzer260f3d22007-01-05 05:56:31 +0000486 int hspacing = table()->hBorderSpacing();
487 int vspacing = table()->vBorderSpacing();
eseidela043f3d2006-01-20 19:24:53 +0000488 int nEffCols = table()->numEffCols();
ddkilzer260f3d22007-01-05 05:56:31 +0000489
hyatt@apple.com7a97d272010-11-11 22:17:38 +0000490 LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), style()->isFlippedBlocksWritingMode());
weinigfef13632007-04-29 20:09:08 +0000491
eseidela043f3d2006-01-20 19:24:53 +0000492 for (int r = 0; r < totalRows; r++) {
hyattd866e592006-03-17 09:50:35 +0000493 // Set the row's x/y position and width/height.
hyatt@apple.comd885df72009-01-22 02:31:52 +0000494 if (RenderTableRow* rowRenderer = m_grid[r].rowRenderer) {
495 rowRenderer->setLocation(0, m_rowPos[r]);
mitz@apple.com8010bba2010-11-05 02:43:17 +0000496 rowRenderer->setLogicalWidth(logicalWidth());
497 rowRenderer->setLogicalHeight(m_rowPos[r + 1] - m_rowPos[r] - vspacing);
simon.fraser@apple.com78e3bdb2010-11-30 00:53:05 +0000498 rowRenderer->updateLayerTransform();
hyattd866e592006-03-17 09:50:35 +0000499 }
500
eseidela043f3d2006-01-20 19:24:53 +0000501 for (int c = 0; c < nEffCols; c++) {
jamesr@google.com36623a32010-07-23 20:22:29 +0000502 CellStruct& cs = cellAt(r, c);
503 RenderTableCell* cell = cs.primaryCell();
504
jamesr@google.comb4266232010-07-28 20:07:19 +0000505 if (!cell || cs.inColSpan)
ap50b58352006-01-22 20:32:36 +0000506 continue;
eseidela043f3d2006-01-20 19:24:53 +0000507
jamesr@google.com36623a32010-07-23 20:22:29 +0000508 rindx = cell->row();
509 rHeight = m_rowPos[rindx + cell->rowSpan()] - m_rowPos[rindx] - vspacing;
eseidela043f3d2006-01-20 19:24:53 +0000510
511 // Force percent height children to lay themselves out again.
512 // This will cause these children to grow to fill the cell.
513 // FIXME: There is still more work to do here to fully match WinIE (should
514 // it become necessary to do so). In quirks mode, WinIE behaves like we
515 // do, but it will clip the cells that spill out of the table section. In
516 // strict mode, Mozilla and WinIE both regrow the table to accommodate the
517 // new height of the cell (thus letting the percentages cause growth one
518 // time only). We may also not be handling row-spanning cells correctly.
519 //
520 // Note also the oddity where replaced elements always flex, and yet blocks/tables do
521 // not necessarily flex. WinIE is crazy and inconsistent, and we can't hope to
522 // match the behavior perfectly, but we'll continue to refine it as we discover new
523 // bugs. :)
524 bool cellChildrenFlex = false;
mitz@apple.com8010bba2010-11-05 02:43:17 +0000525 bool flexAllChildren = cell->style()->logicalHeight().isFixed()
526 || (!table()->style()->logicalHeight().isAuto() && rHeight != cell->logicalHeight());
eseidela043f3d2006-01-20 19:24:53 +0000527
528 for (RenderObject* o = cell->firstChild(); o; o = o->nextSibling()) {
mitz@apple.com8010bba2010-11-05 02:43:17 +0000529 if (!o->isText() && o->style()->logicalHeight().isPercent() && (flexAllChildren || o->isReplaced() || (o->isBox() && toRenderBox(o)->scrollsOverflow()))) {
eseidela043f3d2006-01-20 19:24:53 +0000530 // Tables with no sections do not flex.
darin@apple.com445c34f2009-08-01 00:40:58 +0000531 if (!o->isTable() || toRenderTable(o)->hasSections()) {
eseidela043f3d2006-01-20 19:24:53 +0000532 o->setNeedsLayout(true, false);
eseidela043f3d2006-01-20 19:24:53 +0000533 cellChildrenFlex = true;
534 }
535 }
536 }
mitz@apple.com1547c6d2009-03-20 23:41:54 +0000537
538 if (HashSet<RenderBox*>* percentHeightDescendants = cell->percentHeightDescendants()) {
539 HashSet<RenderBox*>::iterator end = percentHeightDescendants->end();
540 for (HashSet<RenderBox*>::iterator it = percentHeightDescendants->begin(); it != end; ++it) {
541 RenderBox* box = *it;
542 if (!box->isReplaced() && !box->scrollsOverflow() && !flexAllChildren)
543 continue;
544
545 while (box != cell) {
546 if (box->normalChildNeedsLayout())
547 break;
548 box->setChildNeedsLayout(true, false);
549 box = box->containingBlock();
550 ASSERT(box);
551 if (!box)
552 break;
553 }
554 cellChildrenFlex = true;
555 }
556 }
557
eseidela043f3d2006-01-20 19:24:53 +0000558 if (cellChildrenFlex) {
mitz@apple.com1547c6d2009-03-20 23:41:54 +0000559 cell->setChildNeedsLayout(true, false);
eseidela043f3d2006-01-20 19:24:53 +0000560 // Alignment within a cell is based off the calculated
561 // height, which becomes irrelevant once the cell has
hyatt@apple.com0bf6c842009-01-23 03:42:12 +0000562 // been resized based off its percentage.
ap7332aaa2006-04-28 16:02:45 +0000563 cell->setOverrideSize(max(0,
mitz@apple.com8010bba2010-11-05 02:43:17 +0000564 rHeight - cell->borderBefore() - cell->paddingBefore() -
565 cell->borderAfter() - cell->paddingAfter()));
ap7332aaa2006-04-28 16:02:45 +0000566 cell->layoutIfNeeded();
mitz@apple.com8010bba2010-11-05 02:43:17 +0000567
hyatt851433b2007-05-12 06:40:14 +0000568 // If the baseline moved, we may have to update the data for our row. Find out the new baseline.
569 EVerticalAlign va = cell->style()->verticalAlign();
570 if (va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB) {
571 int b = cell->baselinePosition();
mitz@apple.com8010bba2010-11-05 02:43:17 +0000572 if (b > cell->borderBefore() + cell->paddingBefore())
hyatt851433b2007-05-12 06:40:14 +0000573 m_grid[r].baseline = max(m_grid[r].baseline, b);
574 }
eseidela043f3d2006-01-20 19:24:53 +0000575 }
mitz@apple.com8010bba2010-11-05 02:43:17 +0000576
577 int oldIntrinsicPaddingBefore = cell->intrinsicPaddingBefore();
578 int oldIntrinsicPaddingAfter = cell->intrinsicPaddingAfter();
579 int logicalHeightWithoutIntrinsicPadding = cell->logicalHeight() - oldIntrinsicPaddingBefore - oldIntrinsicPaddingAfter;
580
581 int intrinsicPaddingBefore = 0;
bdakinf9936352007-04-19 19:58:20 +0000582 switch (cell->style()->verticalAlign()) {
583 case SUB:
584 case SUPER:
585 case TEXT_TOP:
586 case TEXT_BOTTOM:
hyatt@apple.com5defbad2009-01-23 04:30:47 +0000587 case BASELINE: {
hyatt@apple.com0bf6c842009-01-23 03:42:12 +0000588 int b = cell->baselinePosition();
mitz@apple.com8010bba2010-11-05 02:43:17 +0000589 if (b > cell->borderBefore() + cell->paddingBefore())
590 intrinsicPaddingBefore = getBaseline(r) - (b - oldIntrinsicPaddingBefore);
bdakinf9936352007-04-19 19:58:20 +0000591 break;
hyatt@apple.com5defbad2009-01-23 04:30:47 +0000592 }
bdakinf9936352007-04-19 19:58:20 +0000593 case TOP:
bdakinf9936352007-04-19 19:58:20 +0000594 break;
595 case MIDDLE:
mitz@apple.com8010bba2010-11-05 02:43:17 +0000596 intrinsicPaddingBefore = (rHeight - logicalHeightWithoutIntrinsicPadding) / 2;
bdakinf9936352007-04-19 19:58:20 +0000597 break;
598 case BOTTOM:
mitz@apple.com8010bba2010-11-05 02:43:17 +0000599 intrinsicPaddingBefore = rHeight - logicalHeightWithoutIntrinsicPadding;
bdakinf9936352007-04-19 19:58:20 +0000600 break;
601 default:
602 break;
603 }
hyatt@apple.com0bf6c842009-01-23 03:42:12 +0000604
mitz@apple.com8010bba2010-11-05 02:43:17 +0000605 int intrinsicPaddingAfter = rHeight - logicalHeightWithoutIntrinsicPadding - intrinsicPaddingBefore;
606 cell->setIntrinsicPaddingBefore(intrinsicPaddingBefore);
607 cell->setIntrinsicPaddingAfter(intrinsicPaddingAfter);
608
hyatt@apple.com0bf6c842009-01-23 03:42:12 +0000609 IntRect oldCellRect(cell->x(), cell->y() , cell->width(), cell->height());
mitz@apple.com8010bba2010-11-05 02:43:17 +0000610
hyatt@apple.comc0fa1632010-09-30 20:01:33 +0000611 if (!style()->isLeftToRightDirection())
mitz@apple.com8010bba2010-11-05 02:43:17 +0000612 cell->setLogicalLocation(table()->columnPositions()[nEffCols] - table()->columnPositions()[table()->colToEffCol(cell->col() + cell->colSpan())] + hspacing, m_rowPos[rindx]);
jamesr@google.com36623a32010-07-23 20:22:29 +0000613 else
mitz@apple.com8010bba2010-11-05 02:43:17 +0000614 cell->setLogicalLocation(table()->columnPositions()[c] + hspacing, m_rowPos[rindx]);
hyatt@apple.comb297b492010-09-20 20:21:34 +0000615 view()->addLayoutDelta(IntSize(oldCellRect.x() - cell->x(), oldCellRect.y() - cell->y()));
eseidela043f3d2006-01-20 19:24:53 +0000616
mitz@apple.com8010bba2010-11-05 02:43:17 +0000617 if (intrinsicPaddingBefore != oldIntrinsicPaddingBefore || intrinsicPaddingAfter != oldIntrinsicPaddingAfter)
hyatt@apple.com1259d732010-09-20 16:12:58 +0000618 cell->setNeedsLayout(true, false);
mitz@apple.com8010bba2010-11-05 02:43:17 +0000619
hyatt@apple.com2e47dc82010-12-15 18:10:48 +0000620 if (!cell->needsLayout() && view()->layoutState()->pageLogicalHeight() && view()->layoutState()->pageLogicalOffset(cell->y()) != cell->pageLogicalOffset())
hyatt@apple.com1259d732010-09-20 16:12:58 +0000621 cell->setChildNeedsLayout(true, false);
mitz@apple.com8010bba2010-11-05 02:43:17 +0000622
hyatt@apple.com1259d732010-09-20 16:12:58 +0000623 cell->layoutIfNeeded();
mitz@apple.com8010bba2010-11-05 02:43:17 +0000624
625 // FIXME: Make pagination work with vertical tables.
hyatt@apple.com9285815d2010-12-14 19:58:38 +0000626 if (style()->isHorizontalWritingMode() && view()->layoutState()->pageLogicalHeight() && cell->height() != rHeight)
hyatt@apple.com1259d732010-09-20 16:12:58 +0000627 cell->setHeight(rHeight); // FIXME: Pagination might have made us change size. For now just shrink or grow the cell to fit without doing a relayout.
mitz@apple.com8010bba2010-11-05 02:43:17 +0000628
hyatt@apple.comb297b492010-09-20 20:21:34 +0000629 IntSize childOffset(cell->x() - oldCellRect.x(), cell->y() - oldCellRect.y());
630 if (childOffset.width() || childOffset.height()) {
631 view()->addLayoutDelta(childOffset);
hyatt@apple.com1259d732010-09-20 16:12:58 +0000632
hyatt@apple.comb297b492010-09-20 20:21:34 +0000633 // If the child moved, we have to repaint it as well as any floating/positioned
634 // descendants. An exception is if we need a layout. In this case, we know we're going to
635 // repaint ourselves (and the child) anyway.
636 if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout())
637 cell->repaintDuringLayoutIfMoved(oldCellRect);
638 }
eseidela043f3d2006-01-20 19:24:53 +0000639 }
640 }
641
hyatt@apple.comc93db9b2009-02-04 22:29:04 +0000642#ifndef NDEBUG
643 setNeedsLayoutIsForbidden(false);
644#endif
645
hyatt@apple.com8b2cd1f2009-01-26 03:38:55 +0000646 ASSERT(!needsLayout());
647
mitz@apple.com8010bba2010-11-05 02:43:17 +0000648 setLogicalHeight(m_rowPos[totalRows]);
hyatt@apple.com7214c6a2009-08-19 16:28:51 +0000649
650 // Now that our height has been determined, add in overflow from cells.
651 for (int r = 0; r < totalRows; r++) {
652 for (int c = 0; c < nEffCols; c++) {
jamesr@google.com36623a32010-07-23 20:22:29 +0000653 CellStruct& cs = cellAt(r, c);
654 RenderTableCell* cell = cs.primaryCell();
jamesr@google.comb4266232010-07-28 20:07:19 +0000655 if (!cell || cs.inColSpan)
hyatt@apple.com7214c6a2009-08-19 16:28:51 +0000656 continue;
jamesr@google.com36623a32010-07-23 20:22:29 +0000657 if (r < totalRows - 1 && cell == primaryCellAt(r + 1, c))
hyatt@apple.com7214c6a2009-08-19 16:28:51 +0000658 continue;
659 addOverflowFromChild(cell);
hyatt@apple.com592848f2010-12-06 20:03:43 +0000660 m_hasOverflowingCell |= cell->hasVisualOverflow();
hyatt@apple.com7214c6a2009-08-19 16:28:51 +0000661 }
662 }
weinigfef13632007-04-29 20:09:08 +0000663
mitz@apple.comeea52852009-12-31 18:41:23 +0000664 statePusher.pop();
hyatt@apple.comd885df72009-01-22 02:31:52 +0000665 return height();
eseidela043f3d2006-01-20 19:24:53 +0000666}
667
mitz@apple.com8010bba2010-11-05 02:43:17 +0000668int RenderTableSection::calcOuterBorderBefore() const
darin32011102006-05-15 04:42:09 +0000669{
670 int totalCols = table()->numEffCols();
ddkilzer260f3d22007-01-05 05:56:31 +0000671 if (!m_gridRows || !totalCols)
darin32011102006-05-15 04:42:09 +0000672 return 0;
673
674 unsigned borderWidth = 0;
675
mitz@apple.com8010bba2010-11-05 02:43:17 +0000676 const BorderValue& sb = style()->borderBefore();
darin32011102006-05-15 04:42:09 +0000677 if (sb.style() == BHIDDEN)
678 return -1;
679 if (sb.style() > BHIDDEN)
hyatt@apple.come607db72010-04-09 20:21:55 +0000680 borderWidth = sb.width();
darin32011102006-05-15 04:42:09 +0000681
mitz@apple.com8010bba2010-11-05 02:43:17 +0000682 const BorderValue& rb = firstChild()->style()->borderBefore();
darin32011102006-05-15 04:42:09 +0000683 if (rb.style() == BHIDDEN)
684 return -1;
hyatt@apple.come607db72010-04-09 20:21:55 +0000685 if (rb.style() > BHIDDEN && rb.width() > borderWidth)
686 borderWidth = rb.width();
darin32011102006-05-15 04:42:09 +0000687
688 bool allHidden = true;
689 for (int c = 0; c < totalCols; c++) {
690 const CellStruct& current = cellAt(0, c);
jamesr@google.com36623a32010-07-23 20:22:29 +0000691 if (current.inColSpan || !current.hasCells())
darin32011102006-05-15 04:42:09 +0000692 continue;
mitz@apple.com8010bba2010-11-05 02:43:17 +0000693 const BorderValue& cb = current.primaryCell()->style()->borderBefore(); // FIXME: Make this work with perpendicular and flipped cells.
darin32011102006-05-15 04:42:09 +0000694 // FIXME: Don't repeat for the same col group
695 RenderTableCol* colGroup = table()->colElement(c);
696 if (colGroup) {
mitz@apple.com8010bba2010-11-05 02:43:17 +0000697 const BorderValue& gb = colGroup->style()->borderBefore();
darin32011102006-05-15 04:42:09 +0000698 if (gb.style() == BHIDDEN || cb.style() == BHIDDEN)
699 continue;
jamesr@google.com36623a32010-07-23 20:22:29 +0000700 allHidden = false;
hyatt@apple.come607db72010-04-09 20:21:55 +0000701 if (gb.style() > BHIDDEN && gb.width() > borderWidth)
702 borderWidth = gb.width();
703 if (cb.style() > BHIDDEN && cb.width() > borderWidth)
704 borderWidth = cb.width();
darin32011102006-05-15 04:42:09 +0000705 } else {
706 if (cb.style() == BHIDDEN)
707 continue;
jamesr@google.com36623a32010-07-23 20:22:29 +0000708 allHidden = false;
hyatt@apple.come607db72010-04-09 20:21:55 +0000709 if (cb.style() > BHIDDEN && cb.width() > borderWidth)
710 borderWidth = cb.width();
darin32011102006-05-15 04:42:09 +0000711 }
712 }
713 if (allHidden)
714 return -1;
715
716 return borderWidth / 2;
717}
718
mitz@apple.com8010bba2010-11-05 02:43:17 +0000719int RenderTableSection::calcOuterBorderAfter() const
darin32011102006-05-15 04:42:09 +0000720{
721 int totalCols = table()->numEffCols();
ddkilzer260f3d22007-01-05 05:56:31 +0000722 if (!m_gridRows || !totalCols)
darin32011102006-05-15 04:42:09 +0000723 return 0;
724
725 unsigned borderWidth = 0;
726
mitz@apple.com8010bba2010-11-05 02:43:17 +0000727 const BorderValue& sb = style()->borderAfter();
darin32011102006-05-15 04:42:09 +0000728 if (sb.style() == BHIDDEN)
729 return -1;
730 if (sb.style() > BHIDDEN)
hyatt@apple.come607db72010-04-09 20:21:55 +0000731 borderWidth = sb.width();
darin32011102006-05-15 04:42:09 +0000732
mitz@apple.com8010bba2010-11-05 02:43:17 +0000733 const BorderValue& rb = lastChild()->style()->borderAfter();
darin32011102006-05-15 04:42:09 +0000734 if (rb.style() == BHIDDEN)
735 return -1;
hyatt@apple.come607db72010-04-09 20:21:55 +0000736 if (rb.style() > BHIDDEN && rb.width() > borderWidth)
737 borderWidth = rb.width();
darin32011102006-05-15 04:42:09 +0000738
739 bool allHidden = true;
740 for (int c = 0; c < totalCols; c++) {
ddkilzer260f3d22007-01-05 05:56:31 +0000741 const CellStruct& current = cellAt(m_gridRows - 1, c);
jamesr@google.com36623a32010-07-23 20:22:29 +0000742 if (current.inColSpan || !current.hasCells())
darin32011102006-05-15 04:42:09 +0000743 continue;
mitz@apple.com8010bba2010-11-05 02:43:17 +0000744 const BorderValue& cb = current.primaryCell()->style()->borderAfter(); // FIXME: Make this work with perpendicular and flipped cells.
darin32011102006-05-15 04:42:09 +0000745 // FIXME: Don't repeat for the same col group
746 RenderTableCol* colGroup = table()->colElement(c);
747 if (colGroup) {
mitz@apple.com8010bba2010-11-05 02:43:17 +0000748 const BorderValue& gb = colGroup->style()->borderAfter();
darin32011102006-05-15 04:42:09 +0000749 if (gb.style() == BHIDDEN || cb.style() == BHIDDEN)
750 continue;
jamesr@google.com36623a32010-07-23 20:22:29 +0000751 allHidden = false;
hyatt@apple.come607db72010-04-09 20:21:55 +0000752 if (gb.style() > BHIDDEN && gb.width() > borderWidth)
753 borderWidth = gb.width();
754 if (cb.style() > BHIDDEN && cb.width() > borderWidth)
755 borderWidth = cb.width();
darin32011102006-05-15 04:42:09 +0000756 } else {
757 if (cb.style() == BHIDDEN)
758 continue;
jamesr@google.com36623a32010-07-23 20:22:29 +0000759 allHidden = false;
hyatt@apple.come607db72010-04-09 20:21:55 +0000760 if (cb.style() > BHIDDEN && cb.width() > borderWidth)
761 borderWidth = cb.width();
darin32011102006-05-15 04:42:09 +0000762 }
763 }
764 if (allHidden)
765 return -1;
766
767 return (borderWidth + 1) / 2;
768}
769
mitz@apple.com8010bba2010-11-05 02:43:17 +0000770int RenderTableSection::calcOuterBorderStart() const
darin32011102006-05-15 04:42:09 +0000771{
772 int totalCols = table()->numEffCols();
ddkilzer260f3d22007-01-05 05:56:31 +0000773 if (!m_gridRows || !totalCols)
darin32011102006-05-15 04:42:09 +0000774 return 0;
775
776 unsigned borderWidth = 0;
777
mitz@apple.com8010bba2010-11-05 02:43:17 +0000778 const BorderValue& sb = style()->borderStart();
darin32011102006-05-15 04:42:09 +0000779 if (sb.style() == BHIDDEN)
780 return -1;
781 if (sb.style() > BHIDDEN)
hyatt@apple.come607db72010-04-09 20:21:55 +0000782 borderWidth = sb.width();
darin32011102006-05-15 04:42:09 +0000783
mitz@apple.com8010bba2010-11-05 02:43:17 +0000784 if (RenderTableCol* colGroup = table()->colElement(0)) {
785 const BorderValue& gb = colGroup->style()->borderStart();
darin32011102006-05-15 04:42:09 +0000786 if (gb.style() == BHIDDEN)
787 return -1;
hyatt@apple.come607db72010-04-09 20:21:55 +0000788 if (gb.style() > BHIDDEN && gb.width() > borderWidth)
789 borderWidth = gb.width();
darin32011102006-05-15 04:42:09 +0000790 }
791
792 bool allHidden = true;
ddkilzer260f3d22007-01-05 05:56:31 +0000793 for (int r = 0; r < m_gridRows; r++) {
mitz@apple.com8010bba2010-11-05 02:43:17 +0000794 const CellStruct& current = cellAt(r, 0);
jamesr@google.com36623a32010-07-23 20:22:29 +0000795 if (!current.hasCells())
darin32011102006-05-15 04:42:09 +0000796 continue;
797 // FIXME: Don't repeat for the same cell
mitz@apple.com8010bba2010-11-05 02:43:17 +0000798 const BorderValue& cb = current.primaryCell()->style()->borderStart(); // FIXME: Make this work with perpendicular and flipped cells.
799 const BorderValue& rb = current.primaryCell()->parent()->style()->borderStart();
darin32011102006-05-15 04:42:09 +0000800 if (cb.style() == BHIDDEN || rb.style() == BHIDDEN)
801 continue;
jamesr@google.com36623a32010-07-23 20:22:29 +0000802 allHidden = false;
hyatt@apple.come607db72010-04-09 20:21:55 +0000803 if (cb.style() > BHIDDEN && cb.width() > borderWidth)
804 borderWidth = cb.width();
805 if (rb.style() > BHIDDEN && rb.width() > borderWidth)
806 borderWidth = rb.width();
darin32011102006-05-15 04:42:09 +0000807 }
808 if (allHidden)
809 return -1;
810
mitz@apple.com8010bba2010-11-05 02:43:17 +0000811 return (borderWidth + (table()->style()->isLeftToRightDirection() ? 0 : 1)) / 2;
darin32011102006-05-15 04:42:09 +0000812}
813
mitz@apple.com8010bba2010-11-05 02:43:17 +0000814int RenderTableSection::calcOuterBorderEnd() const
darin32011102006-05-15 04:42:09 +0000815{
816 int totalCols = table()->numEffCols();
ddkilzer260f3d22007-01-05 05:56:31 +0000817 if (!m_gridRows || !totalCols)
darin32011102006-05-15 04:42:09 +0000818 return 0;
819
820 unsigned borderWidth = 0;
821
mitz@apple.com8010bba2010-11-05 02:43:17 +0000822 const BorderValue& sb = style()->borderEnd();
darin32011102006-05-15 04:42:09 +0000823 if (sb.style() == BHIDDEN)
824 return -1;
825 if (sb.style() > BHIDDEN)
hyatt@apple.come607db72010-04-09 20:21:55 +0000826 borderWidth = sb.width();
darin32011102006-05-15 04:42:09 +0000827
mitz@apple.com8010bba2010-11-05 02:43:17 +0000828 if (RenderTableCol* colGroup = table()->colElement(totalCols - 1)) {
829 const BorderValue& gb = colGroup->style()->borderEnd();
darin32011102006-05-15 04:42:09 +0000830 if (gb.style() == BHIDDEN)
831 return -1;
hyatt@apple.come607db72010-04-09 20:21:55 +0000832 if (gb.style() > BHIDDEN && gb.width() > borderWidth)
833 borderWidth = gb.width();
darin32011102006-05-15 04:42:09 +0000834 }
835
836 bool allHidden = true;
ddkilzer260f3d22007-01-05 05:56:31 +0000837 for (int r = 0; r < m_gridRows; r++) {
mitz@apple.com8010bba2010-11-05 02:43:17 +0000838 const CellStruct& current = cellAt(r, totalCols - 1);
jamesr@google.com36623a32010-07-23 20:22:29 +0000839 if (!current.hasCells())
darin32011102006-05-15 04:42:09 +0000840 continue;
841 // FIXME: Don't repeat for the same cell
mitz@apple.com8010bba2010-11-05 02:43:17 +0000842 const BorderValue& cb = current.primaryCell()->style()->borderEnd(); // FIXME: Make this work with perpendicular and flipped cells.
843 const BorderValue& rb = current.primaryCell()->parent()->style()->borderEnd();
darin32011102006-05-15 04:42:09 +0000844 if (cb.style() == BHIDDEN || rb.style() == BHIDDEN)
845 continue;
jamesr@google.com36623a32010-07-23 20:22:29 +0000846 allHidden = false;
hyatt@apple.come607db72010-04-09 20:21:55 +0000847 if (cb.style() > BHIDDEN && cb.width() > borderWidth)
848 borderWidth = cb.width();
849 if (rb.style() > BHIDDEN && rb.width() > borderWidth)
850 borderWidth = rb.width();
darin32011102006-05-15 04:42:09 +0000851 }
852 if (allHidden)
853 return -1;
854
mitz@apple.com8010bba2010-11-05 02:43:17 +0000855 return (borderWidth + (table()->style()->isLeftToRightDirection() ? 1 : 0)) / 2;
darin32011102006-05-15 04:42:09 +0000856}
857
858void RenderTableSection::recalcOuterBorder()
859{
mitz@apple.com8010bba2010-11-05 02:43:17 +0000860 m_outerBorderBefore = calcOuterBorderBefore();
861 m_outerBorderAfter = calcOuterBorderAfter();
862 m_outerBorderStart = calcOuterBorderStart();
863 m_outerBorderEnd = calcOuterBorderEnd();
darin32011102006-05-15 04:42:09 +0000864}
865
hyatt@apple.com916f31f2009-02-11 19:02:12 +0000866int RenderTableSection::firstLineBoxBaseline() const
mitz@apple.com57a2f482008-08-23 07:16:41 +0000867{
868 if (!m_gridRows)
869 return -1;
870
871 int firstLineBaseline = m_grid[0].baseline;
872 if (firstLineBaseline)
873 return firstLineBaseline + m_rowPos[0];
874
875 firstLineBaseline = -1;
876 Row* firstRow = m_grid[0].row;
877 for (size_t i = 0; i < firstRow->size(); ++i) {
jamesr@google.com36623a32010-07-23 20:22:29 +0000878 CellStruct& cs = firstRow->at(i);
879 RenderTableCell* cell = cs.primaryCell();
mitz@apple.com57a2f482008-08-23 07:16:41 +0000880 if (cell)
mitz@apple.com8010bba2010-11-05 02:43:17 +0000881 firstLineBaseline = max(firstLineBaseline, cell->logicalTop() + cell->paddingBefore() + cell->borderBefore() + cell->contentLogicalHeight());
mitz@apple.com57a2f482008-08-23 07:16:41 +0000882 }
883
884 return firstLineBaseline;
885}
darin32011102006-05-15 04:42:09 +0000886
weinig0a635f02006-11-01 21:49:13 +0000887void RenderTableSection::paint(PaintInfo& paintInfo, int tx, int ty)
eseidela043f3d2006-01-20 19:24:53 +0000888{
anttib3666072007-08-17 23:29:50 +0000889 // put this back in when all layout tests can handle it
890 // ASSERT(!needsLayout());
891 // avoid crashing on bugs that cause us to paint with dirty layout
892 if (needsLayout())
893 return;
894
ddkilzer260f3d22007-01-05 05:56:31 +0000895 unsigned totalRows = m_gridRows;
896 unsigned totalCols = table()->columns().size();
eseidela043f3d2006-01-20 19:24:53 +0000897
ddkilzer260f3d22007-01-05 05:56:31 +0000898 if (!totalRows || !totalCols)
ap0e708ba2006-05-20 08:47:37 +0000899 return;
900
hyatt@apple.comd885df72009-01-22 02:31:52 +0000901 tx += x();
hyatt@apple.coma97f4672009-01-23 03:55:34 +0000902 ty += y();
eseidela043f3d2006-01-20 19:24:53 +0000903
hyatt@apple.comfe52f5b2009-02-25 01:21:03 +0000904 PaintPhase phase = paintInfo.phase;
905 bool pushedClip = pushContentsClip(paintInfo, tx, ty);
906 paintObject(paintInfo, tx, ty);
907 if (pushedClip)
908 popContentsClip(paintInfo, phase, tx, ty);
909}
910
jamesr@google.com36623a32010-07-23 20:22:29 +0000911static inline bool compareCellPositions(RenderTableCell* elem1, RenderTableCell* elem2)
912{
913 return elem1->row() < elem2->row();
914}
915
916void RenderTableSection::paintCell(RenderTableCell* cell, PaintInfo& paintInfo, int tx, int ty)
917{
mitz@apple.com8010bba2010-11-05 02:43:17 +0000918 IntPoint cellPoint = flipForWritingMode(cell, IntPoint(tx, ty), ParentToChildFlippingAdjustment);
jamesr@google.com36623a32010-07-23 20:22:29 +0000919 PaintPhase paintPhase = paintInfo.phase;
920 RenderTableRow* row = toRenderTableRow(cell->parent());
921
922 if (paintPhase == PaintPhaseBlockBackground || paintPhase == PaintPhaseChildBlockBackground) {
923 // We need to handle painting a stack of backgrounds. This stack (from bottom to top) consists of
924 // the column group, column, row group, row, and then the cell.
925 RenderObject* col = table()->colElement(cell->col());
926 RenderObject* colGroup = 0;
927 if (col && col->parent()->style()->display() == TABLE_COLUMN_GROUP)
928 colGroup = col->parent();
929
930 // Column groups and columns first.
931 // FIXME: Columns and column groups do not currently support opacity, and they are being painted "too late" in
932 // the stack, since we have already opened a transparency layer (potentially) for the table row group.
933 // Note that we deliberately ignore whether or not the cell has a layer, since these backgrounds paint "behind" the
934 // cell.
mitz@apple.com8010bba2010-11-05 02:43:17 +0000935 cell->paintBackgroundsBehindCell(paintInfo, cellPoint.x(), cellPoint.y(), colGroup);
936 cell->paintBackgroundsBehindCell(paintInfo, cellPoint.x(), cellPoint.y(), col);
jamesr@google.com36623a32010-07-23 20:22:29 +0000937
938 // Paint the row group next.
mitz@apple.com8010bba2010-11-05 02:43:17 +0000939 cell->paintBackgroundsBehindCell(paintInfo, cellPoint.x(), cellPoint.y(), this);
jamesr@google.com36623a32010-07-23 20:22:29 +0000940
941 // Paint the row next, but only if it doesn't have a layer. If a row has a layer, it will be responsible for
942 // painting the row background for the cell.
943 if (!row->hasSelfPaintingLayer())
mitz@apple.com8010bba2010-11-05 02:43:17 +0000944 cell->paintBackgroundsBehindCell(paintInfo, cellPoint.x(), cellPoint.y(), row);
jamesr@google.com36623a32010-07-23 20:22:29 +0000945 }
946 if ((!cell->hasSelfPaintingLayer() && !row->hasSelfPaintingLayer()) || paintInfo.phase == PaintPhaseCollapsedTableBorders)
mitz@apple.com8010bba2010-11-05 02:43:17 +0000947 cell->paint(paintInfo, cellPoint.x(), cellPoint.y());
jamesr@google.com36623a32010-07-23 20:22:29 +0000948}
949
hyatt@apple.comfe52f5b2009-02-25 01:21:03 +0000950void RenderTableSection::paintObject(PaintInfo& paintInfo, int tx, int ty)
951{
ddkilzer260f3d22007-01-05 05:56:31 +0000952 // Check which rows and cols are visible and only paint these.
953 // FIXME: Could use a binary search here.
hyatt@apple.comfe52f5b2009-02-25 01:21:03 +0000954 unsigned totalRows = m_gridRows;
955 unsigned totalCols = table()->columns().size();
956
weinig0a635f02006-11-01 21:49:13 +0000957 PaintPhase paintPhase = paintInfo.phase;
eseidela043f3d2006-01-20 19:24:53 +0000958
bdakinf061af72006-03-31 01:19:41 +0000959 int os = 2 * maximalOutlineSize(paintPhase);
ddkilzer260f3d22007-01-05 05:56:31 +0000960 unsigned startrow = 0;
961 unsigned endrow = totalRows;
mitz@apple.com20e5bec2010-12-08 01:37:57 +0000962
963 IntRect localRepaintRect = paintInfo.rect;
964 localRepaintRect.move(-tx, -ty);
965 if (style()->isFlippedBlocksWritingMode()) {
966 if (style()->isHorizontalWritingMode())
967 localRepaintRect.setY(height() - localRepaintRect.bottom());
968 else
969 localRepaintRect.setX(width() - localRepaintRect.right());
970 }
971
ap292f4df2007-02-18 16:15:07 +0000972 // If some cell overflows, just paint all of them.
973 if (!m_hasOverflowingCell) {
mitz@apple.com20e5bec2010-12-08 01:37:57 +0000974 int before = (style()->isHorizontalWritingMode() ? localRepaintRect.y() : localRepaintRect.x()) - os;
jamesr@google.comf88b2582010-08-17 20:57:15 +0000975 // binary search to find a row
mitz@apple.com20e5bec2010-12-08 01:37:57 +0000976 startrow = std::lower_bound(m_rowPos.begin(), m_rowPos.end(), before) - m_rowPos.begin();
eseidela043f3d2006-01-20 19:24:53 +0000977
jamesr@google.comf88b2582010-08-17 20:57:15 +0000978 // The binary search above gives us the first row with
979 // a y position >= the top of the paint rect. Thus, the previous
980 // may need to be repainted as well.
mitz@apple.com20e5bec2010-12-08 01:37:57 +0000981 if (startrow == m_rowPos.size() || (startrow > 0 && (m_rowPos[startrow] > before)))
jamesr@google.comf88b2582010-08-17 20:57:15 +0000982 --startrow;
983
mitz@apple.com20e5bec2010-12-08 01:37:57 +0000984 int after = (style()->isHorizontalWritingMode() ? localRepaintRect.bottom() : localRepaintRect.right()) + os;
985 endrow = std::lower_bound(m_rowPos.begin(), m_rowPos.end(), after) - m_rowPos.begin();
fsamuel@chromium.org179df3b2010-10-05 23:55:13 +0000986 if (endrow == m_rowPos.size())
jamesr@google.comf88b2582010-08-17 20:57:15 +0000987 --endrow;
988
mitz@apple.com20e5bec2010-12-08 01:37:57 +0000989 if (!endrow && m_rowPos[0] - table()->outerBorderBefore() <= after)
jamesr@google.comf88b2582010-08-17 20:57:15 +0000990 ++endrow;
weinig0a635f02006-11-01 21:49:13 +0000991 }
mitz@apple.com20e5bec2010-12-08 01:37:57 +0000992
ddkilzer260f3d22007-01-05 05:56:31 +0000993 unsigned startcol = 0;
994 unsigned endcol = totalCols;
adeleddfe27f2007-01-05 23:03:50 +0000995 // FIXME: Implement RTL.
hyatt@apple.comc0fa1632010-09-30 20:01:33 +0000996 if (!m_hasOverflowingCell && style()->isLeftToRightDirection()) {
mitz@apple.com20e5bec2010-12-08 01:37:57 +0000997 int start = (style()->isHorizontalWritingMode() ? localRepaintRect.x() : localRepaintRect.y()) - os;
jamesr@google.comf88b2582010-08-17 20:57:15 +0000998 Vector<int>& columnPos = table()->columnPositions();
mitz@apple.com20e5bec2010-12-08 01:37:57 +0000999 startcol = std::lower_bound(columnPos.begin(), columnPos.end(), start) - columnPos.begin();
1000 if ((startcol == columnPos.size()) || (startcol > 0 && (columnPos[startcol] > start)))
jamesr@google.comf88b2582010-08-17 20:57:15 +00001001 --startcol;
weinig0a635f02006-11-01 21:49:13 +00001002
mitz@apple.com20e5bec2010-12-08 01:37:57 +00001003 int end = (style()->isHorizontalWritingMode() ? localRepaintRect.right() : localRepaintRect.bottom()) + os;
1004 endcol = std::lower_bound(columnPos.begin(), columnPos.end(), end) - columnPos.begin();
fsamuel@chromium.org179df3b2010-10-05 23:55:13 +00001005 if (endcol == columnPos.size())
jamesr@google.comf88b2582010-08-17 20:57:15 +00001006 --endcol;
1007
mitz@apple.com20e5bec2010-12-08 01:37:57 +00001008 if (!endcol && columnPos[0] - table()->outerBorderStart() <= end)
jamesr@google.comf88b2582010-08-17 20:57:15 +00001009 ++endcol;
eseidela043f3d2006-01-20 19:24:53 +00001010 }
eseidela043f3d2006-01-20 19:24:53 +00001011 if (startcol < endcol) {
jamesr@google.com36623a32010-07-23 20:22:29 +00001012 if (!m_hasMultipleCellLevels) {
1013 // Draw the dirty cells in the order that they appear.
1014 for (unsigned r = startrow; r < endrow; r++) {
1015 for (unsigned c = startcol; c < endcol; c++) {
1016 CellStruct& current = cellAt(r, c);
1017 RenderTableCell* cell = current.primaryCell();
jamesr@google.comb4266232010-07-28 20:07:19 +00001018 if (!cell || (r > startrow && primaryCellAt(r - 1, c) == cell) || (c > startcol && primaryCellAt(r, c - 1) == cell))
jamesr@google.com36623a32010-07-23 20:22:29 +00001019 continue;
1020 paintCell(cell, paintInfo, tx, ty);
hyatte031dd02006-03-18 01:01:06 +00001021 }
ap50b58352006-01-22 20:32:36 +00001022 }
jamesr@google.com36623a32010-07-23 20:22:29 +00001023 } else {
1024 // Draw the cells in the correct paint order.
1025 Vector<RenderTableCell*> cells;
1026 HashSet<RenderTableCell*> spanningCells;
1027 for (unsigned r = startrow; r < endrow; r++) {
1028 for (unsigned c = startcol; c < endcol; c++) {
1029 CellStruct& current = cellAt(r, c);
jamesr@google.comb4266232010-07-28 20:07:19 +00001030 if (!current.hasCells())
jamesr@google.com36623a32010-07-23 20:22:29 +00001031 continue;
1032 for (unsigned i = 0; i < current.cells.size(); ++i) {
1033 if (current.cells[i]->rowSpan() > 1 || current.cells[i]->colSpan() > 1) {
1034 if (spanningCells.contains(current.cells[i]))
1035 continue;
1036 spanningCells.add(current.cells[i]);
1037 }
1038 cells.append(current.cells[i]);
1039 }
1040 }
1041 }
1042 // Sort the dirty cells by paint order.
1043 std::stable_sort(cells.begin(), cells.end(), compareCellPositions);
1044 int size = cells.size();
1045 // Paint the cells.
1046 for (int i = 0; i < size; ++i)
1047 paintCell(cells[i], paintInfo, tx, ty);
ap50b58352006-01-22 20:32:36 +00001048 }
eseidela043f3d2006-01-20 19:24:53 +00001049 }
1050}
1051
darin@apple.com98a7ac62009-01-05 17:26:53 +00001052void RenderTableSection::imageChanged(WrappedImagePtr, const IntRect*)
bdashb07ae8d2007-02-08 04:56:20 +00001053{
bdashb07ae8d2007-02-08 04:56:20 +00001054 // FIXME: Examine cells and repaint only the rect the image paints in.
1055 repaint();
1056}
1057
eseidela043f3d2006-01-20 19:24:53 +00001058void RenderTableSection::recalcCells()
1059{
ddkilzer260f3d22007-01-05 05:56:31 +00001060 m_cCol = 0;
1061 m_cRow = -1;
eseidela043f3d2006-01-20 19:24:53 +00001062 clearGrid();
ddkilzer260f3d22007-01-05 05:56:31 +00001063 m_gridRows = 0;
eseidela043f3d2006-01-20 19:24:53 +00001064
ddkilzer260f3d22007-01-05 05:56:31 +00001065 for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
hyattd866e592006-03-17 09:50:35 +00001066 if (row->isTableRow()) {
ddkilzer260f3d22007-01-05 05:56:31 +00001067 m_cRow++;
1068 m_cCol = 0;
larsb5288ad2007-01-13 18:48:29 +00001069 if (!ensureRows(m_cRow + 1))
1070 break;
hyatt@apple.comd885df72009-01-22 02:31:52 +00001071
darin@apple.com445c34f2009-08-01 00:40:58 +00001072 RenderTableRow* tableRow = toRenderTableRow(row);
hyatt@apple.comd885df72009-01-22 02:31:52 +00001073 m_grid[m_cRow].rowRenderer = tableRow;
mitz@apple.com8010bba2010-11-05 02:43:17 +00001074 setRowLogicalHeightToRowStyleLogicalHeightIfNotRelative(&m_grid[m_cRow]);
eseidel05ef0432006-05-25 22:11:36 +00001075
ddkilzer260f3d22007-01-05 05:56:31 +00001076 for (RenderObject* cell = row->firstChild(); cell; cell = cell->nextSibling()) {
hyattd866e592006-03-17 09:50:35 +00001077 if (cell->isTableCell())
darin@apple.com445c34f2009-08-01 00:40:58 +00001078 addCell(toRenderTableCell(cell), tableRow);
ddkilzer260f3d22007-01-05 05:56:31 +00001079 }
hyattd866e592006-03-17 09:50:35 +00001080 }
eseidela043f3d2006-01-20 19:24:53 +00001081 }
ddkilzer260f3d22007-01-05 05:56:31 +00001082 m_needsCellRecalc = false;
eseidela043f3d2006-01-20 19:24:53 +00001083 setNeedsLayout(true);
1084}
1085
1086void RenderTableSection::clearGrid()
1087{
ddkilzer260f3d22007-01-05 05:56:31 +00001088 int rows = m_gridRows;
eseidela043f3d2006-01-20 19:24:53 +00001089 while (rows--)
ddkilzer260f3d22007-01-05 05:56:31 +00001090 delete m_grid[rows].row;
ap50b58352006-01-22 20:32:36 +00001091}
1092
1093int RenderTableSection::numColumns() const
1094{
1095 int result = 0;
1096
ddkilzer260f3d22007-01-05 05:56:31 +00001097 for (int r = 0; r < m_gridRows; ++r) {
ap50b58352006-01-22 20:32:36 +00001098 for (int c = result; c < table()->numEffCols(); ++c) {
1099 const CellStruct& cell = cellAt(r, c);
jamesr@google.com36623a32010-07-23 20:22:29 +00001100 if (cell.hasCells() || cell.inColSpan)
ap50b58352006-01-22 20:32:36 +00001101 result = c;
1102 }
1103 }
1104
1105 return result + 1;
eseidela043f3d2006-01-20 19:24:53 +00001106}
1107
ddkilzer260f3d22007-01-05 05:56:31 +00001108void RenderTableSection::appendColumn(int pos)
1109{
jamesr@google.comb4266232010-07-28 20:07:19 +00001110 for (int row = 0; row < m_gridRows; ++row)
ddkilzer260f3d22007-01-05 05:56:31 +00001111 m_grid[row].row->resize(pos + 1);
ddkilzer260f3d22007-01-05 05:56:31 +00001112}
1113
jamesr@google.com36623a32010-07-23 20:22:29 +00001114void RenderTableSection::splitColumn(int pos, int first)
ddkilzer260f3d22007-01-05 05:56:31 +00001115{
1116 if (m_cCol > pos)
1117 m_cCol++;
1118 for (int row = 0; row < m_gridRows; ++row) {
ddkilzer260f3d22007-01-05 05:56:31 +00001119 Row& r = *m_grid[row].row;
jamesr@google.com36623a32010-07-23 20:22:29 +00001120 r.insert(pos + 1, CellStruct());
1121 if (r[pos].hasCells()) {
1122 r[pos + 1].cells.append(r[pos].cells);
1123 RenderTableCell* cell = r[pos].primaryCell();
1124 ASSERT(cell);
1125 int colleft = cell->colSpan() - r[pos].inColSpan;
1126 if (first > colleft)
1127 r[pos + 1].inColSpan = 0;
1128 else
1129 r[pos + 1].inColSpan = first + r[pos].inColSpan;
1130 } else {
1131 r[pos + 1].inColSpan = 0;
1132 }
ddkilzer260f3d22007-01-05 05:56:31 +00001133 }
1134}
1135
hyattd866e592006-03-17 09:50:35 +00001136// Hit Testing
hyatt@apple.comd885df72009-01-22 02:31:52 +00001137bool RenderTableSection::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int xPos, int yPos, int tx, int ty, HitTestAction action)
hyattd866e592006-03-17 09:50:35 +00001138{
jamesr@google.comf88b2582010-08-17 20:57:15 +00001139 // If we have no children then we have nothing to do.
1140 if (!firstChild())
1141 return false;
1142
hyattd866e592006-03-17 09:50:35 +00001143 // Table sections cannot ever be hit tested. Effectively they do not exist.
1144 // Just forward to our children always.
hyatt@apple.comd885df72009-01-22 02:31:52 +00001145 tx += x();
hyatt@apple.coma97f4672009-01-23 03:55:34 +00001146 ty += y();
hyattd866e592006-03-17 09:50:35 +00001147
tonikitoo@webkit.org358d5a62010-10-06 14:53:54 +00001148 if (hasOverflowClip() && !overflowClipRect(tx, ty).intersects(result.rectForPoint(xPos, yPos)))
hyatt@apple.comfe52f5b2009-02-25 01:21:03 +00001149 return false;
1150
jamesr@google.comf88b2582010-08-17 20:57:15 +00001151 if (m_hasOverflowingCell) {
1152 for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
1153 // FIXME: We have to skip over inline flows, since they can show up inside table rows
1154 // at the moment (a demoted inline <form> for example). If we ever implement a
1155 // table-specific hit-test method (which we should do for performance reasons anyway),
1156 // then we can remove this check.
mitz@apple.com39fb3ae2010-11-09 20:46:15 +00001157 if (child->isBox() && !toRenderBox(child)->hasSelfPaintingLayer()) {
1158 IntPoint childPoint = flipForWritingMode(toRenderBox(child), IntPoint(tx, ty), ParentToChildFlippingAdjustment);
1159 if (child->nodeAtPoint(request, result, xPos, yPos, childPoint.x(), childPoint.y(), action)) {
1160 updateHitTestResult(result, IntPoint(xPos - childPoint.x(), yPos - childPoint.y()));
1161 return true;
1162 }
jamesr@google.comf88b2582010-08-17 20:57:15 +00001163 }
1164 }
1165 return false;
1166 }
1167
mitz@apple.com39fb3ae2010-11-09 20:46:15 +00001168 IntPoint location = IntPoint(xPos - tx, yPos - ty);
1169 if (style()->isFlippedBlocksWritingMode()) {
1170 if (style()->isHorizontalWritingMode())
1171 location.setY(height() - location.y());
1172 else
1173 location.setX(width() - location.x());
1174 }
1175
1176 int offsetInColumnDirection = style()->isHorizontalWritingMode() ? location.y() : location.x();
1177 // Find the first row that starts after offsetInColumnDirection.
1178 unsigned nextRow = std::upper_bound(m_rowPos.begin(), m_rowPos.end(), offsetInColumnDirection) - m_rowPos.begin();
1179 if (nextRow == m_rowPos.size())
jamesr@google.comf88b2582010-08-17 20:57:15 +00001180 return false;
mitz@apple.com39fb3ae2010-11-09 20:46:15 +00001181 // Now set hitRow to the index of the hit row, or 0.
1182 unsigned hitRow = nextRow > 0 ? nextRow - 1 : 0;
jamesr@google.comf88b2582010-08-17 20:57:15 +00001183
1184 Vector<int>& columnPos = table()->columnPositions();
mitz@apple.com39fb3ae2010-11-09 20:46:15 +00001185 int offsetInRowDirection = style()->isHorizontalWritingMode() ? location.x() : location.y();
1186 if (!style()->isLeftToRightDirection())
1187 offsetInRowDirection = columnPos[columnPos.size() - 1] - offsetInRowDirection;
jamesr@google.comf88b2582010-08-17 20:57:15 +00001188
mitz@apple.com39fb3ae2010-11-09 20:46:15 +00001189 unsigned nextColumn = std::lower_bound(columnPos.begin(), columnPos.end(), offsetInRowDirection) - columnPos.begin();
1190 if (nextColumn == columnPos.size())
jamesr@google.comf88b2582010-08-17 20:57:15 +00001191 return false;
mitz@apple.com39fb3ae2010-11-09 20:46:15 +00001192 unsigned hitColumn = nextColumn > 0 ? nextColumn - 1 : 0;
jamesr@google.comf88b2582010-08-17 20:57:15 +00001193
mitz@apple.com39fb3ae2010-11-09 20:46:15 +00001194 CellStruct& current = cellAt(hitRow, hitColumn);
jamesr@google.comf88b2582010-08-17 20:57:15 +00001195
1196 // If the cell is empty, there's nothing to do
1197 if (!current.hasCells())
1198 return false;
1199
1200 for (int i = current.cells.size() - 1; i >= 0; --i) {
1201 RenderTableCell* cell = current.cells[i];
mitz@apple.com39fb3ae2010-11-09 20:46:15 +00001202 IntPoint cellPoint = flipForWritingMode(cell, IntPoint(tx, ty), ParentToChildFlippingAdjustment);
1203 if (static_cast<RenderObject*>(cell)->nodeAtPoint(request, result, xPos, yPos, cellPoint.x(), cellPoint.y(), action)) {
1204 updateHitTestResult(result, IntPoint(xPos - cellPoint.x(), yPos - cellPoint.y()));
hyattd866e592006-03-17 09:50:35 +00001205 return true;
1206 }
1207 }
hyattd866e592006-03-17 09:50:35 +00001208 return false;
jamesr@google.comf88b2582010-08-17 20:57:15 +00001209
hyattd866e592006-03-17 09:50:35 +00001210}
1211
ddkilzer260f3d22007-01-05 05:56:31 +00001212} // namespace WebCore