Documentation ¶
Overview ¶
Package csslex provides a simple CSS lexer, without using regexp.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Lex ¶
Lex creates a new lexer and returns channel which will be sent Item tokens. The lexing is started in a goroutine right away, before returing from this method.
Example ¶
const cssText = ` /* comment **/ @import url('style.css') print; body { background-color: white; color: #222 } div p, #id:first-line { white-space: nowrap; } @media print { body { font-size: 10pt } } .c1{color:red}.c2{color:blue} ` style := make(map[string]map[string]string) var skip bool var sel []string for item := range Lex(cssText) { switch item.Typ { case ItemError: log.Fatal(item) case ItemAtRuleIdent, ItemAtRule: continue case ItemAtRuleBlockStart: skip = true continue case ItemAtRuleBlockEnd: skip = false continue case ItemBlockEnd: sel = nil case ItemSelector: if skip { continue } sel = append(sel, item.Val) if _, ok := style[item.Val]; !ok { style[item.Val] = make(map[string]string) } case ItemDecl: if skip || len(sel) == 0 { continue } decl := strings.SplitN(item.Val, ":", 2) decl[0] = strings.TrimSpace(decl[0]) decl[1] = strings.TrimSpace(decl[1]) for _, s := range sel { style[s][decl[0]] = decl[1] } } } sb, err := json.MarshalIndent(style, "", " ") if err != nil { log.Fatal(err) } fmt.Println(string(sb))
Output: { "#id:first-line": { "white-space": "nowrap" }, ".c1": { "color": "red" }, ".c2": { "color": "blue" }, "body": { "background-color": "white", "color": "#222" }, "div p": { "white-space": "nowrap" } }
Types ¶
type ItemType ¶
type ItemType int
ItemType specifies type of Item.
const ( ItemError ItemType = iota // Parsing error. Lex stops at first error. ItemSelector // CSS selector. ItemDecl // CSS declaration in a block. ItemBlockStart // Beginning of a regular CSS block, not inside At-Rule. ItemBlockEnd // Ending of a regular CSS block, not inside At-Rule. ItemAtRuleIdent // At-Rule identifier, including @ symbol. ItemAtRule // The content of an At-Rule. ItemAtRuleBlockStart // Beginning of an At-Rule block. ItemAtRuleBlockEnd // Ending of an At-Rule block. )
Item types emitted by the lexer.
Click to show internal directories.
Click to hide internal directories.