mirror of
https://github.com/github/rails.git
synced 2026-02-01 01:34:57 -05:00
tag_options creates fewer objects
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7512 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -8,6 +8,8 @@ module ActionView
|
||||
module TagHelper
|
||||
include ERB::Util
|
||||
|
||||
BOOLEAN_ATTRIBUTES = Set.new(%w(disabled readonly multiple))
|
||||
|
||||
# Returns an empty HTML tag of type +name+ which by default is XHTML
|
||||
# compliant. Setting +open+ to true will create an open tag compatible
|
||||
# with HTML 4.0 and below. Add HTML attributes by passing an attributes
|
||||
@@ -97,29 +99,28 @@ module ActionView
|
||||
|
||||
private
|
||||
def content_tag_string(name, content, options)
|
||||
tag_options = options ? tag_options(options) : ""
|
||||
tag_options = tag_options(options) if options
|
||||
"<#{name}#{tag_options}>#{content}</#{name}>"
|
||||
end
|
||||
|
||||
|
||||
def tag_options(options)
|
||||
cleaned_options = convert_booleans(options.stringify_keys.reject {|key, value| value.nil?})
|
||||
' ' + cleaned_options.map {|key, value| %(#{key}="#{escape_once(value)}")}.sort * ' ' unless cleaned_options.empty?
|
||||
unless options.blank?
|
||||
attrs = []
|
||||
options.each do |key, value|
|
||||
next unless value
|
||||
key = key.to_s
|
||||
value = BOOLEAN_ATTRIBUTES.include?(key) ? key : escape_once(value)
|
||||
attrs << %(#{key}="#{value}")
|
||||
end
|
||||
" #{attrs.sort * ' '}" unless attrs.empty?
|
||||
end
|
||||
end
|
||||
|
||||
def convert_booleans(options)
|
||||
%w( disabled readonly multiple ).each { |a| boolean_attribute(options, a) }
|
||||
options
|
||||
end
|
||||
|
||||
def boolean_attribute(options, attribute)
|
||||
options[attribute] ? options[attribute] = attribute : options.delete(attribute)
|
||||
end
|
||||
|
||||
# Fix double-escaped entities, such as &amp;, &#123;, etc.
|
||||
def fix_double_escape(escaped)
|
||||
escaped.gsub(/&([a-z]+|(#\d+));/i) { "&#{$1};" }
|
||||
end
|
||||
|
||||
|
||||
def block_is_within_action_view?(block)
|
||||
eval("defined? _erbout", block.binding)
|
||||
end
|
||||
|
||||
@@ -13,7 +13,9 @@ class TagHelperTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_tag_options
|
||||
assert_match /\A<p class="(show|elsewhere)" \/>\z/, tag("p", "class" => "show", :class => "elsewhere")
|
||||
str = tag("p", "class" => "show", :class => "elsewhere")
|
||||
assert_match /class="show"/, str
|
||||
assert_match /class="elsewhere"/, str
|
||||
end
|
||||
|
||||
def test_tag_options_rejects_nil_option
|
||||
|
||||
Reference in New Issue
Block a user