From 3ace7706180af59c7b10b3b0bab5a8615849c92a Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Thu, 30 Jan 2025 21:20:49 -0800 Subject: [PATCH] Improve Test Coverage --- src/PhpWord/Shared/Html.php | 10 +- src/PhpWord/Shared/HtmlColours.php | 5 - tests/PhpWordTests/Shared/Html2Test.php | 140 ++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 10 deletions(-) create mode 100644 tests/PhpWordTests/Shared/Html2Test.php diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index d1bf13d7f7..26bc83e3bb 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -36,6 +36,7 @@ use PhpOffice\PhpWord\SimpleType\NumberFormat; use PhpOffice\PhpWord\SimpleType\TextDirection; use PhpOffice\PhpWord\Style\Paragraph; +use Throwable; /** * Common Html functions. @@ -114,20 +115,20 @@ public static function addHtml($element, $html, $fullHTML = false, $preserveWhit $dom->preserveWhiteSpace = $preserveWhiteSpace; try { - $result = $dom->loadHTML($html, LIBXML_NOWARNING | LIBXML_NOERROR); + $result = @$dom->loadHTML($html); $exceptionMessage = 'DOM loadHTML failed'; - } catch (Exception $e) { + } catch (Throwable $e) { $result = false; $exceptionMessage = $e->getMessage(); } if ($result === false) { - throw new Exception($exceptionMessage); // @codeCoverageIgnore + throw new Exception($exceptionMessage); } self::removeAnnoyingWhitespaceTextNodes($dom); static::$xpath = new DOMXPath($dom); $node = $dom->getElementsByTagName('html'); if (count($node) === 0 || $node->item(0) === null) { - $node = $dom->getElementsByTagName('body'); + $node = $dom->getElementsByTagName('body'); // @codeCoverageIgnore } static::parseNode($node->item(0), $element); @@ -1319,7 +1320,6 @@ protected static function mapListType($cssListType) return NumberFormat::LOWER_ROMAN; // i, ii, iii, iv, .. case 'I': return NumberFormat::UPPER_ROMAN; // I, II, III, IV, .. - case '1': default: return NumberFormat::DECIMAL; // 1, 2, 3, .. } diff --git a/src/PhpWord/Shared/HtmlColours.php b/src/PhpWord/Shared/HtmlColours.php index 40bc0096c6..c50acfd5da 100644 --- a/src/PhpWord/Shared/HtmlColours.php +++ b/src/PhpWord/Shared/HtmlColours.php @@ -524,11 +524,6 @@ class HtmlColours 'yellowgreen' => '9acd32', ]; - public static function colourNameLookup(string $colorName): string - { - return self::COLOUR_MAP[$colorName] ?? ''; - } - public static function convertColour(string $colorName): string { $colorName = trim($colorName); diff --git a/tests/PhpWordTests/Shared/Html2Test.php b/tests/PhpWordTests/Shared/Html2Test.php new file mode 100644 index 0000000000..95866d4b07 --- /dev/null +++ b/tests/PhpWordTests/Shared/Html2Test.php @@ -0,0 +1,140 @@ +expectException(Exception::class); + $this->expectExceptionMessage('loadHTML'); + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, ''); + } + + public function testCssOnIdElement(): void + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + $html = '' + . '
' + . 'test1.
' + . ''; + Html::addHtml($section, $html); + $doc = TestHelperDOCX::getDocument($phpWord); + $marginPath = '/w:document/w:body/w:p/w:pPr/w:spacing'; + self::assertSame('150', $doc->getElement($marginPath)->getAttribute('w:before')); + self::assertSame('150', $doc->getElement($marginPath)->getAttribute('w:after')); + $path = '/w:document/w:body/w:p/w:r'; + self::assertSame('test1.', $doc->getElement($path)->nodeValue); + $boldPath = $path . '/w:rPr/w:b'; + self::assertSame('1', $doc->getElement($boldPath)->getAttribute('w:val')); + } + + public function testListTypes(): void + { + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + $html = '