Class: Sass::Script::Value::List

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

Overview

A SassScript object representing a CSS list. This includes both comma-separated lists and space-separated lists.

Direct Known Subclasses

ArgList

Instance Attribute Summary collapse

Attributes inherited from Base

#options, #source_range

Instance Method Summary collapse

Methods inherited from Base

#==, #_perform, #assert_int!, #div, #eql?, #minus, #neq, #null?, #plus, #single_eq, #to_bool, #to_i, #unary_div, #unary_minus, #unary_not, #unary_plus, #with_contents

Constructor Details

#initialize(value, separator: nil, bracketed: false) ⇒ List

Creates a new list.


27
28
29
30
31
# File 'lib/sass/script/value/list.rb', line 27

def initialize(value, separator: nil, bracketed: false)
  super(value)
  @separator = separator
  @bracketed = bracketed
end

Instance Attribute Details

#bracketedBoolean (readonly)

Whether the list is surrounded by square brackets.


20
21
22
# File 'lib/sass/script/value/list.rb', line 20

def bracketed
  @bracketed
end

#separatorSymbol (readonly)

The operator separating the values of the list. Either :comma or :space.


15
16
17
# File 'lib/sass/script/value/list.rb', line 15

def separator
  @separator
end

#valueArray<Value> (readonly) Also known as: to_a

The Ruby array containing the contents of the list.


8
9
10
# File 'lib/sass/script/value/list.rb', line 8

def value
  @value
end

Instance Method Details

#eq(other)

See Also:

  • Value#eq

40
41
42
43
44
# File 'lib/sass/script/value/list.rb', line 40

def eq(other)
  Sass::Script::Value::Bool.new(
    other.is_a?(List) && value == other.value &&
    separator == other.separator && bracketed == other.bracketed)
end

#hash


46
47
48
# File 'lib/sass/script/value/list.rb', line 46

def hash
  @hash ||= [value, separator, bracketed].hash
end

#inspect

See Also:

  • Value#inspect

90
91
92
93
94
# File 'lib/sass/script/value/list.rb', line 90

def inspect
  (bracketed ? '[' : '(') +
    value.map {|e| e.inspect}.join(sep_str(nil)) +
    (bracketed ? ']' : ')')
end

#options=(options)

See Also:

  • Value#options=

34
35
36
37
# File 'lib/sass/script/value/list.rb', line 34

def options=(options)
  super
  value.each {|v| v.options = options}
end

#to_h

See Also:

  • Value#to_h

84
85
86
87
# File 'lib/sass/script/value/list.rb', line 84

def to_h
  return {} if value.empty?
  super
end

#to_s(opts = {})

See Also:

  • Value#to_s

51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sass/script/value/list.rb', line 51

def to_s(opts = {})
  if !bracketed && value.empty?
    raise Sass::SyntaxError.new("#{inspect} isn't a valid CSS value.")
  end

  members = value.
    reject {|e| e.is_a?(Null) || e.is_a?(List) && e.value.empty?}.
    map {|e| e.to_s(opts)}

  contents = members.join(sep_str)
  bracketed ? "[#{contents}]" : contents
end

#to_sass(opts = {})

See Also:

  • Value#to_sass

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sass/script/value/list.rb', line 65

def to_sass(opts = {})
  return bracketed ? "[]" : "()" if value.empty?
  members = value.map do |v|
    if element_needs_parens?(v)
      "(#{v.to_sass(opts)})"
    else
      v.to_sass(opts)
    end
  end

  if separator == :comma && members.length == 1
    return "#{bracketed ? '[' : '('}#{members.first},#{bracketed ? ']' : ')'}"
  end

  contents = members.join(sep_str(nil))
  bracketed ? "[#{contents}]" : contents
end