/scalate-jruby/src/main/resources/haml-3.0.25/lib/sass/script/literal.rb

http://github.com/scalate/scalate · Ruby · 236 lines · 92 code · 26 blank · 118 comment · 5 complexity · 6e0dd23efa27e5e499097bf2befb4586 MD5 · raw file

  1. module Sass::Script
  2. # The abstract superclass for SassScript objects.
  3. #
  4. # Many of these methods, especially the ones that correspond to SassScript operations,
  5. # are designed to be overridden by subclasses which may change the semantics somewhat.
  6. # The operations listed here are just the defaults.
  7. class Literal < Node
  8. require 'sass/script/string'
  9. require 'sass/script/number'
  10. require 'sass/script/color'
  11. require 'sass/script/bool'
  12. # Returns the Ruby value of the literal.
  13. # The type of this value varies based on the subclass.
  14. #
  15. # @return [Object]
  16. attr_reader :value
  17. # Creates a new literal.
  18. #
  19. # @param value [Object] The object for \{#value}
  20. def initialize(value = nil)
  21. @value = value
  22. super()
  23. end
  24. # Returns an empty array.
  25. #
  26. # @return [Array<Node>] empty
  27. # @see Node#children
  28. def children
  29. []
  30. end
  31. # Returns the options hash for this node.
  32. #
  33. # @return [{Symbol => Object}]
  34. # @raise [Sass::SyntaxError] if the options hash hasn't been set.
  35. # This should only happen when the literal was created
  36. # outside of the parser and \{#to\_s} was called on it
  37. def options
  38. opts = super
  39. return opts if opts
  40. raise Sass::SyntaxError.new(<<MSG)
  41. The #options attribute is not set on this #{self.class}.
  42. This error is probably occurring because #to_s was called
  43. on this literal within a custom Sass function without first
  44. setting the #option attribute.
  45. MSG
  46. end
  47. # The SassScript `and` operation.
  48. #
  49. # @param other [Literal] The right-hand side of the operator
  50. # @return [Literal] The result of a logical and:
  51. # `other` if this literal isn't a false {Bool},
  52. # and this literal otherwise
  53. def and(other)
  54. to_bool ? other : self
  55. end
  56. # The SassScript `or` operation.
  57. #
  58. # @param other [Literal] The right-hand side of the operator
  59. # @return [Literal] The result of the logical or:
  60. # this literal if it isn't a false {Bool},
  61. # and `other` otherwise
  62. def or(other)
  63. to_bool ? self : other
  64. end
  65. # The SassScript `==` operation.
  66. # **Note that this returns a {Sass::Script::Bool} object,
  67. # not a Ruby boolean**.
  68. #
  69. # @param other [Literal] The right-hand side of the operator
  70. # @return [Bool] True if this literal is the same as the other,
  71. # false otherwise
  72. def eq(other)
  73. Sass::Script::Bool.new(self.class == other.class && self.value == other.value)
  74. end
  75. # The SassScript `!=` operation.
  76. # **Note that this returns a {Sass::Script::Bool} object,
  77. # not a Ruby boolean**.
  78. #
  79. # @param other [Literal] The right-hand side of the operator
  80. # @return [Bool] False if this literal is the same as the other,
  81. # true otherwise
  82. def neq(other)
  83. Sass::Script::Bool.new(!eq(other).to_bool)
  84. end
  85. # The SassScript `==` operation.
  86. # **Note that this returns a {Sass::Script::Bool} object,
  87. # not a Ruby boolean**.
  88. #
  89. # @param other [Literal] The right-hand side of the operator
  90. # @return [Bool] True if this literal is the same as the other,
  91. # false otherwise
  92. def unary_not
  93. Sass::Script::Bool.new(!to_bool)
  94. end
  95. # The SassScript default operation (e.g. `$a $b`, `"foo" "bar"`).
  96. #
  97. # @param other [Literal] The right-hand side of the operator
  98. # @return [Script::String] A string containing both literals
  99. # separated by a space
  100. def concat(other)
  101. Sass::Script::String.new("#{self.to_s} #{other.to_s}")
  102. end
  103. # The SassScript `,` operation (e.g. `$a, $b`, `"foo", "bar"`).
  104. #
  105. # @param other [Literal] The right-hand side of the operator
  106. # @return [Script::String] A string containing both literals
  107. # separated by `", "`
  108. def comma(other)
  109. Sass::Script::String.new("#{self.to_s},#{' ' unless options[:style] == :compressed}#{other.to_s}")
  110. end
  111. # The SassScript `=` operation
  112. # (used for proprietary MS syntax like `alpha(opacity=20)`).
  113. #
  114. # @param other [Literal] The right-hand side of the operator
  115. # @return [Script::String] A string containing both literals
  116. # separated by `"="`
  117. def single_eq(other)
  118. Sass::Script::String.new("#{self.to_s}=#{other.to_s}")
  119. end
  120. # The SassScript `+` operation.
  121. #
  122. # @param other [Literal] The right-hand side of the operator
  123. # @return [Script::String] A string containing both literals
  124. # without any separation
  125. def plus(other)
  126. if other.is_a?(Sass::Script::String)
  127. return Sass::Script::String.new(self.to_s + other.value, other.type)
  128. end
  129. Sass::Script::String.new(self.to_s + other.to_s)
  130. end
  131. # The SassScript `-` operation.
  132. #
  133. # @param other [Literal] The right-hand side of the operator
  134. # @return [Script::String] A string containing both literals
  135. # separated by `"-"`
  136. def minus(other)
  137. Sass::Script::String.new("#{self.to_s}-#{other.to_s}")
  138. end
  139. # The SassScript `/` operation.
  140. #
  141. # @param other [Literal] The right-hand side of the operator
  142. # @return [Script::String] A string containing both literals
  143. # separated by `"/"`
  144. def div(other)
  145. Sass::Script::String.new("#{self.to_s}/#{other.to_s}")
  146. end
  147. # The SassScript unary `+` operation (e.g. `+$a`).
  148. #
  149. # @param other [Literal] The right-hand side of the operator
  150. # @return [Script::String] A string containing the literal
  151. # preceded by `"+"`
  152. def unary_plus
  153. Sass::Script::String.new("+#{self.to_s}")
  154. end
  155. # The SassScript unary `-` operation (e.g. `-$a`).
  156. #
  157. # @param other [Literal] The right-hand side of the operator
  158. # @return [Script::String] A string containing the literal
  159. # preceded by `"-"`
  160. def unary_minus
  161. Sass::Script::String.new("-#{self.to_s}")
  162. end
  163. # The SassScript unary `/` operation (e.g. `/$a`).
  164. #
  165. # @param other [Literal] The right-hand side of the operator
  166. # @return [Script::String] A string containing the literal
  167. # preceded by `"/"`
  168. def unary_div
  169. Sass::Script::String.new("/#{self.to_s}")
  170. end
  171. # @return [String] A readable representation of the literal
  172. def inspect
  173. value.inspect
  174. end
  175. # @return [Boolean] `true` (the Ruby boolean value)
  176. def to_bool
  177. true
  178. end
  179. # Compares this object with another.
  180. #
  181. # @param other [Object] The object to compare with
  182. # @return [Boolean] Whether or not this literal is equivalent to `other`
  183. def ==(other)
  184. eq(other).to_bool
  185. end
  186. # @return [Fixnum] The integer value of this literal
  187. # @raise [Sass::SyntaxError] if this literal isn't an integer
  188. def to_i
  189. raise Sass::SyntaxError.new("#{self.inspect} is not an integer.")
  190. end
  191. # @raise [Sass::SyntaxError] if this literal isn't an integer
  192. def assert_int!; to_i; end
  193. # Returns the string representation of this literal
  194. # as it would be output to the CSS document.
  195. #
  196. # @return [String]
  197. def to_s(opts = {})
  198. raise Sass::SyntaxError.new("[BUG] All subclasses of Sass::Literal must implement #to_s.")
  199. end
  200. alias_method :to_sass, :to_s
  201. protected
  202. # Evaluates the literal.
  203. #
  204. # @param environment [Sass::Environment] The environment in which to evaluate the SassScript
  205. # @return [Literal] This literal
  206. def _perform(environment)
  207. self
  208. end
  209. end
  210. end