support HTML5

This commit is contained in:
Mislav Marohnić 2008-02-27 15:16:21 +01:00
parent 83a264d75f
commit cd1dffcddc
3 changed files with 44 additions and 23 deletions

View File

@ -24,16 +24,26 @@ module Haml
# to produce the Haml document. # to produce the Haml document.
attr :precompiled, true attr :precompiled, true
# Returns whether or not this Engine instance is generating XHTML output. # True when the output is XHTML
def xhtml? def xhtml?
not html4? @options[:output] == :xhtml
end end
# Returns whether or not this Engine instance is generating HTML4 output. # True if the output is any flavor of HTML
def html?
html4? or html5?
end
# True when the output is HTML4
def html4? def html4?
@options[:output] == :html4 @options[:output] == :html4
end end
# True when the output is HTML5
def html5?
@options[:output] == :html5
end
# Creates a new instace of Haml::Engine that will compile the given # Creates a new instace of Haml::Engine that will compile the given
# template string when <tt>render</tt> is called. # template string when <tt>render</tt> is called.
# See README for available options. # See README for available options.

View File

@ -595,34 +595,40 @@ END
# Renders an XHTML doctype or XML shebang. # Renders an XHTML doctype or XML shebang.
def render_doctype(line) def render_doctype(line)
raise SyntaxError.new("Illegal Nesting: Nesting within a header command is illegal.") if @block_opened raise SyntaxError.new("Illegal Nesting: Nesting within a header command is illegal.") if @block_opened
push_text text_for_doctype(line) doctype = text_for_doctype(line)
push_text doctype if doctype
end end
def text_for_doctype(text) def text_for_doctype(text)
text = text[3..-1].lstrip.downcase text = text[3..-1].lstrip.downcase
if text[0...3] == "xml" if text.index("xml") == 0
return nil if html4? return nil if html?
wrapper = @options[:attr_wrapper] wrapper = @options[:attr_wrapper]
return "<?xml version=#{wrapper}1.0#{wrapper} encoding=#{wrapper}#{text.split(' ')[1] || "utf-8"}#{wrapper} ?>" return "<?xml version=#{wrapper}1.0#{wrapper} encoding=#{wrapper}#{text.split(' ')[1] || "utf-8"}#{wrapper} ?>"
end end
version, type = text.scan(DOCTYPE_REGEX)[0] if html5?
'<!DOCTYPE html>'
unless html4?
if version == "1.1"
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
end
case type
when "strict"; return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
when "frameset"; return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
else return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
end
else else
case type version, type = text.scan(DOCTYPE_REGEX)[0]
when "strict"; return '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
when "frameset"; return '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">' if xhtml?
else return '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">' if version == "1.1"
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
else
case type
when "strict"; '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
when "frameset"; '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
else '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
end
end
elsif html4?
case type
when "strict"; '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
when "frameset"; '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'
else '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
end
end end
end end
end end

View File

@ -420,11 +420,16 @@ class EngineTest < Test::Unit::TestCase
end end
def test_html_ignores_xml_prolog_declaration def test_html_ignores_xml_prolog_declaration
assert_equal "\n", render('!!! XML', :output => :html4) assert_equal "", render('!!! XML', :output => :html4)
end end
def test_html_has_different_doctype def test_html_has_different_doctype
assert_equal %{<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n}, assert_equal %{<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n},
render('!!!', :output => :html4) render('!!!', :output => :html4)
end end
# because anything before the doctype triggers quirks mode in IE
def test_xml_prolog_and_doctype_dont_result_in_a_leading_whitespace_in_html
assert_no_match /^\s+/, render("!!! xml\n!!!", :output => :html4)
end
end end