css

package
v2.7.13 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 6 Imported by: 37

README

CSS API reference

This package is a CSS3 lexer and parser written in Go. Both follow the specification at CSS Syntax Module Level 3. The lexer takes an io.Reader and converts it into tokens until the EOF. The parser returns a parse tree of the full io.Reader input stream, but the low-level Next function can be used for stream parsing to returns grammar units until the EOF.

Installation

Run the following command

go get -u github.com/tdewolff/parse/v2/css

or add the following import and run project with go get

import "github.com/tdewolff/parse/v2/css"

Lexer

Usage

The following initializes a new Lexer with io.Reader r:

l := css.NewLexer(parse.NewInput(r))

To tokenize until EOF an error, use:

for {
	tt, text := l.Next()
	switch tt {
	case css.ErrorToken:
		// error or EOF set in l.Err()
		return
	// ...
	}
}

All tokens (see CSS Syntax Module Level 3):

ErrorToken			// non-official token, returned when errors occur
IdentToken
FunctionToken		// rgb( rgba( ...
AtKeywordToken		// @abc
HashToken			// #abc
StringToken
BadStringToken
URLToken			// url(
BadURLToken
DelimToken			// any unmatched character
NumberToken			// 5
PercentageToken		// 5%
DimensionToken		// 5em
UnicodeRangeToken
IncludeMatchToken	// ~=
DashMatchToken		// |=
PrefixMatchToken	// ^=
SuffixMatchToken	// $=
SubstringMatchToken // *=
ColumnToken			// ||
WhitespaceToken
CDOToken 			// <!--
CDCToken 			// -->
ColonToken
SemicolonToken
CommaToken
BracketToken 		// ( ) [ ] { }, all bracket tokens use this, Data() can distinguish between the brackets
CommentToken		// non-official token
Examples
package main

import (
	"os"

	"github.com/tdewolff/parse/v2/css"
)

// Tokenize CSS3 from stdin.
func main() {
	l := css.NewLexer(parse.NewInput(os.Stdin))
	for {
		tt, text := l.Next()
		switch tt {
		case css.ErrorToken:
			if l.Err() != io.EOF {
				fmt.Println("Error on line", l.Line(), ":", l.Err())
			}
			return
		case css.IdentToken:
			fmt.Println("Identifier", string(text))
		case css.NumberToken:
			fmt.Println("Number", string(text))
		// ...
		}
	}
}

Parser

Usage

The following creates a new Parser.

// true because this is the content of an inline style attribute
p := css.NewParser(parse.NewInput(bytes.NewBufferString("color: red;")), true)

To iterate over the stylesheet, use:

for {
    gt, _, data := p.Next()
    if gt == css.ErrorGrammar {
        break
    }
    // ...
}

All grammar units returned by Next:

ErrorGrammar
AtRuleGrammar
EndAtRuleGrammar
RulesetGrammar
EndRulesetGrammar
DeclarationGrammar
TokenGrammar
Examples
package main

import (
	"bytes"
	"fmt"

	"github.com/tdewolff/parse/v2/css"
)

func main() {
	// true because this is the content of an inline style attribute
	p := css.NewParser(parse.NewInput(bytes.NewBufferString("color: red;")), true)
	out := ""
	for {
		gt, _, data := p.Next()
		if gt == css.ErrorGrammar {
			break
		} else if gt == css.AtRuleGrammar || gt == css.BeginAtRuleGrammar || gt == css.BeginRulesetGrammar || gt == css.DeclarationGrammar {
			out += string(data)
			if gt == css.DeclarationGrammar {
				out += ":"
			}
			for _, val := range p.Values() {
				out += string(val.Data)
			}
			if gt == css.BeginAtRuleGrammar || gt == css.BeginRulesetGrammar {
				out += "{"
			} else if gt == css.AtRuleGrammar || gt == css.DeclarationGrammar {
				out += ";"
			}
		} else {
			out += string(data)
		}
	}
	fmt.Println(out)
}

License

Released under the MIT license.

Documentation

Overview

Package css is a CSS3 lexer and parser following the specifications at http://www.w3.org/TR/css-syntax-3/.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HSL2RGB

func HSL2RGB(h, s, l float64) (float64, float64, float64)

HSL2RGB converts HSL to RGB with all of range [0,1] from http://www.w3.org/TR/css3-color/#hsl-color

func IsIdent

func IsIdent(b []byte) bool

IsIdent returns true if the bytes are a valid identifier.

func IsURLUnquoted

func IsURLUnquoted(b []byte) bool

IsURLUnquoted returns true if the bytes are a valid unquoted URL.

Types

type GrammarType

type GrammarType uint32

GrammarType determines the type of grammar.

const (
	ErrorGrammar GrammarType = iota // extra token when errors occur
	CommentGrammar
	AtRuleGrammar
	BeginAtRuleGrammar
	EndAtRuleGrammar
	QualifiedRuleGrammar
	BeginRulesetGrammar
	EndRulesetGrammar
	DeclarationGrammar
	TokenGrammar
	CustomPropertyGrammar
)

GrammarType values.

func (GrammarType) String

func (tt GrammarType) String() string

String returns the string representation of a GrammarType.

type Hash

type Hash uint32

Hash defines perfect hashes for a predefined list of strings

const (
	Document  Hash = 0x8    // document
	Font_Face Hash = 0x809  // font-face
	Keyframes Hash = 0x1109 // keyframes
	Media     Hash = 0x2105 // media
	Page      Hash = 0x2604 // page
	Supports  Hash = 0x1908 // supports
)

Unique hash definitions to be used instead of strings

func ToHash

func ToHash(s []byte) Hash

ToHash returns the hash whose name is s. It returns zero if there is no such hash. It is case sensitive.

func (Hash) String

func (i Hash) String() string

String returns the hash' name.

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer is the state for the lexer.

func NewLexer

func NewLexer(r *parse.Input) *Lexer

NewLexer returns a new Lexer for a given io.Reader.

Example
l := NewLexer(parse.NewInputString("color: red;"))
out := ""
for {
	tt, data := l.Next()
	if tt == ErrorToken {
		break
	} else if tt == WhitespaceToken || tt == CommentToken {
		continue
	}
	out += string(data)
}
fmt.Println(out)
Output:

color:red;

func (*Lexer) Err

func (l *Lexer) Err() error

Err returns the error encountered during lexing, this is often io.EOF but also other errors can be returned.

func (*Lexer) Next

func (l *Lexer) Next() (TokenType, []byte)

Next returns the next Token. It returns ErrorToken when an error was encountered. Using Err() one can retrieve the error message.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser is the state for the parser.

func NewParser

func NewParser(r *parse.Input, isInline bool) *Parser

NewParser returns a new CSS parser from an io.Reader. isInline specifies whether this is an inline style attribute.

Example
p := NewParser(parse.NewInputString("color: red;"), true) // false because this is the content of an inline style attribute
out := ""
for {
	gt, _, data := p.Next()
	if gt == ErrorGrammar {
		break
	} else if gt == AtRuleGrammar || gt == BeginAtRuleGrammar || gt == BeginRulesetGrammar || gt == DeclarationGrammar {
		out += string(data)
		if gt == DeclarationGrammar {
			out += ":"
		}
		for _, val := range p.Values() {
			out += string(val.Data)
		}
		if gt == BeginAtRuleGrammar || gt == BeginRulesetGrammar {
			out += "{"
		} else if gt == AtRuleGrammar || gt == DeclarationGrammar {
			out += ";"
		}
	} else {
		out += string(data)
	}
}
fmt.Println(out)
Output:

color:red;

func (*Parser) Err

func (p *Parser) Err() error

Err returns the error encountered during parsing, this is often io.EOF but also other errors can be returned.

func (*Parser) HasParseError added in v2.4.3

func (p *Parser) HasParseError() bool

HasParseError returns true if there is a parse error (and not a read error).

func (*Parser) Next

func (p *Parser) Next() (GrammarType, TokenType, []byte)

Next returns the next Grammar. It returns ErrorGrammar when an error was encountered. Using Err() one can retrieve the error message.

func (*Parser) Offset added in v2.4.0

func (p *Parser) Offset() int

Offset return offset for current Grammar

func (*Parser) Values

func (p *Parser) Values() []Token

Values returns a slice of Tokens for the last Grammar. Only AtRuleGrammar, BeginAtRuleGrammar, BeginRulesetGrammar and Declaration will return the at-rule components, ruleset selector and declaration values respectively.

type State

type State func(*Parser) GrammarType

State is the state function the parser currently is in.

type Token

type Token struct {
	TokenType
	Data []byte
}

Token is a single TokenType and its associated data.

func (Token) String

func (t Token) String() string

type TokenType

type TokenType uint32

TokenType determines the type of token, eg. a number or a semicolon.

const (
	ErrorToken TokenType = iota // extra token when errors occur
	IdentToken
	FunctionToken  // rgb( rgba( ...
	AtKeywordToken // @abc
	HashToken      // #abc
	StringToken
	BadStringToken
	URLToken
	BadURLToken
	DelimToken            // any unmatched character
	NumberToken           // 5
	PercentageToken       // 5%
	DimensionToken        // 5em
	UnicodeRangeToken     // U+554A
	IncludeMatchToken     // ~=
	DashMatchToken        // |=
	PrefixMatchToken      // ^=
	SuffixMatchToken      // $=
	SubstringMatchToken   // *=
	ColumnToken           // ||
	WhitespaceToken       // space \t \r \n \f
	CDOToken              // <!--
	CDCToken              // -->
	ColonToken            // :
	SemicolonToken        // ;
	CommaToken            // ,
	LeftBracketToken      // [
	RightBracketToken     // ]
	LeftParenthesisToken  // (
	RightParenthesisToken // )
	LeftBraceToken        // {
	RightBraceToken       // }
	CommentToken          // extra token for comments
	EmptyToken
	CustomPropertyNameToken
	CustomPropertyValueToken
)

TokenType values.

func (TokenType) String

func (tt TokenType) String() string

String returns the string representation of a TokenType.

Jump to

Keyboard shortcuts

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