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

Methods inherited from AbstractSequence

#_specificity, #eql?, #has_placeholder?, #hash, #specificity, #to_s

Constructor Details

#initialize(selectors, subject, source_range = nil) ⇒ SimpleSequence

Returns a new instance of SimpleSequence.

Parameters:



69
70
71
72
73
74
# File 'lib/sass/selector/simple_sequence.rb', line 69

def initialize(selectors, subject, source_range = nil)
  @members = selectors
  @subject = subject
  @sources = Set.new
  @source_range = source_range
end

Instance Attribute Details

#membersArray<Simple>

The array of individual selectors.

Returns:



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

def members
  @members
end

#source_rangeSass::Source::Range

This sequence source range.

Returns:



32
33
34
# File 'lib/sass/selector/simple_sequence.rb', line 32

def source_range
  @source_range
end

#sourcesSet<Sequence>

The extending selectors that caused this selector sequence to be generated. For example:

a.foo { ... }
b.bar {@extend a}
c.baz {@extend b}

The generated selector b.foo.bar has {b.bar} as its sources set, and the generated selector c.foo.bar.baz has {b.bar, c.baz} as its sources set.

This is populated during the Sass::Selector::Sequence#do_extend process.

Returns:



27
28
29
# File 'lib/sass/selector/simple_sequence.rb', line 27

def sources
  @sources
end

#subject=(value) (writeonly)

See Also:

  • Sass::Selector::SimpleSequence.\{\{#subject?}


35
36
37
# File 'lib/sass/selector/simple_sequence.rb', line 35

def subject=(value)
  @subject = value
end

Instance Method Details

#baseElement, ...

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

Returns:



41
42
43
# File 'lib/sass/selector/simple_sequence.rb', line 41

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

#do_extend(extends, parent_directives) ⇒ Array<Sequence>

Non-destructively extends this selector with the extensions specified in a hash (which should come from Tree::Visitors::Cssize).

The extensions to perform on this selector

Parameters:

Returns:

  • (Array<Sequence>)

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

See Also:



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/sass/selector/simple_sequence.rb', line 143

def do_extend(extends, parent_directives, seen = Set.new)
  groups = Sass::Util.group_by_to_a(extends[members.to_set]) {|ex| ex.extender}
  groups.map! do |seq, group|
    sels = group.map {|e| e.target}.flatten
    # If A {@extend B} and C {...},
    # seq is A, sels is B, and self is C

    self_without_sel = Sass::Util.array_minus(members, sels)
    group.each {|e| e.result = :failed_to_unify unless e.result == :succeeded}
    unified = seq.members.last.unify(self_without_sel, subject?)
    next unless unified
    group.each {|e| e.result = :succeeded}
    group.each {|e| check_directives_match!(e, parent_directives)}
    new_seq = Sequence.new(seq.members[0...-1] + [unified])
    new_seq.add_sources!(sources + [seq])
    [sels, new_seq]
  end
  groups.compact!
  groups.map! do |sels, seq|
    seen.include?(sels) ? [] : seq.do_extend(extends, parent_directives, seen + [sels])
  end
  groups.flatten!
  groups.uniq!
  groups
end

#inspectString

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

Returns:

  • (String)


217
218
219
# File 'lib/sass/selector/simple_sequence.rb', line 217

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

#pseudo_elements



45
46
47
48
# File 'lib/sass/selector/simple_sequence.rb', line 45

def pseudo_elements
  @pseudo_elements ||= (members - [base]).
    select {|sel| sel.is_a?(Pseudo) && sel.type == :element}
end

#resolve_parent_refs(super_cseq) ⇒ CommaSequence

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

Parameters:

Returns:

  • (CommaSequence)

    This selector, with parent references resolved

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sass/selector/simple_sequence.rb', line 83

def resolve_parent_refs(super_cseq)
  # Parent selector only appears as the first selector in the sequence
  unless (parent = @members.first).is_a?(Parent)
    return CommaSequence.new([Sequence.new([self])])
  end

  return super_cseq if @members.size == 1 && parent.suffix.empty?

  CommaSequence.new(super_cseq.members.map do |super_seq|
    members = super_seq.members.dup
    newline = members.pop if members.last == "\n"
    unless members.last.is_a?(SimpleSequence)
      raise Sass::SyntaxError.new("Invalid parent selector for \"#{self}\": \"" +
        super_seq.to_a.join + '"')
    end

    parent_sub = members.last.members
    unless parent.suffix.empty?
      parent_sub = parent_sub.dup
      parent_sub[-1] = parent_sub.last.dup
      case parent_sub.last
      when Sass::Selector::Class, Sass::Selector::Id, Sass::Selector::Placeholder
        parent_sub[-1] = parent_sub.last.class.new(parent_sub.last.name + parent.suffix)
      when Sass::Selector::Element
        parent_sub[-1] = parent_sub.last.class.new(
          parent_sub.last.name + parent.suffix,
          parent_sub.last.namespace)
      when Sass::Selector::Pseudo
        if parent_sub.last.arg
          raise Sass::SyntaxError.new("Invalid parent selector for \"#{self}\": \"" +
            super_seq.to_a.join + '"')
        end
        parent_sub[-1] = parent_sub.last.class.new(
          parent_sub.last.type,
          parent_sub.last.name + parent.suffix,
          nil)
      else
        raise Sass::SyntaxError.new("Invalid parent selector for \"#{self}\": \"" +
          super_seq.to_a.join + '"')
      end
    end

    Sequence.new(members[0...-1] +
      [SimpleSequence.new(parent_sub + @members[1..-1], subject?)] +
      [newline].compact)
  end)
end

#restSet<Simple>

Returns the non-base, non-pseudo-class selectors in this sequence.

Returns:



53
54
55
# File 'lib/sass/selector/simple_sequence.rb', line 53

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

#subject?Boolean

Whether or not this compound selector is the subject of the parent selector; that is, whether it is prepended with $ and represents the actual element that will be selected.

Returns:

  • (Boolean)


62
63
64
# File 'lib/sass/selector/simple_sequence.rb', line 62

def subject?
  @subject
end

#superselector?(sseq) ⇒ Boolean

Returns whether or not this selector matches all elements that the given selector matches (as well as possibly more).

Examples:

(.foo).superselector?(.foo.bar) #=> true
(.foo).superselector?(.bar) #=> false

Parameters:

Returns:

  • (Boolean)


200
201
202
203
204
# File 'lib/sass/selector/simple_sequence.rb', line 200

def superselector?(sseq)
  (base.nil? || base.eql?(sseq.base)) &&
    pseudo_elements.eql?(sseq.pseudo_elements) &&
    rest.subset?(sseq.rest)
end

#to_a



207
208
209
210
211
# File 'lib/sass/selector/simple_sequence.rb', line 207

def to_a
  res = @members.map {|sel| sel.to_a}.flatten
  res << '!' if subject?
  res
end

#unify(sels, other_subject) ⇒ 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



183
184
185
186
187
188
189
190
# File 'lib/sass/selector/simple_sequence.rb', line 183

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

#with_more_sources(sources) ⇒ SimpleSequence

Return a copy of this simple sequence with sources merged into the #sources set.

Parameters:

Returns:



226
227
228
229
230
231
# File 'lib/sass/selector/simple_sequence.rb', line 226

def with_more_sources(sources)
  sseq = dup
  sseq.members = members.dup
  sseq.sources = self.sources | sources
  sseq
end