keeganstreet / specificity

A JavaScript module for calculating the specificity of CSS selectors
MIT License
624 stars 39 forks source link

Added a compare function #20

Closed keeganstreet closed 8 years ago

keeganstreet commented 8 years ago

Comparing two selectors

Specificity Calculator now exposes a compare function. This function accepts two CSS selectors, a and b.

SPECIFICITY.compare('div', '.active');   // -1
SPECIFICITY.compare('#main', 'div');     // 1
SPECIFICITY.compare('span', 'div');      // 0

Ordering an array of selectors by specificity

You can pass the SPECIFICITY.compare function to Array.prototype.sort to sort an array of CSS selectors by specificity.