Class: Twig::Runtime::Escaper

Inherits:
Object
  • Object
show all
Defined in:
lib/twig/runtime/escaper.rb

Constant Summary collapse

JS_SHORT_MAP =
{
  '\\' => '\\\\',
  '/' => '\\/',
  "\x08" => '\b',
  "\x0C" => '\f',
  "\x0A" => '\n',
  "\x0D" => '\r',
  "\x09" => '\t',
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(charset) ⇒ Escaper

Returns a new instance of Escaper.



6
7
8
# File 'lib/twig/runtime/escaper.rb', line 6

def initialize(charset)
  @charset = charset
end

Instance Method Details

#escape(string, strategy = :html, charset = nil, autoescape = false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/twig/runtime/escaper.rb', line 20

def escape(string, strategy = :html, charset = nil, autoescape = false)
  # Allow strings marked as html_safe to get through without escaping
  if string.html_safe? && autoescape
    return string
  end

  case strategy.to_sym
  when :html
    CGI.escapeHTML(string.to_s)
  when :html_attr
    escape_html_attr(string.to_s, charset || @charset)
  when :js
    escape_js(string.to_s, charset || @charset)
  when :css
    escape_css(string.to_s, charset || @charset)
  when :url
    CGI.escape(string.to_s)
  else
    string.to_s
  end.html_safe
end