Skip to content

Commit 456ede6

Browse files
committed
Reader HTML: Support font styles for h1/h6
1 parent 3b12b4e commit 456ede6

File tree

3 files changed

+67
-15
lines changed

3 files changed

+67
-15
lines changed

docs/changes/1.x/1.4.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Add Default font color for Word by [@Collie-IT](https://github.com/Collie-IT) in [#2700](https://github.com/PHPOffice/PHPWord/pull/2700)
1515
- Writer HTML: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey)
1616
- Add basic ruby text (phonetic guide) support for Word2007 and HTML Reader/Writer, RTF Writer, basic support for ODT writing by [@Deadpikle](https://github.com/Deadpikle) in [#2727](https://github.com/PHPOffice/PHPWord/pull/2727)
17+
- Reader HTML: Support font styles for h1/h6 by [@Progi1984](https://github.com/Progi1984) fixing [#2619](https://github.com/PHPOffice/PHPWord/issues/2619) in [#2737](https://github.com/PHPOffice/PHPWord/pull/2737)
1718

1819
### Bug fixes
1920

src/PhpWord/Shared/Html.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,16 @@ protected static function parseNode($node, $element, $styles = [], $data = []):
211211

212212
// Node mapping table
213213
$nodes = [
214-
// $method $node $element $styles $data $argument1 $argument2
215-
'p' => ['Paragraph', $node, $element, $styles, null, null, null],
216-
'h1' => ['Heading', null, $element, $styles, null, 'Heading1', null],
217-
'h2' => ['Heading', null, $element, $styles, null, 'Heading2', null],
218-
'h3' => ['Heading', null, $element, $styles, null, 'Heading3', null],
219-
'h4' => ['Heading', null, $element, $styles, null, 'Heading4', null],
220-
'h5' => ['Heading', null, $element, $styles, null, 'Heading5', null],
221-
'h6' => ['Heading', null, $element, $styles, null, 'Heading6', null],
222-
'#text' => ['Text', $node, $element, $styles, null, null, null],
223-
'strong' => ['Property', null, null, $styles, null, 'bold', true],
214+
// $method $node $element $styles $data $argument1 $argument2
215+
'p' => ['Paragraph', $node, $element, $styles, null, null, null],
216+
'h1' => ['Heading', $node, $element, $styles, null, 'Heading1', null],
217+
'h2' => ['Heading', $node, $element, $styles, null, 'Heading2', null],
218+
'h3' => ['Heading', $node, $element, $styles, null, 'Heading3', null],
219+
'h4' => ['Heading', $node, $element, $styles, null, 'Heading4', null],
220+
'h5' => ['Heading', $node, $element, $styles, null, 'Heading5', null],
221+
'h6' => ['Heading', $node, $element, $styles, null, 'Heading6', null],
222+
'#text' => ['Text', $node, $element, $styles, null, null, null],
223+
'strong' => ['Property', null, null, $styles, null, 'bold', true],
224224
'b' => ['Property', null, null, $styles, null, 'bold', true],
225225
'em' => ['Property', null, null, $styles, null, 'italic', true],
226226
'i' => ['Property', null, null, $styles, null, 'italic', true],
@@ -354,12 +354,13 @@ protected static function parseInput($node, $element, &$styles): void
354354
* @todo Think of a clever way of defining header styles, now it is only based on the assumption, that
355355
* Heading1 - Heading6 are already defined somewhere
356356
*/
357-
protected static function parseHeading($element, &$styles, $argument1)
358-
{
359-
$styles['paragraph'] = $argument1;
360-
$newElement = $element->addTextRun($styles['paragraph']);
357+
protected static function parseHeading(DOMNode $node, AbstractContainer $element, array &$styles, string $argument1): TextRun
358+
{
359+
$style = new Paragraph();
360+
$style->setStyleName($argument1);
361+
$style->setStyleByArray(self::parseInlineStyle($node, $styles['paragraph']));
361362

362-
return $newElement;
363+
return $element->addTextRun($style);
363364
}
364365

365366
/**

tests/PhpWordTests/Shared/HtmlTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@
2222
use PhpOffice\PhpWord\ComplexType\RubyProperties;
2323
use PhpOffice\PhpWord\Element\Section;
2424
use PhpOffice\PhpWord\Element\Table;
25+
use PhpOffice\PhpWord\Element\Text;
26+
use PhpOffice\PhpWord\Element\TextRun;
27+
use PhpOffice\PhpWord\Element\Title;
2528
use PhpOffice\PhpWord\PhpWord;
2629
use PhpOffice\PhpWord\Shared\Converter;
2730
use PhpOffice\PhpWord\Shared\Html;
2831
use PhpOffice\PhpWord\SimpleType\Jc;
2932
use PhpOffice\PhpWord\SimpleType\LineSpacingRule;
3033
use PhpOffice\PhpWord\SimpleType\TblWidth;
34+
use PhpOffice\PhpWord\Style\Font;
3135
use PhpOffice\PhpWord\Style\Paragraph;
3236
use PhpOffice\PhpWordTests\AbstractWebServerEmbedded;
3337
use PhpOffice\PhpWordTests\TestHelperDOCX;
@@ -105,6 +109,52 @@ public function testParseFullHtml(): void
105109
self::assertCount(2, $section->getElements());
106110
}
107111

112+
/**
113+
*/
114+
public function testParseHeader(): void
115+
{
116+
$phpWord = new PhpWord();
117+
$section = $phpWord->addSection();
118+
Html::addHtml($section, '<h1>Text</h1>');
119+
120+
self::assertCount(1, $section->getElements());
121+
$element = $section->getElement(0);
122+
self::assertInstanceOf(TextRun::class, $element);
123+
self::assertInstanceOf(Paragraph::class, $element->getParagraphStyle());
124+
self::assertEquals('Heading1', $element->getParagraphStyle()->getStyleName());
125+
self::assertEquals('', $element->getParagraphStyle()->getAlignment());
126+
self::assertEquals('Text', $element->getText());
127+
self::assertCount(1, $element->getElements());
128+
$subElement = $element->getElement(0);
129+
self::assertInstanceOf(Text::class, $subElement);
130+
self::assertInstanceOf(Font::class, $subElement->getFontStyle());
131+
self::assertNull($subElement->getFontStyle()->getColor());
132+
self::assertEquals('Text', $subElement->getText());
133+
}
134+
135+
/**
136+
*/
137+
public function testParseHeaderStyle(): void
138+
{
139+
$phpWord = new PhpWord();
140+
$section = $phpWord->addSection();
141+
Html::addHtml($section, '<h1 style="color: #ff0000; text-align:center">Text</h1>');
142+
143+
self::assertCount(1, $section->getElements());
144+
$element = $section->getElement(0);
145+
self::assertInstanceOf(TextRun::class, $element);
146+
self::assertInstanceOf(Paragraph::class, $element->getParagraphStyle());
147+
self::assertEquals('Heading1', $element->getParagraphStyle()->getStyleName());
148+
self::assertEquals('center', $element->getParagraphStyle()->getAlignment());
149+
self::assertEquals('Text', $element->getText());
150+
self::assertCount(1, $element->getElements());
151+
$subElement = $element->getElement(0);
152+
self::assertInstanceOf(Text::class, $subElement);
153+
self::assertInstanceOf(Font::class, $subElement->getFontStyle());
154+
self::assertEquals('ff0000', $subElement->getFontStyle()->getColor());
155+
self::assertEquals('Text', $subElement->getText());
156+
}
157+
108158
/**
109159
* Test HTML entities.
110160
*/

0 commit comments

Comments
 (0)