|
15 | 15 | using TheArtOfDev.HtmlRenderer.Adapters; |
16 | 16 | using TheArtOfDev.HtmlRenderer.Adapters.Entities; |
17 | 17 | using TheArtOfDev.HtmlRenderer.Core.Entities; |
| 18 | +using TheArtOfDev.HtmlRenderer.Core.Fragmentation; |
18 | 19 | using TheArtOfDev.HtmlRenderer.Core.Parse; |
19 | 20 | using TheArtOfDev.HtmlRenderer.Core.Utils; |
20 | 21 |
|
@@ -624,12 +625,56 @@ private void LayoutCells(RGraphics g) |
624 | 625 | _tableBox.Location = new RPoint(startx - _tableBox.ActualBorderLeftWidth - _tableBox.ActualPaddingLeft - GetHorizontalSpacing(), _tableBox.Location.Y); |
625 | 626 | } |
626 | 627 |
|
| 628 | + // css-tables-3 6.2: a <thead> repeats on every page the table's body/footer spans, where |
| 629 | + // the group carries an avoiding break-inside (the UA default stylesheet sets this). |
| 630 | + // Reserving the room here, before the first row of each continuation page is positioned, |
| 631 | + // is what keeps that row from being drawn underneath the repeated header instead of below |
| 632 | + // it - a fragment-tree-only repeat (no reservation) would just overlap real content. |
| 633 | + var pageGridContainer = _tableBox.HtmlContainer; |
| 634 | + var repeatsHeader = pageGridContainer != null && pageGridContainer.HasRealPageGrid |
| 635 | + && _headerBox != null && BreakValues.AvoidsBreak(_headerBox.BreakInside); |
| 636 | + var headerRowCount = _headerBox?.Boxes.Count ?? 0; |
| 637 | + double headerHeight = 0; |
| 638 | + int? lastRepeatSlot = null; |
| 639 | + _tableBox.RepeatedHeaderRows = null; |
| 640 | + |
627 | 641 | for (int i = 0; i < _allRows.Count; i++) |
628 | 642 | { |
| 643 | + if (repeatsHeader && i == headerRowCount) |
| 644 | + { |
| 645 | + // The header's own rows (i = 0..headerRowCount-1) just finished; maxBottom is |
| 646 | + // still theirs. Its own page is never itself a "repeat" - the header is already |
| 647 | + // there once, in flow. |
| 648 | + headerHeight = maxBottom - starty; |
| 649 | + lastRepeatSlot = pageGridContainer.PageIndexOf(starty); |
| 650 | + } |
| 651 | + |
| 652 | + if (repeatsHeader && i >= headerRowCount && lastRepeatSlot.HasValue) |
| 653 | + { |
| 654 | + var slot = pageGridContainer.PageIndexOf(cury); |
| 655 | + if (slot > lastRepeatSlot.Value) |
| 656 | + { |
| 657 | + var pageTop = pageGridContainer.PageTopOf(slot); |
| 658 | + cury = pageTop + headerHeight; |
| 659 | + lastRepeatSlot = slot; |
| 660 | + |
| 661 | + _tableBox.RepeatedHeaderRows ??= new List<CssBox>(); |
| 662 | + for (var hi = 0; hi < headerRowCount; hi++) |
| 663 | + { |
| 664 | + var sourceRow = _allRows[hi]; |
| 665 | + // A <tr> box's own Location is never assigned by this row loop (only its |
| 666 | + // cells' is) - the first cell is the real reference point for "where this |
| 667 | + // header row actually renders". |
| 668 | + var sourceRenderedTop = sourceRow.Boxes.Count > 0 ? sourceRow.Boxes[0].Location.Y : starty; |
| 669 | + var targetTop = pageTop + (sourceRenderedTop - starty); |
| 670 | + _tableBox.RepeatedHeaderRows.Add(TableHeaderRepeat.CloneAndPosition(sourceRow, sourceRenderedTop, targetTop)); |
| 671 | + } |
| 672 | + } |
| 673 | + } |
| 674 | + |
629 | 675 | var row = _allRows[i]; |
630 | 676 | double curx = startx; |
631 | 677 | int curCol = 0; |
632 | | - bool breakPage = false; |
633 | 678 |
|
634 | 679 | for (int j = 0; j < row.Boxes.Count; j++) |
635 | 680 | { |
@@ -676,30 +721,31 @@ private void LayoutCells(RGraphics g) |
676 | 721 | spacer.ExtendedBox.ActualBottom = maxBottom; |
677 | 722 | CssLayoutEngine.ApplyCellVerticalAlignment(g, spacer.ExtendedBox); |
678 | 723 | } |
| 724 | + } |
679 | 725 |
|
680 | | - // If one cell crosses page borders then don't need to check other cells in the row |
681 | | - if (_tableBox.PageBreakInside == CssConstants.Avoid) |
| 726 | + // break-inside: avoid (or the legacy page-break-inside) on the table: if this row |
| 727 | + // straddles a page boundary and fits whole on one page, shift the whole row - not |
| 728 | + // just one cell - down to the next page's content top. Rows aren't avoided from |
| 729 | + // splitting by default (css-tables-3 6.1 permits a row to fragment, each cell |
| 730 | + // independently, which is what happens here with no correction: a cell's own content |
| 731 | + // already flows across the boundary via BlockFragmentation/InlineFragmentation) - |
| 732 | + // only when the table author actually asked for it. |
| 733 | + if (pageGridContainer != null && pageGridContainer.HasRealPageGrid && BreakValues.AvoidsBreak(_tableBox.BreakInside) |
| 734 | + && maxBottom > cury) |
| 735 | + { |
| 736 | + var topSlot = pageGridContainer.PageIndexOf(cury); |
| 737 | + var bottomSlot = pageGridContainer.PageIndexOf(Math.Max(cury, maxBottom - 0.01)); |
| 738 | + if (bottomSlot > topSlot && maxBottom - cury < pageGridContainer.PageSize.Height) |
682 | 739 | { |
683 | | - breakPage = cell.BreakPage(); |
684 | | - if (breakPage) |
| 740 | + var delta = pageGridContainer.PageTopOf(topSlot + 1) - cury; |
| 741 | + foreach (CssBox cell in row.Boxes) |
685 | 742 | { |
686 | | - cury = cell.Location.Y; |
687 | | - break; |
| 743 | + cell.OffsetTop(delta); |
688 | 744 | } |
| 745 | + maxBottom += delta; |
689 | 746 | } |
690 | 747 | } |
691 | 748 |
|
692 | | - if (breakPage) // go back to move the whole row to the next page |
693 | | - { |
694 | | - if (i == 1) // do not leave single row in previous page |
695 | | - i = -1; // Start layout from the first row on new page |
696 | | - else |
697 | | - i--; |
698 | | - |
699 | | - maxBottom = 0; |
700 | | - continue; |
701 | | - } |
702 | | - |
703 | 749 | cury = maxBottom + GetVerticalSpacing(); |
704 | 750 |
|
705 | 751 | currentrow++; |
|
0 commit comments