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)
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.
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.
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.
27 #include "RenderTableSection.h"
29 #include "CachedImage.h"
31 #include "HTMLNames.h"
32 #include "RenderTableCell.h"
33 #include "RenderTableCol.h"
34 #include "RenderTableRow.h"
35 #include "RenderView.h"
37 #include <wtf/Vector.h>
43 using namespace HTMLNames;
45 RenderTableSection::RenderTableSection(Node* node)
46 : RenderContainer(node)
50 , m_needsCellRecalc(false)
51 , m_outerBorderLeft(0)
52 , m_outerBorderRight(0)
54 , m_outerBorderBottom(0)
59 , m_hasOverflowingCell(false)
61 // init RenderObject attributes
62 setInline(false); // our object is not Inline
65 RenderTableSection::~RenderTableSection()
70 void RenderTableSection::destroy()
72 RenderTable* recalcTable = table();
74 RenderContainer::destroy();
76 // recalc cell info because RenderTable has unguarded pointers
77 // stored that point to this RenderTableSection.
79 recalcTable->setNeedsSectionRecalc();
82 void RenderTableSection::setStyle(RenderStyle* newStyle)
84 // we don't allow changing this one
86 newStyle->setDisplay(style()->display());
87 else if (newStyle->display() != TABLE_FOOTER_GROUP && newStyle->display() != TABLE_HEADER_GROUP)
88 newStyle->setDisplay(TABLE_ROW_GROUP);
90 RenderContainer::setStyle(newStyle);
93 void RenderTableSection::addChild(RenderObject* child, RenderObject* beforeChild)
95 // Make sure we don't append things after :after-generated content if we have it.
96 if (!beforeChild && isAfterContent(lastChild()))
97 beforeChild = lastChild();
99 bool isTableSection = element() && (element()->hasTagName(theadTag) || element()->hasTagName(tbodyTag) || element()->hasTagName(tfootTag));
101 if (!child->isTableRow()) {
102 if (isTableSection && child->element() && child->element()->hasTagName(formTag) && document()->isHTMLDocument()) {
103 RenderContainer::addChild(child, beforeChild);
107 RenderObject* last = beforeChild;
110 if (last && last->isAnonymous()) {
111 last->addChild(child);
115 // If beforeChild is inside an anonymous cell/row, insert into the cell or into
116 // the anonymous row containing it, if there is one.
117 RenderObject* lastBox = last;
118 while (lastBox && lastBox->parent()->isAnonymous() && !lastBox->isTableRow())
119 lastBox = lastBox->parent();
120 if (lastBox && lastBox->isAnonymous()) {
121 lastBox->addChild(child, beforeChild);
125 RenderObject* row = new (renderArena()) RenderTableRow(document() /* anonymous table */);
126 RenderStyle* newStyle = new (renderArena()) RenderStyle();
127 newStyle->inheritFrom(style());
128 newStyle->setDisplay(TABLE_ROW);
129 row->setStyle(newStyle);
130 addChild(row, beforeChild);
131 row->addChild(child);
136 setNeedsCellRecalc();
141 // make sure we have enough rows
142 if (!ensureRows(m_cRow + 1))
145 m_grid[m_cRow].rowRenderer = child;
148 m_grid[m_cRow].height = child->style()->height();
149 if (m_grid[m_cRow].height.isRelative())
150 m_grid[m_cRow].height = Length();
153 // If the next renderer is actually wrapped in an anonymous table row, we need to go up and find that.
154 while (beforeChild && beforeChild->parent() != this)
155 beforeChild = beforeChild->parent();
157 ASSERT(!beforeChild || beforeChild->isTableRow() || isTableSection && beforeChild->element() && beforeChild->element()->hasTagName(formTag) && document()->isHTMLDocument());
158 RenderContainer::addChild(child, beforeChild);
161 bool RenderTableSection::ensureRows(int numRows)
163 int nRows = m_gridRows;
164 if (numRows > nRows) {
165 if (numRows > static_cast<int>(m_grid.size())) {
166 size_t maxSize = numeric_limits<size_t>::max() / sizeof(RowStruct);
167 if (static_cast<size_t>(numRows) > maxSize)
169 m_grid.grow(numRows);
171 m_gridRows = numRows;
172 int nCols = table()->numEffCols();
173 CellStruct emptyCellStruct;
174 emptyCellStruct.cell = 0;
175 emptyCellStruct.inColSpan = false;
176 for (int r = nRows; r < numRows; r++) {
177 m_grid[r].row = new Row(nCols);
178 m_grid[r].row->fill(emptyCellStruct);
179 m_grid[r].rowRenderer = 0;
180 m_grid[r].baseline = 0;
181 m_grid[r].height = Length();
188 void RenderTableSection::addCell(RenderTableCell* cell, RenderObject* row)
190 int rSpan = cell->rowSpan();
191 int cSpan = cell->colSpan();
192 Vector<RenderTable::ColumnStruct>& columns = table()->columns();
193 int nCols = columns.size();
195 // ### mozilla still seems to do the old HTML way, even for strict DTD
196 // (see the annotation on table cell layouting in the CSS specs and the testcase below:
198 // <TR><TD>1 <TD rowspan="2">2 <TD>3 <TD>4
199 // <TR><TD colspan="2">5
202 while (m_cCol < nCols && (cellAt(m_cRow, m_cCol).cell || cellAt(m_cRow, m_cCol).inColSpan))
206 // we ignore height settings on rowspan cells
207 Length height = cell->style()->height();
208 if (height.isPositive() || (height.isRelative() && height.value() >= 0)) {
209 Length cRowHeight = m_grid[m_cRow].height;
210 switch (height.type()) {
212 if (!(cRowHeight.isPercent()) ||
213 (cRowHeight.isPercent() && cRowHeight.rawValue() < height.rawValue()))
214 m_grid[m_cRow].height = height;
217 if (cRowHeight.type() < Percent ||
218 (cRowHeight.isFixed() && cRowHeight.value() < height.value()))
219 m_grid[m_cRow].height = height;
228 // make sure we have enough rows
229 if (!ensureRows(m_cRow + rSpan))
232 m_grid[m_cRow].rowRenderer = row;
235 // tell the cell where it is
236 CellStruct currentCell;
237 currentCell.cell = cell;
238 currentCell.inColSpan = false;
241 if (m_cCol >= nCols) {
242 table()->appendColumn(cSpan);
245 if (cSpan < columns[m_cCol].span)
246 table()->splitColumn(m_cCol, cSpan);
247 currentSpan = columns[m_cCol].span;
250 for (int r = 0; r < rSpan; r++) {
251 CellStruct& c = cellAt(m_cRow + r, m_cCol);
252 if (currentCell.cell && !c.cell)
253 c.cell = currentCell.cell;
254 if (currentCell.inColSpan)
258 cSpan -= currentSpan;
259 currentCell.cell = 0;
260 currentCell.inColSpan = true;
263 cell->setRow(m_cRow);
264 cell->setCol(table()->effColToCol(col));
268 void RenderTableSection::setCellWidths()
270 Vector<int>& columnPos = table()->columnPositions();
271 bool pushedLayoutState = false;
273 for (int i = 0; i < m_gridRows; i++) {
274 Row& row = *m_grid[i].row;
275 int cols = row.size();
276 for (int j = 0; j < cols; j++) {
277 CellStruct current = row[j];
278 RenderTableCell* cell = current.cell;
283 int cspan = cell->colSpan();
284 while (cspan && endCol < cols) {
285 cspan -= table()->columns()[endCol].span;
288 int w = columnPos[endCol] - columnPos[j] - table()->hBorderSpacing();
289 int oldWidth = cell->width();
291 cell->setNeedsLayout(true);
292 if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout()) {
293 if (!pushedLayoutState) {
294 // Technically, we should also push state for the row, but since
295 // rows don't push a coordinate transform, that's not necessary.
296 view()->pushLayoutState(this, IntSize(m_x, m_y));
297 pushedLayoutState = true;
306 if (pushedLayoutState)
307 view()->popLayoutState();
310 int RenderTableSection::calcRowHeight()
312 RenderTableCell* cell;
314 int spacing = table()->vBorderSpacing();
315 bool pushedLayoutState = false;
317 m_rowPos.resize(m_gridRows + 1);
318 m_rowPos[0] = spacing;
320 for (int r = 0; r < m_gridRows; r++) {
322 m_grid[r].baseline = 0;
325 int ch = m_grid[r].height.calcMinValue(0);
326 int pos = m_rowPos[r] + ch + (m_grid[r].rowRenderer ? spacing : 0);
328 m_rowPos[r + 1] = max(m_rowPos[r + 1], pos);
330 Row* row = m_grid[r].row;
331 int totalCols = row->size();
333 for (int c = 0; c < totalCols; c++) {
334 CellStruct current = cellAt(r, c);
336 if (!cell || current.inColSpan)
338 if (r < m_gridRows - 1 && cellAt(r + 1, c).cell == cell)
341 int indx = max(r - cell->rowSpan() + 1, 0);
343 if (cell->overrideSize() != -1) {
344 if (!pushedLayoutState) {
345 // Technically, we should also push state for the row, but since
346 // rows don't push a coordinate transform, that's not necessary.
347 view()->pushLayoutState(this, IntSize(m_x, m_y));
348 pushedLayoutState = true;
350 cell->setOverrideSize(-1);
351 cell->setChildNeedsLayout(true, false);
352 cell->layoutIfNeeded();
355 // Explicit heights use the border box in quirks mode. In strict mode do the right
356 // thing and actually add in the border and padding.
357 ch = cell->style()->height().calcValue(0) +
358 (cell->style()->htmlHacks() ? 0 : (cell->paddingTop() + cell->paddingBottom() +
359 cell->borderTop() + cell->borderBottom()));
360 ch = max(ch, cell->height());
362 pos = m_rowPos[indx] + ch + (m_grid[r].rowRenderer ? spacing : 0);
364 m_rowPos[r + 1] = max(m_rowPos[r + 1], pos);
366 // find out the baseline
367 EVerticalAlign va = cell->style()->verticalAlign();
368 if (va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB) {
369 int b = cell->baselinePosition();
370 if (b > cell->borderTop() + cell->paddingTop()) {
371 baseline = max(baseline, b);
372 bdesc = max(bdesc, m_rowPos[indx] + ch - b);
377 //do we have baseline aligned elements?
379 // increase rowheight if baseline requires
380 m_rowPos[r + 1] = max(m_rowPos[r + 1], baseline + bdesc + (m_grid[r].rowRenderer ? spacing : 0));
381 m_grid[r].baseline = baseline;
384 m_rowPos[r + 1] = max(m_rowPos[r + 1], m_rowPos[r]);
387 if (pushedLayoutState)
388 view()->popLayoutState();
390 return m_rowPos[m_gridRows];
393 int RenderTableSection::layoutRows(int toAdd)
397 int totalRows = m_gridRows;
399 // Set the width of our section now. The rows will also be this width.
400 m_width = table()->contentWidth();
402 m_overflowWidth = m_width;
404 m_overflowHeight = 0;
405 m_hasOverflowingCell = false;
407 if (toAdd && totalRows && (m_rowPos[totalRows] || !nextSibling())) {
408 int totalHeight = m_rowPos[totalRows] + toAdd;
411 int totalPercent = 0;
413 for (int r = 0; r < totalRows; r++) {
414 if (m_grid[r].height.isAuto())
416 else if (m_grid[r].height.isPercent())
417 totalPercent += m_grid[r].height.rawValue();
420 // try to satisfy percent
422 totalPercent = min(totalPercent, 100 * percentScaleFactor);
423 int rh = m_rowPos[1] - m_rowPos[0];
424 for (int r = 0; r < totalRows; r++) {
425 if (totalPercent > 0 && m_grid[r].height.isPercent()) {
426 int toAdd = min(dh, (totalHeight * m_grid[r].height.rawValue() / (100 * percentScaleFactor)) - rh);
427 // If toAdd is negative, then we don't want to shrink the row (this bug
428 // affected Outlook Web Access).
429 toAdd = max(0, toAdd);
432 totalPercent -= m_grid[r].height.rawValue();
434 if (r < totalRows - 1)
435 rh = m_rowPos[r + 2] - m_rowPos[r + 1];
436 m_rowPos[r + 1] += add;
440 // distribute over variable cols
442 for (int r = 0; r < totalRows; r++) {
443 if (numAuto > 0 && m_grid[r].height.isAuto()) {
444 int toAdd = dh / numAuto;
449 m_rowPos[r + 1] += add;
452 if (dh > 0 && m_rowPos[totalRows]) {
453 // if some left overs, distribute equally.
454 int tot = m_rowPos[totalRows];
456 int prev = m_rowPos[0];
457 for (int r = 0; r < totalRows; r++) {
458 //weight with the original height
459 add += dh * (m_rowPos[r + 1] - prev) / tot;
460 prev = m_rowPos[r + 1];
461 m_rowPos[r + 1] += add;
466 int hspacing = table()->hBorderSpacing();
467 int vspacing = table()->vBorderSpacing();
468 int nEffCols = table()->numEffCols();
470 view()->pushLayoutState(this, IntSize(m_x, m_y));
472 for (int r = 0; r < totalRows; r++) {
473 // Set the row's x/y position and width/height.
474 if (RenderObject* rowRenderer = m_grid[r].rowRenderer) {
475 rowRenderer->setPos(0, m_rowPos[r]);
476 rowRenderer->setWidth(m_width);
477 rowRenderer->setHeight(m_rowPos[r + 1] - m_rowPos[r] - vspacing);
480 for (int c = 0; c < nEffCols; c++) {
481 RenderTableCell* cell = cellAt(r, c).cell;
485 if (r < totalRows - 1 && cell == cellAt(r + 1, c).cell)
488 rindx = max(0, r - cell->rowSpan() + 1);
490 rHeight = m_rowPos[r + 1] - m_rowPos[rindx] - vspacing;
492 // Force percent height children to lay themselves out again.
493 // This will cause these children to grow to fill the cell.
494 // FIXME: There is still more work to do here to fully match WinIE (should
495 // it become necessary to do so). In quirks mode, WinIE behaves like we
496 // do, but it will clip the cells that spill out of the table section. In
497 // strict mode, Mozilla and WinIE both regrow the table to accommodate the
498 // new height of the cell (thus letting the percentages cause growth one
499 // time only). We may also not be handling row-spanning cells correctly.
501 // Note also the oddity where replaced elements always flex, and yet blocks/tables do
502 // not necessarily flex. WinIE is crazy and inconsistent, and we can't hope to
503 // match the behavior perfectly, but we'll continue to refine it as we discover new
505 bool cellChildrenFlex = false;
506 bool flexAllChildren = cell->style()->height().isFixed() ||
507 (!table()->style()->height().isAuto() && rHeight != cell->height());
509 for (RenderObject* o = cell->firstChild(); o; o = o->nextSibling()) {
510 if (!o->isText() && o->style()->height().isPercent() && (o->isReplaced() || o->scrollsOverflow() || flexAllChildren)) {
511 // Tables with no sections do not flex.
512 if (!o->isTable() || static_cast<RenderTable*>(o)->hasSections()) {
513 o->setNeedsLayout(true, false);
514 cell->setChildNeedsLayout(true, false);
515 cellChildrenFlex = true;
519 if (cellChildrenFlex) {
520 // Alignment within a cell is based off the calculated
521 // height, which becomes irrelevant once the cell has
522 // been resized based off its percentage. -dwh
523 cell->setOverrideSize(max(0,
524 rHeight - cell->borderTop() - cell->paddingTop() -
525 cell->borderBottom() - cell->paddingBottom()));
526 cell->layoutIfNeeded();
528 // If the baseline moved, we may have to update the data for our row. Find out the new baseline.
529 EVerticalAlign va = cell->style()->verticalAlign();
530 if (va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB) {
531 int b = cell->baselinePosition();
532 if (b > cell->borderTop() + cell->paddingTop())
533 m_grid[r].baseline = max(m_grid[r].baseline, b);
538 switch (cell->style()->verticalAlign()) {
544 te = getBaseline(r) - cell->baselinePosition();
550 te = (rHeight - cell->height()) / 2;
553 te = rHeight - cell->height();
559 int oldTe = cell->borderTopExtra();
560 int oldBe = cell->borderBottomExtra();
562 int be = rHeight - cell->height() - te;
563 cell->setCellTopExtra(te);
564 cell->setCellBottomExtra(be);
565 if ((te != oldTe || be > oldBe) && !table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout())
568 IntRect oldCellRect(cell->xPos(), cell->yPos() - cell->borderTopExtra() , cell->width(), cell->height());
570 if (style()->direction() == RTL) {
571 cell->setPos(table()->columnPositions()[nEffCols] - table()->columnPositions()[table()->colToEffCol(cell->col() + cell->colSpan())] + hspacing, m_rowPos[rindx]);
573 cell->setPos(table()->columnPositions()[c] + hspacing, m_rowPos[rindx]);
575 m_overflowLeft = min(m_overflowLeft, cell->xPos() + cell->overflowLeft(false));
576 m_overflowWidth = max(m_overflowWidth, cell->xPos() + cell->overflowWidth(false));
577 m_overflowTop = min(m_overflowTop, cell->yPos() + cell->overflowTop(false));
578 m_overflowHeight = max(m_overflowHeight, cell->yPos() + cell->overflowHeight(false));
579 m_hasOverflowingCell |= cell->overflowLeft(false) || cell->overflowWidth(false) > cell->width() || cell->overflowTop(false) || cell->overflowHeight(false) > cell->height();
581 // If the cell moved, we have to repaint it as well as any floating/positioned
582 // descendants. An exception is if we need a layout. In this case, we know we're going to
583 // repaint ourselves (and the cell) anyway.
584 if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout())
585 cell->repaintDuringLayoutIfMoved(oldCellRect);
589 view()->popLayoutState();
591 m_height = m_rowPos[totalRows];
592 m_overflowHeight = max(m_overflowHeight, m_height);
596 int RenderTableSection::lowestPosition(bool includeOverflowInterior, bool includeSelf) const
598 int bottom = RenderContainer::lowestPosition(includeOverflowInterior, includeSelf);
599 if (!includeOverflowInterior && hasOverflowClip())
602 for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
603 for (RenderObject* cell = row->firstChild(); cell; cell = cell->nextSibling()) {
604 if (cell->isTableCell())
605 bottom = max(bottom, cell->yPos() + cell->lowestPosition(false));
612 int RenderTableSection::rightmostPosition(bool includeOverflowInterior, bool includeSelf) const
614 int right = RenderContainer::rightmostPosition(includeOverflowInterior, includeSelf);
615 if (!includeOverflowInterior && hasOverflowClip())
618 for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
619 for (RenderObject* cell = row->firstChild(); cell; cell = cell->nextSibling()) {
620 if (cell->isTableCell())
621 right = max(right, cell->xPos() + cell->rightmostPosition(false));
628 int RenderTableSection::leftmostPosition(bool includeOverflowInterior, bool includeSelf) const
630 int left = RenderContainer::leftmostPosition(includeOverflowInterior, includeSelf);
631 if (!includeOverflowInterior && hasOverflowClip())
634 for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
635 for (RenderObject* cell = row->firstChild(); cell; cell = cell->nextSibling()) {
636 if (cell->isTableCell())
637 left = min(left, cell->xPos() + cell->leftmostPosition(false));
644 int RenderTableSection::calcOuterBorderTop() const
646 int totalCols = table()->numEffCols();
647 if (!m_gridRows || !totalCols)
650 unsigned borderWidth = 0;
652 const BorderValue& sb = style()->borderTop();
653 if (sb.style() == BHIDDEN)
655 if (sb.style() > BHIDDEN)
656 borderWidth = sb.width;
658 const BorderValue& rb = firstChild()->style()->borderTop();
659 if (rb.style() == BHIDDEN)
661 if (rb.style() > BHIDDEN && rb.width > borderWidth)
662 borderWidth = rb.width;
664 bool allHidden = true;
665 for (int c = 0; c < totalCols; c++) {
666 const CellStruct& current = cellAt(0, c);
667 if (current.inColSpan || !current.cell)
669 const BorderValue& cb = current.cell->style()->borderTop();
670 // FIXME: Don't repeat for the same col group
671 RenderTableCol* colGroup = table()->colElement(c);
673 const BorderValue& gb = colGroup->style()->borderTop();
674 if (gb.style() == BHIDDEN || cb.style() == BHIDDEN)
678 if (gb.style() > BHIDDEN && gb.width > borderWidth)
679 borderWidth = gb.width;
680 if (cb.style() > BHIDDEN && cb.width > borderWidth)
681 borderWidth = cb.width;
683 if (cb.style() == BHIDDEN)
687 if (cb.style() > BHIDDEN && cb.width > borderWidth)
688 borderWidth = cb.width;
694 return borderWidth / 2;
697 int RenderTableSection::calcOuterBorderBottom() const
699 int totalCols = table()->numEffCols();
700 if (!m_gridRows || !totalCols)
703 unsigned borderWidth = 0;
705 const BorderValue& sb = style()->borderBottom();
706 if (sb.style() == BHIDDEN)
708 if (sb.style() > BHIDDEN)
709 borderWidth = sb.width;
711 const BorderValue& rb = lastChild()->style()->borderBottom();
712 if (rb.style() == BHIDDEN)
714 if (rb.style() > BHIDDEN && rb.width > borderWidth)
715 borderWidth = rb.width;
717 bool allHidden = true;
718 for (int c = 0; c < totalCols; c++) {
719 const CellStruct& current = cellAt(m_gridRows - 1, c);
720 if (current.inColSpan || !current.cell)
722 const BorderValue& cb = current.cell->style()->borderBottom();
723 // FIXME: Don't repeat for the same col group
724 RenderTableCol* colGroup = table()->colElement(c);
726 const BorderValue& gb = colGroup->style()->borderBottom();
727 if (gb.style() == BHIDDEN || cb.style() == BHIDDEN)
731 if (gb.style() > BHIDDEN && gb.width > borderWidth)
732 borderWidth = gb.width;
733 if (cb.style() > BHIDDEN && cb.width > borderWidth)
734 borderWidth = cb.width;
736 if (cb.style() == BHIDDEN)
740 if (cb.style() > BHIDDEN && cb.width > borderWidth)
741 borderWidth = cb.width;
747 return (borderWidth + 1) / 2;
750 int RenderTableSection::calcOuterBorderLeft(bool rtl) const
752 int totalCols = table()->numEffCols();
753 if (!m_gridRows || !totalCols)
756 unsigned borderWidth = 0;
758 const BorderValue& sb = style()->borderLeft();
759 if (sb.style() == BHIDDEN)
761 if (sb.style() > BHIDDEN)
762 borderWidth = sb.width;
764 int leftmostColumn = rtl ? totalCols - 1 : 0;
765 RenderTableCol* colGroup = table()->colElement(leftmostColumn);
767 const BorderValue& gb = colGroup->style()->borderLeft();
768 if (gb.style() == BHIDDEN)
770 if (gb.style() > BHIDDEN && gb.width > borderWidth)
771 borderWidth = gb.width;
774 bool allHidden = true;
775 for (int r = 0; r < m_gridRows; r++) {
776 const CellStruct& current = cellAt(r, leftmostColumn);
779 // FIXME: Don't repeat for the same cell
780 const BorderValue& cb = current.cell->style()->borderLeft();
781 const BorderValue& rb = current.cell->parent()->style()->borderLeft();
782 if (cb.style() == BHIDDEN || rb.style() == BHIDDEN)
786 if (cb.style() > BHIDDEN && cb.width > borderWidth)
787 borderWidth = cb.width;
788 if (rb.style() > BHIDDEN && rb.width > borderWidth)
789 borderWidth = rb.width;
794 return borderWidth / 2;
797 int RenderTableSection::calcOuterBorderRight(bool rtl) const
799 int totalCols = table()->numEffCols();
800 if (!m_gridRows || !totalCols)
803 unsigned borderWidth = 0;
805 const BorderValue& sb = style()->borderRight();
806 if (sb.style() == BHIDDEN)
808 if (sb.style() > BHIDDEN)
809 borderWidth = sb.width;
811 int rightmostColumn = rtl ? 0 : totalCols - 1;
812 RenderTableCol* colGroup = table()->colElement(rightmostColumn);
814 const BorderValue& gb = colGroup->style()->borderRight();
815 if (gb.style() == BHIDDEN)
817 if (gb.style() > BHIDDEN && gb.width > borderWidth)
818 borderWidth = gb.width;
821 bool allHidden = true;
822 for (int r = 0; r < m_gridRows; r++) {
823 const CellStruct& current = cellAt(r, rightmostColumn);
826 // FIXME: Don't repeat for the same cell
827 const BorderValue& cb = current.cell->style()->borderRight();
828 const BorderValue& rb = current.cell->parent()->style()->borderRight();
829 if (cb.style() == BHIDDEN || rb.style() == BHIDDEN)
833 if (cb.style() > BHIDDEN && cb.width > borderWidth)
834 borderWidth = cb.width;
835 if (rb.style() > BHIDDEN && rb.width > borderWidth)
836 borderWidth = rb.width;
841 return (borderWidth + 1) / 2;
844 void RenderTableSection::recalcOuterBorder()
846 bool rtl = table()->style()->direction() == RTL;
847 m_outerBorderTop = calcOuterBorderTop();
848 m_outerBorderBottom = calcOuterBorderBottom();
849 m_outerBorderLeft = calcOuterBorderLeft(rtl);
850 m_outerBorderRight = calcOuterBorderRight(rtl);
854 void RenderTableSection::paint(PaintInfo& paintInfo, int tx, int ty)
856 // put this back in when all layout tests can handle it
857 // ASSERT(!needsLayout());
858 // avoid crashing on bugs that cause us to paint with dirty layout
862 unsigned totalRows = m_gridRows;
863 unsigned totalCols = table()->columns().size();
865 if (!totalRows || !totalCols)
871 // Check which rows and cols are visible and only paint these.
872 // FIXME: Could use a binary search here.
873 PaintPhase paintPhase = paintInfo.phase;
874 int x = paintInfo.rect.x();
875 int y = paintInfo.rect.y();
876 int w = paintInfo.rect.width();
877 int h = paintInfo.rect.height();
879 int os = 2 * maximalOutlineSize(paintPhase);
880 unsigned startrow = 0;
881 unsigned endrow = totalRows;
883 // If some cell overflows, just paint all of them.
884 if (!m_hasOverflowingCell) {
885 for (; startrow < totalRows; startrow++) {
886 if (ty + m_rowPos[startrow + 1] >= y - os)
889 if (startrow == totalRows && ty + m_rowPos[totalRows] + table()->outerBorderBottom() >= y - os)
892 for (; endrow > 0; endrow--) {
893 if (ty + m_rowPos[endrow - 1] <= y + h + os)
896 if (!endrow && ty + m_rowPos[0] - table()->outerBorderTop() <= y + h + os)
900 unsigned startcol = 0;
901 unsigned endcol = totalCols;
902 // FIXME: Implement RTL.
903 if (!m_hasOverflowingCell && style()->direction() == LTR) {
904 for (; startcol < totalCols; startcol++) {
905 if (tx + table()->columnPositions()[startcol + 1] >= x - os)
908 if (startcol == totalCols && tx + table()->columnPositions()[totalCols] + table()->outerBorderRight() >= x - os)
911 for (; endcol > 0; endcol--) {
912 if (tx + table()->columnPositions()[endcol - 1] <= x + w + os)
915 if (!endcol && tx + table()->columnPositions()[0] - table()->outerBorderLeft() <= y + w + os)
919 if (startcol < endcol) {
921 for (unsigned r = startrow; r < endrow; r++) {
922 unsigned c = startcol;
923 // since a cell can be -1 (indicating a colspan) we might have to search backwards to include it
924 while (c && cellAt(r, c).inColSpan)
926 for (; c < endcol; c++) {
927 CellStruct current = cellAt(r, c);
928 RenderTableCell* cell = current.cell;
930 // Cells must always paint in the order in which they appear taking into account
931 // their upper left originating row/column. For cells with rowspans, avoid repainting
932 // if we've already seen the cell.
933 if (!cell || (r > startrow && (cellAt(r - 1, c).cell == cell)))
936 RenderTableRow* row = static_cast<RenderTableRow*>(cell->parent());
938 if (paintPhase == PaintPhaseBlockBackground || paintPhase == PaintPhaseChildBlockBackground) {
939 // We need to handle painting a stack of backgrounds. This stack (from bottom to top) consists of
940 // the column group, column, row group, row, and then the cell.
941 RenderObject* col = table()->colElement(c);
942 RenderObject* colGroup = 0;
943 if (col && col->parent()->style()->display() == TABLE_COLUMN_GROUP)
944 colGroup = col->parent();
946 // Column groups and columns first.
947 // FIXME: Columns and column groups do not currently support opacity, and they are being painted "too late" in
948 // the stack, since we have already opened a transparency layer (potentially) for the table row group.
949 // Note that we deliberately ignore whether or not the cell has a layer, since these backgrounds paint "behind" the
951 cell->paintBackgroundsBehindCell(paintInfo, tx, ty, colGroup);
952 cell->paintBackgroundsBehindCell(paintInfo, tx, ty, col);
954 // Paint the row group next.
955 cell->paintBackgroundsBehindCell(paintInfo, tx, ty, this);
957 // Paint the row next, but only if it doesn't have a layer. If a row has a layer, it will be responsible for
958 // painting the row background for the cell.
959 if (!row->hasLayer())
960 cell->paintBackgroundsBehindCell(paintInfo, tx, ty, row);
963 if ((!cell->hasLayer() && !row->hasLayer()) || paintInfo.phase == PaintPhaseCollapsedTableBorders)
964 cell->paint(paintInfo, tx, ty);
970 void RenderTableSection::imageChanged(WrappedImagePtr image)
972 // FIXME: Examine cells and repaint only the rect the image paints in.
976 void RenderTableSection::recalcCells()
983 for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
984 if (row->isTableRow()) {
987 if (!ensureRows(m_cRow + 1))
989 m_grid[m_cRow].rowRenderer = row;
991 for (RenderObject* cell = row->firstChild(); cell; cell = cell->nextSibling()) {
992 if (cell->isTableCell())
993 addCell(static_cast<RenderTableCell*>(cell), row);
997 m_needsCellRecalc = false;
998 setNeedsLayout(true);
1001 void RenderTableSection::clearGrid()
1003 int rows = m_gridRows;
1005 delete m_grid[rows].row;
1008 int RenderTableSection::numColumns() const
1012 for (int r = 0; r < m_gridRows; ++r) {
1013 for (int c = result; c < table()->numEffCols(); ++c) {
1014 const CellStruct& cell = cellAt(r, c);
1015 if (cell.cell || cell.inColSpan)
1023 void RenderTableSection::appendColumn(int pos)
1025 for (int row = 0; row < m_gridRows; ++row) {
1026 m_grid[row].row->resize(pos + 1);
1027 CellStruct& c = cellAt(row, pos);
1029 c.inColSpan = false;
1033 void RenderTableSection::splitColumn(int pos, int newSize)
1037 for (int row = 0; row < m_gridRows; ++row) {
1038 m_grid[row].row->resize(newSize);
1039 Row& r = *m_grid[row].row;
1040 memmove(r.data() + pos + 1, r.data() + pos, (newSize - 1 - pos) * sizeof(CellStruct));
1041 r[pos + 1].cell = 0;
1042 r[pos + 1].inColSpan = r[pos].inColSpan || r[pos].cell;
1046 RenderObject* RenderTableSection::removeChildNode(RenderObject* child, bool fullRemove)
1048 setNeedsCellRecalc();
1049 return RenderContainer::removeChildNode(child, fullRemove);
1053 bool RenderTableSection::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int x, int y, int tx, int ty, HitTestAction action)
1055 // Table sections cannot ever be hit tested. Effectively they do not exist.
1056 // Just forward to our children always.
1060 for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
1061 // FIXME: We have to skip over inline flows, since they can show up inside table rows
1062 // at the moment (a demoted inline <form> for example). If we ever implement a
1063 // table-specific hit-test method (which we should do for performance reasons anyway),
1064 // then we can remove this check.
1065 if (!child->hasLayer() && !child->isInlineFlow() && child->nodeAtPoint(request, result, x, y, tx, ty, action)) {
1066 updateHitTestResult(result, IntPoint(x - tx, y - ty));
1074 } // namespace WebCore