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
4 changes: 3 additions & 1 deletion benchmark/parse_comment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ contexts:
prelude: |
require 'rexml/document'

SIZE = 100000
SIZE = 1000

top_level_xml = "<!--" + "a" * SIZE + "-->\n<root/>"
in_doctype_xml = "<!DOCTYPE foo [<!--" + "a" * SIZE + "-->]><root/>"
after_doctype_xml = "<root/><!--" + "a" * SIZE + "-->"
many_comments_xml = "<!---->" * SIZE + "<a/>"

benchmark:
'top_level' : REXML::Document.new(top_level_xml)
'in_doctype' : REXML::Document.new(in_doctype_xml)
'after_doctype' : REXML::Document.new(after_doctype_xml)
'many_comments' : REXML::Document.new(many_comments_xml)
10 changes: 4 additions & 6 deletions lib/rexml/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,8 @@ def add( child )
end
child.parent = self
else
rv = super
raise "attempted adding second root element to document" if @elements.size > 1
rv
raise "attempted adding second root element to document" if child.kind_of?(Element) && root
super
end
end
alias :<< :add
Expand All @@ -211,9 +210,8 @@ def add( child )
#
# REXML::Element.add_element(name_or_element, attributes)
def add_element(arg=nil, arg2=nil)
rv = super
raise "attempted adding second root element to document" if @elements.size > 1
rv
raise "attempted adding second root element to document" if root
super
end

# :call-seq:
Expand Down
44 changes: 44 additions & 0 deletions test/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,50 @@ def test_encoding
end
end

class AddTest < Test::Unit::TestCase
def test_add_second_root_element_raises
doc = REXML::Document.new("<root/>")
assert_raise(RuntimeError, "attempted adding second root element to document") do
doc.add(REXML::Element.new("second"))
end
end

def test_append_operator_second_root_element_raises
doc = REXML::Document.new("<root/>")
assert_raise(RuntimeError, "attempted adding second root element to document") do
doc << REXML::Element.new("second")
end
end

def test_add_element_second_root_raises
doc = REXML::Document.new("<root/>")
assert_raise(RuntimeError, "attempted adding second root element to document") do
doc.add_element("second")
end
end

def test_add_element_with_element_second_root_raises
doc = REXML::Document.new("<root/>")
assert_raise(RuntimeError, "attempted adding second root element to document") do
doc.add_element(REXML::Element.new("second"))
end
end

def test_add_xml_decl_allowed
doc = REXML::Document.new("<root/>")
assert_nothing_raised do
doc.add(REXML::XMLDecl.new("1.0"))
end
end

def test_add_doctype_allowed
doc = REXML::Document.new("<root/>")
assert_nothing_raised do
doc.add(REXML::DocType.new("root"))
end
end
end

class BomTest < Test::Unit::TestCase
class HaveEncodingTest < self
def test_utf_8
Expand Down