Class: Sass::Script::Value::String

Inherits:
Base
  • Object
show all
Defined in:
.ruby-sass/lib/sass/script/value/string.rb

Overview

A SassScript object representing a CSS string or a CSS identifier.

Constant Summary

@@interpolation_deprecation =
Sass::Deprecation.new

Instance Attribute Summary (collapse)

Attributes inherited from Base

#options, #source_range

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Base

#==, #assert_int!, #bracketed, #div, #eq, #eql?, #hash, #minus, #neq, #null?, #single_eq, #to_bool, #to_h, #to_i, #unary_div, #unary_minus, #unary_not, #unary_plus, #with_contents

Constructor Details

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

Creates a new string.

Parameters:

  • value (String)

    See #value

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

    See #type

  • deprecated_interp_equivalent (String?) (defaults to: nil)

    If this was created via a potentially-deprecated string interpolation, this is the replacement expression that should be suggested to the user.



84
85
86
87
88
# File '.ruby-sass/lib/sass/script/value/string.rb', line 84

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

Constructor Details

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

Creates a new string.

Parameters:

  • value (String)

    See #value

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

    See #type

  • deprecated_interp_equivalent (String?) (defaults to: nil)

    If this was created via a potentially-deprecated string interpolation, this is the replacement expression that should be suggested to the user.



84
85
86
87
88
# File '.ruby-sass/lib/sass/script/value/string.rb', line 84

def initialize(value, type = :identifier, deprecated_interp_equivalent = nil)
  super(value)
  @type = type
  @deprecated_interp_equivalent = deprecated_interp_equivalent
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`



17
18
19
# File '.ruby-sass/lib/sass/script/value/string.rb', line 17

def type
  @type
end

#valueString (readonly)

The Ruby value of the string.

Returns:



10
11
12
# File '.ruby-sass/lib/sass/script/value/string.rb', line 10

def value
  @value
end

Class Method Details

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

Returns the quoted string representation of `contents`.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File '.ruby-sass/lib/sass/script/value/string.rb', line 40

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

.value(contents) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File '.ruby-sass/lib/sass/script/value/string.rb', line 19

def self.value(contents)
  contents.gsub("\\\n", "").gsub(/\\(?:([0-9a-fA-F]{1,6})\s?|(.))/) do
    next $2 if $2
    # Handle unicode escapes as per CSS Syntax Level 3 section 4.3.8.
    code_point = $1.to_i(16)
    if code_point == 0 || code_point > 0x10FFFF ||
        (code_point >= 0xD800 && code_point <= 0xDFFF)
      ''
    else
      [code_point].pack("U")
    end
  end
end

Instance Method Details

#check_deprecated_interpObject

Prints a warning if this string was created using potentially-deprecated interpolation.



123
124
125
126
127
128
129
130
131
132
# File '.ruby-sass/lib/sass/script/value/string.rb', line 123

def check_deprecated_interp
  return unless @deprecated_interp_equivalent

  @@interpolation_deprecation.warn(source_range.file, source_range.start_pos.line, <<WARNING)
\#{} interpolation near operators will be simplified in a future version of Sass.
To preserve the current behavior, use quotes:

  #{@deprecated_interp_equivalent}
WARNING
end

#inspectObject



134
135
136
# File '.ruby-sass/lib/sass/script/value/string.rb', line 134

def inspect
  String.quote(value)
end

#plus(other) ⇒ Object

See Also:

  • Value#plus


91
92
93
94
95
96
97
98
# File '.ruby-sass/lib/sass/script/value/string.rb', line 91

def plus(other)
  other_value = if other.is_a?(Sass::Script::Value::String)
                  other.value
                else
                  other.to_s(:quote => :none)
                end
  Sass::Script::Value::String.new(value + other_value, type)
end

#separatorObject



111
112
113
114
# File '.ruby-sass/lib/sass/script/value/string.rb', line 111

def separator
  check_deprecated_interp
  super
end

#to_aObject



116
117
118
119
# File '.ruby-sass/lib/sass/script/value/string.rb', line 116

def to_a
  check_deprecated_interp
  super
end

#to_s(opts = {}) ⇒ Object

See Also:

  • Value#to_s


101
102
103
104
# File '.ruby-sass/lib/sass/script/value/string.rb', line 101

def to_s(opts = {})
  return @value.gsub(/\n\s*/, ' ') if opts[:quote] == :none || @type == :identifier
  String.quote(value, opts)
end

#to_sass(opts = {}) ⇒ Object

See Also:

  • Value#to_sass


107
108
109
# File '.ruby-sass/lib/sass/script/value/string.rb', line 107

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