blob: 95c6f6d461a42865b57f0df010c738bee3a9698c [file]
/*
* Copyright (C) 1997 Martin Jones (mjones@kde.org)
* (C) 1997 Torben Weis (weis@kde.org)
* (C) 1998 Waldo Bastian (bastian@kde.org)
* (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* Copyright (C) 2003-2026 Apple Inc. All rights reserved.
* Copyright (C) 2015 Google Inc. All rights reserved.
* Copyright (C) 2026 Samuel Weinig <sam@webkit.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "RenderTableRow.h"
#include "BackgroundPainter.h"
#include "Document.h"
#include "HTMLNames.h"
#include "HitTestResult.h"
#include "PaintInfo.h"
#include "PaintInfoInlines.h"
#include "RenderBoxInlines.h"
#include "RenderBoxModelObjectInlines.h"
#include "RenderElementInlines.h"
#include "RenderElementStyleInlines.h"
#include "RenderLayoutState.h"
#include "RenderObjectInlines.h"
#include "RenderTableCell.h"
#include "RenderTreeBuilder.h"
#include "RenderView.h"
#include <wtf/StackStats.h>
#include <wtf/TZoneMallocInlines.h>
namespace WebCore {
using namespace HTMLNames;
WTF_MAKE_TZONE_ALLOCATED_IMPL(RenderTableRow);
RenderTableRow::RenderTableRow(Element& element, Style::ComputedStyle&& style)
: RenderBlock(Type::TableRow, element, WTF::move(style), { })
{
setInline(false);
ASSERT(isRenderTableRow());
}
RenderTableRow::RenderTableRow(Document& document, Style::ComputedStyle&& style)
: RenderBlock(Type::TableRow, document, WTF::move(style), { })
{
setInline(false);
ASSERT(isRenderTableRow());
}
RenderTableRow::~RenderTableRow() = default;
ASCIILiteral RenderTableRow::renderName() const
{
return (isAnonymous() || isPseudoElement()) ? "RenderTableRow (anonymous)"_s : "RenderTableRow"_s;
}
void RenderTableRow::willBeRemovedFromTree()
{
RenderBlock::willBeRemovedFromTree();
section()->setNeedsCellRecalc();
}
static bool borderWidthChanged(const Style::ComputedStyle* oldStyle, const Style::ComputedStyle* newStyle)
{
return oldStyle->usedBorderLeftWidth() != newStyle->usedBorderLeftWidth()
|| oldStyle->usedBorderTopWidth() != newStyle->usedBorderTopWidth()
|| oldStyle->usedBorderRightWidth() != newStyle->usedBorderRightWidth()
|| oldStyle->usedBorderBottomWidth() != newStyle->usedBorderBottomWidth();
}
void RenderTableRow::styleDidChange(Style::Difference diff, const Style::ComputedStyle* oldStyle)
{
ASSERT(style().display() == Style::DisplayType::TableRow);
RenderBlock::styleDidChange(diff, oldStyle);
propagateStyleToAnonymousChildren(StylePropagationType::AllChildren);
if (section() && oldStyle && style().logicalHeight() != oldStyle->logicalHeight())
section()->rowLogicalHeightChanged(rowIndex());
// If border was changed, notify table.
if (CheckedPtr table = this->table()) {
if (oldStyle)
table->invalidateCollapsedBordersAfterStyleChangeIfNeeded(*oldStyle, style());
if (oldStyle && diff == Style::DifferenceResult::Layout && needsLayout() && table->collapseBorders() && borderWidthChanged(oldStyle, &style())) {
// If the border width changes on a row, we need to make sure the cells in the row know to lay out again.
// This only happens when borders are collapsed, since they end up affecting the border sides of the cell
// itself.
auto propagateNeedsLayoutOnBorderSizeChange = [&] (auto& row) {
for (auto* cell = row.firstCell(); cell; cell = cell->nextCell())
cell->setNeedsLayoutAndInvalidateContentLogicalWidths();
};
propagateNeedsLayoutOnBorderSizeChange(*this);
if (auto* previousRow = this->previousRow())
propagateNeedsLayoutOnBorderSizeChange(*previousRow);
if (auto* nextRow = this->nextRow())
propagateNeedsLayoutOnBorderSizeChange(*nextRow);
}
}
}
const BorderValue& RenderTableRow::borderAdjoiningStartCell(const RenderTableCell& cell) const
{
ASSERT_UNUSED(cell, cell.isFirstOrLastCellInRow());
// FIXME: https://webkit.org/b/79272 - Add support for mixed directionality at the cell level.
return style().borderStart(table()->writingMode());
}
const BorderValue& RenderTableRow::borderAdjoiningEndCell(const RenderTableCell& cell) const
{
ASSERT_UNUSED(cell, cell.isFirstOrLastCellInRow());
// FIXME: https://webkit.org/b/79272 - Add support for mixed directionality at the cell level.
return style().borderEnd(table()->writingMode());
}
void RenderTableRow::didInsertTableCell(RenderTableCell& child, RenderObject* beforeChild)
{
// Generated content can result in us having a null section so make sure to null check our parent.
if (auto* section = this->section()) {
section->addCell(&child, this);
// rowspan=0 means "span all remaining rows," but during initial construction rows are
// inserted one at a time, so calculateRowSpanForRowspanZero()'s render tree walk will
// undercount. Force a full cell recalc so the span is resolved correctly at layout time
// once all rows are present in the render tree.
if (beforeChild || nextRow() || child.hasRowSpanZero())
section->setNeedsCellRecalc();
}
if (auto* table = this->table())
table->invalidateCollapsedBorders();
}
void RenderTableRow::layout()
{
StackStats::LayoutCheckPoint layoutCheckPoint;
ASSERT(needsLayout());
LayoutStateMaintainer statePusher(*this, locationOffset(), isTransformed() || hasReflection() || writingMode().isBlockFlipped());
auto* layoutState = view().frameView().layoutContext().layoutState();
bool paginated = layoutState->isPaginated();
for (RenderTableCell* cell = firstCell(); cell; cell = cell->nextCell()) {
if (!cell->needsLayout() && paginated && (layoutState->pageLogicalHeightChanged() || (layoutState->pageLogicalHeight() && layoutState->pageLogicalOffset(cell, cell->logicalTop()) != cell->pageLogicalOffset())))
cell->setChildNeedsLayout(MarkingBehavior::MarkOnlyThis);
if (cell->needsLayout())
cell->layout();
}
clearOverflow();
addVisualEffectOverflow();
// We only ever need to repaint if our cells didn't, which menas that they didn't need
// layout, so we know that our bounds didn't change. This code is just making up for
// the fact that we did not repaint in setStyle() because we had a layout hint.
// We cannot call repaint() because our clippedOverflowRect() is taken from the
// parent table, and being mid-layout, that is invalid. Instead, we repaint our cells.
if (selfNeedsLayout() && checkForRepaintDuringLayout()) {
for (RenderTableCell* cell = firstCell(); cell; cell = cell->nextCell())
cell->repaint();
}
// Row dimensions are finalized in RenderTableSection::layoutRows(), which also
// lays out out-of-flow descendants and calls clearNeedsLayout().
}
LayoutRect RenderTableRow::clippedOverflowRect(const RenderLayerModelObject* repaintContainer, const VisibleRectContext& context) const
{
ASSERT(parent());
// Cells are in the row's coordinate space. We need to both compute our overflow rect (which
// will accommodate a row outline and any visual effects on the row itself), but we also need to add in
// the repaint rects of cells.
auto result = RenderBlock::clippedOverflowRect(repaintContainer, context);
for (auto* cell = firstCell(); cell; cell = cell->nextCell()) {
// Even if a cell is a repaint container, it's the row that paints the background behind it.
// So we don't care if a cell is a repaintContainer here.
result.uniteIfNonZero(cell->clippedOverflowRect(repaintContainer, context));
}
return result;
}
auto RenderTableRow::rectsForRepaintingAfterLayout(const RenderLayerModelObject* repaintContainer, RepaintOutlineBounds repaintOutlineBounds) const -> RepaintRects
{
auto rects = RepaintRects { clippedOverflowRect(repaintContainer, visibleRectContextForRepaint()) };
if (repaintOutlineBounds == RepaintOutlineBounds::Yes)
rects.outlineBoundsRect = outlineBoundsForRepaint(repaintContainer);
return rects;
}
bool RenderTableRow::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
{
// Table rows cannot ever be hit tested. Effectively they do not exist.
// Just forward to our children always.
auto* section = this->section();
if (!section)
return false;
auto adjustedOffset = accumulatedOffset + location();
for (RenderTableCell* cell = lastCell(); cell; cell = cell->previousCell()) {
// FIXME: We have to skip over inline flows, since they can show up inside table rows
// at the moment (a demoted inline <form> for example). If we ever implement a
// table-specific hit-test method (which we should do for performance reasons anyway),
// then we can remove this check.
if (!cell->hasSelfPaintingLayer()) {
auto cellPoint = flipForWritingModeForChild(*cell, adjustedOffset);
if (cell->nodeAtPoint(request, result, locationInContainer, cellPoint, action)) {
updateHitTestResult(result, locationInContainer.point() - toLayoutSize(cellPoint));
return true;
}
}
}
return false;
}
void RenderTableRow::paintOutlineForRowIfNeeded(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
PaintPhase paintPhase = paintInfo.phase;
if ((paintPhase == PaintPhase::Outline || paintPhase == PaintPhase::SelfOutline) && style().usedVisibility() == Visibility::Visible) {
auto adjustedPaintOffset = paintOffset + location();
paintOutline(paintInfo, LayoutRect(adjustedPaintOffset, borderBoxSize()));
}
}
void RenderTableRow::paintShadowForRowIfNeeded(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (paintInfo.phase != PaintPhase::BlockBackground && paintInfo.phase != PaintPhase::ChildBlockBackground)
return;
auto adjustedPaintOffset = paintOffset + location();
LayoutRect rect(adjustedPaintOffset, borderBoxSize());
adjustBorderBoxRectForPainting(rect);
BackgroundPainter backgroundPainter { *this, paintInfo };
backgroundPainter.paintBoxShadow(rect, style(), Style::ShadowStyle::Normal);
}
void RenderTableRow::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
ASSERT(hasSelfPaintingLayer());
auto adjustedPaintOffset = paintOffset + location();
if (paintInfo.phase == PaintPhase::Accessibility) {
if (auto* context = paintInfo.accessibilityRegionContext())
context->takeBounds(*this, adjustedPaintOffset);
}
paintOutlineForRowIfNeeded(paintInfo, paintOffset);
for (RenderTableCell* cell = firstCell(); cell; cell = cell->nextCell()) {
// Paint the row background behind the cell.
if (paintInfo.phase == PaintPhase::BlockBackground || paintInfo.phase == PaintPhase::ChildBlockBackground)
cell->paintBackgroundsBehindCell(paintInfo, adjustedPaintOffset, this, paintOffset);
if (!cell->hasSelfPaintingLayer())
cell->paint(paintInfo, adjustedPaintOffset);
}
// When the row has a self-painting layer (position: relative), collapsed
// borders must be painted here after cell backgrounds. Otherwise the cell
// background (in this layer) covers the inner half of the border (painted
// by the table's layer).
if (CheckedPtr table = this->table(); table && table->collapseBorders() && paintInfo.phase == PaintPhase::ChildBlockBackgrounds)
table->paintCollapsedBordersForRow(paintInfo, *this, adjustedPaintOffset);
}
void RenderTableRow::imageChanged(WrappedImagePtr, const IntRect*)
{
// FIXME: Examine cells and repaint only the rect the image paints in.
if (!parent())
return;
repaint();
}
bool RenderTableRow::requiresLayer() const
{
return isPositioned() || hasNonVisibleOverflow() || hasTransformRelatedProperty() || hasHiddenBackface() || hasClipPath() || createsGroup() || isStickilyPositioned() || requiresRenderingConsolidationForViewTransition();
}
} // namespace WebCore