diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index ebd3ad4029..13d9bc0937 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -10,6 +10,7 @@ ### Bug fixes - Writer ODText: Support for images inside a textRun by [@Progi1984](https://github.com/Progi1984) fixing [#2240](https://github.com/PHPOffice/PHPWord/issues/2240) in [#2668](https://github.com/PHPOffice/PHPWord/pull/2668) +- Allow vAlign and vMerge on Style\Cell to be set to null by [@SpraxDev](https://github.com/SpraxDev) fixing [#2673](https://github.com/PHPOffice/PHPWord/issues/2673) in [#2676](https://github.com/PHPOffice/PHPWord/pull/2676) ### Miscellaneous diff --git a/src/PhpWord/Style/Cell.php b/src/PhpWord/Style/Cell.php index 3246471fd5..0888d52083 100644 --- a/src/PhpWord/Style/Cell.php +++ b/src/PhpWord/Style/Cell.php @@ -69,7 +69,7 @@ class Cell extends Border /** * Vertical align (top, center, both, bottom). * - * @var string + * @var null|string */ private $vAlign; @@ -93,7 +93,7 @@ class Cell extends Border * - restart: Start/restart merged region * - continue: Continue merged region * - * @var string + * @var null|string */ private $vMerge; @@ -128,7 +128,7 @@ class Cell extends Border /** * Get vertical align. * - * @return string + * @return null|string */ public function getVAlign() { @@ -138,12 +138,18 @@ public function getVAlign() /** * Set vertical align. * - * @param string $value + * @param null|string $value * * @return self */ public function setVAlign($value = null) { + if ($value === null) { + $this->vAlign = null; + + return $this; + } + VerticalJc::validate($value); $this->vAlign = $this->setEnumVal($value, VerticalJc::values(), $this->vAlign); @@ -235,7 +241,7 @@ public function setGridSpan($value = null) /** * Get vertical merge (rowspan). * - * @return string + * @return null|string */ public function getVMerge() { @@ -245,12 +251,18 @@ public function getVMerge() /** * Set vertical merge (rowspan). * - * @param string $value + * @param null|string $value * * @return self */ public function setVMerge($value = null) { + if ($value === null) { + $this->vMerge = null; + + return $this; + } + $enum = [self::VMERGE_RESTART, self::VMERGE_CONTINUE]; $this->vMerge = $this->setEnumVal($value, $enum, $this->vMerge); diff --git a/tests/PhpWordTests/Writer/HTML/Element/TableTest.php b/tests/PhpWordTests/Writer/HTML/Element/TableTest.php index 88b12e7204..8dec922108 100644 --- a/tests/PhpWordTests/Writer/HTML/Element/TableTest.php +++ b/tests/PhpWordTests/Writer/HTML/Element/TableTest.php @@ -20,6 +20,7 @@ use DOMXPath; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\SimpleType\VerticalJc; +use PhpOffice\PhpWord\Style; use PhpOffice\PhpWordTests\Writer\HTML\Helper; use PHPUnit\Framework\TestCase; @@ -179,6 +180,11 @@ public function testWriteTableCellVAlign(): void $cell->addText('bottom text'); $cell->getStyle()->setVAlign(VerticalJc::BOTTOM); + $cell = $row->addCell(); + $cell->addText('no vAlign'); + $cell->getStyle()->setVAlign(VerticalJc::BOTTOM); + $cell->getStyle()->setVAlign(); + $dom = Helper::getAsHTML($phpWord); $xpath = new DOMXPath($dom); @@ -186,5 +192,43 @@ public function testWriteTableCellVAlign(): void $cell2Style = Helper::getTextContent($xpath, '//table/tr/td[2]', 'style'); self::assertSame('vertical-align: top;', $cell1Style); self::assertSame('vertical-align: bottom;', $cell2Style); + + $cell3Query = $xpath->query('//table/tr/td[3]'); + self::assertNotFalse($cell3Query); + self::assertCount(1, $cell3Query); + + $cell3Style = $cell3Query->item(0)->attributes->getNamedItem('style'); + self::assertNull($cell3Style); + } + + public function testWriteTableCellVMerge(): void + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + + $table = $section->addTable(); + + $cell = $table->addRow()->addCell(); + $cell->addText('text'); + $cell->getStyle()->setVMerge(Style\Cell::VMERGE_RESTART); + + $cell = $table->addRow()->addCell(); + $cell->getStyle()->setVMerge(Style\Cell::VMERGE_CONTINUE); + + $cell = $table->addRow()->addCell(); + $cell->addText('no vMerge'); + $cell->getStyle()->setVMerge(Style\Cell::VMERGE_CONTINUE); + $cell->getStyle()->setVMerge(); + + $dom = Helper::getAsHTML($phpWord); + $xpath = new DOMXPath($dom); + + $cell1Style = Helper::getTextContent($xpath, '//table/tr[1]/td[1]', 'rowspan'); + self::assertSame('2', $cell1Style); + + $cell3Query = $xpath->query('//table/tr[3]/td[1]'); + self::assertNotFalse($cell3Query); + self::assertCount(1, $cell3Query); + self::assertNull($cell3Query->item(0)->attributes->getNamedItem('rowspan')); } }