Class: Sass::Tree::IfNode

Inherits:
Node show all
Defined in:
/var/www/sass-pages/.haml/lib/sass/tree/if_node.rb

Overview

A dynamic node representing a Sass @if statement.

IfNodes are a little odd, in that they also represent @else and @else ifs. This is done as a linked list: each IfNode has a link (#else) to the next IfNode.

See Also:

Instance Attribute Summary

Instance Method Summary

Methods inherited from Node

#<<, #==, #_cssize, #_to_s, #balance, #check_child!, #children_to_src, #cssize, #cssize!, #dasherize, #do_extend, #each, #invisible?, #perform, #perform!, #perform_children, #run_interp, #selector_to_sass, #selector_to_scss, #selector_to_src, #semi, #style, #to_s, #to_sass, #to_scss

Constructor Details

- (IfNode) initialize(expr)

A new instance of IfNode

Parameters:

  • (Script::Expr) expr — The conditional expression. If this is nil, this is an @else node, not an @else if


19
20
21
22
23
# File '/var/www/sass-pages/.haml/lib/sass/tree/if_node.rb', line 19

def initialize(expr)
  @expr = expr
  @last_else = self
  super()
end

Instance Attribute Details

- (IfNode) else

The next IfNode in the if-else list, or nil.

Returns:



15
16
17
# File '/var/www/sass-pages/.haml/lib/sass/tree/if_node.rb', line 15

def else
  @else
end

Instance Method Details

- (Array<Tree::Node>) _perform(environment) (protected)

Runs the child nodes if the conditional expression is true; otherwise, tries the #else nodes.

Parameters:

  • (Sass::Environment) environment — The lexical environment containing variable and mixin values

Returns:

  • (Array<Tree::Node>) — The resulting static nodes

See Also:



62
63
64
65
66
67
# File '/var/www/sass-pages/.haml/lib/sass/tree/if_node.rb', line 62

def _perform(environment)
  environment = Sass::Environment.new(environment)
  return perform_children(environment) if @expr.nil? || @expr.perform(environment).to_bool
  return @else.perform(environment) if @else
  []
end

- add_else(node)

Append an @else node to the end of the list.

Parameters:

  • (IfNode) node — The @else node to append


28
29
30
31
# File '/var/www/sass-pages/.haml/lib/sass/tree/if_node.rb', line 28

def add_else(node)
  @last_else.else = node
  @last_else = node
end

- (Boolean, String) invalid_child?(child) (protected)

Returns an error message if the given child node is invalid, and false otherwise.

ExtendNodes are valid within IfNodes.

Parameters:

Returns:

  • (Boolean, String) — Whether or not the child node is valid, as well as the error message to display if it is invalid


77
78
79
# File '/var/www/sass-pages/.haml/lib/sass/tree/if_node.rb', line 77

def invalid_child?(child)
  super unless child.is_a?(ExtendNode)
end

- options=(options)

See Also:



34
35
36
37
# File '/var/www/sass-pages/.haml/lib/sass/tree/if_node.rb', line 34

def options=(options)
  super
  self.else.options = options if self.else
end

- to_src(tabs, opts, fmt, is_else = false) (protected)

See Also:



42
43
44
45
46
47
48
49
50
51
52
53
# File '/var/www/sass-pages/.haml/lib/sass/tree/if_node.rb', line 42

def to_src(tabs, opts, fmt, is_else = false)
  name =
    if !is_else; "if"
    elsif @expr; "else if"
    else; "else"
    end
  str = "#{'  ' * tabs}@#{name}"
  str << " #{@expr.to_sass(opts)}" if @expr
  str << children_to_src(tabs, opts, fmt)
  str << @else.send(:to_src, tabs, opts, fmt, true) if @else
  str
end