Skip to content

Commit

Permalink
Add support table row height when importing HTML (PHPOffice#2350)
Browse files Browse the repository at this point in the history
* Add support table row height when importing HTML

* fix space

* fix phpcsfixer

* Add test table row height

* Fix xpath test table row height

* Fix attribute first test table row height

* Fix $cValue

Co-authored-by: Progi1984 <[email protected]>

* Fix css

* Fix test on pt

Co-authored-by: Progi1984 <[email protected]>
  • Loading branch information
ytilotti and Progi1984 authored Jan 6, 2023
1 parent 60734dd commit ef99fac
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,11 @@ protected static function parseRow($node, $element, &$styles)
$rowStyles['tblHeader'] = true;
}

return $element->addRow(null, $rowStyles);
// set cell height to control row heights
$height = $rowStyles['height'] ?? null;
unset($rowStyles['height']); // would not apply

return $element->addRow($height, $rowStyles);
}

/**
Expand Down Expand Up @@ -808,6 +812,11 @@ protected static function parseStyleDeclarations(array $selectors, array $styles
$styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::AUTO;
}

break;
case 'height':
$styles['height'] = Converter::cssToTwip($value);
$styles['exactHeight'] = true;

break;
case 'border':
case 'border-top':
Expand Down
52 changes: 52 additions & 0 deletions tests/PhpWordTests/Shared/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,58 @@ public function testParseTableAndCellWidth(): void
self::assertEquals('dxa', $doc->getElement($xpath)->getAttribute('w:type'));
}

/**
* Parse heights in rows, which also allows for controlling column height.
*/
public function testParseTableRowHeight(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection([
'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE,
]);

$html = <<<HTML
<table>
<tr style="height: 100px;">
<td>100px</td>
</tr>
<tr style="height: 200pt;">
<td>200pt</td>
</tr>
<tr>
<td>
<table>
<tr style="height: 300px;">
<td>300px</td>
</tr>
</table>
</td>
</tr>
</table>
HTML;

Html::addHtml($section, $html);
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');

// <tr style="height: 100; ... 100px = 1500 twips (100 / 96 * 1440)
$xpath = '/w:document/w:body/w:tbl/w:tr/w:trPr/w:trHeight';
self::assertTrue($doc->elementExists($xpath));
self::assertEquals(1500, $doc->getElement($xpath)->getAttribute('w:val'));
self::assertEquals('exact', $doc->getElement($xpath)->getAttribute('w:hRule'));

// <tr style="height: 200pt; ... 200pt = 4000 twips (200 / 72 * 1440)
$xpath = '/w:document/w:body/w:tbl/w:tr[2]/w:trPr/w:trHeight';
self::assertTrue($doc->elementExists($xpath));
self::assertEquals(4000, $doc->getElement($xpath)->getAttribute('w:val'));
self::assertEquals('exact', $doc->getElement($xpath)->getAttribute('w:hRule'));

// <tr style="width: 300; .. 300px = 4500 twips (300 / 72 * 1440)
$xpath = '/w:document/w:body/w:tbl/w:tr[3]/w:tc/w:tbl/w:tr/w:trPr/w:trHeight';
self::assertTrue($doc->elementExists($xpath));
self::assertEquals(4500, $doc->getElement($xpath)->getAttribute('w:val'));
self::assertEquals('exact', $doc->getElement($xpath)->getAttribute('w:hRule'));
}

/**
* Test parsing table (attribute border).
*/
Expand Down

0 comments on commit ef99fac

Please sign in to comment.