From 319d622fa5a70879f1eecdec6d6181846e92c4e9 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 11 Sep 2014 14:03:04 +0200 Subject: [PATCH] Include namespaces when converting attrs to XML. --- lib/oga/xml/attribute.rb | 8 +++++++- spec/oga/xml/attribute_spec.rb | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/oga/xml/attribute.rb b/lib/oga/xml/attribute.rb index 01b3c40..7b00ad1 100644 --- a/lib/oga/xml/attribute.rb +++ b/lib/oga/xml/attribute.rb @@ -81,7 +81,13 @@ module Oga # @return [String] # def to_xml - return %Q(#{name}="#{value}") + if namespace_name + full_name = "#{namespace.name}:#{name}" + else + full_name = name + end + + return %Q(#{full_name}="#{value}") end ## diff --git a/spec/oga/xml/attribute_spec.rb b/spec/oga/xml/attribute_spec.rb index 7a595d0..737b7f2 100644 --- a/spec/oga/xml/attribute_spec.rb +++ b/spec/oga/xml/attribute_spec.rb @@ -48,11 +48,26 @@ describe Oga::XML::Attribute do end context '#to_xml' do - example 'return a key/value pair for an XML document' do + example 'convert an attribute to XML' do attr = described_class.new(:name => 'foo', :value => 'bar') attr.to_xml.should == 'foo="bar"' end + + example 'include the namespace when converting an attribute to XML' do + element = Oga::XML::Element.new + + element.register_namespace('foo', 'http://foo') + + attr = described_class.new( + :name => 'class', + :namespace_name => 'foo', + :value => '10', + :element => element + ) + + attr.to_xml.should == 'foo:class="10"' + end end context '#inspect' do