Socket
Book a DemoInstallSign in
Socket

css-priority

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

css-priority

calculate priority of css selector/rule

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
60
39.53%
Maintainers
1
Weekly downloads
 
Created
Source

css-priority

calculate priority of css selector/rule

priority

We use Array(6) to store the priority value:

// rank algorithm
// -----------------
// a. important mark
// b. style attribte
// c. ID attribute (size)
// d. attributes + pseudo-class (size)
// e. element + pseudo-element (size)
// f. position of selector
var priority = [a, b, c, d, e, f]

Usage

calculate priority of selector (with rule info):

var priority = require('css-priority')

// output: [0, 0, 0, 0, 0, 12]
priority.parse('*', {
  line: 12
})

// output: [1, 0, 0, 2, 3, 88]
priority.parse('button.button > i.icon:before', {
  important: true,
  line: 88
})

Compare selectors:

var priority = require('css-priority')

var selectors = [
  '*'
  'li'
  'li:first-line'
  'ul li'
  'ul ol + li'
  'h1 + *[rel=up]'
  'ul ol li.red'
  'li.red.level'
]
var cache = {}

selectors.sort(function (a, b) {
  var calcA = cache[a] || (cache[a] = priority.parse(a))
  var calcB = cache[b] || (cache[b] = priority.parse(b))

  return priority.compare(a, b)
})

API

priority.parse(selector, opts)

  • selector: {String} css selector
  • opts: {Object} rule info
    • opts.important: {Boolean} important mark
    • opts.line: {Number} line number

parse css selector with rule info, and return Array(6) of priority

priority.compare(array1, array2)

  • array1: {Array}
  • array2: {Array}

Compare different priority array

LICENSE

MIT

Keywords

css-rank

FAQs

Package last updated on 25 Feb 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.