Skip to content

Commit 02e3aa9

Browse files
authored
Merge pull request #2338 from Progi1984/className
HTML Reader : Set style name from the CSS class
2 parents 0af4ca1 + 02a92c8 commit 02e3aa9

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/PhpWord/Shared/Html.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ protected static function parseInlineStyle($node, $styles = [])
162162
$attributeClass = $attributes->getNamedItem('class');
163163
if ($attributeClass && self::$css) {
164164
$styles = self::parseStyleDeclarations(self::$css->getStyle('.' . $attributeClass->value), $styles);
165+
$styles['className'] = $attributeClass->value;
165166
}
166167

167168
$attributeStyle = $attributes->getNamedItem('style');
@@ -411,6 +412,11 @@ protected static function parseTable($node, $element, &$styles)
411412

412413
$newElement = $element->addTable($elementStyles);
413414

415+
// Add style name from CSS Class
416+
if (isset($elementStyles['className'])) {
417+
$newElement->getStyle()->setStyleName($elementStyles['className']);
418+
}
419+
414420
$attributes = $node->attributes;
415421
if ($attributes->getNamedItem('border') !== null) {
416422
$border = (int) $attributes->getNamedItem('border')->value;

tests/PhpWordTests/Shared/HtmlTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use Exception;
2121
use PhpOffice\PhpWord\Element\Section;
22+
use PhpOffice\PhpWord\Element\Table;
2223
use PhpOffice\PhpWord\PhpWord;
2324
use PhpOffice\PhpWord\Shared\Html;
2425
use PhpOffice\PhpWord\SimpleType\Jc;
@@ -134,6 +135,17 @@ public function testParseStyle(): void
134135
self::assertEquals('22.5', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:r/w:rPr/w:sz', 'w:val'));
135136
}
136137

138+
public function testParseStyleTableClassName(): void
139+
{
140+
$html = '<style type="text/css">.pStyle { font-size:15px; }</style><table class="pStyle"><tr><td></td></tr></table>';
141+
$phpWord = new PhpWord();
142+
$section = $phpWord->addSection();
143+
Html::addHtml($section, $html);
144+
145+
self::assertInstanceOf(Table::class, $section->getElement(0));
146+
self::assertEquals('pStyle', $section->getElement(0)->getStyle()->getStyleName());
147+
}
148+
137149
/**
138150
* Test underline.
139151
*/

0 commit comments

Comments
 (0)