• Stars
    star
    695
  • Rank 62,787 (Top 2 %)
  • Language
    JavaScript
  • License
    BSD 2-Clause "Sim...
  • Created over 10 years ago
  • Updated 10 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CSS selectors complexity and performance analyzer

analyze-css

NPM version Known Vulnerabilities Coverage Status CodeFactor

Download stats

CSS selectors complexity and performance analyzer. analyze-css is built as a set of rules bound to events fired by CSS parser. Each rule can generate metrics and add "offenders" with more detailed information (see Usage section for an example).

Install

analyze-css comes as a "binary" for command-line and as CommonJS module. Run the following to install them globally:

npm install --global analyze-css

or to install from GitHub's repository:

npm install --global @macbre/analyze-css

Usage

Command line tool

You can use analyze-css "binary" to analyze local CSS files or remote CSS assets:

$ analyze-css --file examples/elecena.css

$ analyze-css --url http://s3.macbre.net/analyze-css/propertyResets.css
$ analyze-css --url https://s3.macbre.net/analyze-css/propertyResets.css --ignore-ssl-errors

You can provide CSS via stdin as well (notice the dash: -):

$ echo ".foo {margin: 0 \!important}" | analyze-css -

This will emit JSON formatted results on stdout. Use --pretty (or -p shortcut) option to make the output readable for human beings.

Basic HTTP authentication can be provided through the options --auth-user and --auth-pass.

HTTP proxy (e.g. http://localhost:8080) can be provided via:

  • --proxy or -x option
  • HTTP_PROXY env variable

CommonJS module

npm i --save analyze-css
const analyze = require('analyze-css');

(async() => {
  const results = await analyze('.foo {margin: 0 !important}');
  console.log(results); // example? see below
})();
// options can be provided
const opts = {
  'noOffenders': true
};

(async() => {
  const results = await analyze('.foo {margin: 0 !important}', opts);
  console.log(results); // example? see below
})();

grunt task

Created by @DeuxHuitHuit

$ npm i grunt-contrib-analyze-css

It uses configurable threshold and compares the analyze-css result with it.

Results

{
  "generator": "analyze-css v0.10.2",
  "metrics": {
    "base64Length": 11332,
    "redundantBodySelectors": 0,
    "redundantChildNodesSelectors": 1,
    "colors": 106,
    "comments": 1,
    "commentsLength": 68,
    "complexSelectors": 37,
    "duplicatedSelectors": 7,
    "duplicatedProperties": 24,
    "emptyRules": 0,
    "expressions": 0,
    "oldIEFixes": 51,
    "imports": 0,
    "importants": 3,
    "mediaQueries": 0,
    "notMinified": 0,
    "multiClassesSelectors": 74,
    "parsingErrors": 0,
    "oldPropertyPrefixes": 79,
    "propertyResets": 0,
    "qualifiedSelectors": 28,
    "specificityIdAvg": 0.04,
    "specificityIdTotal": 25,
    "specificityClassAvg": 1.27,
    "specificityClassTotal": 904,
    "specificityTagAvg": 0.79,
    "specificityTagTotal": 562,
    "selectors": 712,
    "selectorLengthAvg": 1.5722460658082975,
    "selectorsByAttribute": 92,
    "selectorsByClass": 600,
    "selectorsById": 25,
    "selectorsByPseudo": 167,
    "selectorsByTag": 533,
    "length": 55173,
    "rules": 433,
    "declarations": 1288
  },
  "offenders": {
    "importants": [
      ".foo {margin: 0 !important}"
    ]
  }
}

Metrics

  • base64Length: total length of base64-encoded data in CSS source (will warn about base64-encoded data bigger than 4 kB)
  • redundantBodySelectors: number of redundant body selectors (e.g. body .foo, section body h2, but not body > h1)
  • redundantChildNodesSelectors: number of redundant child nodes selectors (e.g. ul li, table tr)
  • colors: number of unique colors used in CSS
  • comments: number of comments in CSS source
  • commentsLength: length of comments content in CSS source
  • complexSelectors: number of complex selectors (consisting of more than three expressions, e.g. header ul li .foo)
  • duplicatedSelectors: number of CSS selectors defined more than once in CSS source
  • duplicatedProperties: number of CSS property definitions duplicated within a selector
  • emptyRules: number of rules with no properties (e.g. .foo { })
  • expressions: number of rules with CSS expressions (e.g. expression( document.body.clientWidth > 600 ? "600px" : "auto" ))
  • oldIEFixes: number of fixes for old versions of Internet Explorer (e.g. * html .foo {} and .foo { *zoom: 1 }, read more)
  • imports number of @import rules
  • importants: number of properties with value forced by !important
  • mediaQueries: number of media queries (e.g. @media screen and (min-width: 1370px))
  • notMinified: set to 1 if the provided CSS is not minified
  • multiClassesSelectors: reports selectors with multiple classes (e.g. span.foo.bar)
  • parsingErrors: number of CSS parsing errors
  • oldPropertyPrefixes: number of properties with no longer needed vendor prefix, powered by data provided by autoprefixer (e.g. --moz-border-radius)
  • propertyResets: number of accidental property resets
  • qualifiedSelectors: number of qualified selectors (e.g. header#nav, .foo#bar, h1.title)
  • specificityIdAvg: average specificity for ID
  • specificityIdTotal: total specificity for ID
  • specificityClassAvg: average specificity for class, pseudo-class or attribute
  • specificityClassTotal: total specificity for class, pseudo-class or attribute
  • specificityTagAvg: average specificity for element
  • specificityTagTotal: total specificity for element
  • selectors: number of selectors (e.g. .foo, .bar { color: red } is counted as two selectors - .foo and .bar)
  • selectorLengthAvg: average length of selector (e.g. for .foo .bar, #test div > span { color: red } will be set as 2.5)
  • selectorsByAttribute: number of selectors by attribute (e.g. .foo[value=bar])
  • selectorsByClass: number of selectors by class
  • selectorsById: number of selectors by ID
  • selectorsByPseudo: number of pseudo-selectors (e,g. :hover)
  • selectorsByTag: number of selectors by tag name
  • length: length of CSS source (in bytes)
  • rules: number of rules (e.g. .foo, .bar { color: red } is counted as one rule)
  • declarations: number of declarations (e.g. .foo, .bar { color: red } is counted as one declaration - color: red)

Read more

Dev hints

Running tests and linting the code:

$ npm test && npm run-script lint

Turning on debug mode (i.e. verbose logging to stderr via debug module):

$ DEBUG=analyze-css* analyze-css ...

Stargazers over time

Stargazers over time

More Repositories

1

phantomas

Headless Chromium-based web performance metrics collector and monitoring tool
JavaScript
2,252
star
2

sql-metadata

Uses tokenized query returned by python-sqlparse and generates query metadata
Python
739
star
3

nodemw

MediaWiki API and WikiData client written in Node.js
JavaScript
235
star
4

index-digest

Analyses your database queries and schema and suggests indices and schema improvements
Python
66
star
5

docker-sphinxsearch

Docker image for Sphinx search engine
Dockerfile
52
star
6

push-to-ghcr

This action simplifies pushes of Docker images to ghcr.io repository and the Docker Hub
Dockerfile
23
star
7

wayback-machine

Internet Wayback Machine nodejs Client
JavaScript
23
star
8

data-flow-graph

Uses your app logs to visualize how the data moves between the code, database, HTTP services, message queue, external storages etc.
Python
22
star
9

mediawiki-dump

Python package for working with MediaWiki XML content dumps
Python
19
star
10

telemetry

Telemetry system based on 8-bit AVR microcontroller with full TCP/IP stack (DHCP, NTP, HTTP)
C
11
star
11

wiki-evolution

Visualize evolution of your MediaWiki based site
JavaScript
7
star
12

phantomas-python

Python module for easy integration with phantomas
Python
7
star
13

query-digest

A dynamic code analysis tool that processes SQL queries logs and data flow information
Python
7
star
14

farerskie-kadry-feed

Turn Instagram and Facebook feeds into RSS
HTML
6
star
15

mobify

Download a webpage as an e-book
Python
5
star
16

plc

Power Line Communication
C
4
star
17

travis-fold

Tiny module for emitting folding syntax for Travis CI output
JavaScript
3
star
18

3pc

3rd party web content database
Python
3
star
19

monolog-python

Python's logging formatter compatible with
Python
3
star
20

actions-index-digest

GitHub Actions for index-digest - database performance regression testing
3
star
21

phantomas-reporter-elasticsearch

elasticsearch reporter for phantomas
JavaScript
3
star
22

airrohr-prometheus-exporter

Make prometheus collect temperature, pressure and PM metrics from your airrohr station
Python
2
star
23

nordic-feed

RSS feeds agregator aka planet for Nordic blogs in Polish
Python
2
star
24

faroese-planet

RSS feeds agregator aka planet for Faroese blogs from around the world
Python
2
star
25

curl-http3

A custom curl build with BoringSSL and http3 support via quiche
Dockerfile
2
star
26

electronics

Various electronics related notes, projects and schematics
C
2
star
27

optimist-config-file

Extends optimist with support for JSON/YAML config file and Docker's inspired environment variables handling
JavaScript
1
star
28

elasticsearch-query

A fork of https://github.com/Wikia/python-commons/tree/master/wikia/common/kibana for public PyPI repository
Python
1
star
29

test-wordpress-bitnami

The Docker Compose setup for the Bitnami-powered WordPress instance with SSH access and wp-cli installed. Can be easily put behind Traefik.
Shell
1
star
30

rs2eth

Ethernet / RS232 / RS485 converter with Telnet, VCOM and MCHP Discovery protocols support
1
star
31

map-porn

Templates used to generate maps based on geo data for Faroe Islands and Ireland
JavaScript
1
star
32

faroese-resources

Faroese resources
HTML
1
star
33

docker-traefik

Dockerized traefik v3 with auto-discovery of other containers on the same Docker network
Shell
1
star
34

pelican-planet

A fork of
Python
1
star
35

github-tools

GitHub toolbox
JavaScript
1
star
36

pyrabot

Skrypty bota używanego na Poznańskiej Wiki
Lua
1
star
37

fussball

IR-based goal system for football tables
C
1
star
38

slides

macbre's slides for Wikia
JavaScript
1
star