Skip to content

Commit 6d721f3

Browse files
armagedon007Progi1984
authored andcommitted
Writer HTML: Fixed null string for Text Elements
1 parent 2f270f2 commit 6d721f3

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

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

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
}

src/PhpWord/Element/Text.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function setText($text)
150150
*
151151
* @return ?string
152152
*/
153-
public function getText()
153+
public function getText(): ?string
154154
{
155155
return $this->text;
156156
}

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 = ' ';
7979
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\PhpWord;
23+
use PhpOffice\PhpWord\Settings;
24+
use PhpOffice\PhpWord\Writer\HTML;
25+
use PhpOffice\PhpWord\Writer\HTML\Element\Text;
26+
use PhpOffice\PhpWord\Writer\PDF;
27+
use PHPUnit\Framework\TestCase;
28+
29+
class TextTest extends TestCase
30+
{
31+
public function testHTMLNullString(): void
32+
{
33+
$writer = new HTML();
34+
$object = new Text($writer, new BaseText());
35+
36+
self::assertEquals('<p>&nbsp;</p>' . PHP_EOL, $object->write());
37+
}
38+
public function testHTMLEmptyString(): void
39+
{
40+
$writer = new HTML();
41+
$object = new Text($writer, new BaseText(''));
42+
43+
self::assertEquals('<p>&nbsp;</p>' . PHP_EOL, $object->write());
44+
}
45+
}

0 commit comments

Comments
 (0)