npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

postcss-set-specificity

v0.0.2

Published

Make the cascade more intuitive by setting selector specificities.

Downloads

8

Readme

postcss-set-specificity - Set selector specificity to equal another's

/* Input */
@set-specificity :root {
  #id::before {
    content: "earlier, more specific selector should not match";
  }

  .class::before {
    content: "later, less specific selector should match";
  }
}

@set-specificity {
  .a, .b, .c {
    --test: "";
  }
}

/* Output */
:is(*,:not(._)):where(#id)::before {
  content: "earlier, more specific selector should not match";
}

:is(*,:not(._)):where(.class)::before {
  content: "later, less specific selector should match";
}

:where(.a), :where(.b), :where(.c) {
  --test: "";
}

Installing

npm i -D postcss-set-specificity

postcss.config.js

module.exports = {
  plugins: [
    require("postcss-set-specificity") // After nesting plugin, if used
  ]
}

Consult PostCSS documentation for alternative usage.

Browser Support

Requires support for these CSS Selectors Level 4 features:

This corresponds to a browserslist roughly like:

Chrome >= 88
Safari >= 14
Firefox >= 78

How It Works

Given that:

  • :is() and :not() have specificities equal to the maximum specificity within them
  • :is(*, :not(x)) matches like the universal selector (*), but with the specificity of x
  • :where() always has a specificity of zero
  • :is(*, :not(x)):where(y) matches like y, but with the specificity of x
  • the specificity of a selector can be matched in an optimal way:
    • element#id.class:pseudo-class[attribute]::pseudo-element element * => 1,3,3
    • _+_+_#_._._._ => 1,3,3
  • pseudo elements can't be used in :where(), :is(), or :not()

If provided with:

@set-specificity x {
  y::before {}
}

The plugin produces:

:is(*,:not(_)):where(y)::before {}

In cases where there is no selector provided, we can simply use :where() alone.

Caveats

  • Does not yet interoperate with CSS Nesting, but wouldn't be hard to with @nest.
  • Pseudo elements are treated as exceptions and add 0,0,1 to the specificity, as they cannot be set to zero, and simply subtracting will lead to inconsistency.
  • Pseudo elements must be written with the :: prefix instead of with their old : prefix

Unimplemented Optimizations

  • Calculate specificities
    • Partial shared specificity
      • :is(*,:not(a#b.c)):where(d#e) to
      • d#e:is(*,:not(.c))
    • Full shared specificity
      • :is(*,:not(a#b.c)):where(d#e.f) to
      • d#e.f
  • Parse selector lists for correctness
    • Valid selector list (invalid selector lists would not fail because :where() is supposed to be forgiving)
      • :where(.a), :where(.b), :where(.c) to
      • :where(.a,.b,.c)
  • Assumptions/loose behavior
    • Assume selector never to match
      • :is(*,:not(#🔝)):where(.a) to
      • :not(#🔝):where(.a)
    • Assume selector will always match (e.g. must use something like <html id="🔝" class="🔝">)
      • :is(*,:not(#_)):where(.a) to
      • #🔝 :where(.a)