Class: Twig::Extension::Escaper

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#expression_parsers, #functions, #globals, #tests

Constructor Details

#initialize(default_strategy = :html) ⇒ Escaper

Returns a new instance of Escaper.



6
7
8
9
10
# File 'lib/twig/extension/escaper.rb', line 6

def initialize(default_strategy = :html)
  super()

  self.default_strategy = default_strategy
end

Class Method Details

.escape_filter_is_safe(filter_args) ⇒ Object

Parameters:



62
63
64
65
66
67
68
69
70
# File 'lib/twig/extension/escaper.rb', line 62

def self.escape_filter_is_safe(filter_args)
  filter_args.nodes.each_value do |arg|
    if arg.is_a?(Node::Expression::Constant)
      return [arg.attributes[:value]]
    end
  end

  [:html]
end

Instance Method Details

#default_strategy(name) ⇒ Symbol, false

Gets the default strategy to use when not defined by the user.

Parameters:

  • name (String)

    The template name

Returns:

  • (Symbol, false)

    The default strategy to use for the template



53
54
55
56
57
58
59
# File 'lib/twig/extension/escaper.rb', line 53

def default_strategy(name)
  if @default_strategy.is_a?(Proc)
    @default_strategy.call(name)
  else
    @default_strategy
  end
end

#default_strategy=(strategy) ⇒ Object

Sets the default strategy to use when not defined by the user.

Parameters:

  • strategy (Symbol, String, false, Proc)

    An escaping strategy



41
42
43
44
45
46
47
# File 'lib/twig/extension/escaper.rb', line 41

def default_strategy=(strategy)
  @default_strategy = if [:name, 'name'].include?(strategy)
                        ->(name) { FileExtensionEscapingStrategy.guess(name) }
                      else
                        strategy
                      end
end

#filtersObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/twig/extension/escaper.rb', line 12

def filters
  [
    TwigFilter.new('escape', runtime(Runtime::Escaper, :escape), {
      is_safe_callback: static(:escape_filter_is_safe),
    }),
    TwigFilter.new('e', runtime(Runtime::Escaper, :escape), {
      is_safe_callback: static(:escape_filter_is_safe),
    }),
    TwigFilter.new('raw', nil, {
      is_safe: [:all], node_class: Node::Expression::Filter::Raw
    }),
  ]
end

#node_visitorsObject



32
33
34
35
36
# File 'lib/twig/extension/escaper.rb', line 32

def node_visitors
  [
    NodeVisitor::Escaper.new,
  ]
end

#token_parsersObject



26
27
28
29
30
# File 'lib/twig/extension/escaper.rb', line 26

def token_parsers
  [
    TokenParser::AutoEscape.new,
  ]
end