Skip to content

Commit e740deb

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

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

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

Lines changed: 15 additions & 3 deletions
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
/**
@@ -52,13 +54,23 @@ public function write()
5254
$text = $writer->write();
5355
}
5456
$css = '';
57+
$write1 = $write2 = $write3 = '';
58+
$style = Style::getStyle('Heading_' . $this->element->getDepth());
59+
if ($style !== null) {
60+
$styleWriter = new Font($style);
61+
$write1 = $styleWriter->write();
62+
}
5563
if (is_object($paragraphStyle)) {
5664
$styleWriter = new Paragraph($paragraphStyle);
57-
$write = $styleWriter->write();
58-
if ($write !== '') {
59-
$css = " style=\"$write\"";
65+
$write3 = $styleWriter->write();
66+
if ($write1 !== '' && $write3 !== '') {
67+
$write2 = ' ';
6068
}
6169
}
70+
$css = "$write1$write2$write3";
71+
if ($css !== '') {
72+
$css = " style=\"$css\"";
73+
}
6274

6375
$content = "<{$tag}{$css}>{$text}</{$tag}>" . PHP_EOL;
6476

tests/PhpWordTests/Shared/HtmlHeadingsTest.php

Lines changed: 13 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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)