Class: Sass::Selector::SimpleSequence

Inherits:
AbstractSequence show all
Defined in:
lib/sass/selector/simple_sequence.rb

Overview

A unseparated sequence of selectors that all apply to a single element. For example, .foo#bar[attr=baz] is a simple sequence of the selectors .foo, #bar, and [attr=baz].

Instance Attribute Summary collapse

Attributes inherited from AbstractSequence

#filename, #line

Instance Method Summary collapse

Constructor Details

#initialize(selectors) ⇒ SimpleSequence

Returns a new instance of SimpleSequence.

Parameters:



29
30
31
# File 'lib/sass/selector/simple_sequence.rb', line 29

def initialize(selectors)
  @members = selectors
end

Instance Attribute Details

#membersArray<Simple> (readonly)

The array of individual selectors.

Returns:



11
12
13
# File 'lib/sass/selector/simple_sequence.rb', line 11

def members
  @members
end

Instance Method Details

#baseElement, ...

Returns the element or universal selector in this sequence, if it exists.

Returns:



17
18
19
# File 'lib/sass/selector/simple_sequence.rb', line 17

def base
  @base ||= (members.first if members.first.is_a?(Element) || members.first.is_a?(Universal))
end

#do_extend(extends) ⇒ Array<Sequence>

Non-destrucively extends this selector with the extensions specified in a hash (which should be populated via Tree::Node#cssize).

Parameters:

Returns:

  • (Array<Sequence>)

    A list of selectors generated by extending this selector with extends.

See Also:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sass/selector/simple_sequence.rb', line 64

def do_extend(extends, supers = [])
  seqs = extends.get(members.to_set).map do |seq, sels|
    # If A {@extend B} and C {...},
    # seq is A, sels is B, and self is C

    self_without_sel = self.members - sels
    next unless unified = seq.members.last.unify(self_without_sel)
    [sels, seq.members[0...-1] + [unified]]
  end.compact.map {|sels, seq| [sels, Sequence.new(seq)]}

  seqs.map {|_, seq| seq}.concat(
    seqs.map do |sels, seq|
      new_seqs = seq.do_extend(extends, supers.unshift(sels))[1..-1]
      supers.shift
      new_seqs
    end.flatten.uniq)
rescue SystemStackError
  handle_extend_loop(supers)
end

#eql?(other) ⇒ Boolean

Checks equality between this and another object.

Parameters:

  • other (Object)

    The object to test equality against

Returns:

  • (Boolean)

    Whether or not this is equal to other



129
130
131
# File 'lib/sass/selector/simple_sequence.rb', line 129

def eql?(other)
  other.class == self.class && other.base.eql?(self.base) && other.rest.eql?(self.rest)
end

#hashFixnum

Returns a hash code for this sequence.

Returns:

  • (Fixnum)


121
122
123
# File 'lib/sass/selector/simple_sequence.rb', line 121

def hash
  [base, rest].hash
end

#inspectString

Returns a string representation of the sequence. This is basically the selector string.

Returns:

  • (String)


114
115
116
# File 'lib/sass/selector/simple_sequence.rb', line 114

def inspect
  members.map {|m| m.inspect}.join
end

#resolve_parent_refs(super_seq) ⇒ Array<SimpleSequence>

Resolves the Parent selectors within this selector by replacing them with the given parent selector, handling commas appropriately.

Parameters:

  • super_seq (Sequence)

    The parent selector sequence

Returns:

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sass/selector/simple_sequence.rb', line 41

def resolve_parent_refs(super_seq)
  # Parent selector only appears as the first selector in the sequence
  return [self] unless @members.first.is_a?(Parent)

  return super_seq.members if @members.size == 1
  unless super_seq.members.last.is_a?(SimpleSequence)
    raise Sass::SyntaxError.new("Invalid parent selector: " + super_seq.to_a.join)
  end

  super_seq.members[0...-1] +
    [SimpleSequence.new(super_seq.members.last.members + @members[1..-1])]
end

#restSet<Simple>

Returns the non-base selectors in this sequence.

Returns:



24
25
26
# File 'lib/sass/selector/simple_sequence.rb', line 24

def rest
  @rest ||= Set.new(base ? members[1..-1] : members)
end

#to_a



106
107
108
# File 'lib/sass/selector/simple_sequence.rb', line 106

def to_a
  @members.map {|sel| sel.to_a}.flatten
end

#unify(sels) ⇒ SimpleSequence?

Unifies this selector with another Sass::Selector::SimpleSequence's members array, returning another SimpleSequence that matches both this selector and the input selector.

Parameters:

Returns:

Raises:

  • (Sass::SyntaxError)

    If this selector cannot be unified. This will only ever occur when a dynamic selector, such as Parent or Interpolation, is used in unification. Since these selectors should be resolved by the time extension and unification happen, this exception will only ever be raised as a result of programmer error



97
98
99
100
101
102
103
# File 'lib/sass/selector/simple_sequence.rb', line 97

def unify(sels)
  return unless sseq = members.inject(sels) do |sseq, sel|
    return unless sseq
    sel.unify(sseq)
  end
  SimpleSequence.new(sseq)
end