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, 2008 Apple Inc. All rights reserved.
8 * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
19 *
20 * You should have received a copy of the GNU Library General Public License
21 * along with this library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
26 #include "config.h"
27 #include "RenderTableSection.h"
28
29 #include "CachedImage.h"
30 #include "Document.h"
31 #include "HTMLNames.h"
32 #include "RenderTableCell.h"
33 #include "RenderTableCol.h"
34 #include "RenderTableRow.h"
35 #include "RenderView.h"
36 #include <limits>
37 #include <wtf/Vector.h>
38 #ifdef ANDROID_LAYOUT
39 #include "Frame.h"
40 #include "Settings.h"
41 #endif
42
43 using namespace std;
44
45 namespace WebCore {
46
47 using namespace HTMLNames;
48
RenderTableSection(Node * node)49 RenderTableSection::RenderTableSection(Node* node)
50 : RenderContainer(node)
51 , m_gridRows(0)
52 , m_cCol(0)
53 , m_cRow(-1)
54 , m_needsCellRecalc(false)
55 , m_outerBorderLeft(0)
56 , m_outerBorderRight(0)
57 , m_outerBorderTop(0)
58 , m_outerBorderBottom(0)
59 , m_overflowLeft(0)
60 , m_overflowWidth(0)
61 , m_overflowTop(0)
62 , m_overflowHeight(0)
63 , m_hasOverflowingCell(false)
64 {
65 // init RenderObject attributes
66 setInline(false); // our object is not Inline
67 }
68
~RenderTableSection()69 RenderTableSection::~RenderTableSection()
70 {
71 clearGrid();
72 }
73
destroy()74 void RenderTableSection::destroy()
75 {
76 RenderTable* recalcTable = table();
77
78 RenderContainer::destroy();
79
80 // recalc cell info because RenderTable has unguarded pointers
81 // stored that point to this RenderTableSection.
82 if (recalcTable)
83 recalcTable->setNeedsSectionRecalc();
84 }
85
addChild(RenderObject * child,RenderObject * beforeChild)86 void RenderTableSection::addChild(RenderObject* child, RenderObject* beforeChild)
87 {
88 // Make sure we don't append things after :after-generated content if we have it.
89 if (!beforeChild && isAfterContent(lastChild()))
90 beforeChild = lastChild();
91
92 bool isTableSection = element() && (element()->hasTagName(theadTag) || element()->hasTagName(tbodyTag) || element()->hasTagName(tfootTag));
93
94 if (!child->isTableRow()) {
95 if (isTableSection && child->element() && child->element()->hasTagName(formTag) && document()->isHTMLDocument()) {
96 RenderContainer::addChild(child, beforeChild);
97 return;
98 }
99
100 RenderObject* last = beforeChild;
101 if (!last)
102 last = lastChild();
103 if (last && last->isAnonymous()) {
104 last->addChild(child);
105 return;
106 }
107
108 // If beforeChild is inside an anonymous cell/row, insert into the cell or into
109 // the anonymous row containing it, if there is one.
110 RenderObject* lastBox = last;
111 while (lastBox && lastBox->parent()->isAnonymous() && !lastBox->isTableRow())
112 lastBox = lastBox->parent();
113 if (lastBox && lastBox->isAnonymous()) {
114 lastBox->addChild(child, beforeChild);
115 return;
116 }
117
118 RenderObject* row = new (renderArena()) RenderTableRow(document() /* anonymous table */);
119 RefPtr<RenderStyle> newStyle = RenderStyle::create();
120 newStyle->inheritFrom(style());
121 newStyle->setDisplay(TABLE_ROW);
122 row->setStyle(newStyle.release());
123 addChild(row, beforeChild);
124 row->addChild(child);
125 return;
126 }
127
128 if (beforeChild)
129 setNeedsCellRecalc();
130
131 ++m_cRow;
132 m_cCol = 0;
133
134 // make sure we have enough rows
135 if (!ensureRows(m_cRow + 1))
136 return;
137
138 m_grid[m_cRow].rowRenderer = static_cast<RenderTableRow*>(child);
139
140 if (!beforeChild) {
141 m_grid[m_cRow].height = child->style()->height();
142 if (m_grid[m_cRow].height.isRelative())
143 m_grid[m_cRow].height = Length();
144 }
145
146 // If the next renderer is actually wrapped in an anonymous table row, we need to go up and find that.
147 while (beforeChild && beforeChild->parent() != this)
148 beforeChild = beforeChild->parent();
149
150 ASSERT(!beforeChild || beforeChild->isTableRow() || isTableSection && beforeChild->element() && beforeChild->element()->hasTagName(formTag) && document()->isHTMLDocument());
151 RenderContainer::addChild(child, beforeChild);
152 }
153
ensureRows(int numRows)154 bool RenderTableSection::ensureRows(int numRows)
155 {
156 int nRows = m_gridRows;
157 if (numRows > nRows) {
158 if (numRows > static_cast<int>(m_grid.size())) {
159 size_t maxSize = numeric_limits<size_t>::max() / sizeof(RowStruct);
160 if (static_cast<size_t>(numRows) > maxSize)
161 return false;
162 m_grid.grow(numRows);
163 }
164 m_gridRows = numRows;
165 int nCols = max(1, table()->numEffCols());
166 CellStruct emptyCellStruct;
167 emptyCellStruct.cell = 0;
168 emptyCellStruct.inColSpan = false;
169 for (int r = nRows; r < numRows; r++) {
170 m_grid[r].row = new Row(nCols);
171 m_grid[r].row->fill(emptyCellStruct);
172 m_grid[r].rowRenderer = 0;
173 m_grid[r].baseline = 0;
174 m_grid[r].height = Length();
175 }
176 }
177
178 return true;
179 }
180
addCell(RenderTableCell * cell,RenderTableRow * row)181 void RenderTableSection::addCell(RenderTableCell* cell, RenderTableRow* row)
182 {
183 int rSpan = cell->rowSpan();
184 int cSpan = cell->colSpan();
185 Vector<RenderTable::ColumnStruct>& columns = table()->columns();
186 int nCols = columns.size();
187
188 // ### mozilla still seems to do the old HTML way, even for strict DTD
189 // (see the annotation on table cell layouting in the CSS specs and the testcase below:
190 // <TABLE border>
191 // <TR><TD>1 <TD rowspan="2">2 <TD>3 <TD>4
192 // <TR><TD colspan="2">5
193 // </TABLE>
194
195 while (m_cCol < nCols && (cellAt(m_cRow, m_cCol).cell || cellAt(m_cRow, m_cCol).inColSpan))
196 m_cCol++;
197
198 if (rSpan == 1) {
199 // we ignore height settings on rowspan cells
200 Length height = cell->style()->height();
201 if (height.isPositive() || (height.isRelative() && height.value() >= 0)) {
202 Length cRowHeight = m_grid[m_cRow].height;
203 switch (height.type()) {
204 case Percent:
205 if (!(cRowHeight.isPercent()) ||
206 (cRowHeight.isPercent() && cRowHeight.rawValue() < height.rawValue()))
207 m_grid[m_cRow].height = height;
208 break;
209 case Fixed:
210 if (cRowHeight.type() < Percent ||
211 (cRowHeight.isFixed() && cRowHeight.value() < height.value()))
212 m_grid[m_cRow].height = height;
213 break;
214 case Relative:
215 default:
216 break;
217 }
218 }
219 }
220
221 // make sure we have enough rows
222 if (!ensureRows(m_cRow + rSpan))
223 return;
224
225 m_grid[m_cRow].rowRenderer = row;
226
227 int col = m_cCol;
228 // tell the cell where it is
229 CellStruct currentCell;
230 currentCell.cell = cell;
231 currentCell.inColSpan = false;
232 while (cSpan) {
233 int currentSpan;
234 if (m_cCol >= nCols) {
235 table()->appendColumn(cSpan);
236 currentSpan = cSpan;
237 } else {
238 if (cSpan < columns[m_cCol].span)
239 table()->splitColumn(m_cCol, cSpan);
240 currentSpan = columns[m_cCol].span;
241 }
242
243 for (int r = 0; r < rSpan; r++) {
244 CellStruct& c = cellAt(m_cRow + r, m_cCol);
245 if (currentCell.cell && !c.cell)
246 c.cell = currentCell.cell;
247 if (currentCell.inColSpan)
248 c.inColSpan = true;
249 }
250 m_cCol++;
251 cSpan -= currentSpan;
252 currentCell.cell = 0;
253 currentCell.inColSpan = true;
254 }
255 if (cell) {
256 cell->setRow(m_cRow);
257 cell->setCol(table()->effColToCol(col));
258 }
259 }
260
setCellWidths()261 void RenderTableSection::setCellWidths()
262 {
263 Vector<int>& columnPos = table()->columnPositions();
264
265 LayoutStateMaintainer statePusher(view());
266
267 #ifdef ANDROID_LAYOUT
268 int visibleWidth = 0;
269 if (view()->frameView()) {
270 const Settings* settings = document()->settings();
271 ASSERT(settings);
272 if (settings->layoutAlgorithm() == Settings::kLayoutFitColumnToScreen) {
273 visibleWidth = view()->frameView()->screenWidth();
274 }
275 }
276 #endif
277
278 for (int i = 0; i < m_gridRows; i++) {
279 Row& row = *m_grid[i].row;
280 int cols = row.size();
281 for (int j = 0; j < cols; j++) {
282 CellStruct current = row[j];
283 RenderTableCell* cell = current.cell;
284
285 if (!cell)
286 continue;
287 int endCol = j;
288 int cspan = cell->colSpan();
289 while (cspan && endCol < cols) {
290 cspan -= table()->columns()[endCol].span;
291 endCol++;
292 }
293 int w = columnPos[endCol] - columnPos[j] - table()->hBorderSpacing();
294 #ifdef ANDROID_LAYOUT
295 if (table()->isSingleColumn())
296 w = table()->width()-(table()->borderLeft()+table()->borderRight()+
297 (table()->collapseBorders()?0:(table()->paddingLeft()+table()->paddingRight()+
298 2*table()->hBorderSpacing())));
299 #endif
300 int oldWidth = cell->width();
301 #ifdef ANDROID_LAYOUT
302 if (w != oldWidth || (visibleWidth > 0 && visibleWidth != cell->getVisibleWidth())) {
303 #else
304 if (w != oldWidth) {
305 #endif
306 cell->setNeedsLayout(true);
307 if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout()) {
308 if (!statePusher.didPush()) {
309 // Technically, we should also push state for the row, but since
310 // rows don't push a coordinate transform, that's not necessary.
311 statePusher.push(this, IntSize(x(), y()));
312 }
313 cell->repaint();
314 }
315 #ifdef ANDROID_LAYOUT
316 if (w != oldWidth)
317 #endif
318 cell->updateWidth(w);
319 }
320 }
321 }
322
323 statePusher.pop(); // only pops if we pushed
324 }
325
326 int RenderTableSection::calcRowHeight()
327 {
328 #ifndef NDEBUG
329 setNeedsLayoutIsForbidden(true);
330 #endif
331
332 ASSERT(!needsLayout());
333 #ifdef ANDROID_LAYOUT
334 if (table()->isSingleColumn())
335 return m_rowPos[m_gridRows];
336 #endif
337
338 RenderTableCell* cell;
339
340 int spacing = table()->vBorderSpacing();
341
342 LayoutStateMaintainer statePusher(view());
343
344 m_rowPos.resize(m_gridRows + 1);
345 m_rowPos[0] = spacing;
346
347 for (int r = 0; r < m_gridRows; r++) {
348 m_rowPos[r + 1] = 0;
349 m_grid[r].baseline = 0;
350 int baseline = 0;
351 int bdesc = 0;
352 int ch = m_grid[r].height.calcMinValue(0);
353 int pos = m_rowPos[r] + ch + (m_grid[r].rowRenderer ? spacing : 0);
354
355 m_rowPos[r + 1] = max(m_rowPos[r + 1], pos);
356
357 Row* row = m_grid[r].row;
358 int totalCols = row->size();
359
360 for (int c = 0; c < totalCols; c++) {
361 CellStruct current = cellAt(r, c);
362 cell = current.cell;
363 if (!cell || current.inColSpan)
364 continue;
365 if (r < m_gridRows - 1 && cellAt(r + 1, c).cell == cell)
366 continue;
367
368 int indx = max(r - cell->rowSpan() + 1, 0);
369
370 if (cell->overrideSize() != -1) {
371 if (!statePusher.didPush()) {
372 // Technically, we should also push state for the row, but since
373 // rows don't push a coordinate transform, that's not necessary.
374 statePusher.push(this, IntSize(x(), y()));
375 }
376 cell->setOverrideSize(-1);
377 cell->setChildNeedsLayout(true, false);
378 cell->layoutIfNeeded();
379 }
380
381 int adjustedPaddingTop = cell->paddingTop() - cell->intrinsicPaddingTop();
382 int adjustedPaddingBottom = cell->paddingBottom() - cell->intrinsicPaddingBottom();
383 int adjustedHeight = cell->height() - (cell->intrinsicPaddingTop() + cell->intrinsicPaddingBottom());
384
385 // Explicit heights use the border box in quirks mode. In strict mode do the right
386 // thing and actually add in the border and padding.
387 ch = cell->style()->height().calcValue(0) +
388 (cell->style()->htmlHacks() ? 0 : (adjustedPaddingTop + adjustedPaddingBottom +
389 cell->borderTop() + cell->borderBottom()));
390 ch = max(ch, adjustedHeight);
391
392 pos = m_rowPos[indx] + ch + (m_grid[r].rowRenderer ? spacing : 0);
393
394 m_rowPos[r + 1] = max(m_rowPos[r + 1], pos);
395
396 // find out the baseline
397 EVerticalAlign va = cell->style()->verticalAlign();
398 if (va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB) {
399 int b = cell->baselinePosition();
400 if (b > cell->borderTop() + cell->paddingTop()) {
401 baseline = max(baseline, b - cell->intrinsicPaddingTop());
402 bdesc = max(bdesc, m_rowPos[indx] + ch - (b - cell->intrinsicPaddingTop()));
403 }
404 }
405 }
406
407 //do we have baseline aligned elements?
408 if (baseline) {
409 // increase rowheight if baseline requires
410 m_rowPos[r + 1] = max(m_rowPos[r + 1], baseline + bdesc + (m_grid[r].rowRenderer ? spacing : 0));
411 m_grid[r].baseline = baseline;
412 }
413
414 m_rowPos[r + 1] = max(m_rowPos[r + 1], m_rowPos[r]);
415 }
416
417 #ifndef NDEBUG
418 setNeedsLayoutIsForbidden(false);
419 #endif
420
421 ASSERT(!needsLayout());
422
423 statePusher.pop();
424
425 return m_rowPos[m_gridRows];
426 }
427
428 int RenderTableSection::layoutRows(int toAdd)
429 {
430 #ifndef NDEBUG
431 setNeedsLayoutIsForbidden(true);
432 #endif
433
434 ASSERT(!needsLayout());
435 #ifdef ANDROID_LAYOUT
436 if (table()->isSingleColumn()) {
437 int totalRows = m_gridRows;
438 int hspacing = table()->hBorderSpacing();
439 int vspacing = table()->vBorderSpacing();
440 int rHeight = vspacing;
441
442 int leftOffset = hspacing;
443
444 int nEffCols = table()->numEffCols();
445 for (int r = 0; r < totalRows; r++) {
446 for (int c = 0; c < nEffCols; c++) {
447 CellStruct current = cellAt(r, c);
448 RenderTableCell* cell = current.cell;
449
450 if (!cell || current.inColSpan)
451 continue;
452 if (r > 0 && (cellAt(r-1, c).cell == cell))
453 continue;
454
455 // cell->setCellTopExtra(0);
456 // cell->setCellBottomExtra(0);
457
458 int oldCellX = cell->x();
459 int oldCellY = cell->y();
460
461 if (style()->direction() == RTL) {
462 cell->setX(table()->width());
463 cell->setY(rHeight);
464 } else {
465 cell->setX(leftOffset);
466 cell->setY(rHeight);
467 }
468
469 // If the cell moved, we have to repaint it as well as any floating/positioned
470 // descendants. An exception is if we need a layout. In this case, we know we're going to
471 // repaint ourselves (and the cell) anyway.
472 if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout()) {
473 // IntRect cellRect(oldCellX, oldCellY - cell->borderTopExtra() , cell->width(), cell->height());
474 IntRect cellRect(oldCellX, oldCellY, cell->width(), cell->height());
475 cell->repaintDuringLayoutIfMoved(cellRect);
476 }
477 rHeight += cell->height() + vspacing;
478 }
479 }
480
481 setHeight(rHeight);
482 return height();
483 }
484 #endif
485
486 int rHeight;
487 int rindx;
488 int totalRows = m_gridRows;
489
490 // Set the width of our section now. The rows will also be this width.
491 setWidth(table()->contentWidth());
492 m_overflowLeft = 0;
493 m_overflowWidth = width();
494 m_overflowTop = 0;
495 m_overflowHeight = 0;
496 m_hasOverflowingCell = false;
497
498 if (toAdd && totalRows && (m_rowPos[totalRows] || !nextSibling())) {
499 int totalHeight = m_rowPos[totalRows] + toAdd;
500
501 int dh = toAdd;
502 int totalPercent = 0;
503 int numAuto = 0;
504 for (int r = 0; r < totalRows; r++) {
505 if (m_grid[r].height.isAuto())
506 numAuto++;
507 else if (m_grid[r].height.isPercent())
508 totalPercent += m_grid[r].height.rawValue();
509 }
510 if (totalPercent) {
511 // try to satisfy percent
512 int add = 0;
513 totalPercent = min(totalPercent, 100 * percentScaleFactor);
514 int rh = m_rowPos[1] - m_rowPos[0];
515 for (int r = 0; r < totalRows; r++) {
516 if (totalPercent > 0 && m_grid[r].height.isPercent()) {
517 int toAdd = min(dh, (totalHeight * m_grid[r].height.rawValue() / (100 * percentScaleFactor)) - rh);
518 // If toAdd is negative, then we don't want to shrink the row (this bug
519 // affected Outlook Web Access).
520 toAdd = max(0, toAdd);
521 add += toAdd;
522 dh -= toAdd;
523 totalPercent -= m_grid[r].height.rawValue();
524 }
525 if (r < totalRows - 1)
526 rh = m_rowPos[r + 2] - m_rowPos[r + 1];
527 m_rowPos[r + 1] += add;
528 }
529 }
530 if (numAuto) {
531 // distribute over variable cols
532 int add = 0;
533 for (int r = 0; r < totalRows; r++) {
534 if (numAuto > 0 && m_grid[r].height.isAuto()) {
535 int toAdd = dh / numAuto;
536 add += toAdd;
537 dh -= toAdd;
538 numAuto--;
539 }
540 m_rowPos[r + 1] += add;
541 }
542 }
543 if (dh > 0 && m_rowPos[totalRows]) {
544 // if some left overs, distribute equally.
545 int tot = m_rowPos[totalRows];
546 int add = 0;
547 int prev = m_rowPos[0];
548 for (int r = 0; r < totalRows; r++) {
549 //weight with the original height
550 add += dh * (m_rowPos[r + 1] - prev) / tot;
551 prev = m_rowPos[r + 1];
552 m_rowPos[r + 1] += add;
553 }
554 }
555 }
556
557 int hspacing = table()->hBorderSpacing();
558 int vspacing = table()->vBorderSpacing();
559 int nEffCols = table()->numEffCols();
560
561 LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()));
562
563 for (int r = 0; r < totalRows; r++) {
564 // Set the row's x/y position and width/height.
565 if (RenderTableRow* rowRenderer = m_grid[r].rowRenderer) {
566 rowRenderer->setLocation(0, m_rowPos[r]);
567 rowRenderer->setWidth(width());
568 rowRenderer->setHeight(m_rowPos[r + 1] - m_rowPos[r] - vspacing);
569 }
570
571 for (int c = 0; c < nEffCols; c++) {
572 RenderTableCell* cell = cellAt(r, c).cell;
573
574 if (!cell)
575 continue;
576 if (r < totalRows - 1 && cell == cellAt(r + 1, c).cell)
577 continue;
578
579 rindx = max(0, r - cell->rowSpan() + 1);
580
581 rHeight = m_rowPos[r + 1] - m_rowPos[rindx] - vspacing;
582
583 // Force percent height children to lay themselves out again.
584 // This will cause these children to grow to fill the cell.
585 // FIXME: There is still more work to do here to fully match WinIE (should
586 // it become necessary to do so). In quirks mode, WinIE behaves like we
587 // do, but it will clip the cells that spill out of the table section. In
588 // strict mode, Mozilla and WinIE both regrow the table to accommodate the
589 // new height of the cell (thus letting the percentages cause growth one
590 // time only). We may also not be handling row-spanning cells correctly.
591 //
592 // Note also the oddity where replaced elements always flex, and yet blocks/tables do
593 // not necessarily flex. WinIE is crazy and inconsistent, and we can't hope to
594 // match the behavior perfectly, but we'll continue to refine it as we discover new
595 // bugs. :)
596 bool cellChildrenFlex = false;
597 bool flexAllChildren = cell->style()->height().isFixed() ||
598 (!table()->style()->height().isAuto() && rHeight != cell->height());
599
600 for (RenderObject* o = cell->firstChild(); o; o = o->nextSibling()) {
601 if (!o->isText() && o->style()->height().isPercent() && (o->isReplaced() || (o->isBox() && toRenderBox(o)->scrollsOverflow()) || flexAllChildren)) {
602 // Tables with no sections do not flex.
603 if (!o->isTable() || static_cast<RenderTable*>(o)->hasSections()) {
604 o->setNeedsLayout(true, false);
605 cell->setChildNeedsLayout(true, false);
606 cellChildrenFlex = true;
607 }
608 }
609 }
610
611 if (cellChildrenFlex) {
612 // Alignment within a cell is based off the calculated
613 // height, which becomes irrelevant once the cell has
614 // been resized based off its percentage.
615 cell->setOverrideSize(max(0,
616 rHeight - cell->borderTop() - cell->paddingTop() -
617 cell->borderBottom() - cell->paddingBottom()));
618 cell->layoutIfNeeded();
619
620 // If the baseline moved, we may have to update the data for our row. Find out the new baseline.
621 EVerticalAlign va = cell->style()->verticalAlign();
622 if (va == BASELINE || va == TEXT_BOTTOM || va == TEXT_TOP || va == SUPER || va == SUB) {
623 int b = cell->baselinePosition();
624 if (b > cell->borderTop() + cell->paddingTop())
625 m_grid[r].baseline = max(m_grid[r].baseline, b);
626 }
627 }
628
629 int oldTe = cell->intrinsicPaddingTop();
630 int oldBe = cell->intrinsicPaddingBottom();
631 int heightWithoutIntrinsicPadding = cell->height() - oldTe - oldBe;
632
633 int te = 0;
634 switch (cell->style()->verticalAlign()) {
635 case SUB:
636 case SUPER:
637 case TEXT_TOP:
638 case TEXT_BOTTOM:
639 case BASELINE: {
640 int b = cell->baselinePosition();
641 if (b > cell->borderTop() + cell->paddingTop())
642 te = getBaseline(r) - (b - oldTe);
643 break;
644 }
645 case TOP:
646 te = 0;
647 break;
648 case MIDDLE:
649 te = (rHeight - heightWithoutIntrinsicPadding) / 2;
650 break;
651 case BOTTOM:
652 te = rHeight - heightWithoutIntrinsicPadding;
653 break;
654 default:
655 break;
656 }
657
658 int be = rHeight - heightWithoutIntrinsicPadding - te;
659 cell->setIntrinsicPaddingTop(te);
660 cell->setIntrinsicPaddingBottom(be);
661 if (te != oldTe || be != oldBe) {
662 cell->setNeedsLayout(true, false);
663 cell->layoutIfNeeded();
664 }
665
666 if ((te != oldTe || be > oldBe) && !table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout())
667 cell->repaint();
668
669 IntRect oldCellRect(cell->x(), cell->y() , cell->width(), cell->height());
670
671 if (style()->direction() == RTL) {
672 cell->setLocation(table()->columnPositions()[nEffCols] - table()->columnPositions()[table()->colToEffCol(cell->col() + cell->colSpan())] + hspacing, m_rowPos[rindx]);
673 } else
674 cell->setLocation(table()->columnPositions()[c] + hspacing, m_rowPos[rindx]);
675
676 m_overflowLeft = min(m_overflowLeft, cell->x() + cell->overflowLeft(false));
677 m_overflowWidth = max(m_overflowWidth, cell->x() + cell->overflowWidth(false));
678 m_overflowTop = min(m_overflowTop, cell->y() + cell->overflowTop(false));
679 m_overflowHeight = max(m_overflowHeight, cell->y() + cell->overflowHeight(false));
680 m_hasOverflowingCell |= cell->overflowLeft(false) || cell->overflowWidth(false) > cell->width() || cell->overflowTop(false) || cell->overflowHeight(false) > cell->height();
681
682 // If the cell moved, we have to repaint it as well as any floating/positioned
683 // descendants. An exception is if we need a layout. In this case, we know we're going to
684 // repaint ourselves (and the cell) anyway.
685 if (!table()->selfNeedsLayout() && cell->checkForRepaintDuringLayout())
686 cell->repaintDuringLayoutIfMoved(oldCellRect);
687 }
688 }
689
690 #ifndef NDEBUG
691 setNeedsLayoutIsForbidden(false);
692 #endif
693
694 ASSERT(!needsLayout());
695
696 statePusher.pop();
697
698 setHeight(m_rowPos[totalRows]);
699 m_overflowHeight = max(m_overflowHeight, height());
700 return height();
701 }
702
703 int RenderTableSection::lowestPosition(bool includeOverflowInterior, bool includeSelf) const
704 {
705 int bottom = RenderContainer::lowestPosition(includeOverflowInterior, includeSelf);
706 if (!includeOverflowInterior && hasOverflowClip())
707 return bottom;
708
709 for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
710 for (RenderObject* cell = row->firstChild(); cell; cell = cell->nextSibling()) {
711 if (cell->isTableCell())
712 bottom = max(bottom, static_cast<RenderTableCell*>(cell)->y() + cell->lowestPosition(false));
713 }
714 }
715
716 return bottom;
717 }
718
719 int RenderTableSection::rightmostPosition(bool includeOverflowInterior, bool includeSelf) const
720 {
721 int right = RenderContainer::rightmostPosition(includeOverflowInterior, includeSelf);
722 if (!includeOverflowInterior && hasOverflowClip())
723 return right;
724
725 for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
726 for (RenderObject* cell = row->firstChild(); cell; cell = cell->nextSibling()) {
727 if (cell->isTableCell())
728 right = max(right, static_cast<RenderTableCell*>(cell)->x() + cell->rightmostPosition(false));
729 }
730 }
731
732 return right;
733 }
734
735 int RenderTableSection::leftmostPosition(bool includeOverflowInterior, bool includeSelf) const
736 {
737 int left = RenderContainer::leftmostPosition(includeOverflowInterior, includeSelf);
738 if (!includeOverflowInterior && hasOverflowClip())
739 return left;
740
741 for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
742 for (RenderObject* cell = row->firstChild(); cell; cell = cell->nextSibling()) {
743 if (cell->isTableCell())
744 left = min(left, static_cast<RenderTableCell*>(cell)->x() + cell->leftmostPosition(false));
745 }
746 }
747
748 return left;
749 }
750
751 int RenderTableSection::calcOuterBorderTop() const
752 {
753 int totalCols = table()->numEffCols();
754 if (!m_gridRows || !totalCols)
755 return 0;
756
757 unsigned borderWidth = 0;
758
759 const BorderValue& sb = style()->borderTop();
760 if (sb.style() == BHIDDEN)
761 return -1;
762 if (sb.style() > BHIDDEN)
763 borderWidth = sb.width;
764
765 const BorderValue& rb = firstChild()->style()->borderTop();
766 if (rb.style() == BHIDDEN)
767 return -1;
768 if (rb.style() > BHIDDEN && rb.width > borderWidth)
769 borderWidth = rb.width;
770
771 bool allHidden = true;
772 for (int c = 0; c < totalCols; c++) {
773 const CellStruct& current = cellAt(0, c);
774 if (current.inColSpan || !current.cell)
775 continue;
776 const BorderValue& cb = current.cell->style()->borderTop();
777 // FIXME: Don't repeat for the same col group
778 RenderTableCol* colGroup = table()->colElement(c);
779 if (colGroup) {
780 const BorderValue& gb = colGroup->style()->borderTop();
781 if (gb.style() == BHIDDEN || cb.style() == BHIDDEN)
782 continue;
783 else
784 allHidden = false;
785 if (gb.style() > BHIDDEN && gb.width > borderWidth)
786 borderWidth = gb.width;
787 if (cb.style() > BHIDDEN && cb.width > borderWidth)
788 borderWidth = cb.width;
789 } else {
790 if (cb.style() == BHIDDEN)
791 continue;
792 else
793 allHidden = false;
794 if (cb.style() > BHIDDEN && cb.width > borderWidth)
795 borderWidth = cb.width;
796 }
797 }
798 if (allHidden)
799 return -1;
800
801 return borderWidth / 2;
802 }
803
804 int RenderTableSection::calcOuterBorderBottom() const
805 {
806 int totalCols = table()->numEffCols();
807 if (!m_gridRows || !totalCols)
808 return 0;
809
810 unsigned borderWidth = 0;
811
812 const BorderValue& sb = style()->borderBottom();
813 if (sb.style() == BHIDDEN)
814 return -1;
815 if (sb.style() > BHIDDEN)
816 borderWidth = sb.width;
817
818 const BorderValue& rb = lastChild()->style()->borderBottom();
819 if (rb.style() == BHIDDEN)
820 return -1;
821 if (rb.style() > BHIDDEN && rb.width > borderWidth)
822 borderWidth = rb.width;
823
824 bool allHidden = true;
825 for (int c = 0; c < totalCols; c++) {
826 const CellStruct& current = cellAt(m_gridRows - 1, c);
827 if (current.inColSpan || !current.cell)
828 continue;
829 const BorderValue& cb = current.cell->style()->borderBottom();
830 // FIXME: Don't repeat for the same col group
831 RenderTableCol* colGroup = table()->colElement(c);
832 if (colGroup) {
833 const BorderValue& gb = colGroup->style()->borderBottom();
834 if (gb.style() == BHIDDEN || cb.style() == BHIDDEN)
835 continue;
836 else
837 allHidden = false;
838 if (gb.style() > BHIDDEN && gb.width > borderWidth)
839 borderWidth = gb.width;
840 if (cb.style() > BHIDDEN && cb.width > borderWidth)
841 borderWidth = cb.width;
842 } else {
843 if (cb.style() == BHIDDEN)
844 continue;
845 else
846 allHidden = false;
847 if (cb.style() > BHIDDEN && cb.width > borderWidth)
848 borderWidth = cb.width;
849 }
850 }
851 if (allHidden)
852 return -1;
853
854 return (borderWidth + 1) / 2;
855 }
856
857 int RenderTableSection::calcOuterBorderLeft(bool rtl) const
858 {
859 int totalCols = table()->numEffCols();
860 if (!m_gridRows || !totalCols)
861 return 0;
862
863 unsigned borderWidth = 0;
864
865 const BorderValue& sb = style()->borderLeft();
866 if (sb.style() == BHIDDEN)
867 return -1;
868 if (sb.style() > BHIDDEN)
869 borderWidth = sb.width;
870
871 int leftmostColumn = rtl ? totalCols - 1 : 0;
872 RenderTableCol* colGroup = table()->colElement(leftmostColumn);
873 if (colGroup) {
874 const BorderValue& gb = colGroup->style()->borderLeft();
875 if (gb.style() == BHIDDEN)
876 return -1;
877 if (gb.style() > BHIDDEN && gb.width > borderWidth)
878 borderWidth = gb.width;
879 }
880
881 bool allHidden = true;
882 for (int r = 0; r < m_gridRows; r++) {
883 const CellStruct& current = cellAt(r, leftmostColumn);
884 if (!current.cell)
885 continue;
886 // FIXME: Don't repeat for the same cell
887 const BorderValue& cb = current.cell->style()->borderLeft();
888 const BorderValue& rb = current.cell->parent()->style()->borderLeft();
889 if (cb.style() == BHIDDEN || rb.style() == BHIDDEN)
890 continue;
891 else
892 allHidden = false;
893 if (cb.style() > BHIDDEN && cb.width > borderWidth)
894 borderWidth = cb.width;
895 if (rb.style() > BHIDDEN && rb.width > borderWidth)
896 borderWidth = rb.width;
897 }
898 if (allHidden)
899 return -1;
900
901 return borderWidth / 2;
902 }
903
904 int RenderTableSection::calcOuterBorderRight(bool rtl) const
905 {
906 int totalCols = table()->numEffCols();
907 if (!m_gridRows || !totalCols)
908 return 0;
909
910 unsigned borderWidth = 0;
911
912 const BorderValue& sb = style()->borderRight();
913 if (sb.style() == BHIDDEN)
914 return -1;
915 if (sb.style() > BHIDDEN)
916 borderWidth = sb.width;
917
918 int rightmostColumn = rtl ? 0 : totalCols - 1;
919 RenderTableCol* colGroup = table()->colElement(rightmostColumn);
920 if (colGroup) {
921 const BorderValue& gb = colGroup->style()->borderRight();
922 if (gb.style() == BHIDDEN)
923 return -1;
924 if (gb.style() > BHIDDEN && gb.width > borderWidth)
925 borderWidth = gb.width;
926 }
927
928 bool allHidden = true;
929 for (int r = 0; r < m_gridRows; r++) {
930 const CellStruct& current = cellAt(r, rightmostColumn);
931 if (!current.cell)
932 continue;
933 // FIXME: Don't repeat for the same cell
934 const BorderValue& cb = current.cell->style()->borderRight();
935 const BorderValue& rb = current.cell->parent()->style()->borderRight();
936 if (cb.style() == BHIDDEN || rb.style() == BHIDDEN)
937 continue;
938 else
939 allHidden = false;
940 if (cb.style() > BHIDDEN && cb.width > borderWidth)
941 borderWidth = cb.width;
942 if (rb.style() > BHIDDEN && rb.width > borderWidth)
943 borderWidth = rb.width;
944 }
945 if (allHidden)
946 return -1;
947
948 return (borderWidth + 1) / 2;
949 }
950
951 void RenderTableSection::recalcOuterBorder()
952 {
953 bool rtl = table()->style()->direction() == RTL;
954 m_outerBorderTop = calcOuterBorderTop();
955 m_outerBorderBottom = calcOuterBorderBottom();
956 m_outerBorderLeft = calcOuterBorderLeft(rtl);
957 m_outerBorderRight = calcOuterBorderRight(rtl);
958 }
959
960 int RenderTableSection::getBaselineOfFirstLineBox() const
961 {
962 if (!m_gridRows)
963 return -1;
964
965 int firstLineBaseline = m_grid[0].baseline;
966 if (firstLineBaseline)
967 return firstLineBaseline + m_rowPos[0];
968
969 firstLineBaseline = -1;
970 Row* firstRow = m_grid[0].row;
971 for (size_t i = 0; i < firstRow->size(); ++i) {
972 RenderTableCell* cell = firstRow->at(i).cell;
973 if (cell)
974 firstLineBaseline = max(firstLineBaseline, cell->y() + cell->paddingTop() + cell->borderTop() + cell->contentHeight());
975 }
976
977 return firstLineBaseline;
978 }
979
980 void RenderTableSection::paint(PaintInfo& paintInfo, int tx, int ty)
981 {
982 // put this back in when all layout tests can handle it
983 // ASSERT(!needsLayout());
984 // avoid crashing on bugs that cause us to paint with dirty layout
985 if (needsLayout())
986 return;
987
988 unsigned totalRows = m_gridRows;
989 unsigned totalCols = table()->columns().size();
990
991 if (!totalRows || !totalCols)
992 return;
993
994 tx += x();
995 ty += y();
996
997 // Check which rows and cols are visible and only paint these.
998 // FIXME: Could use a binary search here.
999 PaintPhase paintPhase = paintInfo.phase;
1000 int x = paintInfo.rect.x();
1001 int y = paintInfo.rect.y();
1002 int w = paintInfo.rect.width();
1003 int h = paintInfo.rect.height();
1004
1005 #ifdef ANDROID_LAYOUT
1006 unsigned int startrow = 0;
1007 unsigned int endrow = totalRows;
1008 unsigned int startcol = 0;
1009 unsigned int endcol = totalCols;
1010 if (table()->isSingleColumn()) {
1011 // FIXME: should we be smarter too?
1012 } else {
1013 // FIXME: possible to rollback to the common tree.
1014 // rowPos size is set in calcRowHeight(), which is called from table layout().
1015 // BUT RenderTableSection is init through parsing. On a slow device, paint() as
1016 // the result of layout() can come after the next parse() as everything is triggered
1017 // by timer. So we have to check rowPos before using it.
1018 if (m_rowPos.size() != (totalRows + 1))
1019 return;
1020 #endif
1021
1022 int os = 2 * maximalOutlineSize(paintPhase);
1023 unsigned startrow = 0;
1024 unsigned endrow = totalRows;
1025
1026 // If some cell overflows, just paint all of them.
1027 if (!m_hasOverflowingCell) {
1028 for (; startrow < totalRows; startrow++) {
1029 if (ty + m_rowPos[startrow + 1] >= y - os)
1030 break;
1031 }
1032 if (startrow == totalRows && ty + m_rowPos[totalRows] + table()->outerBorderBottom() >= y - os)
1033 startrow--;
1034
1035 for (; endrow > 0; endrow--) {
1036 if (ty + m_rowPos[endrow - 1] <= y + h + os)
1037 break;
1038 }
1039 if (!endrow && ty + m_rowPos[0] - table()->outerBorderTop() <= y + h + os)
1040 endrow++;
1041 }
1042
1043 unsigned startcol = 0;
1044 unsigned endcol = totalCols;
1045 // FIXME: Implement RTL.
1046 if (!m_hasOverflowingCell && style()->direction() == LTR) {
1047 for (; startcol < totalCols; startcol++) {
1048 if (tx + table()->columnPositions()[startcol + 1] >= x - os)
1049 break;
1050 }
1051 if (startcol == totalCols && tx + table()->columnPositions()[totalCols] + table()->outerBorderRight() >= x - os)
1052 startcol--;
1053
1054 for (; endcol > 0; endcol--) {
1055 if (tx + table()->columnPositions()[endcol - 1] <= x + w + os)
1056 break;
1057 }
1058 if (!endcol && tx + table()->columnPositions()[0] - table()->outerBorderLeft() <= y + w + os)
1059 endcol++;
1060 }
1061 #ifdef ANDROID_LAYOUT
1062 }
1063 #endif
1064
1065 if (startcol < endcol) {
1066 // draw the cells
1067 for (unsigned r = startrow; r < endrow; r++) {
1068 unsigned c = startcol;
1069 // since a cell can be -1 (indicating a colspan) we might have to search backwards to include it
1070 while (c && cellAt(r, c).inColSpan)
1071 c--;
1072 for (; c < endcol; c++) {
1073 CellStruct current = cellAt(r, c);
1074 RenderTableCell* cell = current.cell;
1075
1076 // Cells must always paint in the order in which they appear taking into account
1077 // their upper left originating row/column. For cells with rowspans, avoid repainting
1078 // if we've already seen the cell.
1079 if (!cell || (r > startrow && (cellAt(r - 1, c).cell == cell)))
1080 continue;
1081
1082 RenderTableRow* row = static_cast<RenderTableRow*>(cell->parent());
1083
1084 if (paintPhase == PaintPhaseBlockBackground || paintPhase == PaintPhaseChildBlockBackground) {
1085 // We need to handle painting a stack of backgrounds. This stack (from bottom to top) consists of
1086 // the column group, column, row group, row, and then the cell.
1087 RenderObject* col = table()->colElement(c);
1088 RenderObject* colGroup = 0;
1089 if (col && col->parent()->style()->display() == TABLE_COLUMN_GROUP)
1090 colGroup = col->parent();
1091
1092 // Column groups and columns first.
1093 // FIXME: Columns and column groups do not currently support opacity, and they are being painted "too late" in
1094 // the stack, since we have already opened a transparency layer (potentially) for the table row group.
1095 // Note that we deliberately ignore whether or not the cell has a layer, since these backgrounds paint "behind" the
1096 // cell.
1097 cell->paintBackgroundsBehindCell(paintInfo, tx, ty, colGroup);
1098 cell->paintBackgroundsBehindCell(paintInfo, tx, ty, col);
1099
1100 // Paint the row group next.
1101 cell->paintBackgroundsBehindCell(paintInfo, tx, ty, this);
1102
1103 // Paint the row next, but only if it doesn't have a layer. If a row has a layer, it will be responsible for
1104 // painting the row background for the cell.
1105 if (!row->hasLayer())
1106 cell->paintBackgroundsBehindCell(paintInfo, tx, ty, row);
1107 }
1108 if ((!cell->hasLayer() && !row->hasLayer()) || paintInfo.phase == PaintPhaseCollapsedTableBorders)
1109 cell->paint(paintInfo, tx, ty);
1110 }
1111 }
1112 }
1113 }
1114
1115 void RenderTableSection::imageChanged(WrappedImagePtr, const IntRect*)
1116 {
1117 // FIXME: Examine cells and repaint only the rect the image paints in.
1118 repaint();
1119 }
1120
1121 void RenderTableSection::recalcCells()
1122 {
1123 m_cCol = 0;
1124 m_cRow = -1;
1125 clearGrid();
1126 m_gridRows = 0;
1127
1128 for (RenderObject* row = firstChild(); row; row = row->nextSibling()) {
1129 if (row->isTableRow()) {
1130 m_cRow++;
1131 m_cCol = 0;
1132 if (!ensureRows(m_cRow + 1))
1133 break;
1134
1135 RenderTableRow* tableRow = static_cast<RenderTableRow*>(row);
1136 m_grid[m_cRow].rowRenderer = tableRow;
1137
1138 for (RenderObject* cell = row->firstChild(); cell; cell = cell->nextSibling()) {
1139 if (cell->isTableCell())
1140 addCell(static_cast<RenderTableCell*>(cell), tableRow);
1141 }
1142 }
1143 }
1144 m_needsCellRecalc = false;
1145 setNeedsLayout(true);
1146 }
1147
1148 void RenderTableSection::clearGrid()
1149 {
1150 int rows = m_gridRows;
1151 while (rows--)
1152 delete m_grid[rows].row;
1153 }
1154
1155 int RenderTableSection::numColumns() const
1156 {
1157 int result = 0;
1158
1159 for (int r = 0; r < m_gridRows; ++r) {
1160 for (int c = result; c < table()->numEffCols(); ++c) {
1161 const CellStruct& cell = cellAt(r, c);
1162 if (cell.cell || cell.inColSpan)
1163 result = c;
1164 }
1165 }
1166
1167 return result + 1;
1168 }
1169
1170 void RenderTableSection::appendColumn(int pos)
1171 {
1172 for (int row = 0; row < m_gridRows; ++row) {
1173 m_grid[row].row->resize(pos + 1);
1174 CellStruct& c = cellAt(row, pos);
1175 c.cell = 0;
1176 c.inColSpan = false;
1177 }
1178 }
1179
1180 void RenderTableSection::splitColumn(int pos, int newSize)
1181 {
1182 if (m_cCol > pos)
1183 m_cCol++;
1184 for (int row = 0; row < m_gridRows; ++row) {
1185 m_grid[row].row->resize(newSize);
1186 Row& r = *m_grid[row].row;
1187 memmove(r.data() + pos + 1, r.data() + pos, (newSize - 1 - pos) * sizeof(CellStruct));
1188 r[pos + 1].cell = 0;
1189 r[pos + 1].inColSpan = r[pos].inColSpan || r[pos].cell;
1190 }
1191 }
1192
1193 RenderObject* RenderTableSection::removeChildNode(RenderObject* child, bool fullRemove)
1194 {
1195 setNeedsCellRecalc();
1196 return RenderContainer::removeChildNode(child, fullRemove);
1197 }
1198
1199 // Hit Testing
1200 bool RenderTableSection::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int xPos, int yPos, int tx, int ty, HitTestAction action)
1201 {
1202 // Table sections cannot ever be hit tested. Effectively they do not exist.
1203 // Just forward to our children always.
1204 tx += x();
1205 ty += y();
1206
1207 for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
1208 // FIXME: We have to skip over inline flows, since they can show up inside table rows
1209 // at the moment (a demoted inline <form> for example). If we ever implement a
1210 // table-specific hit-test method (which we should do for performance reasons anyway),
1211 // then we can remove this check.
1212 if (!child->hasLayer() && !child->isRenderInline() && child->nodeAtPoint(request, result, xPos, yPos, tx, ty, action)) {
1213 updateHitTestResult(result, IntPoint(xPos - tx, yPos - ty));
1214 return true;
1215 }
1216 }
1217
1218 return false;
1219 }
1220
1221 } // namespace WebCore
1222