Skip to content

Commit

Permalink
fix: Allow vAlign and vMerge on Style\Cell to be set to null (#2676)
Browse files Browse the repository at this point in the history
* fix: Allow vAlign and vMerge on Style\Cell to be set to null (#2673)

vAlign and vMerge are initialized as `null` in every style until it is explicitly set.
Right now, it is not possible to unset them, after it has been set once.

I've added a null-check to skip the validation, based on the default
parameter value of `null`, which indicates to me that it once was intended to work like this.

I've also fixed the type-hints, which were wrong from the start.

* docs(changelog): Add note about supporting vAlign and vMerge to be unset

* test: Extend existing table test case to check if unsetting vAlign works

* test: Add test case to check if unsetting vMerge works

This should fix the reduction in test coverage because of the
new null checking code in the Setter
  • Loading branch information
SpraxDev authored Sep 13, 2024
1 parent fa0cea0 commit f0ec497
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
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 @@ -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

Expand Down
24 changes: 18 additions & 6 deletions src/PhpWord/Style/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Cell extends Border
/**
* Vertical align (top, center, both, bottom).
*
* @var string
* @var null|string
*/
private $vAlign;

Expand All @@ -93,7 +93,7 @@ class Cell extends Border
* - restart: Start/restart merged region
* - continue: Continue merged region
*
* @var string
* @var null|string
*/
private $vMerge;

Expand Down Expand Up @@ -128,7 +128,7 @@ class Cell extends Border
/**
* Get vertical align.
*
* @return string
* @return null|string
*/
public function getVAlign()
{
Expand All @@ -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);

Expand Down Expand Up @@ -235,7 +241,7 @@ public function setGridSpan($value = null)
/**
* Get vertical merge (rowspan).
*
* @return string
* @return null|string
*/
public function getVMerge()
{
Expand All @@ -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);

Expand Down
44 changes: 44 additions & 0 deletions tests/PhpWordTests/Writer/HTML/Element/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -179,12 +180,55 @@ 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);

$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);

$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'));
}
}

0 comments on commit f0ec497

Please sign in to comment.