-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathcss.test.ts
231 lines (197 loc) · 7.01 KB
/
css.test.ts
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
import { parse, Rule, Media } from '../src/css';
import {
fixSafariColons,
escapeImportStatement,
stringifyRule,
} from './../src/utils';
describe('css parser', () => {
it('should save the filename and source', () => {
const css = 'booty {\n size: large;\n}\n';
const ast = parse(css, {
source: 'booty.css',
});
expect(ast.stylesheet!.source).toEqual('booty.css');
const position = ast.stylesheet!.rules[0].position!;
expect(position.start).toBeTruthy();
expect(position.end).toBeTruthy();
expect(position.source).toEqual('booty.css');
expect(position.content).toEqual(css);
});
it('should throw when a selector is missing', () => {
expect(() => {
parse('{size: large}');
}).toThrow();
expect(() => {
parse('b { color: red; }\n{ color: green; }\na { color: blue; }');
}).toThrow();
});
it('should throw when a broken comment is found', () => {
expect(() => {
parse('thing { color: red; } /* b { color: blue; }');
}).toThrow();
expect(() => {
parse('/*');
}).toThrow();
/* Nested comments should be fine */
expect(() => {
parse('/* /* */');
}).not.toThrow();
});
it('should allow empty property value', () => {
expect(() => {
parse('p { color:; }');
}).not.toThrow();
});
it('should not throw with silent option', () => {
expect(() => {
parse('thing { color: red; } /* b { color: blue; }', { silent: true });
}).not.toThrow();
});
it('should list the parsing errors and continue parsing', () => {
const result = parse(
'foo { color= red; } bar { color: blue; } baz {}} boo { display: none}',
{
silent: true,
source: 'foo.css',
},
);
const rules = result.stylesheet!.rules;
expect(rules.length).toBeGreaterThan(2);
const errors = result.stylesheet!.parsingErrors!;
expect(errors.length).toEqual(2);
expect(errors[0]).toHaveProperty('message');
expect(errors[0]).toHaveProperty('reason');
expect(errors[0]).toHaveProperty('filename');
expect(errors[0]).toHaveProperty('line');
expect(errors[0]).toHaveProperty('column');
expect(errors[0]).toHaveProperty('source');
expect(errors[0].filename).toEqual('foo.css');
});
it('should set parent property', () => {
const result = parse(
'thing { test: value; }\n' +
'@media (min-width: 100px) { thing { test: value; } }',
);
expect(result.parent).toEqual(null);
const rules = result.stylesheet!.rules;
expect(rules.length).toEqual(2);
let rule = rules[0] as Rule;
expect(rule.parent).toEqual(result);
expect(rule.declarations!.length).toEqual(1);
let decl = rule.declarations![0];
expect(decl.parent).toEqual(rule);
const media = rules[1] as Media;
expect(media.parent).toEqual(result);
expect(media.rules!.length).toEqual(1);
rule = media.rules![0] as Rule;
expect(rule.parent).toEqual(media);
expect(rule.declarations!.length).toEqual(1);
decl = rule.declarations![0];
expect(decl.parent).toEqual(rule);
});
it('parses : in attribute selectors correctly', () => {
const out1 = fixSafariColons('[data-foo] { color: red; }');
expect(out1).toEqual('[data-foo] { color: red; }');
const out2 = fixSafariColons('[data-foo:other] { color: red; }');
expect(out2).toEqual('[data-foo\\:other] { color: red; }');
const out3 = fixSafariColons('[data-aa\\:other] { color: red; }');
expect(out3).toEqual('[data-aa\\:other] { color: red; }');
});
it('does not alter correctly parsed grid template rules', () => {
const cssText =
'#wrapper { display: grid; width: 100%; height: 100%; grid-template: repeat(2, 1fr); margin: 0px auto; }';
const stringified = stringifyRule({
cssText: cssText,
} as Partial<CSSStyleRule> as CSSStyleRule);
expect(stringified).toEqual(cssText);
});
it('fixes incorrectly parsed grid template rules', () => {
const cssText =
'#wrapper { display: grid; grid-template: "header header" max-content / repeat(2, 1fr); margin: 0px auto; }';
// to avoid using JSDom we can fake as much of the CSSStyleDeclaration as we need
const cssStyleDeclaration: Record<string | number, any> = {
length: 3,
0: 'grid-template-areas',
1: 'grid-template-rows',
2: 'grid-template-columns',
items: (i: number): string => {
return [cssStyleDeclaration[i]].toString();
},
getPropertyValue: (key: string): string => {
if (key === 'grid-template-areas') {
return '"header header" "main main" "footer footer"';
}
if (key === 'grid-template-rows') {
return 'repeat(2, 1fr)';
}
if (key === 'grid-template-columns') {
return 'repeat(2, 1fr)';
}
return '';
},
};
const stringified = stringifyRule({
cssText: cssText,
selectorText: '#wrapper',
style: cssStyleDeclaration as unknown as CSSStyleDeclaration,
} as Partial<CSSStyleRule> as CSSStyleRule);
expect(stringified).toEqual(
'#wrapper { display: grid; margin: 0px auto; grid-template-areas: "header header" "main main" "footer footer"; grid-template-rows: repeat(2, 1fr); grid-template-columns: repeat(2, 1fr); }',
);
});
it('parses imports with quotes correctly', () => {
const out1 = escapeImportStatement({
cssText: `@import url("/foo.css;900;800"");`,
href: '/foo.css;900;800"',
media: {
length: 0,
},
layerName: null,
supportsText: null,
} as unknown as CSSImportRule);
expect(out1).toEqual(`@import url("/foo.css;900;800\\"");`);
const out2 = escapeImportStatement({
cssText: `@import url("/foo.css;900;800"") supports(display: flex);`,
href: '/foo.css;900;800"',
media: {
length: 0,
},
layerName: null,
supportsText: 'display: flex',
} as unknown as CSSImportRule);
expect(out2).toEqual(
`@import url("/foo.css;900;800\\"") supports(display: flex);`,
);
const out3 = escapeImportStatement({
cssText: `@import url("/foo.css;900;800"");`,
href: '/foo.css;900;800"',
media: {
length: 1,
mediaText: 'print, screen',
},
layerName: null,
supportsText: null,
} as unknown as CSSImportRule);
expect(out3).toEqual(`@import url("/foo.css;900;800\\"") print, screen;`);
const out4 = escapeImportStatement({
cssText: `@import url("/foo.css;900;800"") layer(layer-1);`,
href: '/foo.css;900;800"',
media: {
length: 0,
},
layerName: 'layer-1',
supportsText: null,
} as unknown as CSSImportRule);
expect(out4).toEqual(`@import url("/foo.css;900;800\\"") layer(layer-1);`);
const out5 = escapeImportStatement({
cssText: `@import url("/foo.css;900;800"") layer;`,
href: '/foo.css;900;800"',
media: {
length: 0,
},
layerName: '',
supportsText: null,
} as unknown as CSSImportRule);
expect(out5).toEqual(`@import url("/foo.css;900;800\\"") layer;`);
});
});