diff --git a/lib/rexml/validation/relaxng.rb b/lib/rexml/validation/relaxng.rb index c6894dcb..b09f8831 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,6 +121,16 @@ def initialize source end def receive event + case event[0] + 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. + 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 4872396b..b7e8d14b 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,173 @@ def test_validate end + 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 + + + + + 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_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 + + + + 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_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