Sha256: 56a308b0769fa368eee8628ff8697cc6a9175d7cb8f5015a026326cc3e594189

Contents?: true

Size: 1.96 KB

Versions: 124

Compression:

Stored size: 1.96 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

    # In addition to setting the \{#context} of the string,
    # this sets the string to be an identifier if the context is `:equals`.
    #
    # @see Node#context=
    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

    # @see Literal#plus
    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 = {})
      if self.type == :identifier
        return %q{""} if context == :equals && self.value.size == 0
        return self.value.gsub("\n", " ")
      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

    # @see Node#to_sass
    def to_sass(opts = {})
      if self.type == :identifier && context == :equals &&
          self.value !~ Sass::SCSS::RX::URI &&
          Sass::SCSS::RX.escape_ident(self.value).include?(?\\)
        return "unquote(#{Sass::Script::String.new(self.value, :string).to_sass})"
      else
        return to_s
      end
    end
  end
end

Version data entries

124 entries across 123 versions & 5 rubygems

Version Path
radiant-1.0.0.rc1 vendor/plugins/haml/lib/sass/script/string.rb
haml-3.0.25 lib/sass/script/string.rb
radiantcms-couchrest_model-0.1.3 vendor/plugins/haml/lib/sass/script/string.rb
radiantcms-couchrest_model-0.1.2 vendor/plugins/haml/lib/sass/script/string.rb
radiantcms-couchrest_model-0.1.1 vendor/plugins/haml/lib/sass/script/string.rb
radiantcms-couchrest_model-0.1 vendor/plugins/haml/lib/sass/script/string.rb
haml-3.0.24 lib/sass/script/string.rb
haml-3.0.23 lib/sass/script/string.rb
haml-3.0.22 lib/sass/script/string.rb
haml-3.1.0.alpha.19 vendor/sass/lib/sass/script/string.rb
haml-3.1.0.alpha.18 vendor/sass/lib/sass/script/string.rb
haml-3.1.0.alpha.17 vendor/sass/lib/sass/script/string.rb
sass-3.1.0.alpha.12 lib/sass/script/string.rb
sass-3.1.0.alpha.11 lib/sass/script/string.rb
sass-3.1.0.alpha.10 lib/sass/script/string.rb
haml-3.0.21 vendor/sass/lib/sass/script/string.rb
haml-3.0.21 lib/sass/script/string.rb
sass-3.1.0.alpha.9 lib/sass/script/string.rb
sass-3.1.0.alpha.6 lib/sass/script/string.rb
sass-3.1.0.alpha.5 lib/sass/script/string.rb