Skip to content

Commit

Permalink
Fix double-escaped entities, such as &, {, etc. [Rick]
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5321 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
technoweenie committed Oct 18, 2006
1 parent a0f7409 commit 02358c8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *SVN*


* Fix double-escaped entities, such as &, {, etc. [Rick]

* Fix deprecation warnings when rendering the template error template. [Nicholas Seckar] * Fix deprecation warnings when rendering the template error template. [Nicholas Seckar]


* Fix routing to correctly determine when generation fails. Closes #6300. [psross]. * Fix routing to correctly determine when generation fails. Closes #6300. [psross].
Expand Down
7 changes: 6 additions & 1 deletion actionpack/lib/action_view/helpers/tag_helper.rb
Expand Up @@ -34,7 +34,7 @@ def cdata_section(content)
private private
def tag_options(options) def tag_options(options)
cleaned_options = convert_booleans(options.stringify_keys.reject {|key, value| value.nil?}) cleaned_options = convert_booleans(options.stringify_keys.reject {|key, value| value.nil?})
' ' + cleaned_options.map {|key, value| %(#{key}="#{html_escape(value.to_s)}")}.sort * ' ' unless cleaned_options.empty? ' ' + cleaned_options.map {|key, value| %(#{key}="#{fix_double_escape(html_escape(value.to_s))}")}.sort * ' ' unless cleaned_options.empty?
end end


def convert_booleans(options) def convert_booleans(options)
Expand All @@ -45,6 +45,11 @@ def convert_booleans(options)
def boolean_attribute(options, attribute) def boolean_attribute(options, attribute)
options[attribute] ? options[attribute] = attribute : options.delete(attribute) options[attribute] ? options[attribute] = attribute : options.delete(attribute)
end end

# Fix double-escaped entities, such as &, {, etc.
def fix_double_escape(escaped)
escaped.gsub(/&([a-z]+|(#\d+));/i) { "&#{$1};" }
end
end end
end end
end end
12 changes: 12 additions & 0 deletions actionpack/test/template/tag_helper_test.rb
Expand Up @@ -38,4 +38,16 @@ def test_content_tag
def test_cdata_section def test_cdata_section
assert_equal "<![CDATA[<hello world>]]>", cdata_section("<hello world>") assert_equal "<![CDATA[<hello world>]]>", cdata_section("<hello world>")
end end

def test_double_escaping_attributes
['1&amp;2', '1 &lt; 2', '&#8220;test&#8220;'].each do |escaped|
assert_equal %(<a href="#{escaped}" />), tag('a', :href => escaped)
end
end

def test_skip_invalid_escaped_attributes
['&1;', '&#1dfa3;', '& #123;'].each do |escaped|
assert_equal %(<a href="#{escaped.gsub /&/, '&amp;'}" />), tag('a', :href => escaped)
end
end
end end

0 comments on commit 02358c8

Please sign in to comment.