From f955a818014be369a50371da2445dda1c6aaeaf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Bourgier?= Date: Fri, 20 Nov 2020 19:51:04 +0100 Subject: [PATCH] Fix TemplateProcessor::fixBrokenMacros to handle whitespaces --- src/PhpWord/TemplateProcessor.php | 6 +++++- tests/PhpWord/TemplateProcessorTest.php | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 3f7770d3d7..2eb26eb8c6 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -961,7 +961,11 @@ protected function fixBrokenMacros($documentPart) return preg_replace_callback( '/\$(?:\{|[^{$]*\>\{)[^}$]*\}/U', function ($match) { - return strip_tags($match[0]); + preg_match_all('/<.+?\s*\/?\s*>/si', $match[0], $tags); + $tags = implode('', $tags[0]); + $tags = str_replace('', '', $tags); + + return strip_tags($match[0]) . $tags; }, $documentPart ); diff --git a/tests/PhpWord/TemplateProcessorTest.php b/tests/PhpWord/TemplateProcessorTest.php index 391daa2de8..dd937b0acf 100644 --- a/tests/PhpWord/TemplateProcessorTest.php +++ b/tests/PhpWord/TemplateProcessorTest.php @@ -695,19 +695,28 @@ public function testFixBrokenMacros() $this->assertEquals('${documentContent}', $fixed); $fixed = $templateProcessor->fixBrokenMacros('${documentContent}'); - $this->assertEquals('${documentContent}', $fixed); + $this->assertEquals('${documentContent}', $fixed); $fixed = $templateProcessor->fixBrokenMacros('$1500${documentContent}'); $this->assertEquals('$1500${documentContent}', $fixed); $fixed = $templateProcessor->fixBrokenMacros('$1500${documentContent}'); - $this->assertEquals('$1500${documentContent}', $fixed); + $this->assertEquals('$1500${documentContent}', $fixed); $fixed = $templateProcessor->fixBrokenMacros('25$ plus some info {hint}'); $this->assertEquals('25$ plus some info {hint}', $fixed); $fixed = $templateProcessor->fixBrokenMacros('$15,000.00. ${variable_name}'); - $this->assertEquals('$15,000.00. ${variable_name}', $fixed); + $this->assertEquals('$15,000.00. ${variable_name}', $fixed); + + $fixed = $templateProcessor->fixBrokenMacros('before ${variable} after'); + $this->assertEquals('before ${variable} after', $fixed); + + $fixed = $templateProcessor->fixBrokenMacros('before ${variable} after'); + $this->assertEquals('before ${variable} after', $fixed); + + $fixed = $templateProcessor->fixBrokenMacros('${variable1} ${variable2}'); + $this->assertEquals('${variable1} ${variable2}', $fixed); } /**