Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion lib/rexml/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ def initialize( source = nil, context = {} )
@entity_expansion_text_limit = Security.entity_expansion_text_limit
super()
@context = context
return if source.nil?
# This is an invalid usage because no root element XML is an invalid XML.
# But we accept `""` for backward compatibility.
return if source.nil? or source == ""
if source.kind_of? Document
@context = source.context
super source
Expand Down
6 changes: 3 additions & 3 deletions test/parse/test_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def parse(xml)
class TestInvalid < self
def test_top_level_no_tag
exception = assert_raise(REXML::ParseException) do
parse("")
parse(" ")
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed XML: No root element
Line: 0
Position: 0
Line: 1
Position: 1
Last 80 unconsumed characters:
DETAIL
Expand Down
20 changes: 20 additions & 0 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,26 @@ def test_ticket_138
REXML::Document.new(doc.root.to_s).root.attributes.to_h)
end

def test_document_with_no_arguments
assert do
REXML::Document.new(nil).children.empty?
end
end

def test_document_with_nil_arguments
assert do
REXML::Document.new(nil).children.empty?
end
end

def test_empty_doc
# This is an invalid usage because no root element XML is an invalid XML.
# But we accept `""` for backward compatibility.
assert do
REXML::Document.new('').children.empty?
end
end

private
def attribute(name, value)
REXML::Attribute.new(name, value)
Expand Down
Loading