【问题标题】:only <a> tag execute from html content in rails只有 <a> 标记从 rails 中的 html 内容执行
【发布时间】:2014-10-16 13:15:42
【问题描述】:

我的问题是只从 rails 中的 html 内容执行标签。

我使用的是原始的,消毒的,但所有的 html 标签都被执行了

例子

@input = '<a href="http://www.example.com/">go</a> <b>bold</b> <i>bat</i>' <%=raw @input%> <%=sanitize @input%> 有相同的输出:go bold bat

我的建议是输出:go&lt;b&gt;bold&lt;/b&gt; &lt;i&gt;bat&lt;/i&gt;

我像 facebook 一样实现哈希标签,但用户输入不安全许多哈希标签和许多 html 标签

有什么想法吗? 谢谢你

【问题讨论】:

    标签: html ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-3.2


    【解决方案1】:

    我也为此苦苦挣扎,以下将有所帮助:

    @input = '<a href="http://www.example.com/">go</a>'
    @input += '<b>bold</b> <i>bat</i>'.encode {xml: :text}
    

    这会将 HTML 特殊字符格式化为原始符号。更多信息请访问the docs for String#encode

    【讨论】:

      【解决方案2】:

      你可以这样做:

      <% @input = '<a href="http://www.example.com/">go</a>'.html_safe+'<b>bold</b> <i>bat</i>' %>
      <%= raw @input %>
      

      【讨论】:

      【解决方案3】:
      @input = '<a href="http://www.example.com/">go</a>'.html_safe
      @input += ' <b>bold</b> <i>bat</i>'
      <%= @input %>
      

      【讨论】:

      • 使用 link_to 比敲出原始 HTML 更可取。
      猜你喜欢
      • 2020-09-12
      • 2019-07-09
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      • 2018-11-13
      相关资源
      最近更新 更多