Skip to content

Commit 16904a2

Browse files
committed
Catch Up
1 parent 56ea8c0 commit 16904a2

File tree

6 files changed

+28
-22
lines changed

6 files changed

+28
-22
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,10 @@ protected static function parseInput($node, $element, &$styles): void
470470
*/
471471
protected static function parseHeading(DOMNode $node, AbstractContainer $element, array &$styles, string $headingStyle): TextRun
472472
{
473-
self::parseInlineStyle($node, $styles['font']);
474-
// Create a TextRun to hold styles and text
475-
$styles['paragraph'] = $headingStyle;
476-
$textRun = new TextRun($styles['paragraph']);
473+
$style = new Paragraph();
474+
$style->setStyleName($headingStyle);
475+
$style->setStyleByArray(self::parseInlineStyle($node, $styles['paragraph']));
476+
$textRun = new TextRun($style);
477477

478478
// Create a title with level corresponding to number in heading style
479479
// (Eg, Heading1 = 1)

Diff for: src/PhpWord/Writer/HTML/Element/Title.php

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

2121
use PhpOffice\PhpWord\Element\Title as PhpWordTitle;
22-
use PhpOffice\PhpWord\Style;
2322
use PhpOffice\PhpWord\Writer\HTML;
24-
use PhpOffice\PhpWord\Writer\HTML\Style\Font;
23+
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph;
2524

2625
/**
2726
* TextRun element HTML writer.
@@ -44,17 +43,21 @@ public function write()
4443
$tag = 'h' . $this->element->getDepth();
4544

4645
$text = $this->element->getText();
46+
$paragraphStyle = null;
4747
if (is_string($text)) {
4848
$text = $this->parentWriter->escapeHTML($text);
4949
} else {
50+
$paragraphStyle = $text->getParagraphStyle();
5051
$writer = new Container($this->parentWriter, $text);
5152
$text = $writer->write();
5253
}
5354
$css = '';
54-
$style = Style::getStyle('Heading_' . $this->element->getDepth());
55-
if ($style !== null) {
56-
$styleWriter = new Font($style);
57-
$css = ' style="' . $styleWriter->write() . '"';
55+
if (is_object($paragraphStyle)) {
56+
$styleWriter = new Paragraph($paragraphStyle);
57+
$write = $styleWriter->write();
58+
if ($write !== '') {
59+
$css = " style=\"$write\"";
60+
}
5861
}
5962

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

Diff for: src/PhpWord/Writer/HTML/Part/Head.php

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ private function writeStyles(): string
137137
$styleWriter = new FontStyleWriter($style);
138138
if ($style->getStyleType() == 'title') {
139139
$name = str_replace('Heading_', 'h', $name);
140+
$css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL;
140141
$styleParagraph = $style->getParagraph();
141142
$style = $styleParagraph;
142143
} else {

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

+4-5
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 style="font-size: 20pt;">Title 1</h1>';
43+
$expectedStrings[] = '<h1>Title 1</h1>';
4444
for ($i = 2; $i <= 6; ++$i) {
4545
$textRun = new TextRun();
4646
$textRun->addText('Title ');
@@ -59,9 +59,8 @@ public function testRoundTripHeadings(): void
5959
SharedHtml::addHtml($newSection, $content, true);
6060
$newWriter = new HtmlWriter($newDoc);
6161
$newContent = $newWriter->getContent();
62-
// Reader transforms Text to TextRun,
63-
// but result is functionally the same.
64-
$firstStringAsTextRun = '<h1><span style="font-size: 20pt;">Title 1</span></h1>';
65-
self::assertSame($content, str_replace($firstStringAsTextRun, $expectedStrings[0], $newContent));
62+
63+
// This needs work
64+
self::assertSame($newContent, str_replace('h1 {font-size: 20pt;}' . PHP_EOL, '', $content));
6665
}
6766
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use PhpOffice\PhpWord\Element\Table;
2525
use PhpOffice\PhpWord\Element\Text;
2626
use PhpOffice\PhpWord\Element\TextRun;
27+
use PhpOffice\PhpWord\Element\Title;
2728
use PhpOffice\PhpWord\PhpWord;
2829
use PhpOffice\PhpWord\Settings;
2930
use PhpOffice\PhpWord\Shared\Converter;
@@ -134,6 +135,8 @@ public function testParseHeader(): void
134135

135136
self::assertCount(1, $section->getElements());
136137
$element = $section->getElement(0);
138+
self::assertInstanceOf(Title::class, $element);
139+
$element = $element->getText();
137140
self::assertInstanceOf(TextRun::class, $element);
138141
self::assertInstanceOf(Paragraph::class, $element->getParagraphStyle());
139142
self::assertEquals('Heading1', $element->getParagraphStyle()->getStyleName());
@@ -155,6 +158,8 @@ public function testParseHeaderStyle(): void
155158

156159
self::assertCount(1, $section->getElements());
157160
$element = $section->getElement(0);
161+
self::assertInstanceOf(Title::class, $element);
162+
$element = $element->getText();
158163
self::assertInstanceOf(TextRun::class, $element);
159164
self::assertInstanceOf(Paragraph::class, $element->getParagraphStyle());
160165
self::assertEquals('Heading1', $element->getParagraphStyle()->getStyleName());

Diff for: tests/PhpWordTests/Writer/HTML/PartTest.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,15 @@ public function testTitleStyles(): void
179179
$xpath = new DOMXPath($dom);
180180

181181
$style = Helper::getTextContent($xpath, '/html/head/style');
182-
//self::assertNotFalse(strpos($style, 'h1 {font-family: \'Calibri\'; font-weight: bold;}'));
182+
self::assertNotFalse(strpos($style, 'h1 {font-family: \'Calibri\'; font-weight: bold;}'));
183183
self::assertNotFalse(strpos($style, 'h1 {margin-top: 0.5pt; margin-bottom: 0.5pt;}'));
184-
//self::assertNotFalse(strpos($style, 'h2 {font-family: \'Times New Roman\'; font-style: italic;}'));
184+
self::assertNotFalse(strpos($style, 'h2 {font-family: \'Times New Roman\'; font-style: italic;}'));
185185
self::assertNotFalse(strpos($style, 'h2 {margin-top: 0.25pt; margin-bottom: 0.25pt;}'));
186186
self::assertEquals(1, Helper::getLength($xpath, '/html/body/div/h1'));
187187
self::assertEquals(2, Helper::getLength($xpath, '/html/body/div/h2'));
188-
// code for getNamedItem had been erroneous
189-
self::assertSame("font-family: 'Calibri'; font-weight: bold;", Helper::getNamedItem($xpath, '/html/body/div/h1', 'style')->textContent);
190188
$html = Helper::getHtmlString($phpWord);
191-
self::assertStringContainsString('<h1 style="font-family: \'Calibri\'; font-weight: bold;">Header 1 #1</h1>', $html);
192-
self::assertStringContainsString('<h2 style="font-family: \'Times New Roman\'; font-style: italic;">Header 2 #1</h2>', $html);
193-
self::assertStringContainsString('<h2 style="font-family: \'Times New Roman\'; font-style: italic;">Header 2 #2</h2>', $html);
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);
194192
}
195193
}

0 commit comments

Comments
 (0)