RenderTableSection.cpp [plain text]
#include "config.h"
#include "RenderTableSection.h"
#include "CachedImage.h"
#include "Document.h"
#include "HitTestResult.h"
#include "HTMLNames.h"
#include "PaintInfo.h"
#include "RenderTableCell.h"
#include "RenderTableCol.h"
#include "RenderTableRow.h"
#include "RenderView.h"
#include <limits>
#include <wtf/HashSet.h>
#include <wtf/Vector.h>
using namespace std;
namespace WebCore {
using namespace HTMLNames;
static inline void setRowLogicalHeightToRowStyleLogicalHeightIfNotRelative(RenderTableSection::RowStruct* row)
{
ASSERT(row && row->rowRenderer);
row->logicalHeight = row->rowRenderer->style()->logicalHeight();
if (row->logicalHeight.isRelative())
row->logicalHeight = Length();
}
RenderTableSection::RenderTableSection(Node* node)
: RenderBox(node)
, m_gridRows(0)
, m_cCol(0)
, m_cRow(-1)
, m_outerBorderStart(0)
, m_outerBorderEnd(0)
, m_outerBorderBefore(0)
, m_outerBorderAfter(0)
, m_needsCellRecalc(false)
, m_hasOverflowingCell(false)
, m_hasMultipleCellLevels(false)
{
setInline(false); }
RenderTableSection::~RenderTableSection()
{
clearGrid();
}
void RenderTableSection::destroy()
{
RenderTable* recalcTable = table();
RenderBox::destroy();
if (recalcTable)
recalcTable->setNeedsSectionRecalc();
}
void RenderTableSection::addChild(RenderObject* child, RenderObject* beforeChild)
{
if (!beforeChild && isAfterContent(lastChild()))
beforeChild = lastChild();
if (!child->isTableRow()) {
RenderObject* last = beforeChild;
if (!last)
last = lastChild();
if (last && last->isAnonymous()) {
if (beforeChild == last)
beforeChild = last->firstChild();
last->addChild(child, beforeChild);
return;
}
RenderObject* lastBox = last;
while (lastBox && lastBox->parent()->isAnonymous() && !lastBox->isTableRow())
lastBox = lastBox->parent();
if (lastBox && lastBox->isAnonymous()) {
lastBox->addChild(child, beforeChild);
return;
}
RenderObject* row = new (renderArena()) RenderTableRow(document() );
RefPtr<RenderStyle> newStyle = RenderStyle::create();
newStyle->inheritFrom(style());
newStyle->setDisplay(TABLE_ROW);
row->setStyle(newStyle.release());
addChild(row, beforeChild);
row->addChild(child);
return;
}
if (beforeChild)
setNeedsCellRecalc();
++m_cRow;
m_cCol = 0;
if (!ensureRows(m_cRow + 1))
return;
m_grid[m_cRow].rowRenderer = toRenderTableRow(child);
if (!beforeChild)
setRowLogicalHeightToRowStyleLogicalHeightIfNotRelative(&m_grid[m_cRow]);
while (beforeChild && beforeChild->parent() != this)
beforeChild = beforeChild->parent();
ASSERT(!beforeChild || beforeChild->isTableRow());
RenderBox::addChild(child, beforeChild);
toRenderTableRow(child)->updateBeforeAndAfterContent();
}
void RenderTableSection::removeChild(RenderObject* oldChild)
{
setNeedsCellRecalc();
RenderBox::removeChild(oldChild);
}
bool RenderTableSection::ensureRows(int numRows)
{
int nRows = m_gridRows;
if (numRows > nRows) {
if (numRows > static_cast<int>(m_grid.size())) {
size_t maxSize = numeric_limits<size_t>::max() / sizeof(RowStruct);
if (static_cast<size_t>(numRows) > maxSize)
return false;
m_grid.grow(numRows);
}
m_gridRows = numRows;
int nCols = max(1, table()->numEffCols());
for (int r = nRows; r < numRows; r++) {
m_grid[r].row = new Row(nCols);
m_grid[r].rowRenderer = 0;
m_grid[r].baseline = 0;
m_grid[r].logicalHeight = Length();
}
}
return true;
}
void RenderTableSection::addCell(RenderTableCell* cell, RenderTableRow* row)
{
int rSpan = cell->rowSpan();
int cSpan = cell->colSpan();
Vector<RenderTable::ColumnStruct>& columns = table()->columns();
int nCols = columns.size();
while (m_cCol < nCols && (cellAt(m_cRow, m_cCol).hasCells() || cellAt(m_cRow, m_cCol).inColSpan))
m_cCol++;
if (rSpan == 1) {
Length logicalHeight = cell->style()->logicalHeight();
if (logicalHeight.isPositive() || (logicalHeight.isRelative() && logicalHeight.value() >= 0)) {
Length cRowLogicalHeight = m_grid[m_cRow].logicalHeight;
switch (logicalHeight.type()) {
case Percent:
if (!(cRowLogicalHeight.isPercent()) ||
(cRowLogicalHeight.isPercent() && cRowLogicalHeight.percent() < logicalHeight.percent()))
m_grid[m_cRow].logicalHeight = logicalHeight;
break;
case Fixed:
if (cRowLogicalHeight.type() < Percent ||
(cRowLogicalHeight.isFixed() && cRowLogicalHeight.value() < logicalHeight.value()))
m_grid[m_cRow].logicalHeight = logicalHeight;
break;
case Relative:
default:
break;
}
}
}
if (!ensureRows(m_cRow + rSpan))
return;
m_grid[m_cRow].rowRenderer = row;
int col = m_cCol;
bool inColSpan = false;
while (cSpan) {
int currentSpan;
if (m_cCol >= nCols) {
table()->appendColumn(cSpan);
currentSpan = cSpan;
} else {
if (cSpan < (int)columns[m_cCol].span)
table()->splitColumn(m_cCol, cSpan);
currentSpan = columns[m_cCol].span;
}
for (int r = 0; r < rSpan; r++) {
CellStruct& c = cellAt(m_cRow + r, m_cCol);
ASSERT(cell);
c.cells.append(cell);
if (c.cells.size() > 1)
m_hasMultipleCellLevels = true;
if (inColSpan)
c.inColSpan = true;
}
m_cCol++;
cSpan -= currentSpan;
inColSpan = true;
}
cell->setRow(m_cRow);
cell->setCol(table()->effColToCol(col));
}
void RenderTableSection::setCellLogicalWidths()
{
Vector<int>& columnPos = table()->columnPositions();
LayoutStateMaintainer statePusher(view());
for (int i = 0; i < m_gridRows; i++) {
Row& row = *m_grid[i].row;
int cols = row.size();
for (int j = 0; j < cols; j++) {
CellStruct& current = row[j];
RenderTableCell* cell = current.primaryCell();
if (!cell || current.inColSpan)
continue;
int endCol = j;
int cspan = cell->colSpan();
while (cspan && endCol < cols) {
ASSERT(endCol < (int)table()->columns().size());
cspan -= table()->columns()[endCol].span;
endCol++;
}
int w = columnPos[endCol] - columnPos[j] - table()->hBorderSpacing();
int oldLogicalWidth = cell->logicalWidth();
if (w != oldLogicalWidth) {
cell->setNeedsLayout(true);
if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout()) {
if (!statePusher.didPush()) {
statePusher.push(this, IntSize(x(), y()));
}
cell->repaint();
}
cell->updateLogicalWidth(w);
}
}
}
statePusher.pop(); }
int RenderTableSection::calcRowLogicalHeight()
{
#ifndef NDEBUG
setNeedsLayoutIsForbidden(true);
#endif
ASSERT(!needsLayout());
RenderTableCell* cell;
int spacing = table()->vBorderSpacing();
LayoutStateMaintainer statePusher(view());
m_rowPos.resize(m_gridRows + 1);
m_rowPos[0] = spacing;
for (int r = 0; r < m_gridRows; r++) {
m_rowPos[r + 1] = 0;
m_grid[r].baseline = 0;
int baseline = 0;
int bdesc = 0;
int ch = m_grid[r].logicalHeight.calcMinValue(0);
int pos = m_rowPos[r] + ch + (m_grid[r].rowRenderer ? spacing : 0);
m_rowPos[r + 1] = max(m_rowPos[r + 1], pos);
Row* row = m_grid[r].row;
int totalCols = row->size();
for (int c = 0; c < totalCols; c++) {
CellStruct& current = cellAt(r, c);
cell = current.primaryCell();
if (!cell || current.inColSpan)
continue;
if ((cell->row() + cell->rowSpan() - 1) > r)
continue;
int indx = max(r - cell->rowSpan() + 1, 0);
if (cell->overrideSize() != -1) {
if (!statePusher.didPush()) {
statePusher.push(this, IntSize(x(), y()));
}
cell->setOverrideSize(-1);
cell->setChildNeedsLayout(true, false);
cell->layoutIfNeeded();
}
int adjustedPaddingBefore = cell->paddingBefore() - cell->intrinsicPaddingBefore();
int adjustedPaddingAfter = cell->paddingAfter() - cell->intrinsicPaddingAfter();
int adjustedLogicalHeight = cell->logicalHeight() - (cell->intrinsicPaddingBefore() + cell->intrinsicPaddingAfter());
ch = cell->style()->logicalHeight().calcValue(0) +
(document()->inQuirksMode() ? 0 : (adjustedPaddingBefore + adjustedPaddingAfter +
cell->borderBefore() + cell->borderAfter()));
ch = max(ch, adjustedLogicalHeight);
pos = m_rowPos[indx] + ch + (m_grid[r].rowRenderer ? spacing : 0);
m_rowPos[r + 1] = max(m_rowPos[r + 1], pos);
EVerticalAlign va = cell->style()->verticalAlign();
if (va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB) {
int b = cell->cellBaselinePosition();
if (b > cell->borderBefore() + cell->paddingBefore()) {
baseline = max(baseline, b - cell->intrinsicPaddingBefore());
bdesc = max(bdesc, m_rowPos[indx] + ch - (b - cell->intrinsicPaddingBefore()));
}
}
}
if (baseline) {
m_rowPos[r + 1] = max(m_rowPos[r + 1], baseline + bdesc + (m_grid[r].rowRenderer ? spacing : 0));
m_grid[r].baseline = baseline;
}
m_rowPos[r + 1] = max(m_rowPos[r + 1], m_rowPos[r]);
}
#ifndef NDEBUG
setNeedsLayoutIsForbidden(false);
#endif
ASSERT(!needsLayout());
statePusher.pop();
return m_rowPos[m_gridRows];
}
void RenderTableSection::layout()
{
ASSERT(needsLayout());
LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), style()->isFlippedBlocksWritingMode());
for (RenderObject* child = children()->firstChild(); child; child = child->nextSibling()) {
if (child->isTableRow()) {
child->layoutIfNeeded();
ASSERT(!child->needsLayout());
}
}
statePusher.pop();
setNeedsLayout(false);
}
int RenderTableSection::layoutRows(int toAdd)
{
#ifndef NDEBUG
setNeedsLayoutIsForbidden(true);
#endif
ASSERT(!needsLayout());
int rHeight;
int rindx;
int totalRows = m_gridRows;
setLogicalWidth(table()->contentLogicalWidth());
m_overflow.clear();
m_hasOverflowingCell = false;
if (toAdd && totalRows && (m_rowPos[totalRows] || !nextSibling())) {
int totalHeight = m_rowPos[totalRows] + toAdd;
int dh = toAdd;
int totalPercent = 0;
int numAuto = 0;
for (int r = 0; r < totalRows; r++) {
if (m_grid[r].logicalHeight.isAuto())
numAuto++;
else if (m_grid[r].logicalHeight.isPercent())
totalPercent += m_grid[r].logicalHeight.percent();
}
if (totalPercent) {
int add = 0;
totalPercent = min(totalPercent, 100);
int rh = m_rowPos[1] - m_rowPos[0];
for (int r = 0; r < totalRows; r++) {
if (totalPercent > 0 && m_grid[r].logicalHeight.isPercent()) {
int toAdd = min(dh, static_cast<int>((totalHeight * m_grid[r].logicalHeight.percent() / 100) - rh));
toAdd = max(0, toAdd);
add += toAdd;
dh -= toAdd;
totalPercent -= m_grid[r].logicalHeight.percent();
}
if (r < totalRows - 1)
rh = m_rowPos[r + 2] - m_rowPos[r + 1];
m_rowPos[r + 1] += add;
}
}
if (numAuto) {
int add = 0;
for (int r = 0; r < totalRows; r++) {
if (numAuto > 0 && m_grid[r].logicalHeight.isAuto()) {
int toAdd = dh / numAuto;
add += toAdd;
dh -= toAdd;
numAuto--;
}
m_rowPos[r + 1] += add;
}
}
if (dh > 0 && m_rowPos[totalRows]) {
int tot = m_rowPos[totalRows];
int add = 0;
int prev = m_rowPos[0];
for (int r = 0; r < totalRows; r++) {
add += dh * (m_rowPos[r + 1] - prev) / tot;
prev = m_rowPos[r + 1];
m_rowPos[r + 1] += add;
}
}
}
int hspacing = table()->hBorderSpacing();
int vspacing = table()->vBorderSpacing();
int nEffCols = table()->numEffCols();
LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), style()->isFlippedBlocksWritingMode());
for (int r = 0; r < totalRows; r++) {
if (RenderTableRow* rowRenderer = m_grid[r].rowRenderer) {
rowRenderer->setLocation(IntPoint(0, m_rowPos[r]));
rowRenderer->setLogicalWidth(logicalWidth());
rowRenderer->setLogicalHeight(m_rowPos[r + 1] - m_rowPos[r] - vspacing);
rowRenderer->updateLayerTransform();
}
for (int c = 0; c < nEffCols; c++) {
CellStruct& cs = cellAt(r, c);
RenderTableCell* cell = cs.primaryCell();
if (!cell || cs.inColSpan)
continue;
rindx = cell->row();
rHeight = m_rowPos[rindx + cell->rowSpan()] - m_rowPos[rindx] - vspacing;
bool cellChildrenFlex = false;
bool flexAllChildren = cell->style()->logicalHeight().isFixed()
|| (!table()->style()->logicalHeight().isAuto() && rHeight != cell->logicalHeight());
for (RenderObject* o = cell->firstChild(); o; o = o->nextSibling()) {
if (!o->isText() && o->style()->logicalHeight().isPercent() && (flexAllChildren || o->isReplaced() || (o->isBox() && toRenderBox(o)->scrollsOverflow()))) {
if (!o->isTable() || toRenderTable(o)->hasSections()) {
o->setNeedsLayout(true, false);
cellChildrenFlex = true;
}
}
}
if (HashSet<RenderBox*>* percentHeightDescendants = cell->percentHeightDescendants()) {
HashSet<RenderBox*>::iterator end = percentHeightDescendants->end();
for (HashSet<RenderBox*>::iterator it = percentHeightDescendants->begin(); it != end; ++it) {
RenderBox* box = *it;
if (!box->isReplaced() && !box->scrollsOverflow() && !flexAllChildren)
continue;
while (box != cell) {
if (box->normalChildNeedsLayout())
break;
box->setChildNeedsLayout(true, false);
box = box->containingBlock();
ASSERT(box);
if (!box)
break;
}
cellChildrenFlex = true;
}
}
if (cellChildrenFlex) {
cell->setChildNeedsLayout(true, false);
cell->setOverrideSizeFromRowHeight(rHeight);
cell->layoutIfNeeded();
EVerticalAlign va = cell->style()->verticalAlign();
if (va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB) {
int b = cell->cellBaselinePosition();
if (b > cell->borderBefore() + cell->paddingBefore())
m_grid[r].baseline = max(m_grid[r].baseline, b);
}
}
int oldIntrinsicPaddingBefore = cell->intrinsicPaddingBefore();
int oldIntrinsicPaddingAfter = cell->intrinsicPaddingAfter();
int logicalHeightWithoutIntrinsicPadding = cell->logicalHeight() - oldIntrinsicPaddingBefore - oldIntrinsicPaddingAfter;
int intrinsicPaddingBefore = 0;
switch (cell->style()->verticalAlign()) {
case SUB:
case SUPER:
case TEXT_TOP:
case TEXT_BOTTOM:
case BASELINE: {
int b = cell->cellBaselinePosition();
if (b > cell->borderBefore() + cell->paddingBefore())
intrinsicPaddingBefore = getBaseline(r) - (b - oldIntrinsicPaddingBefore);
break;
}
case TOP:
break;
case MIDDLE:
intrinsicPaddingBefore = (rHeight - logicalHeightWithoutIntrinsicPadding) / 2;
break;
case BOTTOM:
intrinsicPaddingBefore = rHeight - logicalHeightWithoutIntrinsicPadding;
break;
default:
break;
}
int intrinsicPaddingAfter = rHeight - logicalHeightWithoutIntrinsicPadding - intrinsicPaddingBefore;
cell->setIntrinsicPaddingBefore(intrinsicPaddingBefore);
cell->setIntrinsicPaddingAfter(intrinsicPaddingAfter);
IntRect oldCellRect(cell->x(), cell->y() , cell->width(), cell->height());
IntPoint cellLocation(0, m_rowPos[rindx]);
if (!style()->isLeftToRightDirection())
cellLocation.setX(table()->columnPositions()[nEffCols] - table()->columnPositions()[table()->colToEffCol(cell->col() + cell->colSpan())] + hspacing);
else
cellLocation.setX(table()->columnPositions()[c] + hspacing);
cell->setLogicalLocation(cellLocation);
view()->addLayoutDelta(IntSize(oldCellRect.x() - cell->x(), oldCellRect.y() - cell->y()));
if (intrinsicPaddingBefore != oldIntrinsicPaddingBefore || intrinsicPaddingAfter != oldIntrinsicPaddingAfter)
cell->setNeedsLayout(true, false);
if (!cell->needsLayout() && view()->layoutState()->pageLogicalHeight() && view()->layoutState()->pageLogicalOffset(cell->logicalTop()) != cell->pageLogicalOffset())
cell->setChildNeedsLayout(true, false);
cell->layoutIfNeeded();
if (style()->isHorizontalWritingMode() && view()->layoutState()->pageLogicalHeight() && cell->height() != rHeight)
cell->setHeight(rHeight);
IntSize childOffset(cell->x() - oldCellRect.x(), cell->y() - oldCellRect.y());
if (childOffset.width() || childOffset.height()) {
view()->addLayoutDelta(childOffset);
if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout())
cell->repaintDuringLayoutIfMoved(oldCellRect);
}
}
}
#ifndef NDEBUG
setNeedsLayoutIsForbidden(false);
#endif
ASSERT(!needsLayout());
setLogicalHeight(m_rowPos[totalRows]);
for (int r = 0; r < totalRows; r++) {
for (int c = 0; c < nEffCols; c++) {
CellStruct& cs = cellAt(r, c);
RenderTableCell* cell = cs.primaryCell();
if (!cell || cs.inColSpan)
continue;
if (r < totalRows - 1 && cell == primaryCellAt(r + 1, c))
continue;
addOverflowFromChild(cell);
m_hasOverflowingCell |= cell->hasVisualOverflow();
}
}
statePusher.pop();
return height();
}
int RenderTableSection::calcOuterBorderBefore() const
{
int totalCols = table()->numEffCols();
if (!m_gridRows || !totalCols)
return 0;
unsigned borderWidth = 0;
const BorderValue& sb = style()->borderBefore();
if (sb.style() == BHIDDEN)
return -1;
if (sb.style() > BHIDDEN)
borderWidth = sb.width();
const BorderValue& rb = firstChild()->style()->borderBefore();
if (rb.style() == BHIDDEN)
return -1;
if (rb.style() > BHIDDEN && rb.width() > borderWidth)
borderWidth = rb.width();
bool allHidden = true;
for (int c = 0; c < totalCols; c++) {
const CellStruct& current = cellAt(0, c);
if (current.inColSpan || !current.hasCells())
continue;
const BorderValue& cb = current.primaryCell()->style()->borderBefore(); RenderTableCol* colGroup = table()->colElement(c);
if (colGroup) {
const BorderValue& gb = colGroup->style()->borderBefore();
if (gb.style() == BHIDDEN || cb.style() == BHIDDEN)
continue;
allHidden = false;
if (gb.style() > BHIDDEN && gb.width() > borderWidth)
borderWidth = gb.width();
if (cb.style() > BHIDDEN && cb.width() > borderWidth)
borderWidth = cb.width();
} else {
if (cb.style() == BHIDDEN)
continue;
allHidden = false;
if (cb.style() > BHIDDEN && cb.width() > borderWidth)
borderWidth = cb.width();
}
}
if (allHidden)
return -1;
return borderWidth / 2;
}
int RenderTableSection::calcOuterBorderAfter() const
{
int totalCols = table()->numEffCols();
if (!m_gridRows || !totalCols)
return 0;
unsigned borderWidth = 0;
const BorderValue& sb = style()->borderAfter();
if (sb.style() == BHIDDEN)
return -1;
if (sb.style() > BHIDDEN)
borderWidth = sb.width();
const BorderValue& rb = lastChild()->style()->borderAfter();
if (rb.style() == BHIDDEN)
return -1;
if (rb.style() > BHIDDEN && rb.width() > borderWidth)
borderWidth = rb.width();
bool allHidden = true;
for (int c = 0; c < totalCols; c++) {
const CellStruct& current = cellAt(m_gridRows - 1, c);
if (current.inColSpan || !current.hasCells())
continue;
const BorderValue& cb = current.primaryCell()->style()->borderAfter(); RenderTableCol* colGroup = table()->colElement(c);
if (colGroup) {
const BorderValue& gb = colGroup->style()->borderAfter();
if (gb.style() == BHIDDEN || cb.style() == BHIDDEN)
continue;
allHidden = false;
if (gb.style() > BHIDDEN && gb.width() > borderWidth)
borderWidth = gb.width();
if (cb.style() > BHIDDEN && cb.width() > borderWidth)
borderWidth = cb.width();
} else {
if (cb.style() == BHIDDEN)
continue;
allHidden = false;
if (cb.style() > BHIDDEN && cb.width() > borderWidth)
borderWidth = cb.width();
}
}
if (allHidden)
return -1;
return (borderWidth + 1) / 2;
}
int RenderTableSection::calcOuterBorderStart() const
{
int totalCols = table()->numEffCols();
if (!m_gridRows || !totalCols)
return 0;
unsigned borderWidth = 0;
const BorderValue& sb = style()->borderStart();
if (sb.style() == BHIDDEN)
return -1;
if (sb.style() > BHIDDEN)
borderWidth = sb.width();
if (RenderTableCol* colGroup = table()->colElement(0)) {
const BorderValue& gb = colGroup->style()->borderStart();
if (gb.style() == BHIDDEN)
return -1;
if (gb.style() > BHIDDEN && gb.width() > borderWidth)
borderWidth = gb.width();
}
bool allHidden = true;
for (int r = 0; r < m_gridRows; r++) {
const CellStruct& current = cellAt(r, 0);
if (!current.hasCells())
continue;
const BorderValue& cb = current.primaryCell()->style()->borderStart(); const BorderValue& rb = current.primaryCell()->parent()->style()->borderStart();
if (cb.style() == BHIDDEN || rb.style() == BHIDDEN)
continue;
allHidden = false;
if (cb.style() > BHIDDEN && cb.width() > borderWidth)
borderWidth = cb.width();
if (rb.style() > BHIDDEN && rb.width() > borderWidth)
borderWidth = rb.width();
}
if (allHidden)
return -1;
return (borderWidth + (table()->style()->isLeftToRightDirection() ? 0 : 1)) / 2;
}
int RenderTableSection::calcOuterBorderEnd() const
{
int totalCols = table()->numEffCols();
if (!m_gridRows || !totalCols)
return 0;
unsigned borderWidth = 0;
const BorderValue& sb = style()->borderEnd();
if (sb.style() == BHIDDEN)
return -1;
if (sb.style() > BHIDDEN)
borderWidth = sb.width();
if (RenderTableCol* colGroup = table()->colElement(totalCols - 1)) {
const BorderValue& gb = colGroup->style()->borderEnd();
if (gb.style() == BHIDDEN)
return -1;
if (gb.style() > BHIDDEN && gb.width() > borderWidth)
borderWidth = gb.width();
}
bool allHidden = true;
for (int r = 0; r < m_gridRows; r++) {
const CellStruct& current = cellAt(r, totalCols - 1);
if (!current.hasCells())
continue;
const BorderValue& cb = current.primaryCell()->style()->borderEnd(); const BorderValue& rb = current.primaryCell()->parent()->style()->borderEnd();
if (cb.style() == BHIDDEN || rb.style() == BHIDDEN)
continue;
allHidden = false;
if (cb.style() > BHIDDEN && cb.width() > borderWidth)
borderWidth = cb.width();
if (rb.style() > BHIDDEN && rb.width() > borderWidth)
borderWidth = rb.width();
}
if (allHidden)
return -1;
return (borderWidth + (table()->style()->isLeftToRightDirection() ? 1 : 0)) / 2;
}
void RenderTableSection::recalcOuterBorder()
{
m_outerBorderBefore = calcOuterBorderBefore();
m_outerBorderAfter = calcOuterBorderAfter();
m_outerBorderStart = calcOuterBorderStart();
m_outerBorderEnd = calcOuterBorderEnd();
}
int RenderTableSection::firstLineBoxBaseline() const
{
if (!m_gridRows)
return -1;
int firstLineBaseline = m_grid[0].baseline;
if (firstLineBaseline)
return firstLineBaseline + m_rowPos[0];
firstLineBaseline = -1;
Row* firstRow = m_grid[0].row;
for (size_t i = 0; i < firstRow->size(); ++i) {
CellStruct& cs = firstRow->at(i);
RenderTableCell* cell = cs.primaryCell();
if (cell)
firstLineBaseline = max(firstLineBaseline, cell->logicalTop() + cell->paddingBefore() + cell->borderBefore() + cell->contentLogicalHeight());
}
return firstLineBaseline;
}
void RenderTableSection::paint(PaintInfo& paintInfo, int tx, int ty)
{
if (needsLayout())
return;
unsigned totalRows = m_gridRows;
unsigned totalCols = table()->columns().size();
if (!totalRows || !totalCols)
return;
tx += x();
ty += y();
PaintPhase phase = paintInfo.phase;
bool pushedClip = pushContentsClip(paintInfo, tx, ty);
paintObject(paintInfo, tx, ty);
if (pushedClip)
popContentsClip(paintInfo, phase, tx, ty);
}
static inline bool compareCellPositions(RenderTableCell* elem1, RenderTableCell* elem2)
{
return elem1->row() < elem2->row();
}
void RenderTableSection::paintCell(RenderTableCell* cell, PaintInfo& paintInfo, int tx, int ty)
{
IntPoint cellPoint = flipForWritingMode(cell, IntPoint(tx, ty), ParentToChildFlippingAdjustment);
PaintPhase paintPhase = paintInfo.phase;
RenderTableRow* row = toRenderTableRow(cell->parent());
if (paintPhase == PaintPhaseBlockBackground || paintPhase == PaintPhaseChildBlockBackground) {
RenderObject* col = table()->colElement(cell->col());
RenderObject* colGroup = 0;
if (col && col->parent()->style()->display() == TABLE_COLUMN_GROUP)
colGroup = col->parent();
cell->paintBackgroundsBehindCell(paintInfo, cellPoint.x(), cellPoint.y(), colGroup);
cell->paintBackgroundsBehindCell(paintInfo, cellPoint.x(), cellPoint.y(), col);
cell->paintBackgroundsBehindCell(paintInfo, cellPoint.x(), cellPoint.y(), this);
if (!row->hasSelfPaintingLayer())
cell->paintBackgroundsBehindCell(paintInfo, cellPoint.x(), cellPoint.y(), row);
}
if ((!cell->hasSelfPaintingLayer() && !row->hasSelfPaintingLayer()) || paintInfo.phase == PaintPhaseCollapsedTableBorders)
cell->paint(paintInfo, cellPoint.x(), cellPoint.y());
}
void RenderTableSection::paintObject(PaintInfo& paintInfo, int tx, int ty)
{
unsigned totalRows = m_gridRows;
unsigned totalCols = table()->columns().size();
PaintPhase paintPhase = paintInfo.phase;
int os = 2 * maximalOutlineSize(paintPhase);
unsigned startrow = 0;
unsigned endrow = totalRows;
IntRect localRepaintRect = paintInfo.rect;
localRepaintRect.move(-tx, -ty);
if (style()->isFlippedBlocksWritingMode()) {
if (style()->isHorizontalWritingMode())
localRepaintRect.setY(height() - localRepaintRect.maxY());
else
localRepaintRect.setX(width() - localRepaintRect.maxX());
}
if (!m_hasOverflowingCell) {
int before = (style()->isHorizontalWritingMode() ? localRepaintRect.y() : localRepaintRect.x()) - os;
startrow = std::lower_bound(m_rowPos.begin(), m_rowPos.end(), before) - m_rowPos.begin();
if (startrow == m_rowPos.size() || (startrow > 0 && (m_rowPos[startrow] > before)))
--startrow;
int after = (style()->isHorizontalWritingMode() ? localRepaintRect.maxY() : localRepaintRect.maxX()) + os;
endrow = std::lower_bound(m_rowPos.begin(), m_rowPos.end(), after) - m_rowPos.begin();
if (endrow == m_rowPos.size())
--endrow;
if (!endrow && m_rowPos[0] - table()->outerBorderBefore() <= after)
++endrow;
}
unsigned startcol = 0;
unsigned endcol = totalCols;
if (!m_hasOverflowingCell && style()->isLeftToRightDirection()) {
int start = (style()->isHorizontalWritingMode() ? localRepaintRect.x() : localRepaintRect.y()) - os;
Vector<int>& columnPos = table()->columnPositions();
startcol = std::lower_bound(columnPos.begin(), columnPos.end(), start) - columnPos.begin();
if ((startcol == columnPos.size()) || (startcol > 0 && (columnPos[startcol] > start)))
--startcol;
int end = (style()->isHorizontalWritingMode() ? localRepaintRect.maxX() : localRepaintRect.maxY()) + os;
endcol = std::lower_bound(columnPos.begin(), columnPos.end(), end) - columnPos.begin();
if (endcol == columnPos.size())
--endcol;
if (!endcol && columnPos[0] - table()->outerBorderStart() <= end)
++endcol;
}
if (startcol < endcol) {
if (!m_hasMultipleCellLevels) {
for (unsigned r = startrow; r < endrow; r++) {
for (unsigned c = startcol; c < endcol; c++) {
CellStruct& current = cellAt(r, c);
RenderTableCell* cell = current.primaryCell();
if (!cell || (r > startrow && primaryCellAt(r - 1, c) == cell) || (c > startcol && primaryCellAt(r, c - 1) == cell))
continue;
paintCell(cell, paintInfo, tx, ty);
}
}
} else {
Vector<RenderTableCell*> cells;
HashSet<RenderTableCell*> spanningCells;
for (unsigned r = startrow; r < endrow; r++) {
for (unsigned c = startcol; c < endcol; c++) {
CellStruct& current = cellAt(r, c);
if (!current.hasCells())
continue;
for (unsigned i = 0; i < current.cells.size(); ++i) {
if (current.cells[i]->rowSpan() > 1 || current.cells[i]->colSpan() > 1) {
if (spanningCells.contains(current.cells[i]))
continue;
spanningCells.add(current.cells[i]);
}
cells.append(current.cells[i]);
}
}
}
std::stable_sort(cells.begin(), cells.end(), compareCellPositions);
int size = cells.size();
for (int i = 0; i < size; ++i)
paintCell(cells[i], paintInfo, tx, ty);
}
}
}
void RenderTableSection::imageChanged(WrappedImagePtr, const IntRect*)
{
repaint();
}
void RenderTableSection::recalcCells()
{
m_cCol = 0;
m_cRow = -1;
clearGrid();
m_gridRows = 0;
for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
if (row->isTableRow()) {
m_cRow++;
m_cCol = 0;
if (!ensureRows(m_cRow + 1))
break;
RenderTableRow* tableRow = toRenderTableRow(row);
m_grid[m_cRow].rowRenderer = tableRow;
setRowLogicalHeightToRowStyleLogicalHeightIfNotRelative(&m_grid[m_cRow]);
for (RenderObject* cell = row->firstChild(); cell; cell = cell->nextSibling()) {
if (cell->isTableCell())
addCell(toRenderTableCell(cell), tableRow);
}
}
}
m_needsCellRecalc = false;
setNeedsLayout(true);
}
void RenderTableSection::setNeedsCellRecalc()
{
m_needsCellRecalc = true;
if (RenderTable* t = table())
t->setNeedsSectionRecalc();
}
void RenderTableSection::clearGrid()
{
int rows = m_gridRows;
while (rows--)
delete m_grid[rows].row;
}
int RenderTableSection::numColumns() const
{
int result = 0;
for (int r = 0; r < m_gridRows; ++r) {
for (int c = result; c < table()->numEffCols(); ++c) {
const CellStruct& cell = cellAt(r, c);
if (cell.hasCells() || cell.inColSpan)
result = c;
}
}
return result + 1;
}
void RenderTableSection::appendColumn(int pos)
{
for (int row = 0; row < m_gridRows; ++row)
m_grid[row].row->resize(pos + 1);
}
void RenderTableSection::splitColumn(int pos, int first)
{
if (m_cCol > pos)
m_cCol++;
for (int row = 0; row < m_gridRows; ++row) {
Row& r = *m_grid[row].row;
r.insert(pos + 1, CellStruct());
if (r[pos].hasCells()) {
r[pos + 1].cells.append(r[pos].cells);
RenderTableCell* cell = r[pos].primaryCell();
ASSERT(cell);
int colleft = cell->colSpan() - r[pos].inColSpan;
if (first > colleft)
r[pos + 1].inColSpan = 0;
else
r[pos + 1].inColSpan = first + r[pos].inColSpan;
} else {
r[pos + 1].inColSpan = 0;
}
}
}
bool RenderTableSection::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const IntPoint& pointInContainer, int tx, int ty, HitTestAction action)
{
if (!firstChild())
return false;
tx += x();
ty += y();
if (hasOverflowClip() && !overflowClipRect(tx, ty).intersects(result.rectForPoint(pointInContainer)))
return false;
if (m_hasOverflowingCell) {
for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
if (child->isBox() && !toRenderBox(child)->hasSelfPaintingLayer()) {
IntPoint childPoint = flipForWritingMode(toRenderBox(child), IntPoint(tx, ty), ParentToChildFlippingAdjustment);
if (child->nodeAtPoint(request, result, pointInContainer, childPoint.x(), childPoint.y(), action)) {
updateHitTestResult(result, toPoint(pointInContainer - childPoint));
return true;
}
}
}
return false;
}
IntPoint location = pointInContainer - IntSize(tx, ty);
if (style()->isFlippedBlocksWritingMode()) {
if (style()->isHorizontalWritingMode())
location.setY(height() - location.y());
else
location.setX(width() - location.x());
}
int offsetInColumnDirection = style()->isHorizontalWritingMode() ? location.y() : location.x();
unsigned nextRow = std::upper_bound(m_rowPos.begin(), m_rowPos.end(), offsetInColumnDirection) - m_rowPos.begin();
if (nextRow == m_rowPos.size())
return false;
unsigned hitRow = nextRow > 0 ? nextRow - 1 : 0;
Vector<int>& columnPos = table()->columnPositions();
int offsetInRowDirection = style()->isHorizontalWritingMode() ? location.x() : location.y();
if (!style()->isLeftToRightDirection())
offsetInRowDirection = columnPos[columnPos.size() - 1] - offsetInRowDirection;
unsigned nextColumn = std::lower_bound(columnPos.begin(), columnPos.end(), offsetInRowDirection) - columnPos.begin();
if (nextColumn == columnPos.size())
return false;
unsigned hitColumn = nextColumn > 0 ? nextColumn - 1 : 0;
CellStruct& current = cellAt(hitRow, hitColumn);
if (!current.hasCells())
return false;
for (int i = current.cells.size() - 1; i >= 0; --i) {
RenderTableCell* cell = current.cells[i];
IntPoint cellPoint = flipForWritingMode(cell, IntPoint(tx, ty), ParentToChildFlippingAdjustment);
if (static_cast<RenderObject*>(cell)->nodeAtPoint(request, result, pointInContainer, cellPoint.x(), cellPoint.y(), action)) {
updateHitTestResult(result, toPoint(pointInContainer - cellPoint));
return true;
}
}
return false;
}
}