PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-jruby/src/main/resources/haml-3.0.25/lib/sass/tree/directive_node.rb

http://github.com/scalate/scalate
Ruby | 75 lines | 53 code | 4 blank | 18 comment | 13 complexity | a27f9972114cda90621f23add5590f2a MD5 | raw file
 1module Sass::Tree
 2  # A static node representing an unproccessed Sass `@`-directive.
 3  # Directives known to Sass, like `@for` and `@debug`,
 4  # are handled by their own nodes;
 5  # only CSS directives like `@media` and `@font-face` become {DirectiveNode}s.
 6  #
 7  # `@import` and `@charset` are special cases;
 8  # they become {ImportNode}s and {CharsetNode}s, respectively.
 9  #
10  # @see Sass::Tree
11  class DirectiveNode < Node
12    # The text of the directive, `@` and all.
13    #
14    # @return [String]
15    attr_accessor :value
16
17    # @param value [String] See \{#value}
18    def initialize(value)
19      @value = value
20      super()
21    end
22
23    protected
24
25    # @see Node#to_src
26    def to_src(tabs, opts, fmt)
27      res = "#{'  ' * tabs}#{value}"
28      return res + "#{semi fmt}\n" unless has_children
29      res + children_to_src(tabs, opts, fmt) + "\n"
30    end
31
32    # Computes the CSS for the directive.
33    #
34    # @param tabs [Fixnum] The level of indentation for the CSS
35    # @return [String] The resulting CSS
36    def _to_s(tabs)
37      return value + ";" unless has_children
38      return value + " {}" if children.empty?
39      result = if style == :compressed
40                 "#{value}{"
41               else
42                 "#{'  ' * (tabs - 1)}#{value} {" + (style == :compact ? ' ' : "\n")
43               end
44      was_prop = false
45      first = true
46      children.each do |child|
47        next if child.invisible?
48        if style == :compact
49          if child.is_a?(PropNode)
50            result << "#{child.to_s(first || was_prop ? 1 : tabs + 1)} "
51          else
52            if was_prop
53              result[-1] = "\n"
54            end
55            rendered = child.to_s(tabs + 1).dup
56            rendered = rendered.lstrip if first
57            result << rendered.rstrip + "\n"
58          end
59          was_prop = child.is_a?(PropNode)
60          first = false
61        elsif style == :compressed
62          result << (was_prop ? ";#{child.to_s(1)}" : child.to_s(1))
63          was_prop = child.is_a?(PropNode)
64        else
65          result << child.to_s(tabs + 1) + "\n"
66        end
67      end
68      result.rstrip + if style == :compressed
69                        "}"
70                      else
71                        (style == :expanded ? "\n" : " ") + "}\n"
72                      end
73    end
74  end
75end