Skip to content

Fix TemplateProcessor::fixBrokenMacros to handle whitespaces #1971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1063,14 +1063,28 @@ public function saveAs($fileName): void
*/
protected function fixBrokenMacros($documentPart)
{
$brokenMacroOpeningChars = substr(self::$macroOpeningChars, 0, 1);
$endMacroOpeningChars = substr(self::$macroOpeningChars, 1);
$macroClosingChars = self::$macroClosingChars;

return preg_replace_callback(
'/\\' . $brokenMacroOpeningChars . '(?:\\' . $endMacroOpeningChars . '|[^{$]*\>\{)[^' . $macroClosingChars . '$]*\}/U',
sprintf(
'/%s.+%s/U',
implode('', array_map(
function (string $char) {
return preg_quote($char) . '(?:<[^>]+>)*';
},
str_split(self::$macroOpeningChars)
)),
implode('', array_map(
function (string $char) {
return '(?:<[^>]+>)*' . preg_quote($char);
},
str_split(self::$macroClosingChars)
))
),
function ($match) {
return strip_tags($match[0]);
preg_match_all('/<[^>]+>/s', $match[0], $tags);
$tags = implode('', $tags[0]);
$tags = str_replace('<w:t>', '<w:t xml:space="preserve">', $tags);

return strip_tags($match[0]) . $tags;
},
$documentPart
);
Expand Down
33 changes: 27 additions & 6 deletions tests/PhpWordTests/TemplateProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1253,19 +1253,28 @@ public function testFixBrokenMacros(): void
self::assertEquals('<w:r><w:t>${documentContent}</w:t></w:r>', $fixed);

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

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

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

$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>25$ plus some info {hint}</w:t></w:r>');
self::assertEquals('<w:r><w:t>25$ plus some info {hint}</w:t></w:r>', $fixed);

$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>');
self::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);
self::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);

$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>');
self::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);

$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>');
self::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);

$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>');
self::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);
}

/**
Expand All @@ -1285,19 +1294,31 @@ public function testFixBrokenMacrosWithCustomMacro(): void
self::assertEquals('<w:r><w:t>{{documentContent}}</w:t></w:r>', $fixed);

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

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

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

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

$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>25$ plus some info {hint}</w:t></w:r>');
self::assertEquals('<w:r><w:t>25$ plus some info {hint}</w:t></w:r>', $fixed);

$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>');
self::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);
self::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);

$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>');
self::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);

$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>');
self::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);

$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>');
self::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);
}

/**
Expand Down