class Sass::Script::Value::List

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

Attributes

separator[R]

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

@return [Symbol]

to_a[R]

The Ruby array containing the contents of the list.

@return [Array<Value>]

value[R]

The Ruby array containing the contents of the list.

@return [Array<Value>]

Public Class Methods

assert_valid_index(list, n) click to toggle source

Asserts an index is within the list.

@private

@param list [Sass::Script::Value::List] The list for which the index should be checked. @param n [Sass::Script::Value::Number] The index being checked.

# File lib/sass/script/value/list.rb, line 82
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
new(value, separator) click to toggle source

Creates a new list.

@param value [Array<Value>] See {#value} @param separator [Symbol] See {#separator}

Calls superclass method Sass::Script::Value::Base.new
# File lib/sass/script/value/list.rb, line 21
def initialize(value, separator)
  super(value)
  @separator = separator
end

Public Instance Methods

eq(other) click to toggle source

@see Value#eq

# File lib/sass/script/value/list.rb, line 33
def eq(other)
  Sass::Script::Value::Bool.new(
    other.is_a?(List) && value == other.value &&
    separator == other.separator)
end
hash() click to toggle source
# File lib/sass/script/value/list.rb, line 39
def hash
  @hash ||= [value, separator].hash
end
inspect() click to toggle source

@see Value#inspect

# File lib/sass/script/value/list.rb, line 72
def inspect
  "(#{value.map {|e| e.inspect}.join(sep_str(nil))})"
end
options=(options) click to toggle source

@see Value#options=

Calls superclass method
# File lib/sass/script/value/list.rb, line 27
def options=(options)
  super
  value.each {|v| v.options = options}
end
to_h() click to toggle source

@see Value#to_h

Calls superclass method Sass::Script::Value::Base#to_h
# File lib/sass/script/value/list.rb, line 66
def to_h
  return Sass::Util.ordered_hash if value.empty?
  super
end
to_s(opts = {}) click to toggle source

@see Value#to_s

# File lib/sass/script/value/list.rb, line 44
def to_s(opts = {})
  raise Sass::SyntaxError.new("() isn't a valid CSS value.") if value.empty?
  value.
    reject {|e| e.is_a?(Null) || e.is_a?(List) && e.value.empty?}.
    map {|e| e.to_s(opts)}.join(sep_str)
end
to_sass(opts = {}) click to toggle source

@see Value#to_sass

# File lib/sass/script/value/list.rb, line 52
def to_sass(opts = {})
  return "()" if value.empty?
  members = value.map do |v|
    if element_needs_parens?(v)
      "(#{v.to_sass(opts)})"
    else
      v.to_sass(opts)
    end
  end
  return "(#{members.first},)" if members.length == 1 && separator == :comma
  members.join(sep_str(nil))
end

Private Instance Methods

element_needs_parens?(element) click to toggle source
# File lib/sass/script/value/list.rb, line 95
def element_needs_parens?(element)
  if element.is_a?(List)
    return false if element.value.empty?
    precedence = Sass::Script::Parser.precedence_of(separator)
    return Sass::Script::Parser.precedence_of(element.separator) <= precedence
  end

  return false unless separator == :space
  return false unless element.is_a?(Sass::Script::Tree::UnaryOperation)
  element.operator == :minus || element.operator == :plus
end
sep_str(opts = options) click to toggle source
# File lib/sass/script/value/list.rb, line 107
def sep_str(opts = options)
  return ' ' if separator == :space
  return ',' if opts && opts[:style] == :compressed
  ', '
end