-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
astish.test.js
219 lines (207 loc) · 5.71 KB
/
astish.test.js
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
import { astish } from '../astish';
describe('astish', () => {
it('regular', () => {
expect(
astish(`
prop: value;
`)
).toEqual({
prop: 'value'
});
});
it('nested', () => {
expect(
astish(`
prop: value;
@keyframes foo {
0% {
attr: value;
}
50% {
opacity: 1;
}
100% {
foo: baz;
}
}
named {
background-image: url('/path-to-jpg.png');
}
opacity: 0;
.class,
&:hover {
-webkit-touch: none;
}
`)
).toEqual({
prop: 'value',
opacity: '0',
'.class, &:hover': {
'-webkit-touch': 'none'
},
'@keyframes foo': {
'0%': {
attr: 'value'
},
'50%': {
opacity: '1'
},
'100%': {
foo: 'baz'
}
},
named: {
'background-image': "url('/path-to-jpg.png')"
}
});
});
it('merging', () => {
expect(
astish(`
.c {
font-size:24px;
}
.c {
color:red;
}
`)
).toEqual({
'.c': {
'font-size': '24px',
color: 'red'
}
});
});
it('regression', () => {
expect(
astish(`
&.g0ssss {
aa: foo;
box-shadow: 0 1px rgba(0, 2, 33, 4) inset;
}
named {
transform: scale(1.2), rotate(1, 1);
}
@media screen and (some-rule: 100px) {
foo: baz;
opacity: 1;
level {
one: 1;
level {
two: 2;
}
}
}
.a{
color: red;
}
.b {
color: blue;
}
`)
).toEqual({
'&.g0ssss': {
aa: 'foo',
'box-shadow': '0 1px rgba(0, 2, 33, 4) inset'
},
'.a': {
color: 'red'
},
'.b': {
color: 'blue'
},
named: {
transform: 'scale(1.2), rotate(1, 1)'
},
'@media screen and (some-rule: 100px)': {
foo: 'baz',
opacity: '1',
level: {
one: '1',
level: {
two: '2'
}
}
}
});
});
it('should strip comments', () => {
expect(
astish(`
color: red;
/*
some comment
*/
transform: translate3d(0, 0, 0);
/**
* other comment
*/
background: peachpuff;
font-size: xx-large; /* inline comment */
/* foo: bar */
font-weight: bold;
`)
).toEqual({
color: 'red',
transform: 'translate3d(0, 0, 0)',
background: 'peachpuff',
'font-size': 'xx-large',
'font-weight': 'bold'
});
});
// for reference on what is valid:
// https://www.w3.org/TR/CSS22/syndata.html#value-def-identifier
it('should not mangle valid css identifiers', () => {
expect(
astish(`
:root {
--azAZ-_中文09: 0;
}
`)
).toEqual({
':root': {
'--azAZ-_中文09': '0'
}
});
});
it('should parse multiline background declaration', () => {
expect(
astish(`
background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" fill="white"><path d="M7.5 36.7h58.4v10.6H7.5V36.7zm0-15.9h58.4v10.6H7.5V20.8zm0 31.9h58.4v10.6H7.5V52.7zm0 15.9h58.4v10.6H7.5V68.6zm63.8-15.9l10.6 15.9 10.6-15.9H71.3zm21.2-5.4L81.9 31.4 71.3 47.3h21.2z"/></svg>')
center/contain;
`)
).toEqual({
background: `url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" fill="white"><path d="M7.5 36.7h58.4v10.6H7.5V36.7zm0-15.9h58.4v10.6H7.5V20.8zm0 31.9h58.4v10.6H7.5V52.7zm0 15.9h58.4v10.6H7.5V68.6zm63.8-15.9l10.6 15.9 10.6-15.9H71.3zm21.2-5.4L81.9 31.4 71.3 47.3h21.2z"/></svg>') center/contain`
});
});
it('should handle inline @media block', () => {
expect(
astish(
`h1 { font-size: 1rem; } @media only screen and (min-width: 850px) { h1 { font-size: 2rem; } }`
)
).toEqual({
h1: {
'font-size': '1rem'
},
'@media only screen and (min-width: 850px)': {
h1: {
'font-size': '2rem'
}
}
});
});
it('should handle newlines as part of the rule value', () => {
expect(
astish(
`tag {
font-size: first
second;
}`
)
).toEqual({
tag: {
'font-size': 'first second'
}
});
});
});