-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnesting_test.go
More file actions
227 lines (205 loc) · 5.42 KB
/
nesting_test.go
File metadata and controls
227 lines (205 loc) · 5.42 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
package csshtml
import (
"strings"
"testing"
"github.com/PuerkitoBio/goquery"
)
// TestFlattenNesting_AmpersandBeginning tests & at the start of a nested selector.
func TestFlattenNesting_AmpersandBeginning(t *testing.T) {
css := `.card {
color: blue;
& > h2 {
color: red;
}
}`
blocks := parseAndFlatten(t, css)
if len(blocks) != 2 {
t.Fatalf("got %d blocks, want 2", len(blocks))
}
assertSelector(t, blocks[0], ".card")
assertRule(t, blocks[0], "color", "blue")
assertSelector(t, blocks[1], ".card > h2")
assertRule(t, blocks[1], "color", "red")
}
// TestFlattenNesting_AmpersandEnd tests & at the end of a nested selector.
func TestFlattenNesting_AmpersandEnd(t *testing.T) {
css := `h2 {
color: blue;
.card & {
color: red;
}
}`
blocks := parseAndFlatten(t, css)
if len(blocks) != 2 {
t.Fatalf("got %d blocks, want 2", len(blocks))
}
assertSelector(t, blocks[0], "h2")
assertSelector(t, blocks[1], ".card h2")
}
// TestFlattenNesting_AmpersandMiddle tests & in the middle of a nested selector.
func TestFlattenNesting_AmpersandMiddle(t *testing.T) {
css := `.a {
.b & .c {
color: green;
}
}`
blocks := parseAndFlatten(t, css)
if len(blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(blocks))
}
assertSelector(t, blocks[0], ".b .a .c")
}
// TestFlattenNesting_NoAmpersand tests implicit descendant combinator when & is absent.
func TestFlattenNesting_NoAmpersand(t *testing.T) {
css := `.card {
h2 {
font-size: 2em;
}
}`
blocks := parseAndFlatten(t, css)
if len(blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(blocks))
}
assertSelector(t, blocks[0], ".card h2")
assertRule(t, blocks[0], "font-size", "2em")
}
// TestFlattenNesting_AmpersandSuffix tests &.class (no space, class appended to parent).
func TestFlattenNesting_AmpersandSuffix(t *testing.T) {
css := `.card {
&.active {
background: yellow;
}
}`
blocks := parseAndFlatten(t, css)
if len(blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(blocks))
}
assertSelector(t, blocks[0], ".card.active")
}
// TestFlattenNesting_DeepNesting tests multiple levels of nesting.
func TestFlattenNesting_DeepNesting(t *testing.T) {
css := `.a {
.b {
.c {
color: red;
}
}
}`
blocks := parseAndFlatten(t, css)
if len(blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(blocks))
}
assertSelector(t, blocks[0], ".a .b .c")
}
// TestFlattenNesting_MixedRulesAndNesting tests a block with both rules and nested blocks.
func TestFlattenNesting_MixedRulesAndNesting(t *testing.T) {
css := `.card {
padding: 1em;
border: 1pt solid black;
& > .title {
font-weight: bold;
}
& > .body {
font-size: 0.9em;
}
}`
blocks := parseAndFlatten(t, css)
if len(blocks) != 3 {
t.Fatalf("got %d blocks, want 3", len(blocks))
}
assertSelector(t, blocks[0], ".card")
assertRule(t, blocks[0], "padding", "1em")
assertSelector(t, blocks[1], ".card > .title")
assertRule(t, blocks[1], "font-weight", "bold")
assertSelector(t, blocks[2], ".card > .body")
assertRule(t, blocks[2], "font-size", "0.9em")
}
// TestFlattenNesting_SiblingCombinator tests + combinator with nesting.
func TestFlattenNesting_SiblingCombinator(t *testing.T) {
css := `.a {
& + .b {
margin-top: 0;
}
}`
blocks := parseAndFlatten(t, css)
if len(blocks) != 1 {
t.Fatalf("got %d blocks, want 1", len(blocks))
}
assertSelector(t, blocks[0], ".a + .b")
}
// TestFlattenNesting_Integration tests that nested CSS actually applies to HTML elements.
func TestFlattenNesting_Integration(t *testing.T) {
htmlStr := `<html><head></head><body>
<div class="card">
<h2>Title</h2>
<p>Body</p>
</div>
</body></html>`
css := `.card {
color: blue;
& > h2 {
color: red;
}
& > p {
font-size: 10pt;
}
}`
c := NewCSSParser()
if err := c.AddCSSText(css); err != nil {
t.Fatal(err)
}
doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlStr))
if err != nil {
t.Fatal(err)
}
_, err = c.ApplyCSS(doc)
if err != nil {
t.Fatal(err)
}
// Check that .card got color: blue
card := doc.Find(".card")
if val, exists := card.Attr("!color"); !exists || val != "blue" {
t.Errorf(".card !color = %q (exists=%v), want 'blue'", val, exists)
}
// Check that h2 got color: red
h2 := doc.Find(".card > h2")
if val, exists := h2.Attr("!color"); !exists || val != "red" {
t.Errorf(".card > h2 !color = %q (exists=%v), want 'red'", val, exists)
}
// Check that p got font-size: 10pt
p := doc.Find(".card > p")
if val, exists := p.Attr("!font-size"); !exists || val != "10pt" {
t.Errorf(".card > p !font-size = %q (exists=%v), want '10pt'", val, exists)
}
}
// --- helpers ---
func parseAndFlatten(t *testing.T, css string) []*sBlock {
t.Helper()
toks := tokenizeCSSString(css)
block := consumeBlock(toks, false)
return flattenNestedBlocks(block.blocks)
}
func assertSelector(t *testing.T, block *sBlock, want string) {
t.Helper()
got := selectorString(block.componentValues)
// Normalize whitespace for comparison
got = strings.Join(strings.Fields(got), " ")
want = strings.Join(strings.Fields(want), " ")
if got != want {
t.Errorf("selector = %q, want %q", got, want)
}
}
func assertRule(t *testing.T, block *sBlock, key, wantVal string) {
t.Helper()
for _, r := range block.rules {
k := strings.TrimSpace(r.key.String())
if k == key {
v := strings.TrimSpace(stringValue(r.value))
if v != wantVal {
t.Errorf("rule %s = %q, want %q", key, v, wantVal)
}
return
}
}
t.Errorf("rule %s not found", key)
}