PageRenderTime 48ms CodeModel.GetById 2ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 1ms

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