-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathstringifier.test.js
More file actions
executable file
·288 lines (223 loc) · 7.27 KB
/
stringifier.test.js
File metadata and controls
executable file
·288 lines (223 loc) · 7.27 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
let { test } = require('uvu')
let { is } = require('uvu/assert')
let {
AtRule,
Declaration,
Document,
Node,
parse,
Root,
Rule
} = require('../lib/postcss')
let Stringifier = require('../lib/stringifier')
let str
test.before.each(() => {
str = new Stringifier()
})
test('creates trimmed/raw property', () => {
let b = new Node({ one: 'trim' })
b.raws.one = { raw: 'raw', value: 'trim' }
is(str.rawValue(b, 'one'), 'raw')
b.one = 'trim1'
is(str.rawValue(b, 'one'), 'trim1')
})
test('works without rawValue magic', () => {
let b = new Node()
b.one = '1'
is(b.one, '1')
is(str.rawValue(b, 'one'), '1')
})
test('uses node raw', () => {
let rule = new Rule({ raws: { between: '\n' }, selector: 'a' })
is(str.raw(rule, 'between', 'beforeOpen'), '\n')
})
test('hacks before for nodes without parent', () => {
let rule = new Rule({ selector: 'a' })
is(str.raw(rule, 'before'), '')
})
test('hacks before for first node', () => {
let root = new Root()
root.append(new Rule({ selector: 'a' }))
is(str.raw(root.first, 'before'), '')
})
test('hacks before for first decl', () => {
let decl = new Declaration({ prop: 'color', value: 'black' })
is(str.raw(decl, 'before'), '')
let rule = new Rule({ selector: 'a' })
rule.append(decl)
is(str.raw(decl, 'before'), '\n ')
})
test('detects after raw', () => {
let root = new Root()
root.append({ raws: { after: ' ' }, selector: 'a' })
root.first.append({ prop: 'color', value: 'black' })
root.append({ selector: 'a' })
is(str.raw(root.last, 'after'), ' ')
})
test('uses defaults without parent', () => {
let rule = new Rule({ selector: 'a' })
is(str.raw(rule, 'between', 'beforeOpen'), ' ')
})
test('uses defaults for unique node', () => {
let root = new Root()
root.append(new Rule({ selector: 'a' }))
is(str.raw(root.first, 'between', 'beforeOpen'), ' ')
})
test('clones raw from first node', () => {
let root = new Root()
root.append(new Rule({ raws: { between: '' }, selector: 'a' }))
root.append(new Rule({ selector: 'b' }))
is(str.raw(root.last, 'between', 'beforeOpen'), '')
})
test('indents by default', () => {
let root = new Root()
root.append(new AtRule({ name: 'page' }))
root.first.append(new Rule({ selector: 'a' }))
root.first.first.append({ prop: 'color', value: 'black' })
is(
root.toString(),
'@page {\n' + ' a {\n' + ' color: black\n' + ' }\n' + '}'
)
})
test('clones style', () => {
let compress = parse('@page{ a{ } }')
let spaces = parse('@page {\n a {\n }\n}')
compress.first.first.append({ prop: 'color', value: 'black' })
is(compress.toString(), '@page{ a{ color: black } }')
spaces.first.first.append({ prop: 'color', value: 'black' })
is(spaces.toString(), '@page {\n a {\n color: black\n }\n}')
})
test('clones indent', () => {
let root = parse('a{\n}')
root.first.append({ text: 'a' })
root.first.append({ raws: { before: '\n\n ' }, text: 'b' })
is(root.toString(), 'a{\n\n /* a */\n\n /* b */\n}')
})
test('clones declaration before for comment', () => {
let root = parse('a{\n}')
root.first.append({ text: 'a' })
root.first.append({
prop: 'a',
raws: { before: '\n\n ' },
value: '1'
})
is(root.toString(), 'a{\n\n /* a */\n\n a: 1\n}')
})
test('clones indent by types', () => {
let css = parse('a {\n *color: black\n}\n\nb {\n}')
css.append(new Rule({ selector: 'em' }))
css.last.append({ prop: 'z-index', value: '1' })
is(css.last.first.raw('before'), '\n ')
})
test('ignores non-space symbols in indent cloning', () => {
let css = parse('a {\n color: black\n}\n\nb {\n}')
css.append(new Rule({ selector: 'em' }))
css.last.append({ prop: 'z-index', value: '1' })
is(css.last.raw('before'), '\n\n')
is(css.last.first.raw('before'), '\n ')
})
test('clones indent by before and after', () => {
let css = parse('@page{\n\n a{\n color: black}}')
css.first.append(new Rule({ selector: 'b' }))
css.first.last.append({ prop: 'z-index', value: '1' })
is(css.first.last.raw('before'), '\n\n ')
is(css.first.last.raw('after'), '')
})
test('clones semicolon only from rules with children', () => {
let css = parse('a{}b{one:1;}')
is(str.raw(css.first, 'semicolon'), true)
})
test('clones only spaces in before', () => {
let css = parse('a{*one:1}')
css.first.append({ prop: 'two', value: '2' })
css.append({ name: 'keyframes', params: 'a' })
css.last.append({ selector: 'from' })
is(css.toString(), 'a{*one:1;two:2}\n@keyframes a{\nfrom{}}')
})
test('clones only spaces in between', () => {
let css = parse('a{one/**/:1}')
css.first.append({ prop: 'two', value: '2' })
is(css.toString(), 'a{one/**/:1;two:2}')
})
test('uses optional raws.indent', () => {
let rule = new Rule({ raws: { indent: ' ' }, selector: 'a' })
rule.append({ prop: 'color', value: 'black' })
is(rule.toString(), 'a {\n color: black\n}')
})
test('handles nested roots', () => {
let root = new Root()
let subRoot = new Root()
subRoot.append(new AtRule({ name: 'foo' }))
root.append(subRoot)
is(root.toString(), '@foo')
})
test('handles root', () => {
let root = new Root()
root.append(new AtRule({ name: 'foo' }))
let s = root.toString()
is(s, '@foo')
})
test('handles root with after', () => {
let root = new Root({ raws: { after: ' ' } })
root.append(new AtRule({ name: 'foo' }))
let s = root.toString()
is(s, '@foo ')
})
test('pass nodes to document', () => {
let root = new Root()
let document = new Document({ nodes: [root] })
is(document.toString(), '')
})
test('handles document with one root', () => {
let root = new Root()
root.append(new AtRule({ name: 'foo' }))
let document = new Document()
document.append(root)
let s = document.toString()
is(s, '@foo')
})
test('handles document with one root and after raw', () => {
let document = new Document()
let root = new Root({ raws: { after: ' ' } })
root.append(new AtRule({ name: 'foo' }))
document.append(root)
let s = document.toString()
is(s, '@foo ')
})
test('handles document with one root and before and after', () => {
let document = new Document()
let root = new Root({ raws: { after: 'AFTER' } })
root.append(new AtRule({ name: 'foo' }))
document.append(root)
let s = document.toString()
is(s, '@fooAFTER')
})
test('handles document with three roots without raws', () => {
let root1 = new Root()
root1.append(new AtRule({ name: 'foo' }))
let root2 = new Root()
root2.append(new Rule({ selector: 'a' }))
let root3 = new Root()
root3.append(new Declaration({ prop: 'color', value: 'black' }))
let document = new Document()
document.append(root1)
document.append(root2)
document.append(root3)
let s = document.toString()
is(s, '@fooa {}color: black')
})
test('handles document with three roots, with before and after raws', () => {
let root1 = new Root({ raws: { after: 'AFTER_ONE' } })
root1.append(new Rule({ selector: 'a.one' }))
let root2 = new Root({ raws: { after: 'AFTER_TWO' } })
root2.append(new Rule({ selector: 'a.two' }))
let root3 = new Root({ raws: { after: 'AFTER_THREE' } })
root3.append(new Rule({ selector: 'a.three' }))
let document = new Document()
document.append(root1)
document.append(root2)
document.append(root3)
let s = document.toString()
is(s, 'a.one {}AFTER_ONEa.two {}AFTER_TWOa.three {}AFTER_THREE')
})
test.run()