-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparser_test.go
129 lines (106 loc) · 4.27 KB
/
parser_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
package css
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestWithoutImpotant(t *testing.T) {
css := Parse(`div .a { font-size: 150%;}`)
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Value.Text(), "150%")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Property, "font-size")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Important, false)
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), "div .a")
}
func TestWithImpotant(t *testing.T) {
css := Parse("div .a { font-size: 150% !important;}")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Value.Text(), "150%")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Property, "font-size")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Important, true)
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), "div .a")
}
func TestMultipleDeclarations(t *testing.T) {
css := Parse(`div .a {
font-size: 150%;
width: 100%
}`)
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Value.Text(), "150%")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Property, "font-size")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Important, false)
assert.Equal(t, css.CssRuleList[0].Style.Styles[1].Value.Text(), "100%")
assert.Equal(t, css.CssRuleList[0].Style.Styles[1].Property, "width")
assert.Equal(t, css.CssRuleList[0].Style.Styles[1].Important, false)
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), "div .a")
}
func TestValuePx(t *testing.T) {
css := Parse("div .a { font-size: 45px;}")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Value.Text(), "45px")
}
func TestValueEm(t *testing.T) {
css := Parse("div .a { font-size: 45em;}")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Value.Text(), "45em")
}
func TestValueHex(t *testing.T) {
css := Parse("div .a { color: #123456;}")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Value.Text(), "#123456")
}
func TestValueRGBFunction(t *testing.T) {
css := Parse(".color{ color: rgb(1,2,3);}")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Value.Text(), "rgb(1,2,3)")
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), ".color")
}
func TestValueString(t *testing.T) {
css := Parse("div .center { text-align: center; }")
assert.Equal(t, css.CssRuleList[0].Style.Styles[0].Value.Text(), "center")
}
func TestValueWhiteSpace(t *testing.T) {
css := Parse(".div { padding: 10px 0 0 10px}")
assert.Equal(t, "10px 0 0 10px", css.CssRuleList[0].Style.Styles[0].Value.Text())
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), ".div")
}
func TestValueMixed(t *testing.T) {
css := Parse(`td {
padding: 0 12px 0 10px;
border-right: 1px solid white
}`)
assert.Equal(t, "0 12px 0 10px", css.CssRuleList[0].Style.Styles[0].Value.Text())
assert.Equal(t, "1px solid white", css.CssRuleList[0].Style.Styles[1].Value.Text())
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), "td")
}
func TestQuoteValue(t *testing.T) {
css := Parse(`blockquote {
font-family: "Source Sans Pro", Arial, sans-serif;
font-size: 27px;
line-height: 35px;}`)
assert.Equal(t, "\"Source Sans Pro\", Arial, sans-serif", css.CssRuleList[0].Style.Styles[0].Value.Text())
assert.Equal(t, "27px", css.CssRuleList[0].Style.Styles[1].Value.Text())
assert.Equal(t, "35px", css.CssRuleList[0].Style.Styles[2].Value.Text())
assert.Equal(t, css.CssRuleList[0].Style.Selector.Text(), "blockquote")
}
func TestDashClassname(t *testing.T) {
css := Parse(`.content {
padding: 0px;
}
.content-wrap {
padding: 2px;
}`)
assert.Equal(t, ".content", css.CssRuleList[0].Style.Selector.Text())
assert.Equal(t, ".content-wrap", css.CssRuleList[1].Style.Selector.Text())
assert.Equal(t, "0px", css.CssRuleList[0].Style.Styles[0].Value.Text())
assert.Equal(t, "2px", css.CssRuleList[1].Style.Styles[0].Value.Text())
}
func TestNotSupportedAtRule(t *testing.T) {
rules := []string{
`@namespace url(http://www.w3.org/1999/xhtml);`,
`@document url(http://www.w3.org/),
url-prefix(http://www.w3.org/Style/),
domain(mozilla.org),
regexp("https:.*")
{
body { color: purple; background: yellow; }
}`,
}
css := &CSSStyleSheet{}
css.CssRuleList = make([]*CSSRule, 0)
for _, rule := range rules {
assert.Equal(t, css, Parse(rule))
}
}