Sha256: 9395c336bb58b414308ed72294af3fb1a7b1ab14cfd61589cc321cebe326b9ac

Contents?: true

Size: 1.91 KB

Versions: 18

Compression:

Stored size: 1.91 KB

Contents

require 'sass/script/literal'

module Sass::Script
  # A SassScript object representing a CSS string *or* a CSS identifier.
  class String < Literal
    # The Ruby value of the string.
    #
    # @return [String]
    attr_reader :value

    # Whether this is a CSS string or a CSS identifier.
    # The difference is that strings are written with double-quotes,
    # while identifiers aren't.
    #
    # @return [Symbol] `:string` or `:identifier`
    attr_reader :type

    def context=(context)
      super
      @type = :identifier if context == :equals
    end

    # Creates a new string.
    #
    # @param value [String] See \{#value}
    # @param type [Symbol] See \{#type}
    def initialize(value, type = :identifier)
      super(value)
      @type = type
    end

    def plus(other)
      other_str = other.is_a?(Sass::Script::String) ? other.value : other.to_s
      Sass::Script::String.new(self.value + other_str, self.type)
    end

    # @see Node#to_s
    def to_s(opts = {})
      to_sass(opts)
    end

    # @param opts [{Symbol => Object}]
    #   `opts[:type]` -- The type of string to render this as.
    #     `:string`s have double quotes, `:identifier`s do not.
    #     Defaults to `:identifier`.
    # @see Node#to_sass
    def to_sass(opts = {})
      type = opts[:type] || self.type
      if type == :identifier
        if context == :equals && Sass::SCSS::RX.escape_ident(self.value).include?(?\\)
          return "unquote(#{Sass::Script::String.new(self.value, :string).to_sass})"
        elsif context == :equals && self.value.size == 0
          return %q{""}
        end
        return self.value
      end

      return "\"#{value.gsub('"', "\\\"")}\"" if opts[:quote] == %q{"}
      return "'#{value.gsub("'", "\\'")}'" if opts[:quote] == %q{'}
      return "\"#{value}\"" unless value.include?('"')
      return "'#{value}'" unless value.include?("'")
      "\"#{value.gsub('"', "\\\"")}\"" #'
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
haml-edge-2.3.209 lib/sass/script/string.rb
haml-3.0.0.beta.3 lib/sass/script/string.rb
haml-edge-2.3.208 lib/sass/script/string.rb
haml-edge-2.3.207 lib/sass/script/string.rb
haml-edge-2.3.206 lib/sass/script/string.rb
haml-edge-2.3.205 lib/sass/script/string.rb
haml-edge-2.3.204 lib/sass/script/string.rb
haml-3.0.0.beta.2 lib/sass/script/string.rb
haml-edge-2.3.203 lib/sass/script/string.rb
haml-edge-2.3.202 lib/sass/script/string.rb
haml-edge-2.3.201 lib/sass/script/string.rb
haml-edge-2.3.200 lib/sass/script/string.rb
haml-edge-2.3.199 lib/sass/script/string.rb
haml-edge-2.3.198 lib/sass/script/string.rb
haml-edge-2.3.197 lib/sass/script/string.rb
haml-edge-2.3.196 lib/sass/script/string.rb
haml-edge-2.3.195 lib/sass/script/string.rb
haml-edge-2.3.194 lib/sass/script/string.rb