Skip to content

Commit

Permalink
Merge branch 'master' into Word2007-Reader---Extract-variables-from-word
Browse files Browse the repository at this point in the history
  • Loading branch information
sibalonat committed Jan 7, 2024
2 parents 53e08d5 + 9bf816b commit fda7405
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/changes/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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 All @@ -18,5 +20,6 @@
- Bump phpmd/phpmd from 2.14.1 to 2.15.0 by [@dependabot](https://github.com/dependabot) in [#2538](https://github.com/PHPOffice/PHPWord/pull/2538)
- Bump phpunit/phpunit from 9.6.14 to 9.6.15 by [@dependabot](https://github.com/dependabot) in [#2537](https://github.com/PHPOffice/PHPWord/pull/2537)
- Bump symfony/process from 5.4.28 to 5.4.34 by [@dependabot](https://github.com/dependabot) in [#2536](https://github.com/PHPOffice/PHPWord/pull/2536)
- Allow rgb() when converting Html [@oleibman](https://github.com/oleibman) fixing [#2508](https://github.com/PHPOffice/PHPWord/issues/2508) in [#2512](https://github.com/PHPOffice/PHPWord/pull/2512)

### BC Breaks
17 changes: 14 additions & 3 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
*/
class Html
{
private const RGB_REGEXP = '/^\s*rgb\s*[(]\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*[)]\s*$/';

protected static $listIndex = 0;

protected static $xpath;
Expand Down Expand Up @@ -142,7 +144,7 @@ protected static function parseInlineStyle($node, $styles = [])
break;
case 'bgcolor':
// tables, rows, cells e.g. <tr bgColor="#FF0000">
$styles['bgColor'] = trim($val, '# ');
$styles['bgColor'] = self::convertRgb($val);

break;
case 'valign':
Expand Down Expand Up @@ -720,11 +722,11 @@ protected static function parseStyleDeclarations(array $selectors, array $styles

break;
case 'color':
$styles['color'] = trim($value, '#');
$styles['color'] = self::convertRgb($value);

break;
case 'background-color':
$styles['bgColor'] = trim($value, '#');
$styles['bgColor'] = self::convertRgb($value);

break;
case 'line-height':
Expand Down Expand Up @@ -1170,4 +1172,13 @@ protected static function parseHorizRule($node, $element): void
// - line - that is a shape, has different behaviour
// - repeated text, e.g. underline "_", because of unpredictable line wrapping
}

private static function convertRgb(string $rgb): string
{
if (preg_match(self::RGB_REGEXP, $rgb, $matches) === 1) {
return sprintf('%02X%02X%02X', $matches[1], $matches[2], $matches[3]);
}

return trim($rgb, '# ');
}
}
17 changes: 17 additions & 0 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
42 changes: 42 additions & 0 deletions tests/PhpWordTests/Writer/Word2007/Style/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace PhpOffice\PhpWordTests\Writer\Word2007\Style;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWordTests\TestHelperDOCX;

/**
Expand Down Expand Up @@ -155,4 +157,44 @@ public function testPosition(): void
self::assertTrue($doc->elementExists($path));
self::assertEquals(-20, $doc->getElementAttribute($path, 'w:val'));
}

public static function testRgb(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection(['pageNumberingStart' => 1]);
$html = implode(
"\n",
[
'<table>',
'<tbody>',
'<tr>',
'<td style="color: #A7D9C1;">This one is in color.</td>',
'<td style="color: rgb(167, 217, 193);">This one too.</td>',
'</tr>',
'</tbody>',
'</table>',
]
);

Html::addHtml($section, $html, false, false);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

$element = '/w:document/w:body/w:tbl/w:tr/w:tc/w:p/w:r';
$txtelem = $element . '/w:t';
$styelem = $element . '/w:rPr';
self::assertTrue($doc->elementExists($txtelem));
self::assertSame('This one is in color.', $doc->getElement($txtelem)->textContent);
self::assertTrue($doc->elementExists($styelem));
self::assertTrue($doc->elementExists($styelem . '/w:color'));
self::assertSame('A7D9C1', $doc->getElementAttribute($styelem . '/w:color', 'w:val'));

$element = '/w:document/w:body/w:tbl/w:tr/w:tc[2]/w:p/w:r';
$txtelem = $element . '/w:t';
$styelem = $element . '/w:rPr';
self::assertTrue($doc->elementExists($txtelem));
self::assertSame('This one too.', $doc->getElement($txtelem)->textContent);
self::assertTrue($doc->elementExists($styelem));
self::assertTrue($doc->elementExists($styelem . '/w:color'));
self::assertSame('A7D9C1', $doc->getElementAttribute($styelem . '/w:color', 'w:val'));
}
}

0 comments on commit fda7405

Please sign in to comment.