首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails button_to与W3C验证器一起失败

Rails button_to与W3C验证器一起失败
EN

Stack Overflow用户
提问于 2022-10-30 20:29:32
回答 1查看 55关注 0票数 3

Rails button_to的输出最近在W3C验证中开始失败,返回的消息为:

值隐藏的类型属性的输入元素不能具有值为on或off的自动完成属性。

所以我想消除这个错误。

Rails-7.0.4中相应的视图中的button_to语句是

代码语言:javascript
复制
<%= button_to "Add Entry", new_article_path, form_class: "inline_form button_to",
  method: :get, params: { a_token: "abc" } %>

它生成一个HTML (简化!):

代码语言:javascript
复制
<!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;它只检测任何错误,如果有错误就失败:

代码语言:javascript
复制
 # /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

代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-02 23:07:41

W3C HTML (验证器) @sideshowbarker的维护者善意地提供了信息(参见问题中的评论)。

以下是一个总结:

  1. W3C HTML做得非常正确: <input type="hidden" name="abc" autocomplete="off">无效。
    • 根据HTML表单规范的说法 当佩戴自动填充锚罩时,如果指定了自动完成属性,则必须有一个值,该值是一组有序的空间分隔的标记,由仅自动填充的详细标记组成(即不允许使用"on“和"off”关键字),其中“佩带自填锚地幔”。

它基本上应用于<input type=hidden>标记。

  1. 新的检查项是最近(2022-10-26)在检查器上实现的,因此我的Rails W3C测试的结果最近发生了变化。请参阅Git拉动请求
  2. Rails的button_to方法生成的HTML违反了<input>标记的规范。
    • 具有讽刺意味的是,button_to生成的HTML最近(去年)发生了变化,以应对火狐的不良行为;它当时确实通过了W3C的HTML验证!参见Rails Github第42610期

现在,由于W3C验证器运行正常,您有两个选项:

  1. 改变Rails button_to生成HTML的方式
  2. 在这方面修改W3C验证的结果,这样验证就不会因为这个错误而失败,而且您仍然可以使用所有其他验证。

这是第二项政策的措施。传递W3CValidators::Message的数组,此方法返回相同的数组,但删除了上述错误。原始的相关错误信息可以记录在Logger中。我也将脚本(但有更广泛的描述)放在Github吉斯特中。

代码语言:javascript
复制
# @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
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74256523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档