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 return value.to_s
end end
##
# @return [String]
#
def to_xml
return %Q(#{name}="#{value}")
end
## ##
# @return [String] # @return [String]
# #

View File

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

View File

@ -25,6 +25,14 @@ describe Oga::XML::Attribute do
end end
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 context '#inspect' do
example 'return the inspect value' do example 'return the inspect value' do
obj = described_class.new(:name => 'a', :namespace => 'b', :value => 'c') 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 example 'include the attributes if present' do
instance = described_class.new( instance = described_class.new(
:name => 'p', :name => 'p',
:attributes => {:key => 'value'} :attributes => [
Oga::XML::Attribute.new(:name => 'key', :value => 'value')
]
) )
instance.to_xml.should == '<p key="value"></p>' instance.to_xml.should == '<p key="value"></p>'