aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Edmondson <mark.edmondson@gmail.com>2019-01-18 15:56:12 -0800
committerMark Edmondson <mark.edmondson@gmail.com>2019-01-25 10:04:23 -0800
commitf8696b888e6ac2984927a074de60b2c5bad9bc59 (patch)
tree070b1154b993bdf4f6957e2399324f9dfe3bb95e
parent3c6cfdf7adf4848976b7f425b1811d447b130f6a (diff)
downloadrails-f8696b888e6ac2984927a074de60b2c5bad9bc59.tar.gz
rails-f8696b888e6ac2984927a074de60b2c5bad9bc59.tar.bz2
rails-f8696b888e6ac2984927a074de60b2c5bad9bc59.zip
Fix unique DOM IDs for collection inputs
-rw-r--r--actionview/CHANGELOG.md5
-rw-r--r--actionview/lib/action_view/helpers/tags/base.rb2
-rw-r--r--actionview/test/template/form_collections_helper_test.rb24
3 files changed, 26 insertions, 5 deletions
diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md
index 9e47d81faf..ac8cefa2f8 100644
--- a/actionview/CHANGELOG.md
+++ b/actionview/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Ensure unique DOM IDs for collection inputs with float values.
+ Fixes #34974
+
+ *Mark Edmondson*
+
## Rails 6.0.0.beta1 (January 18, 2019) ##
* Remove deprecated `image_alt` helper.
diff --git a/actionview/lib/action_view/helpers/tags/base.rb b/actionview/lib/action_view/helpers/tags/base.rb
index eef527d36f..0adecf362a 100644
--- a/actionview/lib/action_view/helpers/tags/base.rb
+++ b/actionview/lib/action_view/helpers/tags/base.rb
@@ -138,7 +138,7 @@ module ActionView
end
def sanitized_value(value)
- value.to_s.gsub(/\s/, "_").gsub(/[^-[[:word:]]]/, "").mb_chars.downcase.to_s
+ value.to_s.gsub(/[\s\.]/, "_").gsub(/[^-[[:word:]]]/, "").mb_chars.downcase.to_s
end
def select_content_tag(option_tags, options, html_options)
diff --git a/actionview/test/template/form_collections_helper_test.rb b/actionview/test/template/form_collections_helper_test.rb
index 6db55a1447..ca117d4a30 100644
--- a/actionview/test/template/form_collections_helper_test.rb
+++ b/actionview/test/template/form_collections_helper_test.rb
@@ -48,8 +48,16 @@ class FormCollectionsHelperTest < ActionView::TestCase
test "collection radio should sanitize collection values for labels correctly" do
with_collection_radio_buttons :user, :name, ["$0.99", "$1.99"], :to_s, :to_s
- assert_select "label[for=user_name_099]", "$0.99"
- assert_select "label[for=user_name_199]", "$1.99"
+ assert_select "label[for=user_name_0_99]", "$0.99"
+ assert_select "label[for=user_name_1_99]", "$1.99"
+ end
+
+ test "collection radio correctly builds unique DOM IDs for float values" do
+ with_collection_radio_buttons :user, :name, [1.0, 10], :to_s, :to_s
+ assert_select "label[for=user_name_1_0]", "1.0"
+ assert_select "label[for=user_name_10]", "10"
+ assert_select 'input#user_name_1_0[type=radio][value="1.0"]'
+ assert_select 'input#user_name_10[type=radio][value="10"]'
end
test "collection radio accepts checked item" do
@@ -302,8 +310,16 @@ class FormCollectionsHelperTest < ActionView::TestCase
test "collection check box should sanitize collection values for labels correctly" do
with_collection_check_boxes :user, :name, ["$0.99", "$1.99"], :to_s, :to_s
- assert_select "label[for=user_name_099]", "$0.99"
- assert_select "label[for=user_name_199]", "$1.99"
+ assert_select "label[for=user_name_0_99]", "$0.99"
+ assert_select "label[for=user_name_1_99]", "$1.99"
+ end
+
+ test "collection check boxes correctly builds unique DOM IDs for float values" do
+ with_collection_check_boxes :user, :name, [1.0, 10], :to_s, :to_s
+ assert_select "label[for=user_name_1_0]", "1.0"
+ assert_select "label[for=user_name_10]", "10"
+ assert_select 'input#user_name_1_0[type=checkbox][value="1.0"]'
+ assert_select 'input#user_name_10[type=checkbox][value="10"]'
end
test "collection check boxes generates labels for non-English values correctly" do