Code Monkey home page Code Monkey logo

specificity's Introduction

Specificity Calculator

A JavaScript module for calculating and comparing the specificity of CSS selectors. The module is used on the Specificity Calculator website.

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 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.

Front-end usage

SPECIFICITY.calculate('ul#nav li.active a');   // [{ specificity: '0,1,1,3' }]

Node.js usage

var specificity = require('specificity');
specificity.calculate('ul#nav li.active a');   // [{ specificity: '0,1,1,3' }]

Passing in multiple selectors

You can use comma separation to pass in multiple selectors:

SPECIFICITY.calculate('ul#nav li.active a, body.ie7 .col_3 h2 ~ h2');   // [{ specificity: '0,1,1,3' }, { specificity: '0,0,2,3' }]

Return values

The specificity.calculate function returns an array containing a result object for each selector input. Each result object has the following properties:

  • selector: the input
  • specificity: the result as a string e.g. 0,1,0,0
  • specificityArray: the result as an array of numbers e.g. [0, 1, 0, 0]
  • parts: array with details about each part of the selector that counts towards the specificity

Example

var specificity = require('../'),
    result = specificity.calculate('ul#nav li.active a');

console.log(result);

/* result =
[ {
    selector: 'ul#nav li.active a',
    specificity: '0,1,1,3',
    specificityArray: [0, 1, 1, 3],
    parts: [
      { selector: 'ul', type: 'c', index: 0, length: 2 },
      { selector: '#nav', type: 'a', index: 2, length: 4 },
      { selector: 'li', type: 'c', index: 5, length: 2 },
      { selector: '.active', type: 'b', index: 8, length: 7 },
      { selector: 'a', type: 'c', index: 13, length: 1 }
    ]
} ]
*/

Comparing two selectors

Specificity Calculator also exposes a compare function. This function accepts two CSS selectors or specificity arrays, a and b.

  • It returns -1 if a has a lower specificity than b
  • It returns 1 if a has a higher specificity than b
  • It returns 0 if a has the same specificity than b
SPECIFICITY.compare('div', '.active');         // -1
SPECIFICITY.compare('#main', 'div');           // 1
SPECIFICITY.compare('span', 'div');            // 0
SPECIFICITY.compare('span', [0,0,0,1]);        // 0
SPECIFICITY.compare('#main > div', [0,1,0,1]); // 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.

['#main', 'p', '.active'].sort(SPECIFICITY.compare);   // ['p', '.active', '#main']

Command-line usage

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:

$ specificity
Usage: specificity <selector>
Computes specificity of a CSS selector.

Pass a selector as the first argument to get its specificity computed:

$ specificity "ul#nav li.active a"
0,1,1,3

Testing

To install dependencies, run: npm install

Then to test, run: npm test

specificity's People

Contributors

azproduction avatar elliotekj avatar jhaines avatar keeganstreet avatar prantlf avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.