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

http://github.com/scalate/scalate · Ruby · 163 lines · 103 code · 15 blank · 45 comment · 23 complexity · df1e45a7f4e3a39fb822484ced71a021 MD5 · raw file

  1. module Sass
  2. module Tree
  3. # A static node that is the root node of the Sass document.
  4. class RootNode < Node
  5. # The Sass template from which this node was created
  6. #
  7. # @param template [String]
  8. attr_reader :template
  9. # @param template [String] The Sass template from which this node was created
  10. def initialize(template)
  11. super()
  12. @template = template
  13. end
  14. # @see Node#to_s
  15. def to_s(*args)
  16. super
  17. rescue Sass::SyntaxError => e
  18. e.sass_template ||= @template
  19. raise e
  20. end
  21. # Runs the dynamic Sass code *and* computes the CSS for the tree.
  22. #
  23. # @see #perform
  24. # @see #to_s
  25. def render
  26. result, extends = perform(Environment.new).cssize
  27. result = result.do_extend(extends) unless extends.empty?
  28. result.to_s
  29. end
  30. # @see Node#perform
  31. def perform(environment)
  32. environment.options = @options if environment.options.nil? || environment.options.empty?
  33. super
  34. rescue Sass::SyntaxError => e
  35. e.sass_template ||= @template
  36. raise e
  37. end
  38. # Like {Node#cssize}, except that this method
  39. # will create its own `extends` map if necessary,
  40. # and it returns that map along with the cssized tree.
  41. #
  42. # @return [(Tree::Node, Haml::Util::SubsetMap)] The resulting tree of static nodes
  43. # *and* the extensions defined for this tree
  44. def cssize(extends = Haml::Util::SubsetMap.new, parent = nil)
  45. return super(extends, parent), extends
  46. rescue Sass::SyntaxError => e
  47. e.sass_template ||= @template
  48. raise e
  49. end
  50. # @see \{Node#perform!}
  51. def perform!(environment)
  52. environment.options = @options if environment.options.nil? || environment.options.empty?
  53. super
  54. end
  55. # Converts a node to Sass code that will generate it.
  56. #
  57. # @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize})
  58. # @return [String] The Sass code corresponding to the node
  59. def to_sass(opts = {})
  60. to_src(opts, :sass)
  61. end
  62. # Converts a node to SCSS code that will generate it.
  63. #
  64. # @param opts [{Symbol => Object}] An options hash (see {Sass::CSS#initialize})
  65. # @return [String] The SCSS code corresponding to the node
  66. def to_scss(opts = {})
  67. to_src(opts, :scss)
  68. end
  69. protected
  70. # @see Node#to_src
  71. def to_src(opts, fmt)
  72. Haml::Util.enum_cons(children + [nil], 2).map do |child, nxt|
  73. child.send("to_#{fmt}", 0, opts) +
  74. if nxt &&
  75. (child.is_a?(CommentNode) && child.line + child.value.count("\n") + 1 == nxt.line) ||
  76. (child.is_a?(ImportNode) && nxt.is_a?(ImportNode) && child.line + 1 == nxt.line) ||
  77. (child.is_a?(VariableNode) && nxt.is_a?(VariableNode) && child.line + 1 == nxt.line)
  78. ""
  79. else
  80. "\n"
  81. end
  82. end.join.rstrip + "\n"
  83. end
  84. # Computes the CSS corresponding to this Sass tree.
  85. #
  86. # @param args [Array] ignored
  87. # @return [String] The resulting CSS
  88. # @see Sass::Tree
  89. def _to_s(*args)
  90. result = String.new
  91. children.each do |child|
  92. next if child.invisible?
  93. child_str = child.to_s(1)
  94. result << child_str + (style == :compressed ? '' : "\n")
  95. end
  96. result.rstrip!
  97. return "" if result.empty?
  98. result << "\n"
  99. unless Haml::Util.ruby1_8? || result.ascii_only?
  100. if children.first.is_a?(CharsetNode)
  101. begin
  102. encoding = children.first.name
  103. # Default to big-endian encoding, because we have to decide somehow
  104. encoding << 'BE' if encoding =~ /\Autf-(16|32)\Z/i
  105. result = result.encode(Encoding.find(encoding))
  106. rescue EncodingError
  107. end
  108. end
  109. result = "@charset \"#{result.encoding.name}\";#{
  110. style == :compressed ? '' : "\n"
  111. }".encode(result.encoding) + result
  112. end
  113. result
  114. end
  115. # In Ruby 1.8, ensures that there's only one @charset directive
  116. # and that it's at the top of the document.
  117. #
  118. # @see Node#cssize
  119. def cssize!(extends, parent)
  120. super
  121. # In Ruby 1.9 we can make all @charset nodes invisible
  122. # and infer the final @charset from the encoding of the final string.
  123. if Haml::Util.ruby1_8? && parent.nil?
  124. charset = self.children.find {|c| c.is_a?(CharsetNode)}
  125. self.children.reject! {|c| c.is_a?(CharsetNode)}
  126. self.children.unshift charset if charset
  127. end
  128. end
  129. # Returns an error message if the given child node is invalid,
  130. # and false otherwise.
  131. #
  132. # Only property nodes are invalid at root level.
  133. #
  134. # @see Node#invalid_child?
  135. def invalid_child?(child)
  136. case child
  137. when Tree::ExtendNode
  138. "Extend directives may only be used within rules."
  139. when Tree::PropNode
  140. "Properties aren't allowed at the root of a document." +
  141. child.pseudo_class_selector_message
  142. else
  143. return
  144. end
  145. end
  146. end
  147. end
  148. end