Make HTML5 the default output format everywhere.

Resolves #565
This commit is contained in:
Norman Clarke 2012-06-26 11:33:31 -03:00
parent 3f0ac98fb6
commit f82de38558
10 changed files with 56 additions and 51 deletions

View File

@ -2,6 +2,9 @@
## 3.2.0 (Unreleased)
* HTML5 is now the default output format rather than XHTML. This was already
the default on Rails 3+, so many users will notice no difference.
* The :javascript and :css filters no longer add CDATA tags when the format is
html4 or html5. This can be overridden by setting the `cdata` option to
`true`. CDATA tags are always added when the format is xhtml.

View File

@ -1,7 +1,7 @@
# Haml (XHTML Abstraction Markup Language)
Haml is a markup language that's used to cleanly and simply describe the XHTML
of any web document, without the use of inline code. Haml functions as a
Haml is a markup language that's used to cleanly and simply describe the HTML of
any web document, without the use of inline code. Haml functions as a
replacement for inline page templating systems such as PHP, ERB, and ASP.
However, Haml avoids the need for explicitly coding HTML into the template,
because it is actually an abstract description of the HTML, with some code to
@ -651,8 +651,8 @@ is compiled to:
</html>
You can also specify the specific doctype after the `!!!` When the
[`:format`](#format-option) is set to `:xhtml` (the default except in Rails 3),
the following doctypes are supported:
[`:format`](#format-option) is set to `:xhtml`. The following doctypes are
supported:
`!!!`
: XHTML 1.0 Transitional<br/>
@ -1232,7 +1232,7 @@ For example, tags like `pre` and `textarea` are whitespace-sensitive;
indenting the text makes them render wrong.
Haml deals with this by "preserving" newlines before they're put into the document --
converting them to the XHTML whitespace escape code, `&#x000A;`.
converting them to the HTML whitespace escape code, `&#x000A;`.
Then Haml won't try to re-format the indentation.
Literal `textarea` and `pre` tags automatically preserve content given through `=`.

View File

@ -218,7 +218,7 @@ END
end
opts.on('-f', '--format NAME',
'Output format. Can be xhtml (default), html4, or html5.') do |name|
'Output format. Can be html5 (default), xhtml, or html4.') do |name|
@options[:for_engine][:format] = name.to_sym
end

View File

@ -11,7 +11,7 @@ module Haml
:escape_attrs => true,
:escape_html => false,
:filename => '(haml)',
:format => :xhtml,
:format => :html5,
:hyphenate_data_attrs => true,
:line => 1,
:mime_type => 'text/html',
@ -22,7 +22,7 @@ module Haml
:cdata => false
}
@valid_formats = [:xhtml, :html4, :html5]
@valid_formats = [:html4, :html5, :xhtml]
@buffer_option_keys = [:autoclose, :preserve, :attr_wrapper, :ugly, :format,
:encoding, :escape_html, :escape_attrs, :hyphenate_data_attrs, :cdata]
@ -102,11 +102,13 @@ module Haml
# inline templates, similar to the last argument to `Kernel#eval`.
attr_accessor :line
# Determines the output format. Normally the default is `:xhtml`, although
# under Rails 3 it's `:html5`, since that's the Rails 3's default format.
# Other options are `:html4` and `:html5`, which are identical to `:xhtml`
# except there are no self-closing tags, the XML prolog is ignored and
# correct DOCTYPEs are generated.
# Determines the output format. The default is `:html5`. The other options
# are `:html4` and `:xhtml`. If the output is set to XHTML, then Haml
# automatically generates self-closing tags and wraps the output of the
# Javascript and CSS-like filters inside CDATA. When the output is set to
# :html5 or :html4, XML prologs are ignored. In all cases, an appropriate
# doctype is generated from '!!!'.
#
#
# If the mime_type of the template being rendered is `text/xml` then a
# format of `:xhtml` will be used even if the global output format is set to
@ -223,7 +225,7 @@ module Haml
end
@format = value
end
undef :cdata
def cdata
xhtml? || @cdata

View File

@ -27,6 +27,5 @@ end
Haml::Template.options[:ugly] = !Rails.env.development?
Haml::Template.options[:escape_html] = true
Haml::Template.options[:format] = :html5
require 'haml/template/plugin'
require 'haml/template/plugin'

View File

@ -173,11 +173,11 @@ class EngineTest < MiniTest::Unit::TestCase
end
def test_colon_in_class_attr
assert_equal("<p class='foo:bar' />\n", render("%p.foo:bar/"))
assert_equal("<p class='foo:bar'>\n", render("%p.foo:bar/"))
end
def test_colon_in_id_attr
assert_equal("<p id='foo:bar' />\n", render("%p#foo:bar/"))
assert_equal("<p id='foo:bar'>\n", render("%p#foo:bar/"))
end
def test_dynamic_attributes_with_no_content
@ -258,17 +258,17 @@ HAML
end
def test_static_attributes_with_empty_attr
assert_equal("<img alt='' src='/foo.png' />\n", render("%img{:src => '/foo.png', :alt => ''}"))
assert_equal("<img alt='' src='/foo.png'>\n", render("%img{:src => '/foo.png', :alt => ''}"))
end
def test_dynamic_attributes_with_empty_attr
assert_equal("<img alt='' src='/foo.png' />\n", render("%img{:width => nil, :src => '/foo.png', :alt => String.new}"))
assert_equal("<img alt='' src='/foo.png'>\n", render("%img{:width => nil, :src => '/foo.png', :alt => String.new}"))
end
def test_attribute_hash_with_newlines
assert_equal("<p a='b' c='d'>foop</p>\n", render("%p{:a => 'b',\n :c => 'd'} foop"))
assert_equal("<p a='b' c='d'>\n foop\n</p>\n", render("%p{:a => 'b',\n :c => 'd'}\n foop"))
assert_equal("<p a='b' c='d' />\n", render("%p{:a => 'b',\n :c => 'd'}/"))
assert_equal("<p a='b' c='d'>\n", render("%p{:a => 'b',\n :c => 'd'}/"))
assert_equal("<p a='b' c='d' e='f'></p>\n", render("%p{:a => 'b',\n :c => 'd',\n :e => 'f'}"))
end
@ -290,7 +290,7 @@ HAML
assert_equal(<<HTML, render(<<HAML, :ugly => true))
<p a='2'></p>
<p a='2'>foo</p>
<p a='2' />
<p a='2'>
<p a='2'>foo</p>
<p a='2'>foo
bar</p>
@ -408,7 +408,7 @@ HAML
assert_equal(<<HTML, render(<<HAML))
<html>
<body>
<img src='test' />
<img src='test'>
foo
bar
</body>
@ -909,7 +909,7 @@ HAML
end
def test_static_attributes_should_be_escaped
assert_equal("<img class='atlantis' style='ugly&amp;stupid' />\n",
assert_equal("<img class='atlantis' style='ugly&amp;stupid'>\n",
render("%img.atlantis{:style => 'ugly&stupid'}"))
assert_equal("<div class='atlantis' style='ugly&amp;stupid'>foo</div>\n",
render(".atlantis{:style => 'ugly&stupid'} foo"))
@ -920,13 +920,13 @@ HAML
end
def test_dynamic_attributes_should_be_escaped
assert_equal("<img alt='' src='&amp;foo.png' />\n",
assert_equal("<img alt='' src='&amp;foo.png'>\n",
render("%img{:width => nil, :src => '&foo.png', :alt => String.new}"))
assert_equal("<p alt='' src='&amp;foo.png'>foo</p>\n",
render("%p{:width => nil, :src => '&foo.png', :alt => String.new} foo"))
assert_equal("<div alt='' src='&amp;foo.png'>foo</div>\n",
render("%div{:width => nil, :src => '&foo.png', :alt => String.new}= 'foo'"))
assert_equal("<img alt='' src='foo&#x000A;.png' />\n",
assert_equal("<img alt='' src='foo&#x000A;.png'>\n",
render("%img{:width => nil, :src => \"foo\\n.png\", :alt => String.new}"))
end
@ -1027,9 +1027,9 @@ HAML
def test_stop_eval
assert_equal("", render("= 'Hello'", :suppress_eval => true))
assert_equal("", render("- haml_concat 'foo'", :suppress_eval => true))
assert_equal("<div id='foo' yes='no' />\n", render("#foo{:yes => 'no'}/", :suppress_eval => true))
assert_equal("<div id='foo' />\n", render("#foo{:yes => 'no', :call => a_function() }/", :suppress_eval => true))
assert_equal("<div />\n", render("%div[1]/", :suppress_eval => true))
assert_equal("<div id='foo' yes='no'>\n", render("#foo{:yes => 'no'}/", :suppress_eval => true))
assert_equal("<div id='foo'>\n", render("#foo{:yes => 'no', :call => a_function() }/", :suppress_eval => true))
assert_equal("<div>\n", render("%div[1]/", :suppress_eval => true))
assert_equal("", render(":ruby\n Kernel.puts 'hello'", :suppress_eval => true))
end
@ -1038,17 +1038,17 @@ HAML
render('!!!', :format => :html5).strip)
assert_equal('<!DOCTYPE html>', render('!!! 5').strip)
assert_equal('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
render('!!! strict').strip)
render('!!! strict', :format => :xhtml).strip)
assert_equal('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
render('!!! frameset').strip)
render('!!! frameset', :format => :xhtml).strip)
assert_equal('<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">',
render('!!! mobile').strip)
render('!!! mobile', :format => :xhtml).strip)
assert_equal('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">',
render('!!! basic').strip)
render('!!! basic', :format => :xhtml).strip)
assert_equal('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
render('!!! transitional').strip)
render('!!! transitional', :format => :xhtml).strip)
assert_equal('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
render('!!!').strip)
render('!!!', :format => :xhtml).strip)
assert_equal('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
render('!!! strict', :format => :html4).strip)
assert_equal('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
@ -1064,14 +1064,14 @@ HAML
assert_equal("<p escaped='quo\"te'></p>\n", render("%p{ :escaped => 'quo\"te'}", :attr_wrapper => '"'))
assert_equal("<p escaped=\"quo'te\"></p>\n", render("%p{ :escaped => 'quo\\'te'}", :attr_wrapper => '"'))
assert_equal("<p escaped=\"q'uo&#x0022;te\"></p>\n", render("%p{ :escaped => 'q\\'uo\"te'}", :attr_wrapper => '"'))
assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n", render("!!! XML", :attr_wrapper => '"'))
assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n", render("!!! XML", :attr_wrapper => '"', :format => :xhtml))
end
def test_autoclose_option
assert_equal("<flaz foo='bar' />\n", render("%flaz{:foo => 'bar'}", :autoclose => ["flaz"]))
assert_equal("<flaz foo='bar'>\n", render("%flaz{:foo => 'bar'}", :autoclose => ["flaz"]))
assert_equal(<<HTML, render(<<HAML, :autoclose => [/^flaz/]))
<flaz />
<flaznicate />
<flaz>
<flaznicate>
<flan></flan>
HTML
%flaz
@ -1140,7 +1140,7 @@ HAML
end
def test_dynamic_attrs_with_self_closed_tag
assert_equal("<a b='2' />\nc\n", render("%a{'b' => 1 + 1}/\n= 'c'\n"))
assert_equal("<a b='2'>\nc\n", render("%a{'b' => 1 + 1}/\n= 'c'\n"))
end
EXCEPTION_MAP.each do |key, value|
@ -1236,8 +1236,8 @@ HAML
end
def test_non_literal_attributes
assert_equal("<p a1='foo' a2='bar' a3='baz' />\n",
render("%p{a2, a1, :a3 => 'baz'}/",
assert_equal("<p a1='foo' a2='bar' a3='baz'></p>\n",
render("%p{a2, a1, :a3 => 'baz'}",
:locals => {:a1 => {:a1 => 'foo'}, :a2 => {:a2 => 'bar'}}))
end
@ -1553,9 +1553,9 @@ HAML
end
def test_truthy_new_attributes
assert_equal("<a href='href'>bar</a>\n", render("%a(href) bar"))
assert_equal("<a href='href'>bar</a>\n", render("%a(href) bar", :format => :xhtml))
assert_equal("<a bar='baz' href>bar</a>\n", render("%a(href bar='baz') bar", :format => :html5))
assert_equal("<a href='href'>bar</a>\n", render("%a(href=true) bar"))
assert_equal("<a href>bar</a>\n", render("%a(href=true) bar"))
assert_equal("<a>bar</a>\n", render("%a(href=false) bar"))
end

View File

@ -140,7 +140,7 @@ class JavascriptFilterTest < MiniTest::Unit::TestCase
end
test "should never HTML-escape ampersands" do
html = "<script type='text/javascript'>\n //<![CDATA[\n & < > &\n //]]>\n</script>\n"
html = "<script>\n & < > &\n</script>\n"
haml = %Q{:javascript\n & < > \#{"&"}}
assert_equal(html, render(haml, :escape_html => true))
end
@ -178,10 +178,10 @@ class JavascriptFilterTest < MiniTest::Unit::TestCase
end
class CSSFilterTest < MiniTest::Unit::TestCase
test "should wrap output in CDATA and a CSS tag" do
test "should wrap output in CDATA and a CSS tag when output is XHTML" do
html = "<style type='text/css'>\n /*<![CDATA[*/\n foo\n /*]]>*/\n</style>\n"
haml = ":css\n foo"
assert_equal(html, render(haml))
assert_equal(html, render(haml, :format => :xhtml))
end
test "should not include type in HTML 5 output" do

@ -1 +1 @@
Subproject commit 15288201caf6bbf982953acdba8da7380d0fcad0
Subproject commit 519e0703fc8306d81ffbae58fdd81e748b7bce5a

View File

@ -275,7 +275,7 @@ HAML
end
def test_haml_tag_with_data_hash
assert_equal("<p data-baz='data-baz' data-foo='bar'>foo</p>\n",
assert_equal("<p data-baz data-foo='bar'>foo</p>\n",
render("- haml_tag :p, 'foo', :data => {:foo => 'bar', :baz => true}"))
end

View File

@ -70,6 +70,7 @@ class TemplateTest < MiniTest::Unit::TestCase
def render(text, opts = {})
return @base.render(:inline => text, :type => :haml) if opts == :action_view
opts = opts.merge(:format => :xhtml)
Haml::Engine.new(text, opts).to_html(@base)
end
@ -117,14 +118,14 @@ class TemplateTest < MiniTest::Unit::TestCase
def test_templates_should_render_correctly_with_render_proc
assert_renders_correctly("standard") do |name|
engine = Haml::Engine.new(File.read(File.dirname(__FILE__) + "/templates/#{name}.haml"))
engine = Haml::Engine.new(File.read(File.dirname(__FILE__) + "/templates/#{name}.haml"), :format => :xhtml)
engine.render_proc(@base).call
end
end
def test_templates_should_render_correctly_with_def_method
assert_renders_correctly("standard") do |name|
engine = Haml::Engine.new(File.read(File.dirname(__FILE__) + "/templates/#{name}.haml"))
engine = Haml::Engine.new(File.read(File.dirname(__FILE__) + "/templates/#{name}.haml"), :format => :xhtml)
engine.def_method(@base, "render_standard")
@base.render_standard
end