diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index 6814540290..b1afccaafb 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -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); diff --git a/src/PhpWord/Style/Cell.php b/src/PhpWord/Style/Cell.php index aa065f01de..3246471fd5 100644 --- a/src/PhpWord/Style/Cell.php +++ b/src/PhpWord/Style/Cell.php @@ -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. * @@ -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; + } } diff --git a/src/PhpWord/Writer/Word2007/Style/Cell.php b/src/PhpWord/Writer/Word2007/Style/Cell.php index 9a489b6622..6e22597dd3 100644 --- a/src/PhpWord/Writer/Word2007/Style/Cell.php +++ b/src/PhpWord/Writer/Word2007/Style/Cell.php @@ -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 } diff --git a/tests/PhpWordTests/Reader/Word2007/StyleTest.php b/tests/PhpWordTests/Reader/Word2007/StyleTest.php index 2aeb651c60..be80dc513e 100644 --- a/tests/PhpWordTests/Reader/Word2007/StyleTest.php +++ b/tests/PhpWordTests/Reader/Word2007/StyleTest.php @@ -190,6 +190,27 @@ public function testReadHidden(): void self::assertTrue($fontStyle->isHidden()); } + public function testReadTableCellNoWrap(): void + { + $documentXml = ' + + + + + + + + '; + + $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(); diff --git a/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php index be8f8cc4bf..04bdb54ed7 100644 --- a/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php @@ -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. */