diff --git a/CHANGELOG.md b/CHANGELOG.md index c31546b..1dc74e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ Dates are in the format `yyyy-mm-dd`. ## 2.9 - 2017-02-10 +### Closing tags for HTML void elements + +Certain HTML elements such as `` and `` (called "void elements" in +Oga) are now closed using a `>` tag instead of `/>`. In other words, instead of +outputting `` Oga now outputs ``. + ### Doctypes are now Nodes Each Doctype now inherits from `Oga::XML::Node`. This makes it possible to parse diff --git a/lib/oga/xml/generator.rb b/lib/oga/xml/generator.rb index 15ffa94..176a26a 100644 --- a/lib/oga/xml/generator.rb +++ b/lib/oga/xml/generator.rb @@ -136,7 +136,9 @@ module Oga end if self_closing?(element) - output << "<#{name}#{attrs} />" + closing_tag = html_void_element?(element) ? '>' : ' />' + + output << "<#{name}#{attrs}#{closing_tag}" else output << "<#{name}#{attrs}>" end @@ -214,6 +216,10 @@ module Oga element.children.empty? end end + + def html_void_element?(element) + @html_mode && HTML_VOID_ELEMENTS.allow?(element.name) + end end end end diff --git a/spec/oga/xml/element_spec.rb b/spec/oga/xml/element_spec.rb index 41aae4a..1cf1a4f 100644 --- a/spec/oga/xml/element_spec.rb +++ b/spec/oga/xml/element_spec.rb @@ -462,7 +462,7 @@ describe Oga::XML::Element do element = described_class.new(:name => 'link') document = Oga::XML::Document.new(:type => :html, :children => [element]) - element.to_xml.should == '' + element.to_xml.should == '' end it 'generates the XML for an empty explicitly closed HTML element' do diff --git a/spec/oga/xml/generator_spec.rb b/spec/oga/xml/generator_spec.rb index 27a4fd1..a74d68a 100644 --- a/spec/oga/xml/generator_spec.rb +++ b/spec/oga/xml/generator_spec.rb @@ -107,7 +107,7 @@ describe Oga::XML::Generator do
Hello
@@ -126,5 +126,47 @@ describe Oga::XML::Generator do output.to_xml.should == input end end + + describe 'using an XML document containing HTML void elements' do + describe 'using empty void elements' do + it 'returns a String' do + img = Oga::XML::Element.new(name: 'img') + doc = Oga::XML::Document.new(children: [img], type: :xml) + + doc.to_xml.should == '' + end + end + + describe 'using non-empty void elements' do + it 'returns a String' do + text = Oga::XML::Text.new(text: 'kittens') + img = Oga::XML::Element.new(name: 'img', children: [text]) + doc = Oga::XML::Document.new(children: [img], type: :xml) + + doc.to_xml.should == 'kittens' + end + end + end + + describe 'using an HTML document containing HTML void elements' do + describe 'using empty void elements' do + it 'returns a String' do + img = Oga::XML::Element.new(name: 'img') + doc = Oga::XML::Document.new(children: [img], type: :html) + + doc.to_xml.should == '' + end + end + + describe 'using non-empty void elements' do + it 'returns a String' do + text = Oga::XML::Text.new(text: 'kittens') + img = Oga::XML::Element.new(name: 'img', children: [text]) + doc = Oga::XML::Document.new(children: [img], type: :html) + + doc.to_xml.should == 'kittens' + end + end + end end end