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

Update cloneBlock and replaceBlock to use XML api instead of string search/replace #541

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
162 changes: 133 additions & 29 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,22 +233,25 @@ public function cloneRow($search, $numberOfClones)
public function cloneBlock($blockname, $clones = 1, $replace = true)
{
$xmlBlock = null;
preg_match(
'/(<\?xml.*)(<w:p.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p.*\${\/' . $blockname . '}<\/w:.*?p>)/is',
$this->temporaryDocumentMainPart,
$matches
);

$matches = $this->findBlock($blockname);

if (isset($matches[1]))
{
$xmlBlock = $matches[1];

if (isset($matches[3])) {
$xmlBlock = $matches[3];
$cloned = array();
for ($i = 1; $i <= $clones; $i++) {
$cloned[] = $xmlBlock;

for ($i = 1; $i <= $clones; $i++)
{
$cloned[] = preg_replace('/\${(.*?)}/','${$1_'.$i.'}', $xmlBlock);
}

if ($replace) {
$this->temporaryDocumentMainPart = str_replace(
$matches[2] . $matches[3] . $matches[4],
if ($replace)
{
$this->temporaryDocumentMainPart = str_replace
(
$matches[0],
implode('', $cloned),
$this->temporaryDocumentMainPart
);
Expand All @@ -267,18 +270,16 @@ public function cloneBlock($blockname, $clones = 1, $replace = true)
*/
public function replaceBlock($blockname, $replacement)
{
preg_match(
'/(<\?xml.*)(<w:p.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p.*\${\/' . $blockname . '}<\/w:.*?p>)/is',
$this->temporaryDocumentMainPart,
$matches
);

if (isset($matches[3])) {
$this->temporaryDocumentMainPart = str_replace(
$matches[2] . $matches[3] . $matches[4],
$replacement,
$this->temporaryDocumentMainPart
);
$matches = $this->findBlock($blockname);

if (isset($matches[1]))
{
$this->temporaryDocumentMainPart = str_replace
(
$matches[0],
$replacement,
$this->temporaryDocumentMainPart
);
}
}

Expand Down Expand Up @@ -351,11 +352,11 @@ protected function setValueForPart($documentPartXML, $search, $replace, $limit)
{
$pattern = '|\$\{([^\}]+)\}|U';
preg_match_all($pattern, $documentPartXML, $matches);
foreach ($matches[0] as $value) {
$valueCleaned = preg_replace('/<[^>]+>/', '', $value);
$valueCleaned = preg_replace('/<\/[^>]+>/', '', $valueCleaned);
$documentPartXML = str_replace($value, $valueCleaned, $documentPartXML);
}
// foreach ($matches[0] as $value) {
// $valueCleaned = preg_replace('/<[^>]+>/', '', $value);
// $valueCleaned = preg_replace('/<\/[^>]+>/', '', $valueCleaned);
// $documentPartXML = str_replace($value, $valueCleaned, $documentPartXML);
// }

if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
$search = '${' . $search . '}';
Expand Down Expand Up @@ -452,4 +453,107 @@ private function getSlice($startPosition, $endPosition = 0)

return substr($this->temporaryDocumentMainPart, $startPosition, ($endPosition - $startPosition));
}

private function findBlock($blockname)
{
// Parse the XML
$xml = new \SimpleXMLElement($this->temporaryDocumentMainPart);

// Find the starting and ending tags
$startNode = false; $endNode = false;
foreach ($xml->xpath('//w:t') as $node)
{
if (strpos($node, '${'.$blockname.'}') !== false)
{
$startNode = $node;
continue;
}

if (strpos($node, '${/'.$blockname.'}') !== false)
{
$endNode = $node;
break;
}
}

// Make sure we found the tags
if ($startNode === false || $endNode === false)
{
return null;
}

// Find the parent <w:p> node for the start tag
$node = $startNode; $startNode = null;
while (is_null($startNode))
{
$node = $node->xpath('..')[0];

if ($node->getName() == 'p')
{
$startNode = $node;
}
}

// Find the parent <w:p> node for the end tag
$node = $endNode; $endNode = null;
while (is_null($endNode))
{
$node = $node->xpath('..')[0];

if ($node->getName() == 'p')
{
$endNode = $node;
}
}

/*
* NOTE: Because SimpleXML reduces empty tags to "self-closing" tags.
* We need to replace the original XML with the version of XML as
* SimpleXML sees it. The following example should show the issue
* we are facing.
*
* This is the XML that my document contained orginally.
*
* ```xml
* <w:p>
* <w:pPr>
* <w:pStyle w:val="TextBody"/>
* <w:rPr></w:rPr>
* </w:pPr>
* <w:r>
* <w:rPr></w:rPr>
* <w:t>${CLONEME}</w:t>
* </w:r>
* </w:p>
* ```
*
* This is the XML that SimpleXML returns from asXml().
*
* ```xml
* <w:p>
* <w:pPr>
* <w:pStyle w:val="TextBody"/>
* <w:rPr/>
* </w:pPr>
* <w:r>
* <w:rPr/>
* <w:t>${CLONEME}</w:t>
* </w:r>
* </w:p>
* ```
*/

$this->temporaryDocumentMainPart = $xml->asXml();

// Find the xml in between the tags
$xmlBlock = null;
preg_match
(
'/'.preg_quote($startNode->asXml(), '/').'(.*?)'.preg_quote($endNode->asXml(), '/').'/is',
$this->temporaryDocumentMainPart,
$matches
);

return $matches;
}
}