This repository has been archived by the owner on Jul 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
css.go
205 lines (175 loc) · 3.63 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
package gs
import (
"io"
"strings"
)
// RuleList is a list of css style rules.
type RuleList []CSSRule
func (RuleList) isRule() {}
func (r RuleList) String() string {
s := ""
for _, v := range r {
s += v.String()
}
return s
}
// filter out nil values
func toRuleList(src []CSSRule) RuleList {
var ls RuleList
for _, v := range src {
if v != nil {
ls = append(ls, v)
}
}
return ls
}
// CSS defines a list of style rules. Nil values will be removed.
func CSS(rules ...CSSRule) CSSRule {
return toRuleList(rules)
}
type SimpleRule struct {
Key string
Value string
}
func (SimpleRule) isRule() {}
func (s SimpleRule) String() string {
return s.Key + ":" + s.Value + ";"
}
func (s SimpleRule) Print(o io.Writer) (int64, error) {
v, err := o.Write([]byte(s.Key + ":" + s.Value + ";"))
return int64(v), err
}
func P(key, value string) CSSRule {
return SimpleRule{Key: key, Value: value}
}
func If(cond bool, c CSSRule) CSSRule {
if cond {
return c
}
return nil
}
type StyleRule struct {
Selector string
Rules RuleList
}
func (s StyleRule) String() string {
if len(s.Rules) == 0 {
return ""
}
o := ""
b := ""
for _, v := range s.Rules {
switch v.(type) {
case SimpleRule:
b += indent(v.String(), 2)
default:
o += v.String()
}
}
if b != "" {
return s.Selector + " {\n" + b + "}\n" + o
}
return o
}
func (StyleRule) isRule() {}
func S(selector string, rules ...CSSRule) CSSRule {
return StyleRule{Selector: selector, Rules: toRuleList(rules)}
}
type Conditional struct {
Key string
Rules RuleList
}
func (c Conditional) String() string {
if len(c.Rules) == 0 {
return ""
}
b := ""
for _, v := range c.Rules {
b += v.String()
}
return c.Key + " {\n" + indent(b, 2) + "}\n"
}
func (Conditional) isRule() {}
func indent(s string, by int) string {
s = strings.TrimSpace(s)
p := strings.Split(s, "\n")
o := ""
idx := ""
for i := 0; i < by; i++ {
idx += " "
}
for _, v := range p {
o += idx + v + "\n"
}
return o
}
func FontFace(rules ...CSSRule) CSSRule {
return S("@font-face", rules...)
}
func Cond(cond string, rules ...CSSRule) CSSRule {
return Conditional{Key: cond, Rules: RuleList(rules)}
}
func KeyFrame(name string, rules ...CSSRule) CSSRule {
return Cond("@keyframes "+name, rules...)
}
type CSSRule interface {
//we don't want users to implement this.
String() string
isRule()
}
type Transformer func(CSSRule) CSSRule
func Flattern(rule CSSRule) CSSRule {
return flattern("", rule)
}
func flattern(parent string, rule CSSRule) RuleList {
var o RuleList
switch e := rule.(type) {
case RuleList:
for _, v := range e {
o = append(o, flattern(parent, v)...)
}
case StyleRule:
sel := e.Selector
if parent != "" {
sel = replaceParent(parent, sel)
}
o = append(o, StyleRule{
Selector: sel,
Rules: flattern(sel, e.Rules),
})
case Conditional:
key := e.Key
if parent != "" {
key = replaceParent(parent, key)
}
o = append(o, Conditional{
Key: key,
Rules: flattern(key, e.Rules),
})
default:
o = append(o, e)
}
return o
}
func replaceParent(parent, selector string) string {
if strings.Contains(selector, "&,") {
return strings.Replace(selector, "&,", parent+",\n", -1)
}
return strings.Replace(selector, "&", parent, -1)
}
func ToString(rule CSSRule, ts ...Transformer) string {
rule = Process(rule, ts...)
return strings.TrimSpace(rule.String())
}
type Options struct {
NoPretty bool
}
// Process this applies any transformation to the rule. It automatically
// flatterns the rule tree.
func Process(rule CSSRule, ts ...Transformer) CSSRule {
ts = append(ts, Flattern)
for _, v := range ts {
rule = v(rule)
}
return rule
}