diff --git a/lib/oga/xml/generator.rb b/lib/oga/xml/generator.rb
index 547b4d2..15ffa94 100644
--- a/lib/oga/xml/generator.rb
+++ b/lib/oga/xml/generator.rb
@@ -17,8 +17,8 @@ module Oga
def initialize(root)
@start = root
- if @start.respond_to?(:root_node)
- @html_mode = @start.root_node.html?
+ if @start.respond_to?(:html?)
+ @html_mode = @start.html?
else
@html_mode = false
end
@@ -75,6 +75,10 @@ module Oga
break
else
+ # Make sure to always close the current element before
+ # moving to any siblings.
+ after_element(current, output) if current.is_a?(Element)
+
until next_node = current.is_a?(Node) && current.next
if current.is_a?(Node) && current != @start
current = current.parent
@@ -177,6 +181,14 @@ module Oga
on_doctype(doc.doctype, output)
output << "\n"
end
+
+ first_child = doc.children[0]
+
+ # Prevent excessive newlines in case the next node is a newline text
+ # node.
+ if first_child.is_a?(Text) && first_child.text.start_with?("\r\n", "\n")
+ output.chomp!
+ end
end
# @param [Oga::XML::XmlDeclaration] node
diff --git a/spec/oga/xml/element_spec.rb b/spec/oga/xml/element_spec.rb
index 89c5e6b..41aae4a 100644
--- a/spec/oga/xml/element_spec.rb
+++ b/spec/oga/xml/element_spec.rb
@@ -464,6 +464,13 @@ describe Oga::XML::Element do
element.to_xml.should == ''
end
+
+ it 'generates the XML for an empty explicitly closed HTML element' do
+ element = described_class.new(:name => 'html')
+ document = Oga::XML::Document.new(:type => :html, :children => [element])
+
+ element.to_xml.should == ''
+ end
end
describe '#inspect' do
diff --git a/spec/oga/xml/generator_spec.rb b/spec/oga/xml/generator_spec.rb
index 00d0a87..27a4fd1 100644
--- a/spec/oga/xml/generator_spec.rb
+++ b/spec/oga/xml/generator_spec.rb
@@ -29,6 +29,27 @@ describe Oga::XML::Generator do
end
end
+ describe 'using an HTML Document as the root node' do
+ it 'returns a String' do
+ element = Oga::XML::Element.new(name: 'foo')
+ doc = Oga::XML::Document.new(children: [element], type: :html)
+ output = described_class.new(doc).to_xml
+
+ output.should == '
Hello
+