-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
258 lines (254 loc) · 9.67 KB
/
index.js
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
// @flow
import Stylis from '@emotion/stylis'
import stylisRuleSheet from 'stylis-rule-sheet'
const specs: Array<{
name: string,
sample: string,
skip?: boolean,
only?: boolean
}> = [
{
name: 'calc rules',
sample: 'height:calc( 100vh - 1px );height:calc(100vh -1px);'
},
{
name: 'at-rules',
sample:
'@page { size:A4 landscape; }@document url(http://www.w3.org/),url-prefix(http://www.w3.org/Style/),domain(mozilla.org),regexp("https:.*") { body { color: red; }}@viewport { min-width:640px; max-width:800px;}@counter-style list { system:fixed; symbols:url(); suffix:" ";}',
skip: true
},
{
name: 'monkey-patch some invalid css patterns',
sample:
'margin:20px;.b { border:3px solid green;}}.c {color:red;}color:red;}.d {color red;};@media(screen) { width:20%;};@media(screen) { width:30%;};;;;;h1 {color:red}}};}h1 {color:red}}} ;}h1: hover{color:red;}'
},
{
name: 'escape breaking control characters',
sample: 'content:"\f\u0000\u000b";'
},
{
name: 'universal selector',
sample: '* {color:red;}'
},
{
name: 'flat',
sample: 'color:20px;font-size:20px;transition:all'
},
{
name: 'namespace',
sample: '{color:blue;}& {color:red;}'
},
{
name: 'comments',
sample:
"// line comment// color: red;/** * removes block comments and line comments, * there's a fire in the house // there is */button /* // what's xxx */{color: blue;}// hellobutton /* 1 */{color: red; /* 2 */}"
},
{
name: '&',
sample:
'& {color:blue;}&&& {color:red;}& + & {color:red;}.wrapper button& { color:red;}'
},
{
name: '&:before',
sample: '&:before{color:blue;}'
},
{
name: '@import',
sample: "@import url('http://example.com')",
skip: true
},
{
name: '@supports',
sample:
'@supports (display:block) {color:red;h1 {color:red;h2 {color:blue;}}display:none;}@supports (appearance: none) {color:red;}'
},
{
name: '@media',
skip: true,
sample:
'@media (max-width:600px) {color:red;h1 {color:red;h2 {color:blue;}}display:none;}@media (min-width:576px) {&.card-deck {.card { &:not(:first-child) { margin-left:15px; }&:not(:last-child) { margin-right:15px;}}}}@supports (display:block) {@media (min-width:10px) { background-color:seagreen;}}@media (max-width:600px) { & { color:red } } &:hover { color:orange }'
},
{
name: '@media specifity',
sample:
'> #box-not-working { background:red; padding-left:8px; width:10px; @media only screen and (min-width:10px) {width:calc(10px + 90px *(100vw - 10px) / 90); } @media only screen and (min-width:90px) {width:90px; } height: 10px; @media only screen and (min-width:10px) {height:calc(10px + 90px *(100vw - 10px) / 90); } @media only screen and (min-width:90px) { height: 90px; }}'
},
{
name: '@font-face',
sample:
"@font-face {font-family:Pangolin;src:url('Pangolin-Regular.ttf') format('truetype');}"
},
{
name: 'multiple selectors',
sample: 'span, h1 {color:red;}h1, &:after, &:before {color:red;}'
},
{
name: '[title="a,b"] and :matches(a,b)',
sample:
'.test:matches(a,b,c), .test {color:blue;}.test[title=","] {color:red;}[title="a,b,c, something"], h1, [title="a,b,c"] { color:red}[title="a"],[title="b"] {color:red;}'
},
{
name: 'quoutes',
sample:
'.foo:before { content:".hello {world}"; content:".hello {world} \' "; content:\'.hello {world} " \';}'
},
{
name: 'remove empty css',
sample: '& {}'
},
{
name: 'urls',
sample:
'background:url(http://url.com/});background:url(http://url.com//1234) \'(\'; // sdsdbackground-image:url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAAABCAIAAADsEU8HAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIklEQVQI12P8//8/Aw4wbdq0rKysAZG1trbGJXv06FH8sgDIJBbBfp+hFAAAAABJRU5ErkJggg==");'
},
{
name: 'last semicolon omission',
sample: '.content {display:none}.content {display:flex}'
},
{
name: ':matches(:not())',
sample: 'h1:matches(.a,.b,:not(.c)) {display: none}'
},
{
name: 'vendor prefixing',
sample:
'html { text-size-adjust: none;}input.red::placeholder { color:red;}& {width:var(--foo-content);width:var(-content);width:var(--max-content);width:--max-content;width:max-content;width:min-content;display:flex!important;display:inline-flex;display:inline-box; transform:rotate(30deg); cursor:grab; justify-content:flex-end; justify-content:flex-start; justify-content:justify;}div {align-items:value;align-self:value;align-content:value;}div {color:papayawhip;order:flex;}div {backface-visibility:hidden;}h1:read-only { color:red;}& {transition:transform 1s,transform all 400ms,text-transform;}'
},
{
name: 'vendor prefixing II',
sample:
'div {writing-mode:vertical-lr;writing-mode:vertical-rl;writing-mode:horizontal-tb;writing-mode:sideways-rl;writing-mode:sideways-lr;}'
},
{
name: 'vendor prefixing III',
sample:
'color:red;columns:auto;column-count:auto;column-fill:auto;column-gap:auto;column-rule:auto;column-rule-color:auto;column-rule-style:auto;column-rule-width:auto;column-span:auto;column-width:auto;'
},
{
name: 'vendor prefixing IV',
sample:
'text-size-adjust:none;text-decoration:none;filter:grayscale(100%);fill:red;position: sticky;mask-image: linear-gradient(#fff);mask-image: none;'
},
{
name: 'vendor prefixing V',
sample:
'display :flex!important;justify-content: space-between;align-self: flex-start;align-self: flex-end;'
},
{
name: 'vendor prefixing VI',
sample: 'clip-path: none;mask-image: none;'
},
{
name: 'nested',
sample:
':global(div) {h2 {color:red;h3 {color:blue;}}}.foo & { width:1px; &:hover { color:black; } li { color:white; }}h1, div {color:red;h2,&:before {color:red;}color:blue;header {font-size:12px;}@media {color:red;}@media {color:blue;}}&.foo {&.bar {color:orange}}&.foo {&.bar {&.barbar {color:orange}}}'
},
{
name: 'class namespace',
sample: 'h1 {animation:slide 1s;}'
},
{
name: 'id namespace',
sample: 'h1 {animation: slide 1s;}'
},
{
name: 'attribute namespace',
sample: 'h1 {animation: slide 1s;}'
},
{
name: 'empty namespace',
sample: 'h1 {animation:slide 1s;}@keyframes name {0: {top:0}}'
},
{
name: 'edge cases',
sample:
'@media (min-width:537px) { border-bottom:4px solid red;}&::placeholder { color:pink;}.a {color:\'red\'}.b {color:"red"}.a {color:red;}[role=button]{color:red;}.b {padding:30 3} '
},
{
name: 'whitespace cascade true',
sample: 'html {width:0;}'
},
{
name: 'whitespace cascade false',
sample: 'html{width:0;}'
},
{
name: 'cascade isolation simple',
sample:
'[data-id=foo] {color:red;}p {color:red;}p a {color:red;}p:hover { color:red;}p::before { color:red;}:hover { color:red;}::before { color:red;}:hover p { color:red;}html.something & {color:red;}.class #id {color:red;}& {color:red}.a.b .c {color:red;}:nth-child(2n),:nth-last-child(2n),:nth-of-type(2n) {color:red;}a:not(:focus):disabled {color:red;}a:not(:focus) :disabled {color:red;}'
},
{
name: 'cascade isolation complex',
sample:
'.List :global(>) :global(*) { margin-top:10px;}.List :global(*) :global(+) :global(*) { margin-top:10px;}.List :global(> *) { margin-top:10px;}.List :global(* + *) { margin-top:10px;}:global(.foo #bar > baz) {color:red;}div :global(.react-select .some-child-of-react-select) {color:red;}.root > :global(*):not(header) { color:red;}a:matches( a +b foo:hover :global(marquee) a) > :hover { color:red;}'
},
{
name: 'cascade isolation @at-rules',
sample:
'@keyframes hahaha { from { top:0 } to { top:100 }}span {} @media (min-width:480px) { div { color:red } }'
},
{
name: 'cascade isolation nesting',
sample:
'color:red;h1 {:global(section) {color:red}}h1 {h2 {color:red}}div, span {h1 {color:red}}span {&:hover {color:red}}[data-id=foo] {&:hover {color:red}}'
},
{
name: 'isolation edge cases',
sample: 'width:0;@media(screen) { color:red;}h1 {color:red;}'
},
{
name: 'semi-colons',
sample: 'color:redh2 {color:bluewidth:0h3 {display:none}}'
},
{
name: 'no semi-colons I',
sample:
'color:redh2 {color:bluewidth:0h3 {display:none}}div:hover{color:red}'
},
{
name: 'no semi-colons II',
sample:
'color:redcolor:redh1:hover,h2:hover,h3{color:redwidth:0/2}h1 {grid-template-areas:"header header header"\'. main .\';}h1 {width:calc(20px)20px;}'
},
{
name: 'multiline declaration',
sample:
'html { background-image: linear-gradient(0deg, rgba(255, 255, 255, 0.8), rgba(255, 255, 255, 0.8)), url(/static/background.svg);}'
},
{
name: 'nesting selector multiple levels',
sample: 'a {a {a {a {a {a {a {a {a{a{a{a{color:red;}}}}}}}}}}}}'
},
{
name: 'nesting @media multiple levels',
sample: 'div {@media {a {color:red;@media {h1 {color:red;}}}}}',
skip: true
},
{
name: 'complex nested selector',
sample:
'&:hover{color:blue;&:active{color:red;}}font-size:2rem;padding:16px;&:hover{color:pink;&:active{color:purple;}&.some-class{color:yellow;}}'
}
]
let stylisOptions = {
global: false,
preserve: false,
keyframe: false,
semicolon: true,
cascade: true
}
let stylis = new Stylis(stylisOptions)
let regularStylis = new Stylis(stylisOptions)
specs.forEach((spec, i) => {
const newTest = spec.only ? test.only : spec.skip ? test.skip : test
newTest(spec.name, () => {
let out = []
const plugin = stylisRuleSheet(rule => {
out.push(rule)
})
stylis.use(null)(plugin)
stylis(`.css-${i}`, spec.sample)
expect(out).toMatchSnapshot()
expect(out.join('')).toEqual(regularStylis(`.css-${i}`, spec.sample))
})
})