-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparser.go
158 lines (143 loc) · 4.5 KB
/
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
148
149
150
151
152
153
154
155
156
157
158
package css
import (
"log"
"github.com/gorilla/css/scanner"
)
/*
stylesheet : [ CDO | CDC | S | statement ]*;
statement : ruleset | at-rule;
at-rule : ATKEYWORD S* any* [ block | ';' S* ];
block : '{' S* [ any | block | ATKEYWORD S* | ';' S* ]* '}' S*;
ruleset : selector? '{' S* declaration? [ ';' S* declaration? ]* '}' S*;
selector : any+;
declaration : property S* ':' S* value;
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*;
unused : block | ATKEYWORD S* | ';' S* | CDO S* | CDC S*;
*/
type State int
const (
STATE_NONE State = iota
STATE_SELECTOR
STATE_PROPERTY
STATE_VALUE
)
type parserContext struct {
State State
NowSelector []*scanner.Token
NowRuleType RuleType
CurrentNestedRule *CSSRule
}
func (context *parserContext) resetContextStyleRule() {
context.NowSelector = make([]*scanner.Token, 0)
context.NowRuleType = STYLE_RULE
context.State = STATE_NONE
}
func parseRule(context *parserContext, s *scanner.Scanner, css *CSSStyleSheet) {
rule := NewRule(context.NowRuleType)
selector := append(context.NowSelector, parseSelector(s)...)
rule.Style.Selector = &CSSValue{Tokens: selector}
rule.Style.Styles = parseBlock(s)
if context.CurrentNestedRule != nil {
context.CurrentNestedRule.Rules = append(context.CurrentNestedRule.Rules, rule)
} else {
css.CssRuleList = append(css.CssRuleList, rule)
}
context.resetContextStyleRule()
}
// Parse takes a string of valid css rules, stylesheet,
// and parses it. Be aware this function has poor error handling
// so you should have valid syntax in your css
func Parse(csstext string) *CSSStyleSheet {
context := &parserContext{
State: STATE_NONE,
NowSelector: make([]*scanner.Token, 0),
NowRuleType: STYLE_RULE,
CurrentNestedRule: nil,
}
css := &CSSStyleSheet{}
css.CssRuleList = make([]*CSSRule, 0)
s := scanner.New(csstext)
for {
token := s.Next()
if token.Type == scanner.TokenEOF || token.Type == scanner.TokenError {
break
}
switch token.Type {
case scanner.TokenCDO:
break
case scanner.TokenCDC:
break
case scanner.TokenComment:
break
case scanner.TokenS:
break
case scanner.TokenAtKeyword:
switch token.Value {
case "@media":
context.NowRuleType = MEDIA_RULE
case "@font-face":
// Parse as normal rule, would be nice to parse according to syntax
// https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face
context.NowRuleType = FONT_FACE_RULE
parseRule(context, s, css)
case "@import":
// No validation
// https://developer.mozilla.org/en-US/docs/Web/CSS/@import
rule := parseAtNoBody(s, IMPORT_RULE)
if rule != nil {
css.CssRuleList = append(css.CssRuleList, rule)
}
context.resetContextStyleRule()
case "@charset":
// No validation
// https://developer.mozilla.org/en-US/docs/Web/CSS/@charset
rule := parseAtNoBody(s, CHARSET_RULE)
if rule != nil {
css.CssRuleList = append(css.CssRuleList, rule)
}
context.resetContextStyleRule()
case "@page":
context.NowRuleType = PAGE_RULE
parseRule(context, s, css)
case "@keyframes":
context.NowRuleType = KEYFRAMES_RULE
case "@-webkit-keyframes":
context.NowRuleType = WEBKIT_KEYFRAMES_RULE
case "@counter-style":
context.NowRuleType = COUNTER_STYLE_RULE
parseRule(context, s, css)
default:
log.Printf("Skip unsupported atrule: %s", token.Value)
skipRules(s)
context.resetContextStyleRule()
}
default:
if context.State == STATE_NONE {
if token.Value == "}" && context.CurrentNestedRule != nil {
// close media/keyframe/… rule
css.CssRuleList = append(css.CssRuleList, context.CurrentNestedRule)
context.CurrentNestedRule = nil
break
}
}
if context.NowRuleType == MEDIA_RULE || context.NowRuleType == KEYFRAMES_RULE || context.NowRuleType == WEBKIT_KEYFRAMES_RULE {
context.CurrentNestedRule = NewRule(context.NowRuleType)
sel := append([]*scanner.Token{token}, parseSelector(s)...)
context.CurrentNestedRule.Style.Selector = &CSSValue{Tokens: sel}
context.resetContextStyleRule()
break
} else {
context.NowSelector = append(context.NowSelector, token)
parseRule(context, s, css)
break
}
}
}
return css
}