0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Haml で html_safe を使う (Rails 以外で)

Posted at

haml-4.0.2/lib/haml/template.rb を参考にした。環境は下記の通り。

  • ruby 2.0.0-p0
  • haml 4.0.2
  • activesupport 3.2.13

必要なのは...

  1. active_support/core_ext/string/output_safetyString#html_safe など追加
  2. Haml::Util#rails_xss_safe?true を返す様にする
  3. Haml::HelpersHaml::Helpers::XssModsinclude
  4. Haml::Engine のコンストラクタで escape_html: true 指定
require "active_support/core_ext/string/output_safety"
require "haml"
require "haml/helpers/xss_mods"

Haml::Util.class_eval do
  def rails_xss_safe?
    true
  end
end

Haml::Helpers.class_eval do
  include Haml::Helpers::XssMods
end

haml = <<-HAML
= "<script>alert('1');</script>"
= "<script>alert('1');</script>".html_safe
HAML

puts Haml::Engine.new(haml, escape_html: true).render
&lt;script&gt;alert(1);&lt;/script&gt;
<script>alert(1);</script>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?