Skip to content

Commit f955a81

Browse files
committed
Fix TemplateProcessor::fixBrokenMacros to handle whitespaces
1 parent aca1078 commit f955a81

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,11 @@ protected function fixBrokenMacros($documentPart)
961961
return preg_replace_callback(
962962
'/\$(?:\{|[^{$]*\>\{)[^}$]*\}/U',
963963
function ($match) {
964-
return strip_tags($match[0]);
964+
preg_match_all('/<.+?\s*\/?\s*>/si', $match[0], $tags);
965+
$tags = implode('', $tags[0]);
966+
$tags = str_replace('<w:t>', '<w:t xml:space="preserve">', $tags);
967+
968+
return strip_tags($match[0]) . $tags;
965969
},
966970
$documentPart
967971
);

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,19 +695,28 @@ public function testFixBrokenMacros()
695695
$this->assertEquals('<w:r><w:t>${documentContent}</w:t></w:r>', $fixed);
696696

697697
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>$</w:t><w:t>{documentContent}</w:t></w:r>');
698-
$this->assertEquals('<w:r><w:t>${documentContent}</w:t></w:r>', $fixed);
698+
$this->assertEquals('<w:r><w:t>${documentContent}</w:t><w:t xml:space="preserve"></w:t></w:r>', $fixed);
699699

700700
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>$1500</w:t><w:t>${documentContent}</w:t></w:r>');
701701
$this->assertEquals('<w:r><w:t>$1500</w:t><w:t>${documentContent}</w:t></w:r>', $fixed);
702702

703703
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>$1500</w:t><w:t>$</w:t><w:t>{documentContent}</w:t></w:r>');
704-
$this->assertEquals('<w:r><w:t>$1500</w:t><w:t>${documentContent}</w:t></w:r>', $fixed);
704+
$this->assertEquals('<w:r><w:t>$1500</w:t><w:t>${documentContent}</w:t><w:t xml:space="preserve"></w:t></w:r>', $fixed);
705705

706706
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>25$ plus some info {hint}</w:t></w:r>');
707707
$this->assertEquals('<w:r><w:t>25$ plus some info {hint}</w:t></w:r>', $fixed);
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>');
710-
$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);
710+
$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><w:r w:rsidR="00573DFD" w:rsidRPr="00573DFD"><w:rPr><w:iCs/></w:rPr><w:t xml:space="preserve"></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 xml:space="preserve"></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 xml:space="preserve"></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 ${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>', $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 ${variable}</w:t></w:r><w:r><w:t xml:space="preserve"></w:t></w:r><w:r><w:t xml:space="preserve"> </w:t></w:r><w:r><w:t>after</w:t></w:r>', $fixed);
717+
718+
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>${</w:t></w:r><w:r><w:t>variable1</w:t></w:r><w:r><w:t>} ${</w:t></w:r><w:r><w:t>variable2</w:t></w:r><w:r><w:t>}</w:t></w:r>');
719+
$this->assertEquals('<w:r><w:t>${variable1}</w:t></w:r><w:r><w:t xml:space="preserve"></w:t></w:r><w:r><w:t xml:space="preserve"> ${variable2}</w:t></w:r><w:r><w:t xml:space="preserve"></w:t></w:r><w:r><w:t xml:space="preserve"></w:t></w:r>', $fixed);
711720
}
712721

713722
/**

0 commit comments

Comments
 (0)