From a2c525dd7c08d3a8f6e783d1ddf5aba3dc3e8fa0 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 3 Apr 2014 23:04:21 +0200 Subject: [PATCH] Insert newlines after XML dec/doctypes. --- lib/oga/xml/document.rb | 4 ++-- spec/oga/xml/document_spec.rb | 23 +++++++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/oga/xml/document.rb b/lib/oga/xml/document.rb index 4c35a4a..f93dfcf 100644 --- a/lib/oga/xml/document.rb +++ b/lib/oga/xml/document.rb @@ -21,11 +21,11 @@ module Oga xml = children.map(&:to_xml).join('') if doctype - xml = doctype.to_xml + xml + xml = doctype.to_xml + "\n" + xml.strip end if xml_declaration - xml = xml_declaration.to_xml + xml + xml = xml_declaration.to_xml + "\n" + xml.strip end return xml diff --git a/spec/oga/xml/document_spec.rb b/spec/oga/xml/document_spec.rb index 274cea9..34cabaf 100644 --- a/spec/oga/xml/document_spec.rb +++ b/spec/oga/xml/document_spec.rb @@ -43,7 +43,7 @@ describe Oga::XML::Document do example 'include the XML of the declaration tag' do @document.to_xml - .should == '' + .should == %Q{\n} end end @@ -59,7 +59,26 @@ describe Oga::XML::Document do end example 'include the doctype' do - @document.to_xml.should == '' + @document.to_xml.should == %Q{\n} + end + end + + context '#to_xml with XML declarations and doctypes' do + before do + decl = Oga::XML::XmlDeclaration.new(:version => '5.0') + doctype = Oga::XML::Doctype.new(:name => 'html', :type => 'PUBLIC') + children = [Oga::XML::Comment.new(:text => 'foo')] + + @document = described_class.new( + :doctype => doctype, + :xml_declaration => decl, + :children => children + ) + end + + example 'include the doctype and XML declaration' do + @document.to_xml.should == '' \ + "\n\n" end end end