Skip to content

Commit a41f3cc

Browse files
committed
Change REXML::Document.new("") to behave the same as REXML::Document.new(nil)
## Why? XML without a root element is invalid XML, but to maintain compatibility, we will change the behavior of `REXML::Document.new("")` to be the same as `REXML::Document.new(nil)`.
1 parent 4ffe211 commit a41f3cc

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

lib/rexml/document.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def initialize( source = nil, context = {} )
9595
@entity_expansion_text_limit = Security.entity_expansion_text_limit
9696
super()
9797
@context = context
98-
return if source.nil?
98+
return if source.nil? or source == ""
9999
if source.kind_of? Document
100100
@context = source.context
101101
super source

test/parse/test_element.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def parse(xml)
1414
class TestInvalid < self
1515
def test_top_level_no_tag
1616
exception = assert_raise(REXML::ParseException) do
17-
parse("")
17+
parse(" ")
1818
end
1919
assert_equal(<<-DETAIL.chomp, exception.to_s)
2020
Malformed XML: No root element
21-
Line: 0
22-
Position: 0
21+
Line: 1
22+
Position: 1
2323
Last 80 unconsumed characters:
2424
2525
DETAIL

test/test_core.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,10 @@ def test_ticket_138
15501550
REXML::Document.new(doc.root.to_s).root.attributes.to_h)
15511551
end
15521552

1553+
def test_empty_doc
1554+
assert(REXML::Document.new('').children.empty?)
1555+
end
1556+
15531557
private
15541558
def attribute(name, value)
15551559
REXML::Attribute.new(name, value)

0 commit comments

Comments
 (0)