• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 1997 Martin Jones (mjones@kde.org)
3  *           (C) 1997 Torben Weis (weis@kde.org)
4  *           (C) 1998 Waldo Bastian (bastian@kde.org)
5  *           (C) 1999 Lars Knoll (knoll@kde.org)
6  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
7  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 #include "config.h"
26 #include "core/rendering/RenderTableCell.h"
27 
28 #include "core/HTMLNames.h"
29 #include "core/css/StylePropertySet.h"
30 #include "core/html/HTMLTableCellElement.h"
31 #include "core/rendering/PaintInfo.h"
32 #include "core/rendering/RenderTableCol.h"
33 #include "core/rendering/RenderView.h"
34 #include "core/rendering/SubtreeLayoutScope.h"
35 #include "core/rendering/style/CollapsedBorderValue.h"
36 #include "platform/geometry/FloatQuad.h"
37 #include "platform/geometry/TransformState.h"
38 #include "platform/graphics/GraphicsContextStateSaver.h"
39 
40 using namespace std;
41 
42 namespace WebCore {
43 
44 using namespace HTMLNames;
45 
46 struct SameSizeAsRenderTableCell : public RenderBlockFlow {
47     unsigned bitfields;
48     int paddings[2];
49 };
50 
51 COMPILE_ASSERT(sizeof(RenderTableCell) == sizeof(SameSizeAsRenderTableCell), RenderTableCell_should_stay_small);
52 COMPILE_ASSERT(sizeof(CollapsedBorderValue) == 8, CollapsedBorderValue_should_stay_small);
53 
RenderTableCell(Element * element)54 RenderTableCell::RenderTableCell(Element* element)
55     : RenderBlockFlow(element)
56     , m_column(unsetColumnIndex)
57     , m_cellWidthChanged(false)
58     , m_intrinsicPaddingBefore(0)
59     , m_intrinsicPaddingAfter(0)
60 {
61     // We only update the flags when notified of DOM changes in colSpanOrRowSpanChanged()
62     // so we need to set their initial values here in case something asks for colSpan()/rowSpan() before then.
63     updateColAndRowSpanFlags();
64 }
65 
willBeRemovedFromTree()66 void RenderTableCell::willBeRemovedFromTree()
67 {
68     RenderBlockFlow::willBeRemovedFromTree();
69 
70     section()->setNeedsCellRecalc();
71     section()->removeCachedCollapsedBorders(this);
72 }
73 
parseColSpanFromDOM() const74 unsigned RenderTableCell::parseColSpanFromDOM() const
75 {
76     ASSERT(node());
77     if (isHTMLTableCellElement(*node()))
78         return min<unsigned>(toHTMLTableCellElement(*node()).colSpan(), maxColumnIndex);
79     return 1;
80 }
81 
parseRowSpanFromDOM() const82 unsigned RenderTableCell::parseRowSpanFromDOM() const
83 {
84     ASSERT(node());
85     if (isHTMLTableCellElement(*node()))
86         return min<unsigned>(toHTMLTableCellElement(*node()).rowSpan(), maxRowIndex);
87     return 1;
88 }
89 
updateColAndRowSpanFlags()90 void RenderTableCell::updateColAndRowSpanFlags()
91 {
92     // The vast majority of table cells do not have a colspan or rowspan,
93     // so we keep a bool to know if we need to bother reading from the DOM.
94     m_hasColSpan = node() && parseColSpanFromDOM() != 1;
95     m_hasRowSpan = node() && parseRowSpanFromDOM() != 1;
96 }
97 
colSpanOrRowSpanChanged()98 void RenderTableCell::colSpanOrRowSpanChanged()
99 {
100     ASSERT(node());
101     ASSERT(isHTMLTableCellElement(*node()));
102 
103     updateColAndRowSpanFlags();
104 
105     // FIXME: I suspect that we could return early here if !m_hasColSpan && !m_hasRowSpan.
106 
107     setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
108     if (parent() && section())
109         section()->setNeedsCellRecalc();
110 }
111 
logicalWidthFromColumns(RenderTableCol * firstColForThisCell,Length widthFromStyle) const112 Length RenderTableCell::logicalWidthFromColumns(RenderTableCol* firstColForThisCell, Length widthFromStyle) const
113 {
114     ASSERT(firstColForThisCell && firstColForThisCell == table()->colElement(col()));
115     RenderTableCol* tableCol = firstColForThisCell;
116 
117     unsigned colSpanCount = colSpan();
118     int colWidthSum = 0;
119     for (unsigned i = 1; i <= colSpanCount; i++) {
120         Length colWidth = tableCol->style()->logicalWidth();
121 
122         // Percentage value should be returned only for colSpan == 1.
123         // Otherwise we return original width for the cell.
124         if (!colWidth.isFixed()) {
125             if (colSpanCount > 1)
126                 return widthFromStyle;
127             return colWidth;
128         }
129 
130         colWidthSum += colWidth.value();
131         tableCol = tableCol->nextColumn();
132         // If no next <col> tag found for the span we just return what we have for now.
133         if (!tableCol)
134             break;
135     }
136 
137     // Column widths specified on <col> apply to the border box of the cell, see bug 8126.
138     // FIXME: Why is border/padding ignored in the negative width case?
139     if (colWidthSum > 0)
140         return Length(max(0, colWidthSum - borderAndPaddingLogicalWidth().ceil()), Fixed);
141     return Length(colWidthSum, Fixed);
142 }
143 
computePreferredLogicalWidths()144 void RenderTableCell::computePreferredLogicalWidths()
145 {
146     // 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
147     // cells are added, but relayout can cause the cells to be freed, leaving stale pointers in the sections'
148     // grids.  We must refresh those grids before the child cells try to use them.
149     table()->recalcSectionsIfNeeded();
150 
151     RenderBlockFlow::computePreferredLogicalWidths();
152     if (node() && style()->autoWrap()) {
153         // See if nowrap was set.
154         Length w = styleOrColLogicalWidth();
155         const AtomicString& nowrap = toElement(node())->getAttribute(nowrapAttr);
156         if (!nowrap.isNull() && w.isFixed())
157             // Nowrap is set, but we didn't actually use it because of the
158             // fixed width set on the cell.  Even so, it is a WinIE/Moz trait
159             // to make the minwidth of the cell into the fixed width.  They do this
160             // even in strict mode, so do not make this a quirk.  Affected the top
161             // of hiptop.com.
162             m_minPreferredLogicalWidth = max<LayoutUnit>(w.value(), m_minPreferredLogicalWidth);
163     }
164 }
165 
addLayerHitTestRects(LayerHitTestRects & layerRects,const RenderLayer * currentLayer,const LayoutPoint & layerOffset,const LayoutRect & containerRect) const166 void RenderTableCell::addLayerHitTestRects(LayerHitTestRects& layerRects, const RenderLayer* currentLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const
167 {
168     LayoutPoint adjustedLayerOffset = layerOffset;
169     // RenderTableCell's location includes the offset of it's containing RenderTableRow, so
170     // we need to subtract that again here (as for RenderTableCell::offsetFromContainer.
171     if (parent())
172         adjustedLayerOffset -= parentBox()->locationOffset();
173     RenderBox::addLayerHitTestRects(layerRects, currentLayer, adjustedLayerOffset, containerRect);
174 }
175 
computeIntrinsicPadding(int rowHeight,SubtreeLayoutScope & layouter)176 void RenderTableCell::computeIntrinsicPadding(int rowHeight, SubtreeLayoutScope& layouter)
177 {
178     int oldIntrinsicPaddingBefore = intrinsicPaddingBefore();
179     int oldIntrinsicPaddingAfter = intrinsicPaddingAfter();
180     int logicalHeightWithoutIntrinsicPadding = pixelSnappedLogicalHeight() - oldIntrinsicPaddingBefore - oldIntrinsicPaddingAfter;
181 
182     int intrinsicPaddingBefore = 0;
183     switch (style()->verticalAlign()) {
184     case SUB:
185     case SUPER:
186     case TEXT_TOP:
187     case TEXT_BOTTOM:
188     case LENGTH:
189     case BASELINE: {
190         LayoutUnit baseline = cellBaselinePosition();
191         if (baseline > borderBefore() + paddingBefore())
192             intrinsicPaddingBefore = section()->rowBaseline(rowIndex()) - (baseline - oldIntrinsicPaddingBefore);
193         break;
194     }
195     case TOP:
196         break;
197     case MIDDLE:
198         intrinsicPaddingBefore = (rowHeight - logicalHeightWithoutIntrinsicPadding) / 2;
199         break;
200     case BOTTOM:
201         intrinsicPaddingBefore = rowHeight - logicalHeightWithoutIntrinsicPadding;
202         break;
203     case BASELINE_MIDDLE:
204         break;
205     }
206 
207     int intrinsicPaddingAfter = rowHeight - logicalHeightWithoutIntrinsicPadding - intrinsicPaddingBefore;
208     setIntrinsicPaddingBefore(intrinsicPaddingBefore);
209     setIntrinsicPaddingAfter(intrinsicPaddingAfter);
210 
211     // FIXME: Changing an intrinsic padding shouldn't trigger a relayout as it only shifts the cell inside the row but
212     // doesn't change the logical height.
213     if (intrinsicPaddingBefore != oldIntrinsicPaddingBefore || intrinsicPaddingAfter != oldIntrinsicPaddingAfter)
214         layouter.setNeedsLayout(this);
215 }
216 
updateLogicalWidth()217 void RenderTableCell::updateLogicalWidth()
218 {
219 }
220 
setCellLogicalWidth(int tableLayoutLogicalWidth,SubtreeLayoutScope & layouter)221 void RenderTableCell::setCellLogicalWidth(int tableLayoutLogicalWidth, SubtreeLayoutScope& layouter)
222 {
223     if (tableLayoutLogicalWidth == logicalWidth())
224         return;
225 
226     layouter.setNeedsLayout(this);
227 
228     if (!table()->selfNeedsLayout() && checkForPaintInvalidationDuringLayout())
229         paintInvalidationForWholeRenderer();
230 
231     setLogicalWidth(tableLayoutLogicalWidth);
232     setCellWidthChanged(true);
233 }
234 
layout()235 void RenderTableCell::layout()
236 {
237     ASSERT(needsLayout());
238 
239     updateFirstLetter();
240 
241     int oldCellBaseline = cellBaselinePosition();
242     layoutBlock(cellWidthChanged());
243 
244     // 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
245     // 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
246     // 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
247     // table and row layout can use the correct baseline and height for this cell.
248     if (isBaselineAligned() && section()->rowBaseline(rowIndex()) && cellBaselinePosition() > section()->rowBaseline(rowIndex())) {
249         int newIntrinsicPaddingBefore = max<LayoutUnit>(0, intrinsicPaddingBefore() - max<LayoutUnit>(0, cellBaselinePosition() - oldCellBaseline));
250         setIntrinsicPaddingBefore(newIntrinsicPaddingBefore);
251         SubtreeLayoutScope layouter(*this);
252         layouter.setNeedsLayout(this);
253         layoutBlock(cellWidthChanged());
254     }
255 
256     // FIXME: This value isn't the intrinsic content logical height, but we need
257     // to update the value as its used by flexbox layout. crbug.com/367324
258     updateIntrinsicContentLogicalHeight(contentLogicalHeight());
259 
260     setCellWidthChanged(false);
261 }
262 
paddingTop() const263 LayoutUnit RenderTableCell::paddingTop() const
264 {
265     int result = computedCSSPaddingTop();
266     if (!isHorizontalWritingMode())
267         return result;
268     return result + (style()->writingMode() == TopToBottomWritingMode ? intrinsicPaddingBefore() : intrinsicPaddingAfter());
269 }
270 
paddingBottom() const271 LayoutUnit RenderTableCell::paddingBottom() const
272 {
273     int result = computedCSSPaddingBottom();
274     if (!isHorizontalWritingMode())
275         return result;
276     return result + (style()->writingMode() == TopToBottomWritingMode ? intrinsicPaddingAfter() : intrinsicPaddingBefore());
277 }
278 
paddingLeft() const279 LayoutUnit RenderTableCell::paddingLeft() const
280 {
281     int result = computedCSSPaddingLeft();
282     if (isHorizontalWritingMode())
283         return result;
284     return result + (style()->writingMode() == LeftToRightWritingMode ? intrinsicPaddingBefore() : intrinsicPaddingAfter());
285 }
286 
paddingRight() const287 LayoutUnit RenderTableCell::paddingRight() const
288 {
289     int result = computedCSSPaddingRight();
290     if (isHorizontalWritingMode())
291         return result;
292     return result + (style()->writingMode() == LeftToRightWritingMode ? intrinsicPaddingAfter() : intrinsicPaddingBefore());
293 }
294 
paddingBefore() const295 LayoutUnit RenderTableCell::paddingBefore() const
296 {
297     return static_cast<int>(computedCSSPaddingBefore()) + intrinsicPaddingBefore();
298 }
299 
paddingAfter() const300 LayoutUnit RenderTableCell::paddingAfter() const
301 {
302     return static_cast<int>(computedCSSPaddingAfter()) + intrinsicPaddingAfter();
303 }
304 
setOverrideLogicalContentHeightFromRowHeight(LayoutUnit rowHeight)305 void RenderTableCell::setOverrideLogicalContentHeightFromRowHeight(LayoutUnit rowHeight)
306 {
307     clearIntrinsicPadding();
308     setOverrideLogicalContentHeight(max<LayoutUnit>(0, rowHeight - borderAndPaddingLogicalHeight()));
309 }
310 
offsetFromContainer(const RenderObject * o,const LayoutPoint & point,bool * offsetDependsOnPoint) const311 LayoutSize RenderTableCell::offsetFromContainer(const RenderObject* o, const LayoutPoint& point, bool* offsetDependsOnPoint) const
312 {
313     ASSERT(o == container());
314 
315     LayoutSize offset = RenderBlockFlow::offsetFromContainer(o, point, offsetDependsOnPoint);
316     if (parent())
317         offset -= parentBox()->locationOffset();
318 
319     return offset;
320 }
321 
clippedOverflowRectForPaintInvalidation(const RenderLayerModelObject * paintInvalidationContainer) const322 LayoutRect RenderTableCell::clippedOverflowRectForPaintInvalidation(const RenderLayerModelObject* paintInvalidationContainer) const
323 {
324     // If the table grid is dirty, we cannot get reliable information about adjoining cells,
325     // so we ignore outside borders. This should not be a problem because it means that
326     // the table is going to recalculate the grid, relayout and repaint its current rect, which
327     // includes any outside borders of this cell.
328     if (!table()->collapseBorders() || table()->needsSectionRecalc())
329         return RenderBlockFlow::clippedOverflowRectForPaintInvalidation(paintInvalidationContainer);
330 
331     bool rtl = !styleForCellFlow()->isLeftToRightDirection();
332     int outlineSize = style()->outlineSize();
333     int left = max(borderHalfLeft(true), outlineSize);
334     int right = max(borderHalfRight(true), outlineSize);
335     int top = max(borderHalfTop(true), outlineSize);
336     int bottom = max(borderHalfBottom(true), outlineSize);
337     if ((left && !rtl) || (right && rtl)) {
338         if (RenderTableCell* before = table()->cellBefore(this)) {
339             top = max(top, before->borderHalfTop(true));
340             bottom = max(bottom, before->borderHalfBottom(true));
341         }
342     }
343     if ((left && rtl) || (right && !rtl)) {
344         if (RenderTableCell* after = table()->cellAfter(this)) {
345             top = max(top, after->borderHalfTop(true));
346             bottom = max(bottom, after->borderHalfBottom(true));
347         }
348     }
349     if (top) {
350         if (RenderTableCell* above = table()->cellAbove(this)) {
351             left = max(left, above->borderHalfLeft(true));
352             right = max(right, above->borderHalfRight(true));
353         }
354     }
355     if (bottom) {
356         if (RenderTableCell* below = table()->cellBelow(this)) {
357             left = max(left, below->borderHalfLeft(true));
358             right = max(right, below->borderHalfRight(true));
359         }
360     }
361     LayoutPoint location(max<LayoutUnit>(left, -visualOverflowRect().x()), max<LayoutUnit>(top, -visualOverflowRect().y()));
362     LayoutRect r(-location.x(), -location.y(), location.x() + max(width() + right, visualOverflowRect().maxX()), location.y() + max(height() + bottom, visualOverflowRect().maxY()));
363 
364     if (!RuntimeEnabledFeatures::repaintAfterLayoutEnabled()) {
365         if (RenderView* v = view()) {
366             // FIXME: layoutDelta needs to be applied in parts before/after transforms and
367             // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308
368             r.move(v->layoutDelta());
369         }
370     }
371     mapRectToPaintInvalidationBacking(paintInvalidationContainer, r);
372     return r;
373 }
374 
mapRectToPaintInvalidationBacking(const RenderLayerModelObject * paintInvalidationContainer,LayoutRect & r,bool fixed) const375 void RenderTableCell::mapRectToPaintInvalidationBacking(const RenderLayerModelObject* paintInvalidationContainer, LayoutRect& r, bool fixed) const
376 {
377     if (paintInvalidationContainer == this)
378         return;
379     r.setY(r.y());
380     RenderView* v = view();
381     if ((!v || !v->canMapUsingLayoutStateForContainer(paintInvalidationContainer)) && parent())
382         r.moveBy(-parentBox()->location()); // Rows are in the same coordinate space, so don't add their offset in.
383     RenderBlockFlow::mapRectToPaintInvalidationBacking(paintInvalidationContainer, r, fixed);
384 }
385 
cellBaselinePosition() const386 LayoutUnit RenderTableCell::cellBaselinePosition() const
387 {
388     // <http://www.w3.org/TR/2007/CR-CSS21-20070719/tables.html#height-layout>: The baseline of a cell is the baseline of
389     // the first in-flow line box in the cell, or the first in-flow table-row in the cell, whichever comes first. If there
390     // is no such line box or table-row, the baseline is the bottom of content edge of the cell box.
391     LayoutUnit firstLineBaseline = firstLineBoxBaseline();
392     if (firstLineBaseline != -1)
393         return firstLineBaseline;
394     return paddingBefore() + borderBefore() + contentLogicalHeight();
395 }
396 
styleDidChange(StyleDifference diff,const RenderStyle * oldStyle)397 void RenderTableCell::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
398 {
399     ASSERT(style()->display() == TABLE_CELL);
400     ASSERT(!row() || row()->rowIndexWasSet());
401 
402     RenderBlockFlow::styleDidChange(diff, oldStyle);
403     setHasBoxDecorations(true);
404 
405     if (parent() && section() && oldStyle && style()->height() != oldStyle->height())
406         section()->rowLogicalHeightChanged(rowIndex());
407 
408     // Our intrinsic padding pushes us down to align with the baseline of other cells on the row. If our vertical-align
409     // has changed then so will the padding needed to align with other cells - clear it so we can recalculate it from scratch.
410     if (oldStyle && style()->verticalAlign() != oldStyle->verticalAlign())
411         clearIntrinsicPadding();
412 
413     // If border was changed, notify table.
414     if (parent()) {
415         RenderTable* table = this->table();
416         if (table && !table->selfNeedsLayout() && !table->normalChildNeedsLayout()&& oldStyle && oldStyle->border() != style()->border())
417             table->invalidateCollapsedBorders();
418     }
419 }
420 
421 // The following rules apply for resolving conflicts and figuring out which border
422 // to use.
423 // (1) Borders with the 'border-style' of 'hidden' take precedence over all other conflicting
424 // borders. Any border with this value suppresses all borders at this location.
425 // (2) Borders with a style of 'none' have the lowest priority. Only if the border properties of all
426 // the elements meeting at this edge are 'none' will the border be omitted (but note that 'none' is
427 // the default value for the border style.)
428 // (3) If none of the styles are 'hidden' and at least one of them is not 'none', then narrow borders
429 // are discarded in favor of wider ones. If several have the same 'border-width' then styles are preferred
430 // in this order: 'double', 'solid', 'dashed', 'dotted', 'ridge', 'outset', 'groove', and the lowest: 'inset'.
431 // (4) If border styles differ only in color, then a style set on a cell wins over one on a row,
432 // which wins over a row group, column, column group and, lastly, table. It is undefined which color
433 // is used when two elements of the same type disagree.
compareBorders(const CollapsedBorderValue & border1,const CollapsedBorderValue & border2)434 static int compareBorders(const CollapsedBorderValue& border1, const CollapsedBorderValue& border2)
435 {
436     // Sanity check the values passed in. The null border have lowest priority.
437     if (!border2.exists()) {
438         if (!border1.exists())
439             return 0;
440         return 1;
441     }
442     if (!border1.exists())
443         return -1;
444 
445     // Rule #1 above.
446     if (border2.style() == BHIDDEN) {
447         if (border1.style() == BHIDDEN)
448             return 0;
449         return -1;
450     }
451     if (border1.style() == BHIDDEN)
452         return 1;
453 
454     // Rule #2 above.  A style of 'none' has lowest priority and always loses to any other border.
455     if (border2.style() == BNONE) {
456         if (border1.style() == BNONE)
457             return 0;
458         return 1;
459     }
460     if (border1.style() == BNONE)
461         return -1;
462 
463     // The first part of rule #3 above. Wider borders win.
464     if (border1.width() != border2.width())
465         return border1.width() < border2.width() ? -1 : 1;
466 
467     // The borders have equal width.  Sort by border style.
468     if (border1.style() != border2.style())
469         return border1.style() < border2.style() ? -1 : 1;
470 
471     // The border have the same width and style.  Rely on precedence (cell over row over row group, etc.)
472     if (border1.precedence() == border2.precedence())
473         return 0;
474     return border1.precedence() < border2.precedence() ? -1 : 1;
475 }
476 
chooseBorder(const CollapsedBorderValue & border1,const CollapsedBorderValue & border2)477 static CollapsedBorderValue chooseBorder(const CollapsedBorderValue& border1, const CollapsedBorderValue& border2)
478 {
479     const CollapsedBorderValue& border = compareBorders(border1, border2) < 0 ? border2 : border1;
480     return border.style() == BHIDDEN ? CollapsedBorderValue() : border;
481 }
482 
hasStartBorderAdjoiningTable() const483 bool RenderTableCell::hasStartBorderAdjoiningTable() const
484 {
485     bool isStartColumn = !col();
486     bool isEndColumn = table()->colToEffCol(col() + colSpan() - 1) == table()->numEffCols() - 1;
487     bool hasSameDirectionAsTable = hasSameDirectionAs(table());
488 
489     // The table direction determines the row direction. In mixed directionality, we cannot guarantee that
490     // we have a common border with the table (think a ltr table with rtl start cell).
491     return (isStartColumn && hasSameDirectionAsTable) || (isEndColumn && !hasSameDirectionAsTable);
492 }
493 
hasEndBorderAdjoiningTable() const494 bool RenderTableCell::hasEndBorderAdjoiningTable() const
495 {
496     bool isStartColumn = !col();
497     bool isEndColumn = table()->colToEffCol(col() + colSpan() - 1) == table()->numEffCols() - 1;
498     bool hasSameDirectionAsTable = hasSameDirectionAs(table());
499 
500     // The table direction determines the row direction. In mixed directionality, we cannot guarantee that
501     // we have a common border with the table (think a ltr table with ltr end cell).
502     return (isStartColumn && !hasSameDirectionAsTable) || (isEndColumn && hasSameDirectionAsTable);
503 }
504 
collapsedStartBorder(IncludeBorderColorOrNot includeColor) const505 CollapsedBorderValue RenderTableCell::collapsedStartBorder(IncludeBorderColorOrNot includeColor) const
506 {
507     CollapsedBorderValue result = computeCollapsedStartBorder(includeColor);
508     if (includeColor)
509         section()->setCachedCollapsedBorder(this, CBSStart, result);
510     return result;
511 }
512 
computeCollapsedStartBorder(IncludeBorderColorOrNot includeColor) const513 CollapsedBorderValue RenderTableCell::computeCollapsedStartBorder(IncludeBorderColorOrNot includeColor) const
514 {
515     RenderTable* table = this->table();
516 
517     // For the start border, we need to check, in order of precedence:
518     // (1) Our start border.
519     int startColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderStartColor, styleForCellFlow()->direction(), styleForCellFlow()->writingMode()) : 0;
520     int endColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderEndColor, styleForCellFlow()->direction(), styleForCellFlow()->writingMode()) : 0;
521     CollapsedBorderValue result(style()->borderStart(), includeColor ? resolveColor(startColorProperty) : Color(), BCELL);
522 
523     // (2) The end border of the preceding cell.
524     RenderTableCell* cellBefore = table->cellBefore(this);
525     if (cellBefore) {
526         CollapsedBorderValue cellBeforeAdjoiningBorder = CollapsedBorderValue(cellBefore->borderAdjoiningCellAfter(this), includeColor ? cellBefore->resolveColor(endColorProperty) : Color(), BCELL);
527         // |result| should be the 2nd argument as |cellBefore| should win in case of equality per CSS 2.1 (Border conflict resolution, point 4).
528         result = chooseBorder(cellBeforeAdjoiningBorder, result);
529         if (!result.exists())
530             return result;
531     }
532 
533     bool startBorderAdjoinsTable = hasStartBorderAdjoiningTable();
534     if (startBorderAdjoinsTable) {
535         // (3) Our row's start border.
536         result = chooseBorder(result, CollapsedBorderValue(row()->borderAdjoiningStartCell(this), includeColor ? parent()->resolveColor(startColorProperty) : Color(), BROW));
537         if (!result.exists())
538             return result;
539 
540         // (4) Our row group's start border.
541         result = chooseBorder(result, CollapsedBorderValue(section()->borderAdjoiningStartCell(this), includeColor ? section()->resolveColor(startColorProperty) : Color(), BROWGROUP));
542         if (!result.exists())
543             return result;
544     }
545 
546     // (5) Our column and column group's start borders.
547     bool startColEdge;
548     bool endColEdge;
549     if (RenderTableCol* colElt = table->colElement(col(), &startColEdge, &endColEdge)) {
550         if (colElt->isTableColumnGroup() && startColEdge) {
551             // The |colElt| is a column group and is also the first colgroup (in case of spanned colgroups).
552             result = chooseBorder(result, CollapsedBorderValue(colElt->borderAdjoiningCellStartBorder(this), includeColor ? colElt->resolveColor(startColorProperty) : Color(), BCOLGROUP));
553             if (!result.exists())
554                 return result;
555         } else if (!colElt->isTableColumnGroup()) {
556             // We first consider the |colElt| and irrespective of whether it is a spanned col or not, we apply
557             // its start border. This is as per HTML5 which states that: "For the purposes of the CSS table model,
558             // the col element is expected to be treated as if it was present as many times as its span attribute specifies".
559             result = chooseBorder(result, CollapsedBorderValue(colElt->borderAdjoiningCellStartBorder(this), includeColor ? colElt->resolveColor(startColorProperty) : Color(), BCOL));
560             if (!result.exists())
561                 return result;
562             // Next, apply the start border of the enclosing colgroup but only if it is adjacent to the cell's edge.
563             if (RenderTableCol* enclosingColumnGroup = colElt->enclosingColumnGroupIfAdjacentBefore()) {
564                 result = chooseBorder(result, CollapsedBorderValue(enclosingColumnGroup->borderAdjoiningCellStartBorder(this), includeColor ? enclosingColumnGroup->resolveColor(startColorProperty) : Color(), BCOLGROUP));
565                 if (!result.exists())
566                     return result;
567             }
568         }
569     }
570 
571     // (6) The end border of the preceding column.
572     if (cellBefore) {
573         if (RenderTableCol* colElt = table->colElement(col() - 1, &startColEdge, &endColEdge)) {
574             if (colElt->isTableColumnGroup() && endColEdge) {
575                 // The element is a colgroup and is also the last colgroup (in case of spanned colgroups).
576                 result = chooseBorder(CollapsedBorderValue(colElt->borderAdjoiningCellAfter(this), includeColor ? colElt->resolveColor(endColorProperty) : Color(), BCOLGROUP), result);
577                 if (!result.exists())
578                     return result;
579             } else if (colElt->isTableColumn()) {
580                 // Resolve the collapsing border against the col's border ignoring any 'span' as per HTML5.
581                 result = chooseBorder(CollapsedBorderValue(colElt->borderAdjoiningCellAfter(this), includeColor ? colElt->resolveColor(endColorProperty) : Color(), BCOL), result);
582                 if (!result.exists())
583                     return result;
584                 // Next, if the previous col has a parent colgroup then its end border should be applied
585                 // but only if it is adjacent to the cell's edge.
586                 if (RenderTableCol* enclosingColumnGroup = colElt->enclosingColumnGroupIfAdjacentAfter()) {
587                     result = chooseBorder(CollapsedBorderValue(enclosingColumnGroup->borderAdjoiningCellEndBorder(this), includeColor ? enclosingColumnGroup->resolveColor(endColorProperty) : Color(), BCOLGROUP), result);
588                     if (!result.exists())
589                         return result;
590                 }
591             }
592         }
593     }
594 
595     if (startBorderAdjoinsTable) {
596         // (7) The table's start border.
597         result = chooseBorder(result, CollapsedBorderValue(table->tableStartBorderAdjoiningCell(this), includeColor ? table->resolveColor(startColorProperty) : Color(), BTABLE));
598         if (!result.exists())
599             return result;
600     }
601 
602     return result;
603 }
604 
collapsedEndBorder(IncludeBorderColorOrNot includeColor) const605 CollapsedBorderValue RenderTableCell::collapsedEndBorder(IncludeBorderColorOrNot includeColor) const
606 {
607     CollapsedBorderValue result = computeCollapsedEndBorder(includeColor);
608     if (includeColor)
609         section()->setCachedCollapsedBorder(this, CBSEnd, result);
610     return result;
611 }
612 
computeCollapsedEndBorder(IncludeBorderColorOrNot includeColor) const613 CollapsedBorderValue RenderTableCell::computeCollapsedEndBorder(IncludeBorderColorOrNot includeColor) const
614 {
615     RenderTable* table = this->table();
616     // Note: We have to use the effective column information instead of whether we have a cell after as a table doesn't
617     // have to be regular (any row can have less cells than the total cell count).
618     bool isEndColumn = table->colToEffCol(col() + colSpan() - 1) == table->numEffCols() - 1;
619 
620     // For end border, we need to check, in order of precedence:
621     // (1) Our end border.
622     int startColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderStartColor, styleForCellFlow()->direction(), styleForCellFlow()->writingMode()) : 0;
623     int endColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderEndColor, styleForCellFlow()->direction(), styleForCellFlow()->writingMode()) : 0;
624     CollapsedBorderValue result = CollapsedBorderValue(style()->borderEnd(), includeColor ? resolveColor(endColorProperty) : Color(), BCELL);
625 
626     // (2) The start border of the following cell.
627     if (!isEndColumn) {
628         if (RenderTableCell* cellAfter = table->cellAfter(this)) {
629             CollapsedBorderValue cellAfterAdjoiningBorder = CollapsedBorderValue(cellAfter->borderAdjoiningCellBefore(this), includeColor ? cellAfter->resolveColor(startColorProperty) : Color(), BCELL);
630             result = chooseBorder(result, cellAfterAdjoiningBorder);
631             if (!result.exists())
632                 return result;
633         }
634     }
635 
636     bool endBorderAdjoinsTable = hasEndBorderAdjoiningTable();
637     if (endBorderAdjoinsTable) {
638         // (3) Our row's end border.
639         result = chooseBorder(result, CollapsedBorderValue(row()->borderAdjoiningEndCell(this), includeColor ? parent()->resolveColor(endColorProperty) : Color(), BROW));
640         if (!result.exists())
641             return result;
642 
643         // (4) Our row group's end border.
644         result = chooseBorder(result, CollapsedBorderValue(section()->borderAdjoiningEndCell(this), includeColor ? section()->resolveColor(endColorProperty) : Color(), BROWGROUP));
645         if (!result.exists())
646             return result;
647     }
648 
649     // (5) Our column and column group's end borders.
650     bool startColEdge;
651     bool endColEdge;
652     if (RenderTableCol* colElt = table->colElement(col() + colSpan() - 1, &startColEdge, &endColEdge)) {
653         if (colElt->isTableColumnGroup() && endColEdge) {
654             // The element is a colgroup and is also the last colgroup (in case of spanned colgroups).
655             result = chooseBorder(result, CollapsedBorderValue(colElt->borderAdjoiningCellEndBorder(this), includeColor ? colElt->resolveColor(endColorProperty) : Color(), BCOLGROUP));
656             if (!result.exists())
657                 return result;
658         } else if (!colElt->isTableColumnGroup()) {
659             // First apply the end border of the column irrespective of whether it is spanned or not. This is as per
660             // HTML5 which states that: "For the purposes of the CSS table model, the col element is expected to be
661             // treated as if it was present as many times as its span attribute specifies".
662             result = chooseBorder(result, CollapsedBorderValue(colElt->borderAdjoiningCellEndBorder(this), includeColor ? colElt->resolveColor(endColorProperty) : Color(), BCOL));
663             if (!result.exists())
664                 return result;
665             // Next, if it has a parent colgroup then we apply its end border but only if it is adjacent to the cell.
666             if (RenderTableCol* enclosingColumnGroup = colElt->enclosingColumnGroupIfAdjacentAfter()) {
667                 result = chooseBorder(result, CollapsedBorderValue(enclosingColumnGroup->borderAdjoiningCellEndBorder(this), includeColor ? enclosingColumnGroup->resolveColor(endColorProperty) : Color(), BCOLGROUP));
668                 if (!result.exists())
669                     return result;
670             }
671         }
672     }
673 
674     // (6) The start border of the next column.
675     if (!isEndColumn) {
676         if (RenderTableCol* colElt = table->colElement(col() + colSpan(), &startColEdge, &endColEdge)) {
677             if (colElt->isTableColumnGroup() && startColEdge) {
678                 // This case is a colgroup without any col, we only compute it if it is adjacent to the cell's edge.
679                 result = chooseBorder(result, CollapsedBorderValue(colElt->borderAdjoiningCellBefore(this), includeColor ? colElt->resolveColor(startColorProperty) : Color(), BCOLGROUP));
680                 if (!result.exists())
681                     return result;
682             } else if (colElt->isTableColumn()) {
683                 // Resolve the collapsing border against the col's border ignoring any 'span' as per HTML5.
684                 result = chooseBorder(result, CollapsedBorderValue(colElt->borderAdjoiningCellBefore(this), includeColor ? colElt->resolveColor(startColorProperty) : Color(), BCOL));
685                 if (!result.exists())
686                     return result;
687                 // If we have a parent colgroup, resolve the border only if it is adjacent to the cell.
688                 if (RenderTableCol* enclosingColumnGroup = colElt->enclosingColumnGroupIfAdjacentBefore()) {
689                     result = chooseBorder(result, CollapsedBorderValue(enclosingColumnGroup->borderAdjoiningCellStartBorder(this), includeColor ? enclosingColumnGroup->resolveColor(startColorProperty) : Color(), BCOLGROUP));
690                     if (!result.exists())
691                         return result;
692                 }
693             }
694         }
695     }
696 
697     if (endBorderAdjoinsTable) {
698         // (7) The table's end border.
699         result = chooseBorder(result, CollapsedBorderValue(table->tableEndBorderAdjoiningCell(this), includeColor ? table->resolveColor(endColorProperty) : Color(), BTABLE));
700         if (!result.exists())
701             return result;
702     }
703 
704     return result;
705 }
706 
collapsedBeforeBorder(IncludeBorderColorOrNot includeColor) const707 CollapsedBorderValue RenderTableCell::collapsedBeforeBorder(IncludeBorderColorOrNot includeColor) const
708 {
709     CollapsedBorderValue result = computeCollapsedBeforeBorder(includeColor);
710     if (includeColor)
711         section()->setCachedCollapsedBorder(this, CBSBefore, result);
712     return result;
713 }
714 
computeCollapsedBeforeBorder(IncludeBorderColorOrNot includeColor) const715 CollapsedBorderValue RenderTableCell::computeCollapsedBeforeBorder(IncludeBorderColorOrNot includeColor) const
716 {
717     RenderTable* table = this->table();
718 
719     // For before border, we need to check, in order of precedence:
720     // (1) Our before border.
721     int beforeColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderBeforeColor, styleForCellFlow()->direction(), styleForCellFlow()->writingMode()) : 0;
722     int afterColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderAfterColor, styleForCellFlow()->direction(), styleForCellFlow()->writingMode()) : 0;
723     CollapsedBorderValue result = CollapsedBorderValue(style()->borderBefore(), includeColor ? resolveColor(beforeColorProperty) : Color(), BCELL);
724 
725     RenderTableCell* prevCell = table->cellAbove(this);
726     if (prevCell) {
727         // (2) A before cell's after border.
728         result = chooseBorder(CollapsedBorderValue(prevCell->style()->borderAfter(), includeColor ? prevCell->resolveColor(afterColorProperty) : Color(), BCELL), result);
729         if (!result.exists())
730             return result;
731     }
732 
733     // (3) Our row's before border.
734     result = chooseBorder(result, CollapsedBorderValue(parent()->style()->borderBefore(), includeColor ? parent()->resolveColor(beforeColorProperty) : Color(), BROW));
735     if (!result.exists())
736         return result;
737 
738     // (4) The previous row's after border.
739     if (prevCell) {
740         RenderObject* prevRow = 0;
741         if (prevCell->section() == section())
742             prevRow = parent()->previousSibling();
743         else
744             prevRow = prevCell->section()->lastRow();
745 
746         if (prevRow) {
747             result = chooseBorder(CollapsedBorderValue(prevRow->style()->borderAfter(), includeColor ? prevRow->resolveColor(afterColorProperty) : Color(), BROW), result);
748             if (!result.exists())
749                 return result;
750         }
751     }
752 
753     // Now check row groups.
754     RenderTableSection* currSection = section();
755     if (!rowIndex()) {
756         // (5) Our row group's before border.
757         result = chooseBorder(result, CollapsedBorderValue(currSection->style()->borderBefore(), includeColor ? currSection->resolveColor(beforeColorProperty) : Color(), BROWGROUP));
758         if (!result.exists())
759             return result;
760 
761         // (6) Previous row group's after border.
762         currSection = table->sectionAbove(currSection, SkipEmptySections);
763         if (currSection) {
764             result = chooseBorder(CollapsedBorderValue(currSection->style()->borderAfter(), includeColor ? currSection->resolveColor(afterColorProperty) : Color(), BROWGROUP), result);
765             if (!result.exists())
766                 return result;
767         }
768     }
769 
770     if (!currSection) {
771         // (8) Our column and column group's before borders.
772         RenderTableCol* colElt = table->colElement(col());
773         if (colElt) {
774             result = chooseBorder(result, CollapsedBorderValue(colElt->style()->borderBefore(), includeColor ? colElt->resolveColor(beforeColorProperty) : Color(), BCOL));
775             if (!result.exists())
776                 return result;
777             if (RenderTableCol* enclosingColumnGroup = colElt->enclosingColumnGroup()) {
778                 result = chooseBorder(result, CollapsedBorderValue(enclosingColumnGroup->style()->borderBefore(), includeColor ? enclosingColumnGroup->resolveColor(beforeColorProperty) : Color(), BCOLGROUP));
779                 if (!result.exists())
780                     return result;
781             }
782         }
783 
784         // (9) The table's before border.
785         result = chooseBorder(result, CollapsedBorderValue(table->style()->borderBefore(), includeColor ? table->resolveColor(beforeColorProperty) : Color(), BTABLE));
786         if (!result.exists())
787             return result;
788     }
789 
790     return result;
791 }
792 
collapsedAfterBorder(IncludeBorderColorOrNot includeColor) const793 CollapsedBorderValue RenderTableCell::collapsedAfterBorder(IncludeBorderColorOrNot includeColor) const
794 {
795     CollapsedBorderValue result = computeCollapsedAfterBorder(includeColor);
796     if (includeColor)
797         section()->setCachedCollapsedBorder(this, CBSAfter, result);
798     return result;
799 }
800 
computeCollapsedAfterBorder(IncludeBorderColorOrNot includeColor) const801 CollapsedBorderValue RenderTableCell::computeCollapsedAfterBorder(IncludeBorderColorOrNot includeColor) const
802 {
803     RenderTable* table = this->table();
804 
805     // For after border, we need to check, in order of precedence:
806     // (1) Our after border.
807     int beforeColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderBeforeColor, styleForCellFlow()->direction(), styleForCellFlow()->writingMode()) : 0;
808     int afterColorProperty = includeColor ? CSSProperty::resolveDirectionAwareProperty(CSSPropertyWebkitBorderAfterColor, styleForCellFlow()->direction(), styleForCellFlow()->writingMode()) : 0;
809     CollapsedBorderValue result = CollapsedBorderValue(style()->borderAfter(), includeColor ? resolveColor(afterColorProperty) : Color(), BCELL);
810 
811     RenderTableCell* nextCell = table->cellBelow(this);
812     if (nextCell) {
813         // (2) An after cell's before border.
814         result = chooseBorder(result, CollapsedBorderValue(nextCell->style()->borderBefore(), includeColor ? nextCell->resolveColor(beforeColorProperty) : Color(), BCELL));
815         if (!result.exists())
816             return result;
817     }
818 
819     // (3) Our row's after border. (FIXME: Deal with rowspan!)
820     result = chooseBorder(result, CollapsedBorderValue(parent()->style()->borderAfter(), includeColor ? parent()->resolveColor(afterColorProperty) : Color(), BROW));
821     if (!result.exists())
822         return result;
823 
824     // (4) The next row's before border.
825     if (nextCell) {
826         result = chooseBorder(result, CollapsedBorderValue(nextCell->parent()->style()->borderBefore(), includeColor ? nextCell->parent()->resolveColor(beforeColorProperty) : Color(), BROW));
827         if (!result.exists())
828             return result;
829     }
830 
831     // Now check row groups.
832     RenderTableSection* currSection = section();
833     if (rowIndex() + rowSpan() >= currSection->numRows()) {
834         // (5) Our row group's after border.
835         result = chooseBorder(result, CollapsedBorderValue(currSection->style()->borderAfter(), includeColor ? currSection->resolveColor(afterColorProperty) : Color(), BROWGROUP));
836         if (!result.exists())
837             return result;
838 
839         // (6) Following row group's before border.
840         currSection = table->sectionBelow(currSection, SkipEmptySections);
841         if (currSection) {
842             result = chooseBorder(result, CollapsedBorderValue(currSection->style()->borderBefore(), includeColor ? currSection->resolveColor(beforeColorProperty) : Color(), BROWGROUP));
843             if (!result.exists())
844                 return result;
845         }
846     }
847 
848     if (!currSection) {
849         // (8) Our column and column group's after borders.
850         RenderTableCol* colElt = table->colElement(col());
851         if (colElt) {
852             result = chooseBorder(result, CollapsedBorderValue(colElt->style()->borderAfter(), includeColor ? colElt->resolveColor(afterColorProperty) : Color(), BCOL));
853             if (!result.exists()) return result;
854             if (RenderTableCol* enclosingColumnGroup = colElt->enclosingColumnGroup()) {
855                 result = chooseBorder(result, CollapsedBorderValue(enclosingColumnGroup->style()->borderAfter(), includeColor ? enclosingColumnGroup->resolveColor(afterColorProperty) : Color(), BCOLGROUP));
856                 if (!result.exists())
857                     return result;
858             }
859         }
860 
861         // (9) The table's after border.
862         result = chooseBorder(result, CollapsedBorderValue(table->style()->borderAfter(), includeColor ? table->resolveColor(afterColorProperty) : Color(), BTABLE));
863         if (!result.exists())
864             return result;
865     }
866 
867     return result;
868 }
869 
cachedCollapsedLeftBorder(const RenderStyle * styleForCellFlow) const870 inline CollapsedBorderValue RenderTableCell::cachedCollapsedLeftBorder(const RenderStyle* styleForCellFlow) const
871 {
872     if (styleForCellFlow->isHorizontalWritingMode())
873         return styleForCellFlow->isLeftToRightDirection() ? section()->cachedCollapsedBorder(this, CBSStart) : section()->cachedCollapsedBorder(this, CBSEnd);
874     return styleForCellFlow->isFlippedBlocksWritingMode() ? section()->cachedCollapsedBorder(this, CBSAfter) : section()->cachedCollapsedBorder(this, CBSBefore);
875 }
876 
cachedCollapsedRightBorder(const RenderStyle * styleForCellFlow) const877 inline CollapsedBorderValue RenderTableCell::cachedCollapsedRightBorder(const RenderStyle* styleForCellFlow) const
878 {
879     if (styleForCellFlow->isHorizontalWritingMode())
880         return styleForCellFlow->isLeftToRightDirection() ? section()->cachedCollapsedBorder(this, CBSEnd) : section()->cachedCollapsedBorder(this, CBSStart);
881     return styleForCellFlow->isFlippedBlocksWritingMode() ? section()->cachedCollapsedBorder(this, CBSBefore) : section()->cachedCollapsedBorder(this, CBSAfter);
882 }
883 
cachedCollapsedTopBorder(const RenderStyle * styleForCellFlow) const884 inline CollapsedBorderValue RenderTableCell::cachedCollapsedTopBorder(const RenderStyle* styleForCellFlow) const
885 {
886     if (styleForCellFlow->isHorizontalWritingMode())
887         return styleForCellFlow->isFlippedBlocksWritingMode() ? section()->cachedCollapsedBorder(this, CBSAfter) : section()->cachedCollapsedBorder(this, CBSBefore);
888     return styleForCellFlow->isLeftToRightDirection() ? section()->cachedCollapsedBorder(this, CBSStart) : section()->cachedCollapsedBorder(this, CBSEnd);
889 }
890 
cachedCollapsedBottomBorder(const RenderStyle * styleForCellFlow) const891 inline CollapsedBorderValue RenderTableCell::cachedCollapsedBottomBorder(const RenderStyle* styleForCellFlow) const
892 {
893     if (styleForCellFlow->isHorizontalWritingMode())
894         return styleForCellFlow->isFlippedBlocksWritingMode() ? section()->cachedCollapsedBorder(this, CBSBefore) : section()->cachedCollapsedBorder(this, CBSAfter);
895     return styleForCellFlow->isLeftToRightDirection() ? section()->cachedCollapsedBorder(this, CBSEnd) : section()->cachedCollapsedBorder(this, CBSStart);
896 }
897 
borderLeft() const898 int RenderTableCell::borderLeft() const
899 {
900     return table()->collapseBorders() ? borderHalfLeft(false) : RenderBlockFlow::borderLeft();
901 }
902 
borderRight() const903 int RenderTableCell::borderRight() const
904 {
905     return table()->collapseBorders() ? borderHalfRight(false) : RenderBlockFlow::borderRight();
906 }
907 
borderTop() const908 int RenderTableCell::borderTop() const
909 {
910     return table()->collapseBorders() ? borderHalfTop(false) : RenderBlockFlow::borderTop();
911 }
912 
borderBottom() const913 int RenderTableCell::borderBottom() const
914 {
915     return table()->collapseBorders() ? borderHalfBottom(false) : RenderBlockFlow::borderBottom();
916 }
917 
918 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=46191, make the collapsed border drawing
919 // work with different block flow values instead of being hard-coded to top-to-bottom.
borderStart() const920 int RenderTableCell::borderStart() const
921 {
922     return table()->collapseBorders() ? borderHalfStart(false) : RenderBlockFlow::borderStart();
923 }
924 
borderEnd() const925 int RenderTableCell::borderEnd() const
926 {
927     return table()->collapseBorders() ? borderHalfEnd(false) : RenderBlockFlow::borderEnd();
928 }
929 
borderBefore() const930 int RenderTableCell::borderBefore() const
931 {
932     return table()->collapseBorders() ? borderHalfBefore(false) : RenderBlockFlow::borderBefore();
933 }
934 
borderAfter() const935 int RenderTableCell::borderAfter() const
936 {
937     return table()->collapseBorders() ? borderHalfAfter(false) : RenderBlockFlow::borderAfter();
938 }
939 
borderHalfLeft(bool outer) const940 int RenderTableCell::borderHalfLeft(bool outer) const
941 {
942     const RenderStyle* styleForCellFlow = this->styleForCellFlow();
943     if (styleForCellFlow->isHorizontalWritingMode())
944         return styleForCellFlow->isLeftToRightDirection() ? borderHalfStart(outer) : borderHalfEnd(outer);
945     return styleForCellFlow->isFlippedBlocksWritingMode() ? borderHalfAfter(outer) : borderHalfBefore(outer);
946 }
947 
borderHalfRight(bool outer) const948 int RenderTableCell::borderHalfRight(bool outer) const
949 {
950     const RenderStyle* styleForCellFlow = this->styleForCellFlow();
951     if (styleForCellFlow->isHorizontalWritingMode())
952         return styleForCellFlow->isLeftToRightDirection() ? borderHalfEnd(outer) : borderHalfStart(outer);
953     return styleForCellFlow->isFlippedBlocksWritingMode() ? borderHalfBefore(outer) : borderHalfAfter(outer);
954 }
955 
borderHalfTop(bool outer) const956 int RenderTableCell::borderHalfTop(bool outer) const
957 {
958     const RenderStyle* styleForCellFlow = this->styleForCellFlow();
959     if (styleForCellFlow->isHorizontalWritingMode())
960         return styleForCellFlow->isFlippedBlocksWritingMode() ? borderHalfAfter(outer) : borderHalfBefore(outer);
961     return styleForCellFlow->isLeftToRightDirection() ? borderHalfStart(outer) : borderHalfEnd(outer);
962 }
963 
borderHalfBottom(bool outer) const964 int RenderTableCell::borderHalfBottom(bool outer) const
965 {
966     const RenderStyle* styleForCellFlow = this->styleForCellFlow();
967     if (styleForCellFlow->isHorizontalWritingMode())
968         return styleForCellFlow->isFlippedBlocksWritingMode() ? borderHalfBefore(outer) : borderHalfAfter(outer);
969     return styleForCellFlow->isLeftToRightDirection() ? borderHalfEnd(outer) : borderHalfStart(outer);
970 }
971 
borderHalfStart(bool outer) const972 int RenderTableCell::borderHalfStart(bool outer) const
973 {
974     CollapsedBorderValue border = collapsedStartBorder(DoNotIncludeBorderColor);
975     if (border.exists())
976         return (border.width() + ((styleForCellFlow()->isLeftToRightDirection() ^ outer) ? 1 : 0)) / 2; // Give the extra pixel to top and left.
977     return 0;
978 }
979 
borderHalfEnd(bool outer) const980 int RenderTableCell::borderHalfEnd(bool outer) const
981 {
982     CollapsedBorderValue border = collapsedEndBorder(DoNotIncludeBorderColor);
983     if (border.exists())
984         return (border.width() + ((styleForCellFlow()->isLeftToRightDirection() ^ outer) ? 0 : 1)) / 2;
985     return 0;
986 }
987 
borderHalfBefore(bool outer) const988 int RenderTableCell::borderHalfBefore(bool outer) const
989 {
990     CollapsedBorderValue border = collapsedBeforeBorder(DoNotIncludeBorderColor);
991     if (border.exists())
992         return (border.width() + ((styleForCellFlow()->isFlippedBlocksWritingMode() ^ outer) ? 0 : 1)) / 2; // Give the extra pixel to top and left.
993     return 0;
994 }
995 
borderHalfAfter(bool outer) const996 int RenderTableCell::borderHalfAfter(bool outer) const
997 {
998     CollapsedBorderValue border = collapsedAfterBorder(DoNotIncludeBorderColor);
999     if (border.exists())
1000         return (border.width() + ((styleForCellFlow()->isFlippedBlocksWritingMode() ^ outer) ? 1 : 0)) / 2;
1001     return 0;
1002 }
1003 
paint(PaintInfo & paintInfo,const LayoutPoint & paintOffset)1004 void RenderTableCell::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
1005 {
1006     ASSERT(paintInfo.phase != PaintPhaseCollapsedTableBorders);
1007     RenderBlockFlow::paint(paintInfo, paintOffset);
1008 }
1009 
collapsedBorderStyle(EBorderStyle style)1010 static EBorderStyle collapsedBorderStyle(EBorderStyle style)
1011 {
1012     if (style == OUTSET)
1013         return GROOVE;
1014     if (style == INSET)
1015         return RIDGE;
1016     return style;
1017 }
1018 
1019 struct CollapsedBorder {
1020     CollapsedBorderValue borderValue;
1021     BoxSide side;
1022     bool shouldPaint;
1023     int x1;
1024     int y1;
1025     int x2;
1026     int y2;
1027     EBorderStyle style;
1028 };
1029 
1030 class CollapsedBorders {
1031 public:
CollapsedBorders()1032     CollapsedBorders()
1033         : m_count(0)
1034     {
1035     }
1036 
addBorder(const CollapsedBorderValue & borderValue,BoxSide borderSide,bool shouldPaint,int x1,int y1,int x2,int y2,EBorderStyle borderStyle)1037     void addBorder(const CollapsedBorderValue& borderValue, BoxSide borderSide, bool shouldPaint,
1038                    int x1, int y1, int x2, int y2, EBorderStyle borderStyle)
1039     {
1040         if (borderValue.exists() && shouldPaint) {
1041             m_borders[m_count].borderValue = borderValue;
1042             m_borders[m_count].side = borderSide;
1043             m_borders[m_count].shouldPaint = shouldPaint;
1044             m_borders[m_count].x1 = x1;
1045             m_borders[m_count].x2 = x2;
1046             m_borders[m_count].y1 = y1;
1047             m_borders[m_count].y2 = y2;
1048             m_borders[m_count].style = borderStyle;
1049             m_count++;
1050         }
1051     }
1052 
nextBorder()1053     CollapsedBorder* nextBorder()
1054     {
1055         for (unsigned i = 0; i < m_count; i++) {
1056             if (m_borders[i].borderValue.exists() && m_borders[i].shouldPaint) {
1057                 m_borders[i].shouldPaint = false;
1058                 return &m_borders[i];
1059             }
1060         }
1061 
1062         return 0;
1063     }
1064 
1065     CollapsedBorder m_borders[4];
1066     unsigned m_count;
1067 };
1068 
addBorderStyle(RenderTable::CollapsedBorderValues & borderValues,CollapsedBorderValue borderValue)1069 static void addBorderStyle(RenderTable::CollapsedBorderValues& borderValues,
1070                            CollapsedBorderValue borderValue)
1071 {
1072     if (!borderValue.exists())
1073         return;
1074     size_t count = borderValues.size();
1075     for (size_t i = 0; i < count; ++i)
1076         if (borderValues[i].isSameIgnoringColor(borderValue))
1077             return;
1078     borderValues.append(borderValue);
1079 }
1080 
collectBorderValues(RenderTable::CollapsedBorderValues & borderValues) const1081 void RenderTableCell::collectBorderValues(RenderTable::CollapsedBorderValues& borderValues) const
1082 {
1083     addBorderStyle(borderValues, collapsedStartBorder());
1084     addBorderStyle(borderValues, collapsedEndBorder());
1085     addBorderStyle(borderValues, collapsedBeforeBorder());
1086     addBorderStyle(borderValues, collapsedAfterBorder());
1087 }
1088 
compareBorderValuesForQSort(const void * pa,const void * pb)1089 static int compareBorderValuesForQSort(const void* pa, const void* pb)
1090 {
1091     const CollapsedBorderValue* a = static_cast<const CollapsedBorderValue*>(pa);
1092     const CollapsedBorderValue* b = static_cast<const CollapsedBorderValue*>(pb);
1093     if (a->isSameIgnoringColor(*b))
1094         return 0;
1095     return compareBorders(*a, *b);
1096 }
1097 
sortBorderValues(RenderTable::CollapsedBorderValues & borderValues)1098 void RenderTableCell::sortBorderValues(RenderTable::CollapsedBorderValues& borderValues)
1099 {
1100     qsort(borderValues.data(), borderValues.size(), sizeof(CollapsedBorderValue),
1101         compareBorderValuesForQSort);
1102 }
1103 
paintCollapsedBorders(PaintInfo & paintInfo,const LayoutPoint & paintOffset)1104 void RenderTableCell::paintCollapsedBorders(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
1105 {
1106     ASSERT(paintInfo.phase == PaintPhaseCollapsedTableBorders);
1107 
1108     if (!paintInfo.shouldPaintWithinRoot(this) || style()->visibility() != VISIBLE)
1109         return;
1110 
1111     LayoutRect paintRect = LayoutRect(paintOffset + location(), pixelSnappedSize());
1112     if (paintRect.y() - table()->outerBorderTop() >= paintInfo.rect.maxY())
1113         return;
1114 
1115     if (paintRect.maxY() + table()->outerBorderBottom() <= paintInfo.rect.y())
1116         return;
1117 
1118     GraphicsContext* graphicsContext = paintInfo.context;
1119     if (!table()->currentBorderValue() || graphicsContext->paintingDisabled())
1120         return;
1121 
1122     const RenderStyle* styleForCellFlow = this->styleForCellFlow();
1123     CollapsedBorderValue leftVal = cachedCollapsedLeftBorder(styleForCellFlow);
1124     CollapsedBorderValue rightVal = cachedCollapsedRightBorder(styleForCellFlow);
1125     CollapsedBorderValue topVal = cachedCollapsedTopBorder(styleForCellFlow);
1126     CollapsedBorderValue bottomVal = cachedCollapsedBottomBorder(styleForCellFlow);
1127 
1128     // Adjust our x/y/width/height so that we paint the collapsed borders at the correct location.
1129     int topWidth = topVal.width();
1130     int bottomWidth = bottomVal.width();
1131     int leftWidth = leftVal.width();
1132     int rightWidth = rightVal.width();
1133 
1134     IntRect borderRect = pixelSnappedIntRect(paintRect.x() - leftWidth / 2,
1135             paintRect.y() - topWidth / 2,
1136             paintRect.width() + leftWidth / 2 + (rightWidth + 1) / 2,
1137             paintRect.height() + topWidth / 2 + (bottomWidth + 1) / 2);
1138 
1139     EBorderStyle topStyle = collapsedBorderStyle(topVal.style());
1140     EBorderStyle bottomStyle = collapsedBorderStyle(bottomVal.style());
1141     EBorderStyle leftStyle = collapsedBorderStyle(leftVal.style());
1142     EBorderStyle rightStyle = collapsedBorderStyle(rightVal.style());
1143 
1144     bool renderTop = topStyle > BHIDDEN && !topVal.isTransparent();
1145     bool renderBottom = bottomStyle > BHIDDEN && !bottomVal.isTransparent();
1146     bool renderLeft = leftStyle > BHIDDEN && !leftVal.isTransparent();
1147     bool renderRight = rightStyle > BHIDDEN && !rightVal.isTransparent();
1148 
1149     // We never paint diagonals at the joins.  We simply let the border with the highest
1150     // precedence paint on top of borders with lower precedence.
1151     CollapsedBorders borders;
1152     borders.addBorder(topVal, BSTop, renderTop, borderRect.x(), borderRect.y(), borderRect.maxX(), borderRect.y() + topWidth, topStyle);
1153     borders.addBorder(bottomVal, BSBottom, renderBottom, borderRect.x(), borderRect.maxY() - bottomWidth, borderRect.maxX(), borderRect.maxY(), bottomStyle);
1154     borders.addBorder(leftVal, BSLeft, renderLeft, borderRect.x(), borderRect.y(), borderRect.x() + leftWidth, borderRect.maxY(), leftStyle);
1155     borders.addBorder(rightVal, BSRight, renderRight, borderRect.maxX() - rightWidth, borderRect.y(), borderRect.maxX(), borderRect.maxY(), rightStyle);
1156 
1157     bool antialias = shouldAntialiasLines(graphicsContext);
1158 
1159     for (CollapsedBorder* border = borders.nextBorder(); border; border = borders.nextBorder()) {
1160         if (border->borderValue.isSameIgnoringColor(*table()->currentBorderValue())) {
1161             drawLineForBoxSide(graphicsContext, border->x1, border->y1, border->x2, border->y2, border->side,
1162                 border->borderValue.color().resolve(style()->visitedDependentColor(CSSPropertyColor)), border->style, 0, 0, antialias);
1163         }
1164     }
1165 }
1166 
paintBackgroundsBehindCell(PaintInfo & paintInfo,const LayoutPoint & paintOffset,RenderObject * backgroundObject)1167 void RenderTableCell::paintBackgroundsBehindCell(PaintInfo& paintInfo, const LayoutPoint& paintOffset, RenderObject* backgroundObject)
1168 {
1169     if (!paintInfo.shouldPaintWithinRoot(this))
1170         return;
1171 
1172     if (!backgroundObject)
1173         return;
1174 
1175     if (style()->visibility() != VISIBLE)
1176         return;
1177 
1178     RenderTable* tableElt = table();
1179     if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
1180         return;
1181 
1182     LayoutPoint adjustedPaintOffset = paintOffset;
1183     if (backgroundObject != this)
1184         adjustedPaintOffset.moveBy(location());
1185 
1186     Color c = backgroundObject->resolveColor(CSSPropertyBackgroundColor);
1187     const FillLayer* bgLayer = backgroundObject->style()->backgroundLayers();
1188 
1189     if (bgLayer->hasImage() || c.alpha()) {
1190         // We have to clip here because the background would paint
1191         // on top of the borders otherwise.  This only matters for cells and rows.
1192         bool shouldClip = backgroundObject->hasLayer() && (backgroundObject == this || backgroundObject == parent()) && tableElt->collapseBorders();
1193         GraphicsContextStateSaver stateSaver(*paintInfo.context, shouldClip);
1194         if (shouldClip) {
1195             LayoutRect clipRect(adjustedPaintOffset.x() + borderLeft(), adjustedPaintOffset.y() + borderTop(),
1196                 width() - borderLeft() - borderRight(), height() - borderTop() - borderBottom());
1197             paintInfo.context->clip(clipRect);
1198         }
1199         paintFillLayers(paintInfo, c, bgLayer, LayoutRect(adjustedPaintOffset, pixelSnappedSize()), BackgroundBleedNone, CompositeSourceOver, backgroundObject);
1200     }
1201 }
1202 
paintBoxDecorations(PaintInfo & paintInfo,const LayoutPoint & paintOffset)1203 void RenderTableCell::paintBoxDecorations(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
1204 {
1205     if (!paintInfo.shouldPaintWithinRoot(this))
1206         return;
1207 
1208     RenderTable* tableElt = table();
1209     if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
1210         return;
1211 
1212     LayoutRect paintRect = LayoutRect(paintOffset, pixelSnappedSize());
1213     paintBoxShadow(paintInfo, paintRect, style(), Normal);
1214 
1215     // Paint our cell background.
1216     paintBackgroundsBehindCell(paintInfo, paintOffset, this);
1217 
1218     paintBoxShadow(paintInfo, paintRect, style(), Inset);
1219 
1220     if (!style()->hasBorder() || tableElt->collapseBorders())
1221         return;
1222 
1223     paintBorder(paintInfo, paintRect, style());
1224 }
1225 
paintMask(PaintInfo & paintInfo,const LayoutPoint & paintOffset)1226 void RenderTableCell::paintMask(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
1227 {
1228     if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
1229         return;
1230 
1231     RenderTable* tableElt = table();
1232     if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
1233         return;
1234 
1235     paintMaskImages(paintInfo, LayoutRect(paintOffset, pixelSnappedSize()));
1236 }
1237 
boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance,InlineFlowBox *) const1238 bool RenderTableCell::boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance, InlineFlowBox*) const
1239 {
1240     return false;
1241 }
1242 
scrollbarsChanged(bool horizontalScrollbarChanged,bool verticalScrollbarChanged)1243 void RenderTableCell::scrollbarsChanged(bool horizontalScrollbarChanged, bool verticalScrollbarChanged)
1244 {
1245     LayoutUnit scrollbarHeight = scrollbarLogicalHeight();
1246     if (!scrollbarHeight)
1247         return; // Not sure if we should be doing something when a scrollbar goes away or not.
1248 
1249     // We only care if the scrollbar that affects our intrinsic padding has been added.
1250     if ((isHorizontalWritingMode() && !horizontalScrollbarChanged) ||
1251         (!isHorizontalWritingMode() && !verticalScrollbarChanged))
1252         return;
1253 
1254     // Shrink our intrinsic padding as much as possible to accommodate the scrollbar.
1255     if (style()->verticalAlign() == MIDDLE) {
1256         LayoutUnit totalHeight = logicalHeight();
1257         LayoutUnit heightWithoutIntrinsicPadding = totalHeight - intrinsicPaddingBefore() - intrinsicPaddingAfter();
1258         totalHeight -= scrollbarHeight;
1259         LayoutUnit newBeforePadding = (totalHeight - heightWithoutIntrinsicPadding) / 2;
1260         LayoutUnit newAfterPadding = totalHeight - heightWithoutIntrinsicPadding - newBeforePadding;
1261         setIntrinsicPaddingBefore(newBeforePadding);
1262         setIntrinsicPaddingAfter(newAfterPadding);
1263     } else
1264         setIntrinsicPaddingAfter(intrinsicPaddingAfter() - scrollbarHeight);
1265 }
1266 
createAnonymous(Document * document)1267 RenderTableCell* RenderTableCell::createAnonymous(Document* document)
1268 {
1269     RenderTableCell* renderer = new RenderTableCell(0);
1270     renderer->setDocumentForAnonymous(document);
1271     return renderer;
1272 }
1273 
createAnonymousWithParentRenderer(const RenderObject * parent)1274 RenderTableCell* RenderTableCell::createAnonymousWithParentRenderer(const RenderObject* parent)
1275 {
1276     RenderTableCell* newCell = RenderTableCell::createAnonymous(&parent->document());
1277     RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(parent->style(), TABLE_CELL);
1278     newCell->setStyle(newStyle.release());
1279     return newCell;
1280 }
1281 
1282 } // namespace WebCore
1283