Skip to content

Commit 2f270f2

Browse files
authored
Reader HTML: Support font styles for h1/h6 (#2737)
1 parent 5914e8e commit 2f270f2

File tree

4 files changed

+63
-22
lines changed

4 files changed

+63
-22
lines changed

Diff for: docs/changes/1.x/1.4.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Writer HTML: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2731](https://github.com/PHPOffice/PHPWord/pull/2731)
1616
- Writer ODText: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2735](https://github.com/PHPOffice/PHPWord/pull/2735)
1717
- 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)
18+
- 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)
1819

1920
- Added Support for Writer Epub3 by [@Sambit003](https://github.com/Sambit003) in [#2724](https://github.com/PHPOffice/PHPWord/pull/2724)
2021

Diff for: src/PhpWord/Shared/Html.php

+15-18
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],
@@ -345,21 +345,18 @@ protected static function parseInput($node, $element, &$styles): void
345345
/**
346346
* Parse heading node.
347347
*
348-
* @param AbstractContainer $element
349-
* @param array &$styles
350348
* @param string $argument1 Name of heading style
351349
*
352-
* @return TextRun
353-
*
354350
* @todo Think of a clever way of defining header styles, now it is only based on the assumption, that
355351
* Heading1 - Heading6 are already defined somewhere
356352
*/
357-
protected static function parseHeading($element, &$styles, $argument1)
353+
protected static function parseHeading(DOMNode $node, AbstractContainer $element, array &$styles, string $argument1): TextRun
358354
{
359-
$styles['paragraph'] = $argument1;
360-
$newElement = $element->addTextRun($styles['paragraph']);
355+
$style = new Paragraph();
356+
$style->setStyleName($argument1);
357+
$style->setStyleByArray(self::parseInlineStyle($node, $styles['paragraph']));
361358

362-
return $newElement;
359+
return $element->addTextRun($style);
363360
}
364361

365362
/**

Diff for: src/PhpWord/Style/Font.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Font extends AbstractStyle
109109
/**
110110
* Font color.
111111
*
112-
* @var string
112+
* @var null|string
113113
*/
114114
private $color;
115115

@@ -426,10 +426,8 @@ public function setSize($value = null)
426426

427427
/**
428428
* Get font color.
429-
*
430-
* @return string
431429
*/
432-
public function getColor()
430+
public function getColor(): ?string
433431
{
434432
return $this->color;
435433
}

Diff for: tests/PhpWordTests/Shared/HtmlTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
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;
2527
use PhpOffice\PhpWord\PhpWord;
2628
use PhpOffice\PhpWord\Shared\Converter;
2729
use PhpOffice\PhpWord\Shared\Html;
2830
use PhpOffice\PhpWord\SimpleType\Jc;
2931
use PhpOffice\PhpWord\SimpleType\LineSpacingRule;
3032
use PhpOffice\PhpWord\SimpleType\TblWidth;
33+
use PhpOffice\PhpWord\Style\Font;
3134
use PhpOffice\PhpWord\Style\Paragraph;
3235
use PhpOffice\PhpWordTests\AbstractWebServerEmbedded;
3336
use PhpOffice\PhpWordTests\TestHelperDOCX;
@@ -105,6 +108,48 @@ public function testParseFullHtml(): void
105108
self::assertCount(2, $section->getElements());
106109
}
107110

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

0 commit comments

Comments
 (0)