summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/core/layout/LayoutTable.h
blob: f34d1286c0e8f7ed02481e5df702d6b0a89314a6 (plain)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
 * 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, 2004, 2005, 2006, 2009, 2010 Apple 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.
 */

#ifndef LayoutTable_h
#define LayoutTable_h

#include "core/CSSPropertyNames.h"
#include "core/CoreExport.h"
#include "core/layout/LayoutBlock.h"
#include "core/style/CollapsedBorderValue.h"
#include "wtf/Vector.h"

namespace blink {

class LayoutTableCol;
class LayoutTableCaption;
class LayoutTableCell;
class LayoutTableSection;
class TableLayoutAlgorithm;

enum SkipEmptySectionsValue { DoNotSkipEmptySections, SkipEmptySections };

// LayoutTable is the LayoutObject associated with
// display: table or inline-table.
//
// LayoutTable is the master coordinator for determining the overall table
// structure. The reason is that LayoutTableSection children have a local
// view over what their structure is but don't account for other
// LayoutTableSection. Thus LayoutTable helps keep consistency across
// LayoutTableSection. See e.g. |m_effectiveColumns| below.
//
// LayoutTable expects only 3 types of children:
// - zero or more LayoutTableCol
// - zero or more LayoutTableCaption
// - zero or more LayoutTableSection
// This is aligned with what HTML5 expects:
// https://html.spec.whatwg.org/multipage/tables.html#the-table-element
// with one difference: we allow more than one caption as we follow what
// CSS expects (https://bugs.webkit.org/show_bug.cgi?id=69773).
// Those expectations are enforced by LayoutTable::addChild, that wraps unknown
// children into an anonymous LayoutTableSection. This is what the "generate
// missing child wrapper" step in CSS mandates in
// http://www.w3.org/TR/CSS21/tables.html#anonymous-boxes.
//
// LayoutTable assumes a pretty strict structure that is mandated by CSS:
// (note that this structure in HTML is enforced by the HTML5 Parser).
//
//                 LayoutTable
//                 |        |
//  LayoutTableSection    LayoutTableCaption
//                 |
//      LayoutTableRow
//                 |
//     LayoutTableCell
//
// This means that we have to generate some anonymous table wrappers in order to
// satisfy the structure. See again
// http://www.w3.org/TR/CSS21/tables.html#anonymous-boxes.
// The anonymous table wrappers are inserted in LayoutTable::addChild,
// LayoutTableSection::addChild, LayoutTableRow::addChild and
// LayoutObject::addChild.
//
// Note that this yields to interesting issues in the insertion code. The DOM
// code is unaware of the anonymous LayoutObjects and thus can insert
// LayoutObjects into a different part of the layout tree. An example is:
//
// <!DOCTYPE html>
// <style>
// tablerow { display: table-row; }
// tablecell { display: table-cell; border: 5px solid purple; }
// </style>
// <tablerow id="firstRow">
//     <tablecell>Short first row.</tablecell>
// </tablerow>
// <tablecell id="cell">Long second row, shows the table structure.</tablecell>
//
// The page generates a single anonymous table (LayoutTable) and table row group
// (LayoutTableSection) to wrap the <tablerow> (#firstRow) and an anonymous
// table row (LayoutTableRow) for the second <tablecell>.
// It is possible for JavaScript to insert a new element between these 2
// <tablecell> (using Node.insertBefore), requiring us to split the anonymous
// table (or the anonymous table row group) in 2. Also note that even
// though the second <tablecell> and <tablerow> are siblings in the DOM tree,
// they are not in the layout tree.
//
//
// Note about absolute column index vs effective column index:
//
// To save memory at the expense of massive code complexity, the code tries
// to coalesce columns. This means that we try to the wider column grouping
// seen over the LayoutTableSections.
//
// Note that this is also a defensive pattern as <td colspan="6666666666">
// only allocates a single entry in this Vector. This argument is weak
// though as we cap colspans in HTMLTableCellElement.
//
// The following example would have 2 entries [ 3, 2 ] in effectiveColumns():
// <table>
//   <tr>
//     <td colspan="3"></td>
//     <td colspan="2"></td>
//   </tr>
// </table>
//
// Columns can be split if we add a row with a different colspan structure.
// See splitEffectiveColumn() and appendEffectiveColumn() for operations
// over effectiveColumns() and effectiveColumnPositions().
//
// See absoluteColumnToEffectiveColumn() for converting an absolute column
// index into an index into effectiveColumns() and effectiveColumnPositions().

class CORE_EXPORT LayoutTable final : public LayoutBlock {
public:
    explicit LayoutTable(Element*);
    ~LayoutTable() override;

    // 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".
    int hBorderSpacing() const { return m_hSpacing; }
    int vBorderSpacing() const { return m_vSpacing; }

    bool collapseBorders() const { return style()->borderCollapse(); }

    int borderStart() const override { return m_borderStart; }
    int borderEnd() const override { return m_borderEnd; }
    int borderBefore() const override;
    int borderAfter() const override;

    int borderLeft() const override
    {
        if (style()->isHorizontalWritingMode())
            return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
        return style()->isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
    }

    int borderRight() const override
    {
        if (style()->isHorizontalWritingMode())
            return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
        return style()->isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
    }

    int borderTop() const override
    {
        if (style()->isHorizontalWritingMode())
            return style()->isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
        return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
    }

    int borderBottom() const override
    {
        if (style()->isHorizontalWritingMode())
            return style()->isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
        return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
    }

    int outerBorderBefore() const;
    int outerBorderAfter() const;
    int outerBorderStart() const;
    int outerBorderEnd() const;

    int outerBorderLeft() const
    {
        if (style()->isHorizontalWritingMode())
            return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
        return style()->isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
    }

    int outerBorderRight() const
    {
        if (style()->isHorizontalWritingMode())
            return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
        return style()->isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
    }

    int outerBorderTop() const
    {
        if (style()->isHorizontalWritingMode())
            return style()->isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
        return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
    }

    int outerBorderBottom() const
    {
        if (style()->isHorizontalWritingMode())
            return style()->isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
        return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
    }

    int calcBorderStart() const;
    int calcBorderEnd() const;
    void recalcBordersInRowDirection();

    void addChild(LayoutObject* child, LayoutObject* beforeChild = nullptr) override;
    void addChildIgnoringContinuation(LayoutObject* newChild, LayoutObject* beforeChild = nullptr) override;

    struct ColumnStruct {
        DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
        explicit ColumnStruct(unsigned initialSpan = 1)
            : span(initialSpan)
        {
        }

        unsigned span;
    };

    void forceSectionsRecalc()
    {
        setNeedsSectionRecalc();
        recalcSections();
    }

    const Vector<ColumnStruct>& effectiveColumns() const { return m_effectiveColumns; }
    const Vector<int>& effectiveColumnPositions() const { return m_effectiveColumnPositions; }
    void setEffectiveColumnPosition(unsigned index, int 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_effectiveColumnPositions[index] != position;
        m_effectiveColumnPositions[index] = position;
    }

    LayoutTableSection* header() const { return m_head; }
    LayoutTableSection* footer() const { return m_foot; }
    LayoutTableSection* firstBody() const { return m_firstBody; }

    // This function returns 0 if the table has no section.
    LayoutTableSection* topSection() const;
    LayoutTableSection* bottomSection() const;

    // This function returns 0 if the table has no non-empty sections.
    LayoutTableSection* topNonEmptySection() const;

    unsigned lastEffectiveColumnIndex() const { return numEffectiveColumns() - 1; }

    void splitEffectiveColumn(unsigned index, unsigned firstSpan);
    void appendEffectiveColumn(unsigned span);
    unsigned numEffectiveColumns() const { return m_effectiveColumns.size(); }
    unsigned spanOfEffectiveColumn(unsigned effectiveColumnIndex) const { return m_effectiveColumns[effectiveColumnIndex].span; }

    unsigned absoluteColumnToEffectiveColumn(unsigned absoluteColumnIndex) const
    {
        if (!m_hasCellColspanThatDeterminesTableWidth)
            return absoluteColumnIndex;

        unsigned effectiveColumn = 0;
        unsigned numColumns = numEffectiveColumns();
        for (unsigned c = 0; effectiveColumn < numColumns && c + m_effectiveColumns[effectiveColumn].span - 1 < absoluteColumnIndex; ++effectiveColumn)
            c += m_effectiveColumns[effectiveColumn].span;
        return effectiveColumn;
    }

    unsigned effectiveColumnToAbsoluteColumn(unsigned effectiveColumnIndex) const
    {
        if (!m_hasCellColspanThatDeterminesTableWidth)
            return effectiveColumnIndex;

        unsigned c = 0;
        for (unsigned i = 0; i < effectiveColumnIndex; i++)
            c += m_effectiveColumns[i].span;
        return c;
    }

    LayoutUnit borderSpacingInRowDirection() const
    {
        if (unsigned effectiveColumnCount = numEffectiveColumns())
            return static_cast<LayoutUnit>(effectiveColumnCount + 1) * hBorderSpacing();

        return LayoutUnit();
    }

    // The collapsing border model dissallows paddings on table, which is why we
    // override those functions.
    // See http://www.w3.org/TR/CSS2/tables.html#collapsing-borders.
    LayoutUnit paddingTop() const override;
    LayoutUnit paddingBottom() const override;
    LayoutUnit paddingLeft() const override;
    LayoutUnit paddingRight() const override;

    // Override paddingStart/End to return pixel values to match behavor of LayoutTableCell.
    LayoutUnit paddingEnd() const override { return LayoutUnit(static_cast<int>(LayoutBlock::paddingEnd())); }
    LayoutUnit paddingStart() const override { return LayoutUnit(static_cast<int>(LayoutBlock::paddingStart())); }

    LayoutUnit bordersPaddingAndSpacingInRowDirection() const
    {
        // 'border-spacing' only applies to separate borders (see 17.6.1 The separated borders model).
        return borderStart() + borderEnd() + (collapseBorders() ? LayoutUnit() : (paddingStart() + paddingEnd() + borderSpacingInRowDirection()));
    }

    // Return the first column or column-group.
    LayoutTableCol* firstColumn() const;

    struct ColAndColGroup {
        ColAndColGroup()
            : col(nullptr)
            , colgroup(nullptr)
            , adjoinsStartBorderOfColGroup(false)
            , adjoinsEndBorderOfColGroup(false)
        {
        }
        LayoutTableCol* col;
        LayoutTableCol* colgroup;
        bool adjoinsStartBorderOfColGroup;
        bool adjoinsEndBorderOfColGroup;
        LayoutTableCol* innermostColOrColGroup()
        {
            return col ? col : colgroup;
        }
    };
    ColAndColGroup colElementAtAbsoluteColumn(unsigned absoluteColumnIndex) const
    {
        // The common case is to not have col/colgroup elements, make that case fast.
        if (!m_hasColElements)
            return ColAndColGroup();
        return slowColElementAtAbsoluteColumn(absoluteColumnIndex);
    }

    bool needsSectionRecalc() const { return m_needsSectionRecalc; }
    void setNeedsSectionRecalc()
    {
        if (documentBeingDestroyed())
            return;
        m_needsSectionRecalc = true;
        setNeedsLayoutAndFullPaintInvalidation(LayoutInvalidationReason::TableChanged);
    }

    LayoutTableSection* sectionAbove(const LayoutTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
    LayoutTableSection* sectionBelow(const LayoutTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;

    LayoutTableCell* cellAbove(const LayoutTableCell*) const;
    LayoutTableCell* cellBelow(const LayoutTableCell*) const;
    LayoutTableCell* cellBefore(const LayoutTableCell*) const;
    LayoutTableCell* cellAfter(const LayoutTableCell*) const;

    typedef Vector<CollapsedBorderValue> CollapsedBorderValues;
    void invalidateCollapsedBorders();

    bool hasSections() const { return m_head || m_foot || m_firstBody; }

    void recalcSectionsIfNeeded() const
    {
        if (m_needsSectionRecalc)
            recalcSections();
    }

    static LayoutTable* createAnonymousWithParent(const LayoutObject*);
    LayoutBox* createAnonymousBoxWithSameTypeAs(const LayoutObject* parent) const override
    {
        return createAnonymousWithParent(parent);
    }

    const BorderValue& tableStartBorderAdjoiningCell(const LayoutTableCell*) const;
    const BorderValue& tableEndBorderAdjoiningCell(const LayoutTableCell*) const;

    void addCaption(const LayoutTableCaption*);
    void removeCaption(const LayoutTableCaption*);
    void addColumn(const LayoutTableCol*);
    void removeColumn(const LayoutTableCol*);

    void paintBoxDecorationBackground(const PaintInfo&, const LayoutPoint&) const final;

    void paintMask(const PaintInfo&, const LayoutPoint&) const final;

    const CollapsedBorderValues& collapsedBorders() const
    {
        ASSERT(m_collapsedBordersValid);
        return m_collapsedBorders;
    }

    void subtractCaptionRect(LayoutRect&) const;

    bool isLogicalWidthAuto() const;

    const char* name() const override { return "LayoutTable"; }

    // 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 ComputedStyle* oldStyle) override;
    void simplifiedNormalFlowLayout() override;
    PaintInvalidationReason invalidatePaintIfNeeded(const PaintInvalidationState&) override;
    void invalidatePaintOfSubtreesIfNeeded(const PaintInvalidationState&) override;

private:
    bool isOfType(LayoutObjectType type) const override { return type == LayoutObjectTable || LayoutBlock::isOfType(type); }

    void paintObject(const PaintInfo&, const LayoutPoint&) const override;
    void layout() override;
    void computeIntrinsicLogicalWidths(LayoutUnit& minWidth, LayoutUnit& maxWidth) const override;
    void computePreferredLogicalWidths() override;
    bool nodeAtPoint(HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) override;

    int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const override;
    int firstLineBoxBaseline() const override;
    int inlineBlockBaseline(LineDirectionMode) const override;

    ColAndColGroup slowColElementAtAbsoluteColumn(unsigned col) const;

    void updateColumnCache() const;
    void invalidateCachedColumns();

    void updateLogicalWidth() override;

    LayoutUnit convertStyleLogicalWidthToComputedWidth(const Length& styleLogicalWidth, LayoutUnit availableWidth);
    LayoutUnit convertStyleLogicalHeightToComputedHeight(const Length& styleLogicalHeight);

    LayoutRect overflowClipRect(const LayoutPoint& location, OverlayScrollbarSizeRelevancy = IgnoreOverlayScrollbarSize) const override;

    void addOverflowFromChildren() override;

    void recalcSections() const;
    void layoutCaption(LayoutTableCaption&);

    void distributeExtraLogicalHeight(int extraLogicalHeight);

    void recalcCollapsedBordersIfNeeded();

    // TODO(layout-dev): All mutables in this class are lazily updated by recalcSections()
    // which is called by various getter methods (e.g. borderBefore(), borderAfter()).
    // They allow dirty layout even after DocumentLifecycle::LayoutClean which seems not proper. crbug.com/538236.

    // Holds spans (number of absolute columns) of effective columns.
    // See "absolute column index vs effective column index" in comments of LayoutTable.
    mutable Vector<ColumnStruct> m_effectiveColumns;

    // Holds the logical layout positions of effective columns, and the last item (whose index
    // is numEffectiveColumns()) holds the position of the imaginary column after the last column.
    // Because of the last item, m_effectiveColumnPositions.size() is always numEffectiveColumns() + 1.
    mutable Vector<int> m_effectiveColumnPositions;

    // The captions associated with this object.
    mutable Vector<LayoutTableCaption*> m_captions;

    // Holds pointers to LayoutTableCol objects for <col>s and <colgroup>s under this table.
    // There is no direct relationship between the size of and index into this vector and
    // those of m_effectiveColumns because they hold different things.
    mutable Vector<LayoutTableCol*> m_columnLayoutObjects;

    mutable LayoutTableSection* m_head;
    mutable LayoutTableSection* m_foot;
    mutable LayoutTableSection* m_firstBody;

    // The layout algorithm used by this table.
    //
    // CSS 2.1 defines 2 types of table layouts toggled with 'table-layout':
    // fixed (TableLayoutAlgorithmFixed) and auto (TableLayoutAlgorithmAuto).
    // See http://www.w3.org/TR/CSS21/tables.html#width-layout.
    //
    // The layout algorithm is delegated to TableLayoutAlgorithm. This enables
    // changing 'table-layout' without having to reattach the <table>.
    //
    // As the algorithm is dependent on the style, this field is nullptr before
    // the first style is applied in styleDidChange().
    OwnPtr<TableLayoutAlgorithm> m_tableLayout;

    // A sorted list of all unique border values that we want to paint.
    //
    // Collapsed borders are SUPER EXPENSIVE to compute. The reason is that we
    // need to compare a cells border against all the adjoining cells, rows,
    // row groups, column, column groups and table. Thus we cache them in this
    // field.
    CollapsedBorderValues m_collapsedBorders;
    bool m_collapsedBordersValid : 1;

    mutable bool m_hasColElements : 1;
    mutable bool m_needsSectionRecalc : 1;

    bool m_columnLogicalWidthChanged : 1;
    mutable bool m_columnLayoutObjectsValid: 1;
    mutable bool m_hasCellColspanThatDeterminesTableWidth : 1;
    bool hasCellColspanThatDeterminesTableWidth() const
    {
        for (unsigned c = 0; c < numEffectiveColumns(); c++) {
            if (m_effectiveColumns[c].span > 1)
                return true;
        }
        return false;
    }

    short m_hSpacing;
    short m_vSpacing;
    int m_borderStart;
    int m_borderEnd;
};

inline LayoutTableSection* LayoutTable::topSection() const
{
    ASSERT(!needsSectionRecalc());
    if (m_head)
        return m_head;
    if (m_firstBody)
        return m_firstBody;
    return m_foot;
}

DEFINE_LAYOUT_OBJECT_TYPE_CASTS(LayoutTable, isTable());

} // namespace blink

#endif // LayoutTable_h