Skip to content

UTF-8 corruption in \Dom\HTMLDocumentΒ #17481

Closed

Description

@xPaw

Description

The following code:

<?php
$Repeated = str_repeat( '–', 4096 );
//$Repeated = str_repeat( '😏', 4096 );
$Data = '<!DOCTYPE HTML><html>' . $Repeated . '</html>';
$Document = \Dom\HTMLDocument::createFromString( $Data, 0, 'UTF-8' );

echo $Document->saveHTML();
// var_dump($Document->body->textContent);

The resulting string contains random invalid UTF-8 sequences like with the οΏ½ character. With the repeated emoji, emojis become corrupted. If you repeat the string for longer, there are more corrupted bytes in random places.

I initially spotted this bug when parsing a real HTML document and used textContent (innerHTML produces the same issue) on an element I found with xpath.

image

PHP Version

PHP 8.4.2

Operating System

Windows 11 and Debian 12

Activity

  1. self-assigned this
    on Jan 15, 2025
  2. xPaw commented on Jan 15, 2025

    @xPaw
    ContributorAuthor

    FYI this is reproducible with 2-byte utf-8 sequences too, but not ascii.

  3. ndossche commented on Jan 15, 2025

    @ndossche
    Member

    I'll try to have a look at the encoding code tomorrow.

  4. added a commit that references this issue on Jan 16, 2025
    623cdb9
  5. added 2 commits that reference this issue on Jan 17, 2025
    72708f2
    2952e16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions