Socket
Socket
Sign inDemoInstall

@csstools/selector-specificity

Package Overview
Dependencies
3
Maintainers
3
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@csstools/selector-specificity

Determine selector specificity with postcss-selector-parser


Version published
Maintainers
3
Weekly downloads
5,707,571
decreased by-19.21%
Bundle size
913 bytes
Minified + gzipped

Weekly downloads

Readme

Source

Selector Specificity

npm version Build Status Discord

Usage

Add Selector Specificity to your project:

npm install @csstools/selector-specificity --save-dev
import parser from 'postcss-selector-parser';
import { selectorSpecificity } from '@csstools/selector-specificity';

const selectorAST = parser().astSync('#foo:has(> .foo)');
const specificity = selectorSpecificity(selectorAST);

console.log(specificity.a); // 1
console.log(specificity.b); // 1
console.log(specificity.c); // 0

selectorSpecificity takes a single selector, not a list of selectors (not : a, b, c). To compare or otherwise manipulate lists of selectors you need to call selectorSpecificity on each part.

Comparing

The package exports a utility function to compare two specificities.

import { selectorSpecificity, compare } from '@csstools/selector-specificity';

const s1 = selectorSpecificity(ast1);
const s2 = selectorSpecificity(ast2);
compare(s1, s2); // -1 | 0 | 1
  • if s1 < s2 then compare(s1, s2) returns a negative number (< 0)
  • if s1 > s2 then compare(s1, s2) returns a positive number (> 0)
  • if s1 === s2 then compare(s1, s2) returns zero (=== 0)

Prior Art

For CSSTools we always use postcss-selector-parser and want to calculate specificity from this AST.

Keywords

FAQs

Last updated on 31 Mar 2024

Did you know?

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc