Skip to content

Commit

Permalink
feat: Add support for vAlign styles in the HTML Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
SpraxDev committed Sep 13, 2024
1 parent 2e4f3cf commit 06a1444
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/PhpWord/Writer/HTML/Style/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
27 changes: 26 additions & 1 deletion tests/PhpWordTests/Writer/HTML/Element/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 06a1444

Please sign in to comment.