From 02a92c89241a0d28b84077e1910fc068a9f22a06 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Tue, 22 Nov 2022 21:38:41 +0100 Subject: [PATCH] HTML Reader : Set style name from the CSS class --- src/PhpWord/Shared/Html.php | 6 ++++++ tests/PhpWordTests/Shared/HtmlTest.php | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index b2c99d03..c937d299 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -162,6 +162,7 @@ class Html $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 @@ class Html $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 7c02581a..8da6ccc6 100644 --- a/tests/PhpWordTests/Shared/HtmlTest.php +++ b/tests/PhpWordTests/Shared/HtmlTest.php @@ -19,6 +19,7 @@ namespace PhpOffice\PhpWordTests\Shared; 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 @@ class HtmlTest extends AbstractWebServerEmbeddedTest 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. */