Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: Source/core/layout/TableLayoutAlgorithmAuto.cpp

Issue 988443003: Don't add artifical 1 pixel width to empty tables (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Updated Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2002 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 2002 Lars Knoll (knoll@kde.org)
3 * (C) 2002 Dirk Mueller (mueller@kde.org) 3 * (C) 2002 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2003, 2006, 2008, 2010 Apple Inc. All rights reserved. 4 * Copyright (C) 2003, 2006, 2008, 2010 Apple Inc. All rights reserved.
5 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License. 9 * version 2 of the License.
10 * 10 *
11 * This library is distributed in the hope that it will be useful, 11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details. 14 * Library General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU Library General Public License 16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to 17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA. 19 * Boston, MA 02110-1301, USA.
20 */ 20 */
21 21
22 #include "config.h" 22 #include "config.h"
23 #include "core/layout/TableLayoutAlgorithmAuto.h" 23 #include "core/layout/TableLayoutAlgorithmAuto.h"
24 24
25 #include "core/layout/LayoutTable.h" 25 #include "core/layout/LayoutTable.h"
26 #include "core/layout/LayoutTableCell.h" 26 #include "core/layout/LayoutTableCell.h"
27 #include "core/layout/LayoutTableCol.h" 27 #include "core/layout/LayoutTableCol.h"
28 #include "core/layout/LayoutTableSection.h" 28 #include "core/layout/LayoutTableSection.h"
29 #include "core/layout/LayoutText.h"
29 #include "core/layout/TextAutosizer.h" 30 #include "core/layout/TextAutosizer.h"
30 31
31 namespace blink { 32 namespace blink {
32 33
33 TableLayoutAlgorithmAuto::TableLayoutAlgorithmAuto(LayoutTable* table) 34 TableLayoutAlgorithmAuto::TableLayoutAlgorithmAuto(LayoutTable* table)
34 : TableLayoutAlgorithm(table) 35 : TableLayoutAlgorithm(table)
35 , m_hasPercent(false) 36 , m_hasPercent(false)
36 , m_effectiveLogicalWidthDirty(true) 37 , m_effectiveLogicalWidthDirty(true)
37 { 38 {
38 } 39 }
39 40
40 TableLayoutAlgorithmAuto::~TableLayoutAlgorithmAuto() 41 TableLayoutAlgorithmAuto::~TableLayoutAlgorithmAuto()
41 { 42 {
42 } 43 }
43 44
45 static bool isEmptyCell(LayoutObject* object)
mstensho (USE GERRIT) 2015/05/18 08:45:38 I don't get this. <p>Should be 50% lime and 50% c
rhogan 2015/05/19 19:45:42 The second one should definitely be treated as emp
46 {
47 for (LayoutObject* curr = object; curr; curr = curr->nextSibling()) {
48 if (curr->isText() && toLayoutText(curr)->isAllCollapsibleWhitespace())
49 continue;
50 if (curr->slowFirstChild() && isEmptyCell(curr->slowFirstChild()))
51 continue;
52 return false;
53 }
54 return true;
55 }
56
44 void TableLayoutAlgorithmAuto::recalcColumn(unsigned effCol) 57 void TableLayoutAlgorithmAuto::recalcColumn(unsigned effCol)
45 { 58 {
46 Layout& columnLayout = m_layoutStruct[effCol]; 59 Layout& columnLayout = m_layoutStruct[effCol];
47 60
48 LayoutTableCell* fixedContributor = 0; 61 LayoutTableCell* fixedContributor = 0;
49 LayoutTableCell* maxContributor = 0; 62 LayoutTableCell* maxContributor = 0;
50 63
51 for (LayoutObject* child = m_table->children()->firstChild(); child; child = child->nextSibling()) { 64 for (LayoutObject* child = m_table->children()->firstChild(); child; child = child->nextSibling()) {
52 if (child->isLayoutTableCol()) { 65 if (child->isLayoutTableCol()) {
53 // LayoutTableCols don't have the concept of preferred logical width , but we need to clear their dirty bits 66 // LayoutTableCols don't have the concept of preferred logical width , but we need to clear their dirty bits
54 // so that if we call setPreferredWidthsDirty(true) on a col or one of its descendants, we'll mark it's 67 // so that if we call setPreferredWidthsDirty(true) on a col or one of its descendants, we'll mark it's
55 // ancestors as dirty. 68 // ancestors as dirty.
56 toLayoutTableCol(child)->clearPreferredLogicalWidthsDirtyBits(); 69 toLayoutTableCol(child)->clearPreferredLogicalWidthsDirtyBits();
57 } else if (child->isTableSection()) { 70 } else if (child->isTableSection()) {
58 LayoutTableSection* section = toLayoutTableSection(child); 71 LayoutTableSection* section = toLayoutTableSection(child);
59 unsigned numRows = section->numRows(); 72 unsigned numRows = section->numRows();
60 for (unsigned i = 0; i < numRows; i++) { 73 for (unsigned i = 0; i < numRows; i++) {
61 LayoutTableSection::CellStruct current = section->cellAt(i, effC ol); 74 LayoutTableSection::CellStruct current = section->cellAt(i, effC ol);
62 LayoutTableCell* cell = current.primaryCell(); 75 LayoutTableCell* cell = current.primaryCell();
63 76
64 if (current.inColSpan || !cell) 77 if (current.inColSpan || !cell)
65 continue; 78 continue;
66 79
67 bool cellHasContent = cell->children()->firstChild() || cell->st yle()->hasBorder() || cell->style()->hasPadding() || cell->style()->hasBackgroun d(); 80 bool cellHasContent = cell->style()->hasBorder() || cell->style( )->hasPadding() || !isEmptyCell(cell->firstChild());
mstensho (USE GERRIT) 2015/05/18 08:45:38 This certainly looks like a step in the right dire
rhogan 2015/05/19 19:45:42 Good point - and in your example above (which I've
68 if (cellHasContent) 81 if (cellHasContent)
69 columnLayout.emptyCellsOnly = false; 82 columnLayout.emptyCellsOnly = false;
70 83
71 // A cell originates in this column. Ensure we have 84 // A cell originates in this column. Ensure we have
72 // a min/max width of at least 1px for this column now. 85 // a min/max width of at least 1px for this column now.
73 columnLayout.minLogicalWidth = std::max<int>(columnLayout.minLog icalWidth, cellHasContent ? 1 : 0); 86 columnLayout.minLogicalWidth = std::max<int>(columnLayout.minLog icalWidth, cellHasContent ? 1 : 0);
74 columnLayout.maxLogicalWidth = std::max<int>(columnLayout.maxLog icalWidth, 1);
75 87
76 if (cell->colSpan() == 1) { 88 if (cell->colSpan() == 1) {
77 columnLayout.minLogicalWidth = std::max<int>(cell->minPrefer redLogicalWidth(), columnLayout.minLogicalWidth); 89 columnLayout.minLogicalWidth = std::max<int>(cell->minPrefer redLogicalWidth(), columnLayout.minLogicalWidth);
78 if (cell->maxPreferredLogicalWidth() > columnLayout.maxLogic alWidth) { 90 if (cell->maxPreferredLogicalWidth() > columnLayout.maxLogic alWidth) {
79 columnLayout.maxLogicalWidth = cell->maxPreferredLogical Width(); 91 columnLayout.maxLogicalWidth = cell->maxPreferredLogical Width();
80 maxContributor = cell; 92 maxContributor = cell;
81 } 93 }
82 94
83 // All browsers implement a size limit on the cell's max wid th. 95 // All browsers implement a size limit on the cell's max wid th.
84 // Our limit is based on KHTML's representation that used 16 bits widths. 96 // Our limit is based on KHTML's representation that used 16 bits widths.
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 // can't satify this condition, treat as variable 378 // can't satify this condition, treat as variable
367 cellLogicalWidth = Length(); 379 cellLogicalWidth = Length();
368 } else { 380 } else {
369 maxLogicalWidth = std::max(maxLogicalWidth, static_cast<int>(std ::max(spanMaxLogicalWidth, cellMaxLogicalWidth) * 100 / cellLogicalWidth.percen t())); 381 maxLogicalWidth = std::max(maxLogicalWidth, static_cast<int>(std ::max(spanMaxLogicalWidth, cellMaxLogicalWidth) * 100 / cellLogicalWidth.percen t()));
370 382
371 // all non percent columns in the span get percent values to sum up correctly. 383 // all non percent columns in the span get percent values to sum up correctly.
372 float percentMissing = cellLogicalWidth.percent() - totalPercent ; 384 float percentMissing = cellLogicalWidth.percent() - totalPercent ;
373 int totalWidth = 0; 385 int totalWidth = 0;
374 for (unsigned pos = effCol; pos < lastCol; ++pos) { 386 for (unsigned pos = effCol; pos < lastCol; ++pos) {
375 if (!m_layoutStruct[pos].effectiveLogicalWidth.hasPercent()) 387 if (!m_layoutStruct[pos].effectiveLogicalWidth.hasPercent())
376 totalWidth += m_layoutStruct[pos].effectiveMaxLogicalWid th; 388 totalWidth += m_layoutStruct[pos].clampedEffectiveMaxLog icalWidth();
377 } 389 }
378 390
379 for (unsigned pos = effCol; pos < lastCol && totalWidth > 0; ++p os) { 391 for (unsigned pos = effCol; pos < lastCol && totalWidth > 0; ++p os) {
380 if (!m_layoutStruct[pos].effectiveLogicalWidth.hasPercent()) { 392 if (!m_layoutStruct[pos].effectiveLogicalWidth.hasPercent()) {
381 float percent = percentMissing * static_cast<float>(m_la youtStruct[pos].effectiveMaxLogicalWidth) / totalWidth; 393 float percent = percentMissing * static_cast<float>(m_la youtStruct[pos].effectiveMaxLogicalWidth) / totalWidth;
382 totalWidth -= m_layoutStruct[pos].effectiveMaxLogicalWid th; 394 totalWidth -= m_layoutStruct[pos].clampedEffectiveMaxLog icalWidth();
383 percentMissing -= percent; 395 percentMissing -= percent;
384 if (percent > 0) 396 if (percent > 0)
385 m_layoutStruct[pos].effectiveLogicalWidth.setValue(P ercent, percent); 397 m_layoutStruct[pos].effectiveLogicalWidth.setValue(P ercent, percent);
386 else 398 else
387 m_layoutStruct[pos].effectiveLogicalWidth = Length() ; 399 m_layoutStruct[pos].effectiveLogicalWidth = Length() ;
388 } 400 }
389 } 401 }
390 } 402 }
391 } 403 }
392 404
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 m_layoutStruct[i].computedLogicalWidth = cellLogicalWidth; 543 m_layoutStruct[i].computedLogicalWidth = cellLogicalWidth;
532 available -= cellLogicalWidth; 544 available -= cellLogicalWidth;
533 Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth; 545 Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth;
534 switch (logicalWidth.type()) { 546 switch (logicalWidth.type()) {
535 case Percent: 547 case Percent:
536 havePercent = true; 548 havePercent = true;
537 totalPercent += logicalWidth.percent(); 549 totalPercent += logicalWidth.percent();
538 break; 550 break;
539 case Fixed: 551 case Fixed:
540 numFixed++; 552 numFixed++;
541 totalFixed += m_layoutStruct[i].effectiveMaxLogicalWidth; 553 totalFixed += m_layoutStruct[i].clampedEffectiveMaxLogicalWidth();
542 // fall through 554 // fall through
543 break; 555 break;
544 case Auto: 556 case Auto:
545 if (m_layoutStruct[i].emptyCellsOnly) { 557 if (m_layoutStruct[i].emptyCellsOnly) {
546 numAutoEmptyCellsOnly++; 558 numAutoEmptyCellsOnly++;
547 } else { 559 } else {
548 numAuto++; 560 numAuto++;
549 totalAuto += m_layoutStruct[i].effectiveMaxLogicalWidth; 561 totalAuto += m_layoutStruct[i].clampedEffectiveMaxLogicalWidth() ;
550 allocAuto += cellLogicalWidth; 562 allocAuto += cellLogicalWidth;
551 } 563 }
552 break; 564 break;
553 default: 565 default:
554 break; 566 break;
555 } 567 }
556 } 568 }
557 569
558 // allocate width to percent cols 570 // allocate width to percent cols
559 if (available > 0 && havePercent) { 571 if (available > 0 && havePercent) {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
606 618
607 if (available > 0 && m_hasPercent && totalPercent < 100) 619 if (available > 0 && m_hasPercent && totalPercent < 100)
608 distributeWidthToColumns<float, Percent, AllCells, ExtraWidth, StartToEn d>(available, totalPercent); 620 distributeWidthToColumns<float, Percent, AllCells, ExtraWidth, StartToEn d>(available, totalPercent);
609 621
610 if (available > 0 && nEffCols > numAutoEmptyCellsOnly) { 622 if (available > 0 && nEffCols > numAutoEmptyCellsOnly) {
611 unsigned total = nEffCols - numAutoEmptyCellsOnly; 623 unsigned total = nEffCols - numAutoEmptyCellsOnly;
612 // Starting from the last cell is for compatibility with FF/IE - it isn' t specified anywhere. 624 // Starting from the last cell is for compatibility with FF/IE - it isn' t specified anywhere.
613 distributeWidthToColumns<unsigned, Auto, NonEmptyCells, LeftoverWidth, E ndToStart>(available, total); 625 distributeWidthToColumns<unsigned, Auto, NonEmptyCells, LeftoverWidth, E ndToStart>(available, total);
614 } 626 }
615 627
628 // spread over the empty cells
629 if (available > 0) {
mstensho (USE GERRIT) 2015/05/20 09:34:22 Why not remove this code block entirely? You now s
630 unsigned total = numAutoEmptyCellsOnly;
631 distributeWidthToColumns<unsigned, Auto, EmptyCells, LeftoverWidth, EndT oStart>(available, total);
mstensho (USE GERRIT) 2015/05/18 08:45:39 Is it possible to over-allocate here? If so, don't
rhogan 2015/05/19 19:45:42 No, the shrink step is keyed to Length type only -
632 }
633
616 // If we have overallocated, reduce every cell according to the difference b etween desired width and minwidth 634 // If we have overallocated, reduce every cell according to the difference b etween desired width and minwidth
617 // this seems to produce to the pixel exact results with IE. Wonder is some of this also holds for width distributing. 635 // this seems to produce to the pixel exact results with IE. Wonder is some of this also holds for width distributing.
618 // This is basically the reverse of how we grew the cells. 636 // This is basically the reverse of how we grew the cells.
619 if (available < 0) 637 if (available < 0)
620 shrinkColumnWidth(Auto, available); 638 shrinkColumnWidth(Auto, available);
621 if (available < 0) 639 if (available < 0)
622 shrinkColumnWidth(Fixed, available); 640 shrinkColumnWidth(Fixed, available);
623 if (available < 0) 641 if (available < 0)
624 shrinkColumnWidth(Percent, available); 642 shrinkColumnWidth(Percent, available);
625 643
626 int pos = 0; 644 int pos = 0;
627 for (size_t i = 0; i < nEffCols; ++i) { 645 for (size_t i = 0; i < nEffCols; ++i) {
628 m_table->setColumnPosition(i, pos); 646 m_table->setColumnPosition(i, pos);
629 pos += m_layoutStruct[i].computedLogicalWidth + m_table->hBorderSpacing( ); 647 pos += m_layoutStruct[i].computedLogicalWidth + m_table->hBorderSpacing( );
630 } 648 }
631 m_table->setColumnPosition(m_table->columnPositions().size() - 1, pos); 649 m_table->setColumnPosition(m_table->columnPositions().size() - 1, pos);
632 } 650 }
633 651
634 template<typename Total, LengthType lengthType, CellsToProcess cellsToProcess, D istributionMode distributionMode, DistributionDirection distributionDirection> 652 template<typename Total, LengthType lengthType, CellsToProcess cellsToProcess, D istributionMode distributionMode, DistributionDirection distributionDirection>
635 void TableLayoutAlgorithmAuto::distributeWidthToColumns(int& available, Total to tal) 653 void TableLayoutAlgorithmAuto::distributeWidthToColumns(int& available, Total to tal)
636 { 654 {
637 // TODO(alancutter): Make this work correctly for calc lengths. 655 // TODO(alancutter): Make this work correctly for calc lengths.
638 int nEffCols = static_cast<int>(m_table->numEffCols()); 656 int nEffCols = static_cast<int>(m_table->numEffCols());
639 bool startToEnd = distributionDirection == StartToEnd; 657 bool startToEnd = distributionDirection == StartToEnd;
640 for (int i = startToEnd ? 0 : nEffCols - 1; startToEnd ? i < nEffCols : i > -1; startToEnd ? ++i : --i) { 658 for (int i = startToEnd ? 0 : nEffCols - 1; startToEnd ? i < nEffCols : i > -1; startToEnd ? ++i : --i) {
641 const Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth; 659 const Length& logicalWidth = m_layoutStruct[i].effectiveLogicalWidth;
642 if (cellsToProcess == NonEmptyCells && logicalWidth.isAuto() && m_layout Struct[i].emptyCellsOnly) 660 if (cellsToProcess == NonEmptyCells && logicalWidth.isAuto() && m_layout Struct[i].emptyCellsOnly)
643 continue; 661 continue;
662 if (cellsToProcess == EmptyCells && logicalWidth.isAuto() && !m_layoutSt ruct[i].emptyCellsOnly)
663 continue;
644 if (distributionMode != LeftoverWidth && logicalWidth.type() != lengthTy pe) 664 if (distributionMode != LeftoverWidth && logicalWidth.type() != lengthTy pe)
645 continue; 665 continue;
646 666
647 float factor = 1; 667 float factor = 1;
648 if (distributionMode != LeftoverWidth) { 668 if (distributionMode != LeftoverWidth) {
649 if (lengthType == Percent) 669 if (lengthType == Percent)
650 factor = logicalWidth.percent(); 670 factor = logicalWidth.percent();
651 else if (lengthType == Auto || lengthType == Fixed) 671 else if (lengthType == Auto || lengthType == Fixed)
652 factor = m_layoutStruct[i].effectiveMaxLogicalWidth; 672 factor = m_layoutStruct[i].clampedEffectiveMaxLogicalWidth();
653 } 673 }
654 674
655 int newWidth = available * factor / total; 675 int newWidth = available * factor / total;
656 int cellLogicalWidth = (distributionMode == InitialWidth) ? max<int>(m_l ayoutStruct[i].computedLogicalWidth, newWidth) : newWidth; 676 int cellLogicalWidth = (distributionMode == InitialWidth) ? max<int>(m_l ayoutStruct[i].computedLogicalWidth, newWidth) : newWidth;
657 available -= cellLogicalWidth; 677 available -= cellLogicalWidth;
658 total -= factor; 678 total -= factor;
659 m_layoutStruct[i].computedLogicalWidth = (distributionMode == InitialWid th) ? cellLogicalWidth : m_layoutStruct[i].computedLogicalWidth + cellLogicalWid th; 679 m_layoutStruct[i].computedLogicalWidth = (distributionMode == InitialWid th) ? cellLogicalWidth : m_layoutStruct[i].computedLogicalWidth + cellLogicalWid th;
660 680
661 // If we have run out of width to allocate we're done. 681 // If we have run out of width to allocate we're done.
662 // Also, any overallocation will be unwound later so no point in buildin g up cells to unwind, just bail now. 682 // Also, any overallocation will be unwound later so no point in buildin g up cells to unwind, just bail now.
(...skipping 21 matching lines...) Expand all
684 int reduce = available * minMaxDiff / logicalWidthBeyondMin; 704 int reduce = available * minMaxDiff / logicalWidthBeyondMin;
685 m_layoutStruct[i].computedLogicalWidth += reduce; 705 m_layoutStruct[i].computedLogicalWidth += reduce;
686 available -= reduce; 706 available -= reduce;
687 logicalWidthBeyondMin -= minMaxDiff; 707 logicalWidthBeyondMin -= minMaxDiff;
688 if (available >= 0) 708 if (available >= 0)
689 break; 709 break;
690 } 710 }
691 } 711 }
692 } 712 }
693 } 713 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698