-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparser_selector_test.go
190 lines (160 loc) · 5.47 KB
/
parser_selector_test.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
package css
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMultipleSelectors(t *testing.T) {
css := Parse(`div .a {
font-size: 150%;
}
p .b {
font-size: 250%;
}`)
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), "div .a")
assert.Equal(t, css.CssRuleList[1].Style.Selector.Text(), "p .b")
}
func TestIdSelector(t *testing.T) {
css := Parse("#div { color: red;}")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Value.Text(), "red")
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), "#div")
}
func TestClassSelector(t *testing.T) {
css := Parse(".div { color: green;}")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Value.Text(), "green")
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), ".div")
}
func TestStarSelector(t *testing.T) {
css := Parse("* { text-rendering: optimizelegibility; }")
assert.Equal(t, "optimizelegibility", css.CssRuleList[0].Style.Styles[0].Value.Text())
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), "*")
}
func TestStarSelectorMulti(t *testing.T) {
css := Parse(`div .a {
font-size: 150%;
}
* { text-rendering: optimizelegibility; }`)
assert.Equal(t, "150%", css.CssRuleList[0].Style.Styles[0].Value.Text())
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), "div .a")
assert.Equal(t, "optimizelegibility", css.CssRuleList[1].Style.Styles[0].Value.Text())
assert.Equal(t, css.CssRuleList[1].Style.Selector.Text(), "*")
}
func TestMixedClassSelectors(t *testing.T) {
selectors := []string{".footer__content_wrapper--last",
"table[class=\"body\"] .footer__content td",
"table[class=\"body\"] td.footer__link_wrapper--first",
"table[class=\"body\"] td.footer__link_wrapper--last"}
for _, selector := range selectors {
css := Parse(fmt.Sprintf(` %s {
border-collapse: separate;
padding: 10px 0 0
}`, selector))
assert.Equal(t, "separate", css.CssRuleList[0].Style.Styles[0].Value.Text())
assert.Equal(t, "10px 0 0", css.CssRuleList[0].Style.Styles[1].Value.Text())
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), selector)
}
}
func TestGenericSelectors(t *testing.T) {
selectors := []string{
"p ~ ul",
"div > p",
"div > p",
"div p",
"div, p",
"[target]",
"[target=_blank]",
"[title~=flower]",
"[lang|=en]",
"a[href^=\"https\"]",
"a[href$=\".pdf\"]",
"a[href*=\"css\"]",
".header + .content",
"#firstname",
"table[class=\"body\"] .footer__content td",
"table[class=\"body\"] td.footer__link_wrapper--first"}
for _, selector := range selectors {
css := Parse(fmt.Sprintf(` %s {
border-collapse: separate;
padding: 10px 0 0
}`, selector))
assert.Equal(t, "separate", css.CssRuleList[0].Style.Styles[0].Value.Text())
assert.Equal(t, "10px 0 0", css.CssRuleList[0].Style.Styles[1].Value.Text())
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), selector)
}
}
func TestFilterSelectors(t *testing.T) {
selectors := []string{
"a:active",
"p::after",
"p::before",
"input:checked",
"input:disabled",
"input:in-range",
"input:invalid",
"input:optional",
"input:read-only",
"input:enabled",
"p:empty",
"p:first-child",
"p::first-letter",
"p::first-line",
"p:first-of-type",
"input:focus",
"a:hover",
"p:lang(it)",
"p:last-child",
"p:last-of-type",
"a:link",
":not(p)",
"p:nth-child(2)",
"p:nth-last-child(2)",
"p:only-of-type",
"p:only-child",
"p:nth-last-of-type(2)",
"div:not(:nth-child(1))",
"div:not(:not(:first-child))",
":root",
"::selection",
"#news:target"}
for _, selector := range selectors {
css := Parse(fmt.Sprintf(` %s {
border-collapse: separate;
padding: 10px 0 0
}`, selector))
assert.Equal(t, "separate", css.CssRuleList[0].Style.Styles[0].Value.Text())
assert.Equal(t, "10px 0 0", css.CssRuleList[0].Style.Styles[1].Value.Text())
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), selector)
}
}
func TestFontFace(t *testing.T) {
css := Parse(`@font-face {
font-family: "Bitstream Vera Serif Bold";
src: url("https://mdn.mozillademos.org/files/2468/VeraSeBd.ttf");
}
body { font-family: "Bitstream Vera Serif Bold", serif }`)
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), "")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Value.Text(), "\"Bitstream Vera Serif Bold\"")
assert.Equal(t, css.CssRuleList[0].Style.Styles[1].Value.Text(), "url(\"https://mdn.mozillademos.org/files/2468/VeraSeBd.ttf\")")
assert.Equal(t, css.CssRuleList[1].Style.Styles[0].Value.Text(), "\"Bitstream Vera Serif Bold\", serif")
assert.Equal(t, css.CssRuleList[0].Type, FONT_FACE_RULE)
}
func TestPage(t *testing.T) {
css := Parse(`@page :first {
margin: 2in 3in;
}`)
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), ":first")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Value.Text(), "2in 3in")
assert.Equal(t, css.CssRuleList[0].Type, PAGE_RULE)
}
func TestCounterStyle(t *testing.T) {
css := Parse(`@counter-style winners-list {
system: cyclic;
symbols: "\1F44D";
suffix: " ";
}`)
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), "winners-list")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Value.Text(), "cyclic")
assert.Equal(t, css.CssRuleList[0].Style.Styles[1].Value.Text(), "\"\\1F44D\"")
assert.Equal(t, css.CssRuleList[0].Style.Styles[2].Value.Text(), "\" \"")
assert.Equal(t, css.CssRuleList[0].Type, COUNTER_STYLE_RULE)
}