Skip to content

Commit 67528ad

Browse files
committed
Fix TemplateProcessor::fixBrokenMacros to handle whitespaces
1 parent 1bd7cd6 commit 67528ad

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,10 @@ protected function fixBrokenMacros($documentPart)
959959
return preg_replace_callback(
960960
'/\$(?:\{|[^{$]*\>\{)[^}$]*\}/U',
961961
function ($match) {
962+
if (strpos($match[0], '<w:t xml:space="preserve">') !== false) {
963+
return sprintf('</w:t></w:r><w:r><w:t xml:space="preserve">%s', strip_tags($match[0]));
964+
}
965+
962966
return strip_tags($match[0]);
963967
},
964968
$documentPart

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,12 @@ public function testFixBrokenMacros()
708708

709709
$fixed = $templateProcessor->fixBrokenMacros('<w:t>$</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:t xml:space="preserve">15,000.00. </w:t></w:r><w:r w:rsidR="0056499B"><w:t>$</w:t></w:r><w:r w:rsidR="00573DFD" w:rsidRPr="00573DFD"><w:rPr><w:iCs/></w:rPr><w:t>{</w:t></w:r><w:proofErr w:type="spellStart"/><w:r w:rsidR="00573DFD" w:rsidRPr="00573DFD"><w:rPr><w:iCs/></w:rPr><w:t>variable_name</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidR="00573DFD" w:rsidRPr="00573DFD"><w:rPr><w:iCs/></w:rPr><w:t>}</w:t></w:r>');
710710
$this->assertEquals('<w:t>$</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:t xml:space="preserve">15,000.00. </w:t></w:r><w:r w:rsidR="0056499B"><w:t>${variable_name}</w:t></w:r>', $fixed);
711+
712+
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>before ${</w:t></w:r><w:r><w:t xml:space="preserve">variable} </w:t></w:r><w:r><w:t>after</w:t></w:r>');
713+
$this->assertEquals('<w:r><w:t>before </w:t></w:r><w:r><w:t xml:space="preserve">${variable} </w:t></w:r><w:r><w:t>after</w:t></w:r>', $fixed);
714+
715+
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>before ${</w:t></w:r><w:r><w:t>variable</w:t></w:r><w:r><w:t xml:space="preserve">} </w:t></w:r><w:r><w:t>after</w:t></w:r>');
716+
$this->assertEquals('<w:r><w:t>before </w:t></w:r><w:r><w:t xml:space="preserve">${variable} </w:t></w:r><w:r><w:t>after</w:t></w:r>', $fixed);
711717
}
712718

713719
/**

0 commit comments

Comments
 (0)