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 🙏

© 2026 – Pkg Stats / Ryan Hefner

cssobjectjs

v1.6.1

Published

A Parser CSS in Javascript Vanilla

Readme

GitHub code size in bytes GitHub GitHub release (latest by date) npm

CSSObject.js

A Parser CSS in Javascript Vanilla

Summary

Overview

This project was started from the idea of reading CSS files, to implement an alternative functionality, which I call Pseudo-Event for dynamic styling of elements from CSS. Initially, it was just a gists, but as the code developed, the project expanded.

Usage

Simple Usage

The fast way is using UNPKG link:

<script src="https://unpkg.com/cssobjectjs@latest/cssobject.js"></script>

sample code:

// normal
const cssobj = new CSSObject()

// get local stylesheets
cssobj.local(style => {
    // parsed local style!
    console.log(style)
})

// get external stylesheets
cssobj.external(style => {
    // parsed external style!
    console.log(style)
})

// static stylesheet
cssobj.static(".blob { background-color: #d7d7d7; }", style => {
  // parsed static style
  console.log(style)
})

or use alias CSSObj to CSSObject instance:

// using alias
CSSObj.options({load_min: false})
  .local(style => console.log(style))
  .external(style => console.log(style))
  .static(".blob { background-color: #d7d7d7; }", style => console.log(style))

Full Usage Mode

In your html, use type="module" in <script> tag:

<script src="./main.js" type="module"></script>

In main.js you can use CSSObject to get local stylesheets, styles inside <style> tag in your HTML, or external stylesheets (link), see:

import CSSObject from "CSSObject/CSSObject.js"


// instace CSSObject
let cssobj = new CSSObject()

// get local stylesheets
cssobj.local(style => {
  // parsed local style!
  console.log(style)
})

// get external stylesheets
cssobj.external(style => {
  // parsed external style!
  console.log(style)
})

the external method use a callback in promise, so it has a short call delay...

You can use the method .options(), to filter external stylesheets

cssobj.options({
  load_min: false, // '.min.css' (default is `true`)
  ignore_files: [], // ignored if `only_files` not empty
  only_files: []
}).external(style => {
  console.log(style)
})

the options object can also be passed in CSSObject constructor, and haven't effect for local stylesheets

Supports

See on table, what statments and others things this parser supports:

CSS At-Rules

| Statment | Example | Supports | |---------------|---------------------------------------------------------|----------| | Charset | @charset <type> | ✅ | | Namespace | @namespace <prefix?> <URL> | ❌ | | Import | @import <URL> <where?> | ✅ | | Font Face | @font-face { <rules> } | ✅ | | Keyframes | @keyframes <name> { <blocks> } | ✅ | | Media Query | @media <query> { <blocks> } | ✅ | | Support | @supports <where> { <blocks> } | ❌ | | Font Feature | @font-feature-values <family-name> { <blocks> } | ❌ | | Counter Style | @counter-style <name> { <rules> } | ❌ | | Page | @page <where> { <rules> } | ❌ |

see about this statments and others at-rules here

Others

| Data URI | data:[<media-type>];[base64],<data> | ✅ | | --- | --- | --- | | Comment | /*<text>*/ | ✅ |

Structure

Now, understand how the CSSObject, object class, and file and folder segments are structured

Project Structure

First, I need to show the organization of the project

CSSObject:
  enums: # auxiliary files as interfaces
    ICombiner.js
    ICSS.js
    ICSSStatments.js
    IMedia.js
    ISelector.js
    IPseudo.js
    IUnit.js
  parser: # files responsible for data processing
    BlocksParser.js
    CommentBlock.js
    ParserBlock.js
    StatmentsParser.js
    Stylesheet.js
  queries: # classes thats work of queries part
    Selector.js
    Pseudo.js
  rules: # classes thats work of rules statments
    BlockRule.js
    CharsetRule.js
    FontfaceRule.js
    FunctionRule.js
    ImportRule.js
    KeyframeRule.js
    MediaRule.js
    QuerieRule.js
    Rule.js
    VariableRule.js
  CSSObject.js
  CSSParser.js
example.css # example css code
index.html
main.js # usage of `CSSObject.js`

CSSObject Structure

See below, the objects and their properties that are returned

CSSObject

CSSObject:
  options(options: object)
  static(text: string, callback: function)
  local(callback: function, all: boolean?)
  external(callback: function) # callback in promise

callbacks functions return Stylesheet object

Stylesheet

Stylesheet:
  cssText: string # contains break-lines and spaces
  css: string # clean string (without break-lines and spaces)
  comments: CommentBlock[]
  blocks: BlockRule[]
  variables: VariableRule[]
  filename: string | null
  statments: {
    charsets: CharsetRule[],
    imports: ImportRule[],
    fontfaces: FontfaceRule[],
    keyframes: KeyframeRule[],
    medias: MediaRule[],
  }
Block Rule
BlockRule:
  query: string
  selectors: Selector[]
  rules: Rule[]
Comment Block
CommentBlock:
  text: string
  comment: string # like `text` property (without break-lines and spaces)
  line: number | number[]

Rule

Rule:
  property: string
  value: string | string[] | FunctionRule
  values: object[] # ex.: "10px" => [{value: 10, unit: "PIXELS"}]
  prop: string # alias to `property`
  isImportant: boolean
Variable Rule
VariableRule:
  name: string
  value: string | string[] | FunctionRule
  scope: string
  statment: string
Function Rule
FunctionRule:
  name: string
  value: string | string[] # may contains FunctionRule object
Charset Rule
CharsetRule:
  type: string
Import Rule
ImportRule:
  url: string
  where: string[] # may contains Rule object
Font Face Rule
FontFaceRule:
  name: string # `font-family` value
  rules: Rule[]
Keyframe Rule
KeyframeRule:
  name: string
  blocks: BlockRule[]
Media Query Rule
MediaRule:
  query: string
  queries: QuerieRule[]
  blocks: BlockRule[]
Querie Rule
QuerieRule:
  rules: Rule[]
  types: string[]
  only: boolean
  not: boolean

Selector

Selector:
  name: string
  type: 'UniversalSelector' | 'IDSelector' | 'ClassSelector' | 'AttrSelector' | 'PseudoSelector' | 'HTMLSelector'
  hasCombiner: boolean
  pseudo?: Pseudo # has if `hasCombiner` is `false`
  selectors?: Selector[] # has if `hasCombiner` is `true`
Pseudo
Selector:
  name: string
  type: 'PseudoClass' | 'PseudoElement' | 'PseudoEvent'
  value?: string # has if pseudo contains parameters

the type PseudoEvent is a custom support of CSSObject not has on CSS language

Be a Contributor

  • This project was plan, writted and develop by Kugi Haito 🍪
  • Did you like my project? give star or fork to help, thanks 😁
  • would you like to be a contributor? make me a Pull Request!