class SassC::Script::Value::String

Attributes

type[R]

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`

value[R]

The Ruby value of the string.

Public Class Methods

new(value, type = :identifier) click to toggle source

Creates a new string.

@param value [String] See {#value} @param type [Symbol] See {#type} @param deprecated_interp_equivalent [String?]

If this was created via a potentially-deprecated string interpolation,
this is the replacement expression that should be suggested to the user.
Calls superclass method SassC::Script::Value::new
# File lib/sassc/script/value/string.rb, line 66
def initialize(value, type = :identifier)
  super(value)
  @type = type
end
quote(contents, opts = {}) click to toggle source

Returns the quoted string representation of ‘contents`.

@options opts :quote [String]

The preferred quote style for quoted strings. If `:none`, strings are
always emitted unquoted. If `nil`, quoting is determined automatically.

@options opts :sass [String]

Whether to quote strings for Sass source, as opposed to CSS. Defaults to `false`.
# File lib/sassc/script/value/string.rb, line 22
def self.quote(contents, opts = {})
  quote = opts[:quote]

  # Short-circuit if there are no characters that need quoting.
  unless contents =~ /[\n\\"']|\#\{/
    quote ||= '"'
    return "#{quote}#{contents}#{quote}"
  end

  if quote.nil?
    if contents.include?('"')
      if contents.include?("'")
        quote = '"'
      else
        quote = "'"
      end
    else
      quote = '"'
    end
  end

  # Replace single backslashes with multiples.
  contents = contents.gsub("\\", "\\\\\\\\")

  # Escape interpolation.
  contents = contents.gsub('#{', "\\\#{") if opts[:sass]

  if quote == '"'
    contents = contents.gsub('"', "\\\"")
  else
    contents = contents.gsub("'", "\\'")
  end

  contents = contents.gsub(/\n(?![a-fA-F0-9\s])/, "\\a").gsub("\n", "\\a ")
  "#{quote}#{contents}#{quote}"
end

Public Instance Methods

inspect() click to toggle source
# File lib/sassc/script/value/string.rb, line 92
def inspect
  String.quote(value)
end
plus(other) click to toggle source

@see Value#plus

# File lib/sassc/script/value/string.rb, line 72
def plus(other)
  if other.is_a?(SassC::Script::Value::String)
    other_value = other.value
  else
    other_value = other.to_s(:quote => :none)
  end
  SassC::Script::Value::String.new(value + other_value, type)
end
to_s(opts = {}) click to toggle source

@see Value#to_s

# File lib/sassc/script/value/string.rb, line 82
def to_s(opts = {})
  return @value.gsub(/\n\s*/, ' ') if opts[:quote] == :none || @type == :identifier
  self.class.quote(value, opts)
end
to_sass(opts = {}) click to toggle source

@see Value#to_sass

# File lib/sassc/script/value/string.rb, line 88
def to_sass(opts = {})
  to_s(opts.merge(:sass => true))
end