forked from postcss/postcss-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectifier.test.js
More file actions
205 lines (179 loc) · 4.92 KB
/
objectifier.test.js
File metadata and controls
205 lines (179 loc) · 4.92 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
let { equal } = require('uvu/assert')
let { parse } = require('postcss')
let { test } = require('uvu')
let postcssJS = require('../')
test('converts declaration', () => {
let root = parse('color: black')
equal(postcssJS.objectify(root), { color: 'black' })
})
test('converts declarations to array', () => {
let root = parse('color: black; color: rgba(0,0,0,.5); color: #000.5;')
equal(postcssJS.objectify(root), {
color: ['black', 'rgba(0,0,0,.5)', '#000.5']
})
})
test('converts at-rules to array', () => {
let root = parse(
'@font-face { font-family: A }' +
'@font-face { font-family: B }' +
'@font-face { font-family: C }'
)
equal(postcssJS.objectify(root), {
'@font-face': [
{ fontFamily: 'A' },
{ fontFamily: 'B' },
{ fontFamily: 'C' }
]
})
})
test('converts declarations to camel case', () => {
let root = parse('-webkit-z-index: 1; -ms-z-index: 1; z-index: 1')
equal(postcssJS.objectify(root), {
WebkitZIndex: '1',
msZIndex: '1',
zIndex: 1
})
})
test('preserves case for sass exported variables', () => {
let root = parse(
':export { caseSensitiveOne: 1px }' +
':export { caseSensitiveTwo: 2px }' +
':export { caseSensitiveThree: 3px }'
)
equal(postcssJS.objectify(root), {
':export': {
caseSensitiveOne: '1px',
caseSensitiveTwo: '2px',
caseSensitiveThree: '3px'
}
})
})
test('maintains !important declarations', () => {
let root = parse('margin-bottom: 0 !important')
equal(postcssJS.objectify(root), {
marginBottom: '0 !important'
})
})
test('ignores comments', () => {
let root = parse('color: black; /* test */')
equal(postcssJS.objectify(root), { color: 'black' })
})
test('converts rules', () => {
let root = parse('&:hover { color: black }')
equal(postcssJS.objectify(root), {
'&:hover': {
color: 'black'
}
})
})
test('merge rules', () => {
let root = parse('div { color:blue } div { padding:5px }')
equal(postcssJS.objectify(root), {
div: {
color: 'blue',
padding: '5px'
}
})
})
test('converts at-rules', () => {
let root = parse('@media screen { color: black }')
equal(postcssJS.objectify(root), {
'@media screen': {
color: 'black'
}
})
})
test('converts at-rules without params', () => {
let root = parse('@media { color: black }')
equal(postcssJS.objectify(root), {
'@media': {
color: 'black'
}
})
})
test('converts at-rules without children', () => {
let root = parse('@media screen { }')
equal(postcssJS.objectify(root), {
'@media screen': {}
})
})
test('does fall on at-rules in rules merge', () => {
let root = parse('@media screen { z-index: 1 } z-index: 2')
equal(postcssJS.objectify(root), {
'@media screen': {
zIndex: 1
},
'zIndex': 2
})
})
test('converts at-rules without body', () => {
let root = parse('@charset "UTF-8"')
equal(postcssJS.objectify(root), { '@charset "UTF-8"': true })
})
test('handles mixed case properties', () => {
let root = parse('COLOR: green; -WEBKIT-border-radius: 6px')
equal(postcssJS.objectify(root), {
color: 'green',
WebkitBorderRadius: '6px'
})
})
test("doesn't convert css variables", () => {
let root = parse('--test-variable: 0;')
equal(postcssJS.objectify(root), { '--test-variable': '0' })
})
test('converts unitless value to number instead of string', () => {
let root = parse('z-index: 100; opacity: .1;')
equal(postcssJS.objectify(root), {
zIndex: 100,
opacity: 0.1
})
})
test('merges rules ignoring important', () => {
let root = parse('a { height: 1px !important; };a { height: 2px }')
equal(postcssJS.objectify(root), {
a: {
height: "2px"
}
})
})
test('keeps last important in merge', () => {
let root = parse('a { height: 1px !important; };a { height: 2px !important; }')
equal(postcssJS.objectify(root), {
a: {
height: "2px !important"
}
})
})
test('prioritizes important in merge', () => {
let root = parse('a { height: 1px !important; };a { height: 2px }')
equal(postcssJS.objectify(root, { stringifyImportant: true }), {
a: {
height: "1px !important"
}
})
})
test('keeps last important with priority', () => {
let root = parse('a { height: 1px !important; };a { height: 2px !important; }')
equal(postcssJS.objectify(root, { stringifyImportant: true }), {
a: {
height: "2px !important"
}
})
})
test('keeps last important with priority', () => {
let root = parse('a { vertical-align: 2px center; };a { vertical-align: 1px center; }')
equal(postcssJS.objectify(root, { stringifyImportant: true }), {
a: {
'verticalAlign': "1px center"
}
})
})
test('keeps last important with priority', () => {
let root = parse('a { vertical-align: 2px center !important; };a { vertical-align: 1px center; }')
equal(postcssJS.objectify(root, { stringifyImportant: true }), {
a: {
'verticalAlign': "2px center !important"
}
})
})
test.run()