This repository was archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcss.js
More file actions
120 lines (89 loc) · 4.22 KB
/
css.js
File metadata and controls
120 lines (89 loc) · 4.22 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
115
116
117
118
119
120
var assert = require('assert');
var editTree = require('../../lib/editTree/css');
describe('CSS Edit Tree', function() {
it('checking internals', function() {
var rule = editTree.parse('a{b:c;}');
assert.equal(rule.nameRange().start, 0, 'Selector position');
assert.equal(rule._positions.contentStart, 2, 'Content position');
var property = rule.get(0);
assert.equal(property.namePosition(), 2, 'Name position of property "' + property.name() + '"');
assert.equal(property.valuePosition(), 4, 'Value position of property "' + property.name() + '"');
assert.equal(rule.value('b'), 'c', 'Value of property "' + property.name() + '"');
assert.equal(rule.indexOf('b'), 0, 'Index of property "' + property.name() + '"');
assert.equal(rule.name(), 'a', 'Getting selector value');
rule.add('d', 'e');
var prop = rule.itemFromPosition(7);
assert.equal(prop.name(), 'd', 'Got property from position');
rule.add('t', '123', 0);
});
it('checking modifications', function() {
var rule = editTree.parse('a{c:d;}');
rule.value('c', 'abc');
assert.equal(rule.value('c'), 'abc', 'New value');
assert.equal(rule.source, 'a{c:abc;}', 'New source');
assert.deepEqual(rule.get('c').valueRange(true).toArray(), [4, 7], 'Proper value range');
rule.add('e', 'hello');
assert.equal(rule.value('e'), 'hello', 'New property');
assert.equal(rule.source, 'a{c:abc;e:hello;}', 'Source with new property');
rule.remove('c');
assert.equal(rule.source, 'a{e:hello;}', 'Source with removed property');
});
it('checking semicolor auto-insertion', function() {
var rule = editTree.parse('a{f:g}');
rule.add('h', 'hello');
assert.equal(rule.source, 'a{f:g;h:hello;}', 'Source with auto-inserted semi-colon');
});
it('checking source with formatting', function() {
var rule = editTree.parse('img {\n\tborder: 1px solid red !important; /* comment */\n\tfont: "arial", sans-serif;\n}');
rule.add('color', 'red');
assert.equal(rule.source, 'img {\n\tborder: 1px solid red !important; /* comment */\n\tfont: "arial", sans-serif;\n\tcolor: red;\n}', 'Source with formatting 1');
var rule2 = editTree.parse('.a {\n\tcolor: black;\n\t}');
rule2.add('font', 'bold');
assert.equal(rule2.source, '.a {\n\tcolor: black;\n\tfont: bold;\n\t}', 'Source with formatting 2');
var rule3 = editTree.parse('a {\n\tb: c;\n\t/* c */\n\td: e;\n}');
rule3.add('f', 'g', 1);
assert.equal(rule3.source, 'a {\n\tb: c;\n\t/* c */\n\tf: g;\n\td: e;\n}', 'Source with formatting 3');
rule3.add('h', 'i');
assert.equal(rule3.source, 'a {\n\tb: c;\n\t/* c */\n\tf: g;\n\td: e;\n\th: i;\n}', 'Source with formatting 4');
});
it('checking value parts', function() {
var rule = editTree.parse('.a {b:hello "lorem ipsum", func(lorem ipsum) 123 ""; c: fn1(a), fn2(fn3(b))}');
var prop = rule.get('b');
var parts = prop.valueParts().map(function(r) {
return r.substring(prop.value());
});
assert.deepEqual(parts, ['hello', '"lorem ipsum"', 'func(lorem ipsum)', '123', '""'], 'Correctly splitted complex value');
prop.value('1px');
var parts = prop.valueParts().map(function(r) {
return r.substring(prop.value());
});
assert.deepEqual(parts, ['1px'], 'No need to split simple value');
prop = rule.get('c');
parts = prop.valueParts().map(function(r) {
return r.substring(prop.value());
});
assert.deepEqual(parts, ['fn1(a)', 'fn2(fn3(b))'], 'Correctly splitted complex value with nested functions');
});
it('should work with incomplete rules', function() {
// without colon
var rule = editTree.parse('a{b\nc:d;}');
assert.equal(rule.get(0).name(), 'b');
assert.equal(rule.get(1).name(), 'c');
rule.get(0).value('test');
assert.equal(rule.source, 'a{b:test;\nc:d;}');
// with colon
rule = editTree.parse('a{b:\nc:d;}');
assert.equal(rule.get(0).name(), 'b');
assert.equal(rule.get(1).name(), 'c');
rule.get(0).value('test');
assert.equal(rule.source, 'a{b:test;\nc:d;}');
});
it('should work with nesting', function() {
var rule = editTree.parse('a{b:c; d{e:f} g:h }');
assert.equal(rule.get(0).name(), 'b');
assert.equal(rule.get(1).name(), 'g');
rule.get(0).value('foo');
rule.get(1).value('bar');
assert.equal(rule.source, 'a{b:foo; d{e:f} g:bar }');
});
});