From 06a1444c90b0ce0cbc538cbdf091de508b5203f2 Mon Sep 17 00:00:00 2001 From: Christian Koop Date: Thu, 12 Sep 2024 18:08:10 +0200 Subject: [PATCH] feat: Add support for vAlign styles in the HTML Writer --- src/PhpWord/Writer/HTML/Style/Table.php | 3 +++ .../Writer/HTML/Element/TableTest.php | 27 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/HTML/Style/Table.php b/src/PhpWord/Writer/HTML/Style/Table.php index d2c318a69f..53eea0e96f 100644 --- a/src/PhpWord/Writer/HTML/Style/Table.php +++ b/src/PhpWord/Writer/HTML/Style/Table.php @@ -46,6 +46,9 @@ public function write() $css['direction'] = 'rtl'; } } + if (is_object($style) && method_exists($style, 'getVAlign')) { + $css['vertical-align'] = $style->getVAlign(); + } foreach (['Top', 'Left', 'Bottom', 'Right'] as $direction) { $method = 'getBorder' . $direction . 'Style'; diff --git a/tests/PhpWordTests/Writer/HTML/Element/TableTest.php b/tests/PhpWordTests/Writer/HTML/Element/TableTest.php index 032b7b69cd..88b12e7204 100644 --- a/tests/PhpWordTests/Writer/HTML/Element/TableTest.php +++ b/tests/PhpWordTests/Writer/HTML/Element/TableTest.php @@ -19,7 +19,7 @@ use DOMXPath; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Writer\HTML\Element\Table; +use PhpOffice\PhpWord\SimpleType\VerticalJc; use PhpOffice\PhpWordTests\Writer\HTML\Helper; use PHPUnit\Framework\TestCase; @@ -162,4 +162,29 @@ public function testWriteTableBorders(): void self::assertNotFalse(preg_match('/^[.]tstyle[^\\r\\n]*/m', $style, $matches)); self::assertEquals(".tstyle {table-layout: auto; $cssnone}", $matches[0]); } + + public function testWriteTableCellVAlign(): void + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + + $table = $section->addTable(); + $row = $table->addRow(); + + $cell = $row->addCell(); + $cell->addText('top text'); + $cell->getStyle()->setVAlign(VerticalJc::TOP); + + $cell = $row->addCell(); + $cell->addText('bottom text'); + $cell->getStyle()->setVAlign(VerticalJc::BOTTOM); + + $dom = Helper::getAsHTML($phpWord); + $xpath = new DOMXPath($dom); + + $cell1Style = Helper::getTextContent($xpath, '//table/tr/td[1]', 'style'); + $cell2Style = Helper::getTextContent($xpath, '//table/tr/td[2]', 'style'); + self::assertSame('vertical-align: top;', $cell1Style); + self::assertSame('vertical-align: bottom;', $cell2Style); + } }