Sha256: 4363de943bce1cdd3d2d4091d01e99650b7f788e71cce884cfbada627c41bac7

Contents?: true

Size: 1.9 KB

Versions: 31

Compression:

Stored size: 1.9 KB

Contents

require 'sass/tree/node'

module Sass::Tree
  # A dynamic node representing a Sass `@if` statement.
  #
  # {IfNode}s are a little odd, in that they also represent `@else` and `@else if`s.
  # This is done as a linked list:
  # each {IfNode} has a link (\{#else}) to the next {IfNode}.
  #
  # @see Sass::Tree
  class IfNode < Node
    # The next {IfNode} in the if-else list, or `nil`.
    #
    # @return [IfNode]
    attr_accessor :else

    # @param expr [Script::Expr] The conditional expression.
    #   If this is nil, this is an `@else` node, not an `@else if`
    def initialize(expr)
      @expr = expr
      @last_else = self
      super()
    end

    # Append an `@else` node to the end of the list.
    #
    # @param node [IfNode] The `@else` node to append
    def add_else(node)
      @last_else.else = node
      @last_else = node
    end

    # @see Node#options=
    def options=(options)
      super
      self.else.options = options if self.else
    end

    protected

    # @see Node#to_src
    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

    # Runs the child nodes if the conditional expression is true;
    # otherwise, tries the \{#else} nodes.
    #
    # @param environment [Sass::Environment] The lexical environment containing
    #   variable and mixin values
    # @return [Array<Tree::Node>] The resulting static nodes
    # @see Sass::Tree
    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
  end
end

Version data entries

31 entries across 31 versions & 2 rubygems

Version Path
haml-3.0.25 lib/sass/tree/if_node.rb
haml-edge-2.3.238 lib/sass/tree/if_node.rb
haml-edge-2.3.237 lib/sass/tree/if_node.rb
haml-edge-2.3.236 lib/sass/tree/if_node.rb
haml-3.0.0.rc.3 lib/sass/tree/if_node.rb
haml-edge-2.3.235 lib/sass/tree/if_node.rb
haml-edge-2.3.234 lib/sass/tree/if_node.rb
haml-edge-2.3.233 lib/sass/tree/if_node.rb
haml-edge-2.3.230 lib/sass/tree/if_node.rb
haml-edge-2.3.229 lib/sass/tree/if_node.rb
haml-edge-2.3.228 lib/sass/tree/if_node.rb
haml-edge-2.3.227 lib/sass/tree/if_node.rb
haml-edge-2.3.226 lib/sass/tree/if_node.rb
haml-edge-2.3.225 lib/sass/tree/if_node.rb
haml-edge-2.3.224 lib/sass/tree/if_node.rb
haml-edge-2.3.223 lib/sass/tree/if_node.rb
haml-edge-2.3.222 lib/sass/tree/if_node.rb
haml-3.0.0.rc.2 lib/sass/tree/if_node.rb
haml-edge-2.3.221 lib/sass/tree/if_node.rb
haml-edge-2.3.220 lib/sass/tree/if_node.rb