Class: Sass::Selector::SimpleSequence
- Inherits:
-
AbstractSequence
- Object
- AbstractSequence
- Sass::Selector::SimpleSequence
- 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
-
#members ⇒ Array<Simple>
readonly
The array of individual selectors.
Attributes inherited from AbstractSequence
Instance Method Summary collapse
-
#base ⇒ Element, ...
Returns the element or universal selector in this sequence, if it exists.
-
#do_extend(extends) ⇒ Array<Sequence>
Non-destrucively extends this selector with the extensions specified in a hash (which should come from Tree::Visitors::Cssize).
-
#initialize(selectors) ⇒ SimpleSequence
constructor
A new instance of SimpleSequence.
-
#inspect ⇒ String
Returns a string representation of the sequence.
-
#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.
-
#rest ⇒ Set<Simple>
Returns the non-base selectors in this sequence.
-
#superselector?(sseq) ⇒ Boolean
Returns whether or not this selector matches all elements that the given selector matches (as well as possibly more).
- #to_a
-
#unify(sels) ⇒ SimpleSequence?
Unifies this selector with another SimpleSequence's members array, returning another
SimpleSequence
that matches both this selector and the input selector.
Methods inherited from AbstractSequence
Constructor Details
#initialize(selectors) ⇒ SimpleSequence
Returns a new instance of SimpleSequence.
29 30 31 |
# File 'lib/sass/selector/simple_sequence.rb', line 29
def initialize(selectors)
@members = selectors
end
|
Instance Attribute Details
#members ⇒ Array<Simple> (readonly)
The array of individual selectors.
11 12 13 |
# File 'lib/sass/selector/simple_sequence.rb', line 11
def members
@members
end
|
Instance Method Details
#base ⇒ Element, ...
Returns the element or universal selector in this sequence, if it exists.
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 come from Tree::Visitors::Cssize).
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sass/selector/simple_sequence.rb', line 63
def do_extend(extends, seen = Set.new)
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 do |sels, seq|
seq = Sequence.new(seq)
seen.include?(sels) ? [] : seq.do_extend(extends, seen + [sels])
end.flatten.uniq
end
|
#inspect ⇒ String
Returns a string representation of the sequence. This is basically the selector string.
119 120 121 |
# File 'lib/sass/selector/simple_sequence.rb', line 119
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.
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
|
#rest ⇒ Set<Simple>
Returns the non-base selectors in this sequence.
24 25 26 |
# File 'lib/sass/selector/simple_sequence.rb', line 24
def rest
@rest ||= Set.new(base ? members[1..-1] : members)
end
|
#superselector?(sseq) ⇒ Boolean
Returns whether or not this selector matches all elements that the given selector matches (as well as possibly more).
106 107 108 |
# File 'lib/sass/selector/simple_sequence.rb', line 106
def superselector?(sseq)
(base.nil? || base.eql?(sseq.base)) && rest.subset?(sseq.rest)
end
|
#to_a
111 112 113 |
# File 'lib/sass/selector/simple_sequence.rb', line 111
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.
90 91 92 93 94 95 96 |
# File 'lib/sass/selector/simple_sequence.rb', line 90
def unify(sels)
return unless sseq = members.inject(sels) do |sseq, sel|
return unless sseq
sel.unify(sseq)
end
SimpleSequence.new(sseq)
end
|