From b1e6e6fd26ac72aafa0be010c8f995c8ae01760a Mon Sep 17 00:00:00 2001 From: FFspiky Date: Mon, 16 Nov 2026 09:08:08 +0800 Subject: [PATCH 1/3] Fix RELAX NG handling of XML declaration events --- lib/rexml/validation/relaxng.rb | 1 + test/test_validation_rng.rb | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/rexml/validation/relaxng.rb b/lib/rexml/validation/relaxng.rb index c6894dcb..fdfc5698 100644 --- a/lib/rexml/validation/relaxng.rb +++ b/lib/rexml/validation/relaxng.rb @@ -120,6 +120,7 @@ def initialize source end def receive event + return if event[0] == :xmldecl validate( event ) end end diff --git a/test/test_validation_rng.rb b/test/test_validation_rng.rb index 4872396b..d4c0394d 100644 --- a/test/test_validation_rng.rb +++ b/test/test_validation_rng.rb @@ -1,5 +1,6 @@ # frozen_string_literal: false +require "stringio" require "rexml/validation/relaxng" module REXMLTests @@ -32,6 +33,46 @@ def test_validate end + def test_document_with_declaration + rng = <<-XML + + + + + XML + validator = REXML::Validation::RelaxNG.new( rng ) + + [ + '', + '', + '', + '', + '', + ].each do |declaration| + source = "#{declaration}" + no_error( validator, source ) + no_error( validator, StringIO.new(source) ) + end + end + + def test_document_with_declaration_and_invalid_structure + rng = <<-XML + + + + XML + validator = REXML::Validation::RelaxNG.new( rng ) + declaration = '' + + error( validator, declaration + '' ) + error( validator, declaration + '' ) + error( validator, declaration + '' ) + no_error( validator, declaration + '' ) + error( validator, declaration + '' ) + no_error( validator, '' ) + end + + def test_sequence rng = <<-XML From 3ed87be67b80e4f906a760f7c8a278f1ac52a533 Mon Sep 17 00:00:00 2001 From: FFspiky Date: Mon, 16 Nov 2026 09:16:23 +0800 Subject: [PATCH 2/3] Handle root-external XML whitespace in RELAX NG validation --- lib/rexml/validation/relaxng.rb | 9 ++- test/test_validation_rng.rb | 101 ++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) diff --git a/lib/rexml/validation/relaxng.rb b/lib/rexml/validation/relaxng.rb index fdfc5698..bc326f80 100644 --- a/lib/rexml/validation/relaxng.rb +++ b/lib/rexml/validation/relaxng.rb @@ -46,6 +46,7 @@ class RelaxNG def initialize source parser = REXML::Parsers::BaseParser.new( source ) + @attr_stack = [] @count = 0 @references = {} @root = @current = Sequence.new(self) @@ -120,7 +121,13 @@ def initialize source end def receive event - return if event[0] == :xmldecl + case event[0] + when :xmldecl + return + when :text + # Whitespace outside the root is not element content. + return if @attr_stack.empty? and /\A[ \t\r\n]*\z/.match?(event[1]) + end validate( event ) end end diff --git a/test/test_validation_rng.rb b/test/test_validation_rng.rb index d4c0394d..8af2273d 100644 --- a/test/test_validation_rng.rb +++ b/test/test_validation_rng.rb @@ -36,6 +36,25 @@ def test_validate def test_document_with_declaration rng = <<-XML + + + + + +XML + validator = REXML::Validation::RelaxNG.new(rng) + + source = <<-XML + + +XML + + no_error(validator, source) + end + + def test_document_with_declaration_variants + rng = <<-XML + @@ -73,6 +92,88 @@ def test_document_with_declaration_and_invalid_structure end + def test_document_with_outer_whitespace + rng = '' + validator = REXML::Validation::RelaxNG.new( rng ) + ['', ''].each do |declaration| + ['', " \t\r\n"].each do |before| + ['', " \t\r\n"].each do |after| + source = "#{declaration}#{before}#{after}" + no_error( validator, source ) + no_error( validator, StringIO.new(source) ) + end + end + end + end + + def test_document_with_outer_whitespace_without_reset + rng = '' + validator = REXML::Validation::RelaxNG.new( rng ) + source = "\n\n" + parser = REXML::Parsers::TreeParser.new( source ) + parser.add_listener( validator ) + assert_nothing_raised { parser.parse } + + 2.times do + no_error( validator, source ) + no_error( validator, "\t\r\n" ) + end + + validator.reset + validator.receive( [:start_element, 'root', {}] ) + no_error( validator, source ) + end + + def test_document_with_outer_whitespace_and_invalid_structure + rng = '' + validator = REXML::Validation::RelaxNG.new( rng ) + ['', ''].each do |declaration| + error( validator, "#{declaration}\n\n" ) + error( validator, "#{declaration}\n\n" ) + error( validator, "#{declaration}\n\n" ) + error( validator, "#{declaration}\n \t\n\n" ) + no_error( validator, "#{declaration}\n\n" ) + end + end + + def test_document_with_outer_whitespace_and_text + rng = '' + validator = REXML::Validation::RelaxNG.new( rng ) + source = "\n \t\r\n\n" + no_error( validator, source ) + no_error( validator, "\ntext\n" ) + error( validator, "\n\n" ) + end + + def test_document_with_outer_whitespace_and_value + rng = ' ' + validator = REXML::Validation::RelaxNG.new( rng ) + ['', ''].each do |declaration| + no_error( validator, "#{declaration}\n \n" ) + error( validator, "#{declaration}\n \n" ) + error( validator, "#{declaration}\n\t\n" ) + error( validator, "#{declaration}\n\n" ) + end + end + + def test_only_xml_whitespace_is_ignored_outside_root + rng = '' + validator = REXML::Validation::RelaxNG.new( rng ) + ["text", " \ttext\n", "\u00a0", "\v", "\f"].each do |text| + validator.reset + assert_raise( REXML::Validation::ValidationException ) do + validator.receive( [:text, text] ) + end + validator.reset + validator.receive( [:start_element, 'root', {}] ) + validator.receive( [:end_element, 'root'] ) + assert_raise( REXML::Validation::ValidationException ) do + validator.receive( [:text, text] ) + end + end + end + + def test_sequence rng = <<-XML From 1a90d7c991a6969c4707e4534c34ba9b2d1b0ae5 Mon Sep 17 00:00:00 2001 From: FFspiky Date: Wed, 18 Nov 2026 20:06:31 +0800 Subject: [PATCH 3/3] Ignore comments, processing instructions and DTD declarations in RELAX NG validation --- lib/rexml/validation/relaxng.rb | 5 ++++- test/test_validation_rng.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/rexml/validation/relaxng.rb b/lib/rexml/validation/relaxng.rb index bc326f80..b09f8831 100644 --- a/lib/rexml/validation/relaxng.rb +++ b/lib/rexml/validation/relaxng.rb @@ -122,7 +122,10 @@ def initialize source def receive event case event[0] - when :xmldecl + when :xmldecl, :comment, :processing_instruction, + :start_doctype, :end_doctype, :elementdecl, :entitydecl, + :attlistdecl, :notationdecl, :externalentity + # These events carry no RELAX NG document content. return when :text # Whitespace outside the root is not element content. diff --git a/test/test_validation_rng.rb b/test/test_validation_rng.rb index 8af2273d..b7e8d14b 100644 --- a/test/test_validation_rng.rb +++ b/test/test_validation_rng.rb @@ -74,6 +74,32 @@ def test_document_with_declaration_variants end end + def test_comments_and_processing_instructions + validator = REXML::Validation::RelaxNG.new('') + ['', ''].each do |markup| + no_error(validator, "#{markup}\n") + no_error(validator, "\n#{markup}") + no_error(validator, "#{markup}") + error(validator, "#{markup}") + end + end + + def test_document_with_doctype + validator = REXML::Validation::RelaxNG.new('') + no_error(validator, "\n\n") + doctype = <<~XML + + + + %empty; + + ]> + XML + no_error(validator, doctype + "") + error(validator, doctype + "") + end + def test_document_with_declaration_and_invalid_structure rng = <<-XML