Repository navigation
Expand file tree
/
Copy pathunits.js
More file actions
131 lines (116 loc) · 3.22 KB
/
Copy pathunits.js
File metadata and controls
131 lines (116 loc) · 3.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
121
122
123
124
125
126
127
128
129
130
131
import { expect, it } from 'vitest'
import transformCss from '..'
// List of units from:
// https://developer.mozilla.org/en-US/docs/Web/CSS/length
const lengthUnits = [
'ch',
'em',
'ex',
'rem',
'vh',
'vw',
'vmin',
'vmax',
'cm',
'mm',
'in',
'pc',
'pt',
]
lengthUnits.forEach(unit => {
const value = `2${unit}`
it('allows CSS length units in transformed values', () => {
expect(transformCss([['margin', value]])).toEqual({
marginTop: value,
marginRight: value,
marginBottom: value,
marginLeft: value,
})
expect(transformCss([['padding', value]])).toEqual({
paddingTop: value,
paddingRight: value,
paddingBottom: value,
paddingLeft: value,
})
})
it('allows CSS length units with 0 and unit', () => {
expect(transformCss([['padding', `0${unit}`]])).toEqual({
paddingTop: `0${unit}`,
paddingRight: `0${unit}`,
paddingBottom: `0${unit}`,
paddingLeft: `0${unit}`,
})
})
it('allows mixed units in transformed values', () => {
expect(transformCss([['margin', `10px ${value}`]])).toEqual({
marginTop: 10,
marginRight: value,
marginBottom: 10,
marginLeft: value,
})
})
it('allows units to be used with border shorthand property', () => {
expect(transformCss([['border', `#f00 ${value} dashed`]])).toEqual({
borderWidth: value,
borderColor: '#f00',
borderStyle: 'dashed',
})
expect(transformCss([['border', value]])).toEqual({
borderWidth: value,
borderColor: 'black',
borderStyle: 'solid',
})
})
it('allows units to be used with border-width', () => {
expect(transformCss([['border-width', `1px 2px ${value} 4px`]])).toEqual({
borderTopWidth: 1,
borderRightWidth: 2,
borderBottomWidth: value,
borderLeftWidth: 4,
})
})
it('allows units to be used with border-radius', () => {
expect(transformCss([['border-radius', `1px ${value} 3px 4px`]])).toEqual({
borderTopLeftRadius: 1,
borderTopRightRadius: value,
borderBottomRightRadius: 3,
borderBottomLeftRadius: 4,
})
})
it('allows units to be used with font-size', () => {
expect(transformCss([['font-size', value]])).toEqual({
fontSize: value,
})
})
it('allows units to be used with font shorthand property', () => {
expect(
transformCss([['font', `bold italic ${value}/${value} "Helvetica"`]])
).toEqual({
fontFamily: 'Helvetica',
fontSize: value,
fontWeight: 'bold',
fontStyle: 'italic',
fontVariant: [],
lineHeight: value,
})
})
it('allows untis to be used with text-shadow ', () => {
expect(transformCss([['text-shadow', `10px ${value} red`]])).toEqual({
textShadowOffset: { width: 10, height: value },
textShadowRadius: 0,
textShadowColor: 'red',
})
})
it('allows units to be used with box-shadow', () => {
expect(
transformCss([['box-shadow', `10px ${value} ${value} red`]])
).toEqual({
boxShadow: `10px ${value} ${value} red`,
})
})
})
it('throws for unit that is not supported', () => {
expect(() => transformCss([['margin', '10ic']])).toThrow(
'Failed to parse declaration "margin: 10ic"'
)
})