Skip to content

Commit

Permalink
feat: Add support for vAlign styles in the HTML Writer (Table) (#2675)
Browse files Browse the repository at this point in the history
* feat: Add support for vAlign styles in the HTML Writer

* docs(changelog): Add note about supporting table vAlign in HTML Writer
  • Loading branch information
SpraxDev authored Sep 13, 2024
1 parent 2e4f3cf commit fa0cea0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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 fa0cea0

Please sign in to comment.