Repository navigation
serial: write normalized whitespace for raw attribute values (fixes #50) - #78
Merged
Merged
Conversation
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 	/ / . 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&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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #50.
Root cause
When an attribute value contains an entity reference, the parser keeps its raw source text in
Attribute::raw_valueso that custom entity references can be written back out.write_escaped_attr_preserve_refsinsrc/serial/xml.rsserialized 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:Values with no
&take thewrite_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
\t/\n/\ris written as the single space the parser produced, and a\r\npair is also written as one space. Whitespace written as a character reference (	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:
parse_attribute_value, a literal\r\nbecame two spaces. Line-end normalization (§2.11) comes before §3.3.3, so it should be one space. libxml2 gives one space.#DEFAULT/#FIXEDvalues were copied into the tree straight from the ATTLIST literal, with references unexpanded and whitespace not normalized. For example,<!ATTLIST a b CDATA 'p&q'>gaveb="p&q"instead ofp&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#FIXEDvalues in normalized form, so defaulted and explicit attributes validate the same way.Checked against system
xmllint(libxml2). On<!ATTLIST a b CDATA "p&q\tr\r\ns	t&e;">/<a c="x&y\r\nz\tw	&e;"/>,xmllint --c14ngivesb="p&q r s	tE" c="x&y z w	E", and xmloxide now gives the same values.C14N (
src/serial/c14n.rs) serializes from the normalizedattr.valueand already escapes#x9/#xA/#xDas	/
/
(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
serial/xml.rs,parser/input.rs,parser/xml.rsandvalidation/dtd.rs.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.attr_whitespace_survives_roundtripcovering 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.defattr2,attr3) also fail onmainagainst the current upstream test data, so they are not caused by this change.cargo +1.81 check --all-targets --features cli,ffi,serde,asyncpasses.--all-featurescannot build on 1.81 becausebench-libxml2pulls inlibxml 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	y">,a="&t;"givesx\ty, but §3.3.3 (and libxml2) givex 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 ofexpand_entity_textand is worth its own issue.