Skip to content

Commit 06a1444

Browse files
committed
feat: Add support for vAlign styles in the HTML Writer
1 parent 2e4f3cf commit 06a1444

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/PhpWord/Writer/HTML/Style/Table.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public function write()
4646
$css['direction'] = 'rtl';
4747
}
4848
}
49+
if (is_object($style) && method_exists($style, 'getVAlign')) {
50+
$css['vertical-align'] = $style->getVAlign();
51+
}
4952

5053
foreach (['Top', 'Left', 'Bottom', 'Right'] as $direction) {
5154
$method = 'getBorder' . $direction . 'Style';

tests/PhpWordTests/Writer/HTML/Element/TableTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
use DOMXPath;
2121
use PhpOffice\PhpWord\PhpWord;
22-
use PhpOffice\PhpWord\Writer\HTML\Element\Table;
22+
use PhpOffice\PhpWord\SimpleType\VerticalJc;
2323
use PhpOffice\PhpWordTests\Writer\HTML\Helper;
2424
use PHPUnit\Framework\TestCase;
2525

@@ -162,4 +162,29 @@ public function testWriteTableBorders(): void
162162
self::assertNotFalse(preg_match('/^[.]tstyle[^\\r\\n]*/m', $style, $matches));
163163
self::assertEquals(".tstyle {table-layout: auto; $cssnone}", $matches[0]);
164164
}
165+
166+
public function testWriteTableCellVAlign(): void
167+
{
168+
$phpWord = new PhpWord();
169+
$section = $phpWord->addSection();
170+
171+
$table = $section->addTable();
172+
$row = $table->addRow();
173+
174+
$cell = $row->addCell();
175+
$cell->addText('top text');
176+
$cell->getStyle()->setVAlign(VerticalJc::TOP);
177+
178+
$cell = $row->addCell();
179+
$cell->addText('bottom text');
180+
$cell->getStyle()->setVAlign(VerticalJc::BOTTOM);
181+
182+
$dom = Helper::getAsHTML($phpWord);
183+
$xpath = new DOMXPath($dom);
184+
185+
$cell1Style = Helper::getTextContent($xpath, '//table/tr/td[1]', 'style');
186+
$cell2Style = Helper::getTextContent($xpath, '//table/tr/td[2]', 'style');
187+
self::assertSame('vertical-align: top;', $cell1Style);
188+
self::assertSame('vertical-align: bottom;', $cell2Style);
189+
}
165190
}

0 commit comments

Comments
 (0)