From b8a82b2094ef9debf7861f246f9b68bd7b8e1081 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Sat, 13 Sep 2014 11:47:06 +0200 Subject: [PATCH] Separate XML attributes by spaces. This was originally reported by @jrochkind and partially patched by @billdueber. My patches are built upon the latter, but without the need of using Array#map, Array#join, etc. They also contain a few style changes. This fixes #32 and #33. --- lib/oga/xml/element.rb | 4 +--- spec/oga/xml/element_spec.rb | 14 +++++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb index cde3ff9..8980fc2 100644 --- a/lib/oga/xml/element.rb +++ b/lib/oga/xml/element.rb @@ -211,11 +211,9 @@ module Oga attrs = '' attributes.each do |attr| - attrs << attr.to_xml + attrs << " #{attr.to_xml}" end - attrs = " #{attrs}" unless attrs.empty? - return "<#{ns}#{name}#{attrs}>#{body}" end diff --git a/spec/oga/xml/element_spec.rb b/spec/oga/xml/element_spec.rb index 047c606..88d8716 100644 --- a/spec/oga/xml/element_spec.rb +++ b/spec/oga/xml/element_spec.rb @@ -261,7 +261,7 @@ describe Oga::XML::Element do instance.to_xml.should == '' end - example 'include the attributes if present' do + example 'include a single attribute if present' do instance = described_class.new( :name => 'p', :attributes => [ @@ -272,6 +272,18 @@ describe Oga::XML::Element do instance.to_xml.should == '

' end + example 'include multiple attributes if present' do + instance = described_class.new( + :name => 'p', + :attributes => [ + Oga::XML::Attribute.new(:name => 'key1', :value => 'value1'), + Oga::XML::Attribute.new(:name => 'key2', :value => 'value2'), + ] + ) + + instance.to_xml.should == '

' + end + example 'include the child nodes if present' do instance = described_class.new( :name => 'p',