1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/*
* 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-2025 Apple Inc. All rights reserved.\
* Copyright (C) 2016 Google Inc. All rights reserved.
*
* 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.
*/
#pragma once
#include "CSSPropertyNames.h"
#include "CollapsedBorderValue.h"
#include "RenderBlock.h"
#include <memory>
#include <wtf/HashMap.h>
#include <wtf/Vector.h>
namespace WebCore {
class RenderTableCol;
class RenderTableCaption;
class RenderTableCell;
class RenderTableSection;
class TableLayout;
enum SkipEmptySectionsValue { DoNotSkipEmptySections, SkipEmptySections };
enum class TableIntrinsics : uint8_t { ForLayout, ForKeyword };
class RenderTable : public RenderBlock {
WTF_MAKE_TZONE_OR_ISO_ALLOCATED(RenderTable);
WTF_OVERRIDE_DELETE_FOR_CHECKED_PTR(RenderTable);
public:
RenderTable(Type, Element&, RenderStyle&&);
RenderTable(Type, Document&, RenderStyle&&);
virtual ~RenderTable();
// Per CSS 3 writing-mode: "The first and second values of the 'border-spacing' property represent spacing between columns
// and rows respectively, not necessarily the horizontal and vertical spacing respectively".
LayoutUnit hBorderSpacing() const { return m_hSpacing; }
LayoutUnit vBorderSpacing() const { return m_vSpacing; }
bool collapseBorders() const { return style().borderCollapse() == BorderCollapse::Collapse; }
LayoutUnit borderStart() const final { return m_borderStart; }
LayoutUnit borderEnd() const final { return m_borderEnd; }
LayoutUnit borderBefore() const final;
LayoutUnit borderAfter() const final;
RectEdges<LayoutUnit> borderWidths() const final;
inline LayoutUnit borderLeft() const final;
inline LayoutUnit borderRight() const final;
inline LayoutUnit borderTop() const final;
inline LayoutUnit borderBottom() const final;
Color bgColor() const { return style().visitedDependentColorWithColorFilter(CSSPropertyBackgroundColor); }
LayoutUnit outerBorderBefore() const;
LayoutUnit outerBorderAfter() const;
LayoutUnit outerBorderStart() const;
LayoutUnit outerBorderEnd() const;
inline LayoutUnit outerBorderLeft() const;
inline LayoutUnit outerBorderRight() const;
inline LayoutUnit outerBorderTop() const;
inline LayoutUnit outerBorderBottom() const;
LayoutUnit calcBorderStart() const;
LayoutUnit calcBorderEnd() const;
void recalcBordersInRowDirection();
void forceSectionsRecalc()
{
setNeedsSectionRecalc();
recalcSections();
}
struct ColumnStruct {
unsigned span { 1 };
};
const Vector<ColumnStruct>& columns() const { return m_columns; }
const Vector<LayoutUnit>& columnPositions() const { return m_columnPos; }
void setColumnPosition(unsigned index, LayoutUnit position)
{
// Note that if our horizontal border-spacing changed, our position will change but not
// our column's width. In practice, horizontal border-spacing won't change often.
m_columnLogicalWidthChanged |= m_columnPos[index] != position;
m_columnPos[index] = position;
}
RenderTableSection* header() const;
RenderTableSection* footer() const;
RenderTableSection* firstBody() const;
// This function returns 0 if the table has no section.
RenderTableSection* topSection() const;
RenderTableSection* bottomSection() const;
// This function returns 0 if the table has no non-empty sections.
RenderTableSection* topNonEmptySection() const;
RenderTableSection* bottomNonEmptySection() const;
unsigned lastColumnIndex() const { return numEffCols() - 1; }
void splitColumn(unsigned position, unsigned firstSpan);
void appendColumn(unsigned span);
unsigned numEffCols() const { return m_columns.size(); }
unsigned spanOfEffCol(unsigned effCol) const { return m_columns[effCol].span; }
unsigned colToEffCol(unsigned column) const
{
if (!m_hasCellColspanThatDeterminesTableWidth)
return column;
unsigned effColumn = 0;
unsigned numColumns = numEffCols();
for (unsigned c = 0; effColumn < numColumns && c + m_columns[effColumn].span - 1 < column; ++effColumn)
c += m_columns[effColumn].span;
return effColumn;
}
unsigned effColToCol(unsigned effCol) const
{
if (!m_hasCellColspanThatDeterminesTableWidth)
return effCol;
unsigned c = 0;
for (unsigned i = 0; i < effCol; i++)
c += m_columns[i].span;
return c;
}
LayoutUnit borderSpacingInRowDirection() const
{
if (unsigned effectiveColumnCount = numEffCols())
return (effectiveColumnCount + 1) * hBorderSpacing();
return 0;
}
inline LayoutUnit bordersPaddingAndSpacingInRowDirection() const;
// Return the first column or column-group.
RenderTableCol* firstColumn() const;
RenderTableCol* colElement(unsigned col, bool* startEdge = 0, bool* endEdge = 0) const
{
// The common case is to not have columns, make that case fast.
if (!m_hasColElements)
return 0;
return slowColElement(col, startEdge, endEdge);
}
bool needsSectionRecalc() const { return m_needsSectionRecalc; }
void setNeedsSectionRecalc()
{
if (renderTreeBeingDestroyed())
return;
m_needsSectionRecalc = true;
setNeedsLayout();
}
RenderTableSection* sectionAbove(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
RenderTableSection* sectionBelow(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
RenderTableCell* cellAbove(const RenderTableCell*) const;
RenderTableCell* cellBelow(const RenderTableCell*) const;
RenderTableCell* cellBefore(const RenderTableCell*) const;
RenderTableCell* cellAfter(const RenderTableCell*) const;
typedef Vector<CollapsedBorderValue> CollapsedBorderValues;
bool collapsedBordersAreValid() const { return m_collapsedBordersValid; }
void invalidateCollapsedBorders(RenderTableCell* cellWithStyleChange = nullptr);
void invalidateCollapsedBordersAfterStyleChangeIfNeeded(const RenderStyle& oldStyle, const RenderStyle& newStyle, RenderTableCell* cellWithStyleChange = nullptr);
void collapsedEmptyBorderIsPresent() { m_collapsedEmptyBorderIsPresent = true; }
const CollapsedBorderValue* currentBorderValue() const { return m_currentBorder; }
bool hasSections() const { return m_head || m_foot || m_firstBody; }
void recalcSectionsIfNeeded() const
{
if (m_needsSectionRecalc)
recalcSections();
}
static RenderPtr<RenderTable> createAnonymousWithParentRenderer(const RenderElement&);
RenderPtr<RenderBox> createAnonymousBoxWithSameTypeAs(const RenderBox& renderer) const override;
void addCaption(RenderTableCaption&);
void removeCaption(RenderTableCaption&);
void addColumn(const RenderTableCol*);
void invalidateColumns();
LayoutUnit offsetTopForColumn(const RenderTableCol&) const;
LayoutUnit offsetLeftForColumn(const RenderTableCol&) const;
LayoutUnit offsetWidthForColumn(const RenderTableCol&) const;
LayoutUnit offsetHeightForColumn(const RenderTableCol&) const;
void markForPaginationRelayoutIfNeeded() final;
void willInsertTableColumn(RenderTableCol& child, RenderObject* beforeChild);
void willInsertTableSection(RenderTableSection& child, RenderObject* beforeChild);
LayoutUnit sumCaptionsLogicalHeight() const;
// Whether a table has opaque foreground depends on many factors, e.g. border spacing, missing cells, etc.
// For simplicity, just conservatively assume foreground of all tables are not opaque.
bool foregroundIsKnownToBeOpaqueInRect(const LayoutRect&, unsigned) const override { return false; }
protected:
void styleDidChange(StyleDifference, const RenderStyle* oldStyle) final;
void simplifiedNormalFlowLayout() final;
private:
static RenderPtr<RenderTable> createTableWithStyle(Document&, const RenderStyle&);
ASCIILiteral renderName() const override { return "RenderTable"_s; }
void paint(PaintInfo&, const LayoutPoint&) final;
void paintObject(PaintInfo&, const LayoutPoint&) final;
void paintBoxDecorations(PaintInfo&, const LayoutPoint&) final;
void paintMask(PaintInfo&, const LayoutPoint&) final;
void layout() final;
void computeIntrinsicLogicalWidths(LayoutUnit& minWidth, LayoutUnit& maxWidth, TableIntrinsics) const;
void computeIntrinsicLogicalWidths(LayoutUnit& minWidth, LayoutUnit& maxWidth) const final;
void computeIntrinsicKeywordLogicalWidths(LayoutUnit& minWidth, LayoutUnit& maxWidth) const final;
void computePreferredLogicalWidths() override;
bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) override;
LayoutUnit baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const final;
std::optional<LayoutUnit> firstLineBaseline() const override;
std::optional<LayoutUnit> lastLineBaseline() const override;
std::optional<LayoutUnit> inlineBlockBaseline(LineDirectionMode) const final;
RenderTableCol* slowColElement(unsigned col, bool* startEdge, bool* endEdge) const;
void updateColumnCache() const;
void invalidateCachedColumns();
void invalidateCachedColumnOffsets();
void updateLogicalWidth() final;
LayoutUnit convertStyleLogicalWidthToComputedWidth(const Length& styleLogicalWidth, LayoutUnit availableWidth);
LayoutUnit convertStyleLogicalHeightToComputedHeight(const Length& styleLogicalHeight);
LayoutRect overflowClipRect(const LayoutPoint& location, OverlayScrollbarSizeRelevancy = OverlayScrollbarSizeRelevancy::IgnoreOverlayScrollbarSize, PaintPhase = PaintPhase::BlockBackground) const final;
LayoutRect overflowClipRectForChildLayers(const LayoutPoint& location, OverlayScrollbarSizeRelevancy relevancy) const override { return RenderBox::overflowClipRect(location, relevancy); }
void addOverflowFromChildren() final;
void adjustBorderBoxRectForPainting(LayoutRect&) override;
void recalcCollapsedBorders();
void recalcSections() const;
enum class BottomCaptionLayoutPhase : bool { No, Yes };
void layoutCaptions(BottomCaptionLayoutPhase = BottomCaptionLayoutPhase::No);
void layoutCaption(RenderTableCaption&);
void distributeExtraLogicalHeight(LayoutUnit extraLogicalHeight);
mutable Vector<LayoutUnit> m_columnPos;
mutable Vector<ColumnStruct> m_columns;
mutable Vector<SingleThreadWeakPtr<RenderTableCaption>> m_captions;
mutable Vector<SingleThreadWeakPtr<RenderTableCol>> m_columnRenderers;
unsigned effectiveIndexOfColumn(const RenderTableCol&) const;
using EffectiveColumnIndexMap = UncheckedKeyHashMap<SingleThreadWeakRef<const RenderTableCol>, unsigned>;
mutable EffectiveColumnIndexMap m_effectiveColumnIndexMap;
mutable SingleThreadWeakPtr<RenderTableSection> m_head;
mutable SingleThreadWeakPtr<RenderTableSection> m_foot;
mutable SingleThreadWeakPtr<RenderTableSection> m_firstBody;
std::unique_ptr<TableLayout> m_tableLayout;
CollapsedBorderValues m_collapsedBorders;
const CollapsedBorderValue* m_currentBorder;
bool m_collapsedBordersValid : 1;
bool m_collapsedEmptyBorderIsPresent : 1;
mutable bool m_hasColElements : 1;
mutable bool m_needsSectionRecalc : 1;
bool m_columnLogicalWidthChanged : 1;
mutable bool m_columnRenderersValid: 1;
mutable bool m_hasCellColspanThatDeterminesTableWidth : 1;
bool hasCellColspanThatDeterminesTableWidth() const
{
for (unsigned c = 0; c < numEffCols(); c++) {
if (m_columns[c].span > 1)
return true;
}
return false;
}
bool shouldResetLogicalHeightBeforeLayout() const override { return true; }
LayoutUnit m_hSpacing;
LayoutUnit m_vSpacing;
LayoutUnit m_borderStart;
LayoutUnit m_borderEnd;
mutable LayoutUnit m_columnOffsetTop;
mutable LayoutUnit m_columnOffsetHeight;
unsigned m_recursiveSectionMovedWithPaginationLevel { 0 };
};
inline RenderPtr<RenderBox> RenderTable::createAnonymousBoxWithSameTypeAs(const RenderBox& renderer) const
{
return RenderTable::createTableWithStyle(renderer.document(), renderer.style());
}
} // namespace WebCore
SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderTable, isRenderTable())
|