Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
([#776](https://github.com/MyIntervals/emogrifier/pull/776))

### Changed
- Normalize DOCTYPE declaration according to polyglot markup recommendation
([#866](https://github.com/MyIntervals/emogrifier/pull/866))
- Upgrade to V2 of the PHP setup GitHub action
([#861](https://github.com/MyIntervals/emogrifier/pull/861))
- Move the development tools to Phive
Expand Down
22 changes: 20 additions & 2 deletions src/HtmlProcessor/AbstractHtmlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private function prepareHtmlForDomConversion(string $html): string
}

/**
* Makes sure that the passed HTML has a document type.
* Makes sure that the passed HTML has a document type, with lowercase "html".
*
* @param string $html
*
Expand All @@ -244,12 +244,30 @@ private function ensureDocumentType(string $html): string
{
$hasDocumentType = \stripos($html, '<!DOCTYPE') !== false;
if ($hasDocumentType) {
return $html;
return $this->normalizeDocumentType($html);
}

return static::DEFAULT_DOCUMENT_TYPE . $html;
}

/**
* Makes sure the document type in the passed HTML has lowercase "html".
*
* @param string $html
*
* @return string HTML with normalized document type
*/
private function normalizeDocumentType(string $html): string
{
// Limit to replacing the first occurrence: as an optimization; and in case an example exists as unescaped text.
return \preg_replace(
'/<!DOCTYPE\\s++html(?=[\\s>])/i',
'<!DOCTYPE html',
$html,
1
);
}

/**
* Adds a Content-Type meta tag for the charset.
*
Expand Down
45 changes: 44 additions & 1 deletion tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function renderPreservesOuterHtmlProvidedToFromHtml()
'<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>' .
'<body></body>' .
'</html>';
$formattedHtml = "<!DOCTYPE HTML>\n" .
$formattedHtml = "<!DOCTYPE html>\n" .
"<html>\n" .
'<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>' . "\n" .
"<body></body>\n" .
Expand Down Expand Up @@ -377,6 +377,49 @@ public function keepsExistingDocumentType(string $documentType)
self::assertContains($documentType, $result);
}

/**
* @return string[][]
*/
public function normalizedDocumentTypeDataProvider(): array
{
return [
'HTML5, uppercase' => ['<!DOCTYPE HTML>', '<!DOCTYPE html>'],
'HTML5, lowercase' => ['<!doctype html>', '<!DOCTYPE html>'],
'HTML5, mixed case' => ['<!DocType Html>', '<!DOCTYPE html>'],
'HTML5, extra whitespace' => ['<!DOCTYPE html >', '<!DOCTYPE html>'],
'HTML 4 transitional, uppercase' => [
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" '
. '"http://www.w3.org/TR/REC-html40/loose.dtd">',
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" '
. '"http://www.w3.org/TR/REC-html40/loose.dtd">',
],
'HTML 4 transitional, lowercase' => [
'<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" '
. '"http://www.w3.org/TR/REC-html40/loose.dtd">',
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" '
. '"http://www.w3.org/TR/REC-html40/loose.dtd">',
],
];
}

/**
* @test
*
* @param string $documentType
* @param string $normalizedDocumentType
*
* @dataProvider normalizedDocumentTypeDataProvider
*/
public function normalizesDocumentType(string $documentType, string $normalizedDocumentType)
{
$html = $documentType . '<html></html>';
$subject = TestingHtmlProcessor::fromHtml($html);

$result = $subject->render();

self::assertContains($normalizedDocumentType, $result);
}

/**
* @test
*
Expand Down