css

package module
v0.0.0-...-6538f86 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2014 License: MIT Imports: 6 Imported by: 1

README

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.

Documentation

Overview

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.

Basics

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.

Abstract Syntax Tree

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtRule

type AtRule struct {
	Name    string
	Prelude ComponentValues
	Block   *SimpleBlock
	Pos     Pos
}

AtRule represents a rule starting with an "@" symbol.

type ComponentValue

type ComponentValue interface {
	Node
	// contains filtered or unexported methods
}

ComponentValue represents a component value.

type ComponentValueScanner

type ComponentValueScanner interface {
	Current() ComponentValue
	Scan() ComponentValue
	Unscan()
}

ComponentValueScanner represents a type that can retrieve the next component value.

func NewComponentValueScanner

func NewComponentValueScanner(values ComponentValues) ComponentValueScanner

NewComponentValueScanner returns a scanner for a fixed list of component values. This can be used with nodes which have blocks such as at-rules. For example, a @media query can have a full ruleset inside its block. This block can be further parsed using the consume functions on the Parser.

type ComponentValues

type ComponentValues []ComponentValue

ComponentValues represents a list of component values.

type Declaration

type Declaration struct {
	Name      string
	Values    ComponentValues
	Important bool
	Pos       Pos
}

Declaration represents a name/value pair.

type Declarations

type Declarations []Node

Declarations represents a list of declarations or at-rules.

type Error

type Error struct {
	Message string
	Pos     Pos
}

Error represents a syntax error.

func (*Error) Error

func (e *Error) Error() string

Error returns the formatted string error message.

type ErrorList

type ErrorList []error

ErrorList represents a list of syntax errors.

func (ErrorList) Error

func (a ErrorList) Error() string

Error returns the formatted string error message.

type Function

type Function struct {
	Name   string
	Values ComponentValues
	Pos    Pos
}

Function represents a function call with a list of arguments.

type Node

type Node interface {
	// contains filtered or unexported methods
}

Node represents a node in the CSS3 abstract syntax tree.

type Parser

type Parser struct {
	Errors ErrorList
}

Parser represents a CSS3 parser.

func (*Parser) ConsumeAtRule

func (p *Parser) ConsumeAtRule(s ComponentValueScanner) *AtRule

ConsumeAtRule consumes a single at-rule.

func (*Parser) ConsumeComponentValue

func (p *Parser) ConsumeComponentValue(s ComponentValueScanner) ComponentValue

ConsumeComponentValue consumes a single component value. (§5.4.6)

func (*Parser) ConsumeDeclaration

func (p *Parser) ConsumeDeclaration(s ComponentValueScanner) *Declaration

ConsumeDeclaration consumes a single declaration.

func (*Parser) ConsumeDeclarations

func (p *Parser) ConsumeDeclarations(s ComponentValueScanner) Declarations

ConsumeDeclarations consumes a list of declarations.

func (*Parser) ConsumeFunction

func (p *Parser) ConsumeFunction(s ComponentValueScanner) *Function

ConsumeFunction consumes a function.

func (*Parser) ConsumeQualifiedRule

func (p *Parser) ConsumeQualifiedRule(s ComponentValueScanner) *QualifiedRule

ConsumeQualifiedRule consumes a single qualified rule.

func (*Parser) ConsumeRules

func (p *Parser) ConsumeRules(s ComponentValueScanner, topLevel bool) Rules

ConsumeRules consumes a list of rules from a token stream.

func (*Parser) ConsumeSimpleBlock

func (p *Parser) ConsumeSimpleBlock(s ComponentValueScanner) *SimpleBlock

ConsumeSimpleBlock consumes a simple block. (§5.4.7)

func (*Parser) ParseComponentValue

func (p *Parser) ParseComponentValue(s *Scanner) ComponentValue

ParseComponentValue parses a component value.

func (*Parser) ParseComponentValues

func (p *Parser) ParseComponentValues(s *Scanner) ComponentValues

ParseComponentValues parses a list of component values.

func (*Parser) ParseDeclaration

func (p *Parser) ParseDeclaration(s *Scanner) *Declaration

ParseDeclaration parses a name/value declaration.

func (*Parser) ParseDeclarations

func (p *Parser) ParseDeclarations(s *Scanner) Declarations

ParseDeclarations parses a list of declarations and at-rules.

func (*Parser) ParseRule

func (p *Parser) ParseRule(s *Scanner) Rule

ParseRule parses a qualified rule or at-rule.

func (*Parser) ParseRules

func (p *Parser) ParseRules(s *Scanner) Rules

ParseRule parses a list of rules.

func (*Parser) ParseStyleSheet

func (p *Parser) ParseStyleSheet(s *Scanner) *StyleSheet

ParseStyleSheet parses an input stream into a stylesheet.

type Pos

type Pos struct {
	Char int
	Line int
}

Pos specifies the line and character position of a token. The Char and Line are both zero-based indexes.

func Position

func Position(n Node) Pos

Position returns the position for a given Node.

type Printer

type Printer struct{}

Printer represents a configurable CSS printer.

func (*Printer) Print

func (p *Printer) Print(w io.Writer, n Node) (err error)

type QualifiedRule

type QualifiedRule struct {
	Prelude ComponentValues
	Block   *SimpleBlock
	Pos     Pos
}

QualifiedRule represents an unnamed rule that includes a prelude and block.

type Rule

type Rule interface {
	Node
	// contains filtered or unexported methods
}

Rule represents a qualified rule or at-rule.

type Rules

type Rules []Rule

Rules represents a list of rules.

type Scanner

type Scanner struct {
	// Errors contains a list of all errors that occur during scanning.
	Errors []*Error
	// contains filtered or unexported fields
}

Scanner implements a CSS3 standard compliant tokenizer.

This implementation only allows UTF-8 encoding. @charset directives will be ignored.

func NewScanner

func NewScanner(r io.Reader) *Scanner

New returns a new instance of Scanner.

func (*Scanner) Scan

func (s *Scanner) Scan() *Token

Scan returns the next token from the reader.

type SimpleBlock

type SimpleBlock struct {
	Token  *Token
	Values ComponentValues
	Pos    Pos
}

SimpleBlock represents a {-block, [-block, or (-block.

type StyleSheet

type StyleSheet struct {
	Rules Rules
}

StyleSheet represents a top-level CSS3 stylesheet.

type Tok

type Tok int

Tok represents a lexical token type.

const (
	IdentToken Tok = iota + 1
	FunctionToken
	AtKeywordToken
	HashToken
	StringToken
	BadStringToken
	URLToken
	BadURLToken
	DelimToken
	NumberToken
	PercentageToken
	DimensionToken
	UnicodeRangeToken
	IncludeMatchToken
	DashMatchToken
	PrefixMatchToken
	SuffixMatchToken
	SubstringMatchToken
	ColumnToken
	WhitespaceToken
	CDOToken
	CDCToken
	ColonToken
	SemicolonToken
	CommaToken
	LBrackToken
	RBrackToken
	LParenToken
	RParenToken
	LBraceToken
	RBraceToken
	EOFToken
)

type Token

type Token struct {
	// The type of token.
	Tok Tok

	// A flag set for ident-like tokens to either "id" or "unrestricted".
	// Also set for numeric tokens to either "integer" or "number"
	Type string

	// The literal value of the token as parsed.
	Value string

	// The rune used to close the token. Used for string tokens.
	Ending rune

	// The numeric value and unit used for numeric tokens.
	Number float64
	Unit   string

	// Beginning and ending range for a unicode-range token.
	Start int
	End   int

	// Position of the token in the source document.
	Pos Pos
}

Token represents a lexical token.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL