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 unsigned gMinTableSizeToUseFastPaintPathWithOverflowingCell = 75 * 75;
static float gMaxAllowedOverflowingCellRatioForFastPaintPath = 0.1f;
static inline void setRowLogicalHeightToRowStyleLogicalHeightIfNotRelative(RenderTableSection::RowStruct& row)
{
ASSERT(row.rowRenderer);
row.logicalHeight = row.rowRenderer->style()->logicalHeight();
if (row.logicalHeight.isRelative())
row.logicalHeight = Length();
}
static inline void updateLogicalHeightForCell(RenderTableSection::RowStruct& row, const RenderTableCell* cell)
{
if (cell->rowSpan() != 1)
return;
Length logicalHeight = cell->style()->logicalHeight();
if (logicalHeight.isPositive() || (logicalHeight.isRelative() && logicalHeight.value() >= 0)) {
Length cRowLogicalHeight = row.logicalHeight;
switch (logicalHeight.type()) {
case Percent:
if (!(cRowLogicalHeight.isPercent())
|| (cRowLogicalHeight.isPercent() && cRowLogicalHeight.percent() < logicalHeight.percent()))
row.logicalHeight = logicalHeight;
break;
case Fixed:
if (cRowLogicalHeight.type() < Percent
|| (cRowLogicalHeight.isFixed() && cRowLogicalHeight.value() < logicalHeight.value()))
row.logicalHeight = logicalHeight;
break;
case Relative:
default:
break;
}
}
}
RenderTableSection::RenderTableSection(Node* node)
: RenderBox(node)
, m_cCol(0)
, m_cRow(0)
, m_outerBorderStart(0)
, m_outerBorderEnd(0)
, m_outerBorderBefore(0)
, m_outerBorderAfter(0)
, m_needsCellRecalc(false)
, m_hasMultipleCellLevels(false)
{
setInline(false); }
RenderTableSection::~RenderTableSection()
{
}
void RenderTableSection::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBox::styleDidChange(diff, oldStyle);
propagateStyleToAnonymousChildren();
RenderTable* table = this->table();
if (table && !table->selfNeedsLayout() && !table->normalChildNeedsLayout() && oldStyle && oldStyle->border() != style()->border())
table->invalidateCollapsedBorders();
}
void RenderTableSection::willBeRemovedFromTree()
{
RenderBox::willBeRemovedFromTree();
setNeedsCellRecalc();
}
void RenderTableSection::addChild(RenderObject* child, RenderObject* beforeChild)
{
if (!beforeChild)
beforeChild = afterPseudoElementRenderer();
if (!child->isTableRow()) {
RenderObject* last = beforeChild;
if (!last)
last = lastChild();
if (last && last->isAnonymous() && !last->isBeforeOrAfterContent()) {
if (beforeChild == last)
beforeChild = last->firstChild();
last->addChild(child, beforeChild);
return;
}
if (beforeChild && !beforeChild->isAnonymous() && beforeChild->parent() == this) {
RenderObject* row = beforeChild->previousSibling();
if (row && row->isTableRow() && row->isAnonymous()) {
row->addChild(child);
return;
}
}
RenderObject* lastBox = last;
while (lastBox && lastBox->parent()->isAnonymous() && !lastBox->isTableRow())
lastBox = lastBox->parent();
if (lastBox && lastBox->isAnonymous() && !lastBox->isBeforeOrAfterContent()) {
lastBox->addChild(child, beforeChild);
return;
}
RenderObject* row = RenderTableRow::createAnonymousWithParentRenderer(this);
addChild(row, beforeChild);
row->addChild(child);
return;
}
if (beforeChild)
setNeedsCellRecalc();
unsigned insertionRow = m_cRow;
++m_cRow;
m_cCol = 0;
ensureRows(m_cRow);
RenderTableRow* row = toRenderTableRow(child);
m_grid[insertionRow].rowRenderer = row;
row->setRowIndex(insertionRow);
if (!beforeChild)
setRowLogicalHeightToRowStyleLogicalHeightIfNotRelative(m_grid[insertionRow]);
if (beforeChild && beforeChild->parent() != this)
beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
ASSERT(!beforeChild || beforeChild->isTableRow());
RenderBox::addChild(child, beforeChild);
toRenderTableRow(child)->updateBeforeAndAfterContent();
}
void RenderTableSection::removeChild(RenderObject* oldChild)
{
setNeedsCellRecalc();
RenderBox::removeChild(oldChild);
}
void RenderTableSection::ensureRows(unsigned numRows)
{
if (numRows <= m_grid.size())
return;
unsigned oldSize = m_grid.size();
m_grid.grow(numRows);
unsigned effectiveColumnCount = max(1u, table()->numEffCols());
for (unsigned row = oldSize; row < m_grid.size(); ++row)
m_grid[row].row.grow(effectiveColumnCount);
}
void RenderTableSection::addCell(RenderTableCell* cell, RenderTableRow* row)
{
if (needsCellRecalc())
return;
unsigned rSpan = cell->rowSpan();
unsigned cSpan = cell->colSpan();
Vector<RenderTable::ColumnStruct>& columns = table()->columns();
unsigned nCols = columns.size();
unsigned insertionRow = row->rowIndex();
while (m_cCol < nCols && (cellAt(insertionRow, m_cCol).hasCells() || cellAt(insertionRow, m_cCol).inColSpan))
m_cCol++;
updateLogicalHeightForCell(m_grid[insertionRow], cell);
ensureRows(insertionRow + rSpan);
m_grid[insertionRow].rowRenderer = row;
unsigned col = m_cCol;
bool inColSpan = false;
while (cSpan) {
unsigned currentSpan;
if (m_cCol >= nCols) {
table()->appendColumn(cSpan);
currentSpan = cSpan;
} else {
if (cSpan < columns[m_cCol].span)
table()->splitColumn(m_cCol, cSpan);
currentSpan = columns[m_cCol].span;
}
for (unsigned r = 0; r < rSpan; r++) {
CellStruct& c = cellAt(insertionRow + 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->setCol(table()->effColToCol(col));
}
void RenderTableSection::setCellLogicalWidths()
{
Vector<int>& columnPos = table()->columnPositions();
LayoutStateMaintainer statePusher(view());
for (unsigned i = 0; i < m_grid.size(); i++) {
Row& row = m_grid[i].row;
unsigned cols = row.size();
for (unsigned j = 0; j < cols; j++) {
CellStruct& current = row[j];
RenderTableCell* cell = current.primaryCell();
if (!cell || current.inColSpan)
continue;
unsigned endCol = j;
unsigned cspan = cell->colSpan();
while (cspan && endCol < cols) {
ASSERT(endCol < 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, locationOffset());
}
cell->repaint();
}
cell->updateLogicalWidth(w);
}
}
}
statePusher.pop(); }
int RenderTableSection::calcRowLogicalHeight()
{
#ifndef NDEBUG
setNeedsLayoutIsForbidden(true);
#endif
ASSERT(!needsLayout());
RenderTableCell* cell;
int spacing = table()->vBorderSpacing();
RenderView* viewRenderer = view();
LayoutStateMaintainer statePusher(viewRenderer);
m_rowPos.resize(m_grid.size() + 1);
m_rowPos[0] = spacing;
for (unsigned r = 0; r < m_grid.size(); r++) {
m_grid[r].baseline = 0;
LayoutUnit baselineDescent = 0;
m_rowPos[r + 1] = max(m_rowPos[r] + minimumIntValueForLength(m_grid[r].logicalHeight, 0, viewRenderer), 0);
Row& row = m_grid[r].row;
unsigned totalCols = row.size();
for (unsigned c = 0; c < totalCols; c++) {
CellStruct& current = cellAt(r, c);
cell = current.primaryCell();
if (!cell || current.inColSpan)
continue;
if ((cell->rowIndex() + cell->rowSpan() - 1) != r)
continue;
unsigned cellStartRow = cell->rowIndex();
if (cell->hasOverrideHeight()) {
if (!statePusher.didPush()) {
statePusher.push(this, locationOffset());
}
cell->clearIntrinsicPadding();
cell->clearOverrideSize();
cell->setChildNeedsLayout(true, MarkOnlyThis);
cell->layoutIfNeeded();
}
int cellLogicalHeight = cell->logicalHeightForRowSizing();
m_rowPos[r + 1] = max(m_rowPos[r + 1], m_rowPos[cellStartRow] + cellLogicalHeight);
EVerticalAlign va = cell->style()->verticalAlign();
if (va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB) {
LayoutUnit baselinePosition = cell->cellBaselinePosition();
if (baselinePosition > cell->borderBefore() + cell->paddingBefore()) {
m_grid[r].baseline = max(m_grid[r].baseline, baselinePosition - cell->intrinsicPaddingBefore());
baselineDescent = max(baselineDescent, m_rowPos[cellStartRow] + cellLogicalHeight - (baselinePosition - cell->intrinsicPaddingBefore()));
}
}
}
if (m_grid[r].baseline)
m_rowPos[r + 1] = max<int>(m_rowPos[r + 1], m_grid[r].baseline + baselineDescent);
m_rowPos[r + 1] += m_grid[r].rowRenderer ? spacing : 0;
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_grid.size()];
}
void RenderTableSection::layout()
{
ASSERT(needsLayout());
ASSERT(!needsCellRecalc());
ASSERT(!table()->needsSectionRecalc());
LayoutStateMaintainer statePusher(view(), this, locationOffset(), style()->isFlippedBlocksWritingMode());
for (RenderObject* child = children()->firstChild(); child; child = child->nextSibling()) {
if (child->isTableRow()) {
child->layoutIfNeeded();
ASSERT(!child->needsLayout());
}
}
statePusher.pop();
setNeedsLayout(false);
}
void RenderTableSection::distributeExtraLogicalHeightToPercentRows(int& extraLogicalHeight, int totalPercent)
{
if (!totalPercent)
return;
unsigned totalRows = m_grid.size();
int totalHeight = m_rowPos[totalRows] + extraLogicalHeight;
int totalLogicalHeightAdded = 0;
totalPercent = min(totalPercent, 100);
int rowHeight = m_rowPos[1] - m_rowPos[0];
for (unsigned r = 0; r < totalRows; ++r) {
if (totalPercent > 0 && m_grid[r].logicalHeight.isPercent()) {
int toAdd = min<int>(extraLogicalHeight, (totalHeight * m_grid[r].logicalHeight.percent() / 100) - rowHeight);
toAdd = max(0, toAdd);
totalLogicalHeightAdded += toAdd;
extraLogicalHeight -= toAdd;
totalPercent -= m_grid[r].logicalHeight.percent();
}
ASSERT(totalRows >= 1);
if (r < totalRows - 1)
rowHeight = m_rowPos[r + 2] - m_rowPos[r + 1];
m_rowPos[r + 1] += totalLogicalHeightAdded;
}
}
void RenderTableSection::distributeExtraLogicalHeightToAutoRows(int& extraLogicalHeight, unsigned autoRowsCount)
{
if (!autoRowsCount)
return;
int totalLogicalHeightAdded = 0;
for (unsigned r = 0; r < m_grid.size(); ++r) {
if (autoRowsCount > 0 && m_grid[r].logicalHeight.isAuto()) {
int extraLogicalHeightForRow = extraLogicalHeight / autoRowsCount;
totalLogicalHeightAdded += extraLogicalHeightForRow;
extraLogicalHeight -= extraLogicalHeightForRow;
--autoRowsCount;
}
m_rowPos[r + 1] += totalLogicalHeightAdded;
}
}
void RenderTableSection::distributeRemainingExtraLogicalHeight(int& extraLogicalHeight)
{
unsigned totalRows = m_grid.size();
if (extraLogicalHeight <= 0 || !m_rowPos[totalRows])
return;
int totalRowSize = m_rowPos[totalRows];
int totalLogicalHeightAdded = 0;
int previousRowPosition = m_rowPos[0];
for (unsigned r = 0; r < totalRows; r++) {
totalLogicalHeightAdded += extraLogicalHeight * (m_rowPos[r + 1] - previousRowPosition) / totalRowSize;
previousRowPosition = m_rowPos[r + 1];
m_rowPos[r + 1] += totalLogicalHeightAdded;
}
extraLogicalHeight -= totalLogicalHeightAdded;
}
int RenderTableSection::distributeExtraLogicalHeightToRows(int extraLogicalHeight)
{
if (!extraLogicalHeight)
return extraLogicalHeight;
unsigned totalRows = m_grid.size();
if (!totalRows)
return extraLogicalHeight;
if (!m_rowPos[totalRows] && nextSibling())
return extraLogicalHeight;
unsigned autoRowsCount = 0;
int totalPercent = 0;
for (unsigned r = 0; r < totalRows; r++) {
if (m_grid[r].logicalHeight.isAuto())
++autoRowsCount;
else if (m_grid[r].logicalHeight.isPercent())
totalPercent += m_grid[r].logicalHeight.percent();
}
int remainingExtraLogicalHeight = extraLogicalHeight;
distributeExtraLogicalHeightToPercentRows(remainingExtraLogicalHeight, totalPercent);
distributeExtraLogicalHeightToAutoRows(remainingExtraLogicalHeight, autoRowsCount);
distributeRemainingExtraLogicalHeight(remainingExtraLogicalHeight);
return extraLogicalHeight - remainingExtraLogicalHeight;
}
void RenderTableSection::layoutRows()
{
#ifndef NDEBUG
setNeedsLayoutIsForbidden(true);
#endif
ASSERT(!needsLayout());
int rHeight;
unsigned rindx;
unsigned totalRows = m_grid.size();
setLogicalWidth(table()->contentLogicalWidth());
m_overflow.clear();
m_overflowingCells.clear();
m_forceSlowPaintPathWithOverflowingCell = false;
int hspacing = table()->hBorderSpacing();
int vspacing = table()->vBorderSpacing();
unsigned nEffCols = table()->numEffCols();
LayoutStateMaintainer statePusher(view(), this, locationOffset(), style()->isFlippedBlocksWritingMode());
for (unsigned r = 0; r < totalRows; r++) {
if (RenderTableRow* rowRenderer = m_grid[r].rowRenderer) {
rowRenderer->setLocation(LayoutPoint(0, m_rowPos[r]));
rowRenderer->setLogicalWidth(logicalWidth());
rowRenderer->setLogicalHeight(m_rowPos[r + 1] - m_rowPos[r] - vspacing);
rowRenderer->updateLayerTransform();
}
for (unsigned c = 0; c < nEffCols; c++) {
CellStruct& cs = cellAt(r, c);
RenderTableCell* cell = cs.primaryCell();
if (!cell || cs.inColSpan)
continue;
rindx = cell->rowIndex();
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, MarkOnlyThis);
cellChildrenFlex = true;
}
}
}
if (ListHashSet<RenderBox*>* percentHeightDescendants = cell->percentHeightDescendants()) {
ListHashSet<RenderBox*>::iterator end = percentHeightDescendants->end();
for (ListHashSet<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, MarkOnlyThis);
box = box->containingBlock();
ASSERT(box);
if (!box)
break;
}
cellChildrenFlex = true;
}
}
if (cellChildrenFlex) {
cell->setChildNeedsLayout(true, MarkOnlyThis);
cell->setOverrideHeightFromRowHeight(rHeight);
cell->layoutIfNeeded();
EVerticalAlign va = cell->style()->verticalAlign();
if (va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB) {
LayoutUnit baseline = cell->cellBaselinePosition();
if (baseline > cell->borderBefore() + cell->paddingBefore())
m_grid[r].baseline = max(m_grid[r].baseline, baseline);
}
}
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: {
LayoutUnit 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);
LayoutRect oldCellRect(cell->x(), cell->y() , cell->width(), cell->height());
LayoutPoint 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(oldCellRect.location() - cell->location());
if (intrinsicPaddingBefore != oldIntrinsicPaddingBefore || intrinsicPaddingAfter != oldIntrinsicPaddingAfter)
cell->setNeedsLayout(true, MarkOnlyThis);
if (!cell->needsLayout() && view()->layoutState()->pageLogicalHeight() && view()->layoutState()->pageLogicalOffset(cell->logicalTop()) != cell->pageLogicalOffset())
cell->setChildNeedsLayout(true, MarkOnlyThis);
cell->layoutIfNeeded();
if (view()->layoutState()->pageLogicalHeight() && cell->logicalHeight() != rHeight) {
if (cell->logicalHeight() > rHeight) {
unsigned delta = cell->logicalHeight() - rHeight;
for (unsigned rowIndex = rindx + cell->rowSpan(); rowIndex <= totalRows; rowIndex++)
m_rowPos[rowIndex] += delta;
rHeight = cell->logicalHeight();
} else
cell->setLogicalHeight(rHeight);
}
LayoutSize childOffset(cell->location() - oldCellRect.location());
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]);
unsigned totalCellsCount = nEffCols * totalRows;
int maxAllowedOverflowingCellsCount = totalCellsCount < gMinTableSizeToUseFastPaintPathWithOverflowingCell ? 0 : gMaxAllowedOverflowingCellRatioForFastPaintPath * totalCellsCount;
#ifndef NDEBUG
bool hasOverflowingCell = false;
#endif
for (unsigned r = 0; r < totalRows; r++) {
for (unsigned 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);
#ifndef NDEBUG
hasOverflowingCell |= cell->hasVisualOverflow();
#endif
if (cell->hasVisualOverflow() && !m_forceSlowPaintPathWithOverflowingCell) {
m_overflowingCells.add(cell);
if (m_overflowingCells.size() > maxAllowedOverflowingCellsCount) {
m_forceSlowPaintPathWithOverflowingCell = true;
m_overflowingCells.clear();
}
}
}
}
ASSERT(hasOverflowingCell == this->hasOverflowingCell());
statePusher.pop();
}
int RenderTableSection::calcOuterBorderBefore() const
{
unsigned totalCols = table()->numEffCols();
if (!m_grid.size() || !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 (unsigned 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
{
unsigned totalCols = table()->numEffCols();
if (!m_grid.size() || !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 (unsigned c = 0; c < totalCols; c++) {
const CellStruct& current = cellAt(m_grid.size() - 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
{
unsigned totalCols = table()->numEffCols();
if (!m_grid.size() || !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 (unsigned r = 0; r < m_grid.size(); 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
{
unsigned totalCols = table()->numEffCols();
if (!m_grid.size() || !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 (unsigned r = 0; r < m_grid.size(); 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();
}
LayoutUnit RenderTableSection::firstLineBoxBaseline() const
{
if (!m_grid.size())
return -1;
LayoutUnit firstLineBaseline = m_grid[0].baseline;
if (firstLineBaseline)
return firstLineBaseline + m_rowPos[0];
firstLineBaseline = -1;
const Row& firstRow = m_grid[0].row;
for (size_t i = 0; i < firstRow.size(); ++i) {
const CellStruct& cs = firstRow.at(i);
const RenderTableCell* cell = cs.primaryCell();
if (cell)
firstLineBaseline = max(firstLineBaseline, cell->logicalTop() + cell->paddingBefore() + cell->borderBefore() + cell->contentLogicalHeight());
}
return firstLineBaseline;
}
void RenderTableSection::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (needsLayout())
return;
unsigned totalRows = m_grid.size();
unsigned totalCols = table()->columns().size();
if (!totalRows || !totalCols)
return;
LayoutPoint adjustedPaintOffset = paintOffset + location();
PaintPhase phase = paintInfo.phase;
bool pushedClip = pushContentsClip(paintInfo, adjustedPaintOffset);
paintObject(paintInfo, adjustedPaintOffset);
if (pushedClip)
popContentsClip(paintInfo, phase, adjustedPaintOffset);
if ((phase == PaintPhaseOutline || phase == PaintPhaseSelfOutline) && style()->visibility() == VISIBLE)
paintOutline(paintInfo.context, LayoutRect(adjustedPaintOffset, size()));
}
static inline bool compareCellPositions(RenderTableCell* elem1, RenderTableCell* elem2)
{
return elem1->rowIndex() < elem2->rowIndex();
}
static inline bool compareCellPositionsWithOverflowingCells(RenderTableCell* elem1, RenderTableCell* elem2)
{
if (elem1->rowIndex() != elem2->rowIndex())
return elem1->rowIndex() < elem2->rowIndex();
return elem1->col() < elem2->col();
}
void RenderTableSection::paintCell(RenderTableCell* cell, PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
LayoutPoint cellPoint = flipForWritingModeForChild(cell, paintOffset);
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, colGroup);
cell->paintBackgroundsBehindCell(paintInfo, cellPoint, col);
cell->paintBackgroundsBehindCell(paintInfo, cellPoint, this);
if (!row->hasSelfPaintingLayer())
cell->paintBackgroundsBehindCell(paintInfo, cellPoint, row);
}
if ((!cell->hasSelfPaintingLayer() && !row->hasSelfPaintingLayer()))
cell->paint(paintInfo, cellPoint);
}
CellSpan RenderTableSection::dirtiedRows(const LayoutRect& damageRect) const
{
if (m_forceSlowPaintPathWithOverflowingCell)
return fullTableRowSpan();
LayoutUnit before = style()->isHorizontalWritingMode() ? damageRect.y() : damageRect.x();
unsigned 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;
LayoutUnit after = (style()->isHorizontalWritingMode() ? damageRect.maxY() : damageRect.maxX());
unsigned 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;
return CellSpan(startRow, endRow);
}
CellSpan RenderTableSection::dirtiedColumns(const LayoutRect& damageRect) const
{
if (m_forceSlowPaintPathWithOverflowingCell)
return fullTableColumnSpan();
if (!style()->isLeftToRightDirection())
return fullTableColumnSpan();
LayoutUnit start = style()->isHorizontalWritingMode() ? damageRect.x() : damageRect.y();
Vector<int>& columnPos = table()->columnPositions();
unsigned startCol = std::lower_bound(columnPos.begin(), columnPos.end(), start) - columnPos.begin();
if ((startCol == columnPos.size()) || (startCol > 0 && (columnPos[startCol] > start)))
--startCol;
LayoutUnit end = (style()->isHorizontalWritingMode() ? damageRect.maxX() : damageRect.maxY());
unsigned endCol = std::lower_bound(columnPos.begin(), columnPos.end(), end) - columnPos.begin();
if (endCol == columnPos.size())
--endCol;
if (!endCol && columnPos[0] - table()->outerBorderStart() <= end)
++endCol;
return CellSpan(startCol, endCol);
}
void RenderTableSection::paintObject(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
PaintPhase paintPhase = paintInfo.phase;
LayoutRect localRepaintRect = paintInfo.rect;
localRepaintRect.moveBy(-paintOffset);
if (style()->isFlippedBlocksWritingMode()) {
if (style()->isHorizontalWritingMode())
localRepaintRect.setY(height() - localRepaintRect.maxY());
else
localRepaintRect.setX(width() - localRepaintRect.maxX());
}
localRepaintRect.inflate(maximalOutlineSize(paintPhase));
CellSpan dirtiedRows = this->dirtiedRows(localRepaintRect);
CellSpan dirtiedColumns = this->dirtiedColumns(localRepaintRect);
if (dirtiedColumns.start() < dirtiedColumns.end()) {
if (!m_hasMultipleCellLevels && !m_overflowingCells.size()) {
if (paintInfo.phase == PaintPhaseCollapsedTableBorders) {
for (unsigned r = dirtiedRows.end(); r > dirtiedRows.start(); r--) {
unsigned row = r - 1;
for (unsigned c = dirtiedColumns.end(); c > dirtiedColumns.start(); c--) {
unsigned col = c - 1;
CellStruct& current = cellAt(row, col);
RenderTableCell* cell = current.primaryCell();
if (!cell || (row > dirtiedRows.start() && primaryCellAt(row - 1, col) == cell) || (col > dirtiedColumns.start() && primaryCellAt(row, col - 1) == cell))
continue;
LayoutPoint cellPoint = flipForWritingModeForChild(cell, paintOffset);
cell->paintCollapsedBorders(paintInfo, cellPoint);
}
}
} else {
for (unsigned r = dirtiedRows.start(); r < dirtiedRows.end(); r++) {
RenderTableRow* row = m_grid[r].rowRenderer;
if (row && !row->hasSelfPaintingLayer())
row->paintOutlineForRowIfNeeded(paintInfo, paintOffset);
for (unsigned c = dirtiedColumns.start(); c < dirtiedColumns.end(); c++) {
CellStruct& current = cellAt(r, c);
RenderTableCell* cell = current.primaryCell();
if (!cell || (r > dirtiedRows.start() && primaryCellAt(r - 1, c) == cell) || (c > dirtiedColumns.start() && primaryCellAt(r, c - 1) == cell))
continue;
paintCell(cell, paintInfo, paintOffset);
}
}
}
} else {
#ifndef NDEBUG
unsigned totalRows = m_grid.size();
unsigned totalCols = table()->columns().size();
ASSERT(m_overflowingCells.size() < totalRows * totalCols * gMaxAllowedOverflowingCellRatioForFastPaintPath);
#endif
Vector<RenderTableCell*> cells;
copyToVector(m_overflowingCells, cells);
HashSet<RenderTableCell*> spanningCells;
for (unsigned r = dirtiedRows.start(); r < dirtiedRows.end(); r++) {
RenderTableRow* row = m_grid[r].rowRenderer;
if (row && !row->hasSelfPaintingLayer())
row->paintOutlineForRowIfNeeded(paintInfo, paintOffset);
for (unsigned c = dirtiedColumns.start(); c < dirtiedColumns.end(); c++) {
CellStruct& current = cellAt(r, c);
if (!current.hasCells())
continue;
for (unsigned i = 0; i < current.cells.size(); ++i) {
if (m_overflowingCells.contains(current.cells[i]))
continue;
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]);
}
}
}
if (!m_overflowingCells.size())
std::stable_sort(cells.begin(), cells.end(), compareCellPositions);
else
std::sort(cells.begin(), cells.end(), compareCellPositionsWithOverflowingCells);
if (paintInfo.phase == PaintPhaseCollapsedTableBorders) {
for (unsigned i = cells.size(); i > 0; --i) {
LayoutPoint cellPoint = flipForWritingModeForChild(cells[i - 1], paintOffset);
cells[i - 1]->paintCollapsedBorders(paintInfo, cellPoint);
}
} else {
for (unsigned i = 0; i < cells.size(); ++i)
paintCell(cells[i], paintInfo, paintOffset);
}
}
}
}
void RenderTableSection::imageChanged(WrappedImagePtr, const IntRect*)
{
repaint();
}
void RenderTableSection::recalcCells()
{
ASSERT(m_needsCellRecalc);
m_needsCellRecalc = false;
m_cCol = 0;
m_cRow = 0;
m_grid.clear();
for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
if (row->isTableRow()) {
unsigned insertionRow = m_cRow;
m_cRow++;
m_cCol = 0;
ensureRows(m_cRow);
RenderTableRow* tableRow = toRenderTableRow(row);
m_grid[insertionRow].rowRenderer = tableRow;
tableRow->setRowIndex(insertionRow);
setRowLogicalHeightToRowStyleLogicalHeightIfNotRelative(m_grid[insertionRow]);
for (RenderObject* cell = row->firstChild(); cell; cell = cell->nextSibling()) {
if (!cell->isTableCell())
continue;
RenderTableCell* tableCell = toRenderTableCell(cell);
addCell(tableCell, tableRow);
}
}
}
m_grid.shrinkToFit();
setNeedsLayout(true);
}
void RenderTableSection::rowLogicalHeightChanged(unsigned rowIndex)
{
if (needsCellRecalc())
return;
setRowLogicalHeightToRowStyleLogicalHeightIfNotRelative(m_grid[rowIndex]);
for (RenderObject* cell = m_grid[rowIndex].rowRenderer->firstChild(); cell; cell = cell->nextSibling()) {
if (!cell->isTableCell())
continue;
updateLogicalHeightForCell(m_grid[rowIndex], toRenderTableCell(cell));
}
}
void RenderTableSection::setNeedsCellRecalc()
{
m_needsCellRecalc = true;
if (RenderTable* t = table())
t->setNeedsSectionRecalc();
}
unsigned RenderTableSection::numColumns() const
{
unsigned result = 0;
for (unsigned r = 0; r < m_grid.size(); ++r) {
for (unsigned 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(unsigned pos)
{
ASSERT(!m_needsCellRecalc);
for (unsigned row = 0; row < m_grid.size(); ++row)
m_grid[row].row.resize(pos + 1);
}
void RenderTableSection::splitColumn(unsigned pos, unsigned first)
{
ASSERT(!m_needsCellRecalc);
if (m_cCol > pos)
m_cCol++;
for (unsigned row = 0; row < m_grid.size(); ++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);
ASSERT(cell->colSpan() >= (r[pos].inColSpan ? 1 : 0));
unsigned 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 LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
{
if (!firstChild())
return false;
LayoutPoint adjustedLocation = accumulatedOffset + location();
if (hasOverflowClip() && !overflowClipRect(adjustedLocation, result.region()).intersects(result.rectForPoint(pointInContainer)))
return false;
if (hasOverflowingCell()) {
for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
if (child->isBox() && !toRenderBox(child)->hasSelfPaintingLayer()) {
LayoutPoint childPoint = flipForWritingModeForChild(toRenderBox(child), adjustedLocation);
if (child->nodeAtPoint(request, result, pointInContainer, childPoint, action)) {
updateHitTestResult(result, toLayoutPoint(pointInContainer - childPoint));
return true;
}
}
}
return false;
}
LayoutPoint location = pointInContainer - toLayoutSize(adjustedLocation);
if (style()->isFlippedBlocksWritingMode()) {
if (style()->isHorizontalWritingMode())
location.setY(height() - location.y());
else
location.setX(width() - location.x());
}
LayoutUnit offsetInColumnDirection = style()->isHorizontalWritingMode() ? location.y() : location.x();
recalcCellsIfNeeded();
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();
LayoutUnit 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 (unsigned i = current.cells.size() ; i; ) {
--i;
RenderTableCell* cell = current.cells[i];
LayoutPoint cellPoint = flipForWritingModeForChild(cell, adjustedLocation);
if (static_cast<RenderObject*>(cell)->nodeAtPoint(request, result, pointInContainer, cellPoint, action)) {
updateHitTestResult(result, toLayoutPoint(pointInContainer - cellPoint));
return true;
}
}
return false;
}
void RenderTableSection::removeCachedCollapsedBorders(const RenderTableCell* cell)
{
if (!table()->collapseBorders())
return;
for (int side = CBSBefore; side <= CBSEnd; ++side)
m_cellsCollapsedBorders.remove(make_pair(cell, side));
}
void RenderTableSection::setCachedCollapsedBorder(const RenderTableCell* cell, CollapsedBorderSide side, CollapsedBorderValue border)
{
ASSERT(table()->collapseBorders());
m_cellsCollapsedBorders.set(make_pair(cell, side), border);
}
CollapsedBorderValue& RenderTableSection::cachedCollapsedBorder(const RenderTableCell* cell, CollapsedBorderSide side)
{
ASSERT(table()->collapseBorders());
HashMap<pair<const RenderTableCell*, int>, CollapsedBorderValue>::iterator it = m_cellsCollapsedBorders.find(make_pair(cell, side));
ASSERT(it != m_cellsCollapsedBorders.end());
return it->second;
}
RenderTableSection* RenderTableSection::createAnonymousWithParentRenderer(const RenderObject* parent)
{
RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(parent->style(), TABLE_ROW_GROUP);
RenderTableSection* newSection = new (parent->renderArena()) RenderTableSection(parent->document() );
newSection->setStyle(newStyle.release());
return newSection;
}
}