Skip to content

Commit 9bf816b

Browse files
authored
Allow rgb() When Converting Html (#2512)
* 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. * Update Change Log 2.0.0
1 parent b54465d commit 9bf816b

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

docs/changes/2.x/2.0.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
- 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)
1919
- 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)
2020
- 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)
21+
- 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)
2122

2223
### BC Breaks

src/PhpWord/Shared/Html.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
*/
3838
class Html
3939
{
40+
private const RGB_REGEXP = '/^\s*rgb\s*[(]\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*[)]\s*$/';
41+
4042
protected static $listIndex = 0;
4143

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

147149
break;
148150
case 'valign':
@@ -720,11 +722,11 @@ protected static function parseStyleDeclarations(array $selectors, array $styles
720722

721723
break;
722724
case 'color':
723-
$styles['color'] = trim($value, '#');
725+
$styles['color'] = self::convertRgb($value);
724726

725727
break;
726728
case 'background-color':
727-
$styles['bgColor'] = trim($value, '#');
729+
$styles['bgColor'] = self::convertRgb($value);
728730

729731
break;
730732
case 'line-height':
@@ -1170,4 +1172,13 @@ protected static function parseHorizRule($node, $element): void
11701172
// - line - that is a shape, has different behaviour
11711173
// - repeated text, e.g. underline "_", because of unpredictable line wrapping
11721174
}
1175+
1176+
private static function convertRgb(string $rgb): string
1177+
{
1178+
if (preg_match(self::RGB_REGEXP, $rgb, $matches) === 1) {
1179+
return sprintf('%02X%02X%02X', $matches[1], $matches[2], $matches[3]);
1180+
}
1181+
1182+
return trim($rgb, '# ');
1183+
}
11731184
}

tests/PhpWordTests/Writer/Word2007/Style/FontTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWordTests\Writer\Word2007\Style;
1919

20+
use PhpOffice\PhpWord\PhpWord;
21+
use PhpOffice\PhpWord\Shared\Html;
2022
use PhpOffice\PhpWordTests\TestHelperDOCX;
2123

2224
/**
@@ -155,4 +157,44 @@ public function testPosition(): void
155157
self::assertTrue($doc->elementExists($path));
156158
self::assertEquals(-20, $doc->getElementAttribute($path, 'w:val'));
157159
}
160+
161+
public static function testRgb(): void
162+
{
163+
$phpWord = new PhpWord();
164+
$section = $phpWord->addSection(['pageNumberingStart' => 1]);
165+
$html = implode(
166+
"\n",
167+
[
168+
'<table>',
169+
'<tbody>',
170+
'<tr>',
171+
'<td style="color: #A7D9C1;">This one is in color.</td>',
172+
'<td style="color: rgb(167, 217, 193);">This one too.</td>',
173+
'</tr>',
174+
'</tbody>',
175+
'</table>',
176+
]
177+
);
178+
179+
Html::addHtml($section, $html, false, false);
180+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
181+
182+
$element = '/w:document/w:body/w:tbl/w:tr/w:tc/w:p/w:r';
183+
$txtelem = $element . '/w:t';
184+
$styelem = $element . '/w:rPr';
185+
self::assertTrue($doc->elementExists($txtelem));
186+
self::assertSame('This one is in color.', $doc->getElement($txtelem)->textContent);
187+
self::assertTrue($doc->elementExists($styelem));
188+
self::assertTrue($doc->elementExists($styelem . '/w:color'));
189+
self::assertSame('A7D9C1', $doc->getElementAttribute($styelem . '/w:color', 'w:val'));
190+
191+
$element = '/w:document/w:body/w:tbl/w:tr/w:tc[2]/w:p/w:r';
192+
$txtelem = $element . '/w:t';
193+
$styelem = $element . '/w:rPr';
194+
self::assertTrue($doc->elementExists($txtelem));
195+
self::assertSame('This one too.', $doc->getElement($txtelem)->textContent);
196+
self::assertTrue($doc->elementExists($styelem));
197+
self::assertTrue($doc->elementExists($styelem . '/w:color'));
198+
self::assertSame('A7D9C1', $doc->getElementAttribute($styelem . '/w:color', 'w:val'));
199+
}
158200
}

0 commit comments

Comments
 (0)