Skip to content

Commit a056914

Browse files
committed
bug: TemplateProcessor fix multiline values
1 parent 050e74e commit a056914

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

docs/changes/2.x/2.0.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
- 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)
1010

11+
- 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)
12+
1113
### Miscellaneous
1214

1315
- 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)

src/PhpWord/TemplateProcessor.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,15 @@ public function setValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_
357357
$replace = $xmlEscaper->escape($replace);
358358
}
359359

360+
// convert carriage returns
361+
if (is_array($replace)) {
362+
foreach ($replace as &$item) {
363+
$item = $this->replaceCarriageReturns($item);
364+
}
365+
} else {
366+
$replace = $this->replaceCarriageReturns($replace);
367+
}
368+
360369
$this->tempDocumentHeaders = $this->setValueForPart($search, $replace, $this->tempDocumentHeaders, $limit);
361370
$this->tempDocumentMainPart = $this->setValueForPart($search, $replace, $this->tempDocumentMainPart, $limit);
362371
$this->tempDocumentFooters = $this->setValueForPart($search, $replace, $this->tempDocumentFooters, $limit);
@@ -1305,6 +1314,14 @@ protected function indexClonedVariables($count, $xmlBlock)
13051314
return $results;
13061315
}
13071316

1317+
/**
1318+
* Replace carriage returns with xml.
1319+
*/
1320+
public function replaceCarriageReturns(string $string): string
1321+
{
1322+
return str_replace(["\r\n", "\r", "\n"], '</w:t><w:br/><w:t>', $string);
1323+
}
1324+
13081325
/**
13091326
* Replaces variables with values from array, array keys are the variable names.
13101327
*

tests/PhpWordTests/TemplateProcessorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,24 @@ public function testSetValues(): void
591591
self::assertStringContainsString('Hello John Doe', $templateProcessor->getMainPart());
592592
}
593593

594+
/**
595+
* @covers ::setValues
596+
*/
597+
public function testSetValuesMultiLine(): void
598+
{
599+
$mainPart = '<?xml version="1.0" encoding="UTF-8"?>
600+
<w:p>
601+
<w:r>
602+
<w:t xml:space="preserve">Address: ${address}</w:t>
603+
</w:r>
604+
</w:p>';
605+
606+
$templateProcessor = new TestableTemplateProcesor($mainPart);
607+
$templateProcessor->setValues(['address' => "Peter Pan\nNeverland"]);
608+
609+
self::assertStringContainsString('Address: Peter Pan</w:t><w:br/><w:t>Neverland', $templateProcessor->getMainPart());
610+
}
611+
594612
/**
595613
* @covers ::setValues
596614
*/

0 commit comments

Comments
 (0)