Class: SassC::Script::String

Inherits:
Object
  • Object
show all
Defined in:
lib/sassc/script/string.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new string.

Parameters:

  • value (String)

    See #value

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

    See #type



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

def initialize(value, type = :identifier)
  value.freeze unless value.nil? || value == true || value == false
  @value = 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`



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

def type
  @type
end

#valueString (readonly)

The Ruby value of the string.

Returns:



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

def value
  @value
end

Class Method Details

.quote(contents, quote = nil) ⇒ Object



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
58
59
60
# File 'lib/sassc/script/string.rb', line 30

def self.quote(contents, quote = nil)
  # 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("\\", "\\\\\\\\")

  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

.type(contents) ⇒ Object



62
63
64
# File 'lib/sassc/script/string.rb', line 62

def self.type(contents)
  unquote(contents) == contents ? :identifier : :string
end

.unquote(contents) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sassc/script/string.rb', line 66

def self.unquote(contents)
  s = contents.dup

  case contents[0,1]
  when "'", '"', '`'
    s[0] = ''
  end

  case contents[-1,1]
  when "'", '"', '`'
    s[-1] = ''
  end

  return s
end

.value(contents) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sassc/script/string.rb', line 16

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

#to_nativeObject



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

def to_native
  Native::make_string(to_s)
end

#to_s(opts = {}) ⇒ Object

See Also:

  • Value#to_s


97
98
99
100
# File 'lib/sassc/script/string.rb', line 97

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