mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Automatically preserve Haml textareas as well.
This commit is contained in:
parent
d3ba044529
commit
a7c9aa9d83
5 changed files with 53 additions and 24 deletions
|
@ -78,10 +78,12 @@ module Haml
|
|||
|
||||
# Properly formats the output of a script that was run in the
|
||||
# instance_eval.
|
||||
def push_script(result, flattened, close_tag = nil)
|
||||
def push_script(result, preserve_script, close_tag = nil, preserve_tag = false)
|
||||
tabulation = @real_tabs
|
||||
|
||||
if flattened
|
||||
|
||||
if preserve_tag
|
||||
result = Haml::Helpers.preserve(result)
|
||||
elsif preserve_script
|
||||
result = Haml::Helpers.find_and_preserve(result)
|
||||
end
|
||||
|
||||
|
@ -91,7 +93,7 @@ module Haml
|
|||
result = result[0...-1]
|
||||
end
|
||||
|
||||
if close_tag && (@options[:ugly] || Buffer.one_liner?(result))
|
||||
if close_tag && (@options[:ugly] || Buffer.one_liner?(result) || preserve_tag)
|
||||
@buffer << "#{result}</#{close_tag}>\n"
|
||||
@real_tabs -= 1
|
||||
else
|
||||
|
@ -113,7 +115,7 @@ module Haml
|
|||
|
||||
# Takes the various information about the opening tag for an
|
||||
# element, formats it, and adds it to the buffer.
|
||||
def open_tag(name, atomic, try_one_line, class_id, obj_ref, content, *attributes_hashes)
|
||||
def open_tag(name, atomic, try_one_line, preserve_tag, class_id, obj_ref, content, *attributes_hashes)
|
||||
tabulation = @real_tabs
|
||||
|
||||
attributes = class_id
|
||||
|
@ -125,7 +127,7 @@ module Haml
|
|||
|
||||
if atomic
|
||||
str = " />\n"
|
||||
elsif try_one_line
|
||||
elsif try_one_line || preserve_tag
|
||||
str = ">"
|
||||
else
|
||||
str = ">\n"
|
||||
|
|
|
@ -40,6 +40,23 @@ if defined?(ActionView) and not defined?(Merb::Plugins)
|
|||
alias_method :concat, :concat_with_haml
|
||||
end
|
||||
|
||||
module TagHelper
|
||||
def content_tag_with_haml(name, *args, &block)
|
||||
content = content_tag_without_haml(name, *args, &block)
|
||||
content = Haml::Helpers.preserve content if name.to_s == 'textarea'
|
||||
content
|
||||
end
|
||||
alias_method :content_tag_without_haml, :content_tag
|
||||
alias_method :content_tag, :content_tag_with_haml
|
||||
end
|
||||
|
||||
class InstanceTag
|
||||
# Includes TagHelper
|
||||
|
||||
alias_method :content_tag_without_haml, :content_tag
|
||||
alias_method :content_tag, :content_tag_with_haml
|
||||
end
|
||||
|
||||
module FormTagHelper
|
||||
def form_tag_with_haml(url_for_options = {}, options = {}, *parameters_for_url, &proc)
|
||||
if is_haml?
|
||||
|
@ -61,12 +78,6 @@ if defined?(ActionView) and not defined?(Merb::Plugins)
|
|||
end
|
||||
alias_method :form_tag_without_haml, :form_tag
|
||||
alias_method :form_tag, :form_tag_with_haml
|
||||
|
||||
def text_area_tag_with_haml(*args)
|
||||
preserve text_area_tag_without_haml(*args)
|
||||
end
|
||||
alias_method :text_area_tag_without_haml, :text_area_tag
|
||||
alias_method :text_area_tag, :text_area_tag_with_haml
|
||||
end
|
||||
|
||||
module FormHelper
|
||||
|
|
|
@ -317,15 +317,15 @@ END
|
|||
# Causes <tt>text</tt> to be evaluated in the context of
|
||||
# the scope object and the result to be added to <tt>@buffer</tt>.
|
||||
#
|
||||
# If <tt>flattened</tt> is true, Haml::Helpers#find_and_flatten is run on
|
||||
# If <tt>preserve_script</tt> is true, Haml::Helpers#find_and_flatten is run on
|
||||
# the result before it is added to <tt>@buffer</tt>
|
||||
def push_script(text, flattened, close_tag = nil)
|
||||
def push_script(text, preserve_script, close_tag = nil, preserve_tag = false)
|
||||
flush_merged_text
|
||||
return if options[:suppress_eval]
|
||||
|
||||
push_silent "haml_temp = #{text}"
|
||||
newline true
|
||||
out = "haml_temp = _hamlout.push_script(haml_temp, #{flattened.inspect}, #{close_tag.inspect});"
|
||||
out = "haml_temp = _hamlout.push_script(haml_temp, #{preserve_script.inspect}, #{close_tag.inspect}, #{preserve_tag.inspect});"
|
||||
if @block_opened
|
||||
push_and_tabulate([:loud, out])
|
||||
else
|
||||
|
@ -506,9 +506,11 @@ END
|
|||
|
||||
raise SyntaxError.new("Illegal element: classes and ids must have values.") if attributes =~ /[\.#](\.|#|\z)/
|
||||
|
||||
preserve_tag = (tag_name == 'textarea')
|
||||
|
||||
case action
|
||||
when '/'; atomic = xhtml?
|
||||
when '~'; parse = flattened = true
|
||||
when '~'; parse = preserve_script = true
|
||||
when '='
|
||||
parse = true
|
||||
value = unescape_interpolation(value[1..-1].strip) if value[0] == ?=
|
||||
|
@ -532,10 +534,11 @@ END
|
|||
raise SyntaxError, "Atomic tags can't have content." if atomic && !value.empty?
|
||||
|
||||
atomic ||= !!( !@block_opened && value.empty? && @options[:autoclose].include?(tag_name) )
|
||||
|
||||
if object_ref == "nil" && attributes_hash.nil? && !flattened && (parse || Buffer.one_liner?(value))
|
||||
|
||||
one_liner = Buffer.one_liner?(value) || preserve_tag
|
||||
if object_ref == "nil" && attributes_hash.nil? && !preserve_script && (parse || one_liner)
|
||||
# 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? && one_liner && !parse
|
||||
|
||||
open_tag = prerender_tag(tag_name, atomic, attributes)
|
||||
open_tag << "#{value}</#{tag_name}>" if tag_closed
|
||||
|
@ -547,9 +550,9 @@ END
|
|||
flush_merged_text
|
||||
content = value.empty? || parse ? 'nil' : value.dump
|
||||
attributes_hash = ', ' + attributes_hash if attributes_hash
|
||||
push_silent "_hamlout.open_tag(#{tag_name.inspect}, #{atomic.inspect}, #{(!value.empty?).inspect}, #{attributes.inspect}, #{object_ref}, #{content}#{attributes_hash})"
|
||||
push_silent "_hamlout.open_tag(#{tag_name.inspect}, #{atomic.inspect}, #{(!value.empty?).inspect}, #{preserve_tag.inspect}, #{attributes.inspect}, #{object_ref}, #{content}#{attributes_hash})"
|
||||
end
|
||||
|
||||
|
||||
return if atomic
|
||||
|
||||
if value.empty?
|
||||
|
@ -560,7 +563,7 @@ END
|
|||
|
||||
if parse
|
||||
flush_merged_text
|
||||
push_script(value, flattened, tag_name)
|
||||
push_script(value, preserve_script, tag_name, preserve_tag)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -101,6 +101,14 @@ class EngineTest < Test::Unit::TestCase
|
|||
assert_equal("<p>foo</p>\n<p>bar</p>\n<p>baz</p>\n<p>boom</p>\n", render("%p foo\r%p bar\r\n%p baz\n\r%p boom"))
|
||||
end
|
||||
|
||||
def test_textareas
|
||||
assert_equal("<textarea>Foo
 bar
 baz</textarea>\n",
|
||||
render('%textarea= "Foo\n bar\n baz"'))
|
||||
|
||||
assert_equal("<textarea>#{'a' * 100}</textarea>\n",
|
||||
render("%textarea #{'a' * 100}"))
|
||||
end
|
||||
|
||||
# Options tests
|
||||
|
||||
def test_stop_eval
|
||||
|
|
|
@ -11,10 +11,12 @@ require 'haml/template'
|
|||
|
||||
class HelperTest < Test::Unit::TestCase
|
||||
include Haml::Helpers
|
||||
Post = Struct.new('Post', :body)
|
||||
|
||||
def setup
|
||||
@base = ActionView::Base.new
|
||||
@base.controller = ActionController::Base.new
|
||||
@base.instance_variable_set('@post', Post.new("Foo bar\nbaz"))
|
||||
end
|
||||
|
||||
def render(text, options = {})
|
||||
|
@ -86,9 +88,12 @@ class HelperTest < Test::Unit::TestCase
|
|||
assert_equal(should_be, result)
|
||||
end
|
||||
|
||||
def test_text_area_tag
|
||||
assert_equal("<textarea id=\"body\" name=\"body\">Foo
Bar
 Baz
 Boom</textarea>\n",
|
||||
def test_text_area
|
||||
assert_equal(%(<textarea id="body" name="body">Foo
Bar
 Baz
 Boom</textarea>\n),
|
||||
render('= text_area_tag "body", "Foo\nBar\n Baz\n Boom"', :action_view))
|
||||
|
||||
assert_equal(%(<textarea cols="40" id="post_body" name="post[body]" rows="20">Foo bar
baz</textarea>\n),
|
||||
render('= text_area :post, :body', :action_view))
|
||||
end
|
||||
|
||||
def test_capture_haml
|
||||
|
|
Loading…
Add table
Reference in a new issue