New:Microsoft Teams Notifications Are Now Available in Socket.Learn more
Get Started

@csstools/css-tokenizer

Package Overview
Dependencies
Maintainers
3
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@csstools/css-tokenizer

Tokenize CSS

latest
Source
npmnpm
Version
4.0.1
Version published
Weekly downloads
55M
-2.21%
Maintainers
3
Weekly downloads
 
Created
Source

CSS Tokenizer for CSS

npm version Build Status Discord

Implemented from : https://drafts.csswg.org/css-syntax/

API

Read the API docs

Usage

Add CSS Tokenizer to your project:

npm install @csstools/css-tokenizer --save-dev
import { tokenize } from '@csstools/css-tokenizer';

const myCSS =  `@media only screen and (min-width: 768rem) {
	.foo {
		content: 'Some content!' !important;
	}
}
`;

const tokens = tokenize({
	css: myCSS,
});

console.log(tokens);

Or use the streaming interface:

import { tokenizer, TokenType } from '@csstools/css-tokenizer';

const myCSS = `@media only screen and (min-width: 768rem) {
	.foo {
		content: 'Some content!' !important;
	}
}
`;

const t = tokenizer({
	css: myCSS,
});

while (true) {
	const token = t.nextToken();
	if (token[0] === TokenType.EOF) {
		break;
	}

	console.log(token);
}

Options

{
	onParseError?: (error: ParseError) => void
}

onParseError

The tokenizer is forgiving and won't stop when a parse error is encountered.

To receive parsing error information you can set a callback.

import { tokenizer, TokenType } from '@csstools/css-tokenizer';

const t = tokenizer({
	css: '\\',
}, { onParseError: (err) => console.warn(err) });

while (true) {
	const token = t.nextToken();
	if (token[0] === TokenType.EOF) {
		break;
	}
}

Parser errors will try to inform you where in the tokenizer logic the error happened. This tells you what kind of error occurred.

Order of priorities

  • specification compliance
  • correctness
  • reliability
  • tokenizing and serializing must round trip losslessly
  • exposing useful aspects about the source code
  • runtime performance
  • package size

Keywords

css

FAQs

Package last updated on 18 Sep 2026

Related posts