Skip to content

Commit

Permalink
Writer HTML: Fixed null string for Text Elements (#2738)
Browse files Browse the repository at this point in the history
Co-authored-by: armagedon007 <000yurik000@gmail.com>
  • Loading branch information
Progi1984 and armagedon007 authored Feb 3, 2025
1 parent 2f270f2 commit 1be7a80
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
4 changes: 1 addition & 3 deletions src/PhpWord/Element/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,8 @@ public function getSource()

/**
* Get link text.
*
* @return string
*/
public function getText()
public function getText(): string
{
return $this->text;
}
Expand Down
4 changes: 1 addition & 3 deletions src/PhpWord/Element/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,8 @@ public function setText($text)

/**
* Get Text content.
*
* @return ?string
*/
public function getText()
public function getText(): ?string
{
return $this->text;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Writer/HTML/Element/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '&nbsp;';
}
Expand Down
43 changes: 43 additions & 0 deletions tests/PhpWordTests/Writer/HTML/Element/TextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWordTests\Writer\HTML\Element;

use PhpOffice\PhpWord\Element\Text as BaseText;
use PhpOffice\PhpWord\Writer\HTML;
use PhpOffice\PhpWord\Writer\HTML\Element\Text;
use PHPUnit\Framework\TestCase;

class TextTest extends TestCase
{
public function testHTMLNullString(): void
{
$writer = new HTML();
$object = new Text($writer, new BaseText());

self::assertEquals('<p>&nbsp;</p>' . PHP_EOL, $object->write());
}

public function testHTMLEmptyString(): void
{
$writer = new HTML();
$object = new Text($writer, new BaseText(''));

self::assertEquals('<p>&nbsp;</p>' . PHP_EOL, $object->write());
}
}

0 comments on commit 1be7a80

Please sign in to comment.