Skip to content

Commit 5206c7f

Browse files
committed
fix parsing of border-color and add test
1 parent 6e17274 commit 5206c7f

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ v0.17.0 (?? ??? 2019)
77
----------------------
88
### Added
99
- Add RightToLeft table presentation. @troosan #1550
10+
- Set complex type in template @troosan #1565
1011
- Add support for page vertical alignment. @troosan #672 #1569
1112

1213
### Fixed
14+
- Fix HTML border-color parsing. @troosan #1551 #1570
1315

1416
### Miscelaneous
1517
- Use embedded http server to test loading of remote images @troosan #

samples/Sample_26_Html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
</tr>
7575
</thead>
7676
<tbody>
77-
<tr><td style="border-style: dotted;">1</td><td colspan="2">2</td></tr>
77+
<tr><td style="border-style: dotted; border-color: #FF0000">1</td><td colspan="2">2</td></tr>
7878
<tr><td>This is <b>bold</b> text</td><td></td><td>6</td></tr>
7979
</tbody>
8080
</table>';

src/PhpWord/Shared/Html.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ private static function parseStyle($attribute, $styles)
581581
$styles['spaceAfter'] = Converter::cssToPoint($cValue);
582582
break;
583583
case 'border-color':
584-
$styles['color'] = trim($cValue, '#');
584+
self::mapBorderColor($styles, $cValue);
585585
break;
586586
case 'border-width':
587587
$styles['borderSize'] = Converter::cssToPoint($cValue);
@@ -738,6 +738,20 @@ private static function mapBorderStyle($cssBorderStyle)
738738
}
739739
}
740740

741+
private static function mapBorderColor(&$styles, $cssBorderColor)
742+
{
743+
$numColors = substr_count($cssBorderColor, '#');
744+
if ($numColors === 1) {
745+
$styles['borderColor'] = trim($cssBorderColor, '#');
746+
} elseif ($numColors > 1) {
747+
$colors = explode(' ', $cssBorderColor);
748+
$borders = array('borderTopColor', 'borderRightColor', 'borderBottomColor', 'borderLeftColor');
749+
for ($i = 0; $i < min(4, $numColors, count($colors)); $i++) {
750+
$styles[$borders[$i]] = $colors[$i];
751+
}
752+
}
753+
}
754+
741755
/**
742756
* Transforms a HTML/CSS alignment into a \PhpOffice\PhpWord\SimpleType\Jc
743757
*

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public function testParseTable()
298298
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
299299
<th style="width: 50pt">header a</th>
300300
<th style="width: 50">header b</th>
301-
<th style="border-color: #00FF00; border-width: 3px">header c</th>
301+
<th style="border-color: #00AA00 #00BB00 #00CC00 #00DD00; border-width: 3px">header c</th>
302302
</tr>
303303
</thead>
304304
<tbody>
@@ -313,6 +313,11 @@ public function testParseTable()
313313
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc'));
314314
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tblPr/w:jc'));
315315
$this->assertEquals(Jc::START, $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tblPr/w:jc', 'w:val'));
316+
//check border colors
317+
$this->assertEquals('#00AA00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]/w:tcPr/w:tcBorders/w:top', 'w:color'));
318+
$this->assertEquals('#00BB00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]/w:tcPr/w:tcBorders/w:right', 'w:color'));
319+
$this->assertEquals('#00CC00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]/w:tcPr/w:tcBorders/w:bottom', 'w:color'));
320+
$this->assertEquals('#00DD00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]/w:tcPr/w:tcBorders/w:left', 'w:color'));
316321
}
317322

318323
/**

0 commit comments

Comments
 (0)