package css

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

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

Index

Examples

Variables

var ErrBadDeclaration = errors.New("unexpected token in declaration, expected colon token")

ErrBadDeclaration is returned when a declaration is expected but the colon token is lacking.

var ErrBadQualifiedRule = errors.New("unexpected ending in qualified rule, expected left brace token")

ErrBadQualifiedRule is returned when a qualitied rule is expected, but an error or EOF happened earlier.

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
	AtRuleGrammar
	BeginAtRuleGrammar
	EndAtRuleGrammar
	BeginRulesetGrammar
	EndRulesetGrammar
	DeclarationGrammar
	TokenGrammar
)

GrammarType values.

func (GrammarType) String

func (tt GrammarType) String() string

String returns the string representation of a GrammarType.

type Hash

type Hash uint32

uses github.com/tdewolff/hasher

const (
	Accelerator                 Hash = 0x47f0b
	Aliceblue                   Hash = 0x52509
	Alpha                       Hash = 0x5af05
	Antiquewhite                Hash = 0x45c0c
	Aquamarine                  Hash = 0x7020a
	Azimuth                     Hash = 0x5b307
	Background                  Hash = 0xa
	Background_Attachment       Hash = 0x3a15
	Background_Color            Hash = 0x11c10
	Background_Image            Hash = 0x99210
	Background_Position         Hash = 0x13
	Background_Position_X       Hash = 0x80815
	Background_Position_Y       Hash = 0x15
	Background_Repeat           Hash = 0x1511
	Behavior                    Hash = 0x3108
	Black                       Hash = 0x6005
	Blanchedalmond              Hash = 0x650e
	Blueviolet                  Hash = 0x52a0a
	Bold                        Hash = 0x7a04
	Border                      Hash = 0x8506
	Border_Bottom               Hash = 0x850d
	Border_Bottom_Color         Hash = 0x8513
	Border_Bottom_Style         Hash = 0xbe13
	Border_Bottom_Width         Hash = 0xe113
	Border_Collapse             Hash = 0x1020f
	Border_Color                Hash = 0x1350c
	Border_Left                 Hash = 0x15c0b
	Border_Left_Color           Hash = 0x15c11
	Border_Left_Style           Hash = 0x17911
	Border_Left_Width           Hash = 0x18a11
	Border_Right                Hash = 0x19b0c
	Border_Right_Color          Hash = 0x19b12
	Border_Right_Style          Hash = 0x1ad12
	Border_Right_Width          Hash = 0x1bf12
	Border_Spacing              Hash = 0x1d10e
	Border_Style                Hash = 0x1f40c
	Border_Top                  Hash = 0x2000a
	Border_Top_Color            Hash = 0x20010
	Border_Top_Style            Hash = 0x21010
	Border_Top_Width            Hash = 0x22010
	Border_Width                Hash = 0x2300c
	Bottom                      Hash = 0x8c06
	Burlywood                   Hash = 0x23c09
	Cadetblue                   Hash = 0x25809
	Caption_Side                Hash = 0x2610c
	Charset                     Hash = 0x44207
	Chartreuse                  Hash = 0x2730a
	Chocolate                   Hash = 0x27d09
	Clear                       Hash = 0x2ab05
	Clip                        Hash = 0x2b004
	Color                       Hash = 0x9305
	Content                     Hash = 0x2e507
	Cornflowerblue              Hash = 0x2ff0e
	Cornsilk                    Hash = 0x30d08
	Counter_Increment           Hash = 0x31511
	Counter_Reset               Hash = 0x3540d
	Cue                         Hash = 0x36103
	Cue_After                   Hash = 0x36109
	Cue_Before                  Hash = 0x36a0a
	Cursive                     Hash = 0x37b07
	Cursor                      Hash = 0x38e06
	Darkblue                    Hash = 0x7208
	Darkcyan                    Hash = 0x7d08
	Darkgoldenrod               Hash = 0x2440d
	Darkgray                    Hash = 0x25008
	Darkgreen                   Hash = 0x79209
	Darkkhaki                   Hash = 0x88509
	Darkmagenta                 Hash = 0x4f40b
	Darkolivegreen              Hash = 0x7210e
	Darkorange                  Hash = 0x7860a
	Darkorchid                  Hash = 0x87c0a
	Darksalmon                  Hash = 0x8c00a
	Darkseagreen                Hash = 0x9240c
	Darkslateblue               Hash = 0x3940d
	Darkslategray               Hash = 0x3a10d
	Darkturquoise               Hash = 0x3ae0d
	Darkviolet                  Hash = 0x3bb0a
	Deeppink                    Hash = 0x26b08
	Deepskyblue                 Hash = 0x8930b
	Default                     Hash = 0x57b07
	Direction                   Hash = 0x9f109
	Display                     Hash = 0x3c507
	Document                    Hash = 0x3d308
	Dodgerblue                  Hash = 0x3db0a
	Elevation                   Hash = 0x4a009
	Empty_Cells                 Hash = 0x4c20b
	Fantasy                     Hash = 0x5ce07
	Filter                      Hash = 0x59806
	Firebrick                   Hash = 0x3e509
	Float                       Hash = 0x3ee05
	Floralwhite                 Hash = 0x3f30b
	Font                        Hash = 0xd804
	Font_Face                   Hash = 0xd809
	Font_Family                 Hash = 0x41d0b
	Font_Size                   Hash = 0x42809
	Font_Size_Adjust            Hash = 0x42810
	Font_Stretch                Hash = 0x4380c
	Font_Style                  Hash = 0x4490a
	Font_Variant                Hash = 0x4530c
	Font_Weight                 Hash = 0x46e0b
	Forestgreen                 Hash = 0x3700b
	Fuchsia                     Hash = 0x47907
	Gainsboro                   Hash = 0x14c09
	Ghostwhite                  Hash = 0x1de0a
	Goldenrod                   Hash = 0x24809
	Greenyellow                 Hash = 0x7960b
	Height                      Hash = 0x68506
	Honeydew                    Hash = 0x5b908
	Hsl                         Hash = 0xf303
	Hsla                        Hash = 0xf304
	Ime_Mode                    Hash = 0x88d08
	Import                      Hash = 0x4e306
	Important                   Hash = 0x4e309
	Include_Source              Hash = 0x7f20e
	Indianred                   Hash = 0x4ec09
	Inherit                     Hash = 0x51907
	Initial                     Hash = 0x52007
	Keyframes                   Hash = 0x40109
	Lavender                    Hash = 0xf508
	Lavenderblush               Hash = 0xf50d
	Lawngreen                   Hash = 0x4da09
	Layer_Background_Color      Hash = 0x11616
	Layer_Background_Image      Hash = 0x98c16
	Layout_Flow                 Hash = 0x5030b
	Layout_Grid                 Hash = 0x53f0b
	Layout_Grid_Char            Hash = 0x53f10
	Layout_Grid_Char_Spacing    Hash = 0x53f18
	Layout_Grid_Line            Hash = 0x55710
	Layout_Grid_Mode            Hash = 0x56d10
	Layout_Grid_Type            Hash = 0x58210
	Left                        Hash = 0x16304
	Lemonchiffon                Hash = 0xcf0c
	Letter_Spacing              Hash = 0x5310e
	Lightblue                   Hash = 0x59e09
	Lightcoral                  Hash = 0x5a70a
	Lightcyan                   Hash = 0x5d509
	Lightgoldenrodyellow        Hash = 0x5de14
	Lightgray                   Hash = 0x60509
	Lightgreen                  Hash = 0x60e0a
	Lightpink                   Hash = 0x61809
	Lightsalmon                 Hash = 0x6210b
	Lightseagreen               Hash = 0x62c0d
	Lightskyblue                Hash = 0x6390c
	Lightslateblue              Hash = 0x6450e
	Lightsteelblue              Hash = 0x6530e
	Lightyellow                 Hash = 0x6610b
	Limegreen                   Hash = 0x67709
	Line_Break                  Hash = 0x5630a
	Line_Height                 Hash = 0x6800b
	List_Style                  Hash = 0x68b0a
	List_Style_Image            Hash = 0x68b10
	List_Style_Position         Hash = 0x69b13
	List_Style_Type             Hash = 0x6ae0f
	Magenta                     Hash = 0x4f807
	Margin                      Hash = 0x2c006
	Margin_Bottom               Hash = 0x2c00d
	Margin_Left                 Hash = 0x2cc0b
	Margin_Right                Hash = 0x3320c
	Margin_Top                  Hash = 0x7cd0a
	Marker_Offset               Hash = 0x6bd0d
	Marks                       Hash = 0x6ca05
	Max_Height                  Hash = 0x6e90a
	Max_Width                   Hash = 0x6f309
	Media                       Hash = 0xa1405
	Mediumaquamarine            Hash = 0x6fc10
	Mediumblue                  Hash = 0x70c0a
	Mediumorchid                Hash = 0x7160c
	Mediumpurple                Hash = 0x72f0c
	Mediumseagreen              Hash = 0x73b0e
	Mediumslateblue             Hash = 0x7490f
	Mediumspringgreen           Hash = 0x75811
	Mediumturquoise             Hash = 0x7690f
	Mediumvioletred             Hash = 0x7780f
	Midnightblue                Hash = 0x7a60c
	Min_Height                  Hash = 0x7b20a
	Min_Width                   Hash = 0x7bc09
	Mintcream                   Hash = 0x7c509
	Mistyrose                   Hash = 0x7e309
	Moccasin                    Hash = 0x7ec08
	Monospace                   Hash = 0x8c709
	Namespace                   Hash = 0x49809
	Navajowhite                 Hash = 0x4a80b
	None                        Hash = 0x4bf04
	Normal                      Hash = 0x4d506
	Olivedrab                   Hash = 0x80009
	Orangered                   Hash = 0x78a09
	Orphans                     Hash = 0x48807
	Outline                     Hash = 0x81d07
	Outline_Color               Hash = 0x81d0d
	Outline_Style               Hash = 0x82a0d
	Outline_Width               Hash = 0x8370d
	Overflow                    Hash = 0x2db08
	Overflow_X                  Hash = 0x2db0a
	Overflow_Y                  Hash = 0x8440a
	Padding                     Hash = 0x2b307
	Padding_Bottom              Hash = 0x2b30e
	Padding_Left                Hash = 0x5f90c
	Padding_Right               Hash = 0x7d60d
	Padding_Top                 Hash = 0x8d90b
	Page                        Hash = 0x84e04
	Page_Break_After            Hash = 0x8e310
	Page_Break_Before           Hash = 0x84e11
	Page_Break_Inside           Hash = 0x85f11
	Palegoldenrod               Hash = 0x8700d
	Palegreen                   Hash = 0x89e09
	Paleturquoise               Hash = 0x8a70d
	Palevioletred               Hash = 0x8b40d
	Papayawhip                  Hash = 0x8d00a
	Pause                       Hash = 0x8f305
	Pause_After                 Hash = 0x8f30b
	Pause_Before                Hash = 0x8fe0c
	Peachpuff                   Hash = 0x59009
	Pitch                       Hash = 0x90a05
	Pitch_Range                 Hash = 0x90a0b
	Play_During                 Hash = 0x3c80b
	Position                    Hash = 0xb08
	Powderblue                  Hash = 0x9150a
	Progid                      Hash = 0x91f06
	Quotes                      Hash = 0x93006
	Rgb                         Hash = 0x3803
	Rgba                        Hash = 0x3804
	Richness                    Hash = 0x9708
	Right                       Hash = 0x1a205
	Rosybrown                   Hash = 0x15309
	Royalblue                   Hash = 0xb509
	Ruby_Align                  Hash = 0x12b0a
	Ruby_Overhang               Hash = 0x1400d
	Ruby_Position               Hash = 0x16c0d
	Saddlebrown                 Hash = 0x48e0b
	Sandybrown                  Hash = 0x4cc0a
	Sans_Serif                  Hash = 0x5c50a
	Scrollbar_3d_Light_Color    Hash = 0x9e18
	Scrollbar_Arrow_Color       Hash = 0x29615
	Scrollbar_Base_Color        Hash = 0x40914
	Scrollbar_Dark_Shadow_Color Hash = 0x6ce1b
	Scrollbar_Face_Color        Hash = 0x93514
	Scrollbar_Highlight_Color   Hash = 0x9ce19
	Scrollbar_Shadow_Color      Hash = 0x94916
	Scrollbar_Track_Color       Hash = 0x95f15
	Seagreen                    Hash = 0x63108
	Seashell                    Hash = 0x10f08
	Serif                       Hash = 0x5ca05
	Size                        Hash = 0x42d04
	Slateblue                   Hash = 0x39809
	Slategray                   Hash = 0x3a509
	Speak                       Hash = 0x97405
	Speak_Header                Hash = 0x9740c
	Speak_Numeral               Hash = 0x9800d
	Speak_Punctuation           Hash = 0x9a211
	Speech_Rate                 Hash = 0x9b30b
	Springgreen                 Hash = 0x75e0b
	Steelblue                   Hash = 0x65809
	Stress                      Hash = 0x29106
	Supports                    Hash = 0x9c708
	Table_Layout                Hash = 0x4fd0c
	Text_Align                  Hash = 0x2840a
	Text_Align_Last             Hash = 0x2840f
	Text_Autospace              Hash = 0x1e60e
	Text_Decoration             Hash = 0x4b10f
	Text_Indent                 Hash = 0x9bc0b
	Text_Justify                Hash = 0x250c
	Text_Kashida_Space          Hash = 0x4e12
	Text_Overflow               Hash = 0x2d60d
	Text_Shadow                 Hash = 0x2eb0b
	Text_Transform              Hash = 0x3250e
	Text_Underline_Position     Hash = 0x33d17
	Top                         Hash = 0x20703
	Turquoise                   Hash = 0x3b209
	Unicode_Bidi                Hash = 0x9e70c
	Vertical_Align              Hash = 0x3800e
	Visibility                  Hash = 0x9fa0a
	Voice_Family                Hash = 0xa040c
	Volume                      Hash = 0xa1006
	White                       Hash = 0x1e305
	White_Space                 Hash = 0x4630b
	Whitesmoke                  Hash = 0x3f90a
	Widows                      Hash = 0x5c006
	Width                       Hash = 0xef05
	Word_Break                  Hash = 0x2f50a
	Word_Spacing                Hash = 0x50d0c
	Word_Wrap                   Hash = 0x5f109
	Writing_Mode                Hash = 0x66b0c
	Yellow                      Hash = 0x5ec06
	Yellowgreen                 Hash = 0x79b0b
	Z_Index                     Hash = 0xa1907
)

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 io.Reader) *Lexer

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

Example

Code:

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

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) Free

func (l *Lexer) Free(n int)

Free frees up bytes of length n from previously shifted tokens.

func (*Lexer) Next

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

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 io.Reader, isStylesheet bool) *Parser

NewParser returns a new CSS parser from an io.Reader. isStylesheet specifies whether this is a regular stylesheet (true) or an inline style attribute (false).

Example

Code:

{
	p := NewParser(bytes.NewBufferString("color: red;"), false) // 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;
}

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) 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) 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() 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.

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
)

TokenType values.

func (TokenType) String

func (tt TokenType) String() string

String returns the string representation of a TokenType.

Source Files

hash.go lex.go parse.go util.go

Version
v1.1.0 (latest)
Published
Nov 2, 2015
Platform
linux/amd64
Imports
6 packages
Last checked
3 weeks ago

Tools for package owners.