Skip to content

Commit

Permalink
Don't break Haml with textarea newline fix.
Browse files Browse the repository at this point in the history
See issue rails#393, issue rails#4000, issue rails#5190, and issue rails#5191. Adds a newline after the textarea opening tag based on @codykrieger's original patch so that we don't cause regressions in Haml-using apps. The regression caused textarea tags to add newlines to the field unintentionally (each update/save added an extra newline.)

Also fix 6 more tests that didn't yet have the newline expectation.
  • Loading branch information
jcoleman committed Mar 27, 2012
1 parent e96d04a commit 1438e0e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
6 changes: 5 additions & 1 deletion actionpack/lib/action_view/helpers/tag_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ module TagHelper
autofocus novalidate formnovalidate open pubdate).to_set
BOOLEAN_ATTRIBUTES.merge(BOOLEAN_ATTRIBUTES.map {|attribute| attribute.to_sym })

PRE_CONTENT_STRINGS = {
:textarea => "\n"
}

# Returns an empty HTML tag of type +name+ which by default is XHTML
# compliant. Set +open+ to true to create an open tag compatible
# with HTML 4.0 and below. Add HTML attributes by passing an attributes
Expand Down Expand Up @@ -126,7 +130,7 @@ def escape_once(html)
def content_tag_string(name, content, options, escape = true)
tag_options = tag_options(options, escape) if options
content = ERB::Util.h(content) if escape
"<#{name}#{tag_options}>#{content}</#{name}>".html_safe
"<#{name}#{tag_options}>#{PRE_CONTENT_STRINGS[name.to_sym]}#{content}</#{name}>".html_safe
end

def tag_options(options, escape = true)
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/tags/text_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def render
options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split)
end

content_tag("textarea", "\n#{options.delete('value') || value_before_type_cast(object)}", options)
content_tag("textarea", options.delete('value') || value_before_type_cast(object), options)
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions actionpack/test/template/form_tag_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,19 @@ def test_select_tag_with_prompt_and_include_blank

def test_text_area_tag_size_string
actual = text_area_tag "body", "hello world", "size" => "20x40"
expected = %(<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>)
expected = %(<textarea cols="20" id="body" name="body" rows="40">\nhello world</textarea>)
assert_dom_equal expected, actual
end

def test_text_area_tag_size_symbol
actual = text_area_tag "body", "hello world", :size => "20x40"
expected = %(<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>)
expected = %(<textarea cols="20" id="body" name="body" rows="40">\nhello world</textarea>)
assert_dom_equal expected, actual
end

def test_text_area_tag_should_disregard_size_if_its_given_as_an_integer
actual = text_area_tag "body", "hello world", :size => 20
expected = %(<textarea id="body" name="body">hello world</textarea>)
expected = %(<textarea id="body" name="body">\nhello world</textarea>)
assert_dom_equal expected, actual
end

Expand All @@ -245,19 +245,19 @@ def test_text_area_tag_id_sanitized

def test_text_area_tag_escape_content
actual = text_area_tag "body", "<b>hello world</b>", :size => "20x40"
expected = %(<textarea cols="20" id="body" name="body" rows="40">&lt;b&gt;hello world&lt;/b&gt;</textarea>)
expected = %(<textarea cols="20" id="body" name="body" rows="40">\n&lt;b&gt;hello world&lt;/b&gt;</textarea>)
assert_dom_equal expected, actual
end

def test_text_area_tag_unescaped_content
actual = text_area_tag "body", "<b>hello world</b>", :size => "20x40", :escape => false
expected = %(<textarea cols="20" id="body" name="body" rows="40"><b>hello world</b></textarea>)
expected = %(<textarea cols="20" id="body" name="body" rows="40">\n<b>hello world</b></textarea>)
assert_dom_equal expected, actual
end

def test_text_area_tag_unescaped_nil_content
actual = text_area_tag "body", nil, :escape => false
expected = %(<textarea id="body" name="body"></textarea>)
expected = %(<textarea id="body" name="body">\n</textarea>)
assert_dom_equal expected, actual
end

Expand Down

0 comments on commit 1438e0e

Please sign in to comment.