diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index b2c99d0336..c937d29918 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -162,6 +162,7 @@ protected static function parseInlineStyle($node, $styles = []) $attributeClass = $attributes->getNamedItem('class'); if ($attributeClass && self::$css) { $styles = self::parseStyleDeclarations(self::$css->getStyle('.' . $attributeClass->value), $styles); + $styles['className'] = $attributeClass->value; } $attributeStyle = $attributes->getNamedItem('style'); @@ -411,6 +412,11 @@ protected static function parseTable($node, $element, &$styles) $newElement = $element->addTable($elementStyles); + // Add style name from CSS Class + if (isset($elementStyles['className'])) { + $newElement->getStyle()->setStyleName($elementStyles['className']); + } + $attributes = $node->attributes; if ($attributes->getNamedItem('border') !== null) { $border = (int) $attributes->getNamedItem('border')->value; diff --git a/tests/PhpWordTests/Shared/HtmlTest.php b/tests/PhpWordTests/Shared/HtmlTest.php index 7c02581a97..8da6ccc61e 100644 --- a/tests/PhpWordTests/Shared/HtmlTest.php +++ b/tests/PhpWordTests/Shared/HtmlTest.php @@ -19,6 +19,7 @@ use Exception; use PhpOffice\PhpWord\Element\Section; +use PhpOffice\PhpWord\Element\Table; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Shared\Html; use PhpOffice\PhpWord\SimpleType\Jc; @@ -134,6 +135,17 @@ public function testParseStyle(): void self::assertEquals('22.5', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:r/w:rPr/w:sz', 'w:val')); } + public function testParseStyleTableClassName(): void + { + $html = '
'; + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, $html); + + self::assertInstanceOf(Table::class, $section->getElement(0)); + self::assertEquals('pStyle', $section->getElement(0)->getStyle()->getStyleName()); + } + /** * Test underline. */