Repository navigation
parser: add dtd_attributes option mirroring XML_PARSE_DTDATTR - #83
Merged
Merged
Conversation
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
force-pushed
the
feat/dtd-attr-option
branch
from
November 20, 2026 23:06
8a3cfb5 to
9bf8b66
Compare
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.
Summary
Since a76f96a (PR #18) the parser inserts every DTD
ATTLISTdefault into the tree. libxml2 only does that withXML_PARSE_DTDATTR. This PR keeps the current default and adds an opt-out.ParseOptions::dtd_attributes(bool)(new pub fielddtd_attributes, defaults totrue). Withfalse, onlyxmlns/xmlns:*defaults are inserted, because namespace processing needs them. That matches libxml2 withoutXML_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_strruns a BOM and encoding-declaration check thatparser::parse_str_with_optionsskips. The new method does the same check with custom options, andparse_strnow calls it.--dtdattrflag, or with--c14n/--exc-c14n, which turn on DTDATTR in libxml2 too. I checked this against system xmllint 2.9.13 ontest/defattr2.xml: the output is identical for no flag,--dtdattr,--c14nand--exc-c14n, apart from a trailing newline that was already different.--validdoesn't insert defaults in libxml2, so it doesn't here either.PushParserand async parsing go throughparse_str_with_options, so they honor the option. The SAX parser andXmlReaderhave 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.dtd_attributes(false), the way libxml2's runtest does.attr3
The duplicate
ATTLISTwas already handled correctly: the DTD parser keeps the first declaration (a1 "v1") and dropsz1. attr3 failed for the same reason as defattr2: the defaulta1="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
#FIXED,xmlns, prefixedxmlns:p, prefixedp: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().