• 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 "RenderTableCell.h"
27 
28 #include "FloatQuad.h"
29 #include "GraphicsContext.h"
30 #include "HTMLNames.h"
31 #include "HTMLTableCellElement.h"
32 #include "RenderTableCol.h"
33 #include "RenderView.h"
34 #include "TransformState.h"
35 
36 #ifdef ANDROID_LAYOUT
37 #include "Document.h"
38 #include "Settings.h"
39 #endif
40 
41 using namespace std;
42 
43 namespace WebCore {
44 
45 using namespace HTMLNames;
46 
RenderTableCell(Node * node)47 RenderTableCell::RenderTableCell(Node* node)
48     : RenderBlock(node)
49     , m_row(-1)
50     , m_column(-1)
51     , m_rowSpan(1)
52     , m_columnSpan(1)
53     , m_intrinsicPaddingTop(0)
54     , m_intrinsicPaddingBottom(0)
55     , m_percentageHeight(0)
56 {
57     updateFromElement();
58 }
59 
destroy()60 void RenderTableCell::destroy()
61 {
62     RenderTableSection* recalcSection = parent() ? section() : 0;
63 
64     RenderBlock::destroy();
65 
66     if (recalcSection)
67         recalcSection->setNeedsCellRecalc();
68 }
69 
updateFromElement()70 void RenderTableCell::updateFromElement()
71 {
72     Node* n = node();
73     if (n && (n->hasTagName(tdTag) || n->hasTagName(thTag))) {
74         HTMLTableCellElement* tc = static_cast<HTMLTableCellElement*>(n);
75         int oldRSpan = m_rowSpan;
76         int oldCSpan = m_columnSpan;
77 
78         m_columnSpan = tc->colSpan();
79         m_rowSpan = tc->rowSpan();
80         if ((oldRSpan != m_rowSpan || oldCSpan != m_columnSpan) && style() && parent()) {
81             setNeedsLayoutAndPrefWidthsRecalc();
82             if (section())
83                 section()->setNeedsCellRecalc();
84         }
85     }
86 }
87 
styleOrColWidth() const88 Length RenderTableCell::styleOrColWidth() const
89 {
90     Length w = style()->width();
91     if (colSpan() > 1 || !w.isAuto())
92         return w;
93     RenderTableCol* tableCol = table()->colElement(col());
94     if (tableCol) {
95         w = tableCol->style()->width();
96 
97         // Column widths specified on <col> apply to the border box of the cell.
98         // Percentages don't need to be handled since they're always treated this way (even when specified on the cells).
99         // See Bugzilla bug 8126 for details.
100         if (w.isFixed() && w.value() > 0)
101             w = Length(max(0, w.value() - borderLeft() - borderRight() - paddingLeft() - paddingRight()), Fixed);
102     }
103     return w;
104 }
105 
calcPrefWidths()106 void RenderTableCell::calcPrefWidths()
107 {
108     // The child cells rely on the grids up in the sections to do their calcPrefWidths work.  Normally the sections are set up early, as table
109     // cells are added, but relayout can cause the cells to be freed, leaving stale pointers in the sections'
110     // grids.  We must refresh those grids before the child cells try to use them.
111     table()->recalcSectionsIfNeeded();
112 
113     RenderBlock::calcPrefWidths();
114     if (node() && style()->autoWrap()) {
115         // See if nowrap was set.
116         Length w = styleOrColWidth();
117         String nowrap = static_cast<Element*>(node())->getAttribute(nowrapAttr);
118         if (!nowrap.isNull() && w.isFixed())
119             // Nowrap is set, but we didn't actually use it because of the
120             // fixed width set on the cell.  Even so, it is a WinIE/Moz trait
121             // to make the minwidth of the cell into the fixed width.  They do this
122             // even in strict mode, so do not make this a quirk.  Affected the top
123             // of hiptop.com.
124             m_minPrefWidth = max(w.value(), m_minPrefWidth);
125     }
126 }
127 
calcWidth()128 void RenderTableCell::calcWidth()
129 {
130 #ifdef ANDROID_LAYOUT
131     if (view()->frameView()) {
132         const Settings* settings = document()->settings();
133         ASSERT(settings);
134         if (settings->layoutAlgorithm() == Settings::kLayoutFitColumnToScreen) {
135             m_visibleWidth = view()->frameView()->screenWidth();
136         }
137     }
138 #endif
139 }
140 
updateWidth(int w)141 void RenderTableCell::updateWidth(int w)
142 {
143     if (w != width()) {
144         setWidth(w);
145         setCellWidthChanged(true);
146     }
147 }
148 
layout()149 void RenderTableCell::layout()
150 {
151     layoutBlock(cellWidthChanged());
152     setCellWidthChanged(false);
153 }
154 
paddingTop(bool includeIntrinsicPadding) const155 int RenderTableCell::paddingTop(bool includeIntrinsicPadding) const
156 {
157     return RenderBlock::paddingTop() + (includeIntrinsicPadding ? intrinsicPaddingTop() : 0);
158 }
159 
paddingBottom(bool includeIntrinsicPadding) const160 int RenderTableCell::paddingBottom(bool includeIntrinsicPadding) const
161 {
162     return RenderBlock::paddingBottom() + (includeIntrinsicPadding ? intrinsicPaddingBottom() : 0);
163 }
164 
setOverrideSize(int size)165 void RenderTableCell::setOverrideSize(int size)
166 {
167     clearIntrinsicPadding();
168     RenderBlock::setOverrideSize(size);
169 }
170 
clippedOverflowRectForRepaint(RenderBoxModelObject * repaintContainer)171 IntRect RenderTableCell::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer)
172 {
173     // If the table grid is dirty, we cannot get reliable information about adjoining cells,
174     // so we ignore outside borders. This should not be a problem because it means that
175     // the table is going to recalculate the grid, relayout and repaint its current rect, which
176     // includes any outside borders of this cell.
177     if (!table()->collapseBorders() || table()->needsSectionRecalc())
178         return RenderBlock::clippedOverflowRectForRepaint(repaintContainer);
179 
180     bool rtl = table()->style()->direction() == RTL;
181     int outlineSize = style()->outlineSize();
182     int left = max(borderHalfLeft(true), outlineSize);
183     int right = max(borderHalfRight(true), outlineSize);
184     int top = max(borderHalfTop(true), outlineSize);
185     int bottom = max(borderHalfBottom(true), outlineSize);
186     if ((left && !rtl) || (right && rtl)) {
187         if (RenderTableCell* before = table()->cellBefore(this)) {
188             top = max(top, before->borderHalfTop(true));
189             bottom = max(bottom, before->borderHalfBottom(true));
190         }
191     }
192     if ((left && rtl) || (right && !rtl)) {
193         if (RenderTableCell* after = table()->cellAfter(this)) {
194             top = max(top, after->borderHalfTop(true));
195             bottom = max(bottom, after->borderHalfBottom(true));
196         }
197     }
198     if (top) {
199         if (RenderTableCell* above = table()->cellAbove(this)) {
200             left = max(left, above->borderHalfLeft(true));
201             right = max(right, above->borderHalfRight(true));
202         }
203     }
204     if (bottom) {
205         if (RenderTableCell* below = table()->cellBelow(this)) {
206             left = max(left, below->borderHalfLeft(true));
207             right = max(right, below->borderHalfRight(true));
208         }
209     }
210     left = max(left, -overflowLeft(false));
211     top = max(top, -overflowTop(false));
212     IntRect r(-left, - top, left + max(width() + right, overflowWidth(false)), top + max(height() + bottom, overflowHeight(false)));
213 
214     if (RenderView* v = view()) {
215         // FIXME: layoutDelta needs to be applied in parts before/after transforms and
216         // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308
217         r.move(v->layoutDelta());
218     }
219     computeRectForRepaint(repaintContainer, r);
220     return r;
221 }
222 
computeRectForRepaint(RenderBoxModelObject * repaintContainer,IntRect & r,bool fixed)223 void RenderTableCell::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& r, bool fixed)
224 {
225     if (repaintContainer == this)
226         return;
227     r.setY(r.y());
228     RenderView* v = view();
229     if ((!v || !v->layoutStateEnabled()) && parent())
230         r.move(-parentBox()->x(), -parentBox()->y()); // Rows are in the same coordinate space, so don't add their offset in.
231     RenderBlock::computeRectForRepaint(repaintContainer, r, fixed);
232 }
233 
mapLocalToContainer(RenderBoxModelObject * repaintContainer,bool fixed,bool useTransforms,TransformState & transformState) const234 void RenderTableCell::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed, bool useTransforms, TransformState& transformState) const
235 {
236     if (repaintContainer == this)
237         return;
238 
239     RenderView* v = view();
240     if ((!v || !v->layoutStateEnabled()) && parent()) {
241         // Rows are in the same coordinate space, so don't add their offset in.
242         // FIXME: this is wrong with transforms
243         transformState.move(-parentBox()->x(), -parentBox()->y());
244     }
245     RenderBlock::mapLocalToContainer(repaintContainer, fixed, useTransforms, transformState);
246 }
247 
mapAbsoluteToLocalPoint(bool fixed,bool useTransforms,TransformState & transformState) const248 void RenderTableCell::mapAbsoluteToLocalPoint(bool fixed, bool useTransforms, TransformState& transformState) const
249 {
250     RenderBlock::mapAbsoluteToLocalPoint(fixed, useTransforms, transformState);
251     if (parent()) {
252         // Rows are in the same coordinate space, so add their offset back in.
253         // FIXME: this is wrong with transforms
254         transformState.move(parentBox()->x(), parentBox()->y());
255     }
256 }
257 
baselinePosition(bool firstLine,bool isRootLineBox) const258 int RenderTableCell::baselinePosition(bool firstLine, bool isRootLineBox) const
259 {
260     if (isRootLineBox)
261         return RenderBox::baselinePosition(firstLine, isRootLineBox);
262 
263     // <http://www.w3.org/TR/2007/CR-CSS21-20070719/tables.html#height-layout>: The baseline of a cell is the baseline of
264     // the first in-flow line box in the cell, or the first in-flow table-row in the cell, whichever comes first. If there
265     // is no such line box or table-row, the baseline is the bottom of content edge of the cell box.
266     int firstLineBaseline = firstLineBoxBaseline();
267     if (firstLineBaseline != -1)
268         return firstLineBaseline;
269     return paddingTop() + borderTop() + contentHeight();
270 }
271 
styleWillChange(StyleDifference diff,const RenderStyle * newStyle)272 void RenderTableCell::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
273 {
274     if (parent() && section() && style() && style()->height() != newStyle->height())
275         section()->setNeedsCellRecalc();
276 
277     ASSERT(newStyle->display() == TABLE_CELL);
278 
279     RenderBlock::styleWillChange(diff, newStyle);
280 }
281 
styleDidChange(StyleDifference diff,const RenderStyle * oldStyle)282 void RenderTableCell::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
283 {
284     RenderBlock::styleDidChange(diff, oldStyle);
285     setHasBoxDecorations(true);
286 }
287 
288 // The following rules apply for resolving conflicts and figuring out which border
289 // to use.
290 // (1) Borders with the 'border-style' of 'hidden' take precedence over all other conflicting
291 // borders. Any border with this value suppresses all borders at this location.
292 // (2) Borders with a style of 'none' have the lowest priority. Only if the border properties of all
293 // the elements meeting at this edge are 'none' will the border be omitted (but note that 'none' is
294 // the default value for the border style.)
295 // (3) If none of the styles are 'hidden' and at least one of them is not 'none', then narrow borders
296 // are discarded in favor of wider ones. If several have the same 'border-width' then styles are preferred
297 // in this order: 'double', 'solid', 'dashed', 'dotted', 'ridge', 'outset', 'groove', and the lowest: 'inset'.
298 // (4) If border styles differ only in color, then a style set on a cell wins over one on a row,
299 // which wins over a row group, column, column group and, lastly, table. It is undefined which color
300 // is used when two elements of the same type disagree.
compareBorders(const CollapsedBorderValue & border1,const CollapsedBorderValue & border2)301 static CollapsedBorderValue compareBorders(const CollapsedBorderValue& border1, const CollapsedBorderValue& border2)
302 {
303     // Sanity check the values passed in.  If either is null, return the other.
304     if (!border2.exists())
305         return border1;
306     if (!border1.exists())
307         return border2;
308 
309     // Rule #1 above.
310     if (border1.style() == BHIDDEN || border2.style() == BHIDDEN)
311         return CollapsedBorderValue(); // No border should exist at this location.
312 
313     // Rule #2 above.  A style of 'none' has lowest priority and always loses to any other border.
314     if (border2.style() == BNONE)
315         return border1;
316     if (border1.style() == BNONE)
317         return border2;
318 
319     // The first part of rule #3 above. Wider borders win.
320     if (border1.width() != border2.width())
321         return border1.width() > border2.width() ? border1 : border2;
322 
323     // The borders have equal width.  Sort by border style.
324     if (border1.style() != border2.style())
325         return border1.style() > border2.style() ? border1 : border2;
326 
327     // The border have the same width and style.  Rely on precedence (cell over row over row group, etc.)
328     return border1.precedence >= border2.precedence ? border1 : border2;
329 }
330 
collapsedLeftBorder(bool rtl) const331 CollapsedBorderValue RenderTableCell::collapsedLeftBorder(bool rtl) const
332 {
333     RenderTable* tableElt = table();
334     bool leftmostColumn;
335     if (!rtl)
336         leftmostColumn = col() == 0;
337     else {
338         int effCol = tableElt->colToEffCol(col() + colSpan() - 1);
339         leftmostColumn = effCol == tableElt->numEffCols() - 1;
340     }
341 
342     // For border left, we need to check, in order of precedence:
343     // (1) Our left border.
344     CollapsedBorderValue result(&style()->borderLeft(), BCELL);
345 
346     // (2) The right border of the cell to the left.
347     RenderTableCell* prevCell = rtl ? tableElt->cellAfter(this) : tableElt->cellBefore(this);
348     if (prevCell) {
349         result = rtl ? compareBorders(result, CollapsedBorderValue(&prevCell->style()->borderRight(), BCELL)) : compareBorders(CollapsedBorderValue(&prevCell->style()->borderRight(), BCELL), result);
350         if (!result.exists())
351             return result;
352     } else if (leftmostColumn) {
353         // (3) Our row's left border.
354         result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderLeft(), BROW));
355         if (!result.exists())
356             return result;
357 
358         // (4) Our row group's left border.
359         result = compareBorders(result, CollapsedBorderValue(&section()->style()->borderLeft(), BROWGROUP));
360         if (!result.exists())
361             return result;
362     }
363 
364     // (5) Our column and column group's left borders.
365     bool startColEdge;
366     bool endColEdge;
367     RenderTableCol* colElt = tableElt->colElement(col() + (rtl ? colSpan() - 1 : 0), &startColEdge, &endColEdge);
368     if (colElt && (!rtl ? startColEdge : endColEdge)) {
369         result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL));
370         if (!result.exists())
371             return result;
372         if (colElt->parent()->isTableCol() && (!rtl ? !colElt->previousSibling() : !colElt->nextSibling())) {
373             result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderLeft(), BCOLGROUP));
374             if (!result.exists())
375                 return result;
376         }
377     }
378 
379     // (6) The right border of the column to the left.
380     if (!leftmostColumn) {
381         colElt = tableElt->colElement(col() + (rtl ? colSpan() : -1), &startColEdge, &endColEdge);
382         if (colElt && (!rtl ? endColEdge : startColEdge)) {
383             result = rtl ? compareBorders(result, CollapsedBorderValue(&colElt->style()->borderRight(), BCOL)) : compareBorders(CollapsedBorderValue(&colElt->style()->borderRight(), BCOL), result);
384             if (!result.exists())
385                 return result;
386         }
387     } else {
388         // (7) The table's left border.
389         result = compareBorders(result, CollapsedBorderValue(&tableElt->style()->borderLeft(), BTABLE));
390         if (!result.exists())
391             return result;
392     }
393 
394     return result;
395 }
396 
collapsedRightBorder(bool rtl) const397 CollapsedBorderValue RenderTableCell::collapsedRightBorder(bool rtl) const
398 {
399     RenderTable* tableElt = table();
400     bool rightmostColumn;
401     if (rtl)
402         rightmostColumn = col() == 0;
403     else {
404         int effCol = tableElt->colToEffCol(col() + colSpan() - 1);
405         rightmostColumn = effCol == tableElt->numEffCols() - 1;
406     }
407 
408     // For border right, we need to check, in order of precedence:
409     // (1) Our right border.
410     CollapsedBorderValue result = CollapsedBorderValue(&style()->borderRight(), BCELL);
411 
412     // (2) The left border of the cell to the right.
413     if (!rightmostColumn) {
414         RenderTableCell* nextCell = rtl ? tableElt->cellBefore(this) : tableElt->cellAfter(this);
415         if (nextCell && nextCell->style()) {
416             result = rtl ? compareBorders(CollapsedBorderValue(&nextCell->style()->borderLeft(), BCELL), result) : compareBorders(result, CollapsedBorderValue(&nextCell->style()->borderLeft(), BCELL));
417             if (!result.exists())
418                 return result;
419         }
420     } else {
421         // (3) Our row's right border.
422         result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderRight(), BROW));
423         if (!result.exists())
424             return result;
425 
426         // (4) Our row group's right border.
427         result = compareBorders(result, CollapsedBorderValue(&section()->style()->borderRight(), BROWGROUP));
428         if (!result.exists())
429             return result;
430     }
431 
432     // (5) Our column and column group's right borders.
433     bool startColEdge;
434     bool endColEdge;
435     RenderTableCol* colElt = tableElt->colElement(col() + (rtl ? 0 : colSpan() - 1), &startColEdge, &endColEdge);
436     if (colElt && (!rtl ? endColEdge : startColEdge)) {
437         result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderRight(), BCOL));
438         if (!result.exists())
439             return result;
440         if (colElt->parent()->isTableCol() && (!rtl ? !colElt->nextSibling() : !colElt->previousSibling())) {
441             result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderRight(), BCOLGROUP));
442             if (!result.exists())
443                 return result;
444         }
445     }
446 
447     // (6) The left border of the column to the right.
448     if (!rightmostColumn) {
449         colElt = tableElt->colElement(col() + (rtl ? -1 : colSpan()), &startColEdge, &endColEdge);
450         if (colElt && (!rtl ? startColEdge : endColEdge)) {
451             result = rtl ? compareBorders(CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL), result) : compareBorders(result, CollapsedBorderValue(&colElt->style()->borderLeft(), BCOL));
452             if (!result.exists())
453                 return result;
454         }
455     } else {
456         // (7) The table's right border.
457         result = compareBorders(result, CollapsedBorderValue(&tableElt->style()->borderRight(), BTABLE));
458         if (!result.exists())
459             return result;
460     }
461 
462     return result;
463 }
464 
collapsedTopBorder() const465 CollapsedBorderValue RenderTableCell::collapsedTopBorder() const
466 {
467     // For border top, we need to check, in order of precedence:
468     // (1) Our top border.
469     CollapsedBorderValue result = CollapsedBorderValue(&style()->borderTop(), BCELL);
470 
471     RenderTableCell* prevCell = table()->cellAbove(this);
472     if (prevCell) {
473         // (2) A previous cell's bottom border.
474         result = compareBorders(CollapsedBorderValue(&prevCell->style()->borderBottom(), BCELL), result);
475         if (!result.exists())
476             return result;
477     }
478 
479     // (3) Our row's top border.
480     result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderTop(), BROW));
481     if (!result.exists())
482         return result;
483 
484     // (4) The previous row's bottom border.
485     if (prevCell) {
486         RenderObject* prevRow = 0;
487         if (prevCell->section() == section())
488             prevRow = parent()->previousSibling();
489         else
490             prevRow = prevCell->section()->lastChild();
491 
492         if (prevRow) {
493             result = compareBorders(CollapsedBorderValue(&prevRow->style()->borderBottom(), BROW), result);
494             if (!result.exists())
495                 return result;
496         }
497     }
498 
499     // Now check row groups.
500     RenderTableSection* currSection = section();
501     if (!row()) {
502         // (5) Our row group's top border.
503         result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderTop(), BROWGROUP));
504         if (!result.exists())
505             return result;
506 
507         // (6) Previous row group's bottom border.
508         currSection = table()->sectionAbove(currSection);
509         if (currSection) {
510             result = compareBorders(CollapsedBorderValue(&currSection->style()->borderBottom(), BROWGROUP), result);
511             if (!result.exists())
512                 return result;
513         }
514     }
515 
516     if (!currSection) {
517         // (8) Our column and column group's top borders.
518         RenderTableCol* colElt = table()->colElement(col());
519         if (colElt) {
520             result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderTop(), BCOL));
521             if (!result.exists())
522                 return result;
523             if (colElt->parent()->isTableCol()) {
524                 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderTop(), BCOLGROUP));
525                 if (!result.exists())
526                     return result;
527             }
528         }
529 
530         // (9) The table's top border.
531         result = compareBorders(result, CollapsedBorderValue(&table()->style()->borderTop(), BTABLE));
532         if (!result.exists())
533             return result;
534     }
535 
536     return result;
537 }
538 
collapsedBottomBorder() const539 CollapsedBorderValue RenderTableCell::collapsedBottomBorder() const
540 {
541     // For border top, we need to check, in order of precedence:
542     // (1) Our bottom border.
543     CollapsedBorderValue result = CollapsedBorderValue(&style()->borderBottom(), BCELL);
544 
545     RenderTableCell* nextCell = table()->cellBelow(this);
546     if (nextCell) {
547         // (2) A following cell's top border.
548         result = compareBorders(result, CollapsedBorderValue(&nextCell->style()->borderTop(), BCELL));
549         if (!result.exists())
550             return result;
551     }
552 
553     // (3) Our row's bottom border. (FIXME: Deal with rowspan!)
554     result = compareBorders(result, CollapsedBorderValue(&parent()->style()->borderBottom(), BROW));
555     if (!result.exists())
556         return result;
557 
558     // (4) The next row's top border.
559     if (nextCell) {
560         result = compareBorders(result, CollapsedBorderValue(&nextCell->parent()->style()->borderTop(), BROW));
561         if (!result.exists())
562             return result;
563     }
564 
565     // Now check row groups.
566     RenderTableSection* currSection = section();
567     if (row() + rowSpan() >= currSection->numRows()) {
568         // (5) Our row group's bottom border.
569         result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderBottom(), BROWGROUP));
570         if (!result.exists())
571             return result;
572 
573         // (6) Following row group's top border.
574         currSection = table()->sectionBelow(currSection);
575         if (currSection) {
576             result = compareBorders(result, CollapsedBorderValue(&currSection->style()->borderTop(), BROWGROUP));
577             if (!result.exists())
578                 return result;
579         }
580     }
581 
582     if (!currSection) {
583         // (8) Our column and column group's bottom borders.
584         RenderTableCol* colElt = table()->colElement(col());
585         if (colElt) {
586             result = compareBorders(result, CollapsedBorderValue(&colElt->style()->borderBottom(), BCOL));
587             if (!result.exists()) return result;
588             if (colElt->parent()->isTableCol()) {
589                 result = compareBorders(result, CollapsedBorderValue(&colElt->parent()->style()->borderBottom(), BCOLGROUP));
590                 if (!result.exists())
591                     return result;
592             }
593         }
594 
595         // (9) The table's bottom border.
596         result = compareBorders(result, CollapsedBorderValue(&table()->style()->borderBottom(), BTABLE));
597         if (!result.exists())
598             return result;
599     }
600 
601     return result;
602 }
603 
borderLeft() const604 int RenderTableCell::borderLeft() const
605 {
606     return table()->collapseBorders() ? borderHalfLeft(false) : RenderBlock::borderLeft();
607 }
608 
borderRight() const609 int RenderTableCell::borderRight() const
610 {
611     return table()->collapseBorders() ? borderHalfRight(false) : RenderBlock::borderRight();
612 }
613 
borderTop() const614 int RenderTableCell::borderTop() const
615 {
616     return table()->collapseBorders() ? borderHalfTop(false) : RenderBlock::borderTop();
617 }
618 
borderBottom() const619 int RenderTableCell::borderBottom() const
620 {
621     return table()->collapseBorders() ? borderHalfBottom(false) : RenderBlock::borderBottom();
622 }
623 
borderHalfLeft(bool outer) const624 int RenderTableCell::borderHalfLeft(bool outer) const
625 {
626     CollapsedBorderValue border = collapsedLeftBorder(table()->style()->direction() == RTL);
627     if (border.exists())
628         return (border.width() + (outer ? 0 : 1)) / 2; // Give the extra pixel to top and left.
629     return 0;
630 }
631 
borderHalfRight(bool outer) const632 int RenderTableCell::borderHalfRight(bool outer) const
633 {
634     CollapsedBorderValue border = collapsedRightBorder(table()->style()->direction() == RTL);
635     if (border.exists())
636         return (border.width() + (outer ? 1 : 0)) / 2;
637     return 0;
638 }
639 
borderHalfTop(bool outer) const640 int RenderTableCell::borderHalfTop(bool outer) const
641 {
642     CollapsedBorderValue border = collapsedTopBorder();
643     if (border.exists())
644         return (border.width() + (outer ? 0 : 1)) / 2; // Give the extra pixel to top and left.
645     return 0;
646 }
647 
borderHalfBottom(bool outer) const648 int RenderTableCell::borderHalfBottom(bool outer) const
649 {
650     CollapsedBorderValue border = collapsedBottomBorder();
651     if (border.exists())
652         return (border.width() + (outer ? 1 : 0)) / 2;
653     return 0;
654 }
655 
paint(PaintInfo & paintInfo,int tx,int ty)656 void RenderTableCell::paint(PaintInfo& paintInfo, int tx, int ty)
657 {
658     if (paintInfo.phase == PaintPhaseCollapsedTableBorders && style()->visibility() == VISIBLE) {
659         tx += x();
660         ty += y();
661         int os = 2 * maximalOutlineSize(paintInfo.phase);
662         if (ty - table()->outerBorderTop() < paintInfo.rect.bottom() + os &&
663             ty + height() + table()->outerBorderBottom() > paintInfo.rect.y() - os)
664             paintCollapsedBorder(paintInfo.context, tx, ty, width(), height());
665         return;
666     }
667 
668     RenderBlock::paint(paintInfo, tx, ty);
669 }
670 
collapsedBorderStyle(EBorderStyle style)671 static EBorderStyle collapsedBorderStyle(EBorderStyle style)
672 {
673     if (style == OUTSET)
674         return GROOVE;
675     if (style == INSET)
676         return RIDGE;
677     return style;
678 }
679 
680 struct CollapsedBorder {
681     CollapsedBorderValue borderValue;
682     BoxSide side;
683     bool shouldPaint;
684     int x1;
685     int y1;
686     int x2;
687     int y2;
688     EBorderStyle style;
689 };
690 
691 class CollapsedBorders {
692 public:
CollapsedBorders()693     CollapsedBorders()
694         : m_count(0)
695     {
696     }
697 
addBorder(const CollapsedBorderValue & borderValue,BoxSide borderSide,bool shouldPaint,int x1,int y1,int x2,int y2,EBorderStyle borderStyle)698     void addBorder(const CollapsedBorderValue& borderValue, BoxSide borderSide, bool shouldPaint,
699                    int x1, int y1, int x2, int y2, EBorderStyle borderStyle)
700     {
701         if (borderValue.exists() && shouldPaint) {
702             m_borders[m_count].borderValue = borderValue;
703             m_borders[m_count].side = borderSide;
704             m_borders[m_count].shouldPaint = shouldPaint;
705             m_borders[m_count].x1 = x1;
706             m_borders[m_count].x2 = x2;
707             m_borders[m_count].y1 = y1;
708             m_borders[m_count].y2 = y2;
709             m_borders[m_count].style = borderStyle;
710             m_count++;
711         }
712     }
713 
nextBorder()714     CollapsedBorder* nextBorder()
715     {
716         for (int i = 0; i < m_count; i++) {
717             if (m_borders[i].borderValue.exists() && m_borders[i].shouldPaint) {
718                 m_borders[i].shouldPaint = false;
719                 return &m_borders[i];
720             }
721         }
722 
723         return 0;
724     }
725 
726     CollapsedBorder m_borders[4];
727     int m_count;
728 };
729 
addBorderStyle(RenderTableCell::CollapsedBorderStyles & borderStyles,CollapsedBorderValue borderValue)730 static void addBorderStyle(RenderTableCell::CollapsedBorderStyles& borderStyles, CollapsedBorderValue borderValue)
731 {
732     if (!borderValue.exists())
733         return;
734     size_t count = borderStyles.size();
735     for (size_t i = 0; i < count; ++i)
736         if (borderStyles[i] == borderValue)
737             return;
738     borderStyles.append(borderValue);
739 }
740 
collectBorderStyles(CollapsedBorderStyles & borderStyles) const741 void RenderTableCell::collectBorderStyles(CollapsedBorderStyles& borderStyles) const
742 {
743     bool rtl = table()->style()->direction() == RTL;
744     addBorderStyle(borderStyles, collapsedLeftBorder(rtl));
745     addBorderStyle(borderStyles, collapsedRightBorder(rtl));
746     addBorderStyle(borderStyles, collapsedTopBorder());
747     addBorderStyle(borderStyles, collapsedBottomBorder());
748 }
749 
compareBorderStylesForQSort(const void * pa,const void * pb)750 static int compareBorderStylesForQSort(const void* pa, const void* pb)
751 {
752     const CollapsedBorderValue* a = static_cast<const CollapsedBorderValue*>(pa);
753     const CollapsedBorderValue* b = static_cast<const CollapsedBorderValue*>(pb);
754     if (*a == *b)
755         return 0;
756     CollapsedBorderValue borderWithHigherPrecedence = compareBorders(*a, *b);
757 #ifdef ANDROID_FIX
758     if (*a == borderWithHigherPrecedence) {
759         // klibc uses a combsort for quicksort and requires that two values always give the same answer
760         // regardless of comparison order. Unfortunately, compareBorders does not honor that requirement.
761         // Call compareBorders again with reversed parameters. If it returns the first value again then
762         // we can assume the values are equal. http://bugs.webkit.org/show_bug.cgi?id=13147
763         CollapsedBorderValue qSortHack = compareBorders(*b, *a);
764         if (*b == qSortHack)
765             return 0;
766         return 1;
767     }
768 #else
769     if (*a == borderWithHigherPrecedence)
770         return 1;
771 #endif
772     return -1;
773 }
774 
sortBorderStyles(CollapsedBorderStyles & borderStyles)775 void RenderTableCell::sortBorderStyles(CollapsedBorderStyles& borderStyles)
776 {
777     qsort(borderStyles.data(), borderStyles.size(), sizeof(CollapsedBorderValue),
778         compareBorderStylesForQSort);
779 }
780 
paintCollapsedBorder(GraphicsContext * graphicsContext,int tx,int ty,int w,int h)781 void RenderTableCell::paintCollapsedBorder(GraphicsContext* graphicsContext, int tx, int ty, int w, int h)
782 {
783     if (!table()->currentBorderStyle())
784         return;
785 
786     bool rtl = table()->style()->direction() == RTL;
787     CollapsedBorderValue leftVal = collapsedLeftBorder(rtl);
788     CollapsedBorderValue rightVal = collapsedRightBorder(rtl);
789     CollapsedBorderValue topVal = collapsedTopBorder();
790     CollapsedBorderValue bottomVal = collapsedBottomBorder();
791 
792     // Adjust our x/y/width/height so that we paint the collapsed borders at the correct location.
793     int topWidth = topVal.width();
794     int bottomWidth = bottomVal.width();
795     int leftWidth = leftVal.width();
796     int rightWidth = rightVal.width();
797 
798     tx -= leftWidth / 2;
799     ty -= topWidth / 2;
800     w += leftWidth / 2 + (rightWidth + 1) / 2;
801     h += topWidth / 2 + (bottomWidth + 1) / 2;
802 
803     EBorderStyle topStyle = collapsedBorderStyle(topVal.style());
804     EBorderStyle bottomStyle = collapsedBorderStyle(bottomVal.style());
805     EBorderStyle leftStyle = collapsedBorderStyle(leftVal.style());
806     EBorderStyle rightStyle = collapsedBorderStyle(rightVal.style());
807 
808     bool renderTop = topStyle > BHIDDEN && !topVal.isTransparent();
809     bool renderBottom = bottomStyle > BHIDDEN && !bottomVal.isTransparent();
810     bool renderLeft = leftStyle > BHIDDEN && !leftVal.isTransparent();
811     bool renderRight = rightStyle > BHIDDEN && !rightVal.isTransparent();
812 
813     // We never paint diagonals at the joins.  We simply let the border with the highest
814     // precedence paint on top of borders with lower precedence.
815     CollapsedBorders borders;
816     borders.addBorder(topVal, BSTop, renderTop, tx, ty, tx + w, ty + topWidth, topStyle);
817     borders.addBorder(bottomVal, BSBottom, renderBottom, tx, ty + h - bottomWidth, tx + w, ty + h, bottomStyle);
818     borders.addBorder(leftVal, BSLeft, renderLeft, tx, ty, tx + leftWidth, ty + h, leftStyle);
819     borders.addBorder(rightVal, BSRight, renderRight, tx + w - rightWidth, ty, tx + w, ty + h, rightStyle);
820 
821     for (CollapsedBorder* border = borders.nextBorder(); border; border = borders.nextBorder()) {
822         if (border->borderValue == *table()->currentBorderStyle())
823             drawLineForBoxSide(graphicsContext, border->x1, border->y1, border->x2, border->y2, border->side,
824                                border->borderValue.color(), style()->color(), border->style, 0, 0);
825     }
826 }
827 
paintBackgroundsBehindCell(PaintInfo & paintInfo,int tx,int ty,RenderObject * backgroundObject)828 void RenderTableCell::paintBackgroundsBehindCell(PaintInfo& paintInfo, int tx, int ty, RenderObject* backgroundObject)
829 {
830     if (!backgroundObject)
831         return;
832 
833     if (style()->visibility() != VISIBLE)
834         return;
835 
836     RenderTable* tableElt = table();
837     if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
838         return;
839 
840     if (backgroundObject != this) {
841         tx += x();
842         ty += y();
843     }
844 
845     int w = width();
846     int h = height();
847 
848     Color c = backgroundObject->style()->backgroundColor();
849     const FillLayer* bgLayer = backgroundObject->style()->backgroundLayers();
850 
851     if (bgLayer->hasImage() || c.isValid()) {
852         // We have to clip here because the background would paint
853         // on top of the borders otherwise.  This only matters for cells and rows.
854         bool shouldClip = backgroundObject->hasLayer() && (backgroundObject == this || backgroundObject == parent()) && tableElt->collapseBorders();
855         if (shouldClip) {
856             IntRect clipRect(tx + borderLeft(), ty + borderTop(),
857                 w - borderLeft() - borderRight(), h - borderTop() - borderBottom());
858             paintInfo.context->save();
859             paintInfo.context->clip(clipRect);
860         }
861         paintFillLayers(paintInfo, c, bgLayer, tx, ty, w, h);
862         if (shouldClip)
863             paintInfo.context->restore();
864     }
865 }
866 
paintBoxDecorations(PaintInfo & paintInfo,int tx,int ty)867 void RenderTableCell::paintBoxDecorations(PaintInfo& paintInfo, int tx, int ty)
868 {
869     RenderTable* tableElt = table();
870     if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
871         return;
872 
873     int w = width();
874     int h = height();
875 
876     if (style()->boxShadow())
877         paintBoxShadow(paintInfo.context, tx, ty, w, h, style(), Normal);
878 
879     // Paint our cell background.
880     paintBackgroundsBehindCell(paintInfo, tx, ty, this);
881     if (style()->boxShadow())
882         paintBoxShadow(paintInfo.context, tx, ty, w, h, style(), Inset);
883 
884     if (!style()->hasBorder() || tableElt->collapseBorders())
885         return;
886 
887     paintBorder(paintInfo.context, tx, ty, w, h, style());
888 }
889 
paintMask(PaintInfo & paintInfo,int tx,int ty)890 void RenderTableCell::paintMask(PaintInfo& paintInfo, int tx, int ty)
891 {
892     if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask)
893         return;
894 
895     RenderTable* tableElt = table();
896     if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild())
897         return;
898 
899     int w = width();
900     int h = height();
901 
902     paintMaskImages(paintInfo, tx, ty, w, h);
903 }
904 
905 } // namespace WebCore
906