Class: CSS::Selectors::Specificity

Inherits:
Data
  • Object
show all
Includes:
Comparable
Defined in:
lib/css/selectors/specificity.rb

Overview

Selectors ยง16 specificity tuple โ€˜(a, b, c)`.

a = id selectors
b = class / attribute / pseudo-class selectors
c = type / pseudo-element selectors

Constant Summary collapse

ZERO =
Specificity.new(a: 0, b: 0, c: 0).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a

Returns:

  • (Object)

    the current value of a



7
8
9
# File 'lib/css/selectors/specificity.rb', line 7

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b

Returns:

  • (Object)

    the current value of b



7
8
9
# File 'lib/css/selectors/specificity.rb', line 7

def b
  @b
end

#cObject (readonly)

Returns the value of attribute c

Returns:

  • (Object)

    the current value of c



7
8
9
# File 'lib/css/selectors/specificity.rb', line 7

def c
  @c
end

Instance Method Details

#+(other) ⇒ Object



24
25
26
# File 'lib/css/selectors/specificity.rb', line 24

def +(other)
  Specificity.new(a: a + other.a, b: b + other.b, c: c + other.c)
end

#<=>(other) ⇒ Object

Avoids the per-call allocation of two 3-element arrays โ€” this comparison runs many times in the cascade-sort hot path.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/css/selectors/specificity.rb', line 12

def <=>(other)
  return nil unless other.is_a?(Specificity)

  d = a - other.a
  return d unless d.zero?

  d = b - other.b
  return d unless d.zero?

  c - other.c
end

#to_sObject



28
# File 'lib/css/selectors/specificity.rb', line 28

def to_s = "#{a},#{b},#{c}"