/
string.rb
70 lines (62 loc) · 2.1 KB
/
string.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
58
59
60
61
62
63
64
65
66
67
68
69
70
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 = {})
to_sass(opts)
end
# @param opts [{Symbol => Object}]
# `opts[:type]` -- The type of string to render this as.
# `:string`s have double quotes, `:identifier`s do not.
# Defaults to `:identifier`.
# @see Node#to_sass
def to_sass(opts = {})
type = opts[:type] || self.type
if type == :identifier
if context == :equals && Sass::SCSS::RX.escape_ident(self.value).include?(?\\)
return "unquote(#{Sass::Script::String.new(self.value, :string).to_sass})"
elsif context == :equals && self.value.size == 0
return %q{""}
end
return self.value
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
end
end