mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Updated code and added tests for HTML-generation option.
Signed-off-by: Nathan Weizenbaum <nex342@gmail.com>
This commit is contained in:
parent
45a8bdf68c
commit
eb0ff140ea
3 changed files with 36 additions and 18 deletions
|
@ -37,7 +37,7 @@ module Haml
|
||||||
@options = {
|
@options = {
|
||||||
:suppress_eval => false,
|
:suppress_eval => false,
|
||||||
:attr_wrapper => "'",
|
:attr_wrapper => "'",
|
||||||
:autoclose => ['meta', 'img', 'link', 'br', 'hr', 'input', 'area'],
|
:autoclose => %w(meta img link br hr input area param col base),
|
||||||
:filters => {
|
:filters => {
|
||||||
'sass' => Haml::Filters::Sass,
|
'sass' => Haml::Filters::Sass,
|
||||||
'plain' => Haml::Filters::Plain,
|
'plain' => Haml::Filters::Plain,
|
||||||
|
@ -48,7 +48,7 @@ module Haml
|
||||||
'markdown' => Haml::Filters::Markdown },
|
'markdown' => Haml::Filters::Markdown },
|
||||||
:filename => '(haml)',
|
:filename => '(haml)',
|
||||||
:ugly => false,
|
:ugly => false,
|
||||||
:output => :xhtml
|
:html4 => false
|
||||||
}
|
}
|
||||||
@options.rec_merge! options
|
@options.rec_merge! options
|
||||||
|
|
||||||
|
|
|
@ -474,7 +474,8 @@ END
|
||||||
end
|
end
|
||||||
|
|
||||||
def prerender_tag(name, self_close, attributes)
|
def prerender_tag(name, self_close, attributes)
|
||||||
"<#{name}#{Precompiler.build_attributes(@options[:attr_wrapper], attributes)}#{self_close ? ' />' : '>'}"
|
attributes_string = Precompiler.build_attributes(@options[:attr_wrapper], attributes)
|
||||||
|
"<#{name}#{attributes_string}#{self_close && !@options[:html4] ? ' /' : ''}>"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Parses a line into tag_name, attributes, attributes_hash, object_ref, action, value
|
# Parses a line into tag_name, attributes, attributes_hash, object_ref, action, value
|
||||||
|
@ -502,7 +503,7 @@ END
|
||||||
raise SyntaxError.new("Illegal element: classes and ids must have values.") if attributes =~ /[\.#](\.|#|\z)/
|
raise SyntaxError.new("Illegal element: classes and ids must have values.") if attributes =~ /[\.#](\.|#|\z)/
|
||||||
|
|
||||||
case action
|
case action
|
||||||
when '/'; atomic = true
|
when '/'; atomic = !@options[:html4]
|
||||||
when '~'; parse = flattened = true
|
when '~'; parse = flattened = true
|
||||||
when '='
|
when '='
|
||||||
parse = true
|
parse = true
|
||||||
|
@ -521,18 +522,18 @@ END
|
||||||
attributes = parse_class_and_id(attributes)
|
attributes = parse_class_and_id(attributes)
|
||||||
Buffer.merge_attrs(attributes, static_attributes) if static_attributes
|
Buffer.merge_attrs(attributes, static_attributes) if static_attributes
|
||||||
|
|
||||||
raise SyntaxError.new("Illegal Nesting: Nesting within an atomic tag is illegal.") if @block_opened && atomic
|
raise SyntaxError, "Illegal Nesting: Nesting within an atomic tag is illegal." if @block_opened && atomic
|
||||||
raise SyntaxError.new("Illegal Nesting: Content can't be both given on the same line as %#{tag_name} and nested within it.") if @block_opened && !value.empty?
|
raise SyntaxError, "Illegal Nesting: Content can't be both given on the same line as %#{tag_name} and nested within it." if @block_opened && !value.empty?
|
||||||
raise SyntaxError.new("Tag has no content.") if parse && value.empty?
|
raise SyntaxError, "Tag has no content." if parse && value.empty?
|
||||||
raise SyntaxError.new("Atomic tags can't have content.") if atomic && !value.empty?
|
raise SyntaxError, "Atomic tags can't have content." if atomic && !value.empty?
|
||||||
|
|
||||||
atomic = true if !@block_opened && value.empty? && @options[:autoclose].include?(tag_name)
|
atomic ||= !!( !@block_opened && value.empty? && @options[:autoclose].include?(tag_name) )
|
||||||
|
|
||||||
if object_ref == "nil" && attributes_hash.nil? && !flattened && (parse || Buffer.one_liner?(value))
|
if object_ref == "nil" && attributes_hash.nil? && !flattened && (parse || Buffer.one_liner?(value))
|
||||||
# This means that we can render the tag directly to text and not process it in the buffer
|
# This means that we can render the tag directly to text and not process it in the buffer
|
||||||
tag_closed = !value.empty? && Buffer.one_liner?(value) && !parse
|
tag_closed = !value.empty? && Buffer.one_liner?(value) && !parse
|
||||||
|
|
||||||
open_tag = prerender_tag(tag_name, atomic && options[:output] == :xhtml, attributes)
|
open_tag = prerender_tag(tag_name, atomic, attributes)
|
||||||
open_tag << "#{value}</#{tag_name}>" if tag_closed
|
open_tag << "#{value}</#{tag_name}>" if tag_closed
|
||||||
open_tag << "\n" unless parse
|
open_tag << "\n" unless parse
|
||||||
|
|
||||||
|
@ -600,13 +601,14 @@ 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[0...3] == "xml"
|
||||||
raise SyntaxError.new("XML prolog only valid for XHTML documents.") unless options[:output] == :xhtml
|
return nil if @options[:html4]
|
||||||
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]
|
version, type = text.scan(DOCTYPE_REGEX)[0]
|
||||||
if @options[:output] == :xhtml
|
|
||||||
|
unless @options[:html4]
|
||||||
if version == "1.1"
|
if version == "1.1"
|
||||||
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
|
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
|
||||||
end
|
end
|
||||||
|
|
|
@ -394,12 +394,28 @@ class EngineTest < Test::Unit::TestCase
|
||||||
assert_equal("<p>#{'s' * 75}</p>\n",
|
assert_equal("<p>#{'s' * 75}</p>\n",
|
||||||
render("%p= 's' * 75", :ugly => true))
|
render("%p= 's' * 75", :ugly => true))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_renders_correct_html_or_xhtml
|
|
||||||
assert_equal("<br>\n", render("%br", :output => :html))
|
|
||||||
assert_equal("<br />\n", render("%br"))
|
|
||||||
|
|
||||||
assert_raise(Haml::SyntaxError) { render("!!! XML", :output => :html) }
|
# HTML 4.0
|
||||||
assert_nothing_raised { render("!!! XML") }
|
|
||||||
|
def test_html_has_no_self_closing_tags
|
||||||
|
assert_equal "<p>\n <br>\n</p>\n", render("%p\n %br", :html4 => :true)
|
||||||
|
assert_equal "<br>\n", render("%br/", :html4 => :true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_html_renders_empty_node_with_closing_tag
|
||||||
|
assert_equal %{<div class='foo'>\n</div>\n}, render(".foo", :html4 => :true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_html_ignores_explicit_self_closing_declaration
|
||||||
|
assert_equal "<a>\n</a>\n", render("%a/", :html4 => :true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_html_ignores_xml_prolog_declaration
|
||||||
|
assert_equal "\n", render('!!! XML', :html4 => :true)
|
||||||
|
end
|
||||||
|
|
||||||
|
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},
|
||||||
|
render('!!!', :html4 => :true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue