From 5e79094fc1dbb62e27cf21c0f18b586a26d5de46 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Sun, 17 Oct 2010 08:18:25 -0500 Subject: HTML5 data attribute helpers [#5825 state:resolved]. --- actionpack/lib/action_view/helpers/tag_helper.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index 1b9e152b4d..b00b7636c3 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -25,9 +25,13 @@ module ActionView # escaping. # # ==== Options - # The +options+ hash is used with attributes with no value like (disabled and - # readonly), which you can give a value of true in the +options+ hash. You can use - # symbols or strings for the attribute names. + # Use +true+ with boolean attributes that can render with no value (like + # +disabled+ and +readonly+). + # + # HTML5 data-* attributes can be set with a single +data+ key and a hash + # value of sub-attributes. Sub-attribute keys will be dasherized. + # + # You can use symbols or strings for the attribute names. # # ==== Examples # tag("br") @@ -44,6 +48,9 @@ module ActionView # # tag("img", { :src => "open & shut.png" }, false, false) # # => + # + # tag("div", { :data => { :name => 'Stephen', :city_state => %w(Chicago IL) } }) + # # =>
def tag(name, options = nil, open = false, escape = true) "<#{name}#{tag_options(options, escape) if options}#{open ? ">" : " />"}".html_safe end @@ -118,7 +125,13 @@ module ActionView unless options.blank? attrs = [] options.each_pair do |key, value| - if BOOLEAN_ATTRIBUTES.include?(key) + if key.to_s == 'data' && value.is_a?(Hash) + value.each do |k, v| + final_v = [String, Symbol].include?(v.class) ? v : v.to_json + final_v = html_escape(final_v) if escape + attrs << %(data-#{k.to_s.dasherize}="#{final_v}") + end + elsif BOOLEAN_ATTRIBUTES.include?(key) attrs << %(#{key}="#{key}") if value elsif !value.nil? final_value = value.is_a?(Array) ? value.join(" ") : value -- cgit v1.2.3