Skip to content

Commit

Permalink
HTML API: Fix normalized doctype pub/sys identifier quotes.
Browse files Browse the repository at this point in the history
Changeset [59399] fixed missing DOCTYPEs in normalized HTML output. It missed an edge case where public and system identifiers may contain double quotes, in which case they must be quoted with single quotes.

This commit addresses that issue and adds tests.

Follow-up to [59399].

Props jonsurrell, luisherranz, apermo.
Fixes #62396.


git-svn-id: https://develop.svn.wordpress.org/trunk@59410 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
cbravobernal committed Nov 18, 2024
1 parent 1664ea5 commit 91eb251
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1191,14 +1191,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;

Expand Down
20 changes: 12 additions & 8 deletions tests/phpunit/tests/html-api/wpHtmlProcessor-serialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( '<!DOCTYPE>', '<!DOCTYPE>' ),
'HTML5' => array( '<!DOCTYPE html>', '<!DOCTYPE html>' ),
'Strange name' => array( '<!DOCTYPE WordPress>', '<!DOCTYPE wordpress>' ),
'With public' => array( '<!DOCTYPE html PUBLIC "x">', '<!DOCTYPE html PUBLIC "x">' ),
'With system' => array( '<!DOCTYPE html SYSTEM "y">', '<!DOCTYPE html SYSTEM "y">' ),
'With public and system' => array( '<!DOCTYPE html PUBLIC "x" "y">', '<!DOCTYPE html PUBLIC "x" "y">' ),
'Weird casing' => array( '<!docType HtmL pubLIc\'xxx\'"yyy" all this is ignored>', '<!DOCTYPE html PUBLIC "xxx" "yyy">' ),
'None' => array( '', '' ),
'Empty' => array( '<!DOCTYPE>', '<!DOCTYPE>' ),
'HTML5' => array( '<!DOCTYPE html>', '<!DOCTYPE html>' ),
'Strange name' => array( '<!DOCTYPE WordPress>', '<!DOCTYPE wordpress>' ),
'With public' => array( '<!DOCTYPE html PUBLIC "x">', '<!DOCTYPE html PUBLIC "x">' ),
'With system' => array( '<!DOCTYPE html SYSTEM "y">', '<!DOCTYPE html SYSTEM "y">' ),
'With public and system' => array( '<!DOCTYPE html PUBLIC "x" "y">', '<!DOCTYPE html PUBLIC "x" "y">' ),
'Weird casing' => array( '<!docType HtmL pubLIc\'xxx\'"yyy" all this is ignored>', '<!DOCTYPE html PUBLIC "xxx" "yyy">' ),
'Single quotes in public ID' => array( '<!DOCTYPE html PUBLIC "\'quoted\'">', '<!DOCTYPE html PUBLIC "\'quoted\'">' ),
'Double quotes in public ID' => array( '<!DOCTYPE html PUBLIC \'"quoted"\'\>', '<!DOCTYPE html PUBLIC \'"quoted"\'>' ),
'Single quotes in system ID' => array( '<!DOCTYPE html SYSTEM "\'quoted\'">', '<!DOCTYPE html SYSTEM "\'quoted\'">' ),
'Double quotes in system ID' => array( '<!DOCTYPE html SYSTEM \'"quoted"\'\>', '<!DOCTYPE html SYSTEM \'"quoted"\'>' ),
);
}
}

0 comments on commit 91eb251

Please sign in to comment.