Skip to content

Commit

Permalink
Merge pull request #2359 from kernusr/text-wrap-table-cell
Browse files Browse the repository at this point in the history
Word2007 Reader/Writer : Added `noWrap` table cell property
  • Loading branch information
Progi1984 authored Aug 30, 2023
2 parents 72a76b8 + 87c17d4 commit 0d22fa3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ private function readCellStyle(XMLReader $xmlReader, DOMElement $domNode)
'gridSpan' => [self::READ_VALUE, 'w:gridSpan'],
'vMerge' => [self::READ_VALUE, 'w:vMerge', null, null, 'continue'],
'bgColor' => [self::READ_VALUE, 'w:shd', 'w:fill'],
'noWrap' => [self::READ_VALUE, 'w:noWrap', null, null, true],
];

return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
Expand Down
25 changes: 25 additions & 0 deletions src/PhpWord/Style/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ class Cell extends Border
*/
private $unit = TblWidth::TWIP;

/**
* Prevent text from wrapping in the cell.
*
* @var bool
*/
private $noWrap = true;

/**
* Get vertical align.
*
Expand Down Expand Up @@ -319,4 +326,22 @@ public function setUnit($value)

return $this;
}

/**
* Set noWrap.
*/
public function setNoWrap(bool $value): self
{
$this->noWrap = $this->setBoolVal($value, true);

return $this;
}

/**
* Get noWrap.
*/
public function getNoWrap(): bool
{
return $this->noWrap;
}
}
1 change: 1 addition & 0 deletions src/PhpWord/Writer/Word2007/Style/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function write(): void
$vMerge = $style->getVMerge();
$xmlWriter->writeElementIf(null !== $gridSpan, 'w:gridSpan', 'w:val', $gridSpan);
$xmlWriter->writeElementIf(null !== $vMerge, 'w:vMerge', 'w:val', $vMerge);
$xmlWriter->writeElementIf($style->getNoWrap(), 'w:noWrap');

$xmlWriter->endElement(); // w:tcPr
}
Expand Down
21 changes: 21 additions & 0 deletions tests/PhpWordTests/Reader/Word2007/StyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,27 @@ public function testReadHidden(): void
self::assertTrue($fontStyle->isHidden());
}

public function testReadTableCellNoWrap(): void
{
$documentXml = '<w:tbl>
<w:tr>
<w:tc>
<w:tcPr>
<w:noWrap />
</w:tcPr>
</w:tc>
</w:tr>
</w:tbl>';

$phpWord = $this->getDocumentFromString(['document' => $documentXml]);

$elements = $phpWord->getSection(0)->getElements();
self::assertInstanceOf('PhpOffice\PhpWord\Element\Table', $elements[0]);
$rows = $elements[0]->getRows();
$cells = $rows[0]->getCells();
self::assertTrue($cells[0]->getStyle()->getNoWrap());
}

public function testReadHeading(): void
{
Style::resetStyles();
Expand Down
38 changes: 38 additions & 0 deletions tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,44 @@ public function testWriteCellStyleCellGridSpan(): void
self::assertEquals(5, $element->getAttribute('w:val'));
}

/**
* covers ::_writeCellStyle.
*/
public function testWriteCellStyleCellNoWrapEnabled(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();

$table = $section->addTable();
$table->addRow();

$cell = $table->addCell(200);
$cell->getStyle()->setNoWrap(true);

$doc = TestHelperDOCX::getDocument($phpWord);

self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:noWrap'));
}

/**
* covers ::_writeCellStyle.
*/
public function testWriteCellStyleCellNoWrapDisabled(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();

$table = $section->addTable();
$table->addRow();

$cell = $table->addCell(200);
$cell->getStyle()->setNoWrap(false);

$doc = TestHelperDOCX::getDocument($phpWord);

self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:noWrap'));
}

/**
* Test write gutter and line numbering.
*/
Expand Down

0 comments on commit 0d22fa3

Please sign in to comment.