forked from Surachai-kent/css-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.test.ts
More file actions
114 lines (91 loc) · 3.14 KB
/
parse.test.ts
File metadata and controls
114 lines (91 loc) · 3.14 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
import CssParseError from '../src/CssParseError';
import {parse} from '../src/index';
import {CssMediaAST, CssRuleAST} from '../src/type';
describe('parse(str)', () => {
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).toBe('booty.css');
const position = ast.stylesheet.rules[0].position;
expect(position?.start).toBeDefined();
expect(position?.end).toBeDefined();
expect(position?.source).toBe('booty.css');
// expect(position.content).toBe(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).toBeDefined();
expect(errors?.length).toBe(2);
const firstError = (errors as unknown as Array<CssParseError>)[0];
expect(firstError).toHaveProperty('message');
expect(firstError).toHaveProperty('reason');
expect(firstError).toHaveProperty('filename');
expect(firstError.filename).toBe('foo.css');
expect(firstError).toHaveProperty('line');
expect(firstError).toHaveProperty('column');
expect(firstError).toHaveProperty('source');
});
it('should set parent property', () => {
const result = parse(
'thing { test: value; }\n' +
'@media (min-width: 100px) { thing { test: value; } }'
);
// expect(result).not.toHaveProperty('parent');
const rules = result.stylesheet.rules;
expect(rules.length).toBe(2);
let rule = rules[0] as CssRuleAST;
expect(rule.parent).toBe(result);
expect(rule.declarations.length).toBe(1);
let decl = rule.declarations[0];
expect(decl.parent).toBe(rule);
const media = rules[1] as CssMediaAST;
expect(media.parent).toBe(result);
expect(media.rules.length).toBe(1);
rule = media.rules[0] as CssRuleAST;
expect(rule.parent).toBe(media);
expect(rule.declarations.length).toBe(1);
decl = rule.declarations[0];
expect(decl.parent).toBe(rule);
});
});