Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow rgb() When Converting Html #2512

Merged
merged 3 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
Allow rgb() When Converting Html
Fix #2508. Program currently expects `#xxxxxx` to specify a color when used by the `bgcolor` html attribute or the `color` or `background-color` css attributes. This PR allows support for `rgb(red, green, blue)` as well.
  • Loading branch information
oleibman committed Nov 29, 2023
commit d59c053bba8f3f67269d2739f1211bd8ea0294ee
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, '# ');
}
}
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'));
}
}