2008-10-13 09:49:35 -07:00
|
|
|
require 'sass/tree/node'
|
|
|
|
|
|
|
|
module Sass::Tree
|
2009-04-25 02:43:08 -07:00
|
|
|
# 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
|
2008-10-13 09:49:35 -07:00
|
|
|
class IfNode < Node
|
2009-04-25 02:43:08 -07:00
|
|
|
# The next {IfNode} in the if-else list, or `nil`.
|
|
|
|
#
|
|
|
|
# @return [IfNode]
|
2008-10-14 23:10:41 -07:00
|
|
|
attr_accessor :else
|
|
|
|
|
2009-04-25 02:43:08 -07:00
|
|
|
# @param expr [Script::Expr] The conditional expression.
|
|
|
|
# If this is nil, this is an `@else` node, not an `@else if`
|
2009-04-21 19:25:18 -07:00
|
|
|
def initialize(expr)
|
2008-10-13 09:49:35 -07:00
|
|
|
@expr = expr
|
2008-10-14 23:10:41 -07:00
|
|
|
@last_else = self
|
2009-04-21 19:25:18 -07:00
|
|
|
super()
|
2008-10-13 09:49:35 -07:00
|
|
|
end
|
|
|
|
|
2009-04-25 02:43:08 -07:00
|
|
|
# Append an `@else` node to the end of the list.
|
|
|
|
#
|
|
|
|
# @param node [IfNode] The `@else` node to append
|
2008-10-14 23:10:41 -07:00
|
|
|
def add_else(node)
|
|
|
|
@last_else.else = node
|
|
|
|
@last_else = node
|
|
|
|
end
|
|
|
|
|
2009-04-21 19:13:49 -07:00
|
|
|
def options=(options)
|
|
|
|
super
|
|
|
|
self.else.options = options if self.else
|
|
|
|
end
|
|
|
|
|
2008-10-13 09:49:35 -07:00
|
|
|
protected
|
|
|
|
|
2009-04-25 02:43:08 -07:00
|
|
|
# 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
|
2008-10-13 09:49:35 -07:00
|
|
|
def _perform(environment)
|
2008-10-15 20:00:28 -07:00
|
|
|
environment = Sass::Environment.new(environment)
|
2008-10-14 23:10:41 -07:00
|
|
|
return perform_children(environment) if @expr.nil? || @expr.perform(environment).to_bool
|
|
|
|
return @else.perform(environment) if @else
|
|
|
|
[]
|
2008-10-13 09:49:35 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|