Skip to content

HTMLParser truncates DOCTYPE declarations at '>' inside quoted system identifiers #158720

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: Python Library Reference, html.parser, HTMLParser.handle_decl: "The decl parameter will be the entire contents of the declaration inside the <! ... > markup (e.g. 'DOCTYPE html')."

Expected: handle_decl called exactly once with 'DOCTYPE html SYSTEM "a>b"'.

Actual: handle_decl called with the truncated string 'DOCTYPE html SYSTEM "a'.

from html.parser import HTMLParser
from xml.parsers.expat import ParserCreate

s = '<!DOCTYPE html SYSTEM "' + 'a>b' + '">'
actual, declarations = [], []

try:
    validator = ParserCreate()
    validator.StartDoctypeDeclHandler = lambda *args: declarations.append(args)
    validator.Parse(s + '<html/>', True)
    if declarations != [('html', 'a>b', None, 0)]:
        raise ValueError('system identifier was not preserved')
except Exception as e:
    print('REFUTATION REJECTED:', 'invalid declaration:', str(e))
else:
    parser = HTMLParser()
    parser.handle_decl = actual.append
    parser.feed(s)
    parser.close()
    expected = [s[2:-1]]
    if actual != expected:
        print('REFUTATION CONFIRMED:', repr(s), 'actual:', actual, 'expected:', expected)
    else:
        print('REFUTATION REJECTED:', 'actual matches documented expectation')

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library html.parser:

REFUTATION CONFIRMED: '<!DOCTYPE html SYSTEM "a>b">' actual: ['DOCTYPE html SYSTEM "a'] expected: ['DOCTYPE html SYSTEM "a>b"']

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/html.parser-20261003-091556-c3

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions