Use HTML5 style closing tags for void elements
This ensures that element tags such as <img> tags don't use a closing /> when documents are parsed as HTML documents. Fixes #170
This commit is contained in:
parent
131fba7aed
commit
673f4a29db
|
@ -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 `<img>` and `<link>` (called "void elements" in
|
||||
Oga) are now closed using a `>` tag instead of `/>`. In other words, instead of
|
||||
outputting `<img src="..." />` Oga now outputs `<img src="...">`.
|
||||
|
||||
### Doctypes are now Nodes
|
||||
|
||||
Each Doctype now inherits from `Oga::XML::Node`. This makes it possible to parse
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 == '<link />'
|
||||
element.to_xml.should == '<link>'
|
||||
end
|
||||
|
||||
it 'generates the XML for an empty explicitly closed HTML element' do
|
||||
|
|
|
@ -107,7 +107,7 @@ describe Oga::XML::Generator do
|
|||
<html>
|
||||
<head>
|
||||
<title>Hello</title>
|
||||
<meta charset="utf-8" />
|
||||
<meta charset="utf-8">
|
||||
</head>
|
||||
<body>
|
||||
<p>Hello</p>
|
||||
|
@ -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 == '<img />'
|
||||
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 == '<img>kittens</img>'
|
||||
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 == '<img>'
|
||||
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 == '<img>kittens</img>'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue