Skip to content
Draft
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
9 changes: 9 additions & 0 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4161,6 +4161,15 @@ static function ( $tag_match ) {

case 'TEXTAREA':
case 'TITLE':
$plaintext_content = strtr(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment is probably warranted here to say that these replacements are not required for a spec-complaint HTML parser. But they are done for the sake of other parsers, including DOMDocument.

I'm surprised to see that DOMDocument parses elements as children of TITLE! Happily this is fixed in Dom\HTMLDocument: https://3v4l.org/oDUI3#veol

Test code
<?php
$html = '
  <!DOCTYPE html>
  <html>
   <head>
    <meta charset="utf-8">  
    <title>We <em>love</em> HTML!</title>
   </head>
   <body>
   </body>
  </html>
';

function run_tests( $document ) {
    $title = $document->getElementsByTagName( 'title' )->item( 0 );
    if ( $title->childNodes->length !== 1 ) {
        echo "FAIL: Title tag does not have the the expected single text child. Has {$title->childNodes->length} children.\n";
    } else {
        echo "PASS: Title tag only has a single child.\n";
    }
    if ( $title->getElementsByTagName( 'em' )->length !== 0 ) {
      echo "FAIL: Title tag unexpectedly has a parsed EM child.\n";
    } else {
      echo "PASS: Title tag has no child EM element.\n";  
    }
    if ( str_contains( $title->textContent, '<em>' ) ) {
        echo "PASS: HTML tag <em> was not parsed. Text content: {$title->textContent}\n";
    } else {
        echo "FAIL: HTML tag <em> was parsed. Text content: {$title->textContent}\n";
    }
}

echo "# DOMDocument:\n";
$old_document = new DOMDocument();
$old_document->loadHTML( $html );
run_tests( $old_document );

echo "\n";
echo "# Dom\HTMLDocument:\n";
if ( class_exists( Dom\HTMLDocument::class ) ) {
    $new_document = Dom\HTMLDocument::createFromString( $html );
    run_tests( $new_document );
} else {
    echo "(Not available)\n";
}

$plaintext_content,
array(
'<' => '&lt;',
'&' => '&amp;',
'>' => '&gt;',
)
);
Comment on lines +4164 to +4171

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks right: https://3v4l.org/0Kf28#veol

<title>
&lt;&amp;&gt;: &amp;lt;&amp;amp;&amp;gt;
</title>

Parsed as:

<&>: &lt;&amp;&gt;


$plaintext_content = preg_replace_callback(
"~</(?P<TAG_NAME>{$this->get_tag()})~i",
static function ( $tag_match ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,10 @@ public function test_updates_basic_modifiable_text_on_supported_nodes( string $h
'Should have modified the text at the target node.'
);

$this->assertSame(
$this->assertEqualHTML(
$transformed,
$processor->get_updated_html(),
'<body>',
"Should have transformed the HTML as expected when modifying the target node's modifiable text."
);
}
Expand Down
Loading