# class Sass::Script::Value::List

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

### Attributes

bracketed[R]

Whether the list is surrounded by square brackets.

@return [Boolean]

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 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

new(value, separator: nil, bracketed: false) click to toggle source

Creates a new list.

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

Calls superclass method Sass::Script::Value::Base.new

    # File lib/sass/script/value/list.rb, line 27
    def initialize(value, separator: nil, bracketed: false)
      super(value)
      @separator = separator
      @bracketed = bracketed
    end

### Public Instance Methods

eq(other) click to toggle source

@see Value#eq

    # 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() click to toggle source

    # File lib/sass/script/value/list.rb, line 46
    def hash
      @hash ||= [value, separator, bracketed].hash
    end

inspect() click to toggle source

@see Value#inspect

    # 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) click to toggle source

@see Value#options=

Calls superclass method

    # File lib/sass/script/value/list.rb, line 34
    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 84
    def to_h
      return {} if value.empty?
      super
    end

to_s(opts = {}) click to toggle source

@see Value#to_s

    # 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 = {}) click to toggle source

@see Value#to_sass

    # 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

### Private Instance Methods

element_needs_parens?(element) click to toggle source

    # File lib/sass/script/value/list.rb, line 115
    def element_needs_parens?(element)
      if element.is_a?(List)
        return false if element.value.length < 2
        return false if element.bracketed
        precedence = Sass::Script::Parser.precedence_of(separator || :space)
        return Sass::Script::Parser.precedence_of(element.separator || :space) <= 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 128
    def sep_str(opts = options)
      return ' ' if separator == :space
      return ',' if opts && opts[:style] == :compressed
      ', '
    end