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 ¶
- func HSL2RGB(h, s, l float64) (float64, float64, float64)
- func IsIdent(b []byte) bool
- func IsURLUnquoted(b []byte) bool
- type GrammarType
- type Hash
- type Lexer
- func NewLexer(r io.Reader) *Lexer
- func (l *Lexer) Err() error
- func (l *Lexer) Next() (TokenType, []byte)
- func (l *Lexer) Restore()
- type Parser
- func NewParser(r io.Reader, isInline bool) *Parser
- func (p *Parser) Err() error
- func (p *Parser) Next() (GrammarType, TokenType, []byte)
- func (p *Parser) Restore()
- func (p *Parser) Values() []Token
- type State
- type Token
- type TokenType
Examples ¶
Functions ¶
func HSL2RGB ¶
HSL2RGB converts HSL to RGB with all of range [0,1] from http://www.w3.org/TR/css3-color/#hsl-color
func IsIdent ¶
IsIdent returns true if the bytes are a valid identifier.
func IsURLUnquoted ¶
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 ( Accelerator Hash = 0x4b30b // accelerator Aliceblue Hash = 0x56109 // aliceblue Alpha Hash = 0x5eb05 // alpha Antiquewhite Hash = 0x4900c // antiquewhite Aquamarine Hash = 0x73a0a // aquamarine Azimuth Hash = 0x5ef07 // azimuth Background Hash = 0xa // background Background_Attachment Hash = 0x9115 // background-attachment Background_Color Hash = 0x11210 // background-color Background_Image Hash = 0x9ca10 // background-image Background_Position Hash = 0x13 // background-position Background_Position_X Hash = 0x84015 // background-position-x Background_Position_Y Hash = 0x15 // background-position-y Background_Repeat Hash = 0x1511 // background-repeat Behavior Hash = 0x3108 // behavior Black Hash = 0x5805 // black Blanchedalmond Hash = 0x5d0e // blanchedalmond Blueviolet Hash = 0x5660a // blueviolet Bold Hash = 0x7204 // bold Border Hash = 0x7d06 // border Border_Bottom Hash = 0x7d0d // border-bottom Border_Bottom_Color Hash = 0x7d13 // border-bottom-color Border_Bottom_Style Hash = 0xb413 // border-bottom-style Border_Bottom_Width Hash = 0xd713 // border-bottom-width Border_Collapse Hash = 0xf80f // border-collapse Border_Color Hash = 0x1480c // border-color Border_Left Hash = 0x15d0b // border-left Border_Left_Color Hash = 0x15d11 // border-left-color Border_Left_Style Hash = 0x18911 // border-left-style Border_Left_Width Hash = 0x19a11 // border-left-width Border_Right Hash = 0x1ab0c // border-right Border_Right_Color Hash = 0x1ab12 // border-right-color Border_Right_Style Hash = 0x1c912 // border-right-style Border_Right_Width Hash = 0x1db12 // border-right-width Border_Spacing Hash = 0x1ed0e // border-spacing Border_Style Hash = 0x2100c // border-style Border_Top Hash = 0x21c0a // border-top Border_Top_Color Hash = 0x21c10 // border-top-color Border_Top_Style Hash = 0x22c10 // border-top-style Border_Top_Width Hash = 0x23c10 // border-top-width Border_Width Hash = 0x24c0c // border-width Bottom Hash = 0x8406 // bottom Box_Shadow Hash = 0x2580a // box-shadow Burlywood Hash = 0x26b09 // burlywood Cadetblue Hash = 0x28a09 // cadetblue Calc Hash = 0x28704 // calc Caption_Side Hash = 0x2930c // caption-side Charset Hash = 0x47607 // charset Chartreuse Hash = 0x2a50a // chartreuse Chocolate Hash = 0x2af09 // chocolate Clear Hash = 0x2dd05 // clear Clip Hash = 0x2e204 // clip Color Hash = 0x8b05 // color Content Hash = 0x31e07 // content Cornflowerblue Hash = 0x3530e // cornflowerblue Cornsilk Hash = 0x36108 // cornsilk Counter_Increment Hash = 0x36911 // counter-increment Counter_Reset Hash = 0x3840d // counter-reset Cue Hash = 0x39103 // cue Cue_After Hash = 0x39109 // cue-after Cue_Before Hash = 0x39a0a // cue-before Cursive Hash = 0x3ab07 // cursive Cursor Hash = 0x3be06 // cursor Darkblue Hash = 0x6a08 // darkblue Darkcyan Hash = 0x7508 // darkcyan Darkgoldenrod Hash = 0x2730d // darkgoldenrod Darkgray Hash = 0x27f08 // darkgray Darkgreen Hash = 0x7ca09 // darkgreen Darkkhaki Hash = 0x8bd09 // darkkhaki Darkmagenta Hash = 0x5280b // darkmagenta Darkolivegreen Hash = 0x7590e // darkolivegreen Darkorange Hash = 0x7be0a // darkorange Darkorchid Hash = 0x8b40a // darkorchid Darksalmon Hash = 0x8f80a // darksalmon Darkseagreen Hash = 0x95c0c // darkseagreen Darkslateblue Hash = 0x3c40d // darkslateblue Darkslategray Hash = 0x3d10d // darkslategray Darkturquoise Hash = 0x3de0d // darkturquoise Darkviolet Hash = 0x3eb0a // darkviolet Deeppink Hash = 0x29d08 // deeppink Deepskyblue Hash = 0x8cb0b // deepskyblue Default Hash = 0x5b707 // default Direction Hash = 0xa2909 // direction Display Hash = 0x3f507 // display Document Hash = 0x40308 // document Dodgerblue Hash = 0x40b0a // dodgerblue Elevation Hash = 0x4d409 // elevation Empty_Cells Hash = 0x4f60b // empty-cells Fantasy Hash = 0x60a07 // fantasy Filter Hash = 0x5d406 // filter Firebrick Hash = 0x41509 // firebrick Flex Hash = 0x41e04 // flex Float Hash = 0x42205 // float Floralwhite Hash = 0x4270b // floralwhite Font Hash = 0xce04 // font Font_Face Hash = 0xce09 // font-face Font_Family Hash = 0x4510b // font-family Font_Size Hash = 0x45c09 // font-size Font_Size_Adjust Hash = 0x45c10 // font-size-adjust Font_Stretch Hash = 0x46c0c // font-stretch Font_Style Hash = 0x47d0a // font-style Font_Variant Hash = 0x4870c // font-variant Font_Weight Hash = 0x4a20b // font-weight Forestgreen Hash = 0x3a00b // forestgreen Fuchsia Hash = 0x4ad07 // fuchsia Gainsboro Hash = 0x17909 // gainsboro Ghostwhite Hash = 0x1fa0a // ghostwhite Goldenrod Hash = 0x27709 // goldenrod Greenyellow Hash = 0x7ce0b // greenyellow Height Hash = 0x6ae06 // height Honeydew Hash = 0x5f508 // honeydew Hsl Hash = 0xe903 // hsl Hsla Hash = 0xe904 // hsla Ime_Mode Hash = 0x8c508 // ime-mode Import Hash = 0x51706 // import Important Hash = 0x51709 // important Include_Source Hash = 0x82a0e // include-source Indianred Hash = 0x52009 // indianred Inherit Hash = 0x55507 // inherit Initial Hash = 0x55c07 // initial Keyframes Hash = 0x43509 // keyframes Lavender Hash = 0xeb08 // lavender Lavenderblush Hash = 0xeb0d // lavenderblush Lawngreen Hash = 0x50e09 // lawngreen Layer_Background_Color Hash = 0x10c16 // layer-background-color Layer_Background_Image Hash = 0x9c416 // layer-background-image Layout_Flow Hash = 0x5370b // layout-flow Layout_Grid Hash = 0x57b0b // layout-grid Layout_Grid_Char Hash = 0x57b10 // layout-grid-char Layout_Grid_Char_Spacing Hash = 0x57b18 // layout-grid-char-spacing Layout_Grid_Line Hash = 0x59310 // layout-grid-line Layout_Grid_Mode Hash = 0x5a910 // layout-grid-mode Layout_Grid_Type Hash = 0x5be10 // layout-grid-type Left Hash = 0x16404 // left Lemonchiffon Hash = 0xc50c // lemonchiffon Letter_Spacing Hash = 0x56d0e // letter-spacing Lightblue Hash = 0x5da09 // lightblue Lightcoral Hash = 0x5e30a // lightcoral Lightcyan Hash = 0x61109 // lightcyan Lightgoldenrodyellow Hash = 0x61a14 // lightgoldenrodyellow Lightgray Hash = 0x63909 // lightgray Lightgreen Hash = 0x6420a // lightgreen Lightpink Hash = 0x64c09 // lightpink Lightsalmon Hash = 0x6550b // lightsalmon Lightseagreen Hash = 0x6600d // lightseagreen Lightskyblue Hash = 0x66d0c // lightskyblue Lightslateblue Hash = 0x6790e // lightslateblue Lightsteelblue Hash = 0x6870e // lightsteelblue Lightyellow Hash = 0x6950b // lightyellow Limegreen Hash = 0x6a009 // limegreen Line_Break Hash = 0x59f0a // line-break Line_Height Hash = 0x6a90b // line-height Linear_Gradient Hash = 0x6b40f // linear-gradient List_Style Hash = 0x6c30a // list-style List_Style_Image Hash = 0x6c310 // list-style-image List_Style_Position Hash = 0x6d313 // list-style-position List_Style_Type Hash = 0x6e60f // list-style-type Magenta Hash = 0x52c07 // magenta Margin Hash = 0x2f206 // margin Margin_Bottom Hash = 0x2f20d // margin-bottom Margin_Left Hash = 0x2fe0b // margin-left Margin_Right Hash = 0x3310c // margin-right Margin_Top Hash = 0x8050a // margin-top Marker_Offset Hash = 0x6f50d // marker-offset Marks Hash = 0x70205 // marks Max_Height Hash = 0x7210a // max-height Max_Width Hash = 0x72b09 // max-width Media Hash = 0xa4c05 // media Mediumaquamarine Hash = 0x73410 // mediumaquamarine Mediumblue Hash = 0x7440a // mediumblue Mediumorchid Hash = 0x74e0c // mediumorchid Mediumpurple Hash = 0x7670c // mediumpurple Mediumseagreen Hash = 0x7730e // mediumseagreen Mediumslateblue Hash = 0x7810f // mediumslateblue Mediumspringgreen Hash = 0x79011 // mediumspringgreen Mediumturquoise Hash = 0x7a10f // mediumturquoise Mediumvioletred Hash = 0x7b00f // mediumvioletred Midnightblue Hash = 0x7de0c // midnightblue Min_Height Hash = 0x7ea0a // min-height Min_Width Hash = 0x7f409 // min-width Mintcream Hash = 0x7fd09 // mintcream Mistyrose Hash = 0x81b09 // mistyrose Moccasin Hash = 0x82408 // moccasin Monospace Hash = 0x8ff09 // monospace Namespace Hash = 0x4cc09 // namespace Hash = 0x4dc0b // navajowhite None Hash = 0x4f304 // none Normal Hash = 0x50906 // normal Olivedrab Hash = 0x83809 // olivedrab Orangered Hash = 0x7c209 // orangered Orphans Hash = 0x4bc07 // orphans Outline Hash = 0x85507 // outline Outline_Color Hash = 0x8550d // outline-color Outline_Style Hash = 0x8620d // outline-style Outline_Width Hash = 0x86f0d // outline-width Overflow Hash = 0xaa08 // overflow Overflow_X Hash = 0xaa0a // overflow-x Overflow_Y Hash = 0x87c0a // overflow-y Padding Hash = 0x2e507 // padding Padding_Bottom Hash = 0x2e50e // padding-bottom Padding_Left Hash = 0x5490c // padding-left Padding_Right Hash = 0x80e0d // padding-right Padding_Top Hash = 0x9110b // padding-top Page Hash = 0x88604 // page Page_Break_After Hash = 0x91b10 // page-break-after Page_Break_Before Hash = 0x88611 // page-break-before Page_Break_Inside Hash = 0x89711 // page-break-inside Palegoldenrod Hash = 0x8a80d // palegoldenrod Palegreen Hash = 0x8d609 // palegreen Paleturquoise Hash = 0x8df0d // paleturquoise Palevioletred Hash = 0x8ec0d // palevioletred Papayawhip Hash = 0x9080a // papayawhip Pause Hash = 0x92b05 // pause Pause_After Hash = 0x92b0b // pause-after Pause_Before Hash = 0x9360c // pause-before Peachpuff Hash = 0x5cc09 // peachpuff Pitch Hash = 0x94205 // pitch Pitch_Range Hash = 0x9420b // pitch-range Play_During Hash = 0x3f80b // play-during Position Hash = 0xb08 // position Powderblue Hash = 0x94d0a // powderblue Progid Hash = 0x95706 // progid Quotes Hash = 0x96806 // quotes Radial_Gradient Hash = 0x380f // radial-gradient Rgb Hash = 0x8f03 // rgb Rgba Hash = 0x8f04 // rgba Richness Hash = 0x12108 // richness Right Hash = 0x1b205 // right Rosybrown Hash = 0x18009 // rosybrown Royalblue Hash = 0x13f09 // royalblue Ruby_Align Hash = 0x1530a // ruby-align Ruby_Overhang Hash = 0x16d0d // ruby-overhang Ruby_Position Hash = 0x1bc0d // ruby-position Saddlebrown Hash = 0x4c20b // saddlebrown Sandybrown Hash = 0x5000a // sandybrown Sans_Serif Hash = 0x6010a // sans-serif Scrollbar_3d_Light_Color Hash = 0x12818 // scrollbar-3d-light-color Scrollbar_Arrow_Color Hash = 0x2c815 // scrollbar-arrow-color Scrollbar_Base_Color Hash = 0x43d14 // scrollbar-base-color Scrollbar_Dark_Shadow_Color Hash = 0x7061b // scrollbar-dark-shadow-color Scrollbar_Face_Color Hash = 0x96d14 // scrollbar-face-color Scrollbar_Highlight_Color Hash = 0xa0619 // scrollbar-highlight-color Scrollbar_Shadow_Color Hash = 0x98116 // scrollbar-shadow-color Scrollbar_Track_Color Hash = 0x99715 // scrollbar-track-color Seagreen Hash = 0x66508 // seagreen Seashell Hash = 0x10508 // seashell Serif Hash = 0x60605 // serif Size Hash = 0x46104 // size Slateblue Hash = 0x3c809 // slateblue Slategray Hash = 0x3d509 // slategray Speak Hash = 0x9ac05 // speak Speak_Header Hash = 0x9ac0c // speak-header Speak_Numeral Hash = 0x9b80d // speak-numeral Speak_Punctuation Hash = 0x9da11 // speak-punctuation Speech_Rate Hash = 0x9eb0b // speech-rate Springgreen Hash = 0x7960b // springgreen Steelblue Hash = 0x68c09 // steelblue Stress Hash = 0x2c306 // stress Supports Hash = 0x9ff08 // supports Table_Layout Hash = 0x5310c // table-layout Text_Align Hash = 0x2b60a // text-align Text_Align_Last Hash = 0x2b60f // text-align-last Text_Autospace Hash = 0x2020e // text-autospace Text_Decoration Hash = 0x4e50f // text-decoration Text_Indent Hash = 0x9f40b // text-indent Text_Justify Hash = 0x250c // text-justify Text_Kashida_Space Hash = 0x4612 // text-kashida-space Text_Overflow Hash = 0xa50d // text-overflow Text_Shadow Hash = 0x3080b // text-shadow Text_Transform Hash = 0x3240e // text-transform Text_Underline_Position Hash = 0x33c17 // text-underline-position Top Hash = 0x22303 // top Transparent Hash = 0x3790b // transparent Turquoise Hash = 0x3e209 // turquoise Unicode_Bidi Hash = 0xa1f0c // unicode-bidi Vertical_Align Hash = 0x3b00e // vertical-align Visibility Hash = 0xa320a // visibility Voice_Family Hash = 0xa3c0c // voice-family Volume Hash = 0xa4806 // volume White Hash = 0x1ff05 // white White_Space Hash = 0x4970b // white-space Whitesmoke Hash = 0x42d0a // whitesmoke Widows Hash = 0x5fc06 // widows Width Hash = 0xe505 // width Word_Break Hash = 0x2610a // word-break Word_Spacing Hash = 0x3120c // word-spacing Word_Wrap Hash = 0x54109 // word-wrap Writing_Mode Hash = 0x62d0c // writing-mode Yellow Hash = 0x62806 // yellow Yellowgreen Hash = 0x7d30b // yellowgreen Z_Index Hash = 0xa5107 // z-index )
Unique hash definitions to be used instead of strings
func ToHash ¶
ToHash returns the hash whose name is s. It returns zero if there is no such hash. It is case sensitive.
func (Hash) 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 ¶
NewLexer returns a new Lexer for a given io.Reader.
Code:
Output: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)
}
fmt.Println(out)
// Output: color:red;
}
color:red;
func (*Lexer) Err ¶
Err returns the error encountered during lexing, this is often io.EOF but also other errors can be returned.
func (*Lexer) Next ¶
Next returns the next Token. It returns ErrorToken when an error was encountered. Using Err() one can retrieve the error message.
func (*Lexer) Restore ¶
func (l *Lexer) Restore()
Restore restores the NULL byte at the end of the buffer.
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.
Code:
Output: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;
}
color:red;
func (*Parser) Err ¶
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) Restore ¶
func (p *Parser) Restore()
Restore restores the NULL byte at the end of the buffer.
func (*Parser) Values ¶
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 ¶
Token is a single TokenType and its associated data.
func (Token) 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 ¶
String returns the string representation of a TokenType.
Source Files ¶
hash.go lex.go parse.go util.go
- Version
- v2.3.3+incompatible
- Published
- Jun 7, 2018
- Platform
- windows/amd64
- Imports
- 5 packages
- Last checked
- 6 hours ago –
Tools for package owners.