class Roadie::Selector

@api private

A selector is a domain object for a CSS selector, such as:

body
a:hover
input::placeholder
p:nth-of-child(4n+1) .important a img

“Selectors” such as “strong, em” are actually two selectors and should be represented as two instances of this class.

This class can also calculate specificity for the selector and answer a few questions about them.

Selectors can be coerced into Strings, so they should be transparent to use anywhere a String is expected.

Constants

BAD_PSEUDO_FUNCTIONS

Attributes

selector[R]

Public Class Methods

new(selector, specificity = nil) click to toggle source
# File lib/roadie/selector.rb, line 21
def initialize(selector, specificity = nil)
  @selector = selector.to_s.strip
  @specificity = specificity
end

Public Instance Methods

==(other) click to toggle source

{Selector}s are equal to other {Selector}s if, and only if, their string representations are equal.

Calls superclass method
# File lib/roadie/selector.rb, line 54
def ==(other)
  if other.is_a?(self.class)
    other.selector == selector
  else
    super
  end
end
inlinable?() click to toggle source

Returns whenever or not a selector can be inlined. It's impossible to inline properties that applies to a pseudo element (like ::placeholder, ::before) or a pseudo function (like :active).

We cannot inline styles that appear inside “@” constructs, like +@keyframes+.

# File lib/roadie/selector.rb, line 36
def inlinable?
  !(pseudo_element? || at_rule? || pseudo_function?)
end
inspect() click to toggle source
# File lib/roadie/selector.rb, line 48
def inspect
  selector.inspect
end
specificity() click to toggle source

Returns the specificity of the selector, calculating it if needed.

# File lib/roadie/selector.rb, line 27
def specificity
  @specificity ||= CssParser.calculate_specificity selector
end
to_s() click to toggle source
# File lib/roadie/selector.rb, line 40
def to_s
  selector
end
to_str() click to toggle source
# File lib/roadie/selector.rb, line 44
def to_str
  to_s
end

Private Instance Methods

at_rule?() click to toggle source
# File lib/roadie/selector.rb, line 80
def at_rule?
  selector[0, 1] == "@"
end
pseudo_element?() click to toggle source
# File lib/roadie/selector.rb, line 76
def pseudo_element?
  selector.include? "::"
end
pseudo_function?() click to toggle source
# File lib/roadie/selector.rb, line 84
def pseudo_function?
  BAD_PSEUDO_FUNCTIONS.any? { |bad| selector.include?(bad) }
end