|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Mpdf; |
| 4 | + |
| 5 | +/** |
| 6 | + * page-break-before and break-before on a top-level table start it on a new page, as they do for a block. The blocks |
| 7 | + * around the table are closed and opened again on the new page, with the table inside them. |
| 8 | + */ |
| 9 | +class TablePageBreakBeforeTest extends \Yoast\PHPUnitPolyfills\TestCases\TestCase |
| 10 | +{ |
| 11 | + |
| 12 | + /** |
| 13 | + * The table's text is drawn on the page the break names, and the text after the table follows it |
| 14 | + * |
| 15 | + * @dataProvider providerBreakBefore |
| 16 | + */ |
| 17 | + public function testBreakBeforeTable($html, $pages) |
| 18 | + { |
| 19 | + $lines = $this->lines($html, ['mirrorMargins' => true]); |
| 20 | + |
| 21 | + $this->assertSame($pages, array_map(function ($line) { |
| 22 | + return $line[0]; |
| 23 | + }, $lines)); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Break values on a table, written inline and in a style sheet, and on a nested table |
| 28 | + * |
| 29 | + * @return array |
| 30 | + */ |
| 31 | + public function providerBreakBefore() |
| 32 | + { |
| 33 | + $table = '<table style="%s"><tr><td>MB</td></tr></table>'; |
| 34 | + |
| 35 | + return [ |
| 36 | + 'always' => ['<p>MA</p>' . sprintf($table, 'page-break-before: always') . '<p>MC</p>', ['MA' => 1, 'MB' => 2, 'MC' => 2]], |
| 37 | + 'break-before: page' => ['<p>MA</p>' . sprintf($table, 'break-before: page') . '<p>MC</p>', ['MA' => 1, 'MB' => 2, 'MC' => 2]], |
| 38 | + 'right' => ['<p>MA</p>' . sprintf($table, 'page-break-before: right'), ['MA' => 1, 'MB' => 3]], |
| 39 | + 'left' => ['<p>MA</p>' . sprintf($table, 'page-break-before: left'), ['MA' => 1, 'MB' => 2]], |
| 40 | + 'style sheet' => ['<style>table.new { page-break-before: always; }</style><p>MA</p><table class="new"><tr><td>MB</td></tr></table>', ['MA' => 1, 'MB' => 2]], |
| 41 | + 'auto' => ['<p>MA</p>' . sprintf($table, 'page-break-before: auto'), ['MA' => 1, 'MB' => 1]], |
| 42 | + 'avoid' => ['<p>MA</p>' . sprintf($table, 'page-break-before: avoid'), ['MA' => 1, 'MB' => 1]], |
| 43 | + 'nested table' => ['<table><tr><td>MA' . sprintf($table, 'page-break-before: always') . '</td></tr></table>', ['MA' => 1, 'MB' => 1]], |
| 44 | + ]; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * A table that breaks inside a bordered block stays inside it on the new page, below its border and padding |
| 49 | + */ |
| 50 | + public function testTableStaysInsideTheBlockAroundIt() |
| 51 | + { |
| 52 | + $html = '<table style="page-break-before: always"><tr><td>MB</td></tr></table>'; |
| 53 | + |
| 54 | + $alone = $this->lines('<p>MA</p>' . $html); |
| 55 | + $inside = $this->lines('<div style="border: 1mm solid red; padding: 3mm"><p>MA</p>' . $html . '<p>MC</p></div>'); |
| 56 | + |
| 57 | + $this->assertSame(2, $inside['MB'][0]); |
| 58 | + $this->assertSame(2, $inside['MC'][0]); |
| 59 | + $this->assertEqualsWithDelta($alone['MB'][1] + 1 + 3, $inside['MB'][1], 0.001); |
| 60 | + $this->assertGreaterThan($inside['MB'][1], $inside['MC'][1]); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Render a document and find where each piece of text starting with "M" is drawn |
| 65 | + * |
| 66 | + * @param string $html |
| 67 | + * @param array $config |
| 68 | + * @return array[] The page and top of each marker, keyed by the marker |
| 69 | + */ |
| 70 | + private function lines($html, array $config = []) |
| 71 | + { |
| 72 | + $mpdf = new TextRecordingMpdf($config); |
| 73 | + $mpdf->WriteHTML($html); |
| 74 | + |
| 75 | + $lines = []; |
| 76 | + foreach ($mpdf->drawnText as $i => $text) { |
| 77 | + if (preg_match('/^M[A-Z]$/', trim($text))) { |
| 78 | + $lines[trim($text)] = [$mpdf->drawnBoxes[$i][0], $mpdf->drawnY[$i]]; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return $lines; |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments