Class: MirrorMirror::MirrorVisitor

Inherits:
Sass::Tree::Visitors::Base
  • Object
show all
Defined in:
lib/mirror-mirror/mirror_visitor.rb

Instance Method Summary collapse

Constructor Details

#initializeMirrorVisitor

Returns a new instance of MirrorVisitor.



5
6
7
8
9
10
11
12
13
# File 'lib/mirror-mirror/mirror_visitor.rb', line 5

def initialize
  @in_meta_directive = false
  @nested = false
  @enabled = false
  @inline = false
  @options = {}
  @property_transformers = Transformation::PropertyTransformation.instances
  @debug = false
end

Instance Method Details

#visit(node) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/mirror-mirror/mirror_visitor.rb', line 24

def visit(node)
  method = "visit_#{node_name node}"
  if self.respond_to?(method, true)
    self.send(method, node) {node.children = visit_children(node)}
  else
    node.children = visit_children(node)
    node
  end
end

#visit_children(parent) ⇒ Object



34
35
36
37
38
39
# File 'lib/mirror-mirror/mirror_visitor.rb', line 34

def visit_children(parent)
  r = super
  r.flatten!
  r.compact!
  r
end

#visit_directive(directive_node) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mirror-mirror/mirror_visitor.rb', line 112

def visit_directive(directive_node)
  if directive_node.resolved_value.strip == "@-mirror-mirror"
    @in_meta_directive = true
    begin
      yield
    ensure
      @in_meta_directive = false
    end
    nil # remove this node from the output
  elsif directive_node.resolved_value.strip == "@-mirror-mirror no-flip"
    old_enabled = @enabled
    @enabled = false
    begin
      yield
      directive_node.children
    ensure
      @enabled = old_enabled
    end
  elsif directive_node.resolved_value.strip == "@-mirror-mirror inline-flip"
    old_inline = @inline
    old_enabled = @enabled
    @inline = true
    @enabled = true
    begin
      yield
      directive_node.children
    ensure
      @enabled = old_enabled
      @inline = old_inline
    end
  else
    yield
    directive_node
  end
end

#visit_prop(prop_node) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/mirror-mirror/mirror_visitor.rb', line 49

def visit_prop(prop_node)
  if @in_meta_directive && prop_node.resolved_name == "enabled"
    @enabled = prop_node.resolved_value == "true"
    #if @nested
      #raise Sass::SyntaxError, "Cannot #{@enabled ? 'enable' : 'disable'} mirror flipping in a nested context"
    #end
    puts @enabled ? "ENABLED" : "DISABLED" if @debug
    return prop_node
  end

  puts "PROPERTY: #{prop_node.resolved_name}" if @debug
  unless @enabled
    return prop_node
  end

  prop_name = prop_node.resolved_name.downcase
  value = prop_node.resolved_value
  prefix, prop_name = $1, $2 if prop_name =~ /^(\*|_|#|-moz-|-ms-|-webkit-|-o-)(.*)$/
  original_name = prop_name
  original_value = value
  important = nil

  if value =~ /^(.*[^ ])( *!important *)$/
    value, important = $1, $2
  end

  property_transformers(prop_name, :name).each do |t|
    prop_name = t.transform_name(prop_name)
  end

  property_transformers(original_name, :value).each do |t|
    value = t.transform_value(original_name, value)
  end

  expression = nil
  begin
    property_transformers(original_name, :expression).each do |t|
      expression ||= parsed_value(value, prop_node.line, original_name.size)
      expression = t.transform_expression(original_name, expression)
    end
  rescue Sass::SyntaxError => e
    Sass.logger.warn("Syntax error during mirror flip of `#{original_name}: #{original_value}`: #{e.message}")
    return prop_node
  end
  begin
    value = literalize(expression).to_s(@options) if expression
  rescue => e
    Sass.logger.warn "Failed to stringify the transformation of `#{original_name}: #{original_value}`: #{e.message} (#{expression.class.name})"
    Sass.logger.warn(e.backtrace.join("\n"))
    value = original_value
  end

  if @debug && prop_node.resolved_name != "#{prefix}#{prop_name}"
    puts "CHANGED name FROM #{prop_node.resolved_name} TO #{prefix}#{prop_name}"
  end
  if @debug && prop_node.resolved_value != value
    puts "CHANGED value FROM #{prop_node.resolved_value} TO #{value}"
  end
  prop_node.resolved_name = "#{prefix}#{prop_name}"
  prop_node.resolved_value = "#{value}#{important}"
  prop_node
end

#visit_root(root_node) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/mirror-mirror/mirror_visitor.rb', line 15

def visit_root(root_node)
  # XXX set up defaults from options
  @options = root_node.options
  custom_opts = @options[:custom] || {}
  @enabled = !!custom_opts.fetch(:mirror_mirror, MirrorMirror.flipped_by_default)
  root_node.children = yield
  root_node
end

#visit_rule(rule_node) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/mirror-mirror/mirror_visitor.rb', line 41

def visit_rule(rule_node)
  puts "RULE: #{rule_node.resolved_rules}" if @debug
  @nested = true;
  yield
  @nested = false;
  rule_node
end