1 | /* |
2 | * Copyright (C) 1997 Martin Jones (mjones@kde.org) |
3 | * (C) 1997 Torben Weis (weis@kde.org) |
4 | * (C) 1998 Waldo Bastian (bastian@kde.org) |
5 | * (C) 1999 Lars Knoll (knoll@kde.org) |
6 | * (C) 1999 Antti Koivisto (koivisto@kde.org) |
7 | * Copyright (C) 2003, 2004, 2005, 2006, 2009, 2010, 2014 Apple Inc. All rights reserved. |
8 | * |
9 | * This library is free software; you can redistribute it and/or |
10 | * modify it under the terms of the GNU Library General Public |
11 | * License as published by the Free Software Foundation; either |
12 | * version 2 of the License, or (at your option) any later version. |
13 | * |
14 | * This library is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | * Library General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU Library General Public License |
20 | * along with this library; see the file COPYING.LIB. If not, write to |
21 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
22 | * Boston, MA 02110-1301, USA. |
23 | */ |
24 | |
25 | #pragma once |
26 | |
27 | #include "CSSPropertyNames.h" |
28 | #include "CollapsedBorderValue.h" |
29 | #include "RenderBlock.h" |
30 | #include <memory> |
31 | #include <wtf/HashMap.h> |
32 | #include <wtf/Vector.h> |
33 | |
34 | namespace WebCore { |
35 | |
36 | class RenderTableCol; |
37 | class RenderTableCaption; |
38 | class RenderTableCell; |
39 | class RenderTableSection; |
40 | class TableLayout; |
41 | |
42 | enum SkipEmptySectionsValue { DoNotSkipEmptySections, SkipEmptySections }; |
43 | |
44 | class RenderTable : public RenderBlock { |
45 | WTF_MAKE_ISO_ALLOCATED(RenderTable); |
46 | public: |
47 | RenderTable(Element&, RenderStyle&&); |
48 | RenderTable(Document&, RenderStyle&&); |
49 | virtual ~RenderTable(); |
50 | |
51 | // Per CSS 3 writing-mode: "The first and second values of the 'border-spacing' property represent spacing between columns |
52 | // and rows respectively, not necessarily the horizontal and vertical spacing respectively". |
53 | LayoutUnit hBorderSpacing() const { return m_hSpacing; } |
54 | LayoutUnit vBorderSpacing() const { return m_vSpacing; } |
55 | |
56 | bool collapseBorders() const { return style().borderCollapse() == BorderCollapse::Collapse; } |
57 | |
58 | LayoutUnit borderStart() const override { return m_borderStart; } |
59 | LayoutUnit borderEnd() const override { return m_borderEnd; } |
60 | LayoutUnit borderBefore() const override; |
61 | LayoutUnit borderAfter() const override; |
62 | |
63 | LayoutUnit borderLeft() const override |
64 | { |
65 | if (style().isHorizontalWritingMode()) |
66 | return style().isLeftToRightDirection() ? borderStart() : borderEnd(); |
67 | return style().isFlippedBlocksWritingMode() ? borderAfter() : borderBefore(); |
68 | } |
69 | |
70 | LayoutUnit borderRight() const override |
71 | { |
72 | if (style().isHorizontalWritingMode()) |
73 | return style().isLeftToRightDirection() ? borderEnd() : borderStart(); |
74 | return style().isFlippedBlocksWritingMode() ? borderBefore() : borderAfter(); |
75 | } |
76 | |
77 | LayoutUnit borderTop() const override |
78 | { |
79 | if (style().isHorizontalWritingMode()) |
80 | return style().isFlippedBlocksWritingMode() ? borderAfter() : borderBefore(); |
81 | return style().isLeftToRightDirection() ? borderStart() : borderEnd(); |
82 | } |
83 | |
84 | LayoutUnit borderBottom() const override |
85 | { |
86 | if (style().isHorizontalWritingMode()) |
87 | return style().isFlippedBlocksWritingMode() ? borderBefore() : borderAfter(); |
88 | return style().isLeftToRightDirection() ? borderEnd() : borderStart(); |
89 | } |
90 | |
91 | Color bgColor() const { return style().visitedDependentColorWithColorFilter(CSSPropertyBackgroundColor); } |
92 | |
93 | LayoutUnit outerBorderBefore() const; |
94 | LayoutUnit outerBorderAfter() const; |
95 | LayoutUnit outerBorderStart() const; |
96 | LayoutUnit outerBorderEnd() const; |
97 | |
98 | LayoutUnit outerBorderLeft() const |
99 | { |
100 | if (style().isHorizontalWritingMode()) |
101 | return style().isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd(); |
102 | return style().isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore(); |
103 | } |
104 | |
105 | LayoutUnit outerBorderRight() const |
106 | { |
107 | if (style().isHorizontalWritingMode()) |
108 | return style().isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart(); |
109 | return style().isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter(); |
110 | } |
111 | |
112 | LayoutUnit outerBorderTop() const |
113 | { |
114 | if (style().isHorizontalWritingMode()) |
115 | return style().isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore(); |
116 | return style().isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd(); |
117 | } |
118 | |
119 | LayoutUnit outerBorderBottom() const |
120 | { |
121 | if (style().isHorizontalWritingMode()) |
122 | return style().isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter(); |
123 | return style().isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart(); |
124 | } |
125 | |
126 | LayoutUnit calcBorderStart() const; |
127 | LayoutUnit calcBorderEnd() const; |
128 | void recalcBordersInRowDirection(); |
129 | |
130 | struct ColumnStruct { |
131 | explicit ColumnStruct(unsigned initialSpan = 1) |
132 | : span(initialSpan) |
133 | { |
134 | } |
135 | |
136 | unsigned span; |
137 | }; |
138 | |
139 | void forceSectionsRecalc() |
140 | { |
141 | setNeedsSectionRecalc(); |
142 | recalcSections(); |
143 | } |
144 | |
145 | const Vector<ColumnStruct>& columns() const { return m_columns; } |
146 | const Vector<LayoutUnit>& columnPositions() const { return m_columnPos; } |
147 | void setColumnPosition(unsigned index, LayoutUnit position) |
148 | { |
149 | // Note that if our horizontal border-spacing changed, our position will change but not |
150 | // our column's width. In practice, horizontal border-spacing won't change often. |
151 | m_columnLogicalWidthChanged |= m_columnPos[index] != position; |
152 | m_columnPos[index] = position; |
153 | } |
154 | |
155 | RenderTableSection* () const { return m_head.get(); } |
156 | RenderTableSection* () const { return m_foot.get(); } |
157 | RenderTableSection* firstBody() const { return m_firstBody.get(); } |
158 | |
159 | // This function returns 0 if the table has no section. |
160 | RenderTableSection* topSection() const; |
161 | RenderTableSection* bottomSection() const; |
162 | |
163 | // This function returns 0 if the table has no non-empty sections. |
164 | RenderTableSection* topNonEmptySection() const; |
165 | |
166 | unsigned lastColumnIndex() const { return numEffCols() - 1; } |
167 | |
168 | void splitColumn(unsigned position, unsigned firstSpan); |
169 | void appendColumn(unsigned span); |
170 | unsigned numEffCols() const { return m_columns.size(); } |
171 | unsigned spanOfEffCol(unsigned effCol) const { return m_columns[effCol].span; } |
172 | |
173 | unsigned colToEffCol(unsigned column) const |
174 | { |
175 | if (!m_hasCellColspanThatDeterminesTableWidth) |
176 | return column; |
177 | |
178 | unsigned effColumn = 0; |
179 | unsigned numColumns = numEffCols(); |
180 | for (unsigned c = 0; effColumn < numColumns && c + m_columns[effColumn].span - 1 < column; ++effColumn) |
181 | c += m_columns[effColumn].span; |
182 | return effColumn; |
183 | } |
184 | |
185 | unsigned effColToCol(unsigned effCol) const |
186 | { |
187 | if (!m_hasCellColspanThatDeterminesTableWidth) |
188 | return effCol; |
189 | |
190 | unsigned c = 0; |
191 | for (unsigned i = 0; i < effCol; i++) |
192 | c += m_columns[i].span; |
193 | return c; |
194 | } |
195 | |
196 | LayoutUnit borderSpacingInRowDirection() const |
197 | { |
198 | if (unsigned effectiveColumnCount = numEffCols()) |
199 | return (effectiveColumnCount + 1) * hBorderSpacing(); |
200 | |
201 | return 0; |
202 | } |
203 | |
204 | LayoutUnit bordersPaddingAndSpacingInRowDirection() const |
205 | { |
206 | // 'border-spacing' only applies to separate borders (see 17.6.1 The separated borders model). |
207 | return borderStart() + borderEnd() + (collapseBorders() ? 0_lu : (paddingStart() + paddingEnd() + borderSpacingInRowDirection())); |
208 | } |
209 | |
210 | // Return the first column or column-group. |
211 | RenderTableCol* firstColumn() const; |
212 | |
213 | RenderTableCol* colElement(unsigned col, bool* startEdge = 0, bool* endEdge = 0) const |
214 | { |
215 | // The common case is to not have columns, make that case fast. |
216 | if (!m_hasColElements) |
217 | return 0; |
218 | return slowColElement(col, startEdge, endEdge); |
219 | } |
220 | |
221 | bool needsSectionRecalc() const { return m_needsSectionRecalc; } |
222 | void setNeedsSectionRecalc() |
223 | { |
224 | if (renderTreeBeingDestroyed()) |
225 | return; |
226 | m_needsSectionRecalc = true; |
227 | setNeedsLayout(); |
228 | } |
229 | |
230 | RenderTableSection* sectionAbove(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const; |
231 | RenderTableSection* sectionBelow(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const; |
232 | |
233 | RenderTableCell* cellAbove(const RenderTableCell*) const; |
234 | RenderTableCell* cellBelow(const RenderTableCell*) const; |
235 | RenderTableCell* cellBefore(const RenderTableCell*) const; |
236 | RenderTableCell* cellAfter(const RenderTableCell*) const; |
237 | |
238 | typedef Vector<CollapsedBorderValue> CollapsedBorderValues; |
239 | bool collapsedBordersAreValid() const { return m_collapsedBordersValid; } |
240 | void invalidateCollapsedBorders(RenderTableCell* cellWithStyleChange = nullptr); |
241 | void collapsedEmptyBorderIsPresent() { m_collapsedEmptyBorderIsPresent = true; } |
242 | const CollapsedBorderValue* currentBorderValue() const { return m_currentBorder; } |
243 | |
244 | bool hasSections() const { return m_head || m_foot || m_firstBody; } |
245 | |
246 | void recalcSectionsIfNeeded() const |
247 | { |
248 | if (m_needsSectionRecalc) |
249 | recalcSections(); |
250 | } |
251 | |
252 | static RenderPtr<RenderTable> createAnonymousWithParentRenderer(const RenderElement&); |
253 | RenderPtr<RenderBox> createAnonymousBoxWithSameTypeAs(const RenderBox& renderer) const override; |
254 | |
255 | const BorderValue& tableStartBorderAdjoiningCell(const RenderTableCell&) const; |
256 | const BorderValue& tableEndBorderAdjoiningCell(const RenderTableCell&) const; |
257 | |
258 | void addCaption(RenderTableCaption&); |
259 | void removeCaption(RenderTableCaption&); |
260 | void addColumn(const RenderTableCol*); |
261 | void removeColumn(const RenderTableCol*); |
262 | |
263 | LayoutUnit offsetTopForColumn(const RenderTableCol&) const; |
264 | LayoutUnit offsetLeftForColumn(const RenderTableCol&) const; |
265 | LayoutUnit offsetWidthForColumn(const RenderTableCol&) const; |
266 | LayoutUnit offsetHeightForColumn(const RenderTableCol&) const; |
267 | |
268 | void () final; |
269 | |
270 | void willInsertTableColumn(RenderTableCol& child, RenderObject* beforeChild); |
271 | void willInsertTableSection(RenderTableSection& child, RenderObject* beforeChild); |
272 | |
273 | protected: |
274 | void styleDidChange(StyleDifference, const RenderStyle* oldStyle) final; |
275 | void simplifiedNormalFlowLayout() final; |
276 | |
277 | private: |
278 | static RenderPtr<RenderTable> createTableWithStyle(Document&, const RenderStyle&); |
279 | |
280 | const char* renderName() const override { return "RenderTable" ; } |
281 | |
282 | bool isTable() const final { return true; } |
283 | |
284 | bool avoidsFloats() const final { return true; } |
285 | |
286 | void paint(PaintInfo&, const LayoutPoint&) final; |
287 | void paintObject(PaintInfo&, const LayoutPoint&) final; |
288 | void paintBoxDecorations(PaintInfo&, const LayoutPoint&) final; |
289 | void paintMask(PaintInfo&, const LayoutPoint&) final; |
290 | void layout() final; |
291 | void computeIntrinsicLogicalWidths(LayoutUnit& minWidth, LayoutUnit& maxWidth) const final; |
292 | void computePreferredLogicalWidths() override; |
293 | bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) override; |
294 | |
295 | int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const final; |
296 | Optional<int> firstLineBaseline() const override; |
297 | Optional<int> inlineBlockBaseline(LineDirectionMode) const final; |
298 | |
299 | RenderTableCol* slowColElement(unsigned col, bool* startEdge, bool* endEdge) const; |
300 | |
301 | void updateColumnCache() const; |
302 | void invalidateCachedColumns(); |
303 | |
304 | void invalidateCachedColumnOffsets(); |
305 | |
306 | RenderBlock* firstLineBlock() const final; |
307 | |
308 | void updateLogicalWidth() final; |
309 | |
310 | LayoutUnit convertStyleLogicalWidthToComputedWidth(const Length& styleLogicalWidth, LayoutUnit availableWidth); |
311 | LayoutUnit convertStyleLogicalHeightToComputedHeight(const Length& styleLogicalHeight); |
312 | |
313 | LayoutRect overflowClipRect(const LayoutPoint& location, RenderFragmentContainer*, OverlayScrollbarSizeRelevancy = IgnoreOverlayScrollbarSize, PaintPhase = PaintPhase::BlockBackground) final; |
314 | LayoutRect overflowClipRectForChildLayers(const LayoutPoint& location, RenderFragmentContainer* fragment, OverlayScrollbarSizeRelevancy relevancy) override { return RenderBox::overflowClipRect(location, fragment, relevancy); } |
315 | |
316 | void addOverflowFromChildren() final; |
317 | |
318 | void adjustBorderBoxRectForPainting(LayoutRect&) override; |
319 | |
320 | void recalcCollapsedBorders(); |
321 | void recalcSections() const; |
322 | enum class BottomCaptionLayoutPhase { Yes, No }; |
323 | void layoutCaptions(BottomCaptionLayoutPhase = BottomCaptionLayoutPhase::No); |
324 | void layoutCaption(RenderTableCaption&); |
325 | |
326 | void (LayoutUnit ); |
327 | |
328 | mutable Vector<LayoutUnit> m_columnPos; |
329 | mutable Vector<ColumnStruct> m_columns; |
330 | mutable Vector<WeakPtr<RenderTableCaption>> m_captions; |
331 | mutable Vector<WeakPtr<RenderTableCol>> m_columnRenderers; |
332 | |
333 | unsigned effectiveIndexOfColumn(const RenderTableCol&) const; |
334 | typedef HashMap<const RenderTableCol*, unsigned> EffectiveColumnIndexMap; |
335 | mutable EffectiveColumnIndexMap m_effectiveColumnIndexMap; |
336 | |
337 | mutable WeakPtr<RenderTableSection> m_head; |
338 | mutable WeakPtr<RenderTableSection> ; |
339 | mutable WeakPtr<RenderTableSection> m_firstBody; |
340 | |
341 | std::unique_ptr<TableLayout> m_tableLayout; |
342 | |
343 | CollapsedBorderValues m_collapsedBorders; |
344 | const CollapsedBorderValue* m_currentBorder; |
345 | bool m_collapsedBordersValid : 1; |
346 | bool m_collapsedEmptyBorderIsPresent : 1; |
347 | |
348 | mutable bool m_hasColElements : 1; |
349 | mutable bool m_needsSectionRecalc : 1; |
350 | |
351 | bool m_columnLogicalWidthChanged : 1; |
352 | mutable bool m_columnRenderersValid: 1; |
353 | mutable bool m_hasCellColspanThatDeterminesTableWidth : 1; |
354 | |
355 | bool hasCellColspanThatDeterminesTableWidth() const |
356 | { |
357 | for (unsigned c = 0; c < numEffCols(); c++) { |
358 | if (m_columns[c].span > 1) |
359 | return true; |
360 | } |
361 | return false; |
362 | } |
363 | |
364 | LayoutUnit m_hSpacing; |
365 | LayoutUnit m_vSpacing; |
366 | LayoutUnit m_borderStart; |
367 | LayoutUnit m_borderEnd; |
368 | mutable LayoutUnit m_columnOffsetTop; |
369 | mutable LayoutUnit m_columnOffsetHeight; |
370 | bool { false }; |
371 | }; |
372 | |
373 | inline RenderTableSection* RenderTable::topSection() const |
374 | { |
375 | ASSERT(!needsSectionRecalc()); |
376 | if (m_head) |
377 | return m_head.get(); |
378 | if (m_firstBody) |
379 | return m_firstBody.get(); |
380 | return m_foot.get(); |
381 | } |
382 | |
383 | inline bool isDirectionSame(const RenderBox* tableItem, const RenderBox* otherTableItem) { return tableItem && otherTableItem ? tableItem->style().direction() == otherTableItem->style().direction() : true; } |
384 | |
385 | inline RenderPtr<RenderBox> RenderTable::createAnonymousBoxWithSameTypeAs(const RenderBox& renderer) const |
386 | { |
387 | return RenderTable::createTableWithStyle(renderer.document(), renderer.style()); |
388 | } |
389 | |
390 | } // namespace WebCore |
391 | |
392 | SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderTable, isTable()) |
393 | |