Skip to content

Commit 0d22fa3

Browse files
authored
Merge pull request #2359 from kernusr/text-wrap-table-cell
Word2007 Reader/Writer : Added `noWrap` table cell property
2 parents 72a76b8 + 87c17d4 commit 0d22fa3

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

src/PhpWord/Reader/Word2007/AbstractPart.php

+1
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ private function readCellStyle(XMLReader $xmlReader, DOMElement $domNode)
564564
'gridSpan' => [self::READ_VALUE, 'w:gridSpan'],
565565
'vMerge' => [self::READ_VALUE, 'w:vMerge', null, null, 'continue'],
566566
'bgColor' => [self::READ_VALUE, 'w:shd', 'w:fill'],
567+
'noWrap' => [self::READ_VALUE, 'w:noWrap', null, null, true],
567568
];
568569

569570
return $this->readStyleDefs($xmlReader, $domNode, $styleDefs);

src/PhpWord/Style/Cell.php

+25
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ class Cell extends Border
118118
*/
119119
private $unit = TblWidth::TWIP;
120120

121+
/**
122+
* Prevent text from wrapping in the cell.
123+
*
124+
* @var bool
125+
*/
126+
private $noWrap = true;
127+
121128
/**
122129
* Get vertical align.
123130
*
@@ -319,4 +326,22 @@ public function setUnit($value)
319326

320327
return $this;
321328
}
329+
330+
/**
331+
* Set noWrap.
332+
*/
333+
public function setNoWrap(bool $value): self
334+
{
335+
$this->noWrap = $this->setBoolVal($value, true);
336+
337+
return $this;
338+
}
339+
340+
/**
341+
* Get noWrap.
342+
*/
343+
public function getNoWrap(): bool
344+
{
345+
return $this->noWrap;
346+
}
322347
}

src/PhpWord/Writer/Word2007/Style/Cell.php

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function write(): void
8888
$vMerge = $style->getVMerge();
8989
$xmlWriter->writeElementIf(null !== $gridSpan, 'w:gridSpan', 'w:val', $gridSpan);
9090
$xmlWriter->writeElementIf(null !== $vMerge, 'w:vMerge', 'w:val', $vMerge);
91+
$xmlWriter->writeElementIf($style->getNoWrap(), 'w:noWrap');
9192

9293
$xmlWriter->endElement(); // w:tcPr
9394
}

tests/PhpWordTests/Reader/Word2007/StyleTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,27 @@ public function testReadHidden(): void
190190
self::assertTrue($fontStyle->isHidden());
191191
}
192192

193+
public function testReadTableCellNoWrap(): void
194+
{
195+
$documentXml = '<w:tbl>
196+
<w:tr>
197+
<w:tc>
198+
<w:tcPr>
199+
<w:noWrap />
200+
</w:tcPr>
201+
</w:tc>
202+
</w:tr>
203+
</w:tbl>';
204+
205+
$phpWord = $this->getDocumentFromString(['document' => $documentXml]);
206+
207+
$elements = $phpWord->getSection(0)->getElements();
208+
self::assertInstanceOf('PhpOffice\PhpWord\Element\Table', $elements[0]);
209+
$rows = $elements[0]->getRows();
210+
$cells = $rows[0]->getCells();
211+
self::assertTrue($cells[0]->getStyle()->getNoWrap());
212+
}
213+
193214
public function testReadHeading(): void
194215
{
195216
Style::resetStyles();

tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,44 @@ public function testWriteCellStyleCellGridSpan(): void
655655
self::assertEquals(5, $element->getAttribute('w:val'));
656656
}
657657

658+
/**
659+
* covers ::_writeCellStyle.
660+
*/
661+
public function testWriteCellStyleCellNoWrapEnabled(): void
662+
{
663+
$phpWord = new PhpWord();
664+
$section = $phpWord->addSection();
665+
666+
$table = $section->addTable();
667+
$table->addRow();
668+
669+
$cell = $table->addCell(200);
670+
$cell->getStyle()->setNoWrap(true);
671+
672+
$doc = TestHelperDOCX::getDocument($phpWord);
673+
674+
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:noWrap'));
675+
}
676+
677+
/**
678+
* covers ::_writeCellStyle.
679+
*/
680+
public function testWriteCellStyleCellNoWrapDisabled(): void
681+
{
682+
$phpWord = new PhpWord();
683+
$section = $phpWord->addSection();
684+
685+
$table = $section->addTable();
686+
$table->addRow();
687+
688+
$cell = $table->addCell(200);
689+
$cell->getStyle()->setNoWrap(false);
690+
691+
$doc = TestHelperDOCX::getDocument($phpWord);
692+
693+
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:noWrap'));
694+
}
695+
658696
/**
659697
* Test write gutter and line numbering.
660698
*/

0 commit comments

Comments
 (0)