Skip to content

Commit a1632d8

Browse files
committed
Export headings to <h1> through <h6> (closes PHPOffice#1692)
1 parent 8372a10 commit a1632d8

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ This release marked the addition of strict typing and return type declarations (
3030
- Fixed specifying cell widths, background color, etc on `PhpOffice\PhpWord\Style\Cell` @0b10011 #1669
3131
- Escape arrays of replacements in `TemplateProcessor` @0b10011 #1669
3232
- Escape text provided for `<title>` when exporting to HTML @0b10011
33+
- Export `<h1>` instead of `<p class="Heading1">` for headings @0b10011 #1692
3334

3435
### Miscellaneous
3536
-

src/PhpWord/Shared/Html.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PhpOffice\PhpWord\Element\AbstractContainer;
2323
use PhpOffice\PhpWord\Element\Row;
2424
use PhpOffice\PhpWord\Element\Table;
25+
use PhpOffice\PhpWord\Element\TextRun;
2526
use PhpOffice\PhpWord\Exception\Exception;
2627
use PhpOffice\PhpWord\Settings;
2728
use PhpOffice\PhpWord\Shared\HtmlDpi as Dpi;
@@ -264,20 +265,21 @@ protected static function parseParagraph($node, $element, &$styles)
264265
/**
265266
* Parse heading node
266267
*
267-
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
268-
* @param array &$styles
269-
* @param string $argument1 Name of heading style
270-
* @return \PhpOffice\PhpWord\Element\TextRun
271-
*
272268
* @todo Think of a clever way of defining header styles, now it is only based on the assumption, that
273269
* Heading1 - Heading6 are already defined somewhere
274270
*/
275-
protected static function parseHeading($element, &$styles, $argument1)
271+
protected static function parseHeading(AbstractContainer $element, array &$styles, string $headingStyle): TextRun
276272
{
277-
$styles['paragraph'] = $argument1;
278-
$newElement = $element->addTextRun($styles['paragraph']);
273+
// Create a TextRun to hold styles and text
274+
$styles['paragraph'] = $headingStyle;
275+
$textRun = new TextRun($styles['paragraph']);
279276

280-
return $newElement;
277+
// Create a title with level corresponding to number in heading style
278+
// (Eg, Heading1 = 1)
279+
$element->addTitle($textRun, (int) ltrim($headingStyle, 'Heading'));
280+
281+
// Return TextRun so children are parsed
282+
return $textRun;
281283
}
282284

283285
/**

tests/PhpWord/Shared/HtmlTest.php

+46
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
2222
use PhpOffice\PhpWord\Element\Section;
23+
use PhpOffice\PhpWord\PhpWord;
24+
use PhpOffice\PhpWord\Settings;
2325
use PhpOffice\PhpWord\SimpleType\Jc;
2426
use PhpOffice\PhpWord\SimpleType\LineSpacingRule;
2527
use PhpOffice\PhpWord\Style\BorderSide;
@@ -28,13 +30,57 @@
2830
use PhpOffice\PhpWord\Style\Lengths\Absolute;
2931
use PhpOffice\PhpWord\Style\Paragraph;
3032
use PhpOffice\PhpWord\TestHelperDOCX;
33+
use PhpOffice\PhpWord\Writer\HTML as Writer;
3134

3235
/**
3336
* Test class for PhpOffice\PhpWord\Shared\Html
3437
* @coversDefaultClass \PhpOffice\PhpWord\Shared\Html
3538
*/
3639
class HtmlTest extends AbstractWebServerEmbeddedTest
3740
{
41+
public function testRoundTripHeadings()
42+
{
43+
$file = __DIR__ . '/../_files/temp.html';
44+
$html = <<<'HTML'
45+
<!DOCTYPE html>
46+
<!-- Generated by PHPWord -->
47+
<html>
48+
<head>
49+
<meta charset="UTF-8" />
50+
<title>PHPWord</title>
51+
<style title="PHPWord Base Styles">
52+
* {font-family: Arial; font-size: 10pt;}
53+
a.NoteRef {text-decoration: none;}
54+
hr {height: 1px; padding: 0; margin: 1em 0; border: 0; border-top: 1px solid #CCC;}
55+
table {border: 1px solid black; border-spacing: 0px; width : 100%;}
56+
td {border: 1px solid black;}
57+
</style>
58+
</head>
59+
<body>
60+
<h1>Heading <span style="font-style: italic;">1</span></h1>
61+
<h2>Heading <span style="font-style: italic;">2</span></h2>
62+
<h3>Heading <span style="font-style: italic;">3</span></h3>
63+
<h4>Heading <span style="font-style: italic;">4</span></h4>
64+
<h5>Heading <span style="font-style: italic;">5</span></h5>
65+
<h6>Heading <span style="font-style: italic;">6</span></h6>
66+
</body>
67+
</html>
68+
69+
HTML;
70+
71+
$phpWord = new PhpWord();
72+
Settings::setOutputEscapingEnabled(true);
73+
74+
$section = $phpWord->addSection();
75+
Html::addHtml($section, $html, true);
76+
77+
(new Writer($phpWord))->save($file);
78+
79+
$this->assertEquals($html, file_get_contents($file));
80+
81+
unlink($file);
82+
}
83+
3884
/**
3985
* Test unit conversion functions with various numbers
4086
*/

0 commit comments

Comments
 (0)