Class: SassC::Script::Value::String
- Inherits:
-
SassC::Script::Value
- Object
- SassC::Script::Value
- SassC::Script::Value::String
- Defined in:
- lib/sassc/script/value/string.rb
Instance Attribute Summary collapse
-
#type ⇒ Symbol
readonly
Whether this is a CSS string or a CSS identifier.
-
#value ⇒ Object
readonly
The Ruby value of the string.
Attributes inherited from SassC::Script::Value
Class Method Summary collapse
-
.quote(contents, opts = {}) ⇒ Object
Returns the quoted string representation of ‘contents`.
Instance Method Summary collapse
-
#initialize(value, type = :identifier) ⇒ String
constructor
Creates a new string.
- #inspect ⇒ Object
- #plus(other) ⇒ Object
- #to_s(opts = {}) ⇒ Object
- #to_sass(opts = {}) ⇒ Object
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.
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
#type ⇒ Symbol (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.
13 14 15 |
# File 'lib/sassc/script/value/string.rb', line 13 def type @type end |
#value ⇒ Object (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
#inspect ⇒ Object
92 93 94 |
# File 'lib/sassc/script/value/string.rb', line 92 def inspect String.quote(value) end |
#plus(other) ⇒ Object
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 |