Rails button_to的输出最近在W3C验证中开始失败,返回的消息为:
值隐藏的类型属性的输入元素不能具有值为on或off的自动完成属性。
所以我想消除这个错误。
Rails-7.0.4中相应的视图中的button_to语句是
<%= button_to "Add Entry", new_article_path, form_class: "inline_form button_to",
method: :get, params: { a_token: "abc" } %>它生成一个HTML (简化!):
<!DOCTYPE html>
<html lang="en"> <head> <meta charset="utf-8"> <title>My Title</title> </head>
<body>
<form class="inline_form button_to" method="get" action="/articles/new?locale=en">
<input type="submit" value="Add Entry" />
<input type="hidden" name="a_token" value="abc" autocomplete="off" />
</form>
</body>
</html>如果我把它粘贴到W3C验证器表单中,上面提到的消息就会失败.
显然,W3C验证器不喜欢autocomplete属性存在于带有type="hidden"的input标记中。但这是button_to的默认输出(在Rails 7.0中)。
根据第42610期 in Rails存储库的说法,Rails团队去年增加了第42610期,以使HTML成为可验证的。在相关的讨论中,用户jpwynn (2021-06-27)的更改不会破坏W3C验证。但表面上看,现在它确实做到了……
据我所知,我没有修改我的Rails应用程序的相关部分。然而,我注意到常规测试在W3C验证中已经开始失败。因此,我怀疑W3C最近更改了规范(但我可能错了!)
我的Rails测试非常简单,如下所示,使用Gem 验证器 Ver.1.3.7;它只检测任何错误,如果有错误就失败:
# /test/test_helper.rb
require 'w3c_validators'
def my_w3c_validate
arerr = @validator.validate_text(response.body).errors
assert_equal 0, arerr.size,
"Failed in W3C-validation: ("+arerr.map(&:to_s).join(") (")+")"
end用Gemfile
group :development, :test do
gem 'w3c_validators', '~> 1', '>= 1.3.6' # => 1.3.7 in reality
end那么,Rails button_to的输出是一个有效的HTML吗?如果不是,我如何修改它以便输出HTML将是一个有效的HTML?我正在使用最新的稳定版本,Rails-7.0.4。
发布于 2022-11-02 23:07:41
W3C HTML (验证器) @sideshowbarker的维护者善意地提供了信息(参见问题中的评论)。
以下是一个总结:
<input type="hidden" name="abc" autocomplete="off">无效。它基本上应用于<input type=hidden>标记。
button_to方法生成的HTML违反了<input>标记的规范。button_to生成的HTML最近(去年)发生了变化,以应对火狐的不良行为;它当时确实通过了W3C的HTML验证!参见Rails Github第42610期现在,由于W3C验证器运行正常,您有两个选项:
button_to生成HTML的方式这是第二项政策的措施。传递W3CValidators::Message的数组,此方法返回相同的数组,但删除了上述错误。原始的相关错误信息可以记录在Logger中。我也将脚本(但有更广泛的描述)放在Github吉斯特中。
# @example Usage, maybe in /test/test_helper.rb
# # Make sure to write in /config/environments/test.rb
# # config.ignore_w3c_validate_hidden_autocomplete = true
# #
# #require 'w3c_validators'
# errors = @validator.validate_text(response.body).errors
# errors = _may_ignore_autocomplete_errors_for_hidden(errors, "W3C validaiton failed: ")
# assert_empty errors, "Failed in W3C validation: "+errors.map(&:to_s).inspect
#
# @param errs [Array<W3CValidators::Message>] Output of +@validator.validate_text(response.body).errors+
# @param prefix [String] Prefix of the warning message recorded with Logger.
# If empty, no message is recorded in Logger.
# @return [Array<String>]
def may_ignore_autocomplete_errors_for_hidden(errs, prefix="")
removeds = []
return errs if !Rails.configuration.ignore_w3c_validate_hidden_autocomplete
errs.map{ |es|
# Example of an Error:
# ERROR; line 165: An “input” element with a “type” attribute whose value is “hidden” must not have an “autocomplete” attribute whose value is “on” or “off”
if /\AERROR\b.+\binput\b[^a-z]+\belement.+\btype\b.+\bhidden\b.+\bautocomplete\b[^a-z]+\battribute\b/i =~ es.to_s
removeds << es
nil
else
es
end
}.compact
ensure
# Records it in Logger
if !removeds.empty? && !prefix.blank?
Rails.logger.warn(prefix + removeds.map(&:to_s).uniq.inspect)
end
endhttps://stackoverflow.com/questions/74256523
复制相似问题