Class: SassC::Script::Value::String

Inherits:
SassC::Script::Value show all
Defined in:
lib/sassc/script/value/string.rb

Instance Attribute Summary collapse

Attributes inherited from SassC::Script::Value

#options, #source_range

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SassC::Script::Value

#==, #assert_int!, #bracketed, #eql?, #hash, #null?, #separator, #to_a, #to_bool, #to_h, #to_i, #with_contents

Constructor Details

#initialize(value, type = :identifier) ⇒ String

Creates a new string.

Parameters:

  • value (String)

    See #value

  • type (Symbol) (defaults to: :identifier)

    See #type

  • 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.



66
67
68
69
# File 'lib/sassc/script/value/string.rb', line 66

def initialize(value, type = :identifier)
  super(value)
  @type = type
end

Instance Attribute Details

#typeSymbol (readonly)

Whether this is a CSS string or a CSS identifier. The difference is that strings are written with double-quotes, while identifiers aren’t.

Returns:

  • (Symbol)

    ‘:string` or `:identifier`



13
14
15
# File 'lib/sassc/script/value/string.rb', line 13

def type
  @type
end

#valueObject (readonly)

The Ruby value of the string.



6
7
8
# File 'lib/sassc/script/value/string.rb', line 6

def value
  @value
end

Class Method Details

.quote(contents, opts = {}) ⇒ Object

Returns the quoted string representation of ‘contents`.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

Instance Method Details

#inspectObject



92
93
94
# File 'lib/sassc/script/value/string.rb', line 92

def inspect
  String.quote(value)
end

#plus(other) ⇒ Object

See Also:

  • Value#plus


72
73
74
75
76
77
78
79
# 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 = {}) ⇒ Object



82
83
84
85
# 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 = {}) ⇒ Object



88
89
90
# File 'lib/sassc/script/value/string.rb', line 88

def to_sass(opts = {})
  to_s(opts.merge(:sass => true))
end