Skip to content

fix: support DOCTYPE internal subsets - #1059

Open
tlq5l wants to merge 1 commit into
prettier:mainfrom
tlq5l:fix/doctype-internal-subset
Open

tlq5l wants to merge 1 commit into
prettier:mainfrom
tlq5l:fix/doctype-internal-subset

Conversation

@tlq5l

@tlq5l tlq5l commented Oct 21, 2026

Copy link
Copy Markdown

Fixes #996.

@xml-tools/parser has no grammar rule for the internal subset of a DOCTYPE (<!DOCTYPE x [ <!ENTITY ...> ]>), so the lexer throws on the [.

parser.js now finds the [ ... ] range before parsing (skipping quoted strings and comments, so a ] inside an entity value or comment doesn't end it early). It blanks that range with spaces, which keeps every offset and line number the same, and parses the rest as before. The raw text goes on the docTypeDecl node as internalSubset, and the printer writes it back unchanged between [ and ]. The DTD content is not reformatted.

Added a parser test, a format test (including ] inside a quoted value and a comment, and a second pass to check the output is stable), and a CHANGELOG line.

Ran live: printf '<!DOCTYPE x [<!ENTITY nbsp "&#160;">]>\n<a>hi</a>\n' > t.xml && npx prettier --plugin ./src/plugin.js --parser xml t.xml -> prints the file back unchanged (was SyntaxError: unexpected character: ->[<-).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Formatter fails on XML file with entity definitions

1 participant