Skip to content

Commit 92a4c5e

Browse files
committed
Html Writer Duplicate Header Styles in Style Tags
Nominally redundant, but makes things easier for Html Reader.
1 parent 16904a2 commit 92a4c5e

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/PhpWord/Writer/HTML/Element/Title.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
namespace PhpOffice\PhpWord\Writer\HTML\Element;
2020

2121
use PhpOffice\PhpWord\Element\Title as PhpWordTitle;
22+
use PhpOffice\PhpWord\Style;
2223
use PhpOffice\PhpWord\Writer\HTML;
24+
use PhpOffice\PhpWord\Writer\HTML\Style\Font;
2325
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph;
2426

2527
/**
@@ -51,14 +53,23 @@ public function write()
5153
$writer = new Container($this->parentWriter, $text);
5254
$text = $writer->write();
5355
}
54-
$css = '';
56+
$write1 = $write2 = $write3 = '';
57+
$style = Style::getStyle('Heading_' . $this->element->getDepth());
58+
if ($style !== null) {
59+
$styleWriter = new Font($style);
60+
$write1 = $styleWriter->write();
61+
}
5562
if (is_object($paragraphStyle)) {
5663
$styleWriter = new Paragraph($paragraphStyle);
57-
$write = $styleWriter->write();
58-
if ($write !== '') {
59-
$css = " style=\"$write\"";
64+
$write3 = $styleWriter->write();
65+
if ($write1 !== '' && $write3 !== '') {
66+
$write2 = ' ';
6067
}
6168
}
69+
$css = "$write1$write2$write3";
70+
if ($css !== '') {
71+
$css = " style=\"$css\"";
72+
}
6273

6374
$content = "<{$tag}{$css}>{$text}</{$tag}>" . PHP_EOL;
6475

tests/PhpWordTests/Shared/HtmlHeadingsTest.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testRoundTripHeadings(): void
4040
$section = $originalDoc->addSection();
4141
$expectedStrings = [];
4242
$section->addTitle('Title 1', 1);
43-
$expectedStrings[] = '<h1>Title 1</h1>';
43+
$expectedStrings[] = '<h1 style="font-size: 20pt;">Title 1</h1>';
4444
for ($i = 2; $i <= 6; ++$i) {
4545
$textRun = new TextRun();
4646
$textRun->addText('Title ');
@@ -59,8 +59,18 @@ public function testRoundTripHeadings(): void
5959
SharedHtml::addHtml($newSection, $content, true);
6060
$newWriter = new HtmlWriter($newDoc);
6161
$newContent = $newWriter->getContent();
62+
// Reader does not yet support h1 declaration in css.
63+
$content = str_replace('h1 {font-size: 20pt;}' . PHP_EOL, '', $content);
6264

63-
// This needs work
64-
self::assertSame($newContent, str_replace('h1 {font-size: 20pt;}' . PHP_EOL, '', $content));
65+
// Reader transforms Text to TextRun,
66+
// but result is functionally the same.
67+
self::assertSame(
68+
$newContent,
69+
str_replace(
70+
'<h1 style="font-size: 20pt;">Title 1</h1>',
71+
'<h1><span style="font-size: 20pt;">Title 1</span></h1>',
72+
$content
73+
)
74+
);
6575
}
6676
}

tests/PhpWordTests/Writer/HTML/PartTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ public function testTitleStyles(): void
186186
self::assertEquals(1, Helper::getLength($xpath, '/html/body/div/h1'));
187187
self::assertEquals(2, Helper::getLength($xpath, '/html/body/div/h2'));
188188
$html = Helper::getHtmlString($phpWord);
189-
self::assertStringContainsString('<h1>Header 1 #1</h1>', $html);
190-
self::assertStringContainsString('<h2>Header 2 #1</h2>', $html);
191-
self::assertStringContainsString('<h2>Header 2 #2</h2>', $html);
189+
self::assertStringContainsString('<h1 style="font-family: \'Calibri\'; font-weight: bold;">Header 1 #1</h1>', $html);
190+
self::assertStringContainsString('<h2 style="font-family: \'Times New Roman\'; font-style: italic;">Header 2 #1</h2>', $html);
191+
self::assertStringContainsString('<h2 style="font-family: \'Times New Roman\'; font-style: italic;">Header 2 #2</h2>', $html);
192192
}
193193
}

0 commit comments

Comments
 (0)