-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathblock_parser.go
147 lines (133 loc) · 3.65 KB
/
block_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package css
import (
"github.com/gorilla/css/scanner"
)
type blockParserContext struct {
State State
NowProperty string
NowValue []*scanner.Token
NowImportant bool
}
func (context *blockParserContext) extractDeclaration() *CSSStyleDeclaration {
decl := CSSStyleDeclaration{
Property: context.NowProperty,
Value: &CSSValue{Tokens: context.NowValue},
Important: context.NowImportant,
}
context.NowProperty = ""
context.NowValue = make([]*scanner.Token, 0)
context.NowImportant = false
return &decl
}
// ParseBlock take a string of a css block,
// parses it and returns a map of css style declarations.
func ParseBlock(csstext string) []*CSSStyleDeclaration {
s := scanner.New(csstext)
return parseBlock(s)
}
func parseBlock(s *scanner.Scanner) []*CSSStyleDeclaration {
/* block : '{' S* [ any | block | ATKEYWORD S* | ';' S* ]* '}' S*;
property : IDENT;
value : [ any | block | ATKEYWORD S* ]+;
any : [ IDENT | NUMBER | PERCENTAGE | DIMENSION | STRING
| DELIM | URI | HASH | UNICODE-RANGE | INCLUDES
| DASHMATCH | ':' | FUNCTION S* [any|unused]* ')'
| '(' S* [any|unused]* ')' | '[' S* [any|unused]* ']'
] S*;
*/
decls := make([]*CSSStyleDeclaration, 0)
context := &blockParserContext{
State: STATE_NONE,
NowProperty: "",
NowValue: make([]*scanner.Token, 0),
NowImportant: false,
}
for {
token := s.Next()
//fmt.Printf("BLOCK(%d): %s:'%s'\n", context.State, token.Type.String(), token.Value)
if token.Type == scanner.TokenError {
break
}
if token.Type == scanner.TokenEOF {
if context.State == STATE_VALUE {
// we are ending without ; or }
// this can happen when we parse only css declaration
decl := context.extractDeclaration()
decls = append(decls, decl)
}
break
}
switch token.Type {
case scanner.TokenS:
if context.State == STATE_VALUE {
context.NowValue = append(context.NowValue, token)
}
case scanner.TokenIdent:
if context.State == STATE_NONE {
context.State = STATE_PROPERTY
context.NowProperty += token.Value
break
}
if token.Value == "important" {
context.NowImportant = true
} else {
context.NowValue = append(context.NowValue, token)
}
case scanner.TokenChar:
if context.State == STATE_NONE {
if token.Value == "{" {
break
}
}
if context.State == STATE_PROPERTY {
if token.Value == ":" {
context.State = STATE_VALUE
}
// CHAR and STATE_PROPERTY but not : then weird
// break to ignore it
break
}
// should be no state or value
if token.Value == ";" {
decl := context.extractDeclaration()
decls = append(decls, decl)
context.State = STATE_NONE
} else if token.Value == "}" { // last property in a block can have optional ;
if context.State == STATE_VALUE {
// only valid if state is still VALUE, could be ;}
decl := context.extractDeclaration()
decls = append(decls, decl)
}
// we are done
return decls
} else if token.Value != "!" {
context.NowValue = append(context.NowValue, token)
}
break
// any
case scanner.TokenNumber:
fallthrough
case scanner.TokenPercentage:
fallthrough
case scanner.TokenDimension:
fallthrough
case scanner.TokenString:
fallthrough
case scanner.TokenURI:
fallthrough
case scanner.TokenHash:
fallthrough
case scanner.TokenUnicodeRange:
fallthrough
case scanner.TokenIncludes:
fallthrough
case scanner.TokenDashMatch:
fallthrough
case scanner.TokenFunction:
fallthrough
case scanner.TokenSubstringMatch:
context.NowValue = append(context.NowValue, token)
}
}
return decls
}