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, 2007, 2008, 2009 Apple Inc. All rights reserved.
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.
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.
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.
26 #include "RenderTableCell.h"
28 #include "CollapsedBorderValue.h"
29 #include "FloatQuad.h"
30 #include "GraphicsContext.h"
31 #include "HTMLNames.h"
32 #include "HTMLTableCellElement.h"
33 #include "PaintInfo.h"
34 #include "RenderTableCol.h"
35 #include "RenderView.h"
36 #include "StyleProperties.h"
37 #include "TransformState.h"
38 #include <wtf/StackStats.h>
41 #include "MathMLElement.h"
42 #include "MathMLNames.h"
47 using namespace HTMLNames;
49 struct SameSizeAsRenderTableCell : public RenderBlockFlow {
51 LayoutUnit paddings[2];
54 COMPILE_ASSERT(sizeof(RenderTableCell) == sizeof(SameSizeAsRenderTableCell), RenderTableCell_should_stay_small);
55 COMPILE_ASSERT(sizeof(CollapsedBorderValue) == 12, CollapsedBorderValue_should_stay_small);
57 RenderTableCell::RenderTableCell(Element& element, RenderStyle&& style)
58 : RenderBlockFlow(element, WTFMove(style))
59 , m_column(unsetColumnIndex)
60 , m_cellWidthChanged(false)
63 , m_hasEmptyCollapsedBeforeBorder(false)
64 , m_hasEmptyCollapsedAfterBorder(false)
65 , m_hasEmptyCollapsedStartBorder(false)
66 , m_hasEmptyCollapsedEndBorder(false)
68 // We only update the flags when notified of DOM changes in colSpanOrRowSpanChanged()
69 // so we need to set their initial values here in case something asks for colSpan()/rowSpan() before then.
70 updateColAndRowSpanFlags();
73 RenderTableCell::RenderTableCell(Document& document, RenderStyle&& style)
74 : RenderBlockFlow(document, WTFMove(style))
75 , m_column(unsetColumnIndex)
76 , m_cellWidthChanged(false)
79 , m_hasEmptyCollapsedBeforeBorder(false)
80 , m_hasEmptyCollapsedAfterBorder(false)
81 , m_hasEmptyCollapsedStartBorder(false)
82 , m_hasEmptyCollapsedEndBorder(false)
86 void RenderTableCell::willBeRemovedFromTree()
88 RenderBlockFlow::willBeRemovedFromTree();
89 if (!table() || !section())
91 RenderTableSection* section = this->section();
92 table()->invalidateCollapsedBorders();
93 section->setNeedsCellRecalc();
96 unsigned RenderTableCell::parseColSpanFromDOM() const
99 if (is<HTMLTableCellElement>(*element()))
100 return std::min<unsigned>(downcast<HTMLTableCellElement>(*element()).colSpan(), maxColumnIndex);
102 if (element()->hasTagName(MathMLNames::mtdTag))
103 return std::min<unsigned>(downcast<MathMLElement>(*element()).colSpan(), maxColumnIndex);
108 unsigned RenderTableCell::parseRowSpanFromDOM() const
111 if (is<HTMLTableCellElement>(*element()))
112 return std::min<unsigned>(downcast<HTMLTableCellElement>(*element()).rowSpan(), maxRowIndex);
114 if (element()->hasTagName(MathMLNames::mtdTag))
115 return std::min<unsigned>(downcast<MathMLElement>(*element()).rowSpan(), maxRowIndex);
120 void RenderTableCell::updateColAndRowSpanFlags()
122 // The vast majority of table cells do not have a colspan or rowspan,
123 // so we keep a bool to know if we need to bother reading from the DOM.
124 m_hasColSpan = element() && parseColSpanFromDOM() != 1;
125 m_hasRowSpan = element() && parseRowSpanFromDOM() != 1;
128 void RenderTableCell::colSpanOrRowSpanChanged()
132 ASSERT(element()->hasTagName(tdTag) || element()->hasTagName(thTag) || element()->hasTagName(MathMLNames::mtdTag));
134 ASSERT(element()->hasTagName(tdTag) || element()->hasTagName(thTag));
137 updateColAndRowSpanFlags();
139 // FIXME: I suspect that we could return early here if !m_hasColSpan && !m_hasRowSpan.
141 setNeedsLayoutAndPrefWidthsRecalc();
142 if (parent() && section())
143 section()->setNeedsCellRecalc();
146 Length RenderTableCell::logicalWidthFromColumns(RenderTableCol* firstColForThisCell, Length widthFromStyle) const
148 ASSERT(firstColForThisCell && firstColForThisCell == table()->colElement(col()));
149 RenderTableCol* tableCol = firstColForThisCell;
151 unsigned colSpanCount = colSpan();
152 LayoutUnit colWidthSum = 0;
153 for (unsigned i = 1; i <= colSpanCount; i++) {
154 Length colWidth = tableCol->style().logicalWidth();
156 // Percentage value should be returned only for colSpan == 1.
157 // Otherwise we return original width for the cell.
158 if (!colWidth.isFixed()) {
159 if (colSpanCount > 1)
160 return widthFromStyle;
164 colWidthSum += colWidth.value();
165 tableCol = tableCol->nextColumn();
166 // If no next <col> tag found for the span we just return what we have for now.
171 // Column widths specified on <col> apply to the border box of the cell, see bug 8126.
172 // FIXME: Why is border/padding ignored in the negative width case?
174 return Length(std::max<LayoutUnit>(0, colWidthSum - borderAndPaddingLogicalWidth()), Fixed);
175 return Length(colWidthSum, Fixed);
178 void RenderTableCell::computePreferredLogicalWidths()
180 // The child cells rely on the grids up in the sections to do their computePreferredLogicalWidths work. Normally the sections are set up early, as table
181 // cells are added, but relayout can cause the cells to be freed, leaving stale pointers in the sections'
182 // grids. We must refresh those grids before the child cells try to use them.
183 table()->recalcSectionsIfNeeded();
185 RenderBlockFlow::computePreferredLogicalWidths();
186 if (!element() || !style().autoWrap() || !element()->hasAttributeWithoutSynchronization(nowrapAttr))
189 Length w = styleOrColLogicalWidth();
191 // Nowrap is set, but we didn't actually use it because of the
192 // fixed width set on the cell. Even so, it is a WinIE/Moz trait
193 // to make the minwidth of the cell into the fixed width. They do this
194 // even in strict mode, so do not make this a quirk. Affected the top
196 m_minPreferredLogicalWidth = std::max<LayoutUnit>(w.value(), m_minPreferredLogicalWidth);
200 void RenderTableCell::computeIntrinsicPadding(LayoutUnit rowHeight)
202 LayoutUnit oldIntrinsicPaddingBefore = intrinsicPaddingBefore();
203 LayoutUnit oldIntrinsicPaddingAfter = intrinsicPaddingAfter();
204 LayoutUnit logicalHeightWithoutIntrinsicPadding = logicalHeight() - oldIntrinsicPaddingBefore - oldIntrinsicPaddingAfter;
206 LayoutUnit intrinsicPaddingBefore = 0;
207 switch (style().verticalAlign()) {
214 LayoutUnit baseline = cellBaselinePosition();
215 if (baseline > borderAndPaddingBefore())
216 intrinsicPaddingBefore = section()->rowBaseline(rowIndex()) - (baseline - oldIntrinsicPaddingBefore);
222 intrinsicPaddingBefore = (rowHeight - logicalHeightWithoutIntrinsicPadding) / 2;
225 intrinsicPaddingBefore = rowHeight - logicalHeightWithoutIntrinsicPadding;
227 case BASELINE_MIDDLE:
231 LayoutUnit intrinsicPaddingAfter = rowHeight - logicalHeightWithoutIntrinsicPadding - intrinsicPaddingBefore;
232 setIntrinsicPaddingBefore(intrinsicPaddingBefore);
233 setIntrinsicPaddingAfter(intrinsicPaddingAfter);
235 // FIXME: Changing an intrinsic padding shouldn't trigger a relayout as it only shifts the cell inside the row but
236 // doesn't change the logical height.
237 if (intrinsicPaddingBefore != oldIntrinsicPaddingBefore || intrinsicPaddingAfter != oldIntrinsicPaddingAfter)
238 setNeedsLayout(MarkOnlyThis);
241 void RenderTableCell::updateLogicalWidth()
245 void RenderTableCell::setCellLogicalWidth(LayoutUnit tableLayoutLogicalWidth)
247 if (tableLayoutLogicalWidth == logicalWidth())
250 setNeedsLayout(MarkOnlyThis);
251 row()->setChildNeedsLayout(MarkOnlyThis);
253 if (!table()->selfNeedsLayout() && checkForRepaintDuringLayout())
256 setLogicalWidth(tableLayoutLogicalWidth);
257 setCellWidthChanged(true);
260 void RenderTableCell::layout()
262 StackStats::LayoutCheckPoint layoutCheckPoint;
265 int oldCellBaseline = cellBaselinePosition();
266 layoutBlock(cellWidthChanged());
268 // If we have replaced content, the intrinsic height of our content may have changed since the last time we laid out. If that's the case the intrinsic padding we used
269 // for layout (the padding required to push the contents of the cell down to the row's baseline) is included in our new height and baseline and makes both
270 // of them wrong. So if our content's intrinsic height has changed push the new content up into the intrinsic padding and relayout so that the rest of
271 // table and row layout can use the correct baseline and height for this cell.
272 if (isBaselineAligned() && section()->rowBaseline(rowIndex()) && cellBaselinePosition() > section()->rowBaseline(rowIndex())) {
273 LayoutUnit newIntrinsicPaddingBefore = std::max<LayoutUnit>(0, intrinsicPaddingBefore() - std::max<LayoutUnit>(0, cellBaselinePosition() - oldCellBaseline));
274 setIntrinsicPaddingBefore(newIntrinsicPaddingBefore);
275 setNeedsLayout(MarkOnlyThis);
276 layoutBlock(cellWidthChanged());
278 invalidateHasEmptyCollapsedBorders();
279 setCellWidthChanged(false);
282 LayoutUnit RenderTableCell::paddingTop() const
284 LayoutUnit result = computedCSSPaddingTop();
285 if (!isHorizontalWritingMode())
287 return result + (style().writingMode() == TopToBottomWritingMode ? intrinsicPaddingBefore() : intrinsicPaddingAfter());
290 LayoutUnit RenderTableCell::paddingBottom() const
292 LayoutUnit result = computedCSSPaddingBottom();
293 if (!isHorizontalWritingMode())
295 return result + (style().writingMode() == TopToBottomWritingMode ? intrinsicPaddingAfter() : intrinsicPaddingBefore());
298 LayoutUnit RenderTableCell::paddingLeft() const
300 LayoutUnit result = computedCSSPaddingLeft();
301 if (isHorizontalWritingMode())
303 return result + (style().writingMode() == LeftToRightWritingMode ? intrinsicPaddingBefore() : intrinsicPaddingAfter());
306 LayoutUnit RenderTableCell::paddingRight() const
308 LayoutUnit result = computedCSSPaddingRight();
309 if (isHorizontalWritingMode())
311 return result + (style().writingMode() == LeftToRightWritingMode ? intrinsicPaddingAfter() : intrinsicPaddingBefore());
314 LayoutUnit RenderTableCell::paddingBefore() const
316 return computedCSSPaddingBefore() + intrinsicPaddingBefore();
319 LayoutUnit RenderTableCell::paddingAfter() const
321 return computedCSSPaddingAfter() + intrinsicPaddingAfter();
324 void RenderTableCell::setOverrideLogicalContentHeightFromRowHeight(LayoutUnit rowHeight)
326 clearIntrinsicPadding();
327 setOverrideLogicalContentHeight(std::max<LayoutUnit>(0, rowHeight - borderAndPaddingLogicalHeight()));
330 LayoutSize RenderTableCell::offsetFromContainer(RenderElement& container, const LayoutPoint& point, bool* offsetDependsOnPoint) const
332 ASSERT(&container == this->container());
334 LayoutSize offset = RenderBlockFlow::offsetFromContainer(container, point, offsetDependsOnPoint);
336 offset -= parentBox()->locationOffset();
341 LayoutRect RenderTableCell::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
343 // If the table grid is dirty, we cannot get reliable information about adjoining cells,
344 // so we ignore outside borders. This should not be a problem because it means that
345 // the table is going to recalculate the grid, relayout and repaint its current rect, which
346 // includes any outside borders of this cell.
347 if (!table()->collapseBorders() || table()->needsSectionRecalc())
348 return RenderBlockFlow::clippedOverflowRectForRepaint(repaintContainer);
350 bool rtl = !styleForCellFlow().isLeftToRightDirection();
351 LayoutUnit outlineSize = style().outlineSize();
352 LayoutUnit left = std::max(borderHalfLeft(true), outlineSize);
353 LayoutUnit right = std::max(borderHalfRight(true), outlineSize);
354 LayoutUnit top = std::max(borderHalfTop(true), outlineSize);
355 LayoutUnit bottom = std::max(borderHalfBottom(true), outlineSize);
356 if ((left && !rtl) || (right && rtl)) {
357 if (RenderTableCell* before = table()->cellBefore(this)) {
358 top = std::max(top, before->borderHalfTop(true));
359 bottom = std::max(bottom, before->borderHalfBottom(true));
362 if ((left && rtl) || (right && !rtl)) {
363 if (RenderTableCell* after = table()->cellAfter(this)) {
364 top = std::max(top, after->borderHalfTop(true));
365 bottom = std::max(bottom, after->borderHalfBottom(true));
369 if (RenderTableCell* above = table()->cellAbove(this)) {
370 left = std::max(left, above->borderHalfLeft(true));
371 right = std::max(right, above->borderHalfRight(true));
375 if (RenderTableCell* below = table()->cellBelow(this)) {
376 left = std::max(left, below->borderHalfLeft(true));
377 right = std::max(right, below->borderHalfRight(true));
380 LayoutPoint location(std::max<LayoutUnit>(left, -visualOverflowRect().x()), std::max<LayoutUnit>(top, -visualOverflowRect().y()));
381 LayoutRect r(-location.x(), -location.y(), location.x() + std::max(width() + right, visualOverflowRect().maxX()), location.y() + std::max(height() + bottom, visualOverflowRect().maxY()));
383 // FIXME: layoutDelta needs to be applied in parts before/after transforms and
384 // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308
385 r.move(view().layoutDelta());
386 return computeRectForRepaint(r, repaintContainer);
389 LayoutRect RenderTableCell::computeRectForRepaint(const LayoutRect& rect, const RenderLayerModelObject* repaintContainer, RepaintContext context) const
391 if (repaintContainer == this)
393 LayoutRect adjustedRect = rect;
394 if ((!view().layoutStateEnabled() || repaintContainer) && parent())
395 adjustedRect.moveBy(-parentBox()->location()); // Rows are in the same coordinate space, so don't add their offset in.
396 return RenderBlockFlow::computeRectForRepaint(adjustedRect, repaintContainer, context);
399 LayoutUnit RenderTableCell::cellBaselinePosition() const
401 // <http://www.w3.org/TR/2007/CR-CSS21-20070719/tables.html#height-layout>: The baseline of a cell is the baseline of
402 // the first in-flow line box in the cell, or the first in-flow table-row in the cell, whichever comes first. If there
403 // is no such line box or table-row, the baseline is the bottom of content edge of the cell box.
404 return firstLineBaseline().valueOr(borderAndPaddingBefore() + contentLogicalHeight());
407 static inline void markCellDirtyWhenCollapsedBorderChanges(RenderTableCell* cell)
411 cell->setNeedsLayoutAndPrefWidthsRecalc();
414 void RenderTableCell::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
416 ASSERT(style().display() == TABLE_CELL);
417 ASSERT(!row() || row()->rowIndexWasSet());
419 RenderBlockFlow::styleDidChange(diff, oldStyle);
420 setHasVisibleBoxDecorations(true); // FIXME: Optimize this to only set to true if necessary.
422 if (parent() && section() && oldStyle && style().height() != oldStyle->height())
423 section()->rowLogicalHeightChanged(rowIndex());
425 // Our intrinsic padding pushes us down to align with the baseline of other cells on the row. If our vertical-align
426 // has changed then so will the padding needed to align with other cells - clear it so we can recalculate it from scratch.
427 if (oldStyle && style().verticalAlign() != oldStyle->verticalAlign())
428 clearIntrinsicPadding();
430 // If border was changed, notify table.
431 RenderTable* table = this->table();
432 if (table && oldStyle && oldStyle->border() != style().border()) {
433 table->invalidateCollapsedBorders(this);
434 if (table->collapseBorders() && diff == StyleDifferenceLayout) {
435 markCellDirtyWhenCollapsedBorderChanges(table->cellBelow(this));
436 markCellDirtyWhenCollapsedBorderChanges(table->cellAbove(this));
437 markCellDirtyWhenCollapsedBorderChanges(table->cellBefore(this));
438 markCellDirtyWhenCollapsedBorderChanges(table->cellAfter(this));
443 // The following rules apply for resolving conflicts and figuring out which border
445 // (1) Borders with the 'border-style' of 'hidden' take precedence over all other conflicting
446 // borders. Any border with this value suppresses all borders at this location.
447 // (2) Borders with a style of 'none' have the lowest priority. Only if the border properties of all
448 // the elements meeting at this edge are 'none' will the border be omitted (but note that 'none' is
449 // the default value for the border style.)
450 // (3) If none of the styles are 'hidden' and at least one of them is not 'none', then narrow borders
451 // are discarded in favor of wider ones. If several have the same 'border-width' then styles are preferred
452 // in this order: 'double', 'solid', 'dashed', 'dotted', 'ridge', 'outset', 'groove', and the lowest: 'inset'.
453 // (4) If border styles differ only in color, then a style set on a cell wins over one on a row,
454 // which wins over a row group, column, column group and, lastly, table. It is undefined which color
455 // is used when two elements of the same type disagree.
456 static int compareBorders(const CollapsedBorderValue& border1, const CollapsedBorderValue& border2)
458 // Sanity check the values passed in. The null border have lowest priority.
459 if (!border2.exists()) {
460 if (!border1.exists())
464 if (!border1.exists())
468 if (border2.style() == BHIDDEN) {
469 if (border1.style() == BHIDDEN)
473 if (border1.style() == BHIDDEN)
476 // Rule #2 above. A style of 'none' has lowest priority and always loses to any other border.
477 if (border2.style() == BNONE) {
478 if (border1.style() == BNONE)
482 if (border1.style() == BNONE)
485 // The first part of rule #3 above. Wider borders win.
486 if (border1.width() != border2.width())
487 return border1.width() < border2.width() ? -1 : 1;
489 // The borders have equal width. Sort by border style.
490 if (border1.style() != border2.style())
491 return border1.style() < border2.style() ? -1 : 1;
493 // The border have the same width and style. Rely on precedence (cell over row over row group, etc.)
494 if (border1.precedence() == border2.precedence())
496 return border1.precedence() < border2.precedence() ? -1 : 1;
499 static CollapsedBorderValue chooseBorder(const CollapsedBorderValue& border1, const CollapsedBorderValue& border2)
501 const CollapsedBorderValue& border = compareBorders(border1, border2) < 0 ? border2 : border1;
502 return border.style() == BHIDDEN ? CollapsedBorderValue() : border;
505 bool RenderTableCell::hasStartBorderAdjoiningTable() const
507 bool isStartColumn = !col();
508 bool isEndColumn = table()->colToEffCol(col() + colSpan() - 1) == table()->numEffCols() - 1;
509 bool hasSameDirectionAsTable = isDirectionSame(this, section());
511 // The table direction determines the row direction. In mixed directionality, we cannot guarantee that
512 // we have a common border with the table (think a ltr table with rtl start cell).
513 return (isStartColumn && hasSameDirectionAsTable) || (isEndColumn && !hasSameDirectionAsTable);
516 bool RenderTableCell::hasEndBorderAdjoiningTable() const
518 bool isStartColumn = !col();
519 bool isEndColumn = table()->colToEffCol(col() + colSpan() - 1) == table()->numEffCols() - 1;
520 bool hasSameDirectionAsTable = isDirectionSame(this, section());
522 // The table direction determines the row direction. In mixed directionality, we cannot guarantee that
523 // we have a common border with the table (think a ltr table with ltr end cell).
524 return (isStartColumn && !hasSameDirectionAsTable) || (isEndColumn && hasSameDirectionAsTable);
527 static CollapsedBorderValue emptyBorder()
529 return CollapsedBorderValue(BorderValue(), Color(), BCELL);
532 CollapsedBorderValue RenderTableCell::collapsedStartBorder(IncludeBorderColorOrNot includeColor) const
534 if (!table() || !section())
535 return emptyBorder();
537 if (m_hasEmptyCollapsedStartBorder)
538 return emptyBorder();
540 if (table()->collapsedBordersAreValid())
541 return section()->cachedCollapsedBorder(*this, CBSStart);
543 CollapsedBorderValue result = computeCollapsedStartBorder(includeColor);
544 setHasEmptyCollapsedBorder(CBSStart, !result.width());
545 if (includeColor && !m_hasEmptyCollapsedStartBorder)
546 section()->setCachedCollapsedBorder(*this, CBSStart, result);
550 CollapsedBorderValue RenderTableCell::computeCollapsedStartBorder(IncludeBorderColorOrNot includeColor) const
552 // For the start border, we need to check, in order of precedence:
553 // (1) Our start border.
554 int startColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderStartColor, styleForCellFlow().direction(), styleForCellFlow().writingMode()) : 0;
555 int endColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderEndColor, styleForCellFlow().direction(), styleForCellFlow().writingMode()) : 0;
556 CollapsedBorderValue result(style().borderStart(), includeColor ? style().visitedDependentColor(startColorProperty) : Color(), BCELL);
558 RenderTable* table = this->table();
561 // (2) The end border of the preceding cell.
562 RenderTableCell* cellBefore = table->cellBefore(this);
564 CollapsedBorderValue cellBeforeAdjoiningBorder = CollapsedBorderValue(cellBefore->borderAdjoiningCellAfter(*this), includeColor ? cellBefore->style().visitedDependentColor(endColorProperty) : Color(), BCELL);
565 // |result| should be the 2nd argument as |cellBefore| should win in case of equality per CSS 2.1 (Border conflict resolution, point 4).
566 result = chooseBorder(cellBeforeAdjoiningBorder, result);
567 if (!result.exists())
571 bool startBorderAdjoinsTable = hasStartBorderAdjoiningTable();
572 if (startBorderAdjoinsTable) {
573 // (3) Our row's start border.
574 result = chooseBorder(result, CollapsedBorderValue(row()->borderAdjoiningStartCell(*this), includeColor ? parent()->style().visitedDependentColor(startColorProperty) : Color(), BROW));
575 if (!result.exists())
578 // (4) Our row group's start border.
579 result = chooseBorder(result, CollapsedBorderValue(section()->borderAdjoiningStartCell(*this), includeColor ? section()->style().visitedDependentColor(startColorProperty) : Color(), BROWGROUP));
580 if (!result.exists())
584 // (5) Our column and column group's start borders.
587 if (RenderTableCol* colElt = table->colElement(col(), &startColEdge, &endColEdge)) {
588 if (colElt->isTableColumnGroup() && startColEdge) {
589 // The |colElt| is a column group and is also the first colgroup (in case of spanned colgroups).
590 result = chooseBorder(result, CollapsedBorderValue(colElt->borderAdjoiningCellStartBorder(), includeColor ? colElt->style().visitedDependentColor(startColorProperty) : Color(), BCOLGROUP));
591 if (!result.exists())
593 } else if (!colElt->isTableColumnGroup()) {
594 // We first consider the |colElt| and irrespective of whether it is a spanned col or not, we apply
595 // its start border. This is as per HTML5 which states that: "For the purposes of the CSS table model,
596 // the col element is expected to be treated as if it was present as many times as its span attribute specifies".
597 result = chooseBorder(result, CollapsedBorderValue(colElt->borderAdjoiningCellStartBorder(), includeColor ? colElt->style().visitedDependentColor(startColorProperty) : Color(), BCOL));
598 if (!result.exists())
600 // Next, apply the start border of the enclosing colgroup but only if it is adjacent to the cell's edge.
601 if (RenderTableCol* enclosingColumnGroup = colElt->enclosingColumnGroupIfAdjacentBefore()) {
602 result = chooseBorder(result, CollapsedBorderValue(enclosingColumnGroup->borderAdjoiningCellStartBorder(), includeColor ? enclosingColumnGroup->style().visitedDependentColor(startColorProperty) : Color(), BCOLGROUP));
603 if (!result.exists())
609 // (6) The end border of the preceding column.
611 if (RenderTableCol* colElt = table->colElement(col() - 1, &startColEdge, &endColEdge)) {
612 if (colElt->isTableColumnGroup() && endColEdge) {
613 // The element is a colgroup and is also the last colgroup (in case of spanned colgroups).
614 result = chooseBorder(CollapsedBorderValue(colElt->borderAdjoiningCellAfter(*this), includeColor ? colElt->style().visitedDependentColor(endColorProperty) : Color(), BCOLGROUP), result);
615 if (!result.exists())
617 } else if (colElt->isTableColumn()) {
618 // Resolve the collapsing border against the col's border ignoring any 'span' as per HTML5.
619 result = chooseBorder(CollapsedBorderValue(colElt->borderAdjoiningCellAfter(*this), includeColor ? colElt->style().visitedDependentColor(endColorProperty) : Color(), BCOL), result);
620 if (!result.exists())
622 // Next, if the previous col has a parent colgroup then its end border should be applied
623 // but only if it is adjacent to the cell's edge.
624 if (RenderTableCol* enclosingColumnGroup = colElt->enclosingColumnGroupIfAdjacentAfter()) {
625 result = chooseBorder(CollapsedBorderValue(enclosingColumnGroup->borderAdjoiningCellEndBorder(), includeColor ? enclosingColumnGroup->style().visitedDependentColor(endColorProperty) : Color(), BCOLGROUP), result);
626 if (!result.exists())
633 if (startBorderAdjoinsTable) {
634 // (7) The table's start border.
635 result = chooseBorder(result, CollapsedBorderValue(table->tableStartBorderAdjoiningCell(*this), includeColor ? table->style().visitedDependentColor(startColorProperty) : Color(), BTABLE));
636 if (!result.exists())
643 CollapsedBorderValue RenderTableCell::collapsedEndBorder(IncludeBorderColorOrNot includeColor) const
645 if (!table() || !section())
646 return emptyBorder();
648 if (m_hasEmptyCollapsedEndBorder)
649 return emptyBorder();
651 if (table()->collapsedBordersAreValid())
652 return section()->cachedCollapsedBorder(*this, CBSEnd);
654 CollapsedBorderValue result = computeCollapsedEndBorder(includeColor);
655 setHasEmptyCollapsedBorder(CBSEnd, !result.width());
656 if (includeColor && !m_hasEmptyCollapsedEndBorder)
657 section()->setCachedCollapsedBorder(*this, CBSEnd, result);
661 CollapsedBorderValue RenderTableCell::computeCollapsedEndBorder(IncludeBorderColorOrNot includeColor) const
663 // For end border, we need to check, in order of precedence:
664 // (1) Our end border.
665 int startColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderStartColor, styleForCellFlow().direction(), styleForCellFlow().writingMode()) : 0;
666 int endColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderEndColor, styleForCellFlow().direction(), styleForCellFlow().writingMode()) : 0;
667 CollapsedBorderValue result = CollapsedBorderValue(style().borderEnd(), includeColor ? style().visitedDependentColor(endColorProperty) : Color(), BCELL);
669 RenderTable* table = this->table();
672 // Note: We have to use the effective column information instead of whether we have a cell after as a table doesn't
673 // have to be regular (any row can have less cells than the total cell count).
674 bool isEndColumn = table->colToEffCol(col() + colSpan() - 1) == table->numEffCols() - 1;
675 // (2) The start border of the following cell.
677 if (RenderTableCell* cellAfter = table->cellAfter(this)) {
678 CollapsedBorderValue cellAfterAdjoiningBorder = CollapsedBorderValue(cellAfter->borderAdjoiningCellBefore(*this), includeColor ? cellAfter->style().visitedDependentColor(startColorProperty) : Color(), BCELL);
679 result = chooseBorder(result, cellAfterAdjoiningBorder);
680 if (!result.exists())
685 bool endBorderAdjoinsTable = hasEndBorderAdjoiningTable();
686 if (endBorderAdjoinsTable) {
687 // (3) Our row's end border.
688 result = chooseBorder(result, CollapsedBorderValue(row()->borderAdjoiningEndCell(*this), includeColor ? parent()->style().visitedDependentColor(endColorProperty) : Color(), BROW));
689 if (!result.exists())
692 // (4) Our row group's end border.
693 result = chooseBorder(result, CollapsedBorderValue(section()->borderAdjoiningEndCell(*this), includeColor ? section()->style().visitedDependentColor(endColorProperty) : Color(), BROWGROUP));
694 if (!result.exists())
698 // (5) Our column and column group's end borders.
701 if (RenderTableCol* colElt = table->colElement(col() + colSpan() - 1, &startColEdge, &endColEdge)) {
702 if (colElt->isTableColumnGroup() && endColEdge) {
703 // The element is a colgroup and is also the last colgroup (in case of spanned colgroups).
704 result = chooseBorder(result, CollapsedBorderValue(colElt->borderAdjoiningCellEndBorder(), includeColor ? colElt->style().visitedDependentColor(endColorProperty) : Color(), BCOLGROUP));
705 if (!result.exists())
707 } else if (!colElt->isTableColumnGroup()) {
708 // First apply the end border of the column irrespective of whether it is spanned or not. This is as per
709 // HTML5 which states that: "For the purposes of the CSS table model, the col element is expected to be
710 // treated as if it was present as many times as its span attribute specifies".
711 result = chooseBorder(result, CollapsedBorderValue(colElt->borderAdjoiningCellEndBorder(), includeColor ? colElt->style().visitedDependentColor(endColorProperty) : Color(), BCOL));
712 if (!result.exists())
714 // Next, if it has a parent colgroup then we apply its end border but only if it is adjacent to the cell.
715 if (RenderTableCol* enclosingColumnGroup = colElt->enclosingColumnGroupIfAdjacentAfter()) {
716 result = chooseBorder(result, CollapsedBorderValue(enclosingColumnGroup->borderAdjoiningCellEndBorder(), includeColor ? enclosingColumnGroup->style().visitedDependentColor(endColorProperty) : Color(), BCOLGROUP));
717 if (!result.exists())
723 // (6) The start border of the next column.
725 if (RenderTableCol* colElt = table->colElement(col() + colSpan(), &startColEdge, &endColEdge)) {
726 if (colElt->isTableColumnGroup() && startColEdge) {
727 // This case is a colgroup without any col, we only compute it if it is adjacent to the cell's edge.
728 result = chooseBorder(result, CollapsedBorderValue(colElt->borderAdjoiningCellBefore(*this), includeColor ? colElt->style().visitedDependentColor(startColorProperty) : Color(), BCOLGROUP));
729 if (!result.exists())
731 } else if (colElt->isTableColumn()) {
732 // Resolve the collapsing border against the col's border ignoring any 'span' as per HTML5.
733 result = chooseBorder(result, CollapsedBorderValue(colElt->borderAdjoiningCellBefore(*this), includeColor ? colElt->style().visitedDependentColor(startColorProperty) : Color(), BCOL));
734 if (!result.exists())
736 // If we have a parent colgroup, resolve the border only if it is adjacent to the cell.
737 if (RenderTableCol* enclosingColumnGroup = colElt->enclosingColumnGroupIfAdjacentBefore()) {
738 result = chooseBorder(result, CollapsedBorderValue(enclosingColumnGroup->borderAdjoiningCellStartBorder(), includeColor ? enclosingColumnGroup->style().visitedDependentColor(startColorProperty) : Color(), BCOLGROUP));
739 if (!result.exists())
746 if (endBorderAdjoinsTable) {
747 // (7) The table's end border.
748 result = chooseBorder(result, CollapsedBorderValue(table->tableEndBorderAdjoiningCell(*this), includeColor ? table->style().visitedDependentColor(endColorProperty) : Color(), BTABLE));
749 if (!result.exists())
756 CollapsedBorderValue RenderTableCell::collapsedBeforeBorder(IncludeBorderColorOrNot includeColor) const
758 if (!table() || !section())
759 return emptyBorder();
761 if (m_hasEmptyCollapsedBeforeBorder)
762 return emptyBorder();
764 if (table()->collapsedBordersAreValid())
765 return section()->cachedCollapsedBorder(*this, CBSBefore);
767 CollapsedBorderValue result = computeCollapsedBeforeBorder(includeColor);
768 setHasEmptyCollapsedBorder(CBSBefore, !result.width());
769 if (includeColor && !m_hasEmptyCollapsedBeforeBorder)
770 section()->setCachedCollapsedBorder(*this, CBSBefore, result);
774 CollapsedBorderValue RenderTableCell::computeCollapsedBeforeBorder(IncludeBorderColorOrNot includeColor) const
776 // For before border, we need to check, in order of precedence:
777 // (1) Our before border.
778 int beforeColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderBeforeColor, styleForCellFlow().direction(), styleForCellFlow().writingMode()) : 0;
779 int afterColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderAfterColor, styleForCellFlow().direction(), styleForCellFlow().writingMode()) : 0;
780 CollapsedBorderValue result = CollapsedBorderValue(style().borderBefore(), includeColor ? style().visitedDependentColor(beforeColorProperty) : Color(), BCELL);
782 RenderTable* table = this->table();
785 RenderTableCell* prevCell = table->cellAbove(this);
787 // (2) A before cell's after border.
788 result = chooseBorder(CollapsedBorderValue(prevCell->style().borderAfter(), includeColor ? prevCell->style().visitedDependentColor(afterColorProperty) : Color(), BCELL), result);
789 if (!result.exists())
793 // (3) Our row's before border.
794 result = chooseBorder(result, CollapsedBorderValue(parent()->style().borderBefore(), includeColor ? parent()->style().visitedDependentColor(beforeColorProperty) : Color(), BROW));
795 if (!result.exists())
798 // (4) The previous row's after border.
800 RenderObject* prevRow = 0;
801 if (prevCell->section() == section())
802 prevRow = parent()->previousSibling();
804 prevRow = prevCell->section()->lastRow();
807 result = chooseBorder(CollapsedBorderValue(prevRow->style().borderAfter(), includeColor ? prevRow->style().visitedDependentColor(afterColorProperty) : Color(), BROW), result);
808 if (!result.exists())
813 // Now check row groups.
814 RenderTableSection* currSection = section();
816 // (5) Our row group's before border.
817 result = chooseBorder(result, CollapsedBorderValue(currSection->style().borderBefore(), includeColor ? currSection->style().visitedDependentColor(beforeColorProperty) : Color(), BROWGROUP));
818 if (!result.exists())
821 // (6) Previous row group's after border.
822 currSection = table->sectionAbove(currSection, SkipEmptySections);
824 result = chooseBorder(CollapsedBorderValue(currSection->style().borderAfter(), includeColor ? currSection->style().visitedDependentColor(afterColorProperty) : Color(), BROWGROUP), result);
825 if (!result.exists())
831 // (8) Our column and column group's before borders.
832 RenderTableCol* colElt = table->colElement(col());
834 result = chooseBorder(result, CollapsedBorderValue(colElt->style().borderBefore(), includeColor ? colElt->style().visitedDependentColor(beforeColorProperty) : Color(), BCOL));
835 if (!result.exists())
837 if (RenderTableCol* enclosingColumnGroup = colElt->enclosingColumnGroup()) {
838 result = chooseBorder(result, CollapsedBorderValue(enclosingColumnGroup->style().borderBefore(), includeColor ? enclosingColumnGroup->style().visitedDependentColor(beforeColorProperty) : Color(), BCOLGROUP));
839 if (!result.exists())
844 // (9) The table's before border.
845 result = chooseBorder(result, CollapsedBorderValue(table->style().borderBefore(), includeColor ? table->style().visitedDependentColor(beforeColorProperty) : Color(), BTABLE));
846 if (!result.exists())
853 CollapsedBorderValue RenderTableCell::collapsedAfterBorder(IncludeBorderColorOrNot includeColor) const
855 if (!table() || !section())
856 return emptyBorder();
858 if (m_hasEmptyCollapsedAfterBorder)
859 return emptyBorder();
861 if (table()->collapsedBordersAreValid())
862 return section()->cachedCollapsedBorder(*this, CBSAfter);
864 CollapsedBorderValue result = computeCollapsedAfterBorder(includeColor);
865 setHasEmptyCollapsedBorder(CBSAfter, !result.width());
866 if (includeColor && !m_hasEmptyCollapsedAfterBorder)
867 section()->setCachedCollapsedBorder(*this, CBSAfter, result);
871 CollapsedBorderValue RenderTableCell::computeCollapsedAfterBorder(IncludeBorderColorOrNot includeColor) const
873 // For after border, we need to check, in order of precedence:
874 // (1) Our after border.
875 int beforeColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderBeforeColor, styleForCellFlow().direction(), styleForCellFlow().writingMode()) : 0;
876 int afterColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderAfterColor, styleForCellFlow().direction(), styleForCellFlow().writingMode()) : 0;
877 CollapsedBorderValue result = CollapsedBorderValue(style().borderAfter(), includeColor ? style().visitedDependentColor(afterColorProperty) : Color(), BCELL);
879 RenderTable* table = this->table();
882 RenderTableCell* nextCell = table->cellBelow(this);
884 // (2) An after cell's before border.
885 result = chooseBorder(result, CollapsedBorderValue(nextCell->style().borderBefore(), includeColor ? nextCell->style().visitedDependentColor(beforeColorProperty) : Color(), BCELL));
886 if (!result.exists())
890 // (3) Our row's after border. (FIXME: Deal with rowspan!)
891 result = chooseBorder(result, CollapsedBorderValue(parent()->style().borderAfter(), includeColor ? parent()->style().visitedDependentColor(afterColorProperty) : Color(), BROW));
892 if (!result.exists())
895 // (4) The next row's before border.
897 result = chooseBorder(result, CollapsedBorderValue(nextCell->parent()->style().borderBefore(), includeColor ? nextCell->parent()->style().visitedDependentColor(beforeColorProperty) : Color(), BROW));
898 if (!result.exists())
902 // Now check row groups.
903 RenderTableSection* currSection = section();
904 if (rowIndex() + rowSpan() >= currSection->numRows()) {
905 // (5) Our row group's after border.
906 result = chooseBorder(result, CollapsedBorderValue(currSection->style().borderAfter(), includeColor ? currSection->style().visitedDependentColor(afterColorProperty) : Color(), BROWGROUP));
907 if (!result.exists())
910 // (6) Following row group's before border.
911 currSection = table->sectionBelow(currSection, SkipEmptySections);
913 result = chooseBorder(result, CollapsedBorderValue(currSection->style().borderBefore(), includeColor ? currSection->style().visitedDependentColor(beforeColorProperty) : Color(), BROWGROUP));
914 if (!result.exists())
920 // (8) Our column and column group's after borders.
921 RenderTableCol* colElt = table->colElement(col());
923 result = chooseBorder(result, CollapsedBorderValue(colElt->style().borderAfter(), includeColor ? colElt->style().visitedDependentColor(afterColorProperty) : Color(), BCOL));
924 if (!result.exists()) return result;
925 if (RenderTableCol* enclosingColumnGroup = colElt->enclosingColumnGroup()) {
926 result = chooseBorder(result, CollapsedBorderValue(enclosingColumnGroup->style().borderAfter(), includeColor ? enclosingColumnGroup->style().visitedDependentColor(afterColorProperty) : Color(), BCOLGROUP));
927 if (!result.exists())
932 // (9) The table's after border.
933 result = chooseBorder(result, CollapsedBorderValue(table->style().borderAfter(), includeColor ? table->style().visitedDependentColor(afterColorProperty) : Color(), BTABLE));
934 if (!result.exists())
941 inline CollapsedBorderValue RenderTableCell::cachedCollapsedLeftBorder(const RenderStyle& styleForCellFlow) const
943 if (styleForCellFlow.isHorizontalWritingMode())
944 return styleForCellFlow.isLeftToRightDirection() ? section()->cachedCollapsedBorder(*this, CBSStart) : section()->cachedCollapsedBorder(*this, CBSEnd);
945 return styleForCellFlow.isFlippedBlocksWritingMode() ? section()->cachedCollapsedBorder(*this, CBSAfter) : section()->cachedCollapsedBorder(*this, CBSBefore);
948 inline CollapsedBorderValue RenderTableCell::cachedCollapsedRightBorder(const RenderStyle& styleForCellFlow) const
950 if (styleForCellFlow.isHorizontalWritingMode())
951 return styleForCellFlow.isLeftToRightDirection() ? section()->cachedCollapsedBorder(*this, CBSEnd) : section()->cachedCollapsedBorder(*this, CBSStart);
952 return styleForCellFlow.isFlippedBlocksWritingMode() ? section()->cachedCollapsedBorder(*this, CBSBefore) : section()->cachedCollapsedBorder(*this, CBSAfter);
955 inline CollapsedBorderValue RenderTableCell::cachedCollapsedTopBorder(const RenderStyle& styleForCellFlow) const
957 if (styleForCellFlow.isHorizontalWritingMode())
958 return styleForCellFlow.isFlippedBlocksWritingMode() ? section()->cachedCollapsedBorder(*this, CBSAfter) : section()->cachedCollapsedBorder(*this, CBSBefore);
959 return styleForCellFlow.isLeftToRightDirection() ? section()->cachedCollapsedBorder(*this, CBSStart) : section()->cachedCollapsedBorder(*this, CBSEnd);
962 inline CollapsedBorderValue RenderTableCell::cachedCollapsedBottomBorder(const RenderStyle& styleForCellFlow) const
964 if (styleForCellFlow.isHorizontalWritingMode())
965 return styleForCellFlow.isFlippedBlocksWritingMode() ? section()->cachedCollapsedBorder(*this, CBSBefore) : section()->cachedCollapsedBorder(*this, CBSAfter);
966 return styleForCellFlow.isLeftToRightDirection() ? section()->cachedCollapsedBorder(*this, CBSEnd) : section()->cachedCollapsedBorder(*this, CBSStart);
969 LayoutUnit RenderTableCell::borderLeft() const
971 RenderTable* table = this->table();
973 return RenderBlockFlow::borderLeft();
974 return table->collapseBorders() ? borderHalfLeft(false) : RenderBlockFlow::borderLeft();
977 LayoutUnit RenderTableCell::borderRight() const
979 RenderTable* table = this->table();
981 return RenderBlockFlow::borderRight();
982 return table->collapseBorders() ? borderHalfRight(false) : RenderBlockFlow::borderRight();
985 LayoutUnit RenderTableCell::borderTop() const
987 RenderTable* table = this->table();
989 return RenderBlockFlow::borderTop();
990 return table->collapseBorders() ? borderHalfTop(false) : RenderBlockFlow::borderTop();
993 LayoutUnit RenderTableCell::borderBottom() const
995 RenderTable* table = this->table();
997 return RenderBlockFlow::borderBottom();
998 return table->collapseBorders() ? borderHalfBottom(false) : RenderBlockFlow::borderBottom();
1001 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=46191, make the collapsed border drawing
1002 // work with different block flow values instead of being hard-coded to top-to-bottom.
1003 LayoutUnit RenderTableCell::borderStart() const
1005 RenderTable* table = this->table();
1007 return RenderBlockFlow::borderStart();
1008 return table->collapseBorders() ? borderHalfStart(false) : RenderBlockFlow::borderStart();
1011 LayoutUnit RenderTableCell::borderEnd() const
1013 RenderTable* table = this->table();
1015 return RenderBlockFlow::borderEnd();
1016 return table->collapseBorders() ? borderHalfEnd(false) : RenderBlockFlow::borderEnd();
1019 LayoutUnit RenderTableCell::borderBefore() const
1021 RenderTable* table = this->table();
1023 return RenderBlockFlow::borderBefore();
1024 return table->collapseBorders() ? borderHalfBefore(false) : RenderBlockFlow::borderBefore();
1027 LayoutUnit RenderTableCell::borderAfter() const
1029 RenderTable* table = this->table();
1031 return RenderBlockFlow::borderAfter();
1032 return table->collapseBorders() ? borderHalfAfter(false) : RenderBlockFlow::borderAfter();
1035 LayoutUnit RenderTableCell::borderHalfLeft(bool outer) const
1037 const RenderStyle& styleForCellFlow = this->styleForCellFlow();
1038 if (styleForCellFlow.isHorizontalWritingMode())
1039 return styleForCellFlow.isLeftToRightDirection() ? borderHalfStart(outer) : borderHalfEnd(outer);
1040 return styleForCellFlow.isFlippedBlocksWritingMode() ? borderHalfAfter(outer) : borderHalfBefore(outer);
1043 LayoutUnit RenderTableCell::borderHalfRight(bool outer) const
1045 const RenderStyle& styleForCellFlow = this->styleForCellFlow();
1046 if (styleForCellFlow.isHorizontalWritingMode())
1047 return styleForCellFlow.isLeftToRightDirection() ? borderHalfEnd(outer) : borderHalfStart(outer);
1048 return styleForCellFlow.isFlippedBlocksWritingMode() ? borderHalfBefore(outer) : borderHalfAfter(outer);
1051 LayoutUnit RenderTableCell::borderHalfTop(bool outer) const
1053 const RenderStyle& styleForCellFlow = this->styleForCellFlow();
1054 if (styleForCellFlow.isHorizontalWritingMode())
1055 return styleForCellFlow.isFlippedBlocksWritingMode() ? borderHalfAfter(outer) : borderHalfBefore(outer);
1056 return styleForCellFlow.isLeftToRightDirection() ? borderHalfStart(outer) : borderHalfEnd(outer);
1059 LayoutUnit RenderTableCell::borderHalfBottom(bool outer) const
1061 const RenderStyle& styleForCellFlow = this->styleForCellFlow();
1062 if (styleForCellFlow.isHorizontalWritingMode())
1063 return styleForCellFlow.isFlippedBlocksWritingMode() ? borderHalfBefore(outer) : borderHalfAfter(outer);
1064 return styleForCellFlow.isLeftToRightDirection() ? borderHalfEnd(outer) : borderHalfStart(outer);
1067 LayoutUnit RenderTableCell::borderHalfStart(bool outer) const
1069 CollapsedBorderValue border = collapsedStartBorder(DoNotIncludeBorderColor);
1070 if (border.exists())
1071 return CollapsedBorderValue::adjustedCollapsedBorderWidth(border.width(), document().deviceScaleFactor(), styleForCellFlow().isLeftToRightDirection() ^ outer);
1075 LayoutUnit RenderTableCell::borderHalfEnd(bool outer) const
1077 CollapsedBorderValue border = collapsedEndBorder(DoNotIncludeBorderColor);
1078 if (border.exists())
1079 return CollapsedBorderValue::adjustedCollapsedBorderWidth(border.width(), document().deviceScaleFactor(), !(styleForCellFlow().isLeftToRightDirection() ^ outer));
1083 LayoutUnit RenderTableCell::borderHalfBefore(bool outer) const
1085 CollapsedBorderValue border = collapsedBeforeBorder(DoNotIncludeBorderColor);
1086 if (border.exists())
1087 return CollapsedBorderValue::adjustedCollapsedBorderWidth(border.width(), document().deviceScaleFactor(), !(styleForCellFlow().isFlippedBlocksWritingMode() ^ outer));
1091 LayoutUnit RenderTableCell::borderHalfAfter(bool outer) const
1093 CollapsedBorderValue border = collapsedAfterBorder(DoNotIncludeBorderColor);
1094 if (border.exists())
1095 return CollapsedBorderValue::adjustedCollapsedBorderWidth(border.width(), document().deviceScaleFactor(), styleForCellFlow().isFlippedBlocksWritingMode() ^ outer);
1099 void RenderTableCell::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
1101 ASSERT(paintInfo.phase != PaintPhaseCollapsedTableBorders);
1102 RenderBlockFlow::paint(paintInfo, paintOffset);
1105 struct CollapsedBorder {
1106 CollapsedBorderValue borderValue;
1116 class CollapsedBorders {
1123 void addBorder(const CollapsedBorderValue& borderValue, BoxSide borderSide, bool shouldPaint,
1124 LayoutUnit x1, LayoutUnit y1, LayoutUnit x2, LayoutUnit y2, EBorderStyle borderStyle)
1126 if (borderValue.exists() && shouldPaint) {
1127 m_borders[m_count].borderValue = borderValue;
1128 m_borders[m_count].side = borderSide;
1129 m_borders[m_count].shouldPaint = shouldPaint;
1130 m_borders[m_count].x1 = x1;
1131 m_borders[m_count].x2 = x2;
1132 m_borders[m_count].y1 = y1;
1133 m_borders[m_count].y2 = y2;
1134 m_borders[m_count].style = borderStyle;
1139 CollapsedBorder* nextBorder()
1141 for (unsigned i = 0; i < m_count; i++) {
1142 if (m_borders[i].borderValue.exists() && m_borders[i].shouldPaint) {
1143 m_borders[i].shouldPaint = false;
1144 return &m_borders[i];
1151 CollapsedBorder m_borders[4];
1155 static void addBorderStyle(RenderTable::CollapsedBorderValues& borderValues,
1156 CollapsedBorderValue borderValue)
1158 if (!borderValue.exists())
1160 size_t count = borderValues.size();
1161 for (size_t i = 0; i < count; ++i)
1162 if (borderValues[i].isSameIgnoringColor(borderValue))
1164 borderValues.append(borderValue);
1167 void RenderTableCell::collectBorderValues(RenderTable::CollapsedBorderValues& borderValues) const
1169 addBorderStyle(borderValues, collapsedStartBorder());
1170 addBorderStyle(borderValues, collapsedEndBorder());
1171 addBorderStyle(borderValues, collapsedBeforeBorder());
1172 addBorderStyle(borderValues, collapsedAfterBorder());
1175 static int compareBorderValuesForQSort(const void* pa, const void* pb)
1177 const CollapsedBorderValue* a = static_cast<const CollapsedBorderValue*>(pa);
1178 const CollapsedBorderValue* b = static_cast<const CollapsedBorderValue*>(pb);
1179 if (a->isSameIgnoringColor(*b))
1181 return compareBorders(*a, *b);
1184 void RenderTableCell::sortBorderValues(RenderTable::CollapsedBorderValues& borderValues)
1186 qsort(borderValues.data(), borderValues.size(), sizeof(CollapsedBorderValue),
1187 compareBorderValuesForQSort);
1190 void RenderTableCell::paintCollapsedBorders(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
1192 ASSERT(paintInfo.phase == PaintPhaseCollapsedTableBorders);
1194 if (!paintInfo.shouldPaintWithinRoot(*this) || style().visibility() != VISIBLE)
1197 LayoutRect localRepaintRect = paintInfo.rect;
1198 LayoutRect paintRect = LayoutRect(paintOffset + location(), frameRect().size());
1199 if (paintRect.y() - table()->outerBorderTop() >= localRepaintRect.maxY())
1202 if (paintRect.maxY() + table()->outerBorderBottom() <= localRepaintRect.y())
1205 GraphicsContext& graphicsContext = paintInfo.context();
1206 if (!table()->currentBorderValue() || graphicsContext.paintingDisabled())
1209 const RenderStyle& styleForCellFlow = this->styleForCellFlow();
1210 CollapsedBorderValue leftVal = cachedCollapsedLeftBorder(styleForCellFlow);
1211 CollapsedBorderValue rightVal = cachedCollapsedRightBorder(styleForCellFlow);
1212 CollapsedBorderValue topVal = cachedCollapsedTopBorder(styleForCellFlow);
1213 CollapsedBorderValue bottomVal = cachedCollapsedBottomBorder(styleForCellFlow);
1215 // Adjust our x/y/width/height so that we paint the collapsed borders at the correct location.
1216 LayoutUnit topWidth = topVal.width();
1217 LayoutUnit bottomWidth = bottomVal.width();
1218 LayoutUnit leftWidth = leftVal.width();
1219 LayoutUnit rightWidth = rightVal.width();
1221 float deviceScaleFactor = document().deviceScaleFactor();
1222 LayoutUnit leftHalfCollapsedBorder = CollapsedBorderValue::adjustedCollapsedBorderWidth(leftWidth , deviceScaleFactor, false);
1223 LayoutUnit topHalfCollapsedBorder = CollapsedBorderValue::adjustedCollapsedBorderWidth(topWidth, deviceScaleFactor, false);
1224 LayoutUnit righHalftCollapsedBorder = CollapsedBorderValue::adjustedCollapsedBorderWidth(rightWidth, deviceScaleFactor, true);
1225 LayoutUnit bottomHalfCollapsedBorder = CollapsedBorderValue::adjustedCollapsedBorderWidth(bottomWidth, deviceScaleFactor, true);
1227 LayoutRect borderRect = LayoutRect(paintRect.x() - leftHalfCollapsedBorder,
1228 paintRect.y() - topHalfCollapsedBorder,
1229 paintRect.width() + leftHalfCollapsedBorder + righHalftCollapsedBorder,
1230 paintRect.height() + topHalfCollapsedBorder + bottomHalfCollapsedBorder);
1232 EBorderStyle topStyle = collapsedBorderStyle(topVal.style());
1233 EBorderStyle bottomStyle = collapsedBorderStyle(bottomVal.style());
1234 EBorderStyle leftStyle = collapsedBorderStyle(leftVal.style());
1235 EBorderStyle rightStyle = collapsedBorderStyle(rightVal.style());
1237 bool renderTop = topStyle > BHIDDEN && !topVal.isTransparent() && floorToDevicePixel(topWidth, deviceScaleFactor);
1238 bool renderBottom = bottomStyle > BHIDDEN && !bottomVal.isTransparent() && floorToDevicePixel(bottomWidth, deviceScaleFactor);
1239 bool renderLeft = leftStyle > BHIDDEN && !leftVal.isTransparent() && floorToDevicePixel(leftWidth, deviceScaleFactor);
1240 bool renderRight = rightStyle > BHIDDEN && !rightVal.isTransparent() && floorToDevicePixel(rightWidth, deviceScaleFactor);
1242 // We never paint diagonals at the joins. We simply let the border with the highest
1243 // precedence paint on top of borders with lower precedence.
1244 CollapsedBorders borders;
1245 borders.addBorder(topVal, BSTop, renderTop, borderRect.x(), borderRect.y(), borderRect.maxX(), borderRect.y() + topWidth, topStyle);
1246 borders.addBorder(bottomVal, BSBottom, renderBottom, borderRect.x(), borderRect.maxY() - bottomWidth, borderRect.maxX(), borderRect.maxY(), bottomStyle);
1247 borders.addBorder(leftVal, BSLeft, renderLeft, borderRect.x(), borderRect.y(), borderRect.x() + leftWidth, borderRect.maxY(), leftStyle);
1248 borders.addBorder(rightVal, BSRight, renderRight, borderRect.maxX() - rightWidth, borderRect.y(), borderRect.maxX(), borderRect.maxY(), rightStyle);
1250 bool antialias = shouldAntialiasLines(graphicsContext);
1252 for (CollapsedBorder* border = borders.nextBorder(); border; border = borders.nextBorder()) {
1253 if (border->borderValue.isSameIgnoringColor(*table()->currentBorderValue()))
1254 drawLineForBoxSide(graphicsContext, LayoutRect(LayoutPoint(border->x1, border->y1), LayoutPoint(border->x2, border->y2)), border->side,
1255 border->borderValue.color(), border->style, 0, 0, antialias);
1259 void RenderTableCell::paintBackgroundsBehindCell(PaintInfo& paintInfo, const LayoutPoint& paintOffset, RenderElement* backgroundObject)
1261 if (!paintInfo.shouldPaintWithinRoot(*this))
1264 if (!backgroundObject)
1267 if (style().visibility() != VISIBLE)
1270 RenderTable* tableElt = table();
1271 if (!tableElt->collapseBorders() && style().emptyCells() == HIDE && !firstChild())
1274 LayoutPoint adjustedPaintOffset = paintOffset;
1275 if (backgroundObject != this)
1276 adjustedPaintOffset.moveBy(location());
1278 Color c = backgroundObject->style().visitedDependentColor(CSSPropertyBackgroundColor);
1279 const FillLayer* bgLayer = backgroundObject->style().backgroundLayers();
1281 if (bgLayer->hasImage() || c.isValid()) {
1282 // We have to clip here because the background would paint
1283 // on top of the borders otherwise. This only matters for cells and rows.
1284 bool shouldClip = backgroundObject->hasLayer() && (backgroundObject == this || backgroundObject == parent()) && tableElt->collapseBorders();
1285 GraphicsContextStateSaver stateSaver(paintInfo.context(), shouldClip);
1287 LayoutRect clipRect(adjustedPaintOffset.x() + borderLeft(), adjustedPaintOffset.y() + borderTop(),
1288 width() - borderLeft() - borderRight(), height() - borderTop() - borderBottom());
1289 paintInfo.context().clip(clipRect);
1291 paintFillLayers(paintInfo, c, bgLayer, LayoutRect(adjustedPaintOffset, frameRect().size()), BackgroundBleedNone, CompositeSourceOver, backgroundObject);
1295 void RenderTableCell::paintBoxDecorations(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
1297 if (!paintInfo.shouldPaintWithinRoot(*this))
1300 RenderTable* table = this->table();
1301 if (!table->collapseBorders() && style().emptyCells() == HIDE && !firstChild())
1304 LayoutRect paintRect = LayoutRect(paintOffset, frameRect().size());
1305 paintBoxShadow(paintInfo, paintRect, style(), Normal);
1307 // Paint our cell background.
1308 paintBackgroundsBehindCell(paintInfo, paintOffset, this);
1310 paintBoxShadow(paintInfo, paintRect, style(), Inset);
1312 if (!style().hasBorder() || table->collapseBorders())
1315 paintBorder(paintInfo, paintRect, style());
1318 void RenderTableCell::paintMask(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
1320 if (style().visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
1323 RenderTable* tableElt = table();
1324 if (!tableElt->collapseBorders() && style().emptyCells() == HIDE && !firstChild())
1327 paintMaskImages(paintInfo, LayoutRect(paintOffset, frameRect().size()));
1330 bool RenderTableCell::boxShadowShouldBeAppliedToBackground(const LayoutPoint&, BackgroundBleedAvoidance, InlineFlowBox*) const
1335 void RenderTableCell::scrollbarsChanged(bool horizontalScrollbarChanged, bool verticalScrollbarChanged)
1337 LayoutUnit scrollbarHeight = scrollbarLogicalHeight();
1338 if (!scrollbarHeight)
1339 return; // Not sure if we should be doing something when a scrollbar goes away or not.
1341 // We only care if the scrollbar that affects our intrinsic padding has been added.
1342 if ((isHorizontalWritingMode() && !horizontalScrollbarChanged) ||
1343 (!isHorizontalWritingMode() && !verticalScrollbarChanged))
1346 // Shrink our intrinsic padding as much as possible to accommodate the scrollbar.
1347 if (style().verticalAlign() == MIDDLE) {
1348 LayoutUnit totalHeight = logicalHeight();
1349 LayoutUnit heightWithoutIntrinsicPadding = totalHeight - intrinsicPaddingBefore() - intrinsicPaddingAfter();
1350 totalHeight -= scrollbarHeight;
1351 LayoutUnit newBeforePadding = (totalHeight - heightWithoutIntrinsicPadding) / 2;
1352 LayoutUnit newAfterPadding = totalHeight - heightWithoutIntrinsicPadding - newBeforePadding;
1353 setIntrinsicPaddingBefore(newBeforePadding);
1354 setIntrinsicPaddingAfter(newAfterPadding);
1356 setIntrinsicPaddingAfter(intrinsicPaddingAfter() - scrollbarHeight);
1359 RenderTableCell* RenderTableCell::createAnonymousWithParentRenderer(const RenderObject* parent)
1361 auto cell = new RenderTableCell(parent->document(), RenderStyle::createAnonymousStyleWithDisplay(parent->style(), TABLE_CELL));
1362 cell->initializeStyle();
1366 } // namespace WebCore