Skip to content

Commit

Permalink
Export headings to <h1> through <h6> (closes PHPOffice#1692)
Browse files Browse the repository at this point in the history
  • Loading branch information
0b10011 committed Sep 3, 2019
1 parent 8372a10 commit a1632d8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This release marked the addition of strict typing and return type declarations (
- Fixed specifying cell widths, background color, etc on `PhpOffice\PhpWord\Style\Cell` @0b10011 #1669
- Escape arrays of replacements in `TemplateProcessor` @0b10011 #1669
- Escape text provided for `<title>` when exporting to HTML @0b10011
- Export `<h1>` instead of `<p class="Heading1">` for headings @0b10011 #1692

### Miscellaneous
-
Expand Down
20 changes: 11 additions & 9 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PhpOffice\PhpWord\Element\AbstractContainer;
use PhpOffice\PhpWord\Element\Row;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Shared\HtmlDpi as Dpi;
Expand Down Expand Up @@ -264,20 +265,21 @@ protected static function parseParagraph($node, $element, &$styles)
/**
* Parse heading node
*
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
* @param array &$styles
* @param string $argument1 Name of heading style
* @return \PhpOffice\PhpWord\Element\TextRun
*
* @todo Think of a clever way of defining header styles, now it is only based on the assumption, that
* Heading1 - Heading6 are already defined somewhere
*/
protected static function parseHeading($element, &$styles, $argument1)
protected static function parseHeading(AbstractContainer $element, array &$styles, string $headingStyle): TextRun
{
$styles['paragraph'] = $argument1;
$newElement = $element->addTextRun($styles['paragraph']);
// Create a TextRun to hold styles and text
$styles['paragraph'] = $headingStyle;
$textRun = new TextRun($styles['paragraph']);

return $newElement;
// Create a title with level corresponding to number in heading style
// (Eg, Heading1 = 1)
$element->addTitle($textRun, (int) ltrim($headingStyle, 'Heading'));

// Return TextRun so children are parsed
return $textRun;
}

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/PhpWord/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

use PhpOffice\PhpWord\AbstractWebServerEmbeddedTest;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\SimpleType\LineSpacingRule;
use PhpOffice\PhpWord\Style\BorderSide;
Expand All @@ -28,13 +30,57 @@
use PhpOffice\PhpWord\Style\Lengths\Absolute;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\TestHelperDOCX;
use PhpOffice\PhpWord\Writer\HTML as Writer;

/**
* Test class for PhpOffice\PhpWord\Shared\Html
* @coversDefaultClass \PhpOffice\PhpWord\Shared\Html
*/
class HtmlTest extends AbstractWebServerEmbeddedTest
{
public function testRoundTripHeadings()
{
$file = __DIR__ . '/../_files/temp.html';
$html = <<<'HTML'
<!DOCTYPE html>
<!-- Generated by PHPWord -->
<html>
<head>
<meta charset="UTF-8" />
<title>PHPWord</title>
<style title="PHPWord Base Styles">
* {font-family: Arial; font-size: 10pt;}
a.NoteRef {text-decoration: none;}
hr {height: 1px; padding: 0; margin: 1em 0; border: 0; border-top: 1px solid #CCC;}
table {border: 1px solid black; border-spacing: 0px; width : 100%;}
td {border: 1px solid black;}
</style>
</head>
<body>
<h1>Heading <span style="font-style: italic;">1</span></h1>
<h2>Heading <span style="font-style: italic;">2</span></h2>
<h3>Heading <span style="font-style: italic;">3</span></h3>
<h4>Heading <span style="font-style: italic;">4</span></h4>
<h5>Heading <span style="font-style: italic;">5</span></h5>
<h6>Heading <span style="font-style: italic;">6</span></h6>
</body>
</html>

HTML;

$phpWord = new PhpWord();
Settings::setOutputEscapingEnabled(true);

$section = $phpWord->addSection();
Html::addHtml($section, $html, true);

(new Writer($phpWord))->save($file);

$this->assertEquals($html, file_get_contents($file));

unlink($file);
}

/**
* Test unit conversion functions with various numbers
*/
Expand Down

0 comments on commit a1632d8

Please sign in to comment.