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 ¶
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 ¶ added in v1.1.0
HSL2RGB converts HSL to RGB with all of range [0,1] from http://www.w3.org/TR/css3-color/#hsl-color
func IsURLUnquoted ¶ added in v1.1.0
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 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
Hash defines perfect hashes for a predefined list of strings
const ( Accelerator Hash = 0x47f0b // accelerator Aliceblue Hash = 0x52509 // aliceblue Alpha Hash = 0x5af05 // alpha Antiquewhite Hash = 0x45c0c // antiquewhite Aquamarine Hash = 0x7020a // aquamarine Azimuth Hash = 0x5b307 // azimuth Background Hash = 0xa // background Background_Attachment Hash = 0x3a15 // background-attachment Background_Color Hash = 0x11c10 // background-color Background_Image Hash = 0x99210 // background-image Background_Position Hash = 0x13 // background-position Background_Position_X Hash = 0x80815 // background-position-x Background_Position_Y Hash = 0x15 // background-position-y Background_Repeat Hash = 0x1511 // background-repeat Behavior Hash = 0x3108 // behavior Black Hash = 0x6005 // black Blanchedalmond Hash = 0x650e // blanchedalmond Blueviolet Hash = 0x52a0a // blueviolet Bold Hash = 0x7a04 // bold Border Hash = 0x8506 // border Border_Bottom Hash = 0x850d // border-bottom Border_Bottom_Color Hash = 0x8513 // border-bottom-color Border_Bottom_Style Hash = 0xbe13 // border-bottom-style Border_Bottom_Width Hash = 0xe113 // border-bottom-width Border_Collapse Hash = 0x1020f // border-collapse Border_Color Hash = 0x1350c // border-color Border_Left Hash = 0x15c0b // border-left Border_Left_Color Hash = 0x15c11 // border-left-color Border_Left_Style Hash = 0x17911 // border-left-style Border_Left_Width Hash = 0x18a11 // border-left-width Border_Right Hash = 0x19b0c // border-right Border_Right_Color Hash = 0x19b12 // border-right-color Border_Right_Style Hash = 0x1ad12 // border-right-style Border_Right_Width Hash = 0x1bf12 // border-right-width Border_Spacing Hash = 0x1d10e // border-spacing Border_Style Hash = 0x1f40c // border-style Border_Top Hash = 0x2000a // border-top Border_Top_Color Hash = 0x20010 // border-top-color Border_Top_Style Hash = 0x21010 // border-top-style Border_Top_Width Hash = 0x22010 // border-top-width Border_Width Hash = 0x2300c // border-width Bottom Hash = 0x8c06 // bottom Burlywood Hash = 0x23c09 // burlywood Cadetblue Hash = 0x25809 // cadetblue Caption_Side Hash = 0x2610c // caption-side Charset Hash = 0x44207 // charset Chartreuse Hash = 0x2730a // chartreuse Chocolate Hash = 0x27d09 // chocolate Clear Hash = 0x2ab05 // clear Clip Hash = 0x2b004 // clip Color Hash = 0x9305 // color Content Hash = 0x2e507 // content Cornflowerblue Hash = 0x2ff0e // cornflowerblue Cornsilk Hash = 0x30d08 // cornsilk Counter_Increment Hash = 0x31511 // counter-increment Counter_Reset Hash = 0x3540d // counter-reset Cue Hash = 0x36103 // cue Cue_After Hash = 0x36109 // cue-after Cue_Before Hash = 0x36a0a // cue-before Cursive Hash = 0x37b07 // cursive Cursor Hash = 0x38e06 // cursor Darkblue Hash = 0x7208 // darkblue Darkcyan Hash = 0x7d08 // darkcyan Darkgoldenrod Hash = 0x2440d // darkgoldenrod Darkgray Hash = 0x25008 // darkgray Darkgreen Hash = 0x79209 // darkgreen Darkkhaki Hash = 0x88509 // darkkhaki Darkmagenta Hash = 0x4f40b // darkmagenta Darkolivegreen Hash = 0x7210e // darkolivegreen Darkorange Hash = 0x7860a // darkorange Darkorchid Hash = 0x87c0a // darkorchid Darksalmon Hash = 0x8c00a // darksalmon Darkseagreen Hash = 0x9240c // darkseagreen Darkslateblue Hash = 0x3940d // darkslateblue Darkslategray Hash = 0x3a10d // darkslategray Darkturquoise Hash = 0x3ae0d // darkturquoise Darkviolet Hash = 0x3bb0a // darkviolet Deeppink Hash = 0x26b08 // deeppink Deepskyblue Hash = 0x8930b // deepskyblue Default Hash = 0x57b07 // default Direction Hash = 0x9f109 // direction Display Hash = 0x3c507 // display Document Hash = 0x3d308 // document Dodgerblue Hash = 0x3db0a // dodgerblue Elevation Hash = 0x4a009 // elevation Empty_Cells Hash = 0x4c20b // empty-cells Fantasy Hash = 0x5ce07 // fantasy Filter Hash = 0x59806 // filter Firebrick Hash = 0x3e509 // firebrick Float Hash = 0x3ee05 // float Floralwhite Hash = 0x3f30b // floralwhite Font Hash = 0xd804 // font Font_Face Hash = 0xd809 // font-face Font_Family Hash = 0x41d0b // font-family Font_Size Hash = 0x42809 // font-size Font_Size_Adjust Hash = 0x42810 // font-size-adjust Font_Stretch Hash = 0x4380c // font-stretch Font_Style Hash = 0x4490a // font-style Font_Variant Hash = 0x4530c // font-variant Font_Weight Hash = 0x46e0b // font-weight Forestgreen Hash = 0x3700b // forestgreen Fuchsia Hash = 0x47907 // fuchsia Gainsboro Hash = 0x14c09 // gainsboro Ghostwhite Hash = 0x1de0a // ghostwhite Goldenrod Hash = 0x24809 // goldenrod Greenyellow Hash = 0x7960b // greenyellow Height Hash = 0x68506 // height Honeydew Hash = 0x5b908 // honeydew Hsl Hash = 0xf303 // hsl Hsla Hash = 0xf304 // hsla Ime_Mode Hash = 0x88d08 // ime-mode Import Hash = 0x4e306 // import Important Hash = 0x4e309 // important Include_Source Hash = 0x7f20e // include-source Indianred Hash = 0x4ec09 // indianred Inherit Hash = 0x51907 // inherit Initial Hash = 0x52007 // initial Keyframes Hash = 0x40109 // keyframes Lavender Hash = 0xf508 // lavender Lavenderblush Hash = 0xf50d // lavenderblush Lawngreen Hash = 0x4da09 // lawngreen Layer_Background_Color Hash = 0x11616 // layer-background-color Layer_Background_Image Hash = 0x98c16 // layer-background-image Layout_Flow Hash = 0x5030b // layout-flow Layout_Grid Hash = 0x53f0b // layout-grid Layout_Grid_Char Hash = 0x53f10 // layout-grid-char Layout_Grid_Char_Spacing Hash = 0x53f18 // layout-grid-char-spacing Layout_Grid_Line Hash = 0x55710 // layout-grid-line Layout_Grid_Mode Hash = 0x56d10 // layout-grid-mode Layout_Grid_Type Hash = 0x58210 // layout-grid-type Left Hash = 0x16304 // left Lemonchiffon Hash = 0xcf0c // lemonchiffon Letter_Spacing Hash = 0x5310e // letter-spacing Lightblue Hash = 0x59e09 // lightblue Lightcoral Hash = 0x5a70a // lightcoral Lightcyan Hash = 0x5d509 // lightcyan Lightgoldenrodyellow Hash = 0x5de14 // lightgoldenrodyellow Lightgray Hash = 0x60509 // lightgray Lightgreen Hash = 0x60e0a // lightgreen Lightpink Hash = 0x61809 // lightpink Lightsalmon Hash = 0x6210b // lightsalmon Lightseagreen Hash = 0x62c0d // lightseagreen Lightskyblue Hash = 0x6390c // lightskyblue Lightslateblue Hash = 0x6450e // lightslateblue Lightsteelblue Hash = 0x6530e // lightsteelblue Lightyellow Hash = 0x6610b // lightyellow Limegreen Hash = 0x67709 // limegreen Line_Break Hash = 0x5630a // line-break Line_Height Hash = 0x6800b // line-height List_Style Hash = 0x68b0a // list-style List_Style_Image Hash = 0x68b10 // list-style-image List_Style_Position Hash = 0x69b13 // list-style-position List_Style_Type Hash = 0x6ae0f // list-style-type Magenta Hash = 0x4f807 // magenta Margin Hash = 0x2c006 // margin Margin_Bottom Hash = 0x2c00d // margin-bottom Margin_Left Hash = 0x2cc0b // margin-left Margin_Right Hash = 0x3320c // margin-right Margin_Top Hash = 0x7cd0a // margin-top Marker_Offset Hash = 0x6bd0d // marker-offset Marks Hash = 0x6ca05 // marks Max_Height Hash = 0x6e90a // max-height Max_Width Hash = 0x6f309 // max-width Media Hash = 0xa1405 // media Mediumaquamarine Hash = 0x6fc10 // mediumaquamarine Mediumblue Hash = 0x70c0a // mediumblue Mediumorchid Hash = 0x7160c // mediumorchid Mediumpurple Hash = 0x72f0c // mediumpurple Mediumseagreen Hash = 0x73b0e // mediumseagreen Mediumslateblue Hash = 0x7490f // mediumslateblue Mediumspringgreen Hash = 0x75811 // mediumspringgreen Mediumturquoise Hash = 0x7690f // mediumturquoise Mediumvioletred Hash = 0x7780f // mediumvioletred Midnightblue Hash = 0x7a60c // midnightblue Min_Height Hash = 0x7b20a // min-height Min_Width Hash = 0x7bc09 // min-width Mintcream Hash = 0x7c509 // mintcream Mistyrose Hash = 0x7e309 // mistyrose Moccasin Hash = 0x7ec08 // moccasin Monospace Hash = 0x8c709 // monospace Namespace Hash = 0x49809 // namespace None Hash = 0x4bf04 // none Normal Hash = 0x4d506 // normal Olivedrab Hash = 0x80009 // olivedrab Orangered Hash = 0x78a09 // orangered Orphans Hash = 0x48807 // orphans Outline Hash = 0x81d07 // outline Outline_Color Hash = 0x81d0d // outline-color Outline_Style Hash = 0x82a0d // outline-style Outline_Width Hash = 0x8370d // outline-width Overflow Hash = 0x2db08 // overflow Overflow_X Hash = 0x2db0a // overflow-x Overflow_Y Hash = 0x8440a // overflow-y Padding Hash = 0x2b307 // padding Padding_Bottom Hash = 0x2b30e // padding-bottom Padding_Left Hash = 0x5f90c // padding-left Padding_Right Hash = 0x7d60d // padding-right Padding_Top Hash = 0x8d90b // padding-top Page Hash = 0x84e04 // page Page_Break_After Hash = 0x8e310 // page-break-after Page_Break_Before Hash = 0x84e11 // page-break-before Page_Break_Inside Hash = 0x85f11 // page-break-inside Palegoldenrod Hash = 0x8700d // palegoldenrod Palegreen Hash = 0x89e09 // palegreen Paleturquoise Hash = 0x8a70d // paleturquoise Palevioletred Hash = 0x8b40d // palevioletred Papayawhip Hash = 0x8d00a // papayawhip Pause Hash = 0x8f305 // pause Pause_After Hash = 0x8f30b // pause-after Pause_Before Hash = 0x8fe0c // pause-before Peachpuff Hash = 0x59009 // peachpuff Pitch Hash = 0x90a05 // pitch Pitch_Range Hash = 0x90a0b // pitch-range Play_During Hash = 0x3c80b // play-during Position Hash = 0xb08 // position Powderblue Hash = 0x9150a // powderblue Progid Hash = 0x91f06 // progid Quotes Hash = 0x93006 // quotes Rgb Hash = 0x3803 // rgb Rgba Hash = 0x3804 // rgba Richness Hash = 0x9708 // richness Right Hash = 0x1a205 // right Rosybrown Hash = 0x15309 // rosybrown Royalblue Hash = 0xb509 // royalblue Ruby_Align Hash = 0x12b0a // ruby-align Ruby_Overhang Hash = 0x1400d // ruby-overhang Ruby_Position Hash = 0x16c0d // ruby-position Saddlebrown Hash = 0x48e0b // saddlebrown Sandybrown Hash = 0x4cc0a // sandybrown Sans_Serif Hash = 0x5c50a // sans-serif Scrollbar_3d_Light_Color Hash = 0x9e18 // scrollbar-3d-light-color Scrollbar_Arrow_Color Hash = 0x29615 // scrollbar-arrow-color Scrollbar_Base_Color Hash = 0x40914 // scrollbar-base-color Scrollbar_Dark_Shadow_Color Hash = 0x6ce1b // scrollbar-dark-shadow-color Scrollbar_Face_Color Hash = 0x93514 // scrollbar-face-color Scrollbar_Highlight_Color Hash = 0x9ce19 // scrollbar-highlight-color Scrollbar_Shadow_Color Hash = 0x94916 // scrollbar-shadow-color Scrollbar_Track_Color Hash = 0x95f15 // scrollbar-track-color Seagreen Hash = 0x63108 // seagreen Seashell Hash = 0x10f08 // seashell Serif Hash = 0x5ca05 // serif Size Hash = 0x42d04 // size Slateblue Hash = 0x39809 // slateblue Slategray Hash = 0x3a509 // slategray Speak Hash = 0x97405 // speak Speak_Header Hash = 0x9740c // speak-header Speak_Numeral Hash = 0x9800d // speak-numeral Speak_Punctuation Hash = 0x9a211 // speak-punctuation Speech_Rate Hash = 0x9b30b // speech-rate Springgreen Hash = 0x75e0b // springgreen Steelblue Hash = 0x65809 // steelblue Stress Hash = 0x29106 // stress Supports Hash = 0x9c708 // supports Table_Layout Hash = 0x4fd0c // table-layout Text_Align Hash = 0x2840a // text-align Text_Align_Last Hash = 0x2840f // text-align-last Text_Autospace Hash = 0x1e60e // text-autospace Text_Decoration Hash = 0x4b10f // text-decoration Text_Indent Hash = 0x9bc0b // text-indent Text_Justify Hash = 0x250c // text-justify Text_Kashida_Space Hash = 0x4e12 // text-kashida-space Text_Overflow Hash = 0x2d60d // text-overflow Text_Shadow Hash = 0x2eb0b // text-shadow Text_Transform Hash = 0x3250e // text-transform Text_Underline_Position Hash = 0x33d17 // text-underline-position Top Hash = 0x20703 // top Turquoise Hash = 0x3b209 // turquoise Unicode_Bidi Hash = 0x9e70c // unicode-bidi Vertical_Align Hash = 0x3800e // vertical-align Visibility Hash = 0x9fa0a // visibility Voice_Family Hash = 0xa040c // voice-family Volume Hash = 0xa1006 // volume White Hash = 0x1e305 // white White_Space Hash = 0x4630b // white-space Whitesmoke Hash = 0x3f90a // whitesmoke Widows Hash = 0x5c006 // widows Width Hash = 0xef05 // width Word_Break Hash = 0x2f50a // word-break Word_Spacing Hash = 0x50d0c // word-spacing Word_Wrap Hash = 0x5f109 // word-wrap Writing_Mode Hash = 0x66b0c // writing-mode Yellow Hash = 0x5ec06 // yellow Yellowgreen Hash = 0x79b0b // yellowgreen Z_Index Hash = 0xa1907 // z-index )
Unique hash definitions to be used instead of strings
type Lexer ¶
type Lexer struct {
// contains filtered or unexported fields
}
Lexer is the state for the lexer.
func NewLexer ¶
NewLexer returns a new Lexer for a given io.Reader.
Example ¶
l := NewLexer(bytes.NewBufferString("color: red;")) out := "" for { tt, data := l.Next() if tt == ErrorToken { break } else if tt == WhitespaceToken || tt == CommentToken { continue } out += string(data) l.Free(len(data)) } fmt.Println(out)
Output: color:red;
func (Lexer) Err ¶
Err returns the error encountered during lexing, this is often io.EOF but also other errors can be returned.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is the state for the parser.
func NewParser ¶
NewParser returns a new CSS parser from an io.Reader. isInline specifies whether this is an inline style attribute.
Example ¶
p := NewParser(bytes.NewBufferString("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 ¶
Err returns the error encountered during parsing, this is often io.EOF but also other errors can be returned.
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.