[go: nahoru, domu]

Skip to content
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

Update Html parser to accept line-height:normal #2041

Merged
merged 4 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
'normal' line-height means default line height
Add unit test
  • Loading branch information
DE TROOSTEMBERGH Antoine authored and DE TROOSTEMBERGH Antoine committed Apr 15, 2021
commit a4055fb7a0ddb30c557dcdefbaa37e378a1fa929
11 changes: 2 additions & 9 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,16 +661,9 @@ protected static function parseStyle($attribute, $styles)
break;
case 'line-height':
$matches = array();
if (preg_match('/([a-z]+)/', $cValue, $matches)) {
//$cvalue text and not number
if ($cValue == 'normal') {
$cValue = 1.12;
} else {
$cValue = 1.13;
}
if ($cValue === 'normal') {
$spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO;
//we are subtracting 1 line height because the Spacing writer is adding one line
$spacing = ($cValue * Paragraph::LINE_HEIGHT) - Paragraph::LINE_HEIGHT;
$spacing = 0;
} elseif (preg_match('/([0-9]+\.?[0-9]*[a-z]+)/', $cValue, $matches)) {
//matches number with a unit, e.g. 12px, 15pt, 20mm, ...
$spacingLineRule = \PhpOffice\PhpWord\SimpleType\LineSpacingRule::EXACT;
Expand Down
5 changes: 5 additions & 0 deletions tests/PhpWord/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public function testParseLineHeight()
Html::addHtml($section, '<p style="line-height: 15pt;">test</p>');
Html::addHtml($section, '<p style="line-height: 120%;">test</p>');
Html::addHtml($section, '<p style="line-height: 0.17in;">test</p>');
Html::addHtml($section, '<p style="line-height: normal;">test</p>');

$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:pPr/w:spacing'));
Expand All @@ -175,6 +176,10 @@ public function testParseLineHeight()
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[4]/w:pPr/w:spacing'));
$this->assertEquals(244.8, $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:spacing', 'w:line'));
$this->assertEquals(LineSpacingRule::EXACT, $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:spacing', 'w:lineRule'));

$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[5]/w:pPr/w:spacing'));
$this->assertEquals(Paragraph::LINE_HEIGHT, $doc->getElementAttribute('/w:document/w:body/w:p[5]/w:pPr/w:spacing', 'w:line'));
$this->assertEquals(LineSpacingRule::AUTO, $doc->getElementAttribute('/w:document/w:body/w:p[5]/w:pPr/w:spacing', 'w:lineRule'));
}

/**
Expand Down