UNPKG

2.85 kBJavaScriptView Raw
1const path = require('path')
2const test = require('ava')
3const select = require('unist-util-select')
4const {parse, stringify} = require('..')
5
6const fixture = (...args) => path.resolve(__dirname, 'fixtures', ...args)
7
8const 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
18test('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
23preserves('variables', [
24 '$foo: 1',
25 '$foo-bar: 1',
26])
27
28preserves('unit values', [
29 'x { height: 100%; }',
30 'x { width: 100px; }',
31 'x { width: 10em; }',
32 'x { width: 10rem; }',
33])
34
35preserves('blocks', [
36 'x { color: red; }',
37 'x { color: red; y { color: blue; } }',
38])
39
40preserves('@include', [
41 '@include breakpoint(md)',
42 '@include breakpoint()',
43 '@include breakpoint',
44])
45
46preserves('@mixin', [
47 '@mixin breakpoint($size) { }',
48 '@mixin breakpoint { }',
49])
50
51preserves('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
57preserves('functions', [
58 '$x: y(1)',
59 '$x: y(z(1 + 2))',
60 '$x: $y( z(1 + 2) )',
61 '$x: $z( 1 + 2 )',
62])
63
64preserves('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
72preserves('universal selectors', [
73 '* { box-sizing: border-box; }',
74 '* > li { list-style: none; }',
75 'dl > * { list-style: none; }',
76])
77
78preserves('class selectors', [
79 '.foo { }',
80 '.foo.bar { }',
81 'x.foo { }',
82 'x.foo.bar { }',
83 'x.foo .bar { }',
84])
85
86// see: <https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors>
87preserves('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 // deep cuts here
97 'x[data-foo=x i] { }',
98 'x[data-foo="x" i] { }',
99 'x[data-foo~="x" i] { }',
100])
101
102preserves('placeholders', [
103 'a:hover { &:active { color: green; } }',
104 'a.foo { &:hover { color: red; } }',
105])
106
107preserves('pseudo-classes', [
108 'a:hover { color: red; }',
109 'a:hover { &:active { color: green; } }',
110 'a:-vendor-thing { color: red; }',
111])
112
113preserves('pseudo-elements', [
114 'a::before { content: "hi"; }',
115 'a::-vendor-thing { display: none; }',
116 // browsers understand this
117 'a:before { content: "hi"; }',
118])
119
120preserves('comma-separated values', [
121 'x { background: red, green, blue; }',
122])
123
124preserves('URLs', [
125 'a { background-url: url(foo.png); }',
126 'a { background-url: url("foo.png"); }',
127 "a { background-url: url('foo.png'); }",
128])