Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d532c7f

Browse files
committedNov 20, 2020
Fix TemplateProcessor::fixBrokenMacros to handle whitespaces
1 parent ec1b3d3 commit d532c7f

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
@@ -898,6 +898,10 @@ protected function fixBrokenMacros($documentPart)
898898
return preg_replace_callback(
899899
'/\$(?:\{|[^{$]*\>\{)[^}$]*\}/U',
900900
function ($match) {
901+
if (strpos($match[0], '<w:t xml:space="preserve">') !== false) {
902+
return sprintf('</w:t></w:r><w:r><w:t xml:space="preserve">%s', strip_tags($match[0]));
903+
}
904+
901905
return strip_tags($match[0]);
902906
},
903907
$documentPart

‎tests/PhpWord/TemplateProcessorTest.php

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

702702
$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>');
703703
$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);
704+
705+
$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>');
706+
$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);
707+
708+
$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>');
709+
$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);
704710
}
705711

706712
/**

0 commit comments

Comments
 (0)
Please sign in to comment.