Add support table row height when importing HTML (#2350)

* 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 <progi1984@gmail.com>

* Fix css

* Fix test on pt

Co-authored-by: Progi1984 <progi1984@gmail.com>
This commit is contained in:
Yohann Tilotti 2023-01-06 16:26:34 +01:00 committed by GitHub
parent 60734dd7f1
commit ef99fac8e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 1 deletions

View File

@ -444,7 +444,11 @@ class Html
$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);
}
/**
@ -808,6 +812,11 @@ class Html
$styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::AUTO;
}
break;
case 'height':
$styles['height'] = Converter::cssToTwip($value);
$styles['exactHeight'] = true;
break;
case 'border':
case 'border-top':

View File

@ -464,6 +464,58 @@ HTML;
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).
*/