Skip to content

parser: add dtd_attributes option mirroring XML_PARSE_DTDATTR - #83

Merged
jonwiggins merged 1 commit into
mainfrom
feat/dtd-attr-option
Nov 20, 2026
Merged

jonwiggins merged 1 commit into
mainfrom
feat/dtd-attr-option

Conversation

@jonwiggins

Copy link
Copy Markdown
Owner

Summary

Since a76f96a (PR #18) the parser inserts every DTD ATTLIST default into the tree. libxml2 only does that with XML_PARSE_DTDATTR. This PR keeps the current default and adds an opt-out.

  • ParseOptions::dtd_attributes(bool) (new pub field dtd_attributes, defaults to true). With false, only xmlns / xmlns:* defaults are inserted, because namespace processing needs them. That matches libxml2 without XML_PARSE_DTDATTR. Defaulted values are still normalized and entity-expanded (serial: write normalized whitespace for raw attribute values (fixes #50) #78). The amplification accounting still counts defaults that aren't inserted.
  • Document::parse_str_with_options: Document::parse_str runs a BOM and encoding-declaration check that parser::parse_str_with_options skips. The new method does the same check with custom options, and parse_str now calls it.
  • xmllint now parses like libxml2's xmllint. DTD defaults (other than xmlns) are inserted only with the new --dtdattr flag, or with --c14n / --exc-c14n, which turn on DTDATTR in libxml2 too. I checked this against system xmllint 2.9.13 on test/defattr2.xml: the output is identical for no flag, --dtdattr, --c14n and --exc-c14n, apart from a trailing newline that was already different. --valid doesn't insert defaults in libxml2, so it doesn't here either.
  • Coverage of the option: PushParser and async parsing go through parse_str_with_options, so they honor the option. The SAX parser and XmlReader have never applied ATTLIST defaults, not even xmlns ones. That is unchanged here and stated in the doc comment. The FFI has no parse-options API, so it keeps the default (true), which is unchanged behavior.
  • Compat harness: parses with dtd_attributes(false), the way libxml2's runtest does.

attr3

The duplicate ATTLIST was already handled correctly: the DTD parser keeps the first declaration (a1 "v1") and drops z1. attr3 failed for the same reason as defattr2: the default a1="v1" was inserted, so we printed <doc a1="v1"/> while libxml2's result has <doc/>. The harness doesn't check diagnostics. We also never emit libxml2's validity warning "Attribute a1 of element doc: already defined", because the DTD parser can't report warnings yet. That could be a follow-up.

Results

  • libxml2 compat: 119/119 (was 117/119)
  • W3C XML conformance: 1727/1727
  • C14N conformance: passing
  • New unit tests cover both option values for plain defaults, #FIXED, xmlns, prefixed xmlns:p, prefixed p:att, and an attribute already present in the source. They also check that C14N of a default-parsed document includes the defaults. There's also a push-parser test.

Note

Adding a pub field to ParseOptions, which isn't #[non_exhaustive], breaks code that builds it with a struct literal and no ..Default::default().

DTD attribute defaults stay inserted by default. ParseOptions::dtd_attributes(false)
inserts only xmlns/xmlns:* defaults, matching libxml2 without XML_PARSE_DTDATTR.
Adds Document::parse_str_with_options so the BOM/encoding preflight of
Document::parse_str is available with custom options.

xmllint now parses like libxml2's xmllint: DTD defaults are inserted only
with --dtdattr, --c14n or --exc-c14n.

The libxml2 compat harness parses the way libxml2's runtest does (no DTDATTR),
which fixes defattr2 and attr3: 119/119.
@jonwiggins
jonwiggins merged commit 1aa9673 into main Nov 20, 2026
6 checks passed
@jonwiggins
jonwiggins deleted the feat/dtd-attr-option branch November 20, 2026 23:08
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.

1 participant