From 8e8ea642064b6260a298b24e8cbbc1113bcd7371 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 6 Aug 2014 00:04:42 +0200 Subject: [PATCH] Fixed serializing of elements to XML. --- lib/oga/xml/attribute.rb | 7 +++++++ lib/oga/xml/element.rb | 4 ++-- spec/oga/xml/attribute_spec.rb | 8 ++++++++ spec/oga/xml/element_spec.rb | 4 +++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/oga/xml/attribute.rb b/lib/oga/xml/attribute.rb index 0087585..ae1af3f 100644 --- a/lib/oga/xml/attribute.rb +++ b/lib/oga/xml/attribute.rb @@ -38,6 +38,13 @@ module Oga return value.to_s end + ## + # @return [String] + # + def to_xml + return %Q(#{name}="#{value}") + end + ## # @return [String] # diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb index 31f09a0..6fedff4 100644 --- a/lib/oga/xml/element.rb +++ b/lib/oga/xml/element.rb @@ -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? diff --git a/spec/oga/xml/attribute_spec.rb b/spec/oga/xml/attribute_spec.rb index 7d063f0..59faadc 100644 --- a/spec/oga/xml/attribute_spec.rb +++ b/spec/oga/xml/attribute_spec.rb @@ -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') diff --git a/spec/oga/xml/element_spec.rb b/spec/oga/xml/element_spec.rb index 2c56ee1..71a6876 100644 --- a/spec/oga/xml/element_spec.rb +++ b/spec/oga/xml/element_spec.rb @@ -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 == '

'