tailwindcss/tests/modify-selectors.test.js
Robin Malfait b17b60ef6a
Sort by layer inside variants layer (#10505)
* sort by `layer` inside `variants` layer

We currently have a whole system for sorting the utilties / components
already. Right now we also have a "variants" layer, this is to ensure
that we always have variants at the end of the file regardless of the
`parentLayer` (base, utilties, components).

That said, we also have to make sure that within this `variants` layer
we also sort by the `parentLayer`.

* update changelog

* ensure order is correct now
2023-02-06 16:27:01 +01:00

96 lines
2.6 KiB
JavaScript

import selectorParser from 'postcss-selector-parser'
import { crosscheck, run, html, css } from './util/run'
crosscheck(() => {
test('modify selectors', () => {
let config = {
darkMode: 'class',
content: [
{
raw: html`
<div class="font-bold"></div>
<div class="foo:font-bold"></div>
<div class="foo:hover:font-bold"></div>
<div class="sm:foo:font-bold"></div>
<div class="md:foo:focus:font-bold"></div>
<div class="markdown">
<p>Lorem ipsum dolor sit amet...</p>
</div>
<div class="foo:markdown">
<p>Lorem ipsum dolor sit amet...</p>
</div>
<div class="foo:visited:markdown">
<p>Lorem ipsum dolor sit amet...</p>
</div>
<div class="lg:foo:disabled:markdown">
<p>Lorem ipsum dolor sit amet...</p>
</div>
`,
},
],
corePlugins: { preflight: false },
theme: {},
plugins: [
function ({ addVariant }) {
addVariant('foo', ({ modifySelectors, separator }) => {
modifySelectors(({ selector }) => {
return selectorParser((selectors) => {
selectors.walkClasses((classNode) => {
classNode.value = `foo${separator}${classNode.value}`
classNode.parent.insertBefore(classNode, selectorParser().astSync(`.foo `))
})
}).processSync(selector)
})
})
},
],
}
let input = css`
@tailwind components;
@tailwind utilities;
@layer components {
.markdown > p {
margin-top: 12px;
}
}
`
return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
.markdown > p {
margin-top: 12px;
}
.font-bold {
font-weight: 700;
}
.foo .foo\:markdown > p,
.foo .foo\:visited\:markdown:visited > p {
margin-top: 12px;
}
@media (min-width: 1024px) {
.foo .lg\:foo\:disabled\:markdown:disabled > p {
margin-top: 12px;
}
}
.foo .foo\:font-bold,
.foo .foo\:hover\:font-bold:hover {
font-weight: 700;
}
@media (min-width: 640px) {
.foo .sm\:foo\:font-bold {
font-weight: 700;
}
}
@media (min-width: 768px) {
.foo .md\:foo\:focus\:font-bold:focus {
font-weight: 700;
}
}
`)
})
})
})