From 06a1444c90b0ce0cbc538cbdf091de508b5203f2 Mon Sep 17 00:00:00 2001 From: Christian Koop Date: Thu, 12 Sep 2024 18:08:10 +0200 Subject: [PATCH 1/2] 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); + } } From 70b6536803b9f210c6cb511a535b920b8c52f8e7 Mon Sep 17 00:00:00 2001 From: Christian Koop Date: Fri, 13 Sep 2024 10:46:02 +0200 Subject: [PATCH 2/2] docs(changelog): Add note about supporting table vAlign in HTML Writer --- docs/changes/1.x/1.4.0.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index cdab8bdc77..ebd3ad4029 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -5,6 +5,7 @@ ## Enhancements - Writer ODText: Support for ListItemRun by [@Progi1984](https://github.com/Progi1984) fixing [#2159](https://github.com/PHPOffice/PHPWord/issues/2159), [#2620](https://github.com/PHPOffice/PHPWord/issues/2620) in [#2669](https://github.com/PHPOffice/PHPWord/pull/2669) +- Writer HTML: Support for vAlign in Tables by [@SpraxDev](https://github.com/SpraxDev) in [#2675](https://github.com/PHPOffice/PHPWord/pull/2675) ### Bug fixes