Skip to content

Inconsistent handling of U+0000 NULLs in input stream #2395

Closed

Description

@magictractor

A solitary U+0000 NULL within a <p> tag is removed when parsing. However, that only happens if there is no other text; with other text, the U+0000 is preserved.

This code illustrates the issue, using v1.21.2:

public static void main(String[] args) {
    Document doc = Parser.parse("<p>\u0000</p><p>\u0000\u0000</p><p>Hi\u0000</p><p>\u0000<b>\u0000</b></p>", "");
    System.out.println(doc.child(0).child(1));
}

which has output:

<body>
 <p></p>
 <p>&#x0;&#x0;</p>
 <p>Hi&#x0;</p>
 <p><b></b></p>
</body>

Removing the tags is (I believe) correct, section 13.2.6.4.7 The "in body" insertion mode of the spec says that U+0000 NULL should be ignored.

TokeniserState.Date has a switch statement with a case for U+0000 which adds an error, but the text is appended to the token, and HtmlTreeBuilderState.InBody (line 289), checks whether the token equals a singleton U+0000.

See also
https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody

Activity

  1. self-assigned this
    on Sep 8, 2025
  2. added this to the 1.22.1 milestone on Sep 8, 2025
  3. added
    bugA confirmed bug, that we should fix
    fixedAn {bug|improvement} that has been {fixed|implemented}
    on Sep 8, 2025
  4. jhy commented on Sep 8, 2025

    @jhy
    Owner

    Thanks for raising that!

    Fixed by dedf6ab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugA confirmed bug, that we should fixfixedAn {bug|improvement} that has been {fixed|implemented}

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions