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 "CollapsedBorderValue.h" 29#include "FloatQuad.h" 30#include "GraphicsContext.h" 31#include "HTMLNames.h" 32#include "HTMLTableCellElement.h" 33#include "PaintInfo.h" 34#include "RenderTableCol.h" 35#include "RenderView.h" 36#include "StylePropertySet.h" 37#include "TransformState.h" 38#include <wtf/StackStats.h> 39 40#if ENABLE(MATHML) 41#include "MathMLElement.h" 42#include "MathMLNames.h" 43#endif 44 45using namespace std; 46 47namespace WebCore { 48 49using namespace HTMLNames; 50 51struct SameSizeAsRenderTableCell : public RenderBlock { 52 unsigned bitfields; 53 int paddings[2]; 54}; 55 56COMPILE_ASSERT(sizeof(RenderTableCell) == sizeof(SameSizeAsRenderTableCell), RenderTableCell_should_stay_small); 57COMPILE_ASSERT(sizeof(CollapsedBorderValue) == 8, CollapsedBorderValue_should_stay_small); 58 59RenderTableCell::RenderTableCell(Element* element) 60 : RenderBlock(element) 61 , m_column(unsetColumnIndex) 62 , m_cellWidthChanged(false) 63 , m_intrinsicPaddingBefore(0) 64 , m_intrinsicPaddingAfter(0) 65{ 66 // We only update the flags when notified of DOM changes in colSpanOrRowSpanChanged() 67 // so we need to set their initial values here in case something asks for colSpan()/rowSpan() before then. 68 updateColAndRowSpanFlags(); 69} 70 71void RenderTableCell::willBeRemovedFromTree() 72{ 73 RenderBlock::willBeRemovedFromTree(); 74 75 section()->setNeedsCellRecalc(); 76 section()->removeCachedCollapsedBorders(this); 77} 78 79unsigned RenderTableCell::parseColSpanFromDOM() const 80{ 81 ASSERT(node()); 82 if (node()->hasTagName(tdTag) || node()->hasTagName(thTag)) 83 return min<unsigned>(toHTMLTableCellElement(node())->colSpan(), maxColumnIndex); 84#if ENABLE(MATHML) 85 if (node()->hasTagName(MathMLNames::mtdTag)) 86 return min<unsigned>(toMathMLElement(node())->colSpan(), maxColumnIndex); 87#endif 88 return 1; 89} 90 91unsigned RenderTableCell::parseRowSpanFromDOM() const 92{ 93 ASSERT(node()); 94 if (node()->hasTagName(tdTag) || node()->hasTagName(thTag)) 95 return min<unsigned>(toHTMLTableCellElement(node())->rowSpan(), maxRowIndex); 96#if ENABLE(MATHML) 97 if (node()->hasTagName(MathMLNames::mtdTag)) 98 return min<unsigned>(toMathMLElement(node())->rowSpan(), maxRowIndex); 99#endif 100 return 1; 101} 102 103void RenderTableCell::updateColAndRowSpanFlags() 104{ 105 // The vast majority of table cells do not have a colspan or rowspan, 106 // so we keep a bool to know if we need to bother reading from the DOM. 107 m_hasColSpan = node() && parseColSpanFromDOM() != 1; 108 m_hasRowSpan = node() && parseRowSpanFromDOM() != 1; 109} 110 111void RenderTableCell::colSpanOrRowSpanChanged() 112{ 113 ASSERT(node()); 114#if ENABLE(MATHML) 115 ASSERT(node()->hasTagName(tdTag) || node()->hasTagName(thTag) || node()->hasTagName(MathMLNames::mtdTag)); 116#else 117 ASSERT(node()->hasTagName(tdTag) || node()->hasTagName(thTag)); 118#endif 119 120 updateColAndRowSpanFlags(); 121 122 // FIXME: I suspect that we could return early here if !m_hasColSpan && !m_hasRowSpan. 123 124 setNeedsLayoutAndPrefWidthsRecalc(); 125 if (parent() && section()) 126 section()->setNeedsCellRecalc(); 127} 128 129Length RenderTableCell::logicalWidthFromColumns(RenderTableCol* firstColForThisCell, Length widthFromStyle) const 130{ 131 ASSERT(firstColForThisCell && firstColForThisCell == table()->colElement(col())); 132 RenderTableCol* tableCol = firstColForThisCell; 133 134 unsigned colSpanCount = colSpan(); 135 int colWidthSum = 0; 136 for (unsigned i = 1; i <= colSpanCount; i++) { 137 Length colWidth = tableCol->style()->logicalWidth(); 138 139 // Percentage value should be returned only for colSpan == 1. 140 // Otherwise we return original width for the cell. 141 if (!colWidth.isFixed()) { 142 if (colSpanCount > 1) 143 return widthFromStyle; 144 return colWidth; 145 } 146 147 colWidthSum += colWidth.value(); 148 tableCol = tableCol->nextColumn(); 149 // If no next <col> tag found for the span we just return what we have for now. 150 if (!tableCol) 151 break; 152 } 153 154 // Column widths specified on <col> apply to the border box of the cell, see bug 8126. 155 // FIXME: Why is border/padding ignored in the negative width case? 156 if (colWidthSum > 0) 157 return Length(max(0, colWidthSum - borderAndPaddingLogicalWidth().ceil()), Fixed); 158 return Length(colWidthSum, Fixed); 159} 160 161void RenderTableCell::computePreferredLogicalWidths() 162{ 163 // 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 164 // cells are added, but relayout can cause the cells to be freed, leaving stale pointers in the sections' 165 // grids. We must refresh those grids before the child cells try to use them. 166 table()->recalcSectionsIfNeeded(); 167 168 RenderBlock::computePreferredLogicalWidths(); 169 if (node() && style()->autoWrap()) { 170 // See if nowrap was set. 171 Length w = styleOrColLogicalWidth(); 172 String nowrap = toElement(node())->getAttribute(nowrapAttr); 173 if (!nowrap.isNull() && w.isFixed()) 174 // Nowrap is set, but we didn't actually use it because of the 175 // fixed width set on the cell. Even so, it is a WinIE/Moz trait 176 // to make the minwidth of the cell into the fixed width. They do this 177 // even in strict mode, so do not make this a quirk. Affected the top 178 // of hiptop.com. 179 m_minPreferredLogicalWidth = max<LayoutUnit>(w.value(), m_minPreferredLogicalWidth); 180 } 181} 182 183void RenderTableCell::computeIntrinsicPadding(int rowHeight) 184{ 185 int oldIntrinsicPaddingBefore = intrinsicPaddingBefore(); 186 int oldIntrinsicPaddingAfter = intrinsicPaddingAfter(); 187 int logicalHeightWithoutIntrinsicPadding = pixelSnappedLogicalHeight() - oldIntrinsicPaddingBefore - oldIntrinsicPaddingAfter; 188 189 int intrinsicPaddingBefore = 0; 190 switch (style()->verticalAlign()) { 191 case SUB: 192 case SUPER: 193 case TEXT_TOP: 194 case TEXT_BOTTOM: 195 case LENGTH: 196 case BASELINE: { 197 LayoutUnit baseline = cellBaselinePosition(); 198 if (baseline > borderAndPaddingBefore()) 199 intrinsicPaddingBefore = section()->rowBaseline(rowIndex()) - (baseline - oldIntrinsicPaddingBefore); 200 break; 201 } 202 case TOP: 203 break; 204 case MIDDLE: 205 intrinsicPaddingBefore = (rowHeight - logicalHeightWithoutIntrinsicPadding) / 2; 206 break; 207 case BOTTOM: 208 intrinsicPaddingBefore = rowHeight - logicalHeightWithoutIntrinsicPadding; 209 break; 210 case BASELINE_MIDDLE: 211 break; 212 } 213 214 int intrinsicPaddingAfter = rowHeight - logicalHeightWithoutIntrinsicPadding - intrinsicPaddingBefore; 215 setIntrinsicPaddingBefore(intrinsicPaddingBefore); 216 setIntrinsicPaddingAfter(intrinsicPaddingAfter); 217 218 // FIXME: Changing an intrinsic padding shouldn't trigger a relayout as it only shifts the cell inside the row but 219 // doesn't change the logical height. 220 if (intrinsicPaddingBefore != oldIntrinsicPaddingBefore || intrinsicPaddingAfter != oldIntrinsicPaddingAfter) 221 setNeedsLayout(true, MarkOnlyThis); 222} 223 224void RenderTableCell::updateLogicalWidth() 225{ 226} 227 228void RenderTableCell::setCellLogicalWidth(int tableLayoutLogicalWidth) 229{ 230 if (tableLayoutLogicalWidth == logicalWidth()) 231 return; 232 233 setNeedsLayout(true, MarkOnlyThis); 234 row()->setChildNeedsLayout(true, MarkOnlyThis); 235 236 if (!table()->selfNeedsLayout() && checkForRepaintDuringLayout()) 237 repaint(); 238 239 setLogicalWidth(tableLayoutLogicalWidth); 240 setCellWidthChanged(true); 241} 242 243void RenderTableCell::layout() 244{ 245 StackStats::LayoutCheckPoint layoutCheckPoint; 246 updateFirstLetter(); 247 248 int oldCellBaseline = cellBaselinePosition(); 249 layoutBlock(cellWidthChanged()); 250 251 // 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 252 // 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 253 // 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 254 // table and row layout can use the correct baseline and height for this cell. 255 if (isBaselineAligned() && section()->rowBaseline(rowIndex()) && cellBaselinePosition() > section()->rowBaseline(rowIndex())) { 256 int newIntrinsicPaddingBefore = max<LayoutUnit>(0, intrinsicPaddingBefore() - max<LayoutUnit>(0, cellBaselinePosition() - oldCellBaseline)); 257 setIntrinsicPaddingBefore(newIntrinsicPaddingBefore); 258 setNeedsLayout(true, MarkOnlyThis); 259 layoutBlock(cellWidthChanged()); 260 } 261 262 setCellWidthChanged(false); 263} 264 265LayoutUnit RenderTableCell::paddingTop() const 266{ 267 int result = computedCSSPaddingTop(); 268 if (!isHorizontalWritingMode()) 269 return result; 270 return result + (style()->writingMode() == TopToBottomWritingMode ? intrinsicPaddingBefore() : intrinsicPaddingAfter()); 271} 272 273LayoutUnit RenderTableCell::paddingBottom() const 274{ 275 int result = computedCSSPaddingBottom(); 276 if (!isHorizontalWritingMode()) 277 return result; 278 return result + (style()->writingMode() == TopToBottomWritingMode ? intrinsicPaddingAfter() : intrinsicPaddingBefore()); 279} 280 281LayoutUnit RenderTableCell::paddingLeft() const 282{ 283 int result = computedCSSPaddingLeft(); 284 if (isHorizontalWritingMode()) 285 return result; 286 return result + (style()->writingMode() == LeftToRightWritingMode ? intrinsicPaddingBefore() : intrinsicPaddingAfter()); 287} 288 289LayoutUnit RenderTableCell::paddingRight() const 290{ 291 int result = computedCSSPaddingRight(); 292 if (isHorizontalWritingMode()) 293 return result; 294 return result + (style()->writingMode() == LeftToRightWritingMode ? intrinsicPaddingAfter() : intrinsicPaddingBefore()); 295} 296 297LayoutUnit RenderTableCell::paddingBefore() const 298{ 299 return static_cast<int>(computedCSSPaddingBefore()) + intrinsicPaddingBefore(); 300} 301 302LayoutUnit RenderTableCell::paddingAfter() const 303{ 304 return static_cast<int>(computedCSSPaddingAfter()) + intrinsicPaddingAfter(); 305} 306 307void RenderTableCell::setOverrideLogicalContentHeightFromRowHeight(LayoutUnit rowHeight) 308{ 309 clearIntrinsicPadding(); 310 setOverrideLogicalContentHeight(max<LayoutUnit>(0, rowHeight - borderAndPaddingLogicalHeight())); 311} 312 313LayoutSize RenderTableCell::offsetFromContainer(RenderObject* o, const LayoutPoint& point, bool* offsetDependsOnPoint) const 314{ 315 ASSERT(o == container()); 316 317 LayoutSize offset = RenderBlock::offsetFromContainer(o, point, offsetDependsOnPoint); 318 if (parent()) 319 offset -= parentBox()->locationOffset(); 320 321 return offset; 322} 323 324LayoutRect RenderTableCell::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const 325{ 326 // If the table grid is dirty, we cannot get reliable information about adjoining cells, 327 // so we ignore outside borders. This should not be a problem because it means that 328 // the table is going to recalculate the grid, relayout and repaint its current rect, which 329 // includes any outside borders of this cell. 330 if (!table()->collapseBorders() || table()->needsSectionRecalc()) 331 return RenderBlock::clippedOverflowRectForRepaint(repaintContainer); 332 333 bool rtl = !styleForCellFlow()->isLeftToRightDirection(); 334 int outlineSize = style()->outlineSize(); 335 int left = max(borderHalfLeft(true), outlineSize); 336 int right = max(borderHalfRight(true), outlineSize); 337 int top = max(borderHalfTop(true), outlineSize); 338 int bottom = max(borderHalfBottom(true), outlineSize); 339 if ((left && !rtl) || (right && rtl)) { 340 if (RenderTableCell* before = table()->cellBefore(this)) { 341 top = max(top, before->borderHalfTop(true)); 342 bottom = max(bottom, before->borderHalfBottom(true)); 343 } 344 } 345 if ((left && rtl) || (right && !rtl)) { 346 if (RenderTableCell* after = table()->cellAfter(this)) { 347 top = max(top, after->borderHalfTop(true)); 348 bottom = max(bottom, after->borderHalfBottom(true)); 349 } 350 } 351 if (top) { 352 if (RenderTableCell* above = table()->cellAbove(this)) { 353 left = max(left, above->borderHalfLeft(true)); 354 right = max(right, above->borderHalfRight(true)); 355 } 356 } 357 if (bottom) { 358 if (RenderTableCell* below = table()->cellBelow(this)) { 359 left = max(left, below->borderHalfLeft(true)); 360 right = max(right, below->borderHalfRight(true)); 361 } 362 } 363 LayoutPoint location(max<LayoutUnit>(left, -visualOverflowRect().x()), max<LayoutUnit>(top, -visualOverflowRect().y())); 364 LayoutRect r(-location.x(), -location.y(), location.x() + max(width() + right, visualOverflowRect().maxX()), location.y() + max(height() + bottom, visualOverflowRect().maxY())); 365 366 if (RenderView* v = view()) { 367 // FIXME: layoutDelta needs to be applied in parts before/after transforms and 368 // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308 369 r.move(v->layoutDelta()); 370 } 371 computeRectForRepaint(repaintContainer, r); 372 return r; 373} 374 375void RenderTableCell::computeRectForRepaint(const RenderLayerModelObject* repaintContainer, LayoutRect& r, bool fixed) const 376{ 377 if (repaintContainer == this) 378 return; 379 r.setY(r.y()); 380 RenderView* v = view(); 381 if ((!v || !v->layoutStateEnabled() || repaintContainer) && parent()) 382 r.moveBy(-parentBox()->location()); // Rows are in the same coordinate space, so don't add their offset in. 383 RenderBlock::computeRectForRepaint(repaintContainer, r, fixed); 384} 385 386LayoutUnit 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 borderAndPaddingBefore() + contentLogicalHeight(); 395} 396 397void RenderTableCell::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle) 398{ 399 ASSERT(style()->display() == TABLE_CELL); 400 ASSERT(!row() || row()->rowIndexWasSet()); 401 402 RenderBlock::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. 434static 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 477static 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 483bool 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 494bool 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 505CollapsedBorderValue 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 513CollapsedBorderValue 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 ? style()->visitedDependentColor(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->style()->visitedDependentColor(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()->style()->visitedDependentColor(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()->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(startColorProperty) : Color(), BTABLE)); 598 if (!result.exists()) 599 return result; 600 } 601 602 return result; 603} 604 605CollapsedBorderValue 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 613CollapsedBorderValue 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 ? style()->visitedDependentColor(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->style()->visitedDependentColor(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()->style()->visitedDependentColor(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()->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(endColorProperty) : Color(), BTABLE)); 700 if (!result.exists()) 701 return result; 702 } 703 704 return result; 705} 706 707CollapsedBorderValue 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 715CollapsedBorderValue 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 ? style()->visitedDependentColor(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->style()->visitedDependentColor(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()->style()->visitedDependentColor(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()->lastChild(); 745 746 if (prevRow) { 747 result = chooseBorder(CollapsedBorderValue(prevRow->style()->borderAfter(), includeColor ? prevRow->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(beforeColorProperty) : Color(), BTABLE)); 786 if (!result.exists()) 787 return result; 788 } 789 790 return result; 791} 792 793CollapsedBorderValue 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 801CollapsedBorderValue 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 ? style()->visitedDependentColor(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->style()->visitedDependentColor(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()->style()->visitedDependentColor(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()->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(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->style()->visitedDependentColor(afterColorProperty) : Color(), BTABLE)); 863 if (!result.exists()) 864 return result; 865 } 866 867 return result; 868} 869 870inline 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 877inline 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 884inline 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 891inline 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 898int RenderTableCell::borderLeft() const 899{ 900 return table()->collapseBorders() ? borderHalfLeft(false) : RenderBlock::borderLeft(); 901} 902 903int RenderTableCell::borderRight() const 904{ 905 return table()->collapseBorders() ? borderHalfRight(false) : RenderBlock::borderRight(); 906} 907 908int RenderTableCell::borderTop() const 909{ 910 return table()->collapseBorders() ? borderHalfTop(false) : RenderBlock::borderTop(); 911} 912 913int RenderTableCell::borderBottom() const 914{ 915 return table()->collapseBorders() ? borderHalfBottom(false) : RenderBlock::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. 920int RenderTableCell::borderStart() const 921{ 922 return table()->collapseBorders() ? borderHalfStart(false) : RenderBlock::borderStart(); 923} 924 925int RenderTableCell::borderEnd() const 926{ 927 return table()->collapseBorders() ? borderHalfEnd(false) : RenderBlock::borderEnd(); 928} 929 930int RenderTableCell::borderBefore() const 931{ 932 return table()->collapseBorders() ? borderHalfBefore(false) : RenderBlock::borderBefore(); 933} 934 935int RenderTableCell::borderAfter() const 936{ 937 return table()->collapseBorders() ? borderHalfAfter(false) : RenderBlock::borderAfter(); 938} 939 940int 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 948int 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 956int 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 964int 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 972int 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 980int 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 988int 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 996int 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 1004void RenderTableCell::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset) 1005{ 1006 ASSERT(paintInfo.phase != PaintPhaseCollapsedTableBorders); 1007 RenderBlock::paint(paintInfo, paintOffset); 1008} 1009 1010static EBorderStyle collapsedBorderStyle(EBorderStyle style) 1011{ 1012 if (style == OUTSET) 1013 return GROOVE; 1014 if (style == INSET) 1015 return RIDGE; 1016 return style; 1017} 1018 1019struct 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 1030class CollapsedBorders { 1031public: 1032 CollapsedBorders() 1033 : m_count(0) 1034 { 1035 } 1036 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 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 1069static 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 1081void 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 1089static 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 1098void RenderTableCell::sortBorderValues(RenderTable::CollapsedBorderValues& borderValues) 1099{ 1100 qsort(borderValues.data(), borderValues.size(), sizeof(CollapsedBorderValue), 1101 compareBorderValuesForQSort); 1102} 1103 1104void 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 localRepaintRect = paintInfo.rect; 1112 localRepaintRect.inflate(maximalOutlineSize(paintInfo.phase)); 1113 1114 LayoutRect paintRect = LayoutRect(paintOffset + location(), pixelSnappedSize()); 1115 if (paintRect.y() - table()->outerBorderTop() >= localRepaintRect.maxY()) 1116 return; 1117 1118 if (paintRect.maxY() + table()->outerBorderBottom() <= localRepaintRect.y()) 1119 return; 1120 1121 GraphicsContext* graphicsContext = paintInfo.context; 1122 if (!table()->currentBorderValue() || graphicsContext->paintingDisabled()) 1123 return; 1124 1125 const RenderStyle* styleForCellFlow = this->styleForCellFlow(); 1126 CollapsedBorderValue leftVal = cachedCollapsedLeftBorder(styleForCellFlow); 1127 CollapsedBorderValue rightVal = cachedCollapsedRightBorder(styleForCellFlow); 1128 CollapsedBorderValue topVal = cachedCollapsedTopBorder(styleForCellFlow); 1129 CollapsedBorderValue bottomVal = cachedCollapsedBottomBorder(styleForCellFlow); 1130 1131 // Adjust our x/y/width/height so that we paint the collapsed borders at the correct location. 1132 int topWidth = topVal.width(); 1133 int bottomWidth = bottomVal.width(); 1134 int leftWidth = leftVal.width(); 1135 int rightWidth = rightVal.width(); 1136 1137 IntRect borderRect = pixelSnappedIntRect(paintRect.x() - leftWidth / 2, 1138 paintRect.y() - topWidth / 2, 1139 paintRect.width() + leftWidth / 2 + (rightWidth + 1) / 2, 1140 paintRect.height() + topWidth / 2 + (bottomWidth + 1) / 2); 1141 1142 EBorderStyle topStyle = collapsedBorderStyle(topVal.style()); 1143 EBorderStyle bottomStyle = collapsedBorderStyle(bottomVal.style()); 1144 EBorderStyle leftStyle = collapsedBorderStyle(leftVal.style()); 1145 EBorderStyle rightStyle = collapsedBorderStyle(rightVal.style()); 1146 1147 bool renderTop = topStyle > BHIDDEN && !topVal.isTransparent(); 1148 bool renderBottom = bottomStyle > BHIDDEN && !bottomVal.isTransparent(); 1149 bool renderLeft = leftStyle > BHIDDEN && !leftVal.isTransparent(); 1150 bool renderRight = rightStyle > BHIDDEN && !rightVal.isTransparent(); 1151 1152 // We never paint diagonals at the joins. We simply let the border with the highest 1153 // precedence paint on top of borders with lower precedence. 1154 CollapsedBorders borders; 1155 borders.addBorder(topVal, BSTop, renderTop, borderRect.x(), borderRect.y(), borderRect.maxX(), borderRect.y() + topWidth, topStyle); 1156 borders.addBorder(bottomVal, BSBottom, renderBottom, borderRect.x(), borderRect.maxY() - bottomWidth, borderRect.maxX(), borderRect.maxY(), bottomStyle); 1157 borders.addBorder(leftVal, BSLeft, renderLeft, borderRect.x(), borderRect.y(), borderRect.x() + leftWidth, borderRect.maxY(), leftStyle); 1158 borders.addBorder(rightVal, BSRight, renderRight, borderRect.maxX() - rightWidth, borderRect.y(), borderRect.maxX(), borderRect.maxY(), rightStyle); 1159 1160 bool antialias = shouldAntialiasLines(graphicsContext); 1161 1162 for (CollapsedBorder* border = borders.nextBorder(); border; border = borders.nextBorder()) { 1163 if (border->borderValue.isSameIgnoringColor(*table()->currentBorderValue())) 1164 drawLineForBoxSide(graphicsContext, border->x1, border->y1, border->x2, border->y2, border->side, 1165 border->borderValue.color(), border->style, 0, 0, antialias); 1166 } 1167} 1168 1169void RenderTableCell::paintBackgroundsBehindCell(PaintInfo& paintInfo, const LayoutPoint& paintOffset, RenderObject* backgroundObject) 1170{ 1171 if (!paintInfo.shouldPaintWithinRoot(this)) 1172 return; 1173 1174 if (!backgroundObject) 1175 return; 1176 1177 if (style()->visibility() != VISIBLE) 1178 return; 1179 1180 RenderTable* tableElt = table(); 1181 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild()) 1182 return; 1183 1184 LayoutPoint adjustedPaintOffset = paintOffset; 1185 if (backgroundObject != this) 1186 adjustedPaintOffset.moveBy(location()); 1187 1188 Color c = backgroundObject->style()->visitedDependentColor(CSSPropertyBackgroundColor); 1189 const FillLayer* bgLayer = backgroundObject->style()->backgroundLayers(); 1190 1191 if (bgLayer->hasImage() || c.isValid()) { 1192 // We have to clip here because the background would paint 1193 // on top of the borders otherwise. This only matters for cells and rows. 1194 bool shouldClip = backgroundObject->hasLayer() && (backgroundObject == this || backgroundObject == parent()) && tableElt->collapseBorders(); 1195 GraphicsContextStateSaver stateSaver(*paintInfo.context, shouldClip); 1196 if (shouldClip) { 1197 LayoutRect clipRect(adjustedPaintOffset.x() + borderLeft(), adjustedPaintOffset.y() + borderTop(), 1198 width() - borderLeft() - borderRight(), height() - borderTop() - borderBottom()); 1199 paintInfo.context->clip(clipRect); 1200 } 1201 paintFillLayers(paintInfo, c, bgLayer, LayoutRect(adjustedPaintOffset, pixelSnappedSize()), BackgroundBleedNone, CompositeSourceOver, backgroundObject); 1202 } 1203} 1204 1205void RenderTableCell::paintBoxDecorations(PaintInfo& paintInfo, const LayoutPoint& paintOffset) 1206{ 1207 if (!paintInfo.shouldPaintWithinRoot(this)) 1208 return; 1209 1210 RenderTable* tableElt = table(); 1211 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild()) 1212 return; 1213 1214 LayoutRect paintRect = LayoutRect(paintOffset, pixelSnappedSize()); 1215 paintBoxShadow(paintInfo, paintRect, style(), Normal); 1216 1217 // Paint our cell background. 1218 paintBackgroundsBehindCell(paintInfo, paintOffset, this); 1219 1220 paintBoxShadow(paintInfo, paintRect, style(), Inset); 1221 1222 if (!style()->hasBorder() || tableElt->collapseBorders()) 1223 return; 1224 1225 paintBorder(paintInfo, paintRect, style()); 1226} 1227 1228void RenderTableCell::paintMask(PaintInfo& paintInfo, const LayoutPoint& paintOffset) 1229{ 1230 if (style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask) 1231 return; 1232 1233 RenderTable* tableElt = table(); 1234 if (!tableElt->collapseBorders() && style()->emptyCells() == HIDE && !firstChild()) 1235 return; 1236 1237 paintMaskImages(paintInfo, LayoutRect(paintOffset, pixelSnappedSize())); 1238} 1239 1240bool RenderTableCell::boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance, InlineFlowBox*) const 1241{ 1242 return false; 1243} 1244 1245void RenderTableCell::scrollbarsChanged(bool horizontalScrollbarChanged, bool verticalScrollbarChanged) 1246{ 1247 LayoutUnit scrollbarHeight = scrollbarLogicalHeight(); 1248 if (!scrollbarHeight) 1249 return; // Not sure if we should be doing something when a scrollbar goes away or not. 1250 1251 // We only care if the scrollbar that affects our intrinsic padding has been added. 1252 if ((isHorizontalWritingMode() && !horizontalScrollbarChanged) || 1253 (!isHorizontalWritingMode() && !verticalScrollbarChanged)) 1254 return; 1255 1256 // Shrink our intrinsic padding as much as possible to accommodate the scrollbar. 1257 if (style()->verticalAlign() == MIDDLE) { 1258 LayoutUnit totalHeight = logicalHeight(); 1259 LayoutUnit heightWithoutIntrinsicPadding = totalHeight - intrinsicPaddingBefore() - intrinsicPaddingAfter(); 1260 totalHeight -= scrollbarHeight; 1261 LayoutUnit newBeforePadding = (totalHeight - heightWithoutIntrinsicPadding) / 2; 1262 LayoutUnit newAfterPadding = totalHeight - heightWithoutIntrinsicPadding - newBeforePadding; 1263 setIntrinsicPaddingBefore(newBeforePadding); 1264 setIntrinsicPaddingAfter(newAfterPadding); 1265 } else 1266 setIntrinsicPaddingAfter(intrinsicPaddingAfter() - scrollbarHeight); 1267} 1268 1269RenderTableCell* RenderTableCell::createAnonymous(Document* document) 1270{ 1271 RenderTableCell* renderer = new (document->renderArena()) RenderTableCell(0); 1272 renderer->setDocumentForAnonymous(document); 1273 return renderer; 1274} 1275 1276RenderTableCell* RenderTableCell::createAnonymousWithParentRenderer(const RenderObject* parent) 1277{ 1278 RenderTableCell* newCell = RenderTableCell::createAnonymous(parent->document()); 1279 RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(parent->style(), TABLE_CELL); 1280 newCell->setStyle(newStyle.release()); 1281 return newCell; 1282} 1283 1284} // namespace WebCore 1285