1 | const path = require('path')
|
2 | const test = require('ava')
|
3 | const select = require('unist-util-select')
|
4 | const {parse, stringify} = require('..')
|
5 |
|
6 | const fixture = (...args) => path.resolve(__dirname, 'fixtures', ...args)
|
7 |
|
8 | const preserves = (message, strings) => {
|
9 | test(`it preserves ${message}`, t => {
|
10 | strings.forEach(str => {
|
11 | const parsed = parse(str)
|
12 | t.is(stringify(parsed), str,
|
13 | 'types: ' + select(parsed, '*').map(n => n.type).join(', '))
|
14 | })
|
15 | })
|
16 | }
|
17 |
|
18 | test('it preserves anything with a value', t => {
|
19 | t.is(stringify({value: 'foo'}), 'foo')
|
20 | t.is(stringify({type: 'unknown', value: 'foo'}), 'foo')
|
21 | })
|
22 |
|
23 | preserves('variables', [
|
24 | '$foo: 1',
|
25 | '$foo-bar: 1',
|
26 | ])
|
27 |
|
28 | preserves('unit values', [
|
29 | 'x { height: 100%; }',
|
30 | 'x { width: 100px; }',
|
31 | 'x { width: 10em; }',
|
32 | 'x { width: 10rem; }',
|
33 | ])
|
34 |
|
35 | preserves('blocks', [
|
36 | 'x { color: red; }',
|
37 | 'x { color: red; y { color: blue; } }',
|
38 | ])
|
39 |
|
40 | preserves('@include', [
|
41 | '@include breakpoint(md)',
|
42 | '@include breakpoint()',
|
43 | '@include breakpoint',
|
44 | ])
|
45 |
|
46 | preserves('@mixin', [
|
47 | '@mixin breakpoint($size) { }',
|
48 | '@mixin breakpoint { }',
|
49 | ])
|
50 |
|
51 | preserves('all manner of parentheses', [
|
52 | '$x: (1, 2)',
|
53 | '$x: (1, (2, 2, (1 + 2)))',
|
54 | '$x: (1, (2, 2, ($y + 2 * 5)))',
|
55 | ])
|
56 |
|
57 | preserves('functions', [
|
58 | '$x: y(1)',
|
59 | '$x: y(z(1 + 2))',
|
60 | '$x: $y( z(1 + 2) )',
|
61 | '$x: $z( 1 + 2 )',
|
62 | ])
|
63 |
|
64 | preserves('color values', [
|
65 | '$color: #f00',
|
66 | '$color: red',
|
67 | '$color: rgb(255, 0, 0)',
|
68 | '$color: rgba(255, 0, 0, 0.5)',
|
69 | '$color: rgba(#f00, 0.5)',
|
70 | ])
|
71 |
|
72 | preserves('universal selectors', [
|
73 | '* { box-sizing: border-box; }',
|
74 | '* > li { list-style: none; }',
|
75 | 'dl > * { list-style: none; }',
|
76 | ])
|
77 |
|
78 | preserves('class selectors', [
|
79 | '.foo { }',
|
80 | '.foo.bar { }',
|
81 | 'x.foo { }',
|
82 | 'x.foo.bar { }',
|
83 | 'x.foo .bar { }',
|
84 | ])
|
85 |
|
86 |
|
87 | preserves('attribute selectors', [
|
88 | '[data-foo] { }',
|
89 | 'x[data-foo] { }',
|
90 | 'x[data-foo=x] { }',
|
91 | 'x[data-foo="x"] { }',
|
92 | 'x[data-foo^="x"] { }',
|
93 | 'x[data-foo*="x"] { }',
|
94 | 'x[data-foo$="x"] { }',
|
95 | 'x[data-foo|="x"] { }',
|
96 |
|
97 | 'x[data-foo=x i] { }',
|
98 | 'x[data-foo="x" i] { }',
|
99 | 'x[data-foo~="x" i] { }',
|
100 | ])
|
101 |
|
102 | preserves('placeholders', [
|
103 | 'a:hover { &:active { color: green; } }',
|
104 | 'a.foo { &:hover { color: red; } }',
|
105 | ])
|
106 |
|
107 | preserves('pseudo-classes', [
|
108 | 'a:hover { color: red; }',
|
109 | 'a:hover { &:active { color: green; } }',
|
110 | 'a:-vendor-thing { color: red; }',
|
111 | ])
|
112 |
|
113 | preserves('pseudo-elements', [
|
114 | 'a::before { content: "hi"; }',
|
115 | 'a::-vendor-thing { display: none; }',
|
116 |
|
117 | 'a:before { content: "hi"; }',
|
118 | ])
|
119 |
|
120 | preserves('comma-separated values', [
|
121 | 'x { background: red, green, blue; }',
|
122 | ])
|
123 |
|
124 | preserves('URLs', [
|
125 | 'a { background-url: url(foo.png); }',
|
126 | 'a { background-url: url("foo.png"); }',
|
127 | "a { background-url: url('foo.png'); }",
|
128 | ])
|