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, 2008 Apple Inc. All rights reserved.
8 * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
9 *
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
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
26 #include "config.h"
27 #include "RenderTableSection.h"
28
29 #include "CachedImage.h"
30 #include "Document.h"
31 #include "HTMLNames.h"
32 #include "RenderTableCell.h"
33 #include "RenderTableCol.h"
34 #include "RenderTableRow.h"
35 #include "RenderView.h"
36 #include <limits>
37 #include <wtf/Vector.h>
38 #ifdef ANDROID_LAYOUT
39 #include "Frame.h"
40 #include "Settings.h"
41 #endif
42
43 using namespace std;
44
45 namespace WebCore {
46
47 using namespace HTMLNames;
48
RenderTableSection(Node * node)49 RenderTableSection::RenderTableSection(Node* node)
50 : RenderBox(node)
51 , m_gridRows(0)
52 , m_cCol(0)
53 , m_cRow(-1)
54 , m_outerBorderLeft(0)
55 , m_outerBorderRight(0)
56 , m_outerBorderTop(0)
57 , m_outerBorderBottom(0)
58 , m_overflowLeft(0)
59 , m_overflowWidth(0)
60 , m_overflowTop(0)
61 , m_overflowHeight(0)
62 , m_needsCellRecalc(false)
63 , m_hasOverflowingCell(false)
64 {
65 // init RenderObject attributes
66 setInline(false); // our object is not Inline
67 }
68
~RenderTableSection()69 RenderTableSection::~RenderTableSection()
70 {
71 clearGrid();
72 }
73
destroy()74 void RenderTableSection::destroy()
75 {
76 RenderTable* recalcTable = table();
77
78 RenderBox::destroy();
79
80 // recalc cell info because RenderTable has unguarded pointers
81 // stored that point to this RenderTableSection.
82 if (recalcTable)
83 recalcTable->setNeedsSectionRecalc();
84 }
85
addChild(RenderObject * child,RenderObject * beforeChild)86 void RenderTableSection::addChild(RenderObject* child, RenderObject* beforeChild)
87 {
88 // Make sure we don't append things after :after-generated content if we have it.
89 if (!beforeChild && isAfterContent(lastChild()))
90 beforeChild = lastChild();
91
92 if (!child->isTableRow()) {
93 RenderObject* last = beforeChild;
94 if (!last)
95 last = lastChild();
96 if (last && last->isAnonymous()) {
97 last->addChild(child);
98 return;
99 }
100
101 // If beforeChild is inside an anonymous cell/row, insert into the cell or into
102 // the anonymous row containing it, if there is one.
103 RenderObject* lastBox = last;
104 while (lastBox && lastBox->parent()->isAnonymous() && !lastBox->isTableRow())
105 lastBox = lastBox->parent();
106 if (lastBox && lastBox->isAnonymous()) {
107 lastBox->addChild(child, beforeChild);
108 return;
109 }
110
111 RenderObject* row = new (renderArena()) RenderTableRow(document() /* anonymous table row */);
112 RefPtr<RenderStyle> newStyle = RenderStyle::create();
113 newStyle->inheritFrom(style());
114 newStyle->setDisplay(TABLE_ROW);
115 row->setStyle(newStyle.release());
116 addChild(row, beforeChild);
117 row->addChild(child);
118 return;
119 }
120
121 if (beforeChild)
122 setNeedsCellRecalc();
123
124 ++m_cRow;
125 m_cCol = 0;
126
127 // make sure we have enough rows
128 if (!ensureRows(m_cRow + 1))
129 return;
130
131 m_grid[m_cRow].rowRenderer = toRenderTableRow(child);
132
133 if (!beforeChild) {
134 m_grid[m_cRow].height = child->style()->height();
135 if (m_grid[m_cRow].height.isRelative())
136 m_grid[m_cRow].height = Length();
137 }
138
139 // If the next renderer is actually wrapped in an anonymous table row, we need to go up and find that.
140 while (beforeChild && beforeChild->parent() != this)
141 beforeChild = beforeChild->parent();
142
143 ASSERT(!beforeChild || beforeChild->isTableRow());
144 RenderBox::addChild(child, beforeChild);
145 }
146
removeChild(RenderObject * oldChild)147 void RenderTableSection::removeChild(RenderObject* oldChild)
148 {
149 setNeedsCellRecalc();
150 RenderBox::removeChild(oldChild);
151 }
152
ensureRows(int numRows)153 bool RenderTableSection::ensureRows(int numRows)
154 {
155 int nRows = m_gridRows;
156 if (numRows > nRows) {
157 if (numRows > static_cast<int>(m_grid.size())) {
158 size_t maxSize = numeric_limits<size_t>::max() / sizeof(RowStruct);
159 if (static_cast<size_t>(numRows) > maxSize)
160 return false;
161 m_grid.grow(numRows);
162 }
163 m_gridRows = numRows;
164 int nCols = max(1, table()->numEffCols());
165 CellStruct emptyCellStruct;
166 emptyCellStruct.cell = 0;
167 emptyCellStruct.inColSpan = false;
168 for (int r = nRows; r < numRows; r++) {
169 m_grid[r].row = new Row(nCols);
170 m_grid[r].row->fill(emptyCellStruct);
171 m_grid[r].rowRenderer = 0;
172 m_grid[r].baseline = 0;
173 m_grid[r].height = Length();
174 }
175 }
176
177 return true;
178 }
179
addCell(RenderTableCell * cell,RenderTableRow * row)180 void RenderTableSection::addCell(RenderTableCell* cell, RenderTableRow* row)
181 {
182 int rSpan = cell->rowSpan();
183 int cSpan = cell->colSpan();
184 Vector<RenderTable::ColumnStruct>& columns = table()->columns();
185 int nCols = columns.size();
186
187 // ### mozilla still seems to do the old HTML way, even for strict DTD
188 // (see the annotation on table cell layouting in the CSS specs and the testcase below:
189 // <TABLE border>
190 // <TR><TD>1 <TD rowspan="2">2 <TD>3 <TD>4
191 // <TR><TD colspan="2">5
192 // </TABLE>
193
194 while (m_cCol < nCols && (cellAt(m_cRow, m_cCol).cell || cellAt(m_cRow, m_cCol).inColSpan))
195 m_cCol++;
196
197 if (rSpan == 1) {
198 // we ignore height settings on rowspan cells
199 Length height = cell->style()->height();
200 if (height.isPositive() || (height.isRelative() && height.value() >= 0)) {
201 Length cRowHeight = m_grid[m_cRow].height;
202 switch (height.type()) {
203 case Percent:
204 if (!(cRowHeight.isPercent()) ||
205 (cRowHeight.isPercent() && cRowHeight.rawValue() < height.rawValue()))
206 m_grid[m_cRow].height = height;
207 break;
208 case Fixed:
209 if (cRowHeight.type() < Percent ||
210 (cRowHeight.isFixed() && cRowHeight.value() < height.value()))
211 m_grid[m_cRow].height = height;
212 break;
213 case Relative:
214 default:
215 break;
216 }
217 }
218 }
219
220 // make sure we have enough rows
221 if (!ensureRows(m_cRow + rSpan))
222 return;
223
224 m_grid[m_cRow].rowRenderer = row;
225
226 int col = m_cCol;
227 // tell the cell where it is
228 CellStruct currentCell;
229 currentCell.cell = cell;
230 currentCell.inColSpan = false;
231 while (cSpan) {
232 int currentSpan;
233 if (m_cCol >= nCols) {
234 table()->appendColumn(cSpan);
235 currentSpan = cSpan;
236 } else {
237 if (cSpan < columns[m_cCol].span)
238 table()->splitColumn(m_cCol, cSpan);
239 currentSpan = columns[m_cCol].span;
240 }
241
242 for (int r = 0; r < rSpan; r++) {
243 CellStruct& c = cellAt(m_cRow + r, m_cCol);
244 if (!c.cell)
245 c.cell = currentCell.cell;
246 if (currentCell.inColSpan)
247 c.inColSpan = true;
248 }
249 m_cCol++;
250 cSpan -= currentSpan;
251 currentCell.cell = 0;
252 currentCell.inColSpan = true;
253 }
254 cell->setRow(m_cRow);
255 cell->setCol(table()->effColToCol(col));
256 }
257
setCellWidths()258 void RenderTableSection::setCellWidths()
259 {
260 Vector<int>& columnPos = table()->columnPositions();
261
262 LayoutStateMaintainer statePusher(view());
263
264 #ifdef ANDROID_LAYOUT
265 int visibleWidth = 0;
266 if (view()->frameView()) {
267 const Settings* settings = document()->settings();
268 ASSERT(settings);
269 if (settings->layoutAlgorithm() == Settings::kLayoutFitColumnToScreen) {
270 visibleWidth = view()->frameView()->screenWidth();
271 }
272 }
273 #endif
274
275 for (int i = 0; i < m_gridRows; i++) {
276 Row& row = *m_grid[i].row;
277 int cols = row.size();
278 for (int j = 0; j < cols; j++) {
279 CellStruct current = row[j];
280 RenderTableCell* cell = current.cell;
281
282 if (!cell)
283 continue;
284 int endCol = j;
285 int cspan = cell->colSpan();
286 while (cspan && endCol < cols) {
287 cspan -= table()->columns()[endCol].span;
288 endCol++;
289 }
290 int w = columnPos[endCol] - columnPos[j] - table()->hBorderSpacing();
291 #ifdef ANDROID_LAYOUT
292 if (table()->isSingleColumn())
293 w = table()->width()-(table()->borderLeft()+table()->borderRight()+
294 (table()->collapseBorders()?0:(table()->paddingLeft()+table()->paddingRight()+
295 2*table()->hBorderSpacing())));
296 #endif
297 int oldWidth = cell->width();
298 #ifdef ANDROID_LAYOUT
299 if (w != oldWidth || (visibleWidth > 0 && visibleWidth != cell->getVisibleWidth())) {
300 #else
301 if (w != oldWidth) {
302 #endif
303 cell->setNeedsLayout(true);
304 if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout()) {
305 if (!statePusher.didPush()) {
306 // Technically, we should also push state for the row, but since
307 // rows don't push a coordinate transform, that's not necessary.
308 statePusher.push(this, IntSize(x(), y()));
309 }
310 cell->repaint();
311 }
312 #ifdef ANDROID_LAYOUT
313 if (w != oldWidth)
314 #endif
315 cell->updateWidth(w);
316 }
317 }
318 }
319
320 statePusher.pop(); // only pops if we pushed
321 }
322
323 int RenderTableSection::calcRowHeight()
324 {
325 #ifndef NDEBUG
326 setNeedsLayoutIsForbidden(true);
327 #endif
328
329 ASSERT(!needsLayout());
330 #ifdef ANDROID_LAYOUT
331 if (table()->isSingleColumn())
332 return m_rowPos[m_gridRows];
333 #endif
334
335 RenderTableCell* cell;
336
337 int spacing = table()->vBorderSpacing();
338
339 LayoutStateMaintainer statePusher(view());
340
341 m_rowPos.resize(m_gridRows + 1);
342 m_rowPos[0] = spacing;
343
344 for (int r = 0; r < m_gridRows; r++) {
345 m_rowPos[r + 1] = 0;
346 m_grid[r].baseline = 0;
347 int baseline = 0;
348 int bdesc = 0;
349 int ch = m_grid[r].height.calcMinValue(0);
350 int pos = m_rowPos[r] + ch + (m_grid[r].rowRenderer ? spacing : 0);
351
352 m_rowPos[r + 1] = max(m_rowPos[r + 1], pos);
353
354 Row* row = m_grid[r].row;
355 int totalCols = row->size();
356
357 for (int c = 0; c < totalCols; c++) {
358 CellStruct current = cellAt(r, c);
359 cell = current.cell;
360 if (!cell || current.inColSpan)
361 continue;
362 if (r < m_gridRows - 1 && cellAt(r + 1, c).cell == cell)
363 continue;
364
365 int indx = max(r - cell->rowSpan() + 1, 0);
366
367 if (cell->overrideSize() != -1) {
368 if (!statePusher.didPush()) {
369 // Technically, we should also push state for the row, but since
370 // rows don't push a coordinate transform, that's not necessary.
371 statePusher.push(this, IntSize(x(), y()));
372 }
373 cell->setOverrideSize(-1);
374 cell->setChildNeedsLayout(true, false);
375 cell->layoutIfNeeded();
376 }
377
378 int adjustedPaddingTop = cell->paddingTop() - cell->intrinsicPaddingTop();
379 int adjustedPaddingBottom = cell->paddingBottom() - cell->intrinsicPaddingBottom();
380 int adjustedHeight = cell->height() - (cell->intrinsicPaddingTop() + cell->intrinsicPaddingBottom());
381
382 // Explicit heights use the border box in quirks mode. In strict mode do the right
383 // thing and actually add in the border and padding.
384 ch = cell->style()->height().calcValue(0) +
385 (cell->style()->htmlHacks() ? 0 : (adjustedPaddingTop + adjustedPaddingBottom +
386 cell->borderTop() + cell->borderBottom()));
387 ch = max(ch, adjustedHeight);
388
389 pos = m_rowPos[indx] + ch + (m_grid[r].rowRenderer ? spacing : 0);
390
391 m_rowPos[r + 1] = max(m_rowPos[r + 1], pos);
392
393 // find out the baseline
394 EVerticalAlign va = cell->style()->verticalAlign();
395 if (va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB) {
396 int b = cell->baselinePosition();
397 if (b > cell->borderTop() + cell->paddingTop()) {
398 baseline = max(baseline, b - cell->intrinsicPaddingTop());
399 bdesc = max(bdesc, m_rowPos[indx] + ch - (b - cell->intrinsicPaddingTop()));
400 }
401 }
402 }
403
404 //do we have baseline aligned elements?
405 if (baseline) {
406 // increase rowheight if baseline requires
407 m_rowPos[r + 1] = max(m_rowPos[r + 1], baseline + bdesc + (m_grid[r].rowRenderer ? spacing : 0));
408 m_grid[r].baseline = baseline;
409 }
410
411 m_rowPos[r + 1] = max(m_rowPos[r + 1], m_rowPos[r]);
412 }
413
414 #ifndef NDEBUG
415 setNeedsLayoutIsForbidden(false);
416 #endif
417
418 ASSERT(!needsLayout());
419
420 statePusher.pop();
421
422 return m_rowPos[m_gridRows];
423 }
424
425 void RenderTableSection::layout()
426 {
427 ASSERT(needsLayout());
428
429 LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()));
430 for (RenderObject* child = children()->firstChild(); child; child = child->nextSibling()) {
431 if (child->isTableRow()) {
432 child->layoutIfNeeded();
433 ASSERT(!child->needsLayout());
434 }
435 }
436 statePusher.pop();
437 setNeedsLayout(false);
438 }
439
440 int RenderTableSection::layoutRows(int toAdd)
441 {
442 #ifndef NDEBUG
443 setNeedsLayoutIsForbidden(true);
444 #endif
445
446 ASSERT(!needsLayout());
447 #ifdef ANDROID_LAYOUT
448 if (table()->isSingleColumn()) {
449 int totalRows = m_gridRows;
450 int hspacing = table()->hBorderSpacing();
451 int vspacing = table()->vBorderSpacing();
452 int rHeight = vspacing;
453
454 int leftOffset = hspacing;
455
456 int nEffCols = table()->numEffCols();
457 for (int r = 0; r < totalRows; r++) {
458 for (int c = 0; c < nEffCols; c++) {
459 CellStruct current = cellAt(r, c);
460 RenderTableCell* cell = current.cell;
461
462 if (!cell || current.inColSpan)
463 continue;
464 if (r > 0 && (cellAt(r-1, c).cell == cell))
465 continue;
466
467 // cell->setCellTopExtra(0);
468 // cell->setCellBottomExtra(0);
469
470 int oldCellX = cell->x();
471 int oldCellY = cell->y();
472
473 if (style()->direction() == RTL) {
474 cell->setX(table()->width());
475 cell->setY(rHeight);
476 } else {
477 cell->setX(leftOffset);
478 cell->setY(rHeight);
479 }
480
481 // If the cell moved, we have to repaint it as well as any floating/positioned
482 // descendants. An exception is if we need a layout. In this case, we know we're going to
483 // repaint ourselves (and the cell) anyway.
484 if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout()) {
485 // IntRect cellRect(oldCellX, oldCellY - cell->borderTopExtra() , cell->width(), cell->height());
486 IntRect cellRect(oldCellX, oldCellY, cell->width(), cell->height());
487 cell->repaintDuringLayoutIfMoved(cellRect);
488 }
489 rHeight += cell->height() + vspacing;
490 }
491 }
492
493 setHeight(rHeight);
494 return height();
495 }
496 #endif
497
498 int rHeight;
499 int rindx;
500 int totalRows = m_gridRows;
501
502 // Set the width of our section now. The rows will also be this width.
503 setWidth(table()->contentWidth());
504 m_overflowLeft = 0;
505 m_overflowWidth = width();
506 m_overflowTop = 0;
507 m_overflowHeight = 0;
508 m_hasOverflowingCell = false;
509
510 if (toAdd && totalRows && (m_rowPos[totalRows] || !nextSibling())) {
511 int totalHeight = m_rowPos[totalRows] + toAdd;
512
513 int dh = toAdd;
514 int totalPercent = 0;
515 int numAuto = 0;
516 for (int r = 0; r < totalRows; r++) {
517 if (m_grid[r].height.isAuto())
518 numAuto++;
519 else if (m_grid[r].height.isPercent())
520 totalPercent += m_grid[r].height.rawValue();
521 }
522 if (totalPercent) {
523 // try to satisfy percent
524 int add = 0;
525 totalPercent = min(totalPercent, 100 * percentScaleFactor);
526 int rh = m_rowPos[1] - m_rowPos[0];
527 for (int r = 0; r < totalRows; r++) {
528 if (totalPercent > 0 && m_grid[r].height.isPercent()) {
529 int toAdd = min(dh, (totalHeight * m_grid[r].height.rawValue() / (100 * percentScaleFactor)) - rh);
530 // If toAdd is negative, then we don't want to shrink the row (this bug
531 // affected Outlook Web Access).
532 toAdd = max(0, toAdd);
533 add += toAdd;
534 dh -= toAdd;
535 totalPercent -= m_grid[r].height.rawValue();
536 }
537 if (r < totalRows - 1)
538 rh = m_rowPos[r + 2] - m_rowPos[r + 1];
539 m_rowPos[r + 1] += add;
540 }
541 }
542 if (numAuto) {
543 // distribute over variable cols
544 int add = 0;
545 for (int r = 0; r < totalRows; r++) {
546 if (numAuto > 0 && m_grid[r].height.isAuto()) {
547 int toAdd = dh / numAuto;
548 add += toAdd;
549 dh -= toAdd;
550 numAuto--;
551 }
552 m_rowPos[r + 1] += add;
553 }
554 }
555 if (dh > 0 && m_rowPos[totalRows]) {
556 // if some left overs, distribute equally.
557 int tot = m_rowPos[totalRows];
558 int add = 0;
559 int prev = m_rowPos[0];
560 for (int r = 0; r < totalRows; r++) {
561 //weight with the original height
562 add += dh * (m_rowPos[r + 1] - prev) / tot;
563 prev = m_rowPos[r + 1];
564 m_rowPos[r + 1] += add;
565 }
566 }
567 }
568
569 int hspacing = table()->hBorderSpacing();
570 int vspacing = table()->vBorderSpacing();
571 int nEffCols = table()->numEffCols();
572
573 LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()));
574
575 for (int r = 0; r < totalRows; r++) {
576 // Set the row's x/y position and width/height.
577 if (RenderTableRow* rowRenderer = m_grid[r].rowRenderer) {
578 rowRenderer->setLocation(0, m_rowPos[r]);
579 rowRenderer->setWidth(width());
580 rowRenderer->setHeight(m_rowPos[r + 1] - m_rowPos[r] - vspacing);
581 }
582
583 for (int c = 0; c < nEffCols; c++) {
584 RenderTableCell* cell = cellAt(r, c).cell;
585
586 if (!cell)
587 continue;
588 if (r < totalRows - 1 && cell == cellAt(r + 1, c).cell)
589 continue;
590
591 rindx = max(0, r - cell->rowSpan() + 1);
592
593 rHeight = m_rowPos[r + 1] - m_rowPos[rindx] - vspacing;
594
595 // Force percent height children to lay themselves out again.
596 // This will cause these children to grow to fill the cell.
597 // FIXME: There is still more work to do here to fully match WinIE (should
598 // it become necessary to do so). In quirks mode, WinIE behaves like we
599 // do, but it will clip the cells that spill out of the table section. In
600 // strict mode, Mozilla and WinIE both regrow the table to accommodate the
601 // new height of the cell (thus letting the percentages cause growth one
602 // time only). We may also not be handling row-spanning cells correctly.
603 //
604 // Note also the oddity where replaced elements always flex, and yet blocks/tables do
605 // not necessarily flex. WinIE is crazy and inconsistent, and we can't hope to
606 // match the behavior perfectly, but we'll continue to refine it as we discover new
607 // bugs. :)
608 bool cellChildrenFlex = false;
609 bool flexAllChildren = cell->style()->height().isFixed() ||
610 (!table()->style()->height().isAuto() && rHeight != cell->height());
611
612 for (RenderObject* o = cell->firstChild(); o; o = o->nextSibling()) {
613 if (!o->isText() && o->style()->height().isPercent() && (flexAllChildren || o->isReplaced() || (o->isBox() && toRenderBox(o)->scrollsOverflow()))) {
614 // Tables with no sections do not flex.
615 if (!o->isTable() || toRenderTable(o)->hasSections()) {
616 o->setNeedsLayout(true, false);
617 cellChildrenFlex = true;
618 }
619 }
620 }
621
622 if (HashSet<RenderBox*>* percentHeightDescendants = cell->percentHeightDescendants()) {
623 HashSet<RenderBox*>::iterator end = percentHeightDescendants->end();
624 for (HashSet<RenderBox*>::iterator it = percentHeightDescendants->begin(); it != end; ++it) {
625 RenderBox* box = *it;
626 if (!box->isReplaced() && !box->scrollsOverflow() && !flexAllChildren)
627 continue;
628
629 while (box != cell) {
630 if (box->normalChildNeedsLayout())
631 break;
632 box->setChildNeedsLayout(true, false);
633 box = box->containingBlock();
634 ASSERT(box);
635 if (!box)
636 break;
637 }
638 cellChildrenFlex = true;
639 }
640 }
641
642 if (cellChildrenFlex) {
643 cell->setChildNeedsLayout(true, false);
644 // Alignment within a cell is based off the calculated
645 // height, which becomes irrelevant once the cell has
646 // been resized based off its percentage.
647 cell->setOverrideSize(max(0,
648 rHeight - cell->borderTop() - cell->paddingTop() -
649 cell->borderBottom() - cell->paddingBottom()));
650 cell->layoutIfNeeded();
651
652 // If the baseline moved, we may have to update the data for our row. Find out the new baseline.
653 EVerticalAlign va = cell->style()->verticalAlign();
654 if (va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB) {
655 int b = cell->baselinePosition();
656 if (b > cell->borderTop() + cell->paddingTop())
657 m_grid[r].baseline = max(m_grid[r].baseline, b);
658 }
659 }
660
661 int oldTe = cell->intrinsicPaddingTop();
662 int oldBe = cell->intrinsicPaddingBottom();
663 int heightWithoutIntrinsicPadding = cell->height() - oldTe - oldBe;
664
665 int te = 0;
666 switch (cell->style()->verticalAlign()) {
667 case SUB:
668 case SUPER:
669 case TEXT_TOP:
670 case TEXT_BOTTOM:
671 case BASELINE: {
672 int b = cell->baselinePosition();
673 if (b > cell->borderTop() + cell->paddingTop())
674 te = getBaseline(r) - (b - oldTe);
675 break;
676 }
677 case TOP:
678 te = 0;
679 break;
680 case MIDDLE:
681 te = (rHeight - heightWithoutIntrinsicPadding) / 2;
682 break;
683 case BOTTOM:
684 te = rHeight - heightWithoutIntrinsicPadding;
685 break;
686 default:
687 break;
688 }
689
690 int be = rHeight - heightWithoutIntrinsicPadding - te;
691 cell->setIntrinsicPaddingTop(te);
692 cell->setIntrinsicPaddingBottom(be);
693 if (te != oldTe || be != oldBe) {
694 cell->setNeedsLayout(true, false);
695 cell->layoutIfNeeded();
696 }
697
698 if ((te != oldTe || be > oldBe) && !table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout())
699 cell->repaint();
700
701 IntRect oldCellRect(cell->x(), cell->y() , cell->width(), cell->height());
702
703 if (style()->direction() == RTL) {
704 cell->setLocation(table()->columnPositions()[nEffCols] - table()->columnPositions()[table()->colToEffCol(cell->col() + cell->colSpan())] + hspacing, m_rowPos[rindx]);
705 } else
706 cell->setLocation(table()->columnPositions()[c] + hspacing, m_rowPos[rindx]);
707
708 m_overflowLeft = min(m_overflowLeft, cell->x() + cell->overflowLeft(false));
709 m_overflowWidth = max(m_overflowWidth, cell->x() + cell->overflowWidth(false));
710 m_overflowTop = min(m_overflowTop, cell->y() + cell->overflowTop(false));
711 m_overflowHeight = max(m_overflowHeight, cell->y() + cell->overflowHeight(false));
712 m_hasOverflowingCell |= cell->overflowLeft(false) || cell->overflowWidth(false) > cell->width() || cell->overflowTop(false) || cell->overflowHeight(false) > cell->height();
713
714 // If the cell moved, we have to repaint it as well as any floating/positioned
715 // descendants. An exception is if we need a layout. In this case, we know we're going to
716 // repaint ourselves (and the cell) anyway.
717 if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout())
718 cell->repaintDuringLayoutIfMoved(oldCellRect);
719 }
720 }
721
722 #ifndef NDEBUG
723 setNeedsLayoutIsForbidden(false);
724 #endif
725
726 ASSERT(!needsLayout());
727
728 statePusher.pop();
729
730 setHeight(m_rowPos[totalRows]);
731 m_overflowHeight = max(m_overflowHeight, height());
732 return height();
733 }
734
735 int RenderTableSection::lowestPosition(bool includeOverflowInterior, bool includeSelf) const
736 {
737 int bottom = RenderBox::lowestPosition(includeOverflowInterior, includeSelf);
738 if (!includeOverflowInterior && hasOverflowClip())
739 return bottom;
740
741 for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
742 for (RenderObject* curr = row->firstChild(); curr; curr = curr->nextSibling()) {
743 if (curr->isTableCell()) {
744 RenderTableCell* cell = toRenderTableCell(curr);
745 bottom = max(bottom, cell->y() + cell->lowestPosition(false));
746 }
747 }
748 }
749
750 return bottom;
751 }
752
753 int RenderTableSection::rightmostPosition(bool includeOverflowInterior, bool includeSelf) const
754 {
755 int right = RenderBox::rightmostPosition(includeOverflowInterior, includeSelf);
756 if (!includeOverflowInterior && hasOverflowClip())
757 return right;
758
759 for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
760 for (RenderObject* curr = row->firstChild(); curr; curr = curr->nextSibling()) {
761 if (curr->isTableCell()) {
762 RenderTableCell* cell = toRenderTableCell(curr);
763 right = max(right, cell->x() + cell->rightmostPosition(false));
764 }
765 }
766 }
767
768 return right;
769 }
770
771 int RenderTableSection::leftmostPosition(bool includeOverflowInterior, bool includeSelf) const
772 {
773 int left = RenderBox::leftmostPosition(includeOverflowInterior, includeSelf);
774 if (!includeOverflowInterior && hasOverflowClip())
775 return left;
776
777 for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
778 for (RenderObject* curr = row->firstChild(); curr; curr = curr->nextSibling()) {
779 if (curr->isTableCell()) {
780 RenderTableCell* cell = toRenderTableCell(curr);
781 left = min(left, cell->x() + cell->leftmostPosition(false));
782 }
783 }
784 }
785
786 return left;
787 }
788
789 int RenderTableSection::calcOuterBorderTop() const
790 {
791 int totalCols = table()->numEffCols();
792 if (!m_gridRows || !totalCols)
793 return 0;
794
795 unsigned borderWidth = 0;
796
797 const BorderValue& sb = style()->borderTop();
798 if (sb.style() == BHIDDEN)
799 return -1;
800 if (sb.style() > BHIDDEN)
801 borderWidth = sb.width;
802
803 const BorderValue& rb = firstChild()->style()->borderTop();
804 if (rb.style() == BHIDDEN)
805 return -1;
806 if (rb.style() > BHIDDEN && rb.width > borderWidth)
807 borderWidth = rb.width;
808
809 bool allHidden = true;
810 for (int c = 0; c < totalCols; c++) {
811 const CellStruct& current = cellAt(0, c);
812 if (current.inColSpan || !current.cell)
813 continue;
814 const BorderValue& cb = current.cell->style()->borderTop();
815 // FIXME: Don't repeat for the same col group
816 RenderTableCol* colGroup = table()->colElement(c);
817 if (colGroup) {
818 const BorderValue& gb = colGroup->style()->borderTop();
819 if (gb.style() == BHIDDEN || cb.style() == BHIDDEN)
820 continue;
821 else
822 allHidden = false;
823 if (gb.style() > BHIDDEN && gb.width > borderWidth)
824 borderWidth = gb.width;
825 if (cb.style() > BHIDDEN && cb.width > borderWidth)
826 borderWidth = cb.width;
827 } else {
828 if (cb.style() == BHIDDEN)
829 continue;
830 else
831 allHidden = false;
832 if (cb.style() > BHIDDEN && cb.width > borderWidth)
833 borderWidth = cb.width;
834 }
835 }
836 if (allHidden)
837 return -1;
838
839 return borderWidth / 2;
840 }
841
842 int RenderTableSection::calcOuterBorderBottom() const
843 {
844 int totalCols = table()->numEffCols();
845 if (!m_gridRows || !totalCols)
846 return 0;
847
848 unsigned borderWidth = 0;
849
850 const BorderValue& sb = style()->borderBottom();
851 if (sb.style() == BHIDDEN)
852 return -1;
853 if (sb.style() > BHIDDEN)
854 borderWidth = sb.width;
855
856 const BorderValue& rb = lastChild()->style()->borderBottom();
857 if (rb.style() == BHIDDEN)
858 return -1;
859 if (rb.style() > BHIDDEN && rb.width > borderWidth)
860 borderWidth = rb.width;
861
862 bool allHidden = true;
863 for (int c = 0; c < totalCols; c++) {
864 const CellStruct& current = cellAt(m_gridRows - 1, c);
865 if (current.inColSpan || !current.cell)
866 continue;
867 const BorderValue& cb = current.cell->style()->borderBottom();
868 // FIXME: Don't repeat for the same col group
869 RenderTableCol* colGroup = table()->colElement(c);
870 if (colGroup) {
871 const BorderValue& gb = colGroup->style()->borderBottom();
872 if (gb.style() == BHIDDEN || cb.style() == BHIDDEN)
873 continue;
874 else
875 allHidden = false;
876 if (gb.style() > BHIDDEN && gb.width > borderWidth)
877 borderWidth = gb.width;
878 if (cb.style() > BHIDDEN && cb.width > borderWidth)
879 borderWidth = cb.width;
880 } else {
881 if (cb.style() == BHIDDEN)
882 continue;
883 else
884 allHidden = false;
885 if (cb.style() > BHIDDEN && cb.width > borderWidth)
886 borderWidth = cb.width;
887 }
888 }
889 if (allHidden)
890 return -1;
891
892 return (borderWidth + 1) / 2;
893 }
894
895 int RenderTableSection::calcOuterBorderLeft(bool rtl) const
896 {
897 int totalCols = table()->numEffCols();
898 if (!m_gridRows || !totalCols)
899 return 0;
900
901 unsigned borderWidth = 0;
902
903 const BorderValue& sb = style()->borderLeft();
904 if (sb.style() == BHIDDEN)
905 return -1;
906 if (sb.style() > BHIDDEN)
907 borderWidth = sb.width;
908
909 int leftmostColumn = rtl ? totalCols - 1 : 0;
910 RenderTableCol* colGroup = table()->colElement(leftmostColumn);
911 if (colGroup) {
912 const BorderValue& gb = colGroup->style()->borderLeft();
913 if (gb.style() == BHIDDEN)
914 return -1;
915 if (gb.style() > BHIDDEN && gb.width > borderWidth)
916 borderWidth = gb.width;
917 }
918
919 bool allHidden = true;
920 for (int r = 0; r < m_gridRows; r++) {
921 const CellStruct& current = cellAt(r, leftmostColumn);
922 if (!current.cell)
923 continue;
924 // FIXME: Don't repeat for the same cell
925 const BorderValue& cb = current.cell->style()->borderLeft();
926 const BorderValue& rb = current.cell->parent()->style()->borderLeft();
927 if (cb.style() == BHIDDEN || rb.style() == BHIDDEN)
928 continue;
929 else
930 allHidden = false;
931 if (cb.style() > BHIDDEN && cb.width > borderWidth)
932 borderWidth = cb.width;
933 if (rb.style() > BHIDDEN && rb.width > borderWidth)
934 borderWidth = rb.width;
935 }
936 if (allHidden)
937 return -1;
938
939 return borderWidth / 2;
940 }
941
942 int RenderTableSection::calcOuterBorderRight(bool rtl) const
943 {
944 int totalCols = table()->numEffCols();
945 if (!m_gridRows || !totalCols)
946 return 0;
947
948 unsigned borderWidth = 0;
949
950 const BorderValue& sb = style()->borderRight();
951 if (sb.style() == BHIDDEN)
952 return -1;
953 if (sb.style() > BHIDDEN)
954 borderWidth = sb.width;
955
956 int rightmostColumn = rtl ? 0 : totalCols - 1;
957 RenderTableCol* colGroup = table()->colElement(rightmostColumn);
958 if (colGroup) {
959 const BorderValue& gb = colGroup->style()->borderRight();
960 if (gb.style() == BHIDDEN)
961 return -1;
962 if (gb.style() > BHIDDEN && gb.width > borderWidth)
963 borderWidth = gb.width;
964 }
965
966 bool allHidden = true;
967 for (int r = 0; r < m_gridRows; r++) {
968 const CellStruct& current = cellAt(r, rightmostColumn);
969 if (!current.cell)
970 continue;
971 // FIXME: Don't repeat for the same cell
972 const BorderValue& cb = current.cell->style()->borderRight();
973 const BorderValue& rb = current.cell->parent()->style()->borderRight();
974 if (cb.style() == BHIDDEN || rb.style() == BHIDDEN)
975 continue;
976 else
977 allHidden = false;
978 if (cb.style() > BHIDDEN && cb.width > borderWidth)
979 borderWidth = cb.width;
980 if (rb.style() > BHIDDEN && rb.width > borderWidth)
981 borderWidth = rb.width;
982 }
983 if (allHidden)
984 return -1;
985
986 return (borderWidth + 1) / 2;
987 }
988
989 void RenderTableSection::recalcOuterBorder()
990 {
991 bool rtl = table()->style()->direction() == RTL;
992 m_outerBorderTop = calcOuterBorderTop();
993 m_outerBorderBottom = calcOuterBorderBottom();
994 m_outerBorderLeft = calcOuterBorderLeft(rtl);
995 m_outerBorderRight = calcOuterBorderRight(rtl);
996 }
997
998 int RenderTableSection::firstLineBoxBaseline() const
999 {
1000 if (!m_gridRows)
1001 return -1;
1002
1003 int firstLineBaseline = m_grid[0].baseline;
1004 if (firstLineBaseline)
1005 return firstLineBaseline + m_rowPos[0];
1006
1007 firstLineBaseline = -1;
1008 Row* firstRow = m_grid[0].row;
1009 for (size_t i = 0; i < firstRow->size(); ++i) {
1010 RenderTableCell* cell = firstRow->at(i).cell;
1011 if (cell)
1012 firstLineBaseline = max(firstLineBaseline, cell->y() + cell->paddingTop() + cell->borderTop() + cell->contentHeight());
1013 }
1014
1015 return firstLineBaseline;
1016 }
1017
1018 void RenderTableSection::paint(PaintInfo& paintInfo, int tx, int ty)
1019 {
1020 // put this back in when all layout tests can handle it
1021 // ASSERT(!needsLayout());
1022 // avoid crashing on bugs that cause us to paint with dirty layout
1023 if (needsLayout())
1024 return;
1025
1026 unsigned totalRows = m_gridRows;
1027 unsigned totalCols = table()->columns().size();
1028
1029 if (!totalRows || !totalCols)
1030 return;
1031
1032 tx += x();
1033 ty += y();
1034
1035 PaintPhase phase = paintInfo.phase;
1036 bool pushedClip = pushContentsClip(paintInfo, tx, ty);
1037 paintObject(paintInfo, tx, ty);
1038 if (pushedClip)
1039 popContentsClip(paintInfo, phase, tx, ty);
1040 }
1041
1042 void RenderTableSection::paintObject(PaintInfo& paintInfo, int tx, int ty)
1043 {
1044 // Check which rows and cols are visible and only paint these.
1045 // FIXME: Could use a binary search here.
1046 unsigned totalRows = m_gridRows;
1047 unsigned totalCols = table()->columns().size();
1048
1049 PaintPhase paintPhase = paintInfo.phase;
1050 int x = paintInfo.rect.x();
1051 int y = paintInfo.rect.y();
1052 int w = paintInfo.rect.width();
1053 int h = paintInfo.rect.height();
1054
1055 #ifdef ANDROID_LAYOUT
1056 unsigned int startrow = 0;
1057 unsigned int endrow = totalRows;
1058 unsigned int startcol = 0;
1059 unsigned int endcol = totalCols;
1060 if (table()->isSingleColumn()) {
1061 // FIXME: should we be smarter too?
1062 } else {
1063 // FIXME: possible to rollback to the common tree.
1064 // rowPos size is set in calcRowHeight(), which is called from table layout().
1065 // BUT RenderTableSection is init through parsing. On a slow device, paint() as
1066 // the result of layout() can come after the next parse() as everything is triggered
1067 // by timer. So we have to check rowPos before using it.
1068 if (m_rowPos.size() != (totalRows + 1))
1069 return;
1070 #endif
1071
1072 int os = 2 * maximalOutlineSize(paintPhase);
1073 unsigned startrow = 0;
1074 unsigned endrow = totalRows;
1075
1076 // If some cell overflows, just paint all of them.
1077 if (!m_hasOverflowingCell) {
1078 for (; startrow < totalRows; startrow++) {
1079 if (ty + m_rowPos[startrow + 1] >= y - os)
1080 break;
1081 }
1082 if (startrow == totalRows && ty + m_rowPos[totalRows] + table()->outerBorderBottom() >= y - os)
1083 startrow--;
1084
1085 for (; endrow > 0; endrow--) {
1086 if (ty + m_rowPos[endrow - 1] <= y + h + os)
1087 break;
1088 }
1089 if (!endrow && ty + m_rowPos[0] - table()->outerBorderTop() <= y + h + os)
1090 endrow++;
1091 }
1092
1093 unsigned startcol = 0;
1094 unsigned endcol = totalCols;
1095 // FIXME: Implement RTL.
1096 if (!m_hasOverflowingCell && style()->direction() == LTR) {
1097 for (; startcol < totalCols; startcol++) {
1098 if (tx + table()->columnPositions()[startcol + 1] >= x - os)
1099 break;
1100 }
1101 if (startcol == totalCols && tx + table()->columnPositions()[totalCols] + table()->outerBorderRight() >= x - os)
1102 startcol--;
1103
1104 for (; endcol > 0; endcol--) {
1105 if (tx + table()->columnPositions()[endcol - 1] <= x + w + os)
1106 break;
1107 }
1108 if (!endcol && tx + table()->columnPositions()[0] - table()->outerBorderLeft() <= y + w + os)
1109 endcol++;
1110 }
1111 #ifdef ANDROID_LAYOUT
1112 }
1113 #endif
1114
1115 if (startcol < endcol) {
1116 // draw the cells
1117 for (unsigned r = startrow; r < endrow; r++) {
1118 unsigned c = startcol;
1119 // since a cell can be -1 (indicating a colspan) we might have to search backwards to include it
1120 while (c && cellAt(r, c).inColSpan)
1121 c--;
1122 for (; c < endcol; c++) {
1123 CellStruct current = cellAt(r, c);
1124 RenderTableCell* cell = current.cell;
1125
1126 // Cells must always paint in the order in which they appear taking into account
1127 // their upper left originating row/column. For cells with rowspans, avoid repainting
1128 // if we've already seen the cell.
1129 if (!cell || (r > startrow && (cellAt(r - 1, c).cell == cell)))
1130 continue;
1131
1132 RenderTableRow* row = toRenderTableRow(cell->parent());
1133
1134 if (paintPhase == PaintPhaseBlockBackground || paintPhase == PaintPhaseChildBlockBackground) {
1135 // We need to handle painting a stack of backgrounds. This stack (from bottom to top) consists of
1136 // the column group, column, row group, row, and then the cell.
1137 RenderObject* col = table()->colElement(c);
1138 RenderObject* colGroup = 0;
1139 if (col && col->parent()->style()->display() == TABLE_COLUMN_GROUP)
1140 colGroup = col->parent();
1141
1142 // Column groups and columns first.
1143 // FIXME: Columns and column groups do not currently support opacity, and they are being painted "too late" in
1144 // the stack, since we have already opened a transparency layer (potentially) for the table row group.
1145 // Note that we deliberately ignore whether or not the cell has a layer, since these backgrounds paint "behind" the
1146 // cell.
1147 cell->paintBackgroundsBehindCell(paintInfo, tx, ty, colGroup);
1148 cell->paintBackgroundsBehindCell(paintInfo, tx, ty, col);
1149
1150 // Paint the row group next.
1151 cell->paintBackgroundsBehindCell(paintInfo, tx, ty, this);
1152
1153 // Paint the row next, but only if it doesn't have a layer. If a row has a layer, it will be responsible for
1154 // painting the row background for the cell.
1155 if (!row->hasSelfPaintingLayer())
1156 cell->paintBackgroundsBehindCell(paintInfo, tx, ty, row);
1157 }
1158 if ((!cell->hasSelfPaintingLayer() && !row->hasSelfPaintingLayer()) || paintInfo.phase == PaintPhaseCollapsedTableBorders)
1159 cell->paint(paintInfo, tx, ty);
1160 }
1161 }
1162 }
1163 }
1164
1165 void RenderTableSection::imageChanged(WrappedImagePtr, const IntRect*)
1166 {
1167 // FIXME: Examine cells and repaint only the rect the image paints in.
1168 repaint();
1169 }
1170
1171 void RenderTableSection::recalcCells()
1172 {
1173 m_cCol = 0;
1174 m_cRow = -1;
1175 clearGrid();
1176 m_gridRows = 0;
1177
1178 for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
1179 if (row->isTableRow()) {
1180 m_cRow++;
1181 m_cCol = 0;
1182 if (!ensureRows(m_cRow + 1))
1183 break;
1184
1185 RenderTableRow* tableRow = toRenderTableRow(row);
1186 m_grid[m_cRow].rowRenderer = tableRow;
1187
1188 for (RenderObject* cell = row->firstChild(); cell; cell = cell->nextSibling()) {
1189 if (cell->isTableCell())
1190 addCell(toRenderTableCell(cell), tableRow);
1191 }
1192 }
1193 }
1194 m_needsCellRecalc = false;
1195 setNeedsLayout(true);
1196 }
1197
1198 void RenderTableSection::clearGrid()
1199 {
1200 int rows = m_gridRows;
1201 while (rows--)
1202 delete m_grid[rows].row;
1203 }
1204
1205 int RenderTableSection::numColumns() const
1206 {
1207 int result = 0;
1208
1209 for (int r = 0; r < m_gridRows; ++r) {
1210 for (int c = result; c < table()->numEffCols(); ++c) {
1211 const CellStruct& cell = cellAt(r, c);
1212 if (cell.cell || cell.inColSpan)
1213 result = c;
1214 }
1215 }
1216
1217 return result + 1;
1218 }
1219
1220 void RenderTableSection::appendColumn(int pos)
1221 {
1222 for (int row = 0; row < m_gridRows; ++row) {
1223 m_grid[row].row->resize(pos + 1);
1224 CellStruct& c = cellAt(row, pos);
1225 c.cell = 0;
1226 c.inColSpan = false;
1227 }
1228 }
1229
1230 void RenderTableSection::splitColumn(int pos, int newSize)
1231 {
1232 if (m_cCol > pos)
1233 m_cCol++;
1234 for (int row = 0; row < m_gridRows; ++row) {
1235 m_grid[row].row->resize(newSize);
1236 Row& r = *m_grid[row].row;
1237 memmove(r.data() + pos + 1, r.data() + pos, (newSize - 1 - pos) * sizeof(CellStruct));
1238 r[pos + 1].cell = 0;
1239 r[pos + 1].inColSpan = r[pos].inColSpan || r[pos].cell;
1240 }
1241 }
1242
1243 // Hit Testing
1244 bool RenderTableSection::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int xPos, int yPos, int tx, int ty, HitTestAction action)
1245 {
1246 // Table sections cannot ever be hit tested. Effectively they do not exist.
1247 // Just forward to our children always.
1248 tx += x();
1249 ty += y();
1250
1251 if (hasOverflowClip() && !overflowClipRect(tx, ty).contains(xPos, yPos))
1252 return false;
1253
1254 for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
1255 // FIXME: We have to skip over inline flows, since they can show up inside table rows
1256 // at the moment (a demoted inline <form> for example). If we ever implement a
1257 // table-specific hit-test method (which we should do for performance reasons anyway),
1258 // then we can remove this check.
1259 if (child->isBox() && !toRenderBox(child)->hasSelfPaintingLayer() && child->nodeAtPoint(request, result, xPos, yPos, tx, ty, action)) {
1260 updateHitTestResult(result, IntPoint(xPos - tx, yPos - ty));
1261 return true;
1262 }
1263 }
1264
1265 return false;
1266 }
1267
1268 } // namespace WebCore
1269