Socket
Socket
Sign inDemoInstall

github.com/benbjohnson/css

Package Overview
Dependencies
0
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/benbjohnson/css

Package css implements a CSS3 compliant scanner and parser. This is meant to be a low-level library for extracting a CSS3 abstract syntax tree from raw CSS text. This package can be used for building tools to validate, optimize and format CSS text. CSS parsing occurs in two steps. First the scanner breaks up a stream of code points (runes) into tokens. These tokens represent the most basic units of the CSS syntax tree such as identifiers, whitespace, and strings. The second step is to feed these tokens into the parser which creates the abstract syntax tree (AST) based on the context of the tokens. Unlike many language parsers, the abstract syntax tree for CSS saves many of the original tokens in the stream so they can be reparsed at different levels. For example, parsing a @media query will save off the raw tokens found in the {-block so they can be reparsed as a full style sheet. This package doesn't understand the specifics of how to parse different types of at-rules (such as @media queries) so it defers that to the user to handle parsing. The CSS3 syntax defines a syntax tree of several types. At the top-level there is a StyleSheet. The style sheet is simply a collection of Rules. A Rule can be either an AtRule or a QualifiedRule. An AtRule is defined as a rule starting with an "@" symbol and an identifier, then it's followed by zero or more component values and finally ends with either a {-block or a semicolon. The block is parsed simply as a collection of tokens and it is up to the user to define the exact grammar. A QualifiedRule is defined as a rule starting with one or more component values and ending with a {-block. Inside the {-blocks are a list of declarations. Despite the name, a list of declarations can be either an AtRule or a Declaration. A Declaration is an identifier followed by a colon followed by one or more component values. The declaration can also have it's Important flag set if the last two non-whitespace tokens are a case-insensitive "!important". ComponentValues are the basic unit inside rules and declarations. A ComponentValue can be either a SimpleBlock, a Function, or a Token. A simple block starts with either a {, [, or (, has zero or more component values, and then ends with the mirror of the starting token (}, ], or )). A Function is an identifier immediately followed by a left parenthesis, then zero or more component values, and then ending with a right parenthesis.


Version published

Readme

Source

css Build Status Coverage Status GoDoc Project status

This package provides a CSS parser and scanner in pure Go. It is an implementation as specified in the W3C's CSS Syntax Module Level 3.

For documentation on how to use this package, please see the godoc.

Project Status

The scanner and parser are fully compliant with the CSS3 specification. The printer will print nodes generated from the scanner and parser, however, it is not fully compliant with the CSS3 serialization spec. Additionally, the printer does not provide an option to collapse whitespace although that will be added in the future.

This project has 100% test coverage, however, it is still a new project. Please report any bugs you experience or let me know where the documentation can be clearer.

Caveats

The CSS scanner in this package only supports UTF-8 encoding. The @charset directive will be ignored. If you need to scan a different encoding then please convert it to UTF-8 first using a tool such as iconv.

FAQs

Last updated on 14 Dec 2014

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