Skip to content
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

fix block handling in template processor #2448

Open
wants to merge 1 commit 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
38 changes: 15 additions & 23 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -853,46 +853,38 @@ public function cloneRowAndSetValues($search, $values): void
* @param bool $replace
* @param bool $indexVariables If true, any variables inside the block will be indexed (postfixed with #1, #2, ...)
* @param array $variableReplacements Array containing replacements for macros found inside the block to clone
*
* @return null|string
*/
public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVariables = false, $variableReplacements = null)
public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVariables = false, $variableReplacements = null): void
{
$xmlBlock = null;
$matches = [];
$escapedMacroOpeningChars = self::$macroOpeningChars;
$escapedMacroClosingChars = self::$macroClosingChars;
preg_match(
//'/(.*((?s)<w:p\b(?:(?!<w:p\b).)*?\{{' . $blockname . '}<\/w:.*?p>))(.*)((?s)<w:p\b(?:(?!<w:p\b).)[^$]*?\{{\/' . $blockname . '}<\/w:.*?p>)/is',
'/(.*((?s)<w:p\b(?:(?!<w:p\b).)*?\\' . $escapedMacroOpeningChars . $blockname . $escapedMacroClosingChars . '<\/w:.*?p>))(.*)((?s)<w:p\b(?:(?!<w:p\b).)[^$]*?\\' . $escapedMacroOpeningChars . '\/' . $blockname . $escapedMacroClosingChars . '<\/w:.*?p>)/is',
//'/(.*((?s)<w:p\b(?:(?!<w:p\b).)*?\\'. $escapedMacroOpeningChars . $blockname . '}<\/w:.*?p>))(.*)((?s)<w:p\b(?:(?!<w:p\b).)[^$]*?\\'.$escapedMacroOpeningChars.'\/' . $blockname . '}<\/w:.*?p>)/is',
$this->tempDocumentMainPart,
$matches
);
$open = preg_quote(self::ensureMacroCompleted($blockname));
$close = str_replace('/', '\/', preg_quote(self::ensureMacroCompleted("/$blockname")));

if (isset($matches[3])) {
$xmlBlock = $matches[3];
$beginRe = '(<w:p\b(?:(?!<w:p\b).)*?' . $open . '.*?<\/w:p>)';
$endRe = '(<w:p\b(?:(?!<w:p\b).)*?' . $close . '.*?<\/w:p>)';
$betweenRe = '.*?(<w:p\b(?:(?!<w:p\b).)*?.*?<\/w:p>).*?';
$re = "/$beginRe$betweenRe$endRe/is";

$blockMatches = [];
preg_match_all($re, $this->tempDocumentMainPart, $blockMatches, \PREG_SET_ORDER);

foreach ($blockMatches as $matches) {
$xmlBlock = $matches[2];
if ($indexVariables) {
$cloned = $this->indexClonedVariables($clones, $xmlBlock);
} elseif ($variableReplacements !== null && is_array($variableReplacements)) {
$cloned = $this->replaceClonedVariables($variableReplacements, $xmlBlock);
} else {
$cloned = [];
for ($i = 1; $i <= $clones; ++$i) {
$cloned[] = $xmlBlock;
}
$cloned = array_fill(0, max(0, $clones), $xmlBlock);
}

if ($replace) {
$this->tempDocumentMainPart = str_replace(
$matches[2] . $matches[3] . $matches[4],
$matches[0],
implode('', $cloned),
$this->tempDocumentMainPart
);
}
}

return $xmlBlock;
}

/**
Expand Down
36 changes: 36 additions & 0 deletions tests/PhpWordTests/TemplateProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,42 @@ public function testCloneBlock(): void
self::assertEquals(3, substr_count($templateProcessor->getMainPart(), 'This block will be cloned with ${variable}'));
}

/**
* @covers ::cloneBlock
*/
public function testCloneBlockWithMultipleOccurrencesOfSameBlock(): void
{
$block = '<w:p>
<w:r>
<w:rPr></w:rPr>
<w:t>${CLONEME}</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t xml:space="preserve">This block will be cloned with ${variable}</w:t>
</w:r>
</w:p>
<w:p>
<w:r w:rsidRPr="00204FED">
<w:t>${/CLONEME}</w:t>
</w:r>
</w:p>';
$blocks = array_fill(0, 2, $block);
$mainPart = '<?xml version="1.0" encoding="UTF-8"?>' . implode("\n", $blocks);

$replacements = [
['variable' => 'PHPWord'],
['variable' => 'PhpOffice'],
];

$templateProcessor = new TestableTemplateProcesor($mainPart);
$templateProcessor->cloneBlock('CLONEME', 0, true, false, $replacements);

self::assertEquals(2, substr_count($templateProcessor->getMainPart(), 'This block will be cloned with PHPWord'));
self::assertEquals(2, substr_count($templateProcessor->getMainPart(), 'This block will be cloned with PhpOffice'));
}

/**
* @covers ::cloneBlock
*/
Expand Down