Home > @csstools/css-tokenizer
Tokenize CSS following the CSS Syntax Level 3 specification.
The tokenizing and parsing tools provided by CSS Tools are designed to be low level and generic with strong ties to their respective specifications.
Any analysis or mutation of CSS source code should be done with the least powerful tool that can accomplish the task. For many applications it is sufficient to work with tokens. For others you might need to use @csstools/css-parser-algorithms or a more specific parser.
Tokenize a string of CSS into an array of tokens:
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);
Class |
Description |
---|---|
The CSS Tokenizer is forgiving and will never throw on invalid input. Any errors are reported through the | |
Enumeration |
Description |
---|---|
The type of hash token | |
The type of number token Either | |
All possible CSS token types |
Function |
Description |
---|---|
Deep clone a list of tokens. Useful for mutations without altering the original list. | |
Assert that a given value has the general structure of a CSS token: 1. is an array. 2. has at least four items. 3. has a known token type. 4. has a string representation. 5. has a start position. 6. has an end position. | |
Assert that a token is a numeric token | |
Assert that a token is a whitespace or comment token | |
Get the mirror variant of a given token | |
Get the mirror variant type of a given token type | |
Set the ident value and update the string representation. This handles escaping. | |
Set the unit and update the string representation. This handles escaping. | |
Concatenate the string representation of a list of tokens. This is not a proper serializer that will handle escaping and whitespace. It only produces valid CSS for a token list that is also valid. | |
Tokenize a CSS string into a list of tokens. | |
Create a tokenizer for a CSS string. |
Interface |
Description |
---|---|
The CSS Token interface | |
Variable |
Description |
---|---|
Type Alias |
Description |
---|---|
The union of all possible CSS tokens | |
The union of all possible CSS tokens that represent a numeric value |