[Haml] Fix a bug with HTML5 doctypes and html2haml.

Closes gh-69
This commit is contained in:
Nathan Weizenbaum 2009-12-18 13:29:32 -08:00
parent 8d363dd4a2
commit abbbae842a
3 changed files with 22 additions and 4 deletions

View File

@ -3,6 +3,10 @@
* Table of contents
{:toc}
## 2.2.17 (Unreleased)
* Fix compilation of HTML5 doctypes when using `html2haml`.
## [2.2.16](http://github.com/nex3/haml/commit/2.2.16)
* Abstract out references to `ActionView::TemplateError`,

View File

@ -132,15 +132,16 @@ module Haml
class ::Hpricot::DocType
# @see Haml::HTML::Node#to_haml
def to_haml(tabs, options)
attrs = public_id.scan(/DTD\s+([^\s]+)\s*([^\s]*)\s*([^\s]*)\s*\/\//)[0]
attrs = public_id.nil? ? ["", "", ""] :
public_id.scan(/DTD\s+([^\s]+)\s*([^\s]*)\s*([^\s]*)\s*\/\//)[0]
if attrs == nil
raise Exception.new("Invalid doctype")
end
type, version, strictness = attrs.map { |a| a.downcase }
if type == "html"
version = "1.0"
strictness = "transitional"
version = ""
strictness = "strict" if strictness == ""
end
if version == "1.0" || version.empty?
@ -151,7 +152,7 @@ module Haml
strictness = nil
end
version = " #{version}" if version
version = " #{version.capitalize}" if version
strictness = " #{strictness.capitalize}" if strictness
"#{tabulate(tabs)}!!!#{version}#{strictness}\n"

View File

@ -8,6 +8,19 @@ class Html2HamlTest < Test::Unit::TestCase
assert_equal '', render('')
end
def test_doctype
assert_equal '!!!', render("<!DOCTYPE html>")
assert_equal '!!! 1.1', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">')
assert_equal '!!! Strict', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">')
assert_equal '!!! Frameset', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">')
assert_equal '!!! Mobile 1.2', render('<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">')
assert_equal '!!! Basic 1.1', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">')
assert_equal '!!!', render('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">')
assert_equal '!!! Strict', render('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">')
assert_equal '!!! Frameset', render('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">')
assert_equal '!!!', render('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">')
end
def test_id_and_class_should_be_removed_from_hash
assert_equal '%span#foo.bar', render('<span id="foo" class="bar"> </span>')
end