提交 e6950a33 编写于 作者: J Jonathan Hefner

Add label attribute to <option> from include_blank

The `:include_blank` option of various `<select>`-related helpers causes
an `<option>` element with no content to be rendered.  However, the
[HTML spec] says that unless an `<option>` element has a `label`
attribute (which must be non-empty), its content must be "Text that is
not inter-element whitespace."

In #24923, this issue was addressed for `select_tag` by adding a `label`
attribute to the `<option>`.  This commit addresses the issue in the
same manner for `FormBuilder#select` and various date / time select
helpers.

[HTML spec]: https://html.spec.whatwg.org/multipage/form-elements.html#the-option-element
上级 8a4192fa
......@@ -1053,7 +1053,7 @@ def build_select(type, select_options_as_html)
select_options[:class] = css_class_attribute(type, select_options[:class], @options[:with_css_classes]) if @options[:with_css_classes]
select_html = +"\n"
select_html << content_tag("option", "", value: "") + "\n" if @options[:include_blank]
select_html << content_tag("option", "", value: "", label: " ") + "\n" if @options[:include_blank]
select_html << prompt_option_tag(type, @options[:prompt]) + "\n" if @options[:prompt]
select_html << select_options_as_html
......
......@@ -21,7 +21,7 @@ module Helpers #:nodoc:
# could become:
#
# <select name="post[category]" id="post_category">
# <option value=""></option>
# <option value="" label=" "></option>
# <option value="joke">joke</option>
# <option value="poem">poem</option>
# </select>
......@@ -74,7 +74,6 @@ module Helpers #:nodoc:
# could become:
#
# <select name="post[category]" id="post_category">
# <option value=""></option>
# <option value="joke">joke</option>
# <option value="poem">poem</option>
# <option disabled="disabled" value="restricted">restricted</option>
......@@ -112,7 +111,7 @@ module FormOptionsHelper
# would become:
#
# <select name="post[person_id]" id="post_person_id">
# <option value=""></option>
# <option value="" label=" "></option>
# <option value="1" selected="selected">David</option>
# <option value="2">Eileen</option>
# <option value="3">Rafael</option>
......
......@@ -166,8 +166,11 @@ def placeholder_required?(html_options)
def add_options(option_tags, options, value = nil)
if options[:include_blank]
option_tags = tag_builder.content_tag_string("option", options[:include_blank].kind_of?(String) ? options[:include_blank] : nil, value: "") + "\n" + option_tags
content = (options[:include_blank] if options[:include_blank].is_a?(String))
label = (" " unless content)
option_tags = tag_builder.content_tag_string("option", content, value: "", label: label) + "\n" + option_tags
end
if value.blank? && options[:prompt]
tag_options = { value: "" }.tap do |prompt_opts|
prompt_opts[:disabled] = true if options[:disabled] == ""
......@@ -175,6 +178,7 @@ def add_options(option_tags, options, value = nil)
end
option_tags = tag_builder.content_tag_string("option", prompt_text(options[:prompt]), tag_options) + "\n" + option_tags
end
option_tags
end
......
......@@ -726,7 +726,7 @@ def test_select_with_blank
@post = Post.new
@post.category = "<mus>"
assert_dom_equal(
"<select id=\"post_category\" name=\"post[category]\"><option value=\"\"></option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
"<select id=\"post_category\" name=\"post[category]\"><option value=\"\" label=\" \"></option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\" selected=\"selected\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
select("post", "category", %w( abe <mus> hest), include_blank: true)
)
end
......@@ -795,7 +795,7 @@ def test_select_with_prompt_and_blank
@post = Post.new
@post.category = ""
assert_dom_equal(
"<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"\"></option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
"<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"\" label=\" \"></option>\n<option value=\"abe\">abe</option>\n<option value=\"&lt;mus&gt;\">&lt;mus&gt;</option>\n<option value=\"hest\">hest</option></select>",
select("post", "category", %w( abe <mus> hest), prompt: true, include_blank: true)
)
end
......@@ -804,7 +804,7 @@ def test_select_with_empty
@post = Post.new
@post.category = ""
assert_dom_equal(
"<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"\"></option>\n</select>",
"<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"\" label=\" \"></option>\n</select>",
select("post", "category", [], prompt: true, include_blank: true)
)
end
......@@ -813,7 +813,7 @@ def test_select_with_html_options
@post = Post.new
@post.category = ""
assert_dom_equal(
"<select class=\"disabled\" disabled=\"disabled\" name=\"post[category]\" id=\"post_category\"><option value=\"\">Please select</option>\n<option value=\"\"></option>\n</select>",
"<select class=\"disabled\" disabled=\"disabled\" name=\"post[category]\" id=\"post_category\"><option value=\"\">Please select</option>\n<option value=\"\" label=\" \"></option>\n</select>",
select("post", "category", [], { prompt: true, include_blank: true }, { class: "disabled", disabled: true })
)
end
......@@ -847,7 +847,7 @@ def test_select_with_nil_and_selected_option_as_nil
def test_required_select
assert_dom_equal(
%(<select id="post_category" name="post[category]" required="required"><option value=""></option>\n<option value="abe">abe</option>\n<option value="mus">mus</option>\n<option value="hest">hest</option></select>),
%(<select id="post_category" name="post[category]" required="required"><option value="" label=" "></option>\n<option value="abe">abe</option>\n<option value="mus">mus</option>\n<option value="hest">hest</option></select>),
select("post", "category", %w(abe mus hest), {}, { required: true })
)
end
......@@ -868,7 +868,7 @@ def test_required_select_with_prompt
def test_required_select_display_size_equals_to_one
assert_dom_equal(
%(<select id="post_category" name="post[category]" required="required" size="1"><option value=""></option>\n<option value="abe">abe</option>\n<option value="mus">mus</option>\n<option value="hest">hest</option></select>),
%(<select id="post_category" name="post[category]" required="required" size="1"><option value="" label=" "></option>\n<option value="abe">abe</option>\n<option value="mus">mus</option>\n<option value="hest">hest</option></select>),
select("post", "category", %w(abe mus hest), {}, { required: true, size: 1 })
)
end
......@@ -891,7 +891,7 @@ def test_select_with_integer
@post = Post.new
@post.category = ""
assert_dom_equal(
"<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"\"></option>\n<option value=\"1\">1</option></select>",
"<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"\" label=\" \"></option>\n<option value=\"1\">1</option></select>",
select("post", "category", [1], prompt: true, include_blank: true)
)
end
......@@ -900,7 +900,7 @@ def test_list_of_lists
@post = Post.new
@post.category = ""
assert_dom_equal(
"<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"\"></option>\n<option value=\"number\">Number</option>\n<option value=\"text\">Text</option>\n<option value=\"boolean\">Yes/No</option></select>",
"<select id=\"post_category\" name=\"post[category]\"><option value=\"\">Please select</option>\n<option value=\"\" label=\" \"></option>\n<option value=\"number\">Number</option>\n<option value=\"text\">Text</option>\n<option value=\"boolean\">Yes/No</option></select>",
select("post", "category", [["Number", "number"], ["Text", "text"], ["Yes/No", "boolean"]], prompt: true, include_blank: true)
)
end
......@@ -1051,7 +1051,7 @@ def test_collection_select_with_blank_and_style
@post.author_name = "Babe"
assert_dom_equal(
"<select id=\"post_author_name\" name=\"post[author_name]\" style=\"width: 200px\"><option value=\"\"></option>\n<option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
"<select id=\"post_author_name\" name=\"post[author_name]\" style=\"width: 200px\"><option value=\"\" label=\" \"></option>\n<option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>",
collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { include_blank: true }, { "style" => "width: 200px" })
)
end
......@@ -1070,7 +1070,7 @@ def test_collection_select_with_multiple_option_appends_array_brackets_and_hidde
@post = Post.new
@post.author_name = "Babe"
expected = "<input type=\"hidden\" name=\"post[author_name][]\" value=\"\"/><select id=\"post_author_name\" name=\"post[author_name][]\" multiple=\"multiple\"><option value=\"\"></option>\n<option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>"
expected = "<input type=\"hidden\" name=\"post[author_name][]\" value=\"\"/><select id=\"post_author_name\" name=\"post[author_name][]\" multiple=\"multiple\"><option value=\"\" label=\" \"></option>\n<option value=\"&lt;Abe&gt;\">&lt;Abe&gt;</option>\n<option value=\"Babe\" selected=\"selected\">Babe</option>\n<option value=\"Cabe\">Cabe</option></select>"
# Should suffix default name with [].
assert_dom_equal expected, collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { include_blank: true }, { multiple: true })
......@@ -1084,7 +1084,7 @@ def test_collection_select_with_blank_and_selected
@post.author_name = "Babe"
assert_dom_equal(
%{<select id="post_author_name" name="post[author_name]"><option value=""></option>\n<option value="&lt;Abe&gt;" selected="selected">&lt;Abe&gt;</option>\n<option value="Babe">Babe</option>\n<option value="Cabe">Cabe</option></select>},
%{<select id="post_author_name" name="post[author_name]"><option value="" label=" "></option>\n<option value="&lt;Abe&gt;" selected="selected">&lt;Abe&gt;</option>\n<option value="Babe">Babe</option>\n<option value="Cabe">Cabe</option></select>},
collection_select("post", "author_name", dummy_posts, "author_name", "author_name", include_blank: true, selected: "<Abe>")
)
end
......@@ -1192,7 +1192,7 @@ def test_time_zone_select_with_blank
@firm = Firm.new("D")
html = time_zone_select("firm", "time_zone", nil, include_blank: true)
assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\">" \
"<option value=\"\"></option>\n" \
"<option value=\"\" label=\" \"></option>\n" \
"<option value=\"A\">A</option>\n" \
"<option value=\"B\">B</option>\n" \
"<option value=\"C\">C</option>\n" \
......@@ -1237,7 +1237,7 @@ def test_time_zone_select_with_blank_and_style
html = time_zone_select("firm", "time_zone", nil,
{ include_blank: true }, { "style" => "color: red" })
assert_dom_equal "<select id=\"firm_time_zone\" name=\"firm[time_zone]\" style=\"color: red\">" \
"<option value=\"\"></option>\n" \
"<option value=\"\" label=\" \"></option>\n" \
"<option value=\"A\">A</option>\n" \
"<option value=\"B\">B</option>\n" \
"<option value=\"C\">C</option>\n" \
......
......@@ -792,7 +792,7 @@ If `@article.person_id` is 1, this would become:
```html
<select name="article[person_id]">
<option value=""></option>
<option value="" label=" "></option>
<option value="1" selected="selected">David</option>
<option value="2">Eileen</option>
<option value="3">Rafael</option>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册