# class Sass::Script::Tree::ListLiteral

A parse tree node representing a list literal. When resolved, this returns a {Sass::Tree::Value::List}.

### Attributes

bracketed[R]

Whether the list is surrounded by square brackets.

@return [Boolean]

elements[R]

The parse nodes for members of this list.

@return [Array<Node>]

separator[R]

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

@return [Symbol]

### Public Class Methods

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

Creates a new list literal.

@param elements [Array<Node>] See {#elements} @param separator [Symbol] See {#separator} @param bracketed [Boolean] See {#bracketed}

    # File lib/sass/script/tree/list_literal.rb, line 26
    def initialize(elements, separator: nil, bracketed: false)
      @elements = elements
      @separator = separator
      @bracketed = bracketed
    end

### Public Instance Methods

children() click to toggle source

@see Sass::Script::Tree::Node#children

    # File lib/sass/script/tree/list_literal.rb, line 33
    def children; elements; end

deep_copy() click to toggle source

@see Sass::Script::Tree::Node#deep_copy

    # File lib/sass/script/tree/list_literal.rb, line 55
    def deep_copy
      node = dup
      node.instance_variable_set('@elements', elements.map {|e| e.deep_copy})
      node
    end

force_division!() click to toggle source

    # File lib/sass/script/tree/list_literal.rb, line 67
    def force_division!
      # Do nothing. Lists prevent division propagation.
    end

inspect() click to toggle source

    # File lib/sass/script/tree/list_literal.rb, line 61
    def inspect
      (bracketed ? '[' : '(') +
        elements.map {|e| e.inspect}.join(separator == :space ? ' ' : ', ') +
        (bracketed ? ']' : ')')
    end

to_sass(opts = {}) click to toggle source

@see Value#to_sass

    # File lib/sass/script/tree/list_literal.rb, line 36
    def to_sass(opts = {})
      return bracketed ? "[]" : "()" if elements.empty?
      members = elements.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

### Protected Instance Methods

_perform(environment) click to toggle source

    # File lib/sass/script/tree/list_literal.rb, line 73
    def _perform(environment)
      list = Sass::Script::Value::List.new(
        elements.map {|e| e.perform(environment)},
        separator: separator,
        bracketed: bracketed)
      list.source_range = source_range
      list.options = options
      list
    end

### Private Instance Methods

element_needs_parens?(element) click to toggle source

Returns whether an element in the list should be wrapped in parentheses when serialized to Sass.

    # File lib/sass/script/tree/list_literal.rb, line 87
    def element_needs_parens?(element)
      if element.is_a?(ListLiteral)
        return false if element.elements.length < 2
        return false if element.bracketed
        return Sass::Script::Parser.precedence_of(element.separator || :space) <=
               Sass::Script::Parser.precedence_of(separator || :space)
      end

      return false unless separator == :space

      if element.is_a?(UnaryOperation)
        return element.operator == :minus || element.operator == :plus
      end

      return false unless element.is_a?(Operation)
      return true unless element.operator == :div
      !(is_literal_number?(element.operand1) && is_literal_number?(element.operand2))
    end

is_literal_number?(value) click to toggle source

Returns whether a value is a number literal that shouldn't be divided.

    # File lib/sass/script/tree/list_literal.rb, line 107
    def is_literal_number?(value)
      value.is_a?(Literal) &&
        value.value.is_a?((Sass::Script::Value::Number)) &&
        !value.value.original.nil?
    end

sep_str(opts = options) click to toggle source

    # File lib/sass/script/tree/list_literal.rb, line 113
    def sep_str(opts = options)
      return ' ' if separator == :space
      return ',' if opts && opts[:style] == :compressed
      ', '
    end