Skip to main content

Crate specificity

Crate specificity 

Source
Expand description

§specificity — calculate CSS selector specificity

Compute the specificity (a, b, c) of a CSS selector — a counts ID selectors, b counts classes, attributes and pseudo-classes, and c counts type selectors and pseudo-elements. Modern selectors are handled per the spec: :is(), :not(), :has() and :nth-child(… of S) take the specificity of their most specific argument, and :where() contributes nothing. The Rust counterpart of the specificity npm package. Zero dependencies and #![no_std].

use specificity::{specificity, Specificity};

assert_eq!(specificity("#id .cls a"), Specificity::new(1, 1, 1));
assert_eq!(specificity(":is(.a, #b)"), Specificity::new(1, 0, 0)); // max of args
assert_eq!(specificity(":where(.a)"), Specificity::new(0, 0, 0));  // contributes 0

// Specificity is Ord, so you can compare selectors directly.
assert!(specificity("#id") > specificity(".a.b.c"));

specificity takes a single complex selector (returning the maximum if you pass a comma-separated list); use specificity_list to get one value per selector.

Structs§

Specificity
A CSS specificity value: a (IDs), b (classes/attributes/pseudo-classes), and c (type selectors/pseudo-elements). Ordered a, then b, then c.

Functions§

specificity
Calculate the specificity of a single complex selector.
specificity_list
Calculate the specificity of each selector in a comma-separated list.