From 9992fe05c079a4643ffb03395c95f50c436786d9 Mon Sep 17 00:00:00 2001 From: Florian CAVASIN Date: Thu, 30 May 2024 11:34:36 +0200 Subject: [PATCH 1/3] Fix `0` considered as empty string --- src/PhpWord/TemplateProcessor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 643bae4cb6..360cce74b6 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -269,7 +269,7 @@ protected static function ensureMacroCompleted($macro) */ protected static function ensureUtf8Encoded($subject) { - return $subject ? Text::toUTF8($subject) : ''; + return (null !== $subject) ? Text::toUTF8($subject) : ''; } /** From 1026015a83900ccee6eabffbec767b46a202f202 Mon Sep 17 00:00:00 2001 From: Florian CAVASIN Date: Mon, 14 Oct 2024 09:23:43 +0200 Subject: [PATCH 2/3] Update changelog --- docs/changes/1.x/1.4.0.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 2afadebd5f..31037d02f0 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -27,6 +27,7 @@ - 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) +- TemplateProcessor: Fix 0 considered as empty string. Fixing [#2572](https://github.com/PHPOffice/PHPWord/issues/2572) in [#2617](https://github.com/PHPOffice/PHPWord/pull/2617) ### Miscellaneous From 9a5a6f3fc79a350c48bd205f29ae731b48b881f8 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Wed, 12 Feb 2025 12:02:52 +0100 Subject: [PATCH 3/3] Updated changelog & Added unit test --- docs/changes/1.x/1.4.0.md | 2 +- tests/PhpWordTests/TemplateProcessorTest.php | 97 ++++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 31037d02f0..b5102f25fa 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -27,7 +27,7 @@ - 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) -- TemplateProcessor: Fix 0 considered as empty string. Fixing [#2572](https://github.com/PHPOffice/PHPWord/issues/2572) in [#2617](https://github.com/PHPOffice/PHPWord/pull/2617) +- Template Processor: Fix 0 considered as empty string by [@cavasinf](https://github.com/cavasinf), [@SnipsMine](https://github.com/SnipsMine) and [@Progi1984](https://github.com/Progi1984) fixing [#2572](https://github.com/PHPOffice/PHPWord/issues/2572), [#2703](https://github.com/PHPOffice/PHPWord/issues/2703) in [#2748](https://github.com/PHPOffice/PHPWord/pull/2748) ### Miscellaneous diff --git a/tests/PhpWordTests/TemplateProcessorTest.php b/tests/PhpWordTests/TemplateProcessorTest.php index 9ba6933e74..d031eb75c1 100644 --- a/tests/PhpWordTests/TemplateProcessorTest.php +++ b/tests/PhpWordTests/TemplateProcessorTest.php @@ -1641,4 +1641,101 @@ public function testShouldMakeFieldsUpdateOnOpenWithCustomMacro(): void $templateProcessor->setUpdateFields(false); self::assertStringContainsString('', $templateProcessor->getSettingsPart()); } + + public function testEnsureUtf8Encoded(): void + { + $mainPart = ' + + + + + + + + ${stringZero} + + + + + + + ${intZero} + + + + + + + ${stringTest} + + + + + + + ${null} + + + + + + + ${floatZero} + + + + + + + ${intTen} + + + + + + + ${boolFalse} + + + + + + + ${boolTrue} + + + + + '; + $templateProcessor = new TestableTemplateProcesor($mainPart); + + self::assertEquals( + ['stringZero', 'intZero', 'stringTest', 'null', 'floatZero', 'intTen', 'boolFalse', 'boolTrue'], + $templateProcessor->getVariables() + ); + + $templateProcessor->setValue('stringZero', '0'); + self::assertStringContainsString('0', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('intZero', 0); + self::assertStringContainsString('0', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('stringTest', 'test'); + self::assertStringContainsString('test', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('null', null); + self::assertStringContainsString('', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('floatZero', 0.00); + self::assertStringContainsString('0', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('intTen', 10); + self::assertStringContainsString('10', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('boolFalse', false); + self::assertStringContainsString('', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('boolTrue', true); + self::assertStringContainsString('1', $templateProcessor->getMainPart()); + } }