Home | History | Annotate | Download | only in rendering
      1 /*
      2  * Copyright (C) 1997 Martin Jones (mjones (at) kde.org)
      3  *           (C) 1997 Torben Weis (weis (at) kde.org)
      4  *           (C) 1998 Waldo Bastian (bastian (at) kde.org)
      5  *           (C) 1999 Lars Knoll (knoll (at) kde.org)
      6  *           (C) 1999 Antti Koivisto (koivisto (at) kde.org)
      7  * Copyright (C) 2003, 2004, 2005, 2006, 2009, 2010 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 #ifndef RenderTable_h
     26 #define RenderTable_h
     27 
     28 #include "CSSPropertyNames.h"
     29 #include "core/rendering/RenderBlock.h"
     30 #include "core/rendering/style/CollapsedBorderValue.h"
     31 #include "wtf/Vector.h"
     32 
     33 namespace WebCore {
     34 
     35 class RenderTableCol;
     36 class RenderTableCaption;
     37 class RenderTableCell;
     38 class RenderTableSection;
     39 class TableLayout;
     40 
     41 enum SkipEmptySectionsValue { DoNotSkipEmptySections, SkipEmptySections };
     42 
     43 class RenderTable FINAL : public RenderBlock {
     44 public:
     45     explicit RenderTable(Element*);
     46     virtual ~RenderTable();
     47 
     48     // Per CSS 3 writing-mode: "The first and second values of the 'border-spacing' property represent spacing between columns
     49     // and rows respectively, not necessarily the horizontal and vertical spacing respectively".
     50     int hBorderSpacing() const { return m_hSpacing; }
     51     int vBorderSpacing() const { return m_vSpacing; }
     52 
     53     bool collapseBorders() const { return style()->borderCollapse(); }
     54 
     55     int borderStart() const { return m_borderStart; }
     56     int borderEnd() const { return m_borderEnd; }
     57     int borderBefore() const;
     58     int borderAfter() const;
     59 
     60     int borderLeft() const
     61     {
     62         if (style()->isHorizontalWritingMode())
     63             return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
     64         return style()->isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
     65     }
     66 
     67     int borderRight() const
     68     {
     69         if (style()->isHorizontalWritingMode())
     70             return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
     71         return style()->isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
     72     }
     73 
     74     int borderTop() const
     75     {
     76         if (style()->isHorizontalWritingMode())
     77             return style()->isFlippedBlocksWritingMode() ? borderAfter() : borderBefore();
     78         return style()->isLeftToRightDirection() ? borderStart() : borderEnd();
     79     }
     80 
     81     int borderBottom() const
     82     {
     83         if (style()->isHorizontalWritingMode())
     84             return style()->isFlippedBlocksWritingMode() ? borderBefore() : borderAfter();
     85         return style()->isLeftToRightDirection() ? borderEnd() : borderStart();
     86     }
     87 
     88     Color bgColor() const { return resolveColor(CSSPropertyBackgroundColor); }
     89 
     90     int outerBorderBefore() const;
     91     int outerBorderAfter() const;
     92     int outerBorderStart() const;
     93     int outerBorderEnd() const;
     94 
     95     int outerBorderLeft() const
     96     {
     97         if (style()->isHorizontalWritingMode())
     98             return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
     99         return style()->isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
    100     }
    101 
    102     int outerBorderRight() const
    103     {
    104         if (style()->isHorizontalWritingMode())
    105             return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
    106         return style()->isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
    107     }
    108 
    109     int outerBorderTop() const
    110     {
    111         if (style()->isHorizontalWritingMode())
    112             return style()->isFlippedBlocksWritingMode() ? outerBorderAfter() : outerBorderBefore();
    113         return style()->isLeftToRightDirection() ? outerBorderStart() : outerBorderEnd();
    114     }
    115 
    116     int outerBorderBottom() const
    117     {
    118         if (style()->isHorizontalWritingMode())
    119             return style()->isFlippedBlocksWritingMode() ? outerBorderBefore() : outerBorderAfter();
    120         return style()->isLeftToRightDirection() ? outerBorderEnd() : outerBorderStart();
    121     }
    122 
    123     int calcBorderStart() const;
    124     int calcBorderEnd() const;
    125     void recalcBordersInRowDirection();
    126 
    127     virtual void addChild(RenderObject* child, RenderObject* beforeChild = 0);
    128 
    129     struct ColumnStruct {
    130         explicit ColumnStruct(unsigned initialSpan = 1)
    131             : span(initialSpan)
    132         {
    133         }
    134 
    135         unsigned span;
    136     };
    137 
    138     void forceSectionsRecalc()
    139     {
    140         setNeedsSectionRecalc();
    141         recalcSections();
    142     }
    143 
    144     const Vector<ColumnStruct>& columns() const { return m_columns; }
    145     const Vector<int>& columnPositions() const { return m_columnPos; }
    146     void setColumnPosition(unsigned index, int position)
    147     {
    148         // Note that if our horizontal border-spacing changed, our position will change but not
    149         // our column's width. In practice, horizontal border-spacing won't change often.
    150         m_columnLogicalWidthChanged |= m_columnPos[index] != position;
    151         m_columnPos[index] = position;
    152     }
    153 
    154     RenderTableSection* header() const { return m_head; }
    155     RenderTableSection* footer() const { return m_foot; }
    156     RenderTableSection* firstBody() const { return m_firstBody; }
    157 
    158     // This function returns 0 if the table has no section.
    159     RenderTableSection* topSection() const;
    160     RenderTableSection* bottomSection() const;
    161 
    162     // This function returns 0 if the table has no non-empty sections.
    163     RenderTableSection* topNonEmptySection() const;
    164 
    165     unsigned lastColumnIndex() const { return numEffCols() - 1; }
    166 
    167     void splitColumn(unsigned position, unsigned firstSpan);
    168     void appendColumn(unsigned span);
    169     unsigned numEffCols() const { return m_columns.size(); }
    170     unsigned spanOfEffCol(unsigned effCol) const { return m_columns[effCol].span; }
    171 
    172     unsigned colToEffCol(unsigned column) const
    173     {
    174         unsigned effColumn = 0;
    175         unsigned numColumns = numEffCols();
    176         for (unsigned c = 0; effColumn < numColumns && c + m_columns[effColumn].span - 1 < column; ++effColumn)
    177             c += m_columns[effColumn].span;
    178         return effColumn;
    179     }
    180 
    181     unsigned effColToCol(unsigned effCol) const
    182     {
    183         unsigned c = 0;
    184         for (unsigned i = 0; i < effCol; i++)
    185             c += m_columns[i].span;
    186         return c;
    187     }
    188 
    189     LayoutUnit borderSpacingInRowDirection() const
    190     {
    191         if (unsigned effectiveColumnCount = numEffCols())
    192             return static_cast<LayoutUnit>(effectiveColumnCount + 1) * hBorderSpacing();
    193 
    194         return 0;
    195     }
    196 
    197     // Override paddingStart/End to return pixel values to match behavor of RenderTableCell.
    198     virtual LayoutUnit paddingEnd() const OVERRIDE { return static_cast<int>(RenderBlock::paddingEnd()); }
    199     virtual LayoutUnit paddingStart() const OVERRIDE { return static_cast<int>(RenderBlock::paddingStart()); }
    200 
    201     LayoutUnit bordersPaddingAndSpacingInRowDirection() const
    202     {
    203         // 'border-spacing' only applies to separate borders (see 17.6.1 The separated borders model).
    204         return borderStart() + borderEnd() + (collapseBorders() ? LayoutUnit() : (paddingStart() + paddingEnd() + borderSpacingInRowDirection()));
    205     }
    206 
    207     // Return the first column or column-group.
    208     RenderTableCol* firstColumn() const;
    209 
    210     RenderTableCol* colElement(unsigned col, bool* startEdge = 0, bool* endEdge = 0) const
    211     {
    212         // The common case is to not have columns, make that case fast.
    213         if (!m_hasColElements)
    214             return 0;
    215         return slowColElement(col, startEdge, endEdge);
    216     }
    217 
    218     bool needsSectionRecalc() const { return m_needsSectionRecalc; }
    219     void setNeedsSectionRecalc()
    220     {
    221         if (documentBeingDestroyed())
    222             return;
    223         m_needsSectionRecalc = true;
    224         setNeedsLayout();
    225     }
    226 
    227     RenderTableSection* sectionAbove(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
    228     RenderTableSection* sectionBelow(const RenderTableSection*, SkipEmptySectionsValue = DoNotSkipEmptySections) const;
    229 
    230     RenderTableCell* cellAbove(const RenderTableCell*) const;
    231     RenderTableCell* cellBelow(const RenderTableCell*) const;
    232     RenderTableCell* cellBefore(const RenderTableCell*) const;
    233     RenderTableCell* cellAfter(const RenderTableCell*) const;
    234 
    235     typedef Vector<CollapsedBorderValue> CollapsedBorderValues;
    236     void invalidateCollapsedBorders()
    237     {
    238         m_collapsedBordersValid = false;
    239         m_collapsedBorders.clear();
    240     }
    241     const CollapsedBorderValue* currentBorderValue() const { return m_currentBorder; }
    242 
    243     bool hasSections() const { return m_head || m_foot || m_firstBody; }
    244 
    245     void recalcSectionsIfNeeded() const
    246     {
    247         if (m_needsSectionRecalc)
    248             recalcSections();
    249     }
    250 
    251     static RenderTable* createAnonymousWithParentRenderer(const RenderObject*);
    252     virtual RenderBox* createAnonymousBoxWithSameTypeAs(const RenderObject* parent) const OVERRIDE
    253     {
    254         return createAnonymousWithParentRenderer(parent);
    255     }
    256 
    257     const BorderValue& tableStartBorderAdjoiningCell(const RenderTableCell*) const;
    258     const BorderValue& tableEndBorderAdjoiningCell(const RenderTableCell*) const;
    259 
    260     void addCaption(const RenderTableCaption*);
    261     void removeCaption(const RenderTableCaption*);
    262     void addColumn(const RenderTableCol*);
    263     void removeColumn(const RenderTableCol*);
    264 
    265 protected:
    266     virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
    267     virtual void simplifiedNormalFlowLayout();
    268 
    269 private:
    270     virtual const char* renderName() const { return "RenderTable"; }
    271 
    272     virtual bool isTable() const { return true; }
    273 
    274     virtual bool avoidsFloats() const { return true; }
    275 
    276     virtual void paint(PaintInfo&, const LayoutPoint&);
    277     virtual void paintObject(PaintInfo&, const LayoutPoint&);
    278     virtual void paintBoxDecorations(PaintInfo&, const LayoutPoint&);
    279     virtual void paintMask(PaintInfo&, const LayoutPoint&);
    280     virtual void layout();
    281     virtual void computeIntrinsicLogicalWidths(LayoutUnit& minWidth, LayoutUnit& maxWidth) const OVERRIDE;
    282     virtual void computePreferredLogicalWidths() OVERRIDE;
    283     virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) OVERRIDE;
    284 
    285     virtual int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const OVERRIDE;
    286     virtual int firstLineBoxBaseline() const OVERRIDE;
    287     virtual int inlineBlockBaseline(LineDirectionMode) const OVERRIDE;
    288 
    289     RenderTableCol* slowColElement(unsigned col, bool* startEdge, bool* endEdge) const;
    290 
    291     void updateColumnCache() const;
    292     void invalidateCachedColumns();
    293 
    294     virtual RenderBlock* firstLineBlock() const;
    295     virtual void updateFirstLetter();
    296 
    297     virtual void updateLogicalWidth() OVERRIDE;
    298 
    299     LayoutUnit convertStyleLogicalWidthToComputedWidth(const Length& styleLogicalWidth, LayoutUnit availableWidth);
    300     LayoutUnit convertStyleLogicalHeightToComputedHeight(const Length& styleLogicalHeight);
    301 
    302     virtual LayoutRect overflowClipRect(const LayoutPoint& location, RenderRegion*, OverlayScrollbarSizeRelevancy = IgnoreOverlayScrollbarSize);
    303 
    304     virtual void addOverflowFromChildren();
    305 
    306     void subtractCaptionRect(LayoutRect&) const;
    307 
    308     void recalcCollapsedBorders();
    309     void recalcSections() const;
    310     void layoutCaption(RenderTableCaption*);
    311 
    312     void distributeExtraLogicalHeight(int extraLogicalHeight);
    313 
    314     mutable Vector<int> m_columnPos;
    315     mutable Vector<ColumnStruct> m_columns;
    316     mutable Vector<RenderTableCaption*> m_captions;
    317     mutable Vector<RenderTableCol*> m_columnRenderers;
    318 
    319     mutable RenderTableSection* m_head;
    320     mutable RenderTableSection* m_foot;
    321     mutable RenderTableSection* m_firstBody;
    322 
    323     OwnPtr<TableLayout> m_tableLayout;
    324 
    325     CollapsedBorderValues m_collapsedBorders;
    326     const CollapsedBorderValue* m_currentBorder;
    327     bool m_collapsedBordersValid : 1;
    328 
    329     mutable bool m_hasColElements : 1;
    330     mutable bool m_needsSectionRecalc : 1;
    331 
    332     bool m_columnLogicalWidthChanged : 1;
    333     mutable bool m_columnRenderersValid: 1;
    334 
    335     short m_hSpacing;
    336     short m_vSpacing;
    337     int m_borderStart;
    338     int m_borderEnd;
    339 };
    340 
    341 inline RenderTableSection* RenderTable::topSection() const
    342 {
    343     ASSERT(!needsSectionRecalc());
    344     if (m_head)
    345         return m_head;
    346     if (m_firstBody)
    347         return m_firstBody;
    348     return m_foot;
    349 }
    350 
    351 inline RenderTable* toRenderTable(RenderObject* object)
    352 {
    353     ASSERT_WITH_SECURITY_IMPLICATION(!object || object->isTable());
    354     return static_cast<RenderTable*>(object);
    355 }
    356 
    357 inline const RenderTable* toRenderTable(const RenderObject* object)
    358 {
    359     ASSERT_WITH_SECURITY_IMPLICATION(!object || object->isTable());
    360     return static_cast<const RenderTable*>(object);
    361 }
    362 
    363 // This will catch anyone doing an unnecessary cast.
    364 void toRenderTable(const RenderTable*);
    365 
    366 } // namespace WebCore
    367 
    368 #endif // RenderTable_h
    369