Skip to content

Commit

Permalink
Html Writer Duplicate Header Styles in Style Tags
Browse files Browse the repository at this point in the history
Nominally redundant, but makes things easier for Html Reader.
  • Loading branch information
oleibman committed Feb 6, 2025
1 parent f3f7d9a commit e740deb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
18 changes: 15 additions & 3 deletions src/PhpWord/Writer/HTML/Element/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
namespace PhpOffice\PhpWord\Writer\HTML\Element;

use PhpOffice\PhpWord\Element\Title as PhpWordTitle;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Writer\HTML;
use PhpOffice\PhpWord\Writer\HTML\Style\Font;
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph;

/**
Expand Down Expand Up @@ -52,13 +54,23 @@ public function write()
$text = $writer->write();
}
$css = '';
$write1 = $write2 = $write3 = '';
$style = Style::getStyle('Heading_' . $this->element->getDepth());
if ($style !== null) {
$styleWriter = new Font($style);
$write1 = $styleWriter->write();
}
if (is_object($paragraphStyle)) {
$styleWriter = new Paragraph($paragraphStyle);
$write = $styleWriter->write();
if ($write !== '') {
$css = " style=\"$write\"";
$write3 = $styleWriter->write();
if ($write1 !== '' && $write3 !== '') {
$write2 = ' ';
}
}
$css = "$write1$write2$write3";
if ($css !== '') {
$css = " style=\"$css\"";
}

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

Expand Down
16 changes: 13 additions & 3 deletions tests/PhpWordTests/Shared/HtmlHeadingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testRoundTripHeadings(): void
$section = $originalDoc->addSection();
$expectedStrings = [];
$section->addTitle('Title 1', 1);
$expectedStrings[] = '<h1>Title 1</h1>';
$expectedStrings[] = '<h1 style="font-size: 20pt;">Title 1</h1>';
for ($i = 2; $i <= 6; ++$i) {
$textRun = new TextRun();
$textRun->addText('Title ');
Expand All @@ -59,8 +59,18 @@ public function testRoundTripHeadings(): void
SharedHtml::addHtml($newSection, $content, true);
$newWriter = new HtmlWriter($newDoc);
$newContent = $newWriter->getContent();
// Reader does not yet support h1 declaration in css.
$content = str_replace('h1 {font-size: 20pt;}' . PHP_EOL, '', $content);

// This needs work
self::assertSame($newContent, str_replace('h1 {font-size: 20pt;}' . PHP_EOL, '', $content));
// Reader transforms Text to TextRun,
// but result is functionally the same.
self::assertSame(
$newContent,
str_replace(
'<h1 style="font-size: 20pt;">Title 1</h1>',
'<h1><span style="font-size: 20pt;">Title 1</span></h1>',
$content
)
);
}
}
6 changes: 3 additions & 3 deletions tests/PhpWordTests/Writer/HTML/PartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ public function testTitleStyles(): void
self::assertEquals(1, Helper::getLength($xpath, '/html/body/div/h1'));
self::assertEquals(2, Helper::getLength($xpath, '/html/body/div/h2'));
$html = Helper::getHtmlString($phpWord);
self::assertStringContainsString('<h1>Header 1 #1</h1>', $html);
self::assertStringContainsString('<h2>Header 2 #1</h2>', $html);
self::assertStringContainsString('<h2>Header 2 #2</h2>', $html);
self::assertStringContainsString('<h1 style="font-family: \'Calibri\'; font-weight: bold;">Header 1 #1</h1>', $html);
self::assertStringContainsString('<h2 style="font-family: \'Times New Roman\'; font-style: italic;">Header 2 #1</h2>', $html);
self::assertStringContainsString('<h2 style="font-family: \'Times New Roman\'; font-style: italic;">Header 2 #2</h2>', $html);
}
}

0 comments on commit e740deb

Please sign in to comment.