Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/rexml/validation/relaxng.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
168 changes: 168 additions & 0 deletions test/test_validation_rng.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: false

require "stringio"
require "rexml/validation/relaxng"

module REXMLTests
Expand Down Expand Up @@ -32,6 +33,173 @@ def test_validate
end


def test_document_with_declaration
rng = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
<start>
<element name="root"/>
</start>
</grammar>
XML
validator = REXML::Validation::RelaxNG.new(rng)

source = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<root/>
XML

no_error(validator, source)
end

def test_document_with_declaration_variants
rng = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<element name="root" xmlns="http://relaxng.org/ns/structure/1.0">
<empty/>
</element>
XML
validator = REXML::Validation::RelaxNG.new( rng )

[
'',
'<?xml version="1.0"?>',
'<?xml version="1.0" encoding="UTF-8"?>',
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>',
'<?xml version="1.0" standalone="no"?>',
].each do |declaration|
source = "#{declaration}<root/>"
no_error( validator, source )
no_error( validator, StringIO.new(source) )
end
end

def test_comments_and_processing_instructions
validator = REXML::Validation::RelaxNG.new('<element name="root"><empty/></element>')
['<!-- comment -->', '<?xml-stylesheet href="a.xsl"?>'].each do |markup|
no_error(validator, "#{markup}\n<root/>")
no_error(validator, "<root/>\n#{markup}")
no_error(validator, "<root>#{markup}</root>")
error(validator, "<root>#{markup}<unexpected/></root>")
end
end

def test_document_with_doctype
validator = REXML::Validation::RelaxNG.new('<element name="root"><empty/></element>')
no_error(validator, "<?xml version=\"1.0\"?>\n<!DOCTYPE root>\n<root/>")
doctype = <<~XML
<!DOCTYPE root [
<!ELEMENT root EMPTY>
<!ATTLIST root id ID #IMPLIED>
<!ENTITY % empty "">
%empty;
<!NOTATION image SYSTEM "image-format">
]>
XML
no_error(validator, doctype + "<root/>")
error(validator, doctype + "<wrong/>")
end

def test_document_with_declaration_and_invalid_structure
rng = <<-XML
<element name="root" xmlns="http://relaxng.org/ns/structure/1.0">
<element name="child"><empty/></element>
</element>
XML
validator = REXML::Validation::RelaxNG.new( rng )
declaration = '<?xml version="1.0"?>'

error( validator, declaration + '<wrong><child/></wrong>' )
error( validator, declaration + '<root/>' )
error( validator, declaration + '<root><wrong/></root>' )
no_error( validator, declaration + '<root><child/></root>' )
error( validator, declaration + '<root/>' )
no_error( validator, '<root><child/></root>' )
end


def test_document_with_outer_whitespace
rng = '<element name="root"><empty/></element>'
validator = REXML::Validation::RelaxNG.new( rng )
['', '<?xml version="1.0"?>'].each do |declaration|
['', " \t\r\n"].each do |before|
['', " \t\r\n"].each do |after|
source = "#{declaration}#{before}<root/>#{after}"
no_error( validator, source )
no_error( validator, StringIO.new(source) )
end
end
end
end

def test_document_with_outer_whitespace_without_reset
rng = '<element name="root"><empty/></element>'
validator = REXML::Validation::RelaxNG.new( rng )
source = "<?xml version=\"1.0\"?>\n<root/>\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<root/>\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 = '<element name="root"><element name="child"><empty/></element></element>'
validator = REXML::Validation::RelaxNG.new( rng )
['', '<?xml version="1.0"?>'].each do |declaration|
error( validator, "#{declaration}\n<wrong><child/></wrong>\n" )
error( validator, "#{declaration}\n<root/>\n" )
error( validator, "#{declaration}\n<root><wrong/></root>\n" )
error( validator, "#{declaration}\n<root> \t\n<child/></root>\n" )
no_error( validator, "#{declaration}\n<root><child/></root>\n" )
end
end

def test_document_with_outer_whitespace_and_text
rng = '<element name="root"><text/></element>'
validator = REXML::Validation::RelaxNG.new( rng )
source = "<?xml version=\"1.0\"?>\n<root> \t\r\n</root>\n"
no_error( validator, source )
no_error( validator, "\n<root>text</root>\n" )
error( validator, "\n<root/>\n" )
end

def test_document_with_outer_whitespace_and_value
rng = '<element name="root"><value> </value></element>'
validator = REXML::Validation::RelaxNG.new( rng )
['', '<?xml version="1.0"?>'].each do |declaration|
no_error( validator, "#{declaration}\n<root> </root>\n" )
error( validator, "#{declaration}\n<root> </root>\n" )
error( validator, "#{declaration}\n<root>\t</root>\n" )
error( validator, "#{declaration}\n<root/>\n" )
end
end

def test_only_xml_whitespace_is_ignored_outside_root
rng = '<element name="root"><empty/></element>'
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
<?xml version="1.0" encoding="UTF-8"?>
Expand Down