Skip to content

serial: write normalized whitespace for raw attribute values (fixes #50) - #78

Merged
jonwiggins merged 1 commit into
mainfrom
fix/attr-whitespace-roundtrip
Nov 20, 2026
Merged

jonwiggins merged 1 commit into
mainfrom
fix/attr-whitespace-roundtrip

Conversation

@jonwiggins

Copy link
Copy Markdown
Owner

Fixes #50.

Root cause

When an attribute value contains an entity reference, the parser keeps its raw source text in Attribute::raw_value so that custom entity references can be written back out. write_escaped_attr_preserve_refs in src/serial/xml.rs serialized that raw text, and wrote any literal tab/LF/CR in it as 	/
/
. The parser had already normalized those literal characters to spaces (XML 1.0 §3.3.3). Character references are exempt from normalization, so the next parse turned the space back into a tab/LF/CR:

<a b='x&amp;y\tz'/>  ->  value "x&y z"  ->  <a b="x&amp;y&#9;z"/>  ->  value "x&y\tz"

Values with no & take the write_escaped_attr(attr.value) path, which was already correct. That is why only values that mix an entity reference with whitespace showed the bug.

Fix

  • serializer: in the raw-value path, a literal \t/\n/\r is written as the single space the parser produced, and a \r\n pair is also written as one space. Whitespace written as a character reference (&#9; etc.) is still written as a character reference, so it round-trips as the literal character.

Two related bugs turned up while writing the round-trip tests:

  • parser, CRLF: in the bulk-scan path of parse_attribute_value, a literal \r\n became two spaces. Line-end normalization (§2.11) comes before §3.3.3, so it should be one space. libxml2 gives one space.
  • parser, DTD defaults: #DEFAULT/#FIXED values were copied into the tree straight from the ATTLIST literal, with references unexpanded and whitespace not normalized. For example, <!ATTLIST a b CDATA 'p&amp;q'> gave b="p&amp;q" instead of p&q. They are now normalized once when the DTD is wired into the parser (ParserInput::normalize_attr_literal, which shares the entity-expansion counter). Non-CDATA types get the extra whitespace collapsing. DTD validation now compares #FIXED values in normalized form, so defaulted and explicit attributes validate the same way.

Checked against system xmllint (libxml2). On <!ATTLIST a b CDATA "p&amp;q\tr\r\ns&#9;t&e;"> / <a c="x&amp;y\r\nz\tw&#9;&e;"/>, xmllint --c14n gives b="p&amp;q r s&#x9;tE" c="x&amp;y z w&#x9;E", and xmloxide now gives the same values.

C14N (src/serial/c14n.rs) serializes from the normalized attr.value and already escapes #x9/#xA/#xD as &#x9;/&#xA;/&#xD; (C14N §2.2), so it was not affected. A test now covers it. The HTML serializers are not affected: the HTML parsers do not apply XML attribute-value normalization, so literal whitespace round-trips as-is.

Tests

  • Unit tests in serial/xml.rs, parser/input.rs, parser/xml.rs and validation/dtd.rs.
  • New tests/attribute_normalization.rs: parse → serialize → parse round-trips for tab, LF, CR, CRLF, char refs, builtin and custom entity refs, and DTD defaults (CDATA, #FIXED, NMTOKENS). Each one checks that the attribute values, the serialized form and the C14N output stay the same.
  • New proptest attr_whitespace_survives_roundtrip covering mixed whitespace and references, for both explicit attributes and DTD defaults.

Results:

  • cargo fmt --all: clean.
  • cargo clippy --all-targets --all-features -- -D warnings (stable and 1.100.0): clean.
  • cargo test --all-features: all pass.
  • W3C conformance: 1727/1727.
  • libxml2 compat: 117/119. The same two tests (defattr2, attr3) also fail on main against the current upstream test data, so they are not caused by this change.
  • MSRV: cargo +1.81 check --all-targets --features cli,ffi,serde,async passes. --all-features cannot build on 1.81 because bench-libxml2 pulls in libxml 0.3.8, which needs edition 2024. That was already the case before this change.

Not addressed here (pre-existing, separate)

Literal whitespace that comes from an entity's replacement text is not normalized when the entity is referenced inside an attribute value. For <!ENTITY t "x&#9;y">, a="&t;" gives x\ty, but §3.3.3 (and libxml2) give x y. This does not affect round-trip stability, because the reference is preserved and re-expanded the same way. It needs an attribute-mode variant of expand_entity_text and is worth its own issue.

Attribute values that contained an entity reference were serialized from
their raw source text, and literal tab/LF/CR in that text were written as
&#9;/&#10;/&#13;. The parser had already normalized those literal
characters to spaces (XML 1.0 §3.3.3), and character references are exempt
from normalization, so a parse -> serialize -> parse cycle turned a space
back into a tab/LF/CR. The raw-value serializer now writes the space the
parser produced; character-referenced whitespace is still written as a
character reference.

Two related attribute-normalization bugs found while testing round-trips:

- parser: a literal CRLF in an attribute value became two spaces instead
  of one (line-end normalization, §2.11, must precede §3.3.3). This
  matches libxml2.
- parser: DTD default (#DEFAULT / #FIXED) attribute values were inserted
  verbatim from the ATTLIST literal, with references unexpanded and
  whitespace unnormalized (e.g. "a&amp;b" instead of "a&b"). They are now
  normalized once when the DTD is wired in, including the extra collapsing
  for non-CDATA types. DTD validation compares #FIXED values in normalized
  form so defaulted and explicit attributes validate consistently.

Tests: serializer/parser/DTD unit tests, tests/attribute_normalization.rs
round-trip suite (tab, LF, CR, CRLF, char refs, entity refs, DTD
defaults, C14N), and a proptest round-trip property over mixed whitespace
and references.
@jonwiggins
jonwiggins merged commit fddefeb into main Nov 20, 2026
6 checks passed
@jonwiggins
jonwiggins deleted the fix/attr-whitespace-roundtrip branch November 20, 2026 21:49
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.

Attribute round-trip restores a tab that parsing had normalized to a space

1 participant