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

http://github.com/scalate/scalate · Ruby · 220 lines · 116 code · 27 blank · 77 comment · 38 complexity · 93d0ce079153b3817540c694f7be9c8a MD5 · raw file

  1. module Sass::Tree
  2. # A static node reprenting a CSS property.
  3. #
  4. # @see Sass::Tree
  5. class PropNode < Node
  6. # The name of the property,
  7. # interspersed with {Sass::Script::Node}s
  8. # representing `#{}`-interpolation.
  9. # Any adjacent strings will be merged together.
  10. #
  11. # @return [Array<String, Sass::Script::Node>]
  12. attr_accessor :name
  13. # The name of the property
  14. # after any interpolated SassScript has been resolved.
  15. # Only set once \{Tree::Node#perform} has been called.
  16. #
  17. # @return [String]
  18. attr_accessor :resolved_name
  19. # The value of the property.
  20. #
  21. # @return [Sass::Script::Node]
  22. attr_accessor :value
  23. # The value of the property
  24. # after any interpolated SassScript has been resolved.
  25. # Only set once \{Tree::Node#perform} has been called.
  26. #
  27. # @return [String]
  28. attr_accessor :resolved_value
  29. # How deep this property is indented
  30. # relative to a normal property.
  31. # This is only greater than 0 in the case that:
  32. #
  33. # * This node is in a CSS tree
  34. # * The style is :nested
  35. # * This is a child property of another property
  36. # * The parent property has a value, and thus will be rendered
  37. #
  38. # @return [Fixnum]
  39. attr_accessor :tabs
  40. # @param name [Array<String, Sass::Script::Node>] See \{#name}
  41. # @param value [Sass::Script::Node] See \{#value}
  42. # @param prop_syntax [Symbol] `:new` if this property uses `a: b`-style syntax,
  43. # `:old` if it uses `:a b`-style syntax
  44. def initialize(name, value, prop_syntax)
  45. @name = Haml::Util.strip_string_array(
  46. Haml::Util.merge_adjacent_strings(name))
  47. @value = value
  48. @tabs = 0
  49. @prop_syntax = prop_syntax
  50. super()
  51. end
  52. # Compares the names and values of two properties.
  53. #
  54. # @param other [Object] The object to compare with
  55. # @return [Boolean] Whether or not this node and the other object
  56. # are the same
  57. def ==(other)
  58. self.class == other.class && name == other.name && value == other.value && super
  59. end
  60. # Returns a appropriate message indicating how to escape pseudo-class selectors.
  61. # This only applies for old-style properties with no value,
  62. # so returns the empty string if this is new-style.
  63. #
  64. # @return [String] The message
  65. def pseudo_class_selector_message
  66. return "" if @prop_syntax == :new || !value.is_a?(Sass::Script::String) || !value.value.empty?
  67. "\nIf #{declaration.dump} should be a selector, use \"\\#{declaration}\" instead."
  68. end
  69. protected
  70. # @see Node#to_src
  71. def to_src(tabs, opts, fmt)
  72. res = declaration(tabs, opts, fmt)
  73. return res + "#{semi fmt}\n" if children.empty?
  74. res + children_to_src(tabs, opts, fmt).rstrip + semi(fmt) + "\n"
  75. end
  76. # Computes the CSS for the property.
  77. #
  78. # @param tabs [Fixnum] The level of indentation for the CSS
  79. # @return [String] The resulting CSS
  80. def _to_s(tabs)
  81. to_return = ' ' * (tabs - 1 + self.tabs) + resolved_name + ":" +
  82. (style == :compressed ? '' : ' ') + resolved_value + (style == :compressed ? "" : ";")
  83. end
  84. # Converts nested properties into flat properties.
  85. #
  86. # @param extends [Haml::Util::SubsetMap{Selector::Simple => Selector::Sequence}]
  87. # The extensions defined for this tree
  88. # @param parent [PropNode, nil] The parent node of this node,
  89. # or nil if the parent isn't a {PropNode}
  90. # @raise [Sass::SyntaxError] if the property uses invalid syntax
  91. def _cssize(extends, parent)
  92. node = super
  93. result = node.children.dup
  94. if !node.resolved_value.empty? || node.children.empty?
  95. node.send(:check!)
  96. result.unshift(node)
  97. end
  98. result
  99. end
  100. # Updates the name and indentation of this node based on the parent name
  101. # and nesting level.
  102. #
  103. # @param extends [Haml::Util::SubsetMap{Selector::Simple => Selector::Sequence}]
  104. # The extensions defined for this tree
  105. # @param parent [PropNode, nil] The parent node of this node,
  106. # or nil if the parent isn't a {PropNode}
  107. def cssize!(extends, parent)
  108. self.resolved_name = "#{parent.resolved_name}-#{resolved_name}" if parent
  109. self.tabs = parent.tabs + (parent.resolved_value.empty? ? 0 : 1) if parent && style == :nested
  110. super
  111. end
  112. # Runs any SassScript that may be embedded in the property,
  113. # and invludes the parent property, if any.
  114. #
  115. # @param environment [Sass::Environment] The lexical environment containing
  116. # variable and mixin values
  117. def perform!(environment)
  118. @resolved_name = run_interp(@name, environment)
  119. val = @value.perform(environment)
  120. @resolved_value =
  121. if @value.context == :equals && val.is_a?(Sass::Script::String)
  122. val.value
  123. else
  124. val.to_s
  125. end
  126. super
  127. end
  128. # Returns an error message if the given child node is invalid,
  129. # and false otherwise.
  130. #
  131. # {PropNode} only allows other {PropNode}s and {CommentNode}s as children.
  132. # @param child [Tree::Node] A potential child node
  133. # @return [String] An error message if the child is invalid, or nil otherwise
  134. def invalid_child?(child)
  135. if !child.is_a?(PropNode) && !child.is_a?(CommentNode)
  136. "Illegal nesting: Only properties may be nested beneath properties."
  137. end
  138. end
  139. private
  140. def check!
  141. if @options[:property_syntax] == :old && @prop_syntax == :new
  142. raise Sass::SyntaxError.new("Illegal property syntax: can't use new syntax when :property_syntax => :old is set.")
  143. elsif @options[:property_syntax] == :new && @prop_syntax == :old
  144. raise Sass::SyntaxError.new("Illegal property syntax: can't use old syntax when :property_syntax => :new is set.")
  145. elsif resolved_value.empty?
  146. raise Sass::SyntaxError.new("Invalid property: #{declaration.dump} (no value)." +
  147. pseudo_class_selector_message)
  148. end
  149. end
  150. def declaration(tabs = 0, opts = {:old => @prop_syntax == :old}, fmt = :sass)
  151. name = self.name.map {|n| n.is_a?(String) ? n : "\#{#{n.to_sass(opts)}}"}.join
  152. if name[0] == ?:
  153. raise Sass::SyntaxError.new("The \"#{name}: #{self.class.val_to_sass(value, opts)}\" hack is not allowed in the Sass indented syntax")
  154. end
  155. old = opts[:old] && fmt == :sass
  156. initial = old ? ':' : ''
  157. mid = old ? '' : ':'
  158. "#{' ' * tabs}#{initial}#{name}#{mid} #{self.class.val_to_sass(value, opts)}".rstrip
  159. end
  160. class << self
  161. # @private
  162. def val_to_sass(value, opts)
  163. val_to_sass_comma(value, opts).to_sass(opts)
  164. end
  165. private
  166. def val_to_sass_comma(node, opts)
  167. return node unless node.is_a?(Sass::Script::Operation)
  168. return val_to_sass_concat(node, opts) unless node.operator == :comma
  169. Sass::Script::Operation.new(
  170. val_to_sass_concat(node.operand1, opts),
  171. val_to_sass_comma(node.operand2, opts),
  172. node.operator)
  173. end
  174. def val_to_sass_concat(node, opts)
  175. return node unless node.is_a?(Sass::Script::Operation)
  176. return val_to_sass_div(node, opts) unless node.operator == :concat
  177. Sass::Script::Operation.new(
  178. val_to_sass_div(node.operand1, opts),
  179. val_to_sass_concat(node.operand2, opts),
  180. node.operator)
  181. end
  182. def val_to_sass_div(node, opts)
  183. unless node.is_a?(Sass::Script::Operation) && node.operator == :div &&
  184. node.operand1.is_a?(Sass::Script::Number) &&
  185. node.operand2.is_a?(Sass::Script::Number) &&
  186. (node.context == :equals || !node.operand1.original || !node.operand2.original)
  187. return node
  188. end
  189. Sass::Script::String.new("(#{node.to_sass(opts)})")
  190. end
  191. end
  192. end
  193. end