Skip to content

Fix RELAX NG validation of documents with XML declarations - #380

Merged
naitoh merged 4 commits into
ruby:masterfrom
FFspiky:fix/relaxng-xml-declaration-241
Nov 28, 2026
Merged

naitoh merged 4 commits into
ruby:masterfrom
FFspiky:fix/relaxng-xml-declaration-241

Conversation

@FFspiky

@FFspiky FFspiky commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Documents with XML declarations fail RELAX NG validation on :xmldecl. The multiline example in #241 also emits whitespace before and after the root, which the validator incorrectly treats as element content.

Handle the declaration and root-external XML whitespace in RelaxNG#receive. Skip comments, processing instructions, and DTD declarations so markup before, inside, or after the root does not advance the schema. Use the existing attribute stack to identify element nesting, initialize it for validation without an initial reset, and retain its existing reset behavior. Whitespace inside elements still reaches schema validation, including text and exact value patterns. The shared Validator is unchanged.

Fixes #241.

The regression tests keep the original schema and multiline document from #241. They check whitespace around the root with and without an XML declaration, first use without an initial reset, and reuse after an unfinished document. They also exercise invalid structure, whitespace inside elements, exact text values, comments, processing instructions, and DTDs with an internal subset.

Validation on macOS arm64 with Ruby 4.0.7 at b894913:

  • The focused RELAX NG suite passed all 32 tests (179 assertions). With the same tests, eight fail against the unpatched upstream revision 0fee9c5. Two still fail on 3ed87be, before the changes for comments, processing instructions, and DTDs.
  • bundle exec rake test and bundle exec rake test RUBYOPT=--enable-frozen-string-literal each passed all 850 tests (2,720 assertions, zero omissions).
  • git diff --check passed.

The preceding revision was also checked with Ruby 3.4.9, including testing the installed gem and building RDoc.

OpenAI Codex prepared the implementation, tests, description, and follow-up and ran the local checks.

@FFspiky

FFspiky commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

The Test workflow currently reports action_required with no jobs started. Could a maintainer approve it for this PR? The Ruby 3.4.9 local full, frozen-string and installed-gem test results are recorded above; the hosted Ruby/platform matrix is still unverified.

This follow-up was prepared with Codex.

Comment thread lib/rexml/validation/relaxng.rb Resolved

@naitoh naitoh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for fixing this issue!
The implementation in lib/rexml/validation/relaxng.rb looks good to me.

I left a few inline suggestions on test/test_validation_rng.rb to remove redundant test cases and simplify nested loops so the test suite stays concise and easy to maintain.

Comment thread test/test_validation_rng.rb Resolved
Comment thread test/test_validation_rng.rb Resolved
Comment thread test/test_validation_rng.rb Resolved
Comment thread test/test_validation_rng.rb Resolved
Comment thread test/test_validation_rng.rb Resolved

@naitoh naitoh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@naitoh
naitoh merged commit 0837d4f into ruby:master Nov 28, 2026
71 checks passed
@naitoh

naitoh commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Thanks!

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.

RELAX NG Validator fails to handle XML documents with XML declaration

3 participants