diff --git a/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php b/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php index f22acc84..22c87cd6 100644 --- a/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php +++ b/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php @@ -79,7 +79,20 @@ public function renderRendersDocumentProvidedToFromDomDocument() /** * @test */ - public function reformatsHtml() + public function renderPreservesBodyContentProvidedToFromHtml() + { + $innerHtml = '
Hello world!
'; + $subject = TestingHtmlProcessor::fromHtml('' . $innerHtml . ''); + + $html = $subject->render(); + + self::assertContains($innerHtml, $html); + } + + /** + * @test + */ + public function renderPreservesOuterHtmlProvidedToFromHtml() { $rawHtml = '' . '' . @@ -93,8 +106,9 @@ public function reformatsHtml() "\n"; $subject = TestingHtmlProcessor::fromHtml($rawHtml); + $html = $subject->render(); - self::assertSame($formattedHtml, $subject->render()); + self::assertEqualsHtml($formattedHtml, $html); } /** @@ -706,7 +720,7 @@ public function getDomDocumentWithNormalizedHtmlRepresentsTheGivenHtml() $domDocument = $subject->getDomDocument(); - self::assertSame($html, $domDocument->saveHTML()); + self::assertEqualsHtml($html, $domDocument->saveHTML()); } /** @@ -732,4 +746,42 @@ public function getDomDocumentVoidElementNotHasChildNodes(string $htmlWithNonXml self::assertFalse($element->hasChildNodes()); } } + + /** + * Asserts that two HTML strings are equal, allowing for whitespace differences in the HTML element itself (but not + * its descendants) and after its closing tag. + * + * @param string $expected + * @param string $actual + * @param string $message + */ + private static function assertEqualsHtml(string $expected, string $actual, string $message = '') + { + $normalizedExpected = self::normalizeHtml($expected); + $normalizedActual = self::normalizeHtml($actual); + + self::assertSame($normalizedExpected, $normalizedActual, $message); + } + + /** + * Normalizes whitespace in the HTML element itself (but not its descendants) and after its closing tag, with a + * single newline inserted or replacing whitespace at positions where whitespace may occur but is superfluous. + * + * @param string $html + * + * @return string + */ + private static function normalizeHtml(string $html) + { + return \preg_replace( + [ + '%(])[^>]*+>)\\s*+(])%', + '%()\\s*+(])%', + '%()\\s*+()%', + '%(