1 # Specificity Calculator
3 A JavaScript module for calculating and comparing the [specificity of CSS selectors](http://www.w3.org/TR/css3-selectors/#specificity). The module is used on the [Specificity Calculator](http://specificity.keegan.st/) website.
5 Specificity Calculator is built for CSS Selectors Level 3. Specificity Calculator isn’t a CSS validator. If you enter invalid selectors it will return incorrect results. For example, the [negation pseudo-class](http://www.w3.org/TR/css3-selectors/#negation) may only take a simple selector as an argument. Using a psuedo-element or combinator as an argument for `:not()` is invalid CSS3 so Specificity Calculator will return incorrect results.
11 SPECIFICITY.calculate('ul#nav li.active a'); // [{ specificity: '0,1,1,3' }]
17 var specificity = require('specificity');
18 specificity.calculate('ul#nav li.active a'); // [{ specificity: '0,1,1,3' }]
21 ## Passing in multiple selectors
23 You can use comma separation to pass in multiple selectors:
26 SPECIFICITY.calculate('ul#nav li.active a, body.ie7 .col_3 h2 ~ h2'); // [{ specificity: '0,1,1,3' }, { specificity: '0,0,2,3' }]
31 The `specificity.calculate` function returns an array containing a result object for each selector input. Each result object has the following properties:
33 * `selector`: the input
34 * `specificity`: the result as a string e.g. `0,1,0,0`
35 * `specificityArray`: the result as an array of numbers e.g. `[0, 1, 0, 0]`
36 * `parts`: array with details about each part of the selector that counts towards the specificity
41 var specificity = require('../'),
42 result = specificity.calculate('ul#nav li.active a');
48 selector: 'ul#nav li.active a',
49 specificity: '0,1,1,3',
50 specificityArray: [0, 1, 1, 3],
52 { selector: 'ul', type: 'c', index: 0, length: 2 },
53 { selector: '#nav', type: 'a', index: 2, length: 4 },
54 { selector: 'li', type: 'c', index: 5, length: 2 },
55 { selector: '.active', type: 'b', index: 8, length: 7 },
56 { selector: 'a', type: 'c', index: 13, length: 1 }
62 ## Comparing two selectors
64 Specificity Calculator also exposes a `compare` function. This function accepts two CSS selectors or specificity arrays, `a` and `b`.
66 * It returns `-1` if `a` has a lower specificity than `b`
67 * It returns `1` if `a` has a higher specificity than `b`
68 * It returns `0` if `a` has the same specificity than `b`
71 SPECIFICITY.compare('div', '.active'); // -1
72 SPECIFICITY.compare('#main', 'div'); // 1
73 SPECIFICITY.compare('span', 'div'); // 0
74 SPECIFICITY.compare('span', [0,0,0,1]); // 0
75 SPECIFICITY.compare('#main > div', [0,1,0,1]); // 0
78 ## Ordering an array of selectors by specificity
80 You can pass the `SPECIFICITY.compare` function to `Array.prototype.sort` to sort an array of CSS selectors by specificity.
83 ['#main', 'p', '.active'].sort(SPECIFICITY.compare); // ['p', '.active', '#main']
88 Run `npm install specificity` to install the module locally, or `npm install -g specificity` for global installation. You may need to elevate permissions by `sudo` for the latter. Run `specificity` without arguments to learn about its usage:
92 Usage: specificity <selector>
93 Computes specificity of a CSS selector.
96 Pass a selector as the first argument to get its specificity computed:
99 $ specificity "ul#nav li.active a"
105 To install dependencies, run: `npm install`
107 Then to test, run: `npm test`