Skip to content

Commit 1be7a80

Browse files
Writer HTML: Fixed null string for Text Elements (#2738)
Co-authored-by: armagedon007 <[email protected]>
1 parent 2f270f2 commit 1be7a80

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

Diff for: docs/changes/1.x/1.4.0.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
- Writer ODText: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2735](https://github.com/PHPOffice/PHPWord/pull/2735)
1717
- 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)
1818
- 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)
19-
20-
- Added Support for Writer Epub3 by [@Sambit003](https://github.com/Sambit003) in [#2724](https://github.com/PHPOffice/PHPWord/pull/2724)
19+
- 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)
2120

2221
### Bug fixes
2322

@@ -27,6 +26,7 @@
2726
- 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)
2827
- 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)
2928
- 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)
29+
- 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)
3030

3131
### Miscellaneous
3232

Diff for: src/PhpWord/Element/Link.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,8 @@ public function getSource()
9999

100100
/**
101101
* Get link text.
102-
*
103-
* @return string
104102
*/
105-
public function getText()
103+
public function getText(): string
106104
{
107105
return $this->text;
108106
}

Diff for: src/PhpWord/Element/Text.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,8 @@ public function setText($text)
147147

148148
/**
149149
* Get Text content.
150-
*
151-
* @return ?string
152150
*/
153-
public function getText()
151+
public function getText(): ?string
154152
{
155153
return $this->text;
156154
}

Diff for: src/PhpWord/Writer/HTML/Element/Text.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function write()
7373
/** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
7474
$element = $this->element;
7575

76-
$text = $this->parentWriter->escapeHTML($element->getText());
76+
$text = $this->parentWriter->escapeHTML($element->getText() ?? '');
7777
if (!$this->withoutP && !trim($text)) {
7878
$text = '&nbsp;';
7979
}

Diff for: tests/PhpWordTests/Writer/HTML/Element/TextTest.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWordTests\Writer\HTML\Element;
20+
21+
use PhpOffice\PhpWord\Element\Text as BaseText;
22+
use PhpOffice\PhpWord\Writer\HTML;
23+
use PhpOffice\PhpWord\Writer\HTML\Element\Text;
24+
use PHPUnit\Framework\TestCase;
25+
26+
class TextTest extends TestCase
27+
{
28+
public function testHTMLNullString(): void
29+
{
30+
$writer = new HTML();
31+
$object = new Text($writer, new BaseText());
32+
33+
self::assertEquals('<p>&nbsp;</p>' . PHP_EOL, $object->write());
34+
}
35+
36+
public function testHTMLEmptyString(): void
37+
{
38+
$writer = new HTML();
39+
$object = new Text($writer, new BaseText(''));
40+
41+
self::assertEquals('<p>&nbsp;</p>' . PHP_EOL, $object->write());
42+
}
43+
}

0 commit comments

Comments
 (0)