Class: Sass::Selector::Simple

Inherits:
Object
  • Object
show all
Defined in:
lib/sass/selector/simple.rb

Overview

The abstract superclass for simple selectors (that is, those that don't compose multiple selectors).

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameString?

The name of the file in which this selector was declared, or nil if it was not declared in a file (e.g. on stdin).

Returns:

  • (String, nil)


15
16
17
# File 'lib/sass/selector/simple.rb', line 15

def filename
  @filename
end

#lineFixnum

The line of the Sass template on which this selector was declared.

Returns:

  • (Fixnum)


9
10
11
# File 'lib/sass/selector/simple.rb', line 9

def line
  @line
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Checks equality between this and another object.

By default, this is based on the value of #to_a, so if that contains information irrelevant to the identity of the selector, this should be overridden.

Parameters:

  • other (Object)

    The object to test equality against

Returns:

  • (Boolean)

    Whether or not this is equal to other



55
56
57
# File 'lib/sass/selector/simple.rb', line 55

def eql?(other)
  other.class == self.class && other.hash == self.hash && other.to_a.eql?(to_a)
end

#hashFixnum

Returns a hash code for this selector object.

By default, this is based on the value of #to_a, so if that contains information irrelevant to the identity of the selector, this should be overridden.

Returns:

  • (Fixnum)


43
44
45
# File 'lib/sass/selector/simple.rb', line 43

def hash
  @_hash ||= to_a.hash
end

#inspectString

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

Returns:

  • (String)


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

def inspect
  to_a.map {|e| e.is_a?(Sass::Script::Node) ? "\#{#{e.to_sass}}" : e}.join
end

#to_aArray<String, Sass::Script::Node>

Returns a representation of the node as an array of strings and potentially Sass::Script::Nodes (if there's interpolation in the selector). When the interpolation is resolved and the strings are joined together, this will be the string representation of this node.

Returns:



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

def to_a
  Sass::Util.abstract(self)
end

#unify(sels) ⇒ Array<Simple>?

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

By default, this just appends this selector to the end of the array (or returns the original array if this selector already exists in it).

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



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sass/selector/simple.rb', line 77

def unify(sels)
  return sels if sels.any? {|sel2| eql?(sel2)}
  sels_with_ix = Sass::Util.enum_with_index(sels)
  _, i =
    if self.is_a?(Pseudo) || self.is_a?(SelectorPseudoClass)
      sels_with_ix.find {|sel, _| sel.is_a?(Pseudo) && sels.last.type == :element}
    else
      sels_with_ix.find {|sel, _| sel.is_a?(Pseudo) || sel.is_a?(SelectorPseudoClass)}
    end
  return sels + [self] unless i
  return sels[0...i] + [self] + sels[i..-1]
end

#unify_namespaces(ns1, ns2) ⇒ Array(String or nil, Boolean) (protected)

Unifies two namespaces, returning a namespace that works for both of them if possible.

Parameters:

  • ns1 (String, nil)

    The first namespace. nil means none specified, e.g. foo. The empty string means no namespace specified, e.g. |foo. "*" means any namespace is allowed, e.g. *|foo.

  • ns2 (String, nil)

    The second namespace. See ns1.

Returns:

  • (Array(String or nil, Boolean))

    The first value is the unified namespace, or nil for no namespace. The second value is whether or not a namespace that works for both inputs could be found at all. If the second value is false, the first should be ignored.



105
106
107
108
109
110
# File 'lib/sass/selector/simple.rb', line 105

def unify_namespaces(ns1, ns2)
  return nil, false unless ns1 == ns2 || ns1.nil? || ns1 == ['*'] || ns2.nil? || ns2 == ['*']
  return ns2, true if ns1 == ['*']
  return ns1, true if ns2 == ['*']
  return ns1 || ns2, true
end