Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow vAlign and vMerge on Style\Cell to be set to null #2676

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
}
}
Loading