-
Notifications
You must be signed in to change notification settings - Fork 11
/
css.go
159 lines (125 loc) · 2.97 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
package css
import (
"fmt"
"strings"
"qlova.org/seed"
"qlova.org/seed/client"
)
func init() {
client.ClickCursor = SetCursor(Pointer)
}
type data struct {
selector string
rules *OrderedMap
prefix, suffix *OrderedMap
queries map[string]Rules
sheets map[string]struct{}
requires map[string]string
}
type ruleable interface {
Rule() Rule
}
type Style interface {
Rules() Rules
}
//Rule is a single css Rule.
type Rule string
func (r Rule) Rules() Rules {
return Rules{r}
}
func (r Rule) Split() (property, value string) {
split := strings.Split(string(r), ":")
return split[0], split[1][:len(split[1])-1]
}
func (r Rule) Property() string {
return strings.Split(string(r), ":")[0]
}
func (r Rule) Value() string {
value := strings.Split(string(r), ":")[1]
return value[:len(value)-1]
}
func (r Rule) AddTo(c seed.Seed) {
var d data
c.Load(&d)
switch mode, q := client.Seed(c); mode {
case client.AddTo:
property, value := r.Split()
if strings.HasPrefix(property, "-") {
fmt.Fprintf(q, `%v.style.setProperty("%v", "%v");`, client.Element(c), property, value)
} else {
fmt.Fprintf(q, `%v.style.%v = "%v";`, client.Element(c), dashes2camels(property), value)
}
case client.Undo:
property := r.Property()
if strings.HasPrefix(property, "-") {
fmt.Fprintf(q, `%v.style.setProperty("%v", "");`, client.Element(c), property)
} else {
fmt.Fprintf(q, `%v.style.%v = "";`, client.Element(c), dashes2camels(property))
}
default:
if d.rules == nil {
d.rules = NewOrderedMap()
}
property, value := r.Split()
d.rules.Set(property, value)
}
c.Save(d)
}
func (r Rule) And(options ...seed.Option) seed.Option {
return seed.And(r, options...)
}
//Important returns an important version of this rule.
func (r Rule) Important() Rule {
return r[:len(r)-1] + " !important;"
}
//Selector returns the css selector of this seed.
func Selector(c seed.Seed) string {
c.Use()
var d data
c.Load(&d)
if d.selector != "" {
return d.selector
}
return "#" + client.ID(c)
}
func dashes2camels(s string) string {
var camel string
var parts = strings.Split(s, "-")
for i, part := range parts {
if i == 0 {
camel += part
} else {
camel += strings.Title(part)
}
}
return camel
}
//SetSelector sets the CSS selector of this seed.
func SetSelector(selector string) seed.Option {
return seed.NewOption(func(c seed.Seed) {
var d data
c.Load(&d)
d.selector = selector
c.Save(d)
})
}
//Set sets the CSS property to be set to the given value.
func Set(property, value string) Rule {
return Rule(property + ":" + value + ";")
}
//Add adds a stylesheet associated with this seed.
func Add(stylesheet string) seed.Option {
return seed.NewOption(func(c seed.Seed) {
switch mode, _ := client.Seed(c); mode {
case client.AddTo, client.Undo:
panic("css.Add must not be called on a client.Seed")
}
var d data
c.Load(&d)
if d.sheets == nil {
d.sheets = make(map[string]struct{})
c.Save(d)
}
d.sheets[stylesheet] = struct{}{}
})
}