This source file includes following definitions.
- setRowLogicalHeightToRowStyleLogicalHeight
- updateLogicalHeightForCell
- m_hasMultipleCellLevels
- styleDidChange
- willBeRemovedFromTree
- addChild
- ensureRows
- addCell
- rowHasOnlySpanningCells
- populateSpanningRowsHeightFromCell
- distributeExtraRowSpanHeightToPercentRows
- updatePositionIncreasedWithRowHeight
- distributeExtraRowSpanHeightToAutoRows
- distributeExtraRowSpanHeightToRemainingRows
- cellIsFullyIncludedInOtherCell
- compareRowSpanCellsInHeightDistributionOrder
- isHeightNeededForRowHavingOnlySpanningCells
- calcRowHeightHavingOnlySpanningCells
- updateRowsHeightHavingOnlySpanningCells
- distributeRowSpanHeightToRows
- updateBaselineForCell
- calcRowLogicalHeight
- layout
- distributeExtraLogicalHeightToPercentRows
- distributeExtraLogicalHeightToAutoRows
- distributeRemainingExtraLogicalHeight
- distributeExtraLogicalHeightToRows
- shouldFlexCellChild
- layoutRows
- computeOverflowFromCells
- computeOverflowFromCells
- calcBlockDirectionOuterBorder
- calcInlineDirectionOuterBorder
- recalcOuterBorder
- firstLineBoxBaseline
- paint
- compareCellPositions
- compareCellPositionsWithOverflowingCells
- paintCell
- logicalRectForWritingModeAndDirection
- dirtiedRows
- dirtiedColumns
- spannedRows
- spannedColumns
- paintObject
- imageChanged
- recalcCells
- rowLogicalHeightChanged
- setNeedsCellRecalc
- numColumns
- borderAdjoiningStartCell
- borderAdjoiningEndCell
- firstRowCellAdjoiningTableStart
- firstRowCellAdjoiningTableEnd
- appendColumn
- splitColumn
- nodeAtPoint
- removeCachedCollapsedBorders
- setCachedCollapsedBorder
- cachedCollapsedBorder
- createAnonymousWithParentRenderer
- setLogicalPositionForCell
#include "config.h"
#include "core/rendering/RenderTableSection.h"
#include <limits>
#include "core/rendering/GraphicsContextAnnotator.h"
#include "core/rendering/HitTestResult.h"
#include "core/rendering/LayoutRectRecorder.h"
#include "core/rendering/PaintInfo.h"
#include "core/rendering/RenderTableCell.h"
#include "core/rendering/RenderTableCol.h"
#include "core/rendering/RenderTableRow.h"
#include "core/rendering/RenderView.h"
#include "core/rendering/SubtreeLayoutScope.h"
#include "wtf/HashSet.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
static unsigned gMinTableSizeToUseFastPaintPathWithOverflowingCell = 75 * 75;
static float gMaxAllowedOverflowingCellRatioForFastPaintPath = 0.1f;
static inline void setRowLogicalHeightToRowStyleLogicalHeight(RenderTableSection::RowStruct& row)
{
ASSERT(row.rowRenderer);
row.logicalHeight = row.rowRenderer->style()->logicalHeight();
}
static inline void updateLogicalHeightForCell(RenderTableSection::RowStruct& row, const RenderTableCell* cell)
{
if (cell->rowSpan() != 1)
return;
Length logicalHeight = cell->style()->logicalHeight();
if (logicalHeight.isPositive()) {
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;
default:
break;
}
}
}
RenderTableSection::RenderTableSection(Element* element)
: RenderBox(element)
, 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 (!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)
setRowLogicalHeightToRowStyleLogicalHeight(m_grid[insertionRow]);
if (beforeChild && beforeChild->parent() != this)
beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
ASSERT(!beforeChild || beforeChild->isTableRow());
RenderBox::addChild(child, beforeChild);
}
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();
const 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));
}
bool RenderTableSection::rowHasOnlySpanningCells(unsigned row)
{
unsigned totalCols = m_grid[row].row.size();
if (!totalCols)
return false;
for (unsigned col = 0; col < totalCols; col++) {
const CellStruct& rowSpanCell = cellAt(row, col);
if (rowSpanCell.cells.isEmpty())
return false;
if (rowSpanCell.cells[0]->rowSpan() == 1)
return false;
}
return true;
}
void RenderTableSection::populateSpanningRowsHeightFromCell(RenderTableCell* cell, struct SpanningRowsHeight& spanningRowsHeight)
{
const unsigned rowSpan = cell->rowSpan();
const unsigned rowIndex = cell->rowIndex();
spanningRowsHeight.spanningCellHeightIgnoringBorderSpacing = cell->logicalHeightForRowSizing();
spanningRowsHeight.rowHeight.resize(rowSpan);
spanningRowsHeight.totalRowsHeight = 0;
for (unsigned row = 0; row < rowSpan; row++) {
unsigned actualRow = row + rowIndex;
spanningRowsHeight.rowHeight[row] = m_rowPos[actualRow + 1] - m_rowPos[actualRow] - borderSpacingForRow(actualRow);
if (!spanningRowsHeight.rowHeight[row])
spanningRowsHeight.isAnyRowWithOnlySpanningCells |= rowHasOnlySpanningCells(actualRow);
spanningRowsHeight.totalRowsHeight += spanningRowsHeight.rowHeight[row];
spanningRowsHeight.spanningCellHeightIgnoringBorderSpacing -= borderSpacingForRow(actualRow);
}
spanningRowsHeight.spanningCellHeightIgnoringBorderSpacing += borderSpacingForRow(rowIndex + rowSpan - 1);
}
void RenderTableSection::distributeExtraRowSpanHeightToPercentRows(RenderTableCell* cell, int totalPercent, int& extraRowSpanningHeight, Vector<int>& rowsHeight)
{
if (!extraRowSpanningHeight || !totalPercent)
return;
const unsigned rowSpan = cell->rowSpan();
const unsigned rowIndex = cell->rowIndex();
int percent = min(totalPercent, 100);
const int tableHeight = m_rowPos[m_grid.size()] + extraRowSpanningHeight;
int accumulatedPositionIncrease = 0;
for (unsigned row = rowIndex; row < (rowIndex + rowSpan); row++) {
if (percent > 0 && extraRowSpanningHeight > 0) {
if (m_grid[row].logicalHeight.isPercent()) {
int toAdd = (tableHeight * m_grid[row].logicalHeight.percent() / 100) - rowsHeight[row - rowIndex];
toAdd = min(toAdd, extraRowSpanningHeight);
accumulatedPositionIncrease += toAdd;
extraRowSpanningHeight -= toAdd;
percent -= m_grid[row].logicalHeight.percent();
}
}
m_rowPos[row + 1] += accumulatedPositionIncrease;
}
}
static void updatePositionIncreasedWithRowHeight(long long extraHeight, long long rowHeight, long long totalHeight, int& accumulatedPositionIncrease, int& remainder)
{
COMPILE_ASSERT(sizeof(long long int) > sizeof(int), int_should_be_less_than_longlong);
accumulatedPositionIncrease += (extraHeight * rowHeight) / totalHeight;
remainder += (extraHeight * rowHeight) % totalHeight;
}
void RenderTableSection::distributeExtraRowSpanHeightToAutoRows(RenderTableCell* cell, int totalAutoRowsHeight, int& extraRowSpanningHeight, Vector<int>& rowsHeight)
{
if (!extraRowSpanningHeight || !totalAutoRowsHeight)
return;
const unsigned rowSpan = cell->rowSpan();
const unsigned rowIndex = cell->rowIndex();
int accumulatedPositionIncrease = 0;
int remainder = 0;
for (unsigned row = rowIndex; row < (rowIndex + rowSpan); row++) {
if (m_grid[row].logicalHeight.isAuto()) {
updatePositionIncreasedWithRowHeight(extraRowSpanningHeight, rowsHeight[row - rowIndex], totalAutoRowsHeight, accumulatedPositionIncrease, remainder);
if (remainder >= totalAutoRowsHeight) {
remainder -= totalAutoRowsHeight;
accumulatedPositionIncrease++;
}
}
m_rowPos[row + 1] += accumulatedPositionIncrease;
}
ASSERT(!remainder);
extraRowSpanningHeight -= accumulatedPositionIncrease;
}
void RenderTableSection::distributeExtraRowSpanHeightToRemainingRows(RenderTableCell* cell, int totalRemainingRowsHeight, int& extraRowSpanningHeight, Vector<int>& rowsHeight)
{
if (!extraRowSpanningHeight || !totalRemainingRowsHeight)
return;
const unsigned rowSpan = cell->rowSpan();
const unsigned rowIndex = cell->rowIndex();
int accumulatedPositionIncrease = 0;
int remainder = 0;
for (unsigned row = rowIndex; row < (rowIndex + rowSpan); row++) {
if (!m_grid[row].logicalHeight.isPercent()) {
updatePositionIncreasedWithRowHeight(extraRowSpanningHeight, rowsHeight[row - rowIndex], totalRemainingRowsHeight, accumulatedPositionIncrease, remainder);
if (remainder >= totalRemainingRowsHeight) {
remainder -= totalRemainingRowsHeight;
accumulatedPositionIncrease++;
}
}
m_rowPos[row + 1] += accumulatedPositionIncrease;
}
ASSERT(!remainder);
extraRowSpanningHeight -= accumulatedPositionIncrease;
}
static bool cellIsFullyIncludedInOtherCell(const RenderTableCell* cell1, const RenderTableCell* cell2)
{
return (cell1->rowIndex() >= cell2->rowIndex() && (cell1->rowIndex() + cell1->rowSpan()) <= (cell2->rowIndex() + cell2->rowSpan()));
}
static bool compareRowSpanCellsInHeightDistributionOrder(const RenderTableCell* cell1, const RenderTableCell* cell2)
{
if (cell1->rowIndex() == cell2->rowIndex() && cell1->rowSpan() == cell2->rowSpan())
return (cell1->logicalHeightForRowSizing() > cell2->logicalHeightForRowSizing());
if (cellIsFullyIncludedInOtherCell(cell1, cell2))
return true;
if (!cellIsFullyIncludedInOtherCell(cell2, cell1))
return (cell1->rowIndex() < cell2->rowIndex());
return false;
}
bool RenderTableSection::isHeightNeededForRowHavingOnlySpanningCells(unsigned row)
{
unsigned totalCols = m_grid[row].row.size();
if (!totalCols)
return false;
for (unsigned col = 0; col < totalCols; col++) {
const CellStruct& rowSpanCell = cellAt(row, col);
if (rowSpanCell.cells.size()) {
RenderTableCell* cell = rowSpanCell.cells[0];
const unsigned rowIndex = cell->rowIndex();
const unsigned rowSpan = cell->rowSpan();
int totalRowSpanCellHeight = 0;
for (unsigned row = 0; row < rowSpan; row++) {
unsigned actualRow = row + rowIndex;
totalRowSpanCellHeight += m_rowPos[actualRow + 1] - m_rowPos[actualRow];
}
totalRowSpanCellHeight -= borderSpacingForRow(rowIndex + rowSpan - 1);
if (totalRowSpanCellHeight < cell->logicalHeightForRowSizing())
return true;
}
}
return false;
}
unsigned RenderTableSection::calcRowHeightHavingOnlySpanningCells(unsigned row)
{
ASSERT(rowHasOnlySpanningCells(row));
unsigned totalCols = m_grid[row].row.size();
if (!totalCols)
return 0;
unsigned rowHeight = 0;
for (unsigned col = 0; col < totalCols; col++) {
const CellStruct& rowSpanCell = cellAt(row, col);
if (rowSpanCell.cells.size() && rowSpanCell.cells[0]->rowSpan() > 1)
rowHeight = max(rowHeight, rowSpanCell.cells[0]->logicalHeightForRowSizing() / rowSpanCell.cells[0]->rowSpan());
}
return rowHeight;
}
void RenderTableSection::updateRowsHeightHavingOnlySpanningCells(RenderTableCell* cell, struct SpanningRowsHeight& spanningRowsHeight)
{
ASSERT(spanningRowsHeight.rowHeight.size());
int accumulatedPositionIncrease = 0;
const unsigned rowSpan = cell->rowSpan();
const unsigned rowIndex = cell->rowIndex();
ASSERT_UNUSED(rowSpan, rowSpan == spanningRowsHeight.rowHeight.size());
for (unsigned row = 0; row < spanningRowsHeight.rowHeight.size(); row++) {
unsigned actualRow = row + rowIndex;
if (!spanningRowsHeight.rowHeight[row] && rowHasOnlySpanningCells(actualRow) && isHeightNeededForRowHavingOnlySpanningCells(actualRow)) {
spanningRowsHeight.rowHeight[row] = calcRowHeightHavingOnlySpanningCells(actualRow);
accumulatedPositionIncrease += spanningRowsHeight.rowHeight[row];
}
m_rowPos[actualRow + 1] += accumulatedPositionIncrease;
}
spanningRowsHeight.totalRowsHeight += accumulatedPositionIncrease;
}
void RenderTableSection::distributeRowSpanHeightToRows(SpanningRenderTableCells& rowSpanCells)
{
ASSERT(rowSpanCells.size());
std::sort(rowSpanCells.begin(), rowSpanCells.end(), compareRowSpanCellsInHeightDistributionOrder);
unsigned extraHeightToPropagate = 0;
unsigned lastRowIndex = 0;
unsigned lastRowSpan = 0;
for (unsigned i = 0; i < rowSpanCells.size(); i++) {
RenderTableCell* cell = rowSpanCells[i];
unsigned rowIndex = cell->rowIndex();
unsigned rowSpan = cell->rowSpan();
unsigned spanningCellEndIndex = rowIndex + rowSpan;
unsigned lastSpanningCellEndIndex = lastRowIndex + lastRowSpan;
if (rowIndex == lastRowIndex && rowSpan == lastRowSpan)
continue;
int originalBeforePosition = m_rowPos[spanningCellEndIndex];
if (spanningCellEndIndex == lastSpanningCellEndIndex)
originalBeforePosition -= extraHeightToPropagate;
if (extraHeightToPropagate) {
for (unsigned row = lastSpanningCellEndIndex + 1; row <= spanningCellEndIndex; row++)
m_rowPos[row] += extraHeightToPropagate;
}
lastRowIndex = rowIndex;
lastRowSpan = rowSpan;
struct SpanningRowsHeight spanningRowsHeight;
populateSpanningRowsHeightFromCell(cell, spanningRowsHeight);
if (spanningRowsHeight.isAnyRowWithOnlySpanningCells)
updateRowsHeightHavingOnlySpanningCells(cell, spanningRowsHeight);
if (!spanningRowsHeight.totalRowsHeight) {
if (spanningRowsHeight.spanningCellHeightIgnoringBorderSpacing)
m_rowPos[spanningCellEndIndex] += spanningRowsHeight.spanningCellHeightIgnoringBorderSpacing + borderSpacingForRow(spanningCellEndIndex - 1);
extraHeightToPropagate = m_rowPos[spanningCellEndIndex] - originalBeforePosition;
continue;
}
if (spanningRowsHeight.spanningCellHeightIgnoringBorderSpacing <= spanningRowsHeight.totalRowsHeight) {
extraHeightToPropagate = m_rowPos[rowIndex + rowSpan] - originalBeforePosition;
continue;
}
int totalPercent = 0;
int totalAutoRowsHeight = 0;
int totalRemainingRowsHeight = spanningRowsHeight.totalRowsHeight;
for (unsigned row = rowIndex; row < spanningCellEndIndex; row++) {
if (m_grid[row].logicalHeight.isPercent()) {
totalPercent += m_grid[row].logicalHeight.percent();
totalRemainingRowsHeight -= spanningRowsHeight.rowHeight[row - rowIndex];
} else if (m_grid[row].logicalHeight.isAuto()) {
totalAutoRowsHeight += spanningRowsHeight.rowHeight[row - rowIndex];
}
}
int extraRowSpanningHeight = spanningRowsHeight.spanningCellHeightIgnoringBorderSpacing - spanningRowsHeight.totalRowsHeight;
distributeExtraRowSpanHeightToPercentRows(cell, totalPercent, extraRowSpanningHeight, spanningRowsHeight.rowHeight);
distributeExtraRowSpanHeightToAutoRows(cell, totalAutoRowsHeight, extraRowSpanningHeight, spanningRowsHeight.rowHeight);
distributeExtraRowSpanHeightToRemainingRows(cell, totalRemainingRowsHeight, extraRowSpanningHeight, spanningRowsHeight.rowHeight);
ASSERT(!extraRowSpanningHeight);
extraHeightToPropagate = m_rowPos[spanningCellEndIndex] - originalBeforePosition;
}
if (extraHeightToPropagate) {
for (unsigned row = lastRowIndex + lastRowSpan + 1; row <= m_grid.size(); row++)
m_rowPos[row] += extraHeightToPropagate;
}
}
void RenderTableSection::updateBaselineForCell(RenderTableCell* cell, unsigned row, LayoutUnit& baselineDescent)
{
if (!cell->isBaselineAligned())
return;
LayoutUnit baselinePosition = cell->cellBaselinePosition() - cell->intrinsicPaddingBefore();
if (baselinePosition > cell->borderBefore() + (cell->paddingBefore() - cell->intrinsicPaddingBefore())) {
m_grid[row].baseline = max(m_grid[row].baseline, baselinePosition);
int cellStartRowBaselineDescent = 0;
if (cell->rowSpan() == 1) {
baselineDescent = max(baselineDescent, cell->logicalHeightForRowSizing() - baselinePosition);
cellStartRowBaselineDescent = baselineDescent;
}
m_rowPos[row + 1] = max<int>(m_rowPos[row + 1], m_rowPos[row] + m_grid[row].baseline + cellStartRowBaselineDescent);
}
}
int RenderTableSection::calcRowLogicalHeight()
{
#ifndef NDEBUG
SetLayoutNeededForbiddenScope layoutForbiddenScope(this);
#endif
ASSERT(!needsLayout());
RenderTableCell* cell;
LayoutStateMaintainer statePusher(*this);
m_rowPos.resize(m_grid.size() + 1);
if (this == table()->topSection())
m_rowPos[0] = table()->vBorderSpacing();
else
m_rowPos[0] = 0;
SpanningRenderTableCells rowSpanCells;
#ifndef NDEBUG
HashSet<const RenderTableCell*> uniqueCells;
#endif
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] + minimumValueForLength(m_grid[r].logicalHeight, 0).round(), 0);
Row& row = m_grid[r].row;
unsigned totalCols = row.size();
RenderTableCell* lastRowSpanCell = 0;
for (unsigned c = 0; c < totalCols; c++) {
CellStruct& current = cellAt(r, c);
for (unsigned i = 0; i < current.cells.size(); i++) {
cell = current.cells[i];
if (current.inColSpan && cell->rowSpan() == 1)
continue;
if (cell->rowSpan() > 1) {
if (lastRowSpanCell != cell && cell->rowIndex() == r) {
#ifndef NDEBUG
ASSERT(!uniqueCells.contains(cell));
uniqueCells.add(cell);
#endif
rowSpanCells.append(cell);
lastRowSpanCell = cell;
updateBaselineForCell(cell, r, baselineDescent);
}
continue;
}
ASSERT(cell->rowSpan() == 1);
if (cell->hasOverrideHeight()) {
if (!statePusher.didPush()) {
statePusher.push(*this, locationOffset());
}
cell->clearIntrinsicPadding();
cell->clearOverrideSize();
cell->forceChildLayout();
}
m_rowPos[r + 1] = max(m_rowPos[r + 1], m_rowPos[r] + cell->logicalHeightForRowSizing());
updateBaselineForCell(cell, r, baselineDescent);
}
}
m_rowPos[r + 1] += borderSpacingForRow(r);
m_rowPos[r + 1] = max(m_rowPos[r + 1], m_rowPos[r]);
}
if (!rowSpanCells.isEmpty())
distributeRowSpanHeightToRows(rowSpanCells);
ASSERT(!needsLayout());
return m_rowPos[m_grid.size()];
}
void RenderTableSection::layout()
{
ASSERT(needsLayout());
ASSERT(!needsCellRecalc());
ASSERT(!table()->needsSectionRecalc());
LayoutRectRecorder recorder(*this);
m_grid.shrinkToFit();
LayoutStateMaintainer statePusher(*this, locationOffset());
const Vector<int>& columnPos = table()->columnPositions();
SubtreeLayoutScope layouter(this);
for (unsigned r = 0; r < m_grid.size(); ++r) {
Row& row = m_grid[r].row;
unsigned cols = row.size();
for (unsigned startColumn = 0; startColumn < cols; ++startColumn) {
CellStruct& current = row[startColumn];
RenderTableCell* cell = current.primaryCell();
if (!cell || current.inColSpan)
continue;
LayoutRectRecorder cellRecorder(*cell);
unsigned endCol = startColumn;
unsigned cspan = cell->colSpan();
while (cspan && endCol < cols) {
ASSERT(endCol < table()->columns().size());
cspan -= table()->columns()[endCol].span;
endCol++;
}
int tableLayoutLogicalWidth = columnPos[endCol] - columnPos[startColumn] - table()->hBorderSpacing();
cell->setCellLogicalWidth(tableLayoutLogicalWidth, layouter);
}
if (RenderTableRow* rowRenderer = m_grid[r].rowRenderer) {
if (!rowRenderer->needsLayout())
rowRenderer->markForPaginationRelayoutIfNeeded(layouter);
rowRenderer->layoutIfNeeded();
}
}
clearNeedsLayout();
}
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;
}
static bool shouldFlexCellChild(RenderObject* cellDescendant)
{
return cellDescendant->isReplaced() || (cellDescendant->isBox() && toRenderBox(cellDescendant)->scrollsOverflow());
}
void RenderTableSection::layoutRows()
{
#ifndef NDEBUG
SetLayoutNeededForbiddenScope layoutForbiddenScope(this);
#endif
ASSERT(!needsLayout());
unsigned totalRows = m_grid.size();
setLogicalWidth(table()->contentLogicalWidth());
m_overflow.clear();
m_overflowingCells.clear();
m_forceSlowPaintPathWithOverflowingCell = false;
int vspacing = table()->vBorderSpacing();
unsigned nEffCols = table()->numEffCols();
LayoutStateMaintainer statePusher(*this, locationOffset());
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();
rowRenderer->clearAllOverflows();
rowRenderer->addVisualEffectOverflow();
}
int rowHeightIncreaseForPagination = 0;
for (unsigned c = 0; c < nEffCols; c++) {
CellStruct& cs = cellAt(r, c);
RenderTableCell* cell = cs.primaryCell();
if (!cell || cs.inColSpan)
continue;
LayoutRectRecorder cellRecorder(*cell);
int rowIndex = cell->rowIndex();
int rHeight = m_rowPos[rowIndex + cell->rowSpan()] - m_rowPos[rowIndex] - vspacing;
bool cellChildrenFlex = false;
bool flexAllChildren = cell->style()->logicalHeight().isFixed()
|| (!table()->style()->logicalHeight().isAuto() && rHeight != cell->logicalHeight());
for (RenderObject* child = cell->firstChild(); child; child = child->nextSibling()) {
if (!child->isText() && child->style()->logicalHeight().isPercent()
&& (flexAllChildren || shouldFlexCellChild(child))
&& (!child->isTable() || toRenderTable(child)->hasSections())) {
cellChildrenFlex = true;
break;
}
}
if (!cellChildrenFlex) {
if (TrackedRendererListHashSet* percentHeightDescendants = cell->percentHeightDescendants()) {
TrackedRendererListHashSet::iterator end = percentHeightDescendants->end();
for (TrackedRendererListHashSet::iterator it = percentHeightDescendants->begin(); it != end; ++it) {
if (flexAllChildren || shouldFlexCellChild(*it)) {
cellChildrenFlex = true;
break;
}
}
}
}
if (cellChildrenFlex) {
cell->setOverrideLogicalContentHeightFromRowHeight(rHeight);
cell->forceChildLayout();
if (cell->isBaselineAligned()) {
LayoutUnit baseline = cell->cellBaselinePosition();
if (baseline > cell->borderBefore() + cell->paddingBefore())
m_grid[r].baseline = max(m_grid[r].baseline, baseline);
}
}
SubtreeLayoutScope layouter(cell);
cell->computeIntrinsicPadding(rHeight, layouter);
LayoutRect oldCellRect = cell->frameRect();
setLogicalPositionForCell(cell, c);
if (!cell->needsLayout())
cell->markForPaginationRelayoutIfNeeded(layouter);
cell->layoutIfNeeded();
if (view()->layoutState()->pageLogicalHeight() && cell->logicalHeight() != rHeight) {
LayoutUnit oldLogicalHeight = cell->logicalHeight();
if (oldLogicalHeight > rHeight)
rowHeightIncreaseForPagination = max<int>(rowHeightIncreaseForPagination, oldLogicalHeight - rHeight);
cell->setLogicalHeight(rHeight);
cell->computeOverflow(oldLogicalHeight, false);
}
LayoutSize childOffset(cell->location() - oldCellRect.location());
if (childOffset.width() || childOffset.height()) {
if (!RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
view()->addLayoutDelta(childOffset);
if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout())
cell->repaintDuringLayoutIfMoved(oldCellRect);
}
}
if (rowHeightIncreaseForPagination) {
for (unsigned rowIndex = r + 1; rowIndex <= totalRows; rowIndex++)
m_rowPos[rowIndex] += rowHeightIncreaseForPagination;
for (unsigned c = 0; c < nEffCols; ++c) {
Vector<RenderTableCell*, 1>& cells = cellAt(r, c).cells;
for (size_t i = 0; i < cells.size(); ++i) {
LayoutUnit oldLogicalHeight = cells[i]->logicalHeight();
cells[i]->setLogicalHeight(oldLogicalHeight + rowHeightIncreaseForPagination);
cells[i]->computeOverflow(oldLogicalHeight, false);
}
}
}
}
ASSERT(!needsLayout());
setLogicalHeight(m_rowPos[totalRows]);
computeOverflowFromCells(totalRows, nEffCols);
}
void RenderTableSection::computeOverflowFromCells()
{
unsigned totalRows = m_grid.size();
unsigned nEffCols = table()->numEffCols();
computeOverflowFromCells(totalRows, nEffCols);
}
void RenderTableSection::computeOverflowFromCells(unsigned totalRows, unsigned nEffCols)
{
unsigned totalCellsCount = nEffCols * totalRows;
unsigned 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());
}
int RenderTableSection::calcBlockDirectionOuterBorder(BlockBorderSide side) const
{
unsigned totalCols = table()->numEffCols();
if (!m_grid.size() || !totalCols)
return 0;
unsigned borderWidth = 0;
const BorderValue& sb = side == BorderBefore ? style()->borderBefore() : style()->borderAfter();
if (sb.style() == BHIDDEN)
return -1;
if (sb.style() > BHIDDEN)
borderWidth = sb.width();
const BorderValue& rb = side == BorderBefore ? firstChild()->style()->borderBefore() : 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(side == BorderBefore ? 0 : m_grid.size() - 1, c);
if (current.inColSpan || !current.hasCells())
continue;
const RenderStyle* primaryCellStyle = current.primaryCell()->style();
const BorderValue& cb = side == BorderBefore ? primaryCellStyle->borderBefore() : primaryCellStyle->borderAfter();
RenderTableCol* colGroup = table()->colElement(c);
if (colGroup) {
const BorderValue& gb = side == BorderBefore ? colGroup->style()->borderBefore() : 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;
if (side == BorderAfter)
borderWidth++;
return borderWidth / 2;
}
int RenderTableSection::calcInlineDirectionOuterBorder(InlineBorderSide side) const
{
unsigned totalCols = table()->numEffCols();
if (!m_grid.size() || !totalCols)
return 0;
unsigned colIndex = side == BorderStart ? 0 : totalCols - 1;
unsigned borderWidth = 0;
const BorderValue& sb = side == BorderStart ? style()->borderStart() : style()->borderEnd();
if (sb.style() == BHIDDEN)
return -1;
if (sb.style() > BHIDDEN)
borderWidth = sb.width();
if (RenderTableCol* colGroup = table()->colElement(colIndex)) {
const BorderValue& gb = side == BorderStart ? colGroup->style()->borderStart() : 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, colIndex);
if (!current.hasCells())
continue;
const RenderStyle* primaryCellStyle = current.primaryCell()->style();
const RenderStyle* primaryCellParentStyle = current.primaryCell()->parent()->style();
const BorderValue& cb = side == BorderStart ? primaryCellStyle->borderStart() : primaryCellStyle->borderEnd();
const BorderValue& rb = side == BorderStart ? primaryCellParentStyle->borderStart() : primaryCellParentStyle->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;
if ((side == BorderStart) != table()->style()->isLeftToRightDirection())
borderWidth++;
return borderWidth / 2;
}
void RenderTableSection::recalcOuterBorder()
{
m_outerBorderBefore = calcBlockDirectionOuterBorder(BorderBefore);
m_outerBorderAfter = calcBlockDirectionOuterBorder(BorderAfter);
m_outerBorderStart = calcInlineDirectionOuterBorder(BorderStart);
m_outerBorderEnd = calcInlineDirectionOuterBorder(BorderEnd);
}
int RenderTableSection::firstLineBoxBaseline() const
{
if (!m_grid.size())
return -1;
int 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 && cell->contentLogicalHeight())
firstLineBaseline = max<int>(firstLineBaseline, cell->logicalTop() + cell->paddingBefore() + cell->borderBefore() + cell->contentLogicalHeight());
}
return firstLineBaseline;
}
void RenderTableSection::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
ANNOTATE_GRAPHICS_CONTEXT(paintInfo, this);
ASSERT(!needsLayout());
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, ForceContentsClip);
paintObject(paintInfo, adjustedPaintOffset);
if (pushedClip)
popContentsClip(paintInfo, phase, adjustedPaintOffset);
if ((phase == PaintPhaseOutline || phase == PaintPhaseSelfOutline) && style()->visibility() == VISIBLE)
paintOutline(paintInfo, 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) {
RenderTableCol* column = table()->colElement(cell->col());
RenderTableCol* columnGroup = column ? column->enclosingColumnGroup() : 0;
cell->paintBackgroundsBehindCell(paintInfo, cellPoint, columnGroup);
cell->paintBackgroundsBehindCell(paintInfo, cellPoint, column);
cell->paintBackgroundsBehindCell(paintInfo, cellPoint, this);
if (!row->hasSelfPaintingLayer())
cell->paintBackgroundsBehindCell(paintInfo, cellPoint, row);
}
if ((!cell->hasSelfPaintingLayer() && !row->hasSelfPaintingLayer()))
cell->paint(paintInfo, cellPoint);
}
LayoutRect RenderTableSection::logicalRectForWritingModeAndDirection(const LayoutRect& rect) const
{
LayoutRect tableAlignedRect(rect);
flipForWritingMode(tableAlignedRect);
if (!style()->isHorizontalWritingMode())
tableAlignedRect = tableAlignedRect.transposedRect();
const Vector<int>& columnPos = table()->columnPositions();
if (!style()->isLeftToRightDirection())
tableAlignedRect.setX(columnPos[columnPos.size() - 1] - tableAlignedRect.maxX());
return tableAlignedRect;
}
CellSpan RenderTableSection::dirtiedRows(const LayoutRect& damageRect) const
{
if (m_forceSlowPaintPathWithOverflowingCell)
return fullTableRowSpan();
CellSpan coveredRows = spannedRows(damageRect);
if (coveredRows.start() >= m_rowPos.size() - 1 && m_rowPos[m_rowPos.size() - 1] + table()->outerBorderAfter() >= damageRect.y())
--coveredRows.start();
if (!coveredRows.end() && m_rowPos[0] - table()->outerBorderBefore() <= damageRect.maxY())
++coveredRows.end();
return coveredRows;
}
CellSpan RenderTableSection::dirtiedColumns(const LayoutRect& damageRect) const
{
if (m_forceSlowPaintPathWithOverflowingCell)
return fullTableColumnSpan();
CellSpan coveredColumns = spannedColumns(damageRect);
const Vector<int>& columnPos = table()->columnPositions();
if (coveredColumns.start() >= columnPos.size() - 1 && columnPos[columnPos.size() - 1] + table()->outerBorderEnd() >= damageRect.x())
--coveredColumns.start();
if (!coveredColumns.end() && columnPos[0] - table()->outerBorderStart() <= damageRect.maxX())
++coveredColumns.end();
return coveredColumns;
}
CellSpan RenderTableSection::spannedRows(const LayoutRect& flippedRect) const
{
unsigned nextRow = std::upper_bound(m_rowPos.begin(), m_rowPos.end(), flippedRect.y()) - m_rowPos.begin();
if (nextRow == m_rowPos.size())
return CellSpan(m_rowPos.size() - 1, m_rowPos.size() - 1);
unsigned startRow = nextRow > 0 ? nextRow - 1 : 0;
unsigned endRow;
if (m_rowPos[nextRow] >= flippedRect.maxY())
endRow = nextRow;
else {
endRow = std::upper_bound(m_rowPos.begin() + nextRow, m_rowPos.end(), flippedRect.maxY()) - m_rowPos.begin();
if (endRow == m_rowPos.size())
endRow = m_rowPos.size() - 1;
}
return CellSpan(startRow, endRow);
}
CellSpan RenderTableSection::spannedColumns(const LayoutRect& flippedRect) const
{
const Vector<int>& columnPos = table()->columnPositions();
unsigned nextColumn = std::upper_bound(columnPos.begin(), columnPos.end(), flippedRect.x()) - columnPos.begin();
if (nextColumn == columnPos.size())
return CellSpan(columnPos.size() - 1, columnPos.size() - 1);
unsigned startColumn = nextColumn > 0 ? nextColumn - 1 : 0;
unsigned endColumn;
if (columnPos[nextColumn] >= flippedRect.maxX())
endColumn = nextColumn;
else {
endColumn = std::upper_bound(columnPos.begin() + nextColumn, columnPos.end(), flippedRect.maxX()) - columnPos.begin();
if (endColumn == columnPos.size())
endColumn = columnPos.size() - 1;
}
return CellSpan(startColumn, endColumn);
}
void RenderTableSection::paintObject(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
LayoutRect localRepaintRect = paintInfo.rect;
localRepaintRect.moveBy(-paintOffset);
LayoutRect tableAlignedRect = logicalRectForWritingModeAndDirection(localRepaintRect);
CellSpan dirtiedRows = this->dirtiedRows(tableAlignedRect);
CellSpan dirtiedColumns = this->dirtiedColumns(tableAlignedRect);
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.add(current.cells[i]).isNewEntry)
continue;
}
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);
setRowLogicalHeightToRowStyleLogicalHeight(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();
}
void RenderTableSection::rowLogicalHeightChanged(unsigned rowIndex)
{
if (needsCellRecalc())
return;
setRowLogicalHeightToRowStyleLogicalHeight(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;
}
const BorderValue& RenderTableSection::borderAdjoiningStartCell(const RenderTableCell* cell) const
{
ASSERT(cell->isFirstOrLastCellInRow());
return hasSameDirectionAs(cell) ? style()->borderStart() : style()->borderEnd();
}
const BorderValue& RenderTableSection::borderAdjoiningEndCell(const RenderTableCell* cell) const
{
ASSERT(cell->isFirstOrLastCellInRow());
return hasSameDirectionAs(cell) ? style()->borderEnd() : style()->borderStart();
}
const RenderTableCell* RenderTableSection::firstRowCellAdjoiningTableStart() const
{
unsigned adjoiningStartCellColumnIndex = hasSameDirectionAs(table()) ? 0 : table()->lastColumnIndex();
return cellAt(0, adjoiningStartCellColumnIndex).primaryCell();
}
const RenderTableCell* RenderTableSection::firstRowCellAdjoiningTableEnd() const
{
unsigned adjoiningEndCellColumnIndex = hasSameDirectionAs(table()) ? table()->lastColumnIndex() : 0;
return cellAt(0, adjoiningEndCellColumnIndex).primaryCell();
}
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.appendVector(r[pos].cells);
RenderTableCell* cell = r[pos].primaryCell();
ASSERT(cell);
ASSERT(cell->colSpan() >= (r[pos].inColSpan ? 1u : 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 HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
{
if (!firstChild())
return false;
LayoutPoint adjustedLocation = accumulatedOffset + location();
if (hasOverflowClip() && !locationInContainer.intersects(overflowClipRect(adjustedLocation)))
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, locationInContainer, childPoint, action)) {
updateHitTestResult(result, toLayoutPoint(locationInContainer.point() - childPoint));
return true;
}
}
}
return false;
}
recalcCellsIfNeeded();
LayoutRect hitTestRect = locationInContainer.boundingBox();
hitTestRect.moveBy(-adjustedLocation);
LayoutRect tableAlignedRect = logicalRectForWritingModeAndDirection(hitTestRect);
CellSpan rowSpan = spannedRows(tableAlignedRect);
CellSpan columnSpan = spannedColumns(tableAlignedRect);
for (unsigned hitRow = rowSpan.start(); hitRow < rowSpan.end(); ++hitRow) {
for (unsigned hitColumn = columnSpan.start(); hitColumn < columnSpan.end(); ++hitColumn) {
CellStruct& current = cellAt(hitRow, hitColumn);
if (!current.hasCells())
continue;
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, locationInContainer, cellPoint, action)) {
updateHitTestResult(result, locationInContainer.point() - toLayoutSize(cellPoint));
return true;
}
}
if (!result.isRectBasedTest())
break;
}
if (!result.isRectBasedTest())
break;
}
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_WITH_SECURITY_IMPLICATION(it != m_cellsCollapsedBorders.end());
return it->value;
}
RenderTableSection* RenderTableSection::createAnonymousWithParentRenderer(const RenderObject* parent)
{
RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(parent->style(), TABLE_ROW_GROUP);
RenderTableSection* newSection = new RenderTableSection(0);
newSection->setDocumentForAnonymous(&parent->document());
newSection->setStyle(newStyle.release());
return newSection;
}
void RenderTableSection::setLogicalPositionForCell(RenderTableCell* cell, unsigned effectiveColumn) const
{
LayoutPoint oldCellLocation = cell->location();
LayoutPoint cellLocation(0, m_rowPos[cell->rowIndex()]);
int horizontalBorderSpacing = table()->hBorderSpacing();
if (!style()->isLeftToRightDirection())
cellLocation.setX(table()->columnPositions()[table()->numEffCols()] - table()->columnPositions()[table()->colToEffCol(cell->col() + cell->colSpan())] + horizontalBorderSpacing);
else
cellLocation.setX(table()->columnPositions()[effectiveColumn] + horizontalBorderSpacing);
cell->setLogicalLocation(cellLocation);
if (!RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
view()->addLayoutDelta(oldCellLocation - cell->location());
}
}