Socket
Socket
Sign inDemoInstall

css-tokens

Package Overview
Dependencies
0
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    css-tokens

A regex that tokenizes CSS.


Version published
Weekly downloads
34
decreased by-58.02%
Maintainers
1
Install size
9.26 kB
Created
Weekly downloads
 

Changelog

Source

Version 2.0.0 (2017-01-11) ###

This release contains one breaking change, that should [improve performance in V8][v8-perf]:

So how can you, as a JavaScript developer, ensure that your RegExps are fast? If you are not interested in hooking into RegExp internals, make sure that neither the RegExp instance, nor its prototype is modified in order to get the best performance:

var re = /./g;
re.exec('');  // Fast path.
re.new_property = 'slow';

This module used to export a single regex, with .matchToToken bolted on, just like in the above example. This release changes the exports of the module to avoid this issue.

Before:

import cssTokens from "css-tokens"
// or:
var cssTokens = require("css-tokens")
var matchToToken = cssTokens.matchToToken

After:

import cssTokens, {matchToToken} from "css-tokens"
// or:
var cssTokens = require("css-tokens").default
var matchToToken = require("css-tokens").matchToToken

Readme

Source

Overview Build Status

A regex that tokenizes CSS.

var cssTokens = require("css-tokens").default

var cssString = ".foo{prop: foo;}\n..."

cssString.match(cssTokens)
// [".foo", "{", "prop", ":", " ", "foo", ";", "}", "\n", ...]

Installation

  • npm install css-tokens
import cssTokens from "css-tokens"
// or:
var cssTokens = require("css-tokens").default

Usage

cssTokens

A regex with the g flag that matches CSS tokens.

The regex always matches, even invalid CSS and the empty string.

The next match is always directly after the previous.

var token = matchToToken(match)

import {matchToToken} from "css-tokens"
// or:
var matchToToken = require("css-tokens").matchToToken

Takes a match returned by cssTokens.exec(string), and returns a {type: String, value: String} object. The following types are available:

  • string
  • comment
  • number
  • unquotedUrl
  • name
  • punctuator
  • whitespace
  • invalid

Comments and strings also have a closed property indicating if the token was closed or not (see below).

Strings come in two flavors. To distinguish them, check if the token starts with ' or ".

Names may start with @ (as in at-rule names), . (as in class selectors) and # (as in id selectors and hex colors).

Invalid code handling

Unterminated strings are still matched as strings. CSS strings cannot contain (unescaped) newlines, so unterminated strings simply end at the end of the line.

Unterminated multi-line comments are also still matched as comments. They simply go on to the end of the string.

Unterminated unquoted urls are also still matched as unquoted urls. They continue as long as there are valid characters.

Invalid ASCII characters have their own capturing group.

Limitations

Tokenizing CSS using regexes—in fact, one single regex—won’t be perfect. But that’s not the point either.

Quoted vs. unquoted urls

The following is hardly a “limitation”, but could be mentioned:

url(http://www.w3.org/2000/svg)
url('http://www.w3.org/2000/svg')

The first line is matched as one single token (unquotedUrl), while the second is matched as four (name + punctuator + string + punctuator). This could be fixed, but isn’t to simplify the regex.

License

MIT.

Keywords

FAQs

Last updated on 11 Jan 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc