Skip to content

Commit 7df75b2

Browse files
authored
Merge pull request #2294 from Progi1984/htmlTableBorder
HTML Reader : Use `border` attribute for tables
2 parents 6258490 + a4cdcd6 commit 7df75b2

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -372,17 +372,11 @@ protected static function parseTable($node, $element, &$styles)
372372

373373
$newElement = $element->addTable($elementStyles);
374374

375-
// $attributes = $node->attributes;
376-
// if ($attributes->getNamedItem('width') !== null) {
377-
// $newElement->setWidth($attributes->getNamedItem('width')->value);
378-
// }
379-
380-
// if ($attributes->getNamedItem('height') !== null) {
381-
// $newElement->setHeight($attributes->getNamedItem('height')->value);
382-
// }
383-
// if ($attributes->getNamedItem('width') !== null) {
384-
// $newElement=$element->addCell($width=$attributes->getNamedItem('width')->value);
385-
// }
375+
$attributes = $node->attributes;
376+
if ($attributes->getNamedItem('border') !== null) {
377+
$border = (int) $attributes->getNamedItem('border')->value;
378+
$newElement->getStyle()->setBorderSize(Converter::pixelToTwip($border));
379+
}
386380

387381
return $newElement;
388382
}

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,35 @@ public function testParseTable()
351351
$this->assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:p/w:pPr/w:pBdr'));
352352
}
353353

354+
/**
355+
* Test parsing table (attribute border)
356+
*/
357+
public function testParseTableAttributeBorder()
358+
{
359+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
360+
$section = $phpWord->addSection();
361+
$html = '<table border="10">
362+
<thead>
363+
<tr>
364+
<th>Header</th>
365+
</tr>
366+
</thead>
367+
<tbody>
368+
<tr><td>Cell 1</td></tr>
369+
<tr><td>Cell 2</td></tr>
370+
</tbody>
371+
</table>';
372+
Html::addHtml($section, $html);
373+
374+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
375+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl'));
376+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tblPr'));
377+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tblPr/w:tblBorders'));
378+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tblPr/w:tblBorders/w:top'));
379+
// 10 pixels = 150 twips
380+
$this->assertEquals(150, $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tblPr/w:tblBorders/w:top', 'w:sz'));
381+
}
382+
354383
/**
355384
* Tests parsing of ul/li
356385
*/

0 commit comments

Comments
 (0)