diff --git a/CHANGELOG.md b/CHANGELOG.md
index a273326a..735f6d4d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ v0.16.0 (xx dec 2018)
### Fixed
- Fix regex in `cloneBlock` function @nicoder #1269
- HTML Title Writer loses text when Title contains a TextRun instead a string. @begnini #1436
+- Fix regex in fixBrokenMacros, make it less greedy @MuriloSo @brainwood @yurii-sio2 #1502 #1345
- 240 twips are being added to line spacing, should not happen when using lineRule fixed @troosan #1509 #1505
- Adding table layout to the generated HTML @aarangara #1441
- Fix loading of Sharepoint document @Garrcomm #1498
diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php
index 0d4bfdeb..86d0c07d 100644
--- a/src/PhpWord/TemplateProcessor.php
+++ b/src/PhpWord/TemplateProcessor.php
@@ -442,17 +442,13 @@ class TemplateProcessor
*/
protected function fixBrokenMacros($documentPart)
{
- $fixedDocumentPart = $documentPart;
-
- $fixedDocumentPart = preg_replace_callback(
- '|\$[^{]*\{[^}]*\}|U',
+ return preg_replace_callback(
+ '/\$(?:\{|[^{$]*\>\{)[^}$]*\}/U',
function ($match) {
return strip_tags($match[0]);
},
- $fixedDocumentPart
+ $documentPart
);
-
- return $fixedDocumentPart;
}
/**
diff --git a/tests/PhpWord/TemplateProcessorTest.php b/tests/PhpWord/TemplateProcessorTest.php
index 1513486e..8839200d 100644
--- a/tests/PhpWord/TemplateProcessorTest.php
+++ b/tests/PhpWord/TemplateProcessorTest.php
@@ -277,6 +277,38 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
}
}
+ /**
+ * Template macros can be fixed.
+ *
+ * @covers ::fixBrokenMacros
+ * @test
+ */
+ public function testFixBrokenMacros()
+ {
+ $templateProcessor = new TestableTemplateProcesor();
+
+ $fixed = $templateProcessor->fixBrokenMacros('normal text');
+ $this->assertEquals('normal text', $fixed);
+
+ $fixed = $templateProcessor->fixBrokenMacros('${documentContent}');
+ $this->assertEquals('${documentContent}', $fixed);
+
+ $fixed = $templateProcessor->fixBrokenMacros('${documentContent}');
+ $this->assertEquals('${documentContent}', $fixed);
+
+ $fixed = $templateProcessor->fixBrokenMacros('$1500${documentContent}');
+ $this->assertEquals('$1500${documentContent}', $fixed);
+
+ $fixed = $templateProcessor->fixBrokenMacros('$1500${documentContent}');
+ $this->assertEquals('$1500${documentContent}', $fixed);
+
+ $fixed = $templateProcessor->fixBrokenMacros('25$ plus some info {hint}');
+ $this->assertEquals('25$ plus some info {hint}', $fixed);
+
+ $fixed = $templateProcessor->fixBrokenMacros('$15,000.00. ${variable_name}');
+ $this->assertEquals('$15,000.00. ${variable_name}', $fixed);
+ }
+
public function testMainPartNameDetection()
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/document22-xml.docx');
diff --git a/tests/PhpWord/_includes/TestableTemplateProcesor.php b/tests/PhpWord/_includes/TestableTemplateProcesor.php
new file mode 100644
index 00000000..f76da417
--- /dev/null
+++ b/tests/PhpWord/_includes/TestableTemplateProcesor.php
@@ -0,0 +1,30 @@
+