PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/scalate/scalate
Ruby | 67 lines | 39 code | 8 blank | 20 comment | 14 complexity | e6c0d823bbd37dc76bccfebde9d00ed4 MD5 | raw file
 1require 'sass/script/literal'
 2
 3module Sass::Script
 4  # A SassScript object representing a CSS string *or* a CSS identifier.
 5  class String < Literal
 6    # The Ruby value of the string.
 7    #
 8    # @return [String]
 9    attr_reader :value
10
11    # Whether this is a CSS string or a CSS identifier.
12    # The difference is that strings are written with double-quotes,
13    # while identifiers aren't.
14    #
15    # @return [Symbol] `:string` or `:identifier`
16    attr_reader :type
17
18    # In addition to setting the \{#context} of the string,
19    # this sets the string to be an identifier if the context is `:equals`.
20    #
21    # @see Node#context=
22    def context=(context)
23      super
24      @type = :identifier if context == :equals
25    end
26
27    # Creates a new string.
28    #
29    # @param value [String] See \{#value}
30    # @param type [Symbol] See \{#type}
31    def initialize(value, type = :identifier)
32      super(value)
33      @type = type
34    end
35
36    # @see Literal#plus
37    def plus(other)
38      other_str = other.is_a?(Sass::Script::String) ? other.value : other.to_s
39      Sass::Script::String.new(self.value + other_str, self.type)
40    end
41
42    # @see Node#to_s
43    def to_s(opts = {})
44      if self.type == :identifier
45        return %q{""} if context == :equals && self.value.size == 0
46        return self.value.gsub("\n", " ")
47      end
48
49      return "\"#{value.gsub('"', "\\\"")}\"" if opts[:quote] == %q{"}
50      return "'#{value.gsub("'", "\\'")}'" if opts[:quote] == %q{'}
51      return "\"#{value}\"" unless value.include?('"')
52      return "'#{value}'" unless value.include?("'")
53      "\"#{value.gsub('"', "\\\"")}\"" #'
54    end
55
56    # @see Node#to_sass
57    def to_sass(opts = {})
58      if self.type == :identifier && context == :equals &&
59          self.value !~ Sass::SCSS::RX::URI &&
60          Sass::SCSS::RX.escape_ident(self.value).include?(?\\)
61        return "unquote(#{Sass::Script::String.new(self.value, :string).to_sass})"
62      else
63        return to_s
64      end
65    end
66  end
67end