Class: Twig::NodeVisitor::Escaper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#priority

Constructor Details

#initializeEscaper

Returns a new instance of Escaper.



8
9
10
11
12
13
14
15
16
# File 'lib/twig/node_visitor/escaper.rb', line 8

def initialize
  super

  @default_strategy = false
  @status_stack = []
  @blocks = {}
  @safe_vars = []
  @safe_analysis = SafeAnalysis.new
end

Instance Attribute Details

#escaping_strategyObject (readonly)

Returns the value of attribute escaping_strategy.



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

def escaping_strategy
  @escaping_strategy
end

Instance Method Details

#enter_node(node, env) ⇒ Object



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

def enter_node(node, env)
  case node
  when Node::Module
    if env.extension?(Extension::Escaper)
      default_strategy = env.extension(Extension::Escaper).default_strategy(node.template_name)
      @default_strategy = default_strategy if default_strategy
    end

    @blocks = {}
    @safe_vars = []
  when Node::AutoEscape
    @status_stack << node.attributes[:value].then { |v| v.is_a?(String) ? v.to_sym : v }
  when Node::Block
    @status_stack << blocks.fetch(node.attributes[:name], need_escaping)
  when Node::Import
    @safe_vars << node.nodes[:var].nodes[:var].attributes[:name]
  end

  node
end

#leave_node(node, env) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/twig/node_visitor/escaper.rb', line 39

def leave_node(node, env)
  if node.is_a?(Node::Module)
    @default_strategy = false
    @safe_vars = []
    @blocks = {}
  elsif node.is_a?(Node::Expression::Filter)
    return pre_escape_filter_node(node, env)
  elsif node.is_a?(Node::Print) && (type = need_escaping) != false
    expression = node.nodes[:expr]

    if expression.is_a?(Node::Expression::OperatorEscape)
      escape_conditional(expression, env, type)
    else
      node.nodes[:expr] = escape_expression(expression, env, type)
    end

    return node
  end

  if node.is_a?(Node::AutoEscape) || node.is_a?(Node::Block)
    @status_stack.pop
  elsif node.is_a?(Node::BlockReference)
    @blocks[node.attributes[:name]] = need_escaping
  end

  node
end