Permalink
Browse files

Made tag and content_tag a little more HTML 5 friendly

  • Loading branch information...
1 parent c44aa1b commit 653e57bab171ac08495db55bf50b7c91b7758fd1 Benjamin Bloch committed Jan 19, 2012
@@ -153,7 +153,7 @@ def button_to(*args, &block)
# @api public
def feed_tag(mime, url, options={})
full_mime = (mime == :atom) ? 'application/atom+xml' : 'application/rss+xml'
- content_tag(:link, options.reverse_merge(:rel => 'alternate', :type => full_mime, :title => mime, :href => url))
+ tag(:link, options.reverse_merge(:rel => 'alternate', :type => full_mime, :title => mime, :href => url))
end
##
@@ -291,9 +291,9 @@ def stylesheet_link_tag(*sources)
# @api public
def javascript_include_tag(*sources)
options = sources.extract_options!.symbolize_keys
- options.reverse_merge!(:type => 'text/javascript', :content => "")
+ options.reverse_merge!(:type => 'text/javascript')
sources.flatten.map { |source|
- tag(:script, options.reverse_merge(:src => asset_path(:js, source)))
+ content_tag(:script, nil, options.reverse_merge(:src => asset_path(:js, source)))
}.join("\n")
end
@@ -81,7 +81,7 @@ def strip_tags(html)
# @api public
def simple_format(text, options={})
t = options.delete(:tag) || :p
- start_tag = tag(t, options.merge(:open => true))
+ start_tag = tag(t, options, true)
text = text.to_s.dup
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
text.gsub!(/\n\n+/, "</#{t}>\n\n#{start_tag}") # 2+ newline -> paragraph
View
@@ -13,6 +13,21 @@ module TagHelpers
'"' => "&quot;"
}
+ BOOLEAN_ATTRIBUTES = [
+ :autoplay,
+ :autofocus,
+ :autobuffer,
+ :checked,
+ :disabled,
+ :hidden,
+ :loop,
+ :multiple,
+ :muted,
+ :readonly,
+ :required,
+ :selected,
+ ]
+
##
# Creates an html tag with given name, content and options
#
@@ -32,12 +47,16 @@ module TagHelpers
# content_tag(:p, :class => 'dark') { ... }
#
# @api public
- def content_tag(*args, &block)
- name = args.first
- options = args.extract_options!
- tag_html = block_given? ? capture_html(&block) : args[1]
- tag_result = tag(name, options.merge(:content => tag_html))
- block_is_template?(block) ? concat_content(tag_result) : tag_result
+ def content_tag(name, content = nil, options = nil, &block)
+ if block_given?
+ options = content if content.is_a?(Hash)
+ content = capture_html(&block)
+ end
+
+ content = content.join("\n") if content.respond_to?(:join)
+
+ output = "<#{name}#{tag_options(options) if options}>#{content}</#{name}>"
+ block_is_template?(block) ? concat_content(output) : output
end
##
@@ -72,24 +91,30 @@ def input_tag(type, options = {})
#
# @example
# tag(:br, :style => 'clear:both')
- # tag(:p, :content => "hello", :class => 'large')
#
# @api public
- def tag(name, options={})
- content, open_tag = options.delete(:content), options.delete(:open)
- content = content.join("\n") if content.respond_to?(:join)
- identity_tag_attributes.each { |attr| options[attr] = attr.to_s if options[attr] }
- html_attrs = options.map { |a, v| v.nil? || v == false ? nil : "#{a}=\"#{escape_value(v)}\"" }.compact.join(" ")
- base_tag = (html_attrs.present? ? "<#{name} #{html_attrs}" : "<#{name}")
- base_tag << (open_tag ? ">" : (content ? ">#{content}</#{name}>" : " />"))
+ def tag(name, options = nil, open = false)
+ "<#{name}#{tag_options(options) if options}#{open ? '>' : ' />'}"
end
private
##
- # Returns a list of attributes which can only contain an identity value (i.e selected)
+ # Returns a compiled list of HTML attributes
#
- def identity_tag_attributes
- [:checked, :disabled, :selected, :multiple]
+ def tag_options(options)
+ unless options.blank?
+ attributes = []
+ options.each do |attribute, value|
+ next if value.nil? || value == false
+ value = attribute if BOOLEAN_ATTRIBUTES.include?(attribute)
+ if attribute == :data && value.is_a?(Hash)
+ value.each { |k, v| attributes << %[data-#{k.to_s.dasherize}="#{escape_value(v)}"] }
+ else
+ attributes << %[#{attribute}="#{escape_value(value)}"]
+ end
+ end
+ " #{attributes.join(' ')}"
+ end
end
##
View
@@ -21,17 +21,13 @@ def app
assert_has_tag('option', :selected => 'selected') { actual_html }
end
- should "support tags with content no attributes" do
- assert_has_tag(:p, :content => "Demo String") { tag(:p, :content => "Demo String") }
- end
-
- should "support tags with content and attributes" do
- actual_html = tag(:p, :content => "Demo", :class => 'large', :id => 'intro')
- assert_has_tag('p#intro.large', :content => "Demo") { actual_html }
+ should "support data attributes" do
+ actual_html = tag(:a, :data => { :remote => true, :method => 'post'})
+ assert_has_tag(:a, 'data-remote' => 'true', 'data-method' => 'post') { actual_html }
end
should "support open tags" do
- actual_html = tag(:p, :class => 'demo', :open => true)
+ actual_html = tag(:p, { :class => 'demo' }, true)
assert_equal "<p class=\"demo\">", actual_html
end

0 comments on commit 653e57b

Please sign in to comment.