-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss_test.go
More file actions
244 lines (224 loc) · 6.64 KB
/
Copy pathcss_test.go
File metadata and controls
244 lines (224 loc) · 6.64 KB
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
package csshtml
import (
"strings"
"testing"
)
func TestNestedAtrule(t *testing.T) {
str := `
@page {
size: a5;
@bottom-right-corner {
border: 4pt solid green;
border-bottom-color: rebeccapurple;
}
/* @top-left-corner {
border: 1pt solid green;
border-bottom-color: rebeccapurple;
} */
@top-right-corner {
border: 3pt solid green;
border-bottom-color: rebeccapurple;
}
@bottom-left-corner {
border: 2pt solid green;
border-bottom-color: rebeccapurple;
}
}`
toks := tokenizeCSSString(str)
bl := consumeBlock(toks, false)
if len(bl.childAtRules[0].childAtRules) != 3 {
t.Errorf("want 3 child @ rules, got %d", len(bl.childAtRules[0].childAtRules))
}
}
func TestFontFace(t *testing.T) {
str := `@font-face {
font-family: "Trickster";
src:
local("Trickster"),
url("trickster-COLRv1.otf") format("opentype") tech(color-COLRv1),
url("trickster-outline.otf") format("opentype"),
url("trickster-outline.woff") format("woff");
}`
cp := NewCSSParser()
err := cp.AddCSSText(str)
if err != nil {
t.Error(err)
}
fontfaces := cp.FontFaces
if got, want := len(fontfaces), 1; got != want {
t.Errorf("len(c.FontFaces) = %d, want %d", got, want)
}
firstFontFace := fontfaces[0]
if want, got := 4, len(firstFontFace.Source); got != want {
t.Errorf("len(c.FontFaces[0].Source) = %d, want %d", got, want)
}
if want, got := "color-COLRv1", firstFontFace.Source[1].Tech; got != want {
t.Errorf(`firstFontFace.Source[1].Tech = %s, want %s`, got, want)
}
}
func TestConsumeBlock_SimpleRules(t *testing.T) {
css := `p { color: red; font-size: 12pt; }`
toks := tokenizeCSSString(css)
bl := consumeBlock(toks, false)
if len(bl.blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(bl.blocks))
}
if len(bl.blocks[0].rules) != 2 {
t.Errorf("got %d rules, want 2", len(bl.blocks[0].rules))
}
}
func TestConsumeBlock_MultipleSelectors(t *testing.T) {
css := `h1 { color: blue; } p { color: red; }`
toks := tokenizeCSSString(css)
bl := consumeBlock(toks, false)
if len(bl.blocks) != 2 {
t.Fatalf("got %d blocks, want 2", len(bl.blocks))
}
}
func TestConsumeBlock_ClassSelector(t *testing.T) {
css := `.highlight { background: yellow; }`
toks := tokenizeCSSString(css)
bl := consumeBlock(toks, false)
if len(bl.blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(bl.blocks))
}
sel := selectorString(bl.blocks[0].componentValues)
if !strings.Contains(sel, ".highlight") {
t.Errorf("selector = %q, want to contain '.highlight'", sel)
}
}
func TestConsumeBlock_IDSelector(t *testing.T) {
css := `#main { width: 100%; }`
toks := tokenizeCSSString(css)
bl := consumeBlock(toks, false)
if len(bl.blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(bl.blocks))
}
sel := selectorString(bl.blocks[0].componentValues)
if !strings.Contains(sel, "#main") {
t.Errorf("selector = %q, want to contain '#main'", sel)
}
}
func TestConsumeBlock_DescendantSelector(t *testing.T) {
css := `div p { margin: 0; }`
toks := tokenizeCSSString(css)
bl := consumeBlock(toks, false)
if len(bl.blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(bl.blocks))
}
sel := strings.Join(strings.Fields(selectorString(bl.blocks[0].componentValues)), " ")
if sel != "div p" {
t.Errorf("selector = %q, want 'div p'", sel)
}
}
func TestConsumeBlock_ChildCombinator(t *testing.T) {
css := `ul > li { list-style: none; }`
toks := tokenizeCSSString(css)
bl := consumeBlock(toks, false)
if len(bl.blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(bl.blocks))
}
sel := strings.Join(strings.Fields(selectorString(bl.blocks[0].componentValues)), " ")
if sel != "ul > li" {
t.Errorf("selector = %q, want 'ul > li'", sel)
}
}
func TestConsumeBlock_RuleWithoutTrailingSemicolon(t *testing.T) {
css := `p { color: red }`
toks := tokenizeCSSString(css)
bl := consumeBlock(toks, false)
if len(bl.blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(bl.blocks))
}
if len(bl.blocks[0].rules) != 1 {
t.Errorf("got %d rules, want 1", len(bl.blocks[0].rules))
}
}
func TestConsumeBlock_EmptyBlock(t *testing.T) {
css := `p { }`
toks := tokenizeCSSString(css)
bl := consumeBlock(toks, false)
if len(bl.blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(bl.blocks))
}
if len(bl.blocks[0].rules) != 0 {
t.Errorf("got %d rules, want 0", len(bl.blocks[0].rules))
}
}
func TestSelectorString_IDNotDoubleHash(t *testing.T) {
css := `#important { color: red; }`
toks := tokenizeCSSString(css)
bl := consumeBlock(toks, false)
if len(bl.blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(bl.blocks))
}
sel := selectorString(bl.blocks[0].componentValues)
if sel != "#important" {
t.Errorf("selector = %q, want %q", sel, "#important")
}
}
func TestApplyCSS_IDSelector(t *testing.T) {
htmlStr := `<html><head></head><body><p id="important">text</p></body></html>`
css := `#important { color: green; }`
c := NewCSSParser()
if err := c.AddCSSText(css); err != nil {
t.Fatal(err)
}
doc, err := c.ProcessHTMLChunk(htmlStr)
if err != nil {
t.Fatal(err)
}
_, err = c.ApplyCSS(doc)
if err != nil {
t.Fatalf("ApplyCSS failed: %v", err)
}
p := doc.Find("#important")
if val, exists := p.Attr("!color"); !exists || val != "green" {
t.Errorf("#important !color = %q (exists=%v), want 'green'", val, exists)
}
}
func TestConsumeBlock_PseudoClass(t *testing.T) {
css := `a:hover { color: green; }`
toks := tokenizeCSSString(css)
bl := consumeBlock(toks, false)
if len(bl.blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(bl.blocks))
}
sel := selectorString(bl.blocks[0].componentValues)
if !strings.Contains(sel, ":hover") {
t.Errorf("selector = %q, want to contain ':hover'", sel)
}
}
// TestPageMarginLonghands: the margin longhands are valid page-context
// properties (CSS Paged Media 3 §7.2) and must reach the Page margin fields
// just like the margin shorthand. A longhand following the shorthand in the
// same rule overrides that side.
func TestPageMarginLonghands(t *testing.T) {
str := `
@page {
size: a4;
margin: 2cm;
margin-right: 6cm;
}
@page :first {
margin-top: 8cm;
}`
cp := NewCSSParser()
if err := cp.AddCSSText(str); err != nil {
t.Fatal(err)
}
base := cp.Pages[""]
if got, want := base.MarginTop, "2cm"; got != want {
t.Errorf("base MarginTop = %q, want %q", got, want)
}
if got, want := base.MarginRight, "6cm"; got != want {
t.Errorf("base MarginRight = %q, want %q (longhand must override the shorthand)", got, want)
}
first := cp.Pages[":first"]
if got, want := first.MarginTop, "8cm"; got != want {
t.Errorf(":first MarginTop = %q, want %q", got, want)
}
if got := first.MarginLeft; got != "" {
t.Errorf(":first MarginLeft = %q, want empty (inherited later via mergePageWithBase)", got)
}
}