# README
CSS

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 github.com/cdvelop/vanify/pkg/minify/parse/css
or add the following import and run project with go get
import "github.com/cdvelop/vanify/pkg/minify/parse/css"
Lexer
Usage
The following initializes a new Lexer with io.Reader r
:
l := css.NewLexer(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/cdvelop/vanify/pkg/minify/parse/css"
)
// Tokenize CSS3 from stdin.
func main() {
l := css.NewLexer(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(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/cdvelop/vanify/pkg/minify/parse/css"
)
func main() {
// true because this is the content of an inline style attribute
p := css.NewParser(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.
# Functions
HSL2RGB converts HSL to RGB with all of range [0,1] from http://www.w3.org/TR/css3-color/#hsl-color.
IsIdent returns true if the bytes are a valid identifier.
IsURLUnquoted returns true if the bytes are a valid unquoted URL.
NewLexer returns a new Lexer for a given io.Reader.
NewParser returns a new CSS parser from an io.Reader.
ToHash returns the hash whose name is s.
# Constants
accelerator.
aliceblue.
alpha.
antiquewhite.
aquamarine.
@abc.
GrammarType values.
azimuth.
background.
background-attachment.
background-color.
background-image.
background-position.
background-position-x.
background-position-y.
background-repeat.
background-size.
TokenType values.
TokenType values.
GrammarType values.
GrammarType values.
behavior.
black.
blanchedalmond.
blueviolet.
bold.
border.
border-bottom.
border-bottom-color.
border-bottom-style.
border-bottom-width.
border-box.
border-collapse.
border-color.
border-left.
border-left-color.
border-left-style.
border-left-width.
border-right.
border-right-color.
border-right-style.
border-right-width.
border-spacing.
border-style.
border-top.
border-top-color.
border-top-style.
border-top-width.
border-width.
bottom.
box-shadow.
burlywood.
cadetblue.
calc.
caption-side.
caret-color.
-->.
<!--.
center.
charset.
chartreuse.
chocolate.
clear.
clip.
:.
color.
column-rule.
column-rule-color.
||.
,.
GrammarType values.
extra token for comments.
content.
cornflowerblue.
cornsilk.
counter-increment.
counter-reset.
cue.
cue-after.
cue-before.
currentcolor.
cursive.
cursor.
GrammarType values.
TokenType values.
TokenType values.
darkblue.
darkcyan.
darkgoldenrod.
darkgray.
darkgreen.
darkkhaki.
darkmagenta.
darkolivegreen.
darkorange.
darkorchid.
darksalmon.
darkseagreen.
darkslateblue.
darkslategray.
darkturquoise.
darkviolet.
|=.
GrammarType values.
deeppink.
deepskyblue.
default.
any unmatched character.
5em.
direction.
display.
document.
dodgerblue.
elevation.
empty-cells.
TokenType values.
GrammarType values.
GrammarType values.
extra token when errors occur.
extra token when errors occur.
fantasy.
fill.
filter.
firebrick.
flex.
float.
floralwhite.
font.
font-face.
font-family.
font-size.
font-size-adjust.
font-stretch.
font-style.
font-variant.
font-weight.
forestgreen.
fuchsia.
rgb( rgba( ...
gainsboro.
ghostwhite.
goldenrod.
greenyellow.
grid.
#abc.
height.
honeydew.
hsl.
hsla.
TokenType values.
ime-mode.
import.
important.
include-source.
~=.
indianred.
inherit.
initial.
keyframes.
large.
larger.
lavender.
lavenderblush.
lawngreen.
layer-background-color.
layer-background-image.
layout-flow.
layout-grid.
layout-grid-char.
layout-grid-char-spacing.
layout-grid-line.
layout-grid-mode.
layout-grid-type.
left.
{.
[.
(.
lemonchiffon.
letter-spacing.
lightblue.
lightcoral.
lightcyan.
lightgoldenrodyellow.
lightgray.
lightgreen.
lightpink.
lightsalmon.
lightseagreen.
lightskyblue.
lightslateblue.
lightsteelblue.
lightyellow.
limegreen.
line-break.
line-height.
linear-gradient.
list-style.
list-style-image.
list-style-position.
list-style-type.
local.
magenta.
margin.
margin-bottom.
margin-left.
margin-right.
margin-top.
marker-offset.
marks.
mask.
max-height.
max-width.
media.
medium.
mediumaquamarine.
mediumblue.
mediumorchid.
mediumpurple.
mediumseagreen.
mediumslateblue.
mediumspringgreen.
mediumturquoise.
mediumvioletred.
midnightblue.
min-height.
min-width.
mintcream.
mistyrose.
moccasin.
monospace.
-ms-filter.
namespace.
navajowhite.
no-repeat.
none.
normal.
5.
olivedrab.
orangered.
orphans.
outline.
outline-color.
outline-style.
outline-width.
overflow.
overflow-x.
overflow-y.
padding.
padding-bottom.
padding-box.
padding-left.
padding-right.
padding-top.
page.
page-break-after.
page-break-before.
page-break-inside.
palegoldenrod.
palegreen.
paleturquoise.
palevioletred.
papayawhip.
pause.
pause-after.
pause-before.
peachpuff.
5%.
pitch.
pitch-range.
play-during.
position.
powderblue.
^=.
progid.
GrammarType values.
quotes.
radial-gradient.
repeat.
rgb.
rgba.
richness.
right.
}.
].
).
rosybrown.
round.
royalblue.
ruby-align.
ruby-overhang.
ruby-position.
saddlebrown.
sandybrown.
sans-serif.
scroll.
scrollbar-3d-light-color.
scrollbar-arrow-color.
scrollbar-base-color.
scrollbar-dark-shadow-color.
scrollbar-face-color.
scrollbar-highlight-color.
scrollbar-shadow-color.
scrollbar-track-color.
seagreen.
seashell.
;.
serif.
size.
slateblue.
slategray.
small.
smaller.
space.
speak.
speak-header.
speak-numeral.
speak-punctuation.
speech-rate.
springgreen.
steelblue.
stress.
TokenType values.
stroke.
*=.
$=.
supports.
table-layout.
text-align.
text-align-last.
text-autospace.
text-decoration.
text-decoration-color.
text-emphasis.
text-emphasis-color.
text-indent.
text-justify.
text-kashida-space.
text-overflow.
text-shadow.
text-transform.
text-underline-position.
GrammarType values.
top.
transition.
transparent.
turquoise.
unicode-bidi.
U+554A.
unset.
TokenType values.
vertical-align.
visibility.
voice-family.
volume.
white.
white-space.
whitesmoke.
space \t \r \n \f.
widows.
width.
word-break.
word-spacing.
word-wrap.
writing-mode.
x-large.
x-small.
xx-large.
xx-small.
yellow.
yellowgreen.
z-index.
# Type aliases
GrammarType determines the type of grammar.
Hash defines perfect hashes for a predefined list of strings.
State is the state function the parser currently is in.
TokenType determines the type of token, eg.