-
Notifications
You must be signed in to change notification settings - Fork 7
/
css.go
256 lines (223 loc) · 4.52 KB
/
css.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package htmlPDF
import (
"regexp"
"strconv"
)
type Stylesheet struct {
rules map[int]*Rule
}
type Rule struct {
selectors map[int]SimpleSelector
declaration map[int]Declaration
}
type SimpleSelector struct {
tag_name string
id string
class map[int]string
}
type Specificity struct {
a int
b int
c int
}
//Calculate specificity
//https://www.w3.org/TR/selectors/#specificity
//cahnge algorithms
func (s SimpleSelector) specificity() Specificity {
var a, b, c int
if len(s.id) > 0 {
a++
}
if len(s.class) > 0 {
b = len(s.class)
}
if len(s.tag_name) > 0 {
c++
}
return Specificity{a, b, c}
}
type Declaration struct {
name string
value Value
}
type Value struct {
keyword string
length Length
color Color
}
type Length struct {
value float64
unit string //only px
}
type Color struct {
r uint
g uint
b uint
a uint
}
type Parser struct {
pos int
input string
}
func validLengthChar(c string) bool {
var valid = regexp.MustCompile("[0-9.]")
return valid.MatchString(c)
}
//Parse a whole CSS stylesheet
func CssParser(source string) *Parser {
return &Parser{
pos: 0,
input: source,
}
}
//Parse a list of rule sets, separated by optional whitespace
func (p *Parser) parseRules() Stylesheet {
rules := map[int]*Rule{}
for {
p.consumeWhitespace()
if p.eof() {
break
}
rules[len(rules)] = p.parseRule()
}
return Stylesheet{rules}
}
//Parse a rule: 'selectors { declarations }'
//declarations it's pair of 'property: value;'
func (p *Parser) parseRule() *Rule {
return &Rule{
selectors: p.parseSelectors(),
declaration: p.parseDeclarations(),
}
}
//Parse a list of declarations enclosed in '{ ... }'
func (p *Parser) parseDeclarations() map[int]Declaration {
p.consumeChar()
decl := map[int]Declaration{}
for {
p.consumeWhitespace()
if p.nextChar() == "}" {
p.consumeChar()
break
}
decl[len(decl)] = p.parseDeclaration()
}
return decl
}
// Parse one declaration pair: 'property: value;'
func (p *Parser) parseDeclaration() Declaration {
name := p.parseIdentifier()
p.consumeWhitespace()
for p.consumeChar() != ":" {
}
p.consumeWhitespace()
value := p.parseValue()
p.consumeWhitespace()
for p.consumeChar() != ";" {
}
return Declaration{name, value}
}
//Parse value
func (p *Parser) parseValue() Value {
var valid = regexp.MustCompile("[0-9]")
switch {
case p.nextChar() == "#":
p.consumeChar()
return p.parseColor()
case valid.MatchString(string(p.nextChar())):
return p.parseLength()
default:
return Value{keyword: p.parseIdentifier()}
}
}
func (p *Parser) parseLength() Value {
return Value{length: p.parseFloat()}
}
//Parse value 000px, support only px
func (p *Parser) parseFloat() Length {
var result string
for !p.eof() && validLengthChar(p.nextChar()) {
result += string(p.consumeChar())
}
r, e := strconv.ParseFloat(result, 64)
if e != nil {
return Length{0, "px"}
}
return Length{r, "px"}
}
//Parse color #000000
func (p *Parser) parseColor() Value {
return Value{
color: Color{
r: p.parseHexPair(),
g: p.parseHexPair(),
b: p.parseHexPair(),
a: 255,
},
}
}
//Parse two hexadecimal digits
func (p *Parser) parseHexPair() uint {
s := p.input[p.pos : p.pos+2]
p.pos += 2
r, e := strconv.ParseUint(s, 16, 64)
if e != nil {
return 0
}
return uint(r)
}
//Parse a comma-separated list of selectors
func (p *Parser) parseSelectors() map[int]SimpleSelector {
s := map[int]SimpleSelector{}
Loopsels:
for {
s[len(s)] = p.parseSelector()
p.consumeWhitespace()
switch p.nextChar() {
case ",":
p.consumeChar()
p.consumeWhitespace()
case "{":
break Loopsels
default:
panic("Unexpected character")
}
}
return s
}
//Parse one simple selector, e.g.: '#id, class1, class2, class3'
func (p *Parser) parseSelector() SimpleSelector {
m := SimpleSelector{class: map[int]string{}}
Loopsel:
for !p.eof() {
c := p.nextChar()
switch {
case c == "#":
p.consumeChar()
m.id = p.parseIdentifier()
case c == ".":
p.consumeChar()
m.class[len(m.class)] = p.parseIdentifier()
case c == "*":
// universal selector
p.consumeChar()
case validIdentifierChar(c):
m.tag_name = p.parseIdentifier()
default:
break Loopsel
}
p.consumeWhitespace()
}
return m
}
func validIdentifierChar(c string) bool {
var valid = regexp.MustCompile("[a-zA-Z0-9-_]")
return valid.MatchString(c)
}
//Parse a property name or keyword
func (p *Parser) parseIdentifier() string {
var valid = regexp.MustCompile("[a-zA-Z0-9-_]")
return p.consumeWhile(func(char string) bool {
return valid.MatchString(char)
})
}