Skip to content

Commit aa449a8

Browse files
authored
Merge pull request rails#43280 from ryanfb/hidden_autocomplete_off
Add autocomplete="off" to all generated hidden fields (fixes rails#42610)
2 parents b2db4c7 + 58127ec commit aa449a8

16 files changed

+339
-333
lines changed

actiontext/test/template/form_helper_test.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def form_with(*, **)
3434

3535
assert_dom_equal \
3636
'<form action="/messages" accept-charset="UTF-8" method="post">' \
37-
'<input type="hidden" name="content" id="trix_input_1" />' \
37+
'<input type="hidden" name="content" id="trix_input_1" autocomplete="off" />' \
3838
'<trix-editor input="trix_input_1" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
3939
"</trix-editor>" \
4040
"</form>",
@@ -48,7 +48,7 @@ def form_with(*, **)
4848

4949
assert_dom_equal \
5050
'<form action="/messages" accept-charset="UTF-8" method="post">' \
51-
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
51+
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" autocomplete="off" />' \
5252
'<trix-editor id="message_content" input="message_content_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
5353
"</trix-editor>" \
5454
"</form>",
@@ -62,7 +62,7 @@ def form_with(*, **)
6262

6363
assert_dom_equal \
6464
'<form action="/messages" accept-charset="UTF-8" method="post">' \
65-
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
65+
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" autocomplete="off" />' \
6666
'<trix-editor id="message_content" input="message_content_trix_input_message" class="custom-class" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
6767
"</trix-editor>" \
6868
"</form>",
@@ -76,7 +76,7 @@ def form_with(*, **)
7676

7777
assert_dom_equal \
7878
'<form action="/messages" accept-charset="UTF-8" method="post">' \
79-
'<input type="hidden" name="message[not_an_attribute]" id="message_not_an_attribute_trix_input_message" />' \
79+
'<input type="hidden" name="message[not_an_attribute]" id="message_not_an_attribute_trix_input_message" autocomplete="off" />' \
8080
'<trix-editor id="message_not_an_attribute" input="message_not_an_attribute_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
8181
"</trix-editor>" \
8282
"</form>",
@@ -90,7 +90,7 @@ def form_with(*, **)
9090

9191
assert_dom_equal \
9292
'<form action="/messages" accept-charset="UTF-8" method="post">' \
93-
'<input type="hidden" name="message[content]" id="trix_input_2" />' \
93+
'<input type="hidden" name="message[content]" id="trix_input_2" autocomplete="off" />' \
9494
'<trix-editor id="message_content" input="trix_input_2" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
9595
"</trix-editor>" \
9696
"</form>",
@@ -104,7 +104,7 @@ def form_with(*, **)
104104

105105
assert_dom_equal \
106106
'<form action="/messages" accept-charset="UTF-8" method="post">' \
107-
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
107+
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" autocomplete="off" />' \
108108
'<trix-editor placeholder="Content" id="message_content" input="message_content_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
109109
"</trix-editor>" \
110110
"</form>",
@@ -120,7 +120,7 @@ def form_with(*, **)
120120

121121
assert_dom_equal \
122122
'<form action="/messages" accept-charset="UTF-8" method="post">' \
123-
'<input type="hidden" name="message[title]" id="message_title_trix_input_message" />' \
123+
'<input type="hidden" name="message[title]" id="message_title_trix_input_message" autocomplete="off" />' \
124124
'<trix-editor placeholder="Story title" id="message_title" input="message_title_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
125125
"</trix-editor>" \
126126
"</form>",
@@ -134,7 +134,7 @@ def form_with(*, **)
134134

135135
assert_dom_equal \
136136
'<form action="/messages" accept-charset="UTF-8" method="post">' \
137-
'<input type="hidden" name="message[title]" id="message_title_trix_input_message" value="&lt;h1&gt;hello world&lt;/h1&gt;" />' \
137+
'<input type="hidden" name="message[title]" id="message_title_trix_input_message" value="&lt;h1&gt;hello world&lt;/h1&gt;" autocomplete="off" />' \
138138
'<trix-editor id="message_title" input="message_title_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
139139
"</trix-editor>" \
140140
"</form>",
@@ -148,7 +148,7 @@ def form_with(*, **)
148148

149149
assert_dom_equal \
150150
'<form action="/messages" accept-charset="UTF-8" method="post">' \
151-
'<input type="hidden" name="message[title]" id="message_title_trix_input_message" form="other_form" />' \
151+
'<input type="hidden" name="message[title]" id="message_title_trix_input_message" form="other_form" autocomplete="off" />' \
152152
'<trix-editor id="message_title" input="message_title_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
153153
"</trix-editor>" \
154154
"</form>",
@@ -162,7 +162,7 @@ def form_with(*, **)
162162

163163
assert_dom_equal \
164164
'<form action="/messages" accept-charset="UTF-8" method="post">' \
165-
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
165+
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" autocomplete="off" />' \
166166
'<trix-editor id="message_content" input="message_content_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/direct_uploads" data-blob-url-template="http://test.host/rails/active_storage/blobs/redirect/:signed_id/:filename">' \
167167
"</trix-editor>" \
168168
"</form>",
@@ -176,7 +176,7 @@ def form_with(*, **)
176176

177177
assert_dom_equal \
178178
'<form action="/messages" accept-charset="UTF-8" method="post">' \
179-
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" />' \
179+
'<input type="hidden" name="message[content]" id="message_content_trix_input_message" autocomplete="off" />' \
180180
'<trix-editor id="message_content" input="message_content_trix_input_message" class="trix-content" data-direct-upload-url="http://test.host/rails/active_storage/direct_uploads" data-blob-url-template="http://test.host/blobs/:signed_id/:filename">' \
181181
"</trix-editor>" \
182182
"</form>",

actionview/lib/action_view/helpers/date_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,8 @@ def build_hidden(type, value)
11011101
type: "hidden",
11021102
id: input_id_from_type(type),
11031103
name: input_name_from_type(type),
1104-
value: value
1104+
value: value,
1105+
autocomplete: "off"
11051106
}.merge!(@html_options.slice(:disabled))
11061107
select_options[:disabled] = "disabled" if @options[:disabled]
11071108

actionview/lib/action_view/helpers/form_tag_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def label_tag(name = nil, content_or_options = nil, options = nil, &block)
277277
# # => <input id="collected_input" name="collected_input" onchange="alert('Input collected!')"
278278
# # type="hidden" value="" />
279279
def hidden_field_tag(name, value = nil, options = {})
280-
text_field_tag(name, value, options.merge(type: :hidden))
280+
text_field_tag(name, value, options.merge(type: :hidden, autocomplete: "off"))
281281
end
282282

283283
# Creates a file upload field. If you are using file uploads then you will also need
@@ -866,7 +866,7 @@ def utf8_enforcer_tag
866866
# Use raw HTML to ensure the value is written as an HTML entity; it
867867
# needs to be the right character regardless of which encoding the
868868
# browser infers.
869-
'<input name="utf8" type="hidden" value="&#x2713;" />'.html_safe
869+
'<input name="utf8" type="hidden" value="&#x2713;" autocomplete="off" />'.html_safe
870870
end
871871

872872
private

actionview/lib/action_view/helpers/tags/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def select_content_tag(option_tags, options, html_options)
141141
select = content_tag("select", add_options(option_tags, options, value), html_options)
142142

143143
if html_options["multiple"] && options.fetch(:include_hidden, true)
144-
tag("input", disabled: html_options["disabled"], name: html_options["name"], type: "hidden", value: "") + select
144+
tag("input", disabled: html_options["disabled"], name: html_options["name"], type: "hidden", value: "", autocomplete: "off") + select
145145
else
146146
select
147147
end

actionview/lib/action_view/helpers/tags/check_box.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def checked?(value)
5757
end
5858

5959
def hidden_field_for_checkbox(options)
60-
@unchecked_value ? tag("input", options.slice("name", "disabled", "form").merge!("type" => "hidden", "value" => @unchecked_value)) : "".html_safe
60+
@unchecked_value ? tag("input", options.slice("name", "disabled", "form").merge!("type" => "hidden", "value" => @unchecked_value, "autocomplete" => "off")) : "".html_safe
6161
end
6262
end
6363
end

actionview/lib/action_view/helpers/tags/hidden_field.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ module ActionView
44
module Helpers
55
module Tags # :nodoc:
66
class HiddenField < TextField # :nodoc:
7+
def render
8+
@options[:autocomplete] = "off"
9+
super
10+
end
711
end
812
end
913
end

actionview/lib/action_view/helpers/url_helper.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ def button_to(name = nil, options = nil, html_options = nil, &block)
362362
inner_tags = method_tag.safe_concat(button).safe_concat(request_token_tag)
363363
if params
364364
to_form_params(params).each do |param|
365-
inner_tags.safe_concat tag(:input, type: "hidden", name: param[:name], value: param[:value])
365+
inner_tags.safe_concat tag(:input, type: "hidden", name: param[:name], value: param[:value],
366+
autocomplete: "off")
366367
end
367368
end
368369
content_tag("form", inner_tags, form_options)
@@ -768,14 +769,14 @@ def method_not_get_method?(method)
768769
def token_tag(token = nil, form_options: {})
769770
if token != false && defined?(protect_against_forgery?) && protect_against_forgery?
770771
token ||= form_authenticity_token(form_options: form_options)
771-
tag(:input, type: "hidden", name: request_forgery_protection_token.to_s, value: token)
772+
tag(:input, type: "hidden", name: request_forgery_protection_token.to_s, value: token, autocomplete: "off")
772773
else
773774
""
774775
end
775776
end
776777

777778
def method_tag(method)
778-
tag("input", type: "hidden", name: "_method", value: method.to_s)
779+
tag("input", type: "hidden", name: "_method", value: method.to_s, autocomplete: "off")
779780
end
780781

781782
# Returns an array of hashes each containing :name and :value keys

actionview/test/activerecord/form_helper_activerecord_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attribut
5151

5252
expected = whole_form("/developers/123", "edit_developer_123", "edit_developer", method: "patch") do
5353
'<input id="developer_projects_attributes_abc_name" name="developer[projects_attributes][abc][name]" type="text" value="project #321" />' \
54-
'<input id="developer_projects_attributes_abc_id" name="developer[projects_attributes][abc][id]" type="hidden" value="321" />'
54+
'<input id="developer_projects_attributes_abc_id" name="developer[projects_attributes][abc][id]" type="hidden" value="321" autocomplete="off" />'
5555
end
5656

5757
assert_dom_equal expected, output_buffer
5858
end
5959

6060
private
6161
def hidden_fields(method = nil)
62-
txt = +%{<input name="utf8" type="hidden" value="&#x2713;" />}
62+
txt = +%{<input name="utf8" type="hidden" value="&#x2713;" autocomplete="off" />}
6363

6464
if method && !%w(get post).include?(method.to_s)
65-
txt << %{<input name="_method" type="hidden" value="#{method}" />}
65+
txt << %{<input name="_method" type="hidden" value="#{method}" autocomplete="off" />}
6666
end
6767

6868
txt

0 commit comments

Comments
 (0)