Skip to content

Commit e180cfe

Browse files
committed
Html parser (addHtml) - support cellspacing, bgColor
1 parent 701f770 commit e180cfe

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected static function parseInlineStyle($node, $styles = array())
9696
$attributes = $node->attributes; // get all the attributes(eg: id, class)
9797

9898
foreach ($attributes as $attribute) {
99-
switch ($attribute->name) {
99+
switch (strtolower($attribute->name)) {
100100
case 'style':
101101
$styles = self::parseStyle($attribute, $styles);
102102
break;
@@ -119,6 +119,15 @@ protected static function parseInlineStyle($node, $styles = array())
119119
$styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP;
120120
}
121121
break;
122+
case 'cellspacing':
123+
// tables e.g. <table cellspacing="2">, where "2" = 2px (always pixels)
124+
$val = intval($attribute->value).'px';
125+
$styles['cellSpacing'] = Converter::cssToTwip($val);
126+
break;
127+
case 'bgcolor':
128+
// tables, rows, cells e.g. <tr bgColor="#FF0000">
129+
$styles['bgColor'] = trim($attribute->value, '# ');
130+
break;
122131
}
123132
}
124133
}
@@ -519,7 +528,8 @@ protected static function parseStyle($attribute, $styles)
519528
foreach ($properties as $property) {
520529
list($cKey, $cValue) = array_pad(explode(':', $property, 2), 2, null);
521530
$cValue = trim($cValue);
522-
switch (trim($cKey)) {
531+
$cKey = strtolower(trim($cKey));
532+
switch ($cKey) {
523533
case 'text-decoration':
524534
switch ($cValue) {
525535
case 'underline':

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,4 +702,46 @@ public function testParseTableAndCellWidth()
702702
$this->assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type'));
703703
}
704704

705+
public function testParseCellspacingRowBgColor()
706+
{
707+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
708+
$section = $phpWord->addSection([
709+
'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE,
710+
]);
711+
712+
// borders & backgrounds are here just for better visual comparison
713+
$html = <<<HTML
714+
<table cellspacing="3" bgColor="lightgreen" width="50%" align="center">
715+
<tr>
716+
<td>A</td>
717+
<td>B</td>
718+
</tr>
719+
<tr bgcolor="#FF0000">
720+
<td>C</td>
721+
<td>D</td>
722+
</tr>
723+
</table>
724+
HTML;
725+
726+
Html::addHtml($section, $html);
727+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
728+
729+
// uncomment to see results
730+
file_put_contents('./table_src.html', $html);
731+
file_put_contents('./table_result_'.time().'.docx', file_get_contents( TestHelperDOCX::getFile() ) );
732+
733+
$xpath = '/w:document/w:body/w:tbl/w:tblPr/w:tblCellSpacing';
734+
$this->assertTrue($doc->elementExists($xpath));
735+
$this->assertEquals(3 * 15, $doc->getElement($xpath)->getAttribute('w:w'));
736+
$this->assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type'));
737+
738+
$xpath = '/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:tcPr/w:shd';
739+
$this->assertTrue($doc->elementExists($xpath));
740+
$this->assertEquals('lightgreen', $doc->getElement($xpath)->getAttribute('w:fill'));
741+
742+
$xpath = '/w:document/w:body/w:tbl/w:tr[2]/w:tc[1]/w:tcPr/w:shd';
743+
$this->assertTrue($doc->elementExists($xpath));
744+
$this->assertEquals('FF0000', $doc->getElement($xpath)->getAttribute('w:fill'));
745+
}
746+
705747
}

0 commit comments

Comments
 (0)