-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Html Reader Process Titles as Headings Not Paragraphs #2533
Open
oleibman
wants to merge
13
commits into
PHPOffice:master
Choose a base branch
from
oleibman:word1692
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+162
−58
Open
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7301f7d
Html Reader Process Titles as Headings Not Paragraphs
oleibman 3638e1b
Changes Requested by @Progi1984
oleibman a6e8940
Merge branch 'master' into word1692
oleibman c0f2310
Merge branch 'master' into word1692
oleibman 6e09f4c
Update 2.0.0.md
oleibman fdc30b6
Merge branch 'master' into word1692
oleibman 03ad7ec
Php-cs-fixer Recommendations
oleibman be553c1
Merge branch 'master' into word1692
oleibman 5d34906
Update FontTest.php
oleibman 2e0eb70
Merge branch 'master' into word1692
oleibman f3f7d9a
Catch Up
oleibman e740deb
Html Writer Duplicate Header Styles in Style Tags
oleibman 1368e21
Apply Suggested Changes
oleibman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 | ||
---|---|---|---|---|
|
@@ -92,17 +92,16 @@ private function writeStyles(): string | |||
'font-size' => Settings::getDefaultFontSize() . 'pt', | ||||
'color' => "#{$defaultFontColor}", | ||||
]; | ||||
// Mpdf sometimes needs separate tag for body; doesn't harm others. | ||||
$bodyarray = $astarray; | ||||
|
||||
$defaultWhiteSpace = $this->getParentWriter()->getDefaultWhiteSpace(); | ||||
if ($defaultWhiteSpace) { | ||||
$astarray['white-space'] = $defaultWhiteSpace; | ||||
} | ||||
$bodyarray = $astarray; | ||||
|
||||
foreach ([ | ||||
'body' => $bodyarray, | ||||
'*' => $astarray, | ||||
//'*' => $astarray, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Latest push has this change. |
||||
'a.NoteRef' => [ | ||||
'text-decoration' => 'none', | ||||
], | ||||
|
@@ -135,12 +134,13 @@ private function writeStyles(): string | |||
$styleWriter = new FontStyleWriter($style); | ||||
if ($style->getStyleType() == 'title') { | ||||
$name = str_replace('Heading_', 'h', $name); | ||||
$css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL; | ||||
$styleParagraph = $style->getParagraph(); | ||||
$style = $styleParagraph; | ||||
} else { | ||||
$name = '.' . $name; | ||||
$css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL; | ||||
} | ||||
$css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL; | ||||
} | ||||
if ($style instanceof Paragraph) { | ||||
$styleWriter = new ParagraphStyleWriter($style); | ||||
|
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,76 @@ | ||
<?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 does not yet support h1 declaration in css. | ||
$content = 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 | ||
) | ||
); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Latest push has this change.