forked from voidlabs/mosaico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmensch-spec.js
114 lines (102 loc) · 3.32 KB
/
mensch-spec.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
'use strict';
/* globals describe: false, it: false, expect: false */
var mockery = require('mockery');
mockery.enable();
mockery.registerAllowables(['../src/js/converter/declarations.js', 'console', './utils.js', './domutils.js', 'console', '../node_modules/mensch']);
var currentDocument;
mockery.registerMock('jquery', function() {
return currentDocument.apply(currentDocument, arguments);
});
mockery.registerMock('mensch/lib/parser.js', function() {
var parse = require('../node_modules/mensch').parse;
return parse.apply(parse, arguments);
});
var utils = require('../src/js/converter/utils.js');
describe('Mensch parser', function() {
it('should return expected positions', function() {
var styleText = " \nselector \n{\n color: red\n;\t}\n selector2{a:b}";
var styleSheet = require('mensch/lib/parser.js')(styleText, {
comments: true,
position: true
});
var declarations = styleSheet.stylesheet.rules[0].declarations;
expect(styleSheet.stylesheet.rules[0].position).toEqual({
start: {
line: 2,
col: 1
},
end: {
line: 3,
col: 1
}
});
expect(declarations[0].position).toEqual({
start: {
line: 4,
col: 2
},
end: {
line: 5,
col: 1
}
});
expect(styleSheet.stylesheet.rules[1].position).toEqual({
start: {
line: 6,
col: 2
},
end: {
line: 6,
col: 11
}
});
var replacedText = styleText;
replacedText = utils.removeStyle(replacedText, styleSheet.stylesheet.rules[1].position.start, styleSheet.stylesheet.rules[1].position.end, 0, 0, 0, 'CCC');
replacedText = utils.removeStyle(replacedText, declarations[0].position.start, declarations[0].position.end, 0, 0, 0, 'BBB');
replacedText = utils.removeStyle(replacedText, styleSheet.stylesheet.rules[0].position.start, styleSheet.stylesheet.rules[0].position.end, 0, 0, 0, 'AAA');
expect(replacedText).toEqual(" \nAAA{\n BBB;\t}\n CCC{a:b}");
});
it('should return expected positions 2', function() {
var styleText = "a { b: c; d: e }\na { b: c; d: e }";
var styleSheet = require('mensch/lib/parser.js')(styleText, {
comments: true,
position: true
});
var declarations = styleSheet.stylesheet.rules[0].declarations;
expect(styleSheet.stylesheet.rules[0].position).toEqual({
start: {
line: 1,
col: 1
},
end: {
line: 1,
col: 3
}
});
expect(declarations[0].position).toEqual({
start: {
line: 1,
col: 5
},
end: {
line: 1,
col: 9
}
});
expect(styleSheet.stylesheet.rules[1].position).toEqual({
start: {
line: 2,
col: 1
},
end: {
line: 2,
col: 3
}
});
var replacedText = styleText;
replacedText = utils.removeStyle(replacedText, declarations[1].position.start, declarations[1].position.end, 0, 0, 0, 'D:E');
replacedText = utils.removeStyle(replacedText, declarations[0].position.start, declarations[0].position.end, 0, 0, 0, 'B:C');
replacedText = utils.removeStyle(replacedText, styleSheet.stylesheet.rules[0].position.start, styleSheet.stylesheet.rules[0].position.end, 0, 0, 0, 'A');
expect(replacedText).toEqual("A{ B:C; D:E}\na { b: c; d: e }");
});
});