Module: Rich::I18n::Actionpack::ActionView::Sanitizor

Extended by:
Sanitizor
Included in:
Sanitizor
Defined in:
lib/rich/i18n/actionpack/action_view/sanitizor.rb

Instance Method Summary collapse

Instance Method Details

#sanitize_html(html) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rich/i18n/actionpack/action_view/sanitizor.rb', line 11

def sanitize_html(html)
  return html unless (html || "").include?("<i18n ")

  doc = Hpricot html

  (doc/"head i18n,select i18n").each do |i18n|
    i18n.swap strip_tags(i18n.raw_attributes["data-i18n_translation"])
  end

  (doc/"input,textarea").each do |input|
    sanitize_input(input)
  end

  (doc/"i18n").each do |i18n|
    elem = Hpricot::Elem.new i18n.raw_attributes["data-i18n_tag"] || (i18n.raw_attributes["data-editable_input_type"] == "html" ? "div" : "span"),
                             i18n.raw_attributes.reject{|k, v| k == "data-i18n_translation"}.merge({:class => "i18n"})
    elem.inner_html = CGI.unescapeHTML(i18n.raw_attributes["data-i18n_translation"])
    i18n.swap elem.to_html
  end

  doc.to_html
rescue
  html.gsub(/(\<i18n[^\>]+\>)|(\<\/i18n\>)/, "")
end