Fixed serializing of elements to XML.

This commit is contained in:
Yorick Peterse 2014-08-06 00:04:42 +02:00
parent e0bbc81351
commit 8e8ea64206
4 changed files with 20 additions and 3 deletions

View File

@ -38,6 +38,13 @@ module Oga
return value.to_s
end
##
# @return [String]
#
def to_xml
return %Q(#{name}="#{value}")
end
##
# @return [String]
#

View File

@ -94,8 +94,8 @@ module Oga
body = children.map(&:to_xml).join('')
attrs = ''
attributes.each do |key, value|
attrs << "#{key}=#{value.inspect}"
attributes.each do |attr|
attrs << attr.to_xml
end
attrs = " #{attrs}" unless attrs.empty?

View File

@ -25,6 +25,14 @@ describe Oga::XML::Attribute do
end
end
context '#to_xml' do
example 'return a key/value pair for an XML document' do
attr = described_class.new(:name => 'foo', :value => 'bar')
attr.to_xml.should == 'foo="bar"'
end
end
context '#inspect' do
example 'return the inspect value' do
obj = described_class.new(:name => 'a', :namespace => 'b', :value => 'c')

View File

@ -111,7 +111,9 @@ describe Oga::XML::Element do
example 'include the attributes if present' do
instance = described_class.new(
:name => 'p',
:attributes => {:key => 'value'}
:attributes => [
Oga::XML::Attribute.new(:name => 'key', :value => 'value')
]
)
instance.to_xml.should == '<p key="value"></p>'