/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

  1. module 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. # @param value [String] See \{#value}
  17. def initialize(value)
  18. @value = value
  19. super()
  20. end
  21. protected
  22. # @see Node#to_src
  23. def to_src(tabs, opts, fmt)
  24. res = "#{' ' * tabs}#{value}"
  25. return res + "#{semi fmt}\n" unless has_children
  26. res + children_to_src(tabs, opts, fmt) + "\n"
  27. end
  28. # Computes the CSS for the directive.
  29. #
  30. # @param tabs [Fixnum] The level of indentation for the CSS
  31. # @return [String] The resulting CSS
  32. def _to_s(tabs)
  33. return value + ";" unless has_children
  34. return value + " {}" if children.empty?
  35. result = if style == :compressed
  36. "#{value}{"
  37. else
  38. "#{' ' * (tabs - 1)}#{value} {" + (style == :compact ? ' ' : "\n")
  39. end
  40. was_prop = false
  41. first = true
  42. children.each do |child|
  43. next if child.invisible?
  44. if style == :compact
  45. if child.is_a?(PropNode)
  46. result << "#{child.to_s(first || was_prop ? 1 : tabs + 1)} "
  47. else
  48. if was_prop
  49. result[-1] = "\n"
  50. end
  51. rendered = child.to_s(tabs + 1).dup
  52. rendered = rendered.lstrip if first
  53. result << rendered.rstrip + "\n"
  54. end
  55. was_prop = child.is_a?(PropNode)
  56. first = false
  57. elsif style == :compressed
  58. result << (was_prop ? ";#{child.to_s(1)}" : child.to_s(1))
  59. was_prop = child.is_a?(PropNode)
  60. else
  61. result << child.to_s(tabs + 1) + "\n"
  62. end
  63. end
  64. result.rstrip + if style == :compressed
  65. "}"
  66. else
  67. (style == :expanded ? "\n" : " ") + "}\n"
  68. end
  69. end
  70. end
  71. end