Skip to content

Commit fa0cea0

Browse files
authored
feat: Add support for vAlign styles in the HTML Writer (Table) (#2675)
* feat: Add support for vAlign styles in the HTML Writer * docs(changelog): Add note about supporting table vAlign in HTML Writer
1 parent 2e4f3cf commit fa0cea0

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

docs/changes/1.x/1.4.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## Enhancements
66

77
- 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)
8+
- Writer HTML: Support for vAlign in Tables by [@SpraxDev](https://github.com/SpraxDev) in [#2675](https://github.com/PHPOffice/PHPWord/pull/2675)
89

910
### Bug fixes
1011

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

+3
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

+26-1
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)