From 3f2b90c40ed39eca2423594df516e7c635b9d65f Mon Sep 17 00:00:00 2001 From: Artem Vasilev Date: Tue, 20 Dec 2022 20:46:28 +0300 Subject: [PATCH 1/7] Add nowrap parameter for table cell --- src/PhpWord/Reader/Word2007/AbstractPart.php | 1 + src/PhpWord/Style/Cell.php | 29 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index df3cfed709..96ef9b28d7 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -560,6 +560,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 b2bd9aeb21..56b26c42e7 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; + /** * Get vertical align. * @@ -312,4 +319,26 @@ public function setUnit($value) return $this; } + + /** + * Set noWrap + * + * @param $value + */ + public function setNoWrap($value) + { + $this->noWrap = $this->setBoolVal($value, true); + + return $this; + } + + /** + * Get noWrap + * + * @return bool + */ + public function getNoWrap() + { + return $this->noWrap; + } } From 9ba0fa762b95c1f5e35abb1da62d87592dee3fc3 Mon Sep 17 00:00:00 2001 From: Artem Vasilev Date: Tue, 20 Dec 2022 20:51:04 +0300 Subject: [PATCH 2/7] ADD: noWrap to Word2007 writer --- src/PhpWord/Writer/Word2007/Style/Cell.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/PhpWord/Writer/Word2007/Style/Cell.php b/src/PhpWord/Writer/Word2007/Style/Cell.php index 9a489b6622..870419cadd 100644 --- a/src/PhpWord/Writer/Word2007/Style/Cell.php +++ b/src/PhpWord/Writer/Word2007/Style/Cell.php @@ -89,6 +89,10 @@ public function write(): void $xmlWriter->writeElementIf(null !== $gridSpan, 'w:gridSpan', 'w:val', $gridSpan); $xmlWriter->writeElementIf(null !== $vMerge, 'w:vMerge', 'w:val', $vMerge); + // noWrap + $noWrap = $style->getNoWrap(); + $xmlWriter->writeElementIf(true === $noWrap, 'w:noWrap'); + $xmlWriter->endElement(); // w:tcPr } From 831c1bc6ac3db2a9b046338cc22c69ae110da9bc Mon Sep 17 00:00:00 2001 From: Artem Vasilev Date: Tue, 20 Dec 2022 21:09:02 +0300 Subject: [PATCH 3/7] FIX: code style --- src/PhpWord/Reader/Word2007/AbstractPart.php | 2 +- src/PhpWord/Style/Cell.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index 96ef9b28d7..6c7fd245a7 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -560,7 +560,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] + '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 56b26c42e7..e96fb65422 100644 --- a/src/PhpWord/Style/Cell.php +++ b/src/PhpWord/Style/Cell.php @@ -119,7 +119,7 @@ class Cell extends Border private $unit = TblWidth::TWIP; /** - * Prevent text from wrapping in the cell + * Prevent text from wrapping in the cell. * * @var bool */ @@ -321,7 +321,7 @@ public function setUnit($value) } /** - * Set noWrap + * Set noWrap. * * @param $value */ @@ -333,7 +333,7 @@ public function setNoWrap($value) } /** - * Get noWrap + * Get noWrap. * * @return bool */ From 7601a3ed25c5c14f3b257d82acb14683519ad97f Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Wed, 30 Aug 2023 09:05:11 +0200 Subject: [PATCH 4/7] Fixed some typehint --- src/PhpWord/Style/Cell.php | 9 +++++---- src/PhpWord/Writer/Word2007/Style/Cell.php | 5 +---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/PhpWord/Style/Cell.php b/src/PhpWord/Style/Cell.php index e96fb65422..2ba0713c27 100644 --- a/src/PhpWord/Style/Cell.php +++ b/src/PhpWord/Style/Cell.php @@ -123,7 +123,7 @@ class Cell extends Border * * @var bool */ - private $noWrap; + private $noWrap = true; /** * Get vertical align. @@ -323,9 +323,10 @@ public function setUnit($value) /** * Set noWrap. * - * @param $value + * @param bool $value + * @return self */ - public function setNoWrap($value) + public function setNoWrap(bool $value): self { $this->noWrap = $this->setBoolVal($value, true); @@ -337,7 +338,7 @@ public function setNoWrap($value) * * @return bool */ - public function getNoWrap() + 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 870419cadd..6e22597dd3 100644 --- a/src/PhpWord/Writer/Word2007/Style/Cell.php +++ b/src/PhpWord/Writer/Word2007/Style/Cell.php @@ -88,10 +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); - - // noWrap - $noWrap = $style->getNoWrap(); - $xmlWriter->writeElementIf(true === $noWrap, 'w:noWrap'); + $xmlWriter->writeElementIf($style->getNoWrap(), 'w:noWrap'); $xmlWriter->endElement(); // w:tcPr } From 32aa83e197d4c13a4c1d5da73ba5bd67354d6a32 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Wed, 30 Aug 2023 09:07:20 +0200 Subject: [PATCH 5/7] Removed PHPDoc --- src/PhpWord/Style/Cell.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/PhpWord/Style/Cell.php b/src/PhpWord/Style/Cell.php index 2ba0713c27..3bd4a6cae6 100644 --- a/src/PhpWord/Style/Cell.php +++ b/src/PhpWord/Style/Cell.php @@ -322,9 +322,6 @@ public function setUnit($value) /** * Set noWrap. - * - * @param bool $value - * @return self */ public function setNoWrap(bool $value): self { @@ -335,8 +332,6 @@ public function setNoWrap(bool $value): self /** * Get noWrap. - * - * @return bool */ public function getNoWrap(): bool { From 63db12440585eb5e463f6a734f43d25fdee3d20f Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Wed, 30 Aug 2023 09:37:10 +0200 Subject: [PATCH 6/7] Word2007 Writer : Added unit test --- .../Writer/Word2007/Part/DocumentTest.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php index 98449f3b6d..95cdcf87fc 100644 --- a/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php @@ -645,6 +645,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. */ From 87c17d4a68401121cde4f974539a9f6a4ddbb500 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Wed, 30 Aug 2023 09:55:12 +0200 Subject: [PATCH 7/7] Word2007 Reader : Added unit test --- .../Reader/Word2007/StyleTest.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) 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();