diff --git a/src/wp-includes/html-api/class-wp-html-processor.php b/src/wp-includes/html-api/class-wp-html-processor.php
index b34b2b9baf506..14cb296d433ae 100644
--- a/src/wp-includes/html-api/class-wp-html-processor.php
+++ b/src/wp-includes/html-api/class-wp-html-processor.php
@@ -1170,14 +1170,17 @@ protected function serialize_token(): string {
}
if ( null !== $doctype->public_identifier ) {
- $html .= " PUBLIC \"{$doctype->public_identifier}\"";
+ $quote = str_contains( $doctype->public_identifier, '"' ) ? "'" : '"';
+ $html .= " PUBLIC {$quote}{$doctype->public_identifier}{$quote}";
}
if ( null !== $doctype->system_identifier ) {
if ( null === $doctype->public_identifier ) {
$html .= ' SYSTEM';
}
- $html .= " \"{$doctype->system_identifier}\"";
+ $quote = str_contains( $doctype->system_identifier, '"' ) ? "'" : '"';
+ $html .= " {$quote}{$doctype->system_identifier}{$quote}";
}
+
$html .= '>';
break;
diff --git a/tests/phpunit/tests/html-api/wpHtmlProcessor-serialize.php b/tests/phpunit/tests/html-api/wpHtmlProcessor-serialize.php
index 021c798a9e8ec..e2b5a79c2de2f 100644
--- a/tests/phpunit/tests/html-api/wpHtmlProcessor-serialize.php
+++ b/tests/phpunit/tests/html-api/wpHtmlProcessor-serialize.php
@@ -307,14 +307,18 @@ public function test_full_document_serialize_includes_doctype( string $doctype_i
*/
public static function data_provider_serialize_doctype() {
return array(
- 'None' => array( '', '' ),
- 'Empty' => array( '', '' ),
- 'HTML5' => array( '', '' ),
- 'Strange name' => array( '', '' ),
- 'With public' => array( '', '' ),
- 'With system' => array( '', '' ),
- 'With public and system' => array( '', '' ),
- 'Weird casing' => array( '', '' ),
+ 'None' => array( '', '' ),
+ 'Empty' => array( '', '' ),
+ 'HTML5' => array( '', '' ),
+ 'Strange name' => array( '', '' ),
+ 'With public' => array( '', '' ),
+ 'With system' => array( '', '' ),
+ 'With public and system' => array( '', '' ),
+ 'Weird casing' => array( '', '' ),
+ 'Single quotes in public ID' => array( '', '' ),
+ 'Double quotes in public ID' => array( '', '' ),
+ 'Single quotes in system ID' => array( '', '' ),
+ 'Double quotes in system ID' => array( '', '' ),
);
}
}