Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): use recursive callback to resolve variant (#1126)
- Loading branch information
1 parent
a95a877
commit 7c88219
Showing
4 changed files
with
180 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Vitest Snapshot v1 | ||
|
||
exports[`variants > variant can stack 1`] = ` | ||
"/* layer: default */ | ||
.first\\\\:second\\\\:third\\\\:foo > :third > :second > :first, | ||
.first\\\\:three\\\\:two\\\\:foo > :first + :three + :two, | ||
.one\\\\:two\\\\:three\\\\:foo + :one + :two + :three{name:bar;}" | ||
`; | ||
|
||
exports[`variants > variant context is propagated 1`] = ` | ||
"/* layer: default */ | ||
.foo{name:bar;} | ||
/* layer: variant */ | ||
@supports{ | ||
.selector{name:bar !important;} | ||
}" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { createGenerator } from '@unocss/core' | ||
import { describe, expect, test } from 'vitest' | ||
|
||
describe('variants', () => { | ||
test('variant context is propagated', async () => { | ||
const uno = createGenerator({ | ||
rules: [ | ||
['foo', { name: 'bar' }], | ||
], | ||
variants: [ | ||
{ | ||
match(input) { | ||
const match = input.match(/^var:/) | ||
if (match) { | ||
return { | ||
matcher: input.slice(match[0].length), | ||
handle: (input, next) => next({ | ||
selector: '.selector', | ||
entries: input.entries.map((entry) => { | ||
entry[1] += ' !important' | ||
return entry | ||
}), | ||
parent: '@supports', | ||
layer: 'variant', | ||
}), | ||
} | ||
} | ||
}, | ||
}, | ||
], | ||
}) | ||
|
||
const { css } = await uno.generate([ | ||
'foo', | ||
'var:foo', | ||
].join(' '), { preflights: false }) | ||
|
||
expect(css).toMatchSnapshot() | ||
}) | ||
|
||
test('variant can stack', async () => { | ||
const uno = createGenerator({ | ||
rules: [ | ||
['foo', { name: 'bar' }], | ||
], | ||
variants: [ | ||
{ | ||
multiPass: true, | ||
match(input) { | ||
const match = input.match(/^(first|second|third):/) | ||
if (match) { | ||
return { | ||
matcher: input.slice(match[0].length), | ||
handle: (input, next) => next({ | ||
...input, | ||
selector: `${input.selector} > :${match[1]}`, | ||
}), | ||
} | ||
} | ||
}, | ||
}, | ||
{ | ||
multiPass: true, | ||
match(input) { | ||
const match = input.match(/^(one|two|three):/) | ||
if (match) { | ||
return { | ||
matcher: input.slice(match[0].length), | ||
handle: (input, next) => { | ||
const result = next(input) | ||
return { | ||
...result, | ||
selector: `${result.selector} + :${match[1]}`, | ||
} | ||
}, | ||
} | ||
} | ||
}, | ||
}, | ||
], | ||
}) | ||
|
||
const { css } = await uno.generate([ | ||
'first:second:third:foo', | ||
'one:two:three:foo', | ||
'first:three:two:foo', | ||
].join(' '), { preflights: false }) | ||
|
||
expect(css).toMatchSnapshot() | ||
}) | ||
}) |