forked from PHPOffice/PHPWord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Html Reader Process Titles as Headings Not Paragraphs
Fix PHPOffice#1692. Builds on work started some time ago by @0b10011, to whom primary credit is due. Html Reader does not process the `head` section of the document, and, in particular, does not process its `style` section. It will, however, process inline styles, so 0b10011's model of adding the title as a text run (with styles) will work well once this change is applied. However, that model would not deal with the alternative method of assigning a Title Style, and just adding the title as text. In order to accommodate that, I have removed the declaration of heading font styles in the head section, and now generate them all inline in the body. This has the added benefit of being able to read the doc as html, then saving it as docx, preserving, at least in part, any user-defined font styles. Note that html does have pre-defined title styles, but docx does not. @constip suggests in the original issue that margin top and bottom are being applied too frequently. I believe that was addressed by recently merged PR PHPOffice#2475. It is also suggested that the `*` css selector be dropped in favor of `body`. 2475 added the body selector. I agree that this renders the `*` selector unnecessary, and, as stated in the issue, it can cause problems. This PR drops that selector. It is also suggested that `loadHTML` be used instead of `loadXML`. This is not as easy a change as it seems, because loadHTML uses ISO-8859-1 charset rather than UTF-8, so I will not attempt that change.
- Loading branch information
Showing
8 changed files
with
155 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# [1.3.0](https://github.com/PHPOffice/PHPWord/tree/1.3.0) (WIP) | ||
|
||
[Full Changelog](https://github.com/PHPOffice/PHPWord/compare/1.2.0...1.3.0) | ||
|
||
## Enhancements | ||
|
||
### Bug fixes | ||
|
||
- MsDoc Reader : Correct Font Size Calculation by [@oleibman](https://github.com/oleibman) Issue [#2526](https://github.com/PHPOffice/PHPWord/issues/2526) PR [#2531](https://github.com/PHPOffice/PHPWord/pull/2531) | ||
- Html Reader : Process Titles as Headings not Paragraphs [@0b10011](https://github.com/0b10011) and [@oleibman](https://github.com/oleibman) Issue [#1692](https://github.com/PHPOffice/PHPWord/issues/1692) PR [#2533](https://github.com/PHPOffice/PHPWord/pull/2533) | ||
|
||
### Miscellaneous | ||
|
||
|
||
### BC Breaks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* This file is part of PHPWord - A pure PHP library for reading and writing | ||
* word processing documents. | ||
* | ||
* PHPWord is free software distributed under the terms of the GNU Lesser | ||
* General Public License version 3 as published by the Free Software Foundation. | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. For the full list of | ||
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors. | ||
* | ||
* @see https://github.com/PHPOffice/PHPWord | ||
* | ||
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 | ||
*/ | ||
|
||
namespace PhpOffice\PhpWordTests\Shared; | ||
|
||
use PhpOffice\PhpWord\Element\TextRun; | ||
use PhpOffice\PhpWord\PhpWord; | ||
use PhpOffice\PhpWord\Settings; | ||
use PhpOffice\PhpWord\Shared\Html as SharedHtml; | ||
use PhpOffice\PhpWord\Writer\HTML as HtmlWriter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Test class for PhpOffice\PhpWord\Shared\Html. | ||
* | ||
* @coversDefaultClass \PhpOffice\PhpWord\Shared\Html | ||
*/ | ||
class HtmlHeadingsTest extends TestCase | ||
{ | ||
public function testRoundTripHeadings(): void | ||
{ | ||
Settings::setOutputEscapingEnabled(true); | ||
$originalDoc = new PhpWord(); | ||
$originalDoc->addTitleStyle(1, ['size' => 20]); | ||
$section = $originalDoc->addSection(); | ||
$expectedStrings = []; | ||
$section->addTitle('Title 1', 1); | ||
$expectedStrings[] = '<h1 style="font-size: 20pt;">Title 1</h1>'; | ||
for ($i = 2; $i <= 6; ++$i) { | ||
$textRun = new TextRun(); | ||
$textRun->addText('Title '); | ||
$textRun->addText("$i", ['italic' => true]); | ||
$section->addTitle($textRun, $i); | ||
$expectedStrings[] = "<h$i>Title <span style=\"font-style: italic;\">$i</span></h$i>"; | ||
} | ||
$writer = new HtmlWriter($originalDoc); | ||
$content = $writer->getContent(); | ||
foreach ($expectedStrings as $expectedString) { | ||
self::assertStringContainsString($expectedString, $content); | ||
} | ||
|
||
$newDoc = new PhpWord(); | ||
$newSection = $newDoc->addSection(); | ||
SharedHtml::addHtml($newSection, $content, true); | ||
$newWriter = new HtmlWriter($newDoc); | ||
$newContent = $newWriter->getContent(); | ||
// Reader transforms Text to TextRun, | ||
// but result is functionally the same. | ||
$firstStringAsTextRun = '<h1><span style="font-size: 20pt;">Title 1</span></h1>'; | ||
self::assertSame($content, str_replace($firstStringAsTextRun, $expectedStrings[0], $newContent)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters