From 1be7a807f2a89c48ddac833e2de01dc29d6c0afb Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Mon, 3 Feb 2025 09:13:23 +0100 Subject: [PATCH] Writer HTML: Fixed null string for Text Elements (#2738) Co-authored-by: armagedon007 <000yurik000@gmail.com> --- docs/changes/1.x/1.4.0.md | 4 +- src/PhpWord/Element/Link.php | 4 +- src/PhpWord/Element/Text.php | 4 +- src/PhpWord/Writer/HTML/Element/Text.php | 2 +- .../Writer/HTML/Element/TextTest.php | 43 +++++++++++++++++++ 5 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 tests/PhpWordTests/Writer/HTML/Element/TextTest.php diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 0f225edd64..2afadebd5f 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -16,8 +16,7 @@ - Writer ODText: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2735](https://github.com/PHPOffice/PHPWord/pull/2735) - Add basic ruby text (phonetic guide) support for Word2007 and HTML Reader/Writer, RTF Writer, basic support for ODT writing by [@Deadpikle](https://github.com/Deadpikle) in [#2727](https://github.com/PHPOffice/PHPWord/pull/2727) - Reader HTML: Support font styles for h1/h6 by [@Progi1984](https://github.com/Progi1984) fixing [#2619](https://github.com/PHPOffice/PHPWord/issues/2619) in [#2737](https://github.com/PHPOffice/PHPWord/pull/2737) - -- Added Support for Writer Epub3 by [@Sambit003](https://github.com/Sambit003) in [#2724](https://github.com/PHPOffice/PHPWord/pull/2724) +- Writer EPub3: Basic support by [@Sambit003](https://github.com/Sambit003) fixing [#55](https://github.com/PHPOffice/PHPWord/issues/55) in [#2724](https://github.com/PHPOffice/PHPWord/pull/2724) ### Bug fixes @@ -27,6 +26,7 @@ - Reader Word2007: Respect paragraph indent units by [@tugmaks](https://github.com/tugmaks) & [@Progi1984](https://github.com/Progi1984) fixing [#507](https://github.com/PHPOffice/PHPWord/issues/507) in [#2726](https://github.com/PHPOffice/PHPWord/pull/2726) - Reader Word2007: Support Header elements within Title elements by [@SpraxDev](https://github.com/SpraxDev) fixing [#2616](https://github.com/PHPOffice/PHPWord/issues/2616), [#2426](https://github.com/PHPOffice/PHPWord/issues/2426) in [#2674](https://github.com/PHPOffice/PHPWord/pull/2674) - Reader HTML: Support for inherit value for property line-height by [@Progi1984](https://github.com/Progi1984) fixing [#2683](https://github.com/PHPOffice/PHPWord/issues/2683) in [#2733](https://github.com/PHPOffice/PHPWord/pull/2733) +- Writer HTML: Fixed null string for Text Elements by [@armagedon007](https://github.com/armagedon007) and [@Progi1984](https://github.com/Progi1984) in [#2738](https://github.com/PHPOffice/PHPWord/pull/2738) ### Miscellaneous diff --git a/src/PhpWord/Element/Link.php b/src/PhpWord/Element/Link.php index 755fd7ba0f..a0deb0a5ec 100644 --- a/src/PhpWord/Element/Link.php +++ b/src/PhpWord/Element/Link.php @@ -99,10 +99,8 @@ public function getSource() /** * Get link text. - * - * @return string */ - public function getText() + public function getText(): string { return $this->text; } diff --git a/src/PhpWord/Element/Text.php b/src/PhpWord/Element/Text.php index ea5f5f87a4..f20b273e02 100644 --- a/src/PhpWord/Element/Text.php +++ b/src/PhpWord/Element/Text.php @@ -147,10 +147,8 @@ public function setText($text) /** * Get Text content. - * - * @return ?string */ - public function getText() + public function getText(): ?string { return $this->text; } diff --git a/src/PhpWord/Writer/HTML/Element/Text.php b/src/PhpWord/Writer/HTML/Element/Text.php index 312d3a19c2..7be95a5c76 100644 --- a/src/PhpWord/Writer/HTML/Element/Text.php +++ b/src/PhpWord/Writer/HTML/Element/Text.php @@ -73,7 +73,7 @@ public function write() /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ $element = $this->element; - $text = $this->parentWriter->escapeHTML($element->getText()); + $text = $this->parentWriter->escapeHTML($element->getText() ?? ''); if (!$this->withoutP && !trim($text)) { $text = ' '; } diff --git a/tests/PhpWordTests/Writer/HTML/Element/TextTest.php b/tests/PhpWordTests/Writer/HTML/Element/TextTest.php new file mode 100644 index 0000000000..2dfdd53be3 --- /dev/null +++ b/tests/PhpWordTests/Writer/HTML/Element/TextTest.php @@ -0,0 +1,43 @@ + 

' . PHP_EOL, $object->write()); + } + + public function testHTMLEmptyString(): void + { + $writer = new HTML(); + $object = new Text($writer, new BaseText('')); + + self::assertEquals('

 

' . PHP_EOL, $object->write()); + } +}