1.0.0 • Published 3 years ago

specificity v1.0.0

Weekly downloads
2,321,938
License
MIT
Repository
github
Last release
3 years ago

Specificity Calculator

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

Note that version 1 is a complete re-write with support for CSS Selectors Level 4, and has a different API than earlier versions.

calculate(selector)

Parameters

  • selector: string - should be a valid CSS selector

Returns

A Specificity object with a type of Record<"A" | "B" | "C", number>.

Examples

calculate("#id");
{
  A: 1,
  B: 0,
  C: 0
}

calculate(".classname");
{
  A: 0,
  B: 1,
  C: 0
}

calculate("element");
{
  A: 0,
  B: 0,
  C: 1
}

calculate('ul#nav li.active a');
{
  A: 1,
  B: 1,
  C: 3
}

compare(a, b)

Parameters

  • a: Specificity object with a type of Record<"A" | "B" | "C", number>
  • b: Specificity object with a type of Record<"A" | "B" | "C", number>

Returns

Returns a positive number if a has a higher specificity than b Returns a negative number if a has a lower specificity than b Returns 0 if a has the same specificity than b

Examples

[
  "element",
  ".classname",
  "#id",
]
  .map(calculate)
  .sort(compare);

[ { A: 0, B: 0, C: 1 }, { A: 0, B: 1, C: 0 }, { A: 1, B: 0, C: 0 } ]

compareDesc(a, b)

Same as compare but returns the opposite value, for use in sorting specificity objects with the highest specificity first.

accessibility-layer@everything-registry/sub-chunk-2804@petergok/tempo-devtools@realrisman/svg2pdftempo-devtoolssvg-icon-toolboxsvg2pdf.jsspecificity-graphvisual-htmlimmutable-cssinline-cssinline-react-stylehtml-tuilongest-selectors@whinc/web-console@webbitjs/webbit@thundersolutions/access@yankovsky/subfontanalyze-cssassetgraphapim-developer-portalget-specified-style.jsfusion-theme-configec0lint-cssec0lint-styleec0lint-style-config-postcsseleventy-img-helperfont-tracercomponent-styleschangedsvg2pdf.jsckeditor5-build-classic-ticketckeditor5-build-classic-v-editdelenitiveritatiscss-specificity-mapcss-sortcss-statisticscss-querycssstatsblessed-cssbase-editorbrowser-xdfeuk-frontenddfeuk-frontend-manualsort-specificitysubfonttyprodtheme-config-kityu-svg2pdf.jszubtesttodayunfontuse-theme-editormprdevopticssrc-editor-listreact-mirrorscss-codemodsposthtml-inline-cssposthtml-css-inlinepostcss-sort-style-rulespostcss-basscsspostcss-remove-mutationsrepaintreplace-importantpsyclone-studioresolve-cascadereact-round-divshadow-domgrumpkin@gui-one/react-settings-form@hanwenbo/web-editor@nodaomg/cssstats@notesnook/clipper@leetcode/stylelint@leoko/svg2pdf.js@jackdbd/eleventy-plugin-text-to-speech@opticss/simple-template@oriduk/ckeditor5-rebuild-musicanote@oriduk/ckeditor5-rebuild-oriduk@petergok/tempo-devtools-test@paperbits/amp@paperbits/emails128981semzub@alifd/fusion-theme-config@ckeditor/ckeditor5-export-inline-styles@michaelallenwarner/tailwind-grid-inner@dimanech/cssstat-core@sbaldwin24/stylelint-config@silexlabs/grapesjs-advanced-selector@silver0542/directus-extension-cma-components-editor@emotion/jest@mybricks/editors-pc-common@fabwcie/ckeditor5-preview@qwik-ui/utils@rai/react-components
1.0.0

3 years ago

1.0.0-beta

3 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

9 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

12 years ago

0.1.3

13 years ago

0.1.2

13 years ago

0.1.1

13 years ago

0.1.0

13 years ago