-
Notifications
You must be signed in to change notification settings - Fork 264
/
Copy pathparse.js
108 lines (84 loc) · 2.88 KB
/
parse.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
var parse = require('../').parse;
var should = require('should');
describe('parse(str)', function() {
it('should save the filename and source', function() {
var css = 'booty {\n size: large;\n}\n';
var ast = parse(css, {
source: 'booty.css'
});
ast.stylesheet.source.should.equal('booty.css');
var position = ast.stylesheet.rules[0].position;
position.start.should.be.ok;
position.end.should.be.ok;
position.source.should.equal('booty.css');
position.content.should.equal(css);
});
it('should throw when a selector is missing', function() {
should(function() {
parse('{size: large}');
}).throw();
should(function() {
parse('b { color: red; }\n{ color: green; }\na { color: blue; }');
}).throw();
});
it('should throw when a broken comment is found', function () {
should(function() {
parse('thing { color: red; } /* b { color: blue; }');
}).throw();
should(function() {
parse('/*');
}).throw();
/* Nested comments should be fine */
should(function() {
parse('/* /* */');
}).not.throw();
});
it('should allow empty property value', function() {
should(function() {
parse('p { color:; }');
}).not.throw();
});
it('should not throw with silent option', function () {
should(function() {
parse('thing { color: red; } /* b { color: blue; }', { silent: true });
}).not.throw();
});
it('should list the parsing errors and continue parsing', function() {
var result = parse('foo { color= red; } bar { color: blue; } baz {}} boo { display: none}', {
silent: true,
source: 'foo.css'
});
var rules = result.stylesheet.rules;
rules.length.should.be.above(2);
var errors = result.stylesheet.parsingErrors;
errors.length.should.equal(2);
errors[0].should.have.a.property('message');
errors[0].should.have.a.property('reason');
errors[0].should.have.a.property('filename');
errors[0].filename.should.equal('foo.css');
errors[0].should.have.a.property('line');
errors[0].should.have.a.property('column');
errors[0].should.have.a.property('source');
});
it('should set parent property', function() {
var result = parse(
'thing { test: value; }\n' +
'@media (min-width: 100px) { thing { test: value; } }');
should(result.parent).equal(null);
var rules = result.stylesheet.rules;
rules.length.should.equal(2);
var rule = rules[0];
rule.parent.should.equal(result);
rule.declarations.length.should.equal(1);
var decl = rule.declarations[0];
decl.parent.should.equal(rule);
var media = rules[1];
media.parent.should.equal(result);
media.rules.length.should.equal(1);
rule = media.rules[0];
rule.parent.should.equal(media);
rule.declarations.length.should.equal(1);
decl = rule.declarations[0];
decl.parent.should.equal(rule);
});
});