1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
haml--haml/lib/sass/tree/directive_node.rb
2009-04-25 12:19:08 -07:00

72 lines
2.2 KiB
Ruby

module Sass::Tree
# A static node representing an unproccessed Sass `@`-directive.
# Directives known to Sass, like `@for` and `@debug`,
# are handled by their own nodes;
# only CSS directives like `@media` and `@font-face` become {DirectiveNode}s.
#
# `@import` is a bit of a weird case;
# if it's importing a Sass file,
# it becomes a {FileNode},
# but if it's importing a plain CSS file,
# it becomes a {DirectiveNode}.
#
# @see Sass::Tree
class DirectiveNode < Node
# The text of the directive, `@` and all.
#
# @return [String]
attr_accessor :value
# @param value [String] See \{#value}
# @param options [Hash<Symbol, Object>] An options hash;
# see [the Sass options documentation](../../Sass.html#sass_options)
def initialize(value, options)
@value = value
super(options)
end
# Computes the CSS for the directive.
#
# @param tabs [Fixnum] The level of indentation for the CSS
# @return [String] The resulting CSS
def to_s(tabs)
if children.empty?
value + ";"
else
result = if @style == :compressed
"#{value}{"
else
"#{' ' * (tabs - 1)}#{value} {" + (@style == :compact ? ' ' : "\n")
end
was_attr = false
first = true
children.each do |child|
if @style == :compact
if child.is_a?(AttrNode)
result << "#{child.to_s(first || was_attr ? 1 : tabs + 1)} "
else
if was_attr
result[-1] = "\n"
end
rendered = child.to_s(tabs + 1)
rendered.lstrip! if first
result << rendered
end
was_attr = child.is_a?(AttrNode)
first = false
elsif @style == :compressed
result << (was_attr ? ";#{child.to_s(1)}" : child.to_s(1))
was_attr = child.is_a?(AttrNode)
else
result << child.to_s(tabs + 1) + "\n"
end
end
result.rstrip + if @style == :compressed
"}"
else
(@style == :expanded ? "\n" : " ") + "}\n"
end
end
end
end
end