提交 a794fd2c 编写于 作者: R Rafael Mendonça França

Merge pull request #16796 from ARIATeam/aria

Add support for ARIA attributes in tags
* Add support for ARIA attributes in tags.
Example:
<%= f.text_field :name, aria: { required: "true", hidden: "false" } %>
now generates:
<input aria-hidden="false" aria-required="true" id="user_name" name="user[name]" type="text">
*Paola Garcia Casadiego*
* Provide a `builder` object when using the `label` form helper in block form.
The new `builder` object responds to `translation`, allowing I18n fallback support
......
......@@ -148,9 +148,9 @@ def tag_options(options, escape = true)
return if options.blank?
attrs = []
options.each_pair do |key, value|
if key.to_s == 'data' && value.is_a?(Hash)
if (key.to_s == 'data' || key.to_s == 'aria') && value.is_a?(Hash)
value.each_pair do |k, v|
attrs << data_tag_option(k, v, escape)
attrs << prefix_tag_option(key, k, v, escape)
end
elsif BOOLEAN_ATTRIBUTES.include?(key)
attrs << boolean_tag_option(key) if value
......@@ -161,8 +161,8 @@ def tag_options(options, escape = true)
" #{attrs.sort! * ' '}" unless attrs.empty?
end
def data_tag_option(key, value, escape)
key = "data-#{key.to_s.dasherize}"
def prefix_tag_option(prefix, key, value, escape)
key = "#{prefix}-#{key.to_s.dasherize}"
unless value.is_a?(String) || value.is_a?(Symbol) || value.is_a?(BigDecimal)
value = value.to_json
end
......
......@@ -156,4 +156,11 @@ def test_data_attributes
tag('a', { data => { a_float: 3.14, a_big_decimal: BigDecimal.new("-123.456"), a_number: 1, string: 'hello', symbol: :foo, array: [1, 2, 3], hash: { key: 'value'}, string_with_quotes: 'double"quote"party"' } })
}
end
def test_aria_attributes
['aria', :aria].each { |aria|
assert_dom_equal '<a aria-a-float="3.14" aria-a-big-decimal="-123.456" aria-a-number="1" aria-array="[1,2,3]" aria-hash="{&quot;key&quot;:&quot;value&quot;}" aria-string-with-quotes="double&quot;quote&quot;party&quot;" aria-string="hello" aria-symbol="foo" />',
tag('a', { aria => { a_float: 3.14, a_big_decimal: BigDecimal.new("-123.456"), a_number: 1, string: 'hello', symbol: :foo, array: [1, 2, 3], hash: { key: 'value'}, string_with_quotes: 'double"quote"party"' } })
}
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册