File: string.rb

package info (click to toggle)
ruby-sassc 2.4.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 368 kB
  • sloc: ruby: 2,277; makefile: 3
file content (96 lines) | stat: -rw-r--r-- 2,579 bytes parent folder | download | duplicates (3)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true

class SassC::Script::Value::String < SassC::Script::Value

  # The Ruby value of the 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

  # Returns the quoted string representation of `contents`.
  #
  # @options opts :quote [String]
  #   The preferred quote style for quoted strings. If `:none`, strings are
  #   always emitted unquoted. If `nil`, quoting is determined automatically.
  # @options opts :sass [String]
  #   Whether to quote strings for Sass source, as opposed to CSS. Defaults to `false`.
  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

  # Creates a new string.
  #
  # @param value [String] See \{#value}
  # @param type [Symbol] See \{#type}
  # @param 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.
  def initialize(value, type = :identifier)
    super(value)
    @type = type
  end

  # @see Value#plus
  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

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

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

  def inspect
    String.quote(value)
  end

end