From a4cdcd625006f49097ad9a6904f9ebeaa8c7956b Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Wed, 14 Sep 2022 17:50:54 +0200 Subject: [PATCH] HTML Reader : Use `border` attribute for tables --- src/PhpWord/Shared/Html.php | 16 +++++----------- tests/PhpWord/Shared/HtmlTest.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 3b8d587f..f57fb0a9 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -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; } diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index eecec002..fdf3e37c 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -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 = ' + + + + + + + + + +
Header
Cell 1
Cell 2
'; + 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 */