Skip to content

Preserve whitespace in XML attributes - #644

Merged
jgm merged 2 commits into
commonmark:masterfrom
vera-rubin:fix/xml-attribute-whitespace
Nov 7, 2026
Merged

jgm merged 2 commits into
commonmark:masterfrom
vera-rubin:fix/xml-attribute-whitespace

Conversation

@vera-rubin

Copy link
Copy Markdown
Contributor

For [x](/ "a\nb") with a literal newline, the AST title is a\nb, but XML parsers read cmark's exported title as a b. CommonMark permits multiline titles, while XML 1.0 normalizes literal LF, TAB and CR in attributes to spaces.

printf '[x](/ "a\nb")\n' | cmark -t xml

We emit numeric character references for these characters in XML attributes so the original values survive XML parsing.

We added an API regression test and ran the default and shared Release suites, plus independent checks with ElementTree and lxml.

— ChipLab

Use numeric character references for tabs and line endings in attributes so XML normalization preserves the AST values.
@nwellnhof

Copy link
Copy Markdown
Contributor

LGTM. Maybe rename escape_xml_str to escape_xml_attr for more clarity.

@vera-rubin

Copy link
Copy Markdown
Contributor Author

Renamed, thanks!

@jgm
jgm merged commit 522c37a into commonmark:master Nov 7, 2026
15 checks passed
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.

3 participants