Coder Social home page Coder Social logo

specimen's Introduction

specimen

A CSS selector specificity calculator.

usage

npm install specimen
var specimen = require('specimen');

specimen('#home #warning p.message');
//=> [0, 2, 1, 1]
specimen('#home #warning p.message, .footer a[rel=me]:hover');
//=> [ [0,2,1,1], [0,0,3,1] ]

specimen([
  '#home #warning p.message',
  '.footer a[rel=me]:hover'
]);
//=> [ [0,2,1,1], [0,0,3,1] ]

api

specimen(selectors)

Calculates the specificity of one or more CSS selectors. The selectors argument is a string containing one or more selectors, or an array of selector strings.

Returns an array representing the specificity, or undefined if a non-string was given.

For multiple selectors, whether passed as string or array, an array of specificities is returned. Returns undefined if all values in the array would have been undefined.

specimen's specificity

Specificity is represented as a four element array where each element corresponds to a selector category.

The selector categories are, in order of most to least specific:

  • i: inline styles
  • a: IDs
  • b: classes, attributes & psuedo-classes
  • c: elements & pseudo-elements

how it works

A a selector is split up into chunks and sorted into selector categories. The categories are tallied, and the result is specificity: [i, a, b, c].

specimen('#home #warning p.message');
//=> [0, 2, 1, 1]

style="!important"

Specimen calculates selector specificity only; it cannot know the specificity of inline styles. It also cannot account for the use of !important.

gigo

Specimen will attempt to calculate the CSS selector specificty of any string. A result of [0, 0, 0, 0] means the string is probably not a CSS selector, or contains only the universal selector "*" which has no affect on specificity.

credits

Specimen is based heavily on keeganstreet/specificity.

I needed a small specificity module for another project and Keegan Street's specificity fit the bill perfectly. But I also needed a small project to help curb my fear of programming. I decided I didn't need the additional information that specificity provides. I settled on a simpler API and specimen was born.

specimen's People

Contributors

brettstimmerman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jonnyscholes

specimen's Issues

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.