Module: CSS::Selectors::SpecificityCalculator

Extended by:
SpecificityCalculator
Included in:
SpecificityCalculator
Defined in:
lib/css/selectors/specificity.rb

Overview

Computes specificity for any selector AST node.

Note on the nesting selector (‘&`): without parent context its specificity is conservatively reported as zero. Callers wanting accurate cascade behavior should run `CSS.desugar(stylesheet)` first so `&` is replaced by the parent’s compounds.

Instance Method Summary collapse

Instance Method Details

#calculate(node) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/css/selectors/specificity.rb', line 42

def calculate(node)
  case node
  when SelectorList     then node.selectors.map { calculate(it) }.max || Specificity::ZERO
  when ComplexSelector  then sum(node.compounds)
  when CompoundSelector then sum(node.components)
  when IdSelector       then Specificity.new(a: 1, b: 0, c: 0)
  when ClassSelector,
       AttributeSelector then Specificity.new(a: 0, b: 1, c: 0)
  when TypeSelector     then Specificity.new(a: 0, b: 0, c: 1)
  when PseudoElement    then Specificity.new(a: 0, b: 0, c: 1)
  when PseudoClass      then specificity_of_pseudo_class(node)
  when UniversalSelector,
       NestingSelector  then Specificity::ZERO
  else                       Specificity::ZERO
  end
end