Skip to content

Commit 0a73bfd

Browse files
authored
Merge pull request #1570 from troosan/fix_parsing_html_border_color
fix parsing of border-color and add test
2 parents 6e17274 + 3219950 commit 0a73bfd

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ private static function parseStyle($attribute, $styles)
516516
$styles['alignment'] = self::mapAlign($cValue);
517517
break;
518518
case 'display':
519-
$styles['hidden'] = $cValue === 'none';
519+
$styles['hidden'] = $cValue === 'none' || $cValue === 'hidden';
520520
break;
521521
case 'direction':
522522
$styles['rtl'] = $cValue === 'rtl';
@@ -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]] = trim($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: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ public function testParseTable()
297297
<thead>
298298
<tr style="background-color: #FF0000; text-align: center; color: #FFFFFF; font-weight: bold; ">
299299
<th style="width: 50pt">header a</th>
300-
<th style="width: 50">header b</th>
301-
<th style="border-color: #00FF00; border-width: 3px">header c</th>
300+
<th style="width: 50; border-color: #00EE00">header b</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,17 @@ 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+
317+
//check border colors
318+
$this->assertEquals('00EE00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:tcPr/w:tcBorders/w:top', 'w:color'));
319+
$this->assertEquals('00EE00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:tcPr/w:tcBorders/w:right', 'w:color'));
320+
$this->assertEquals('00EE00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:tcPr/w:tcBorders/w:bottom', 'w:color'));
321+
$this->assertEquals('00EE00', $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:tcPr/w:tcBorders/w:left', 'w:color'));
322+
323+
$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'));
324+
$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'));
325+
$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'));
326+
$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'));
316327
}
317328

318329
/**
@@ -590,4 +601,35 @@ public function testParseMalformedStyleIsIgnored()
590601
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
591602
$this->assertFalse($doc->elementExists('/w:document/w:body/w:p[1]/w:pPr/w:jc'));
592603
}
604+
605+
/**
606+
* Tests parsing hidden text
607+
*/
608+
public function testParseHiddenText()
609+
{
610+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
611+
$section = $phpWord->addSection();
612+
$html = '<p style="display: hidden">This is some hidden text.</p>';
613+
Html::addHtml($section, $html);
614+
615+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
616+
617+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:vanish'));
618+
}
619+
620+
/**
621+
* Tests parsing letter spacing
622+
*/
623+
public function testParseLetterSpacing()
624+
{
625+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
626+
$section = $phpWord->addSection();
627+
$html = '<p style="letter-spacing: 150px">This is some text with letter spacing.</p>';
628+
Html::addHtml($section, $html);
629+
630+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
631+
632+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:spacing'));
633+
$this->assertEquals(150 * 15, $doc->getElement('/w:document/w:body/w:p/w:r/w:rPr/w:spacing')->getAttribute('w:val'));
634+
}
593635
}

0 commit comments

Comments
 (0)