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

Inherits:
Base
  • Object
show all
Defined in:
.ruby-sass/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

Direct Known Subclasses

ArgList

Instance Attribute Summary (collapse)

Attributes inherited from Base

#options, #source_range

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Base

#==, #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.

Parameters:



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

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

Constructor Details

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

Creates a new list.

Parameters:



27
28
29
30
31
# File '.ruby-sass/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.

Returns:

  • (Boolean)


20
21
22
# File '.ruby-sass/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`.

Returns:

  • (Symbol)


15
16
17
# File '.ruby-sass/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.

Returns:



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

def value
  @value
end

Class Method Details

.assert_valid_index(list, n) ⇒ Object

Asserts an index is within the list.

Parameters:



102
103
104
105
106
107
108
109
110
111
# File '.ruby-sass/lib/sass/script/value/list.rb', line 102

def self.assert_valid_index(list, n)
  if !n.int? || n.to_i == 0
    raise ArgumentError.new("List index #{n} must be a non-zero integer")
  elsif list.to_a.size == 0
    raise ArgumentError.new("List index is #{n} but list has no items")
  elsif n.to_i.abs > (size = list.to_a.size)
    raise ArgumentError.new(
      "List index is #{n} but list is only #{size} item#{'s' if size != 1} long")
  end
end

Instance Method Details

#eq(other) ⇒ Object

See Also:

  • Value#eq


40
41
42
43
44
# File '.ruby-sass/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

#hashObject



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

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

#inspectObject

See Also:

  • Value#inspect


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

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

#options=(options) ⇒ Object

See Also:

  • Value#options=


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

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

#to_hObject

See Also:

  • Value#to_h


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

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

#to_s(opts = {}) ⇒ Object

See Also:

  • Value#to_s


51
52
53
54
55
56
57
58
59
60
61
62
# File '.ruby-sass/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 = {}) ⇒ Object

See Also:

  • Value#to_sass


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File '.ruby-sass/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