Merge pull request #2294 from Progi1984/htmlTableBorder

HTML Reader : Use `border` attribute for tables
This commit is contained in:
Progi1984 2022-09-14 18:16:34 +02:00 committed by GitHub
commit 7df75b2efe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 11 deletions

View File

@ -372,17 +372,11 @@ class Html
$newElement = $element->addTable($elementStyles);
// $attributes = $node->attributes;
// if ($attributes->getNamedItem('width') !== null) {
// $newElement->setWidth($attributes->getNamedItem('width')->value);
// }
// if ($attributes->getNamedItem('height') !== null) {
// $newElement->setHeight($attributes->getNamedItem('height')->value);
// }
// if ($attributes->getNamedItem('width') !== null) {
// $newElement=$element->addCell($width=$attributes->getNamedItem('width')->value);
// }
$attributes = $node->attributes;
if ($attributes->getNamedItem('border') !== null) {
$border = (int) $attributes->getNamedItem('border')->value;
$newElement->getStyle()->setBorderSize(Converter::pixelToTwip($border));
}
return $newElement;
}

View File

@ -351,6 +351,35 @@ class HtmlTest extends AbstractWebServerEmbeddedTest
$this->assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:p/w:pPr/w:pBdr'));
}
/**
* Test parsing table (attribute border)
*/
public function testParseTableAttributeBorder()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<table border="10">
<thead>
<tr>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr><td>Cell 1</td></tr>
<tr><td>Cell 2</td></tr>
</tbody>
</table>';
Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tblPr'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tblPr/w:tblBorders'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tblPr/w:tblBorders/w:top'));
// 10 pixels = 150 twips
$this->assertEquals(150, $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tblPr/w:tblBorders/w:top', 'w:sz'));
}
/**
* Tests parsing of ul/li
*/