Html parser (addHtml) - support cellspacing, bgColor

This commit is contained in:
lubosdz 2020-07-11 00:24:08 +02:00
parent 701f770ab7
commit e180cfe456
2 changed files with 54 additions and 2 deletions

View File

@ -96,7 +96,7 @@ class Html
$attributes = $node->attributes; // get all the attributes(eg: id, class) $attributes = $node->attributes; // get all the attributes(eg: id, class)
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
switch ($attribute->name) { switch (strtolower($attribute->name)) {
case 'style': case 'style':
$styles = self::parseStyle($attribute, $styles); $styles = self::parseStyle($attribute, $styles);
break; break;
@ -119,6 +119,15 @@ class Html
$styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP; $styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::TWIP;
} }
break; break;
case 'cellspacing':
// tables e.g. <table cellspacing="2">, where "2" = 2px (always pixels)
$val = intval($attribute->value).'px';
$styles['cellSpacing'] = Converter::cssToTwip($val);
break;
case 'bgcolor':
// tables, rows, cells e.g. <tr bgColor="#FF0000">
$styles['bgColor'] = trim($attribute->value, '# ');
break;
} }
} }
} }
@ -519,7 +528,8 @@ class Html
foreach ($properties as $property) { foreach ($properties as $property) {
list($cKey, $cValue) = array_pad(explode(':', $property, 2), 2, null); list($cKey, $cValue) = array_pad(explode(':', $property, 2), 2, null);
$cValue = trim($cValue); $cValue = trim($cValue);
switch (trim($cKey)) { $cKey = strtolower(trim($cKey));
switch ($cKey) {
case 'text-decoration': case 'text-decoration':
switch ($cValue) { switch ($cValue) {
case 'underline': case 'underline':

View File

@ -702,4 +702,46 @@ HTML;
$this->assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type')); $this->assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type'));
} }
public function testParseCellspacingRowBgColor()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection([
'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE,
]);
// borders & backgrounds are here just for better visual comparison
$html = <<<HTML
<table cellspacing="3" bgColor="lightgreen" width="50%" align="center">
<tr>
<td>A</td>
<td>B</td>
</tr>
<tr bgcolor="#FF0000">
<td>C</td>
<td>D</td>
</tr>
</table>
HTML;
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
// uncomment to see results
file_put_contents('./table_src.html', $html);
file_put_contents('./table_result_'.time().'.docx', file_get_contents( TestHelperDOCX::getFile() ) );
$xpath = '/w:document/w:body/w:tbl/w:tblPr/w:tblCellSpacing';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals(3 * 15, $doc->getElement($xpath)->getAttribute('w:w'));
$this->assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type'));
$xpath = '/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:tcPr/w:shd';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals('lightgreen', $doc->getElement($xpath)->getAttribute('w:fill'));
$xpath = '/w:document/w:body/w:tbl/w:tr[2]/w:tc[1]/w:tcPr/w:shd';
$this->assertTrue($doc->elementExists($xpath));
$this->assertEquals('FF0000', $doc->getElement($xpath)->getAttribute('w:fill'));
}
} }