Skip to content

Commit 260bb75

Browse files
yurii-sio2troosan
authored andcommitted
Fix TemplateProcessor :: fixBrokenMacros; (#1502)
* Fix TemplateProcessor :: fixBrokenMacros; * add unit test for fixBrokenMacros
1 parent af5a271 commit 260bb75

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ v0.16.0 (xx dec 2018)
1111
### Fixed
1212
- Fix regex in `cloneBlock` function @nicoder #1269
1313
- HTML Title Writer loses text when Title contains a TextRun instead a string. @begnini #1436
14+
- Fix regex in fixBrokenMacros, make it less greedy @MuriloSo @brainwood @yurii-sio2 #1502 #1345
1415
- 240 twips are being added to line spacing, should not happen when using lineRule fixed @troosan #1509 #1505
1516
- Adding table layout to the generated HTML @aarangara #1441
1617
- Fix loading of Sharepoint document @Garrcomm #1498

src/PhpWord/TemplateProcessor.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,17 +442,13 @@ public function saveAs($fileName)
442442
*/
443443
protected function fixBrokenMacros($documentPart)
444444
{
445-
$fixedDocumentPart = $documentPart;
446-
447-
$fixedDocumentPart = preg_replace_callback(
448-
'|\$[^{]*\{[^}]*\}|U',
445+
return preg_replace_callback(
446+
'/\$(?:\{|[^{$]*\>\{)[^}$]*\}/U',
449447
function ($match) {
450448
return strip_tags($match[0]);
451449
},
452-
$fixedDocumentPart
450+
$documentPart
453451
);
454-
455-
return $fixedDocumentPart;
456452
}
457453

458454
/**

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,38 @@ public function cloneBlockCanCloneABlockTwice()
277277
}
278278
}
279279

280+
/**
281+
* Template macros can be fixed.
282+
*
283+
* @covers ::fixBrokenMacros
284+
* @test
285+
*/
286+
public function testFixBrokenMacros()
287+
{
288+
$templateProcessor = new TestableTemplateProcesor();
289+
290+
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>normal text</w:t></w:r>');
291+
$this->assertEquals('<w:r><w:t>normal text</w:t></w:r>', $fixed);
292+
293+
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>${documentContent}</w:t></w:r>');
294+
$this->assertEquals('<w:r><w:t>${documentContent}</w:t></w:r>', $fixed);
295+
296+
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>$</w:t><w:t>{documentContent}</w:t></w:r>');
297+
$this->assertEquals('<w:r><w:t>${documentContent}</w:t></w:r>', $fixed);
298+
299+
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>$1500</w:t><w:t>${documentContent}</w:t></w:r>');
300+
$this->assertEquals('<w:r><w:t>$1500</w:t><w:t>${documentContent}</w:t></w:r>', $fixed);
301+
302+
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>$1500</w:t><w:t>$</w:t><w:t>{documentContent}</w:t></w:r>');
303+
$this->assertEquals('<w:r><w:t>$1500</w:t><w:t>${documentContent}</w:t></w:r>', $fixed);
304+
305+
$fixed = $templateProcessor->fixBrokenMacros('<w:r><w:t>25$ plus some info {hint}</w:t></w:r>');
306+
$this->assertEquals('<w:r><w:t>25$ plus some info {hint}</w:t></w:r>', $fixed);
307+
308+
$fixed = $templateProcessor->fixBrokenMacros('<w:t>$</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:t xml:space="preserve">15,000.00. </w:t></w:r><w:r w:rsidR="0056499B"><w:t>$</w:t></w:r><w:r w:rsidR="00573DFD" w:rsidRPr="00573DFD"><w:rPr><w:iCs/></w:rPr><w:t>{</w:t></w:r><w:proofErr w:type="spellStart"/><w:r w:rsidR="00573DFD" w:rsidRPr="00573DFD"><w:rPr><w:iCs/></w:rPr><w:t>variable_name</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r w:rsidR="00573DFD" w:rsidRPr="00573DFD"><w:rPr><w:iCs/></w:rPr><w:t>}</w:t></w:r>');
309+
$this->assertEquals('<w:t>$</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/><w:r><w:t xml:space="preserve">15,000.00. </w:t></w:r><w:r w:rsidR="0056499B"><w:t>${variable_name}</w:t></w:r>', $fixed);
310+
}
311+
280312
public function testMainPartNameDetection()
281313
{
282314
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/document22-xml.docx');
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2018 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord;
19+
20+
class TestableTemplateProcesor extends TemplateProcessor
21+
{
22+
public function __construct()
23+
{
24+
}
25+
26+
public function fixBrokenMacros($documentPart)
27+
{
28+
return parent::fixBrokenMacros($documentPart);
29+
}
30+
}

0 commit comments

Comments
 (0)