Skip to content

Commit

Permalink
bug: TemplateProcessor fix multiline values
Browse files Browse the repository at this point in the history
  • Loading branch information
gimler committed Jan 7, 2024
1 parent 050e74e commit a056914
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/changes/2.x/2.0.0.md
Expand Up @@ -8,6 +8,8 @@

- MsDoc Reader : Correct Font Size Calculation by [@oleibman](https://github.com/oleibman) fixing [#2526](https://github.com/PHPOffice/PHPWord/issues/2526) in [#2531](https://github.com/PHPOffice/PHPWord/pull/2531)

- bug: TemplateProcessor fix multiline values [@gimler](https://github.com/gimler) fixing [#268](https://github.com/PHPOffice/PHPWord/issues/268), [#2323](https://github.com/PHPOffice/PHPWord/issues/2323) and [#2486](https://github.com/PHPOffice/PHPWord/issues/2486) in [#2522](https://github.com/PHPOffice/PHPWord/pull/2522)

### Miscellaneous

- Bump dompdf/dompdf from 2.0.3 to 2.0.4 by [@dependabot](https://github.com/dependabot) in [#2530](https://github.com/PHPOffice/PHPWord/pull/2530)
Expand Down
17 changes: 17 additions & 0 deletions src/PhpWord/TemplateProcessor.php
Expand Up @@ -357,6 +357,15 @@ public function setValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_
$replace = $xmlEscaper->escape($replace);
}

// convert carriage returns
if (is_array($replace)) {
foreach ($replace as &$item) {
$item = $this->replaceCarriageReturns($item);
}
} else {
$replace = $this->replaceCarriageReturns($replace);
}

$this->tempDocumentHeaders = $this->setValueForPart($search, $replace, $this->tempDocumentHeaders, $limit);
$this->tempDocumentMainPart = $this->setValueForPart($search, $replace, $this->tempDocumentMainPart, $limit);
$this->tempDocumentFooters = $this->setValueForPart($search, $replace, $this->tempDocumentFooters, $limit);
Expand Down Expand Up @@ -1305,6 +1314,14 @@ protected function indexClonedVariables($count, $xmlBlock)
return $results;
}

/**
* Replace carriage returns with xml.
*/
public function replaceCarriageReturns(string $string): string
{
return str_replace(["\r\n", "\r", "\n"], '</w:t><w:br/><w:t>', $string);
}

/**
* Replaces variables with values from array, array keys are the variable names.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/PhpWordTests/TemplateProcessorTest.php
Expand Up @@ -591,6 +591,24 @@ public function testSetValues(): void
self::assertStringContainsString('Hello John Doe', $templateProcessor->getMainPart());
}

/**
* @covers ::setValues
*/
public function testSetValuesMultiLine(): void
{
$mainPart = '<?xml version="1.0" encoding="UTF-8"?>
<w:p>
<w:r>
<w:t xml:space="preserve">Address: ${address}</w:t>
</w:r>
</w:p>';

$templateProcessor = new TestableTemplateProcesor($mainPart);
$templateProcessor->setValues(['address' => "Peter Pan\nNeverland"]);

self::assertStringContainsString('Address: Peter Pan</w:t><w:br/><w:t>Neverland', $templateProcessor->getMainPart());
}

/**
* @covers ::setValues
*/
Expand Down

0 comments on commit a056914

Please sign in to comment.