Documentation ¶
Overview ¶
Package scanner generates tokens for a CSS2/3 input.
It is a CSS2 scanner with bits of a CSS3 scanner in it.
To use it, create a new scanner for a given CSS string and call Next() until the token returned has type scanner.EOF or scanner.Error:
s := scanner.New(myCSS) for { token := s.Next() if token.Type == scanner.TokenEOF || token.Type == scanner.TokenError { break } // Do something with the token... }
Following the CSS3 specification, an error can only occur when the scanner finds an unclosed quote or unclosed comment. In these cases the text becomes "untokenizable". Everything else is tokenizable and it is up to a parser to make sense of the token stream (or ignore nonsensical token sequences).
Note: the scanner doesn't perform lexical analysis or, in other words, it doesn't care about the token context. It is intended to be used by a lexer or parser.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var AtKeyword = Type{3}
AtKeyword token type is for things like @import. The .Value has the @ removed.
var BOM = Type{22}
BOM token type refers to Byte Order Marks.
var CDC = Type{12}
CDC token type represents the --> string.
var CDO = Type{11}
CDO token type represents the <!-- string.
var Comment = Type{14}
Comment token type is for comments. The internals of the comment will be in the .Value, with no additional processing.
var DashMatch = Type{17}
DashMatch token type refers to |=.
var Delim = Type{21}
Delim token type refers to a character which CSS does not otherwise know how to process as any of the above.
var Dimension = Type{8}
Dimension token type is for dimensions. No further parsing is done on the dimension, which may be bad since we could break in into number and unit.
var EOF = Type{1}
EOF token type is the end of the string.
var Error = Type{0}
Error token type is returned when there are errors in the parse.
CSS tries to avoid these; these are mostly unclosed strings and other things with delimiters.
var Function = Type{15}
Function token type refers to a function invocation, like "rgb(". The .Value does not have the parenthesis on it.
var Hash = Type{5}
Hash token type is for things like colors: #fff. The value does not contain the #.
var Ident = Type{2}
Ident token type for identifiers.
var Includes = Type{16}
Includes token type refers to ~=.
var Number = Type{6}
Number token type is for numbers that are not percentages or dimensions.
var Percentage = Type{7}
Percentage token type is for percentages. The .Value does not include the %.
var PrefixMatch = Type{18}
PrefixMatch token type refers to ^=.
var S = Type{13}
S token type is for whitespace. The original space content will be in .Value.
var String = Type{4}
String token type is for double- or single-quote delimited strings. The strings have been processed to their values and do not contain the quotes.
var SubstringMatch = Type{20}
SubstringMatch token type refers to *=.
var SuffixMatch = Type{19}
SuffixMatch token type refers to $=.
var URI = Type{9}
URI token type is for URIs. The .Value will be the processed URI.
var UnicodeRange = Type{10}
UnicodeRange token type is for Unicode ranges.
Functions ¶
Types ¶
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner scans an input and emits tokens following the CSS3 specification.
type Token ¶
Token represents a token and the corresponding string.
func (*Token) Emit ¶
Emit will write a string representation of the given token to the target io.Writer. An error will be returned if you either try to emit Error or EOF, or if the Writer returns an error.
Emit will make many small writes to the io.Writer.
Emit assumes you have not set the token's .Value to an invalid value for many of these; for instance, if you manually take a Number token and set its .Value to "sometext", you will emit something that is not a number.