diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c56f15e..adb1c69e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). - Drop support for PHP 7.1 (#967) ### Fixed +- Allow line feeds within `` tag (#987) ## 5.0.1 diff --git a/src/HtmlProcessor/AbstractHtmlProcessor.php b/src/HtmlProcessor/AbstractHtmlProcessor.php index 645160da..f29b9f3e 100644 --- a/src/HtmlProcessor/AbstractHtmlProcessor.php +++ b/src/HtmlProcessor/AbstractHtmlProcessor.php @@ -310,7 +310,7 @@ private function addContentTypeMetaTag(string $html): string ); } elseif ($hasHtmlTag) { $reworkedHtml = \preg_replace( - '//i', + '//is', '' . self::CONTENT_TYPE_META_TAG . '', $html ); diff --git a/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php b/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php index 8c13d495..c3878e0e 100644 --- a/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php +++ b/tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php @@ -420,6 +420,75 @@ public function addsMissingHtmlTag(string $html): void self::assertStringContainsString('', $result); } + /** + * @return array> + */ + public function provideContentWithHtmlTag(): array + { + return [ + 'HTML only' => [''], + 'HTML start tag only' => [''], + 'doctype and HTML only' => [''], + 'HTML and body content only' => ['

Hello

'], + 'HTML and HEAD element' => [''], + 'HTML and BODY element' => [''], + 'HTML, HEAD AND BODY element' => [''], + ]; + } + + /** + * @return array> The second element of each dataset is optional, and is the expected + * normalization of the `` tag, if different. + */ + public function provideHtmlTagWithAttributes(): array + { + return [ + 'with one attribute' => [''], + 'with two attributes' => [''], + 'with line feeds within tag' => ["", ''], + 'with Windows line endings within tag' => ["", ''], + 'with TABs within tag' => ["", ''], + ]; + } + + /** + * @test + * + * @param string $html + * + * @dataProvider provideContentWithHtmlTag + * @dataProvider provideHtmlTagWithAttributes + */ + public function notAddsSecondHtmlTag(string $html): void + { + $subject = TestingHtmlProcessor::fromHtml($html); + + $result = $subject->render(); + + $htmlTagCount = \preg_match_all('%]%', $result); + self::assertSame(1, $htmlTagCount); + } + + /** + * @test + * + * @param string $html + * @param ?string $normalizedHtmlTag + * + * @dataProvider provideHtmlTagWithAttributes + */ + public function preservesHtmlTagAttributes(string $html, ?string $normalizedHtmlTag = null): void + { + if ($normalizedHtmlTag === null) { + $normalizedHtmlTag = $html; + } + $subject = TestingHtmlProcessor::fromHtml($html); + + $result = $subject->render(); + + self::assertStringContainsString($normalizedHtmlTag, $result); + } + /** * @return string[][] */ @@ -457,6 +526,7 @@ public function provideContentWithoutHeadTag(): array * @param string $html * * @dataProvider provideContentWithoutHeadTag + * @dataProvider provideHtmlTagWithAttributes */ public function addsMissingHeadTagExactlyOnce(string $html): void { @@ -464,7 +534,7 @@ public function addsMissingHeadTagExactlyOnce(string $html): void $result = $subject->render(); - $headTagCount = \substr_count($result, ''); + $headTagCount = \preg_match_all('%]%', $result); self::assertSame(1, $headTagCount); }