123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640 |
- import { run, html, css, defaults } from './util/run'
- test('prefix', () => {
- let config = {
- prefix: 'tw-',
- darkMode: 'class',
- content: [
- {
- raw: html`
- <div class="tw--ml-4"></div>
- <div class="md:tw--ml-5"></div>
- <div class="md:hover:tw--ml-6"></div>
- <div class="tw-container"></div>
- <div class="btn-no-prefix"></div>
- <div class="tw-btn-prefix"></div>
- <div class="tw-custom-util-prefix"></div>
- <div class="custom-util-no-prefix"></div>
- <div class="custom-component"></div>
- <div class="tw-custom-component-prefix"></div>
- <div class="custom-component-no-prefix"></div>
- <div class="tw-font-bold"></div>
- <div class="md:hover:tw-text-right"></div>
- <div class="motion-safe:hover:tw-text-center"></div>
- <div class="dark:focus:tw-text-left"></div>
- <div class="dark:tw-bg-[rgb(255,0,0)]"></div>
- <div class="group-hover:focus-within:tw-text-left"></div>
- <div class="rtl:active:tw-text-center"></div>
- <div class="tw-animate-ping"></div>
- <div class="tw-animate-spin"></div>
- `,
- },
- ],
- corePlugins: { preflight: false },
- theme: {
- animation: {
- spin: 'spin 1s linear infinite',
- ping: 'ping 1s cubic-bezier(0, 0, 0.2, 1) infinite',
- },
- keyframes: {
- spin: { to: { transform: 'rotate(360deg)' } },
- },
- },
- plugins: [
- function ({ addComponents, addUtilities }) {
- addComponents({
- '.btn-prefix': {
- button: 'yes',
- },
- })
- addComponents(
- {
- '.btn-no-prefix': {
- button: 'yes',
- },
- },
- { respectPrefix: false }
- )
- addUtilities({
- '.custom-util-prefix': {
- button: 'no',
- },
- })
- addUtilities(
- {
- '.custom-util-no-prefix': {
- button: 'no',
- },
- },
- { respectPrefix: false }
- )
- },
- ],
- }
- let input = css`
- @tailwind base;
- @tailwind components;
- @layer components {
- .custom-component {
- @apply tw-font-bold dark:group-hover:tw-font-normal;
- }
- }
- @tailwind utilities;
- @layer utilities {
- .custom-utility {
- foo: bar;
- }
- }
- `
- return run(input, config).then((result) => {
- expect(result.css).toMatchFormattedCss(css`
- ${defaults}
- .tw-container {
- width: 100%;
- }
- @media (min-width: 640px) {
- .tw-container {
- max-width: 640px;
- }
- }
- @media (min-width: 768px) {
- .tw-container {
- max-width: 768px;
- }
- }
- @media (min-width: 1024px) {
- .tw-container {
- max-width: 1024px;
- }
- }
- @media (min-width: 1280px) {
- .tw-container {
- max-width: 1280px;
- }
- }
- @media (min-width: 1536px) {
- .tw-container {
- max-width: 1536px;
- }
- }
- .tw-btn-prefix,
- .btn-no-prefix {
- button: yes;
- }
- .custom-component {
- font-weight: 700;
- }
- :is(.tw-dark .tw-group:hover .custom-component) {
- font-weight: 400;
- }
- .tw--ml-4 {
- margin-left: -1rem;
- }
- .tw-animate-ping {
- animation: 1s cubic-bezier(0, 0, 0.2, 1) infinite ping;
- }
- @keyframes tw-spin {
- to {
- transform: rotate(360deg);
- }
- }
- .tw-animate-spin {
- animation: 1s linear infinite tw-spin;
- }
- .tw-font-bold {
- font-weight: 700;
- }
- .tw-custom-util-prefix,
- .custom-util-no-prefix {
- button: no;
- }
- .tw-group:hover .group-hover\:focus-within\:tw-text-left:focus-within {
- text-align: left;
- }
- :is([dir='rtl'] .rtl\:active\:tw-text-center:active) {
- text-align: center;
- }
- @media (prefers-reduced-motion: no-preference) {
- .motion-safe\:hover\:tw-text-center:hover {
- text-align: center;
- }
- }
- :is(.tw-dark .dark\:tw-bg-\[rgb\(255\,0\,0\)\]) {
- --tw-bg-opacity: 1;
- background-color: rgb(255 0 0 / var(--tw-bg-opacity));
- }
- :is(.tw-dark .dark\:focus\:tw-text-left:focus) {
- text-align: left;
- }
- @media (min-width: 768px) {
- .md\:tw--ml-5 {
- margin-left: -1.25rem;
- }
- .md\:hover\:tw--ml-6:hover {
- margin-left: -1.5rem;
- }
- .md\:hover\:tw-text-right:hover {
- text-align: right;
- }
- }
- `)
- })
- })
- test('negative values: marker before prefix', async () => {
- let config = {
- prefix: 'tw-',
- content: [{ raw: html`<div class="-tw-top-1"></div>` }],
- corePlugins: { preflight: false },
- }
- let input = css`
- @tailwind utilities;
- `
- await run(input, config)
- const result = await run(input, config)
- expect(result.css).toMatchFormattedCss(css`
- .-tw-top-1 {
- top: -0.25rem;
- }
- `)
- })
- test('negative values: marker after prefix', async () => {
- let config = {
- prefix: 'tw-',
- content: [{ raw: html`<div class="tw--top-1"></div>` }],
- corePlugins: { preflight: false },
- }
- let input = css`
- @tailwind utilities;
- `
- await run(input, config)
- const result = await run(input, config)
- expect(result.css).toMatchFormattedCss(css`
- .tw--top-1 {
- top: -0.25rem;
- }
- `)
- })
- test('negative values: marker before prefix and arbitrary value', async () => {
- let config = {
- prefix: 'tw-',
- content: [{ raw: html`<div class="-tw-top-[1px]"></div>` }],
- corePlugins: { preflight: false },
- }
- let input = css`
- @tailwind utilities;
- `
- await run(input, config)
- const result = await run(input, config)
- expect(result.css).toMatchFormattedCss(css`
- .-tw-top-\[1px\] {
- top: -1px;
- }
- `)
- })
- test('negative values: marker after prefix and arbitrary value', async () => {
- let config = {
- prefix: 'tw-',
- content: [{ raw: html`<div class="tw--top-[1px]"></div>` }],
- corePlugins: { preflight: false },
- }
- let input = css`
- @tailwind utilities;
- `
- await run(input, config)
- const result = await run(input, config)
- expect(result.css).toMatchFormattedCss(css`
- .tw--top-\[1px\] {
- top: -1px;
- }
- `)
- })
- test('negative values: no marker and arbitrary value', async () => {
- let config = {
- prefix: 'tw-',
- content: [{ raw: html`<div class="tw-top-[-1px]"></div>` }],
- corePlugins: { preflight: false },
- }
- let input = css`
- @tailwind utilities;
- `
- await run(input, config)
- const result = await run(input, config)
- expect(result.css).toMatchFormattedCss(css`
- .tw-top-\[-1px\] {
- top: -1px;
- }
- `)
- })
- test('negative values: variant versions', async () => {
- let config = {
- prefix: 'tw-',
- content: [
- {
- raw: html`
- <div class="hover:-tw-top-1 hover:tw--top-1"></div>
- <div class="hover:-tw-top-[1px] hover:tw--top-[1px]"></div>
- <div class="hover:tw-top-[-1px]"></div>
- <!-- this one should not generate anything -->
- <div class="-hover:tw-top-1"></div>
- `,
- },
- ],
- corePlugins: { preflight: false },
- }
- let input = css`
- @tailwind utilities;
- `
- await run(input, config)
- const result = await run(input, config)
- expect(result.css).toMatchFormattedCss(css`
- .hover\:-tw-top-1:hover {
- top: -0.25rem;
- }
- .hover\:-tw-top-\[1px\]:hover {
- top: -1px;
- }
- .hover\:tw--top-1:hover {
- top: -0.25rem;
- }
- .hover\:tw--top-\[1px\]:hover,
- .hover\:tw-top-\[-1px\]:hover {
- top: -1px;
- }
- `)
- })
- test('negative values: prefix and apply', async () => {
- let config = {
- prefix: 'tw-',
- content: [{ raw: html`` }],
- corePlugins: { preflight: false },
- }
- let input = css`
- @tailwind utilities;
- .a {
- @apply hover:tw--top-1;
- }
- .b {
- @apply hover:-tw-top-1;
- }
- .c {
- @apply hover:-tw-top-[1px];
- }
- .d {
- @apply hover:tw--top-[1px];
- }
- .e {
- @apply hover:tw-top-[-1px];
- }
- `
- await run(input, config)
- const result = await run(input, config)
- expect(result.css).toMatchFormattedCss(css`
- .a:hover,
- .b:hover {
- top: -0.25rem;
- }
- .c:hover,
- .d:hover,
- .e:hover {
- top: -1px;
- }
- `)
- })
- test('negative values: prefix in the safelist', async () => {
- let config = {
- prefix: 'tw-',
- safelist: [{ pattern: /-tw-top-1/g }, { pattern: /tw--top-1/g }],
- theme: {
- inset: {
- 1: '0.25rem',
- },
- },
- content: [{ raw: html`` }],
- corePlugins: { preflight: false },
- }
- let input = css`
- @tailwind utilities;
- `
- await run(input, config)
- const result = await run(input, config)
- expect(result.css).toMatchFormattedCss(css`
- .-tw-top-1,
- .tw--top-1 {
- top: -0.25rem;
- }
- `)
- })
- test('prefix with negative values and variants in the safelist', async () => {
- let config = {
- prefix: 'tw-',
- safelist: [
- { pattern: /-tw-top-1/, variants: ['hover', 'sm:hover'] },
- { pattern: /tw--top-1/, variants: ['hover', 'sm:hover'] },
- ],
- theme: {
- inset: {
- 1: '0.25rem',
- },
- },
- content: [{ raw: html`` }],
- corePlugins: { preflight: false },
- }
- let input = css`
- @tailwind utilities;
- `
- await run(input, config)
- const result = await run(input, config)
- expect(result.css).toMatchFormattedCss(css`
- .-tw-top-1,
- .tw--top-1,
- .hover\:-tw-top-1:hover,
- .hover\:tw--top-1:hover {
- top: -0.25rem;
- }
- @media (min-width: 640px) {
- .sm\:hover\:-tw-top-1:hover,
- .sm\:hover\:tw--top-1:hover {
- top: -0.25rem;
- }
- }
- `)
- })
- test('prefix does not detect and generate unnecessary classes', async () => {
- let config = {
- prefix: 'tw-_',
- content: [{ raw: html`-aaa-filter aaaa-table aaaa-hidden` }],
- corePlugins: { preflight: false },
- }
- let input = css`
- @tailwind utilities;
- `
- const result = await run(input, config)
- expect(result.css).toMatchFormattedCss(css``)
- })
- test('supports prefixed utilities using arbitrary values', async () => {
- let config = {
- prefix: 'tw-',
- content: [{ raw: html`foo` }],
- corePlugins: { preflight: false },
- }
- let input = css`
- .foo {
- @apply tw-text-[color:rgb(var(--button-background,var(--primary-button-background)))];
- @apply tw-ease-[cubic-bezier(0.77,0,0.175,1)];
- @apply tw-rounded-[min(4px,var(--input-border-radius))];
- }
- `
- const result = await run(input, config)
- expect(result.css).toMatchFormattedCss(css`
- .foo {
- color: rgb(var(--button-background, var(--primary-button-background)));
- border-radius: min(4px, var(--input-border-radius));
- transition-timing-function: cubic-bezier(0.77, 0, 0.175, 1);
- }
- `)
- })
- test('supports non-word prefixes (1)', async () => {
- let config = {
- prefix: '@',
- content: [
- {
- raw: html`
- <div class="@underline"></div>
- <div class="@bg-black"></div>
- <div class="@[color:red]"></div>
- <div class="hover:before:@content-['Hovering']"></div>
- <div class="my-utility"></div>
- <div class="foo"></div>
- <!-- these won't be detected -->
- <div class="overline"></div>
- `,
- },
- ],
- corePlugins: { preflight: false },
- }
- let input = css`
- @tailwind utilities;
- @layer utilities {
- .my-utility {
- color: orange;
- }
- }
- .foo {
- @apply @text-white;
- @apply [background-color:red];
- }
- `
- const result = await run(input, config)
- expect(result.css).toMatchFormattedCss(css`
- .\@bg-black {
- --tw-bg-opacity: 1;
- background-color: rgb(0 0 0 / var(--tw-bg-opacity));
- }
- .\@underline {
- text-decoration-line: underline;
- }
- .my-utility {
- color: orange;
- }
- .foo {
- --tw-text-opacity: 1;
- color: rgb(255 255 255 / var(--tw-text-opacity));
- background-color: red;
- }
- .hover\:before\:\@content-\[\'Hovering\'\]:hover:before {
- --tw-content: 'Hovering';
- content: var(--tw-content);
- }
- `)
- })
- test('supports non-word prefixes (2)', async () => {
- let config = {
- prefix: '@]$',
- content: [
- {
- raw: html`
- <div class="@]$underline"></div>
- <div class="@]$bg-black"></div>
- <div class="@]$[color:red]"></div>
- <div class="hover:before:@]$content-['Hovering']"></div>
- <div class="my-utility"></div>
- <div class="foo"></div>
- <!-- these won't be detected -->
- <div class="overline"></div>
- `,
- },
- ],
- corePlugins: { preflight: false },
- }
- let input = css`
- @tailwind utilities;
- @layer utilities {
- .my-utility {
- color: orange;
- }
- }
- .foo {
- @apply @]$text-white;
- @apply [background-color:red];
- }
- `
- const result = await run(input, config)
- // TODO: The class `.hover\:before\:\@\]\$content-\[\'Hovering\'\]:hover::before` is not generated
- // This happens because of the parenthesis/brace/bracket clipping performed on candidates
- expect(result.css).toMatchFormattedCss(css`
- .\@\]\$bg-black {
- --tw-bg-opacity: 1;
- background-color: rgb(0 0 0 / var(--tw-bg-opacity));
- }
- .\@\]\$underline {
- text-decoration-line: underline;
- }
- .my-utility {
- color: orange;
- }
- .foo {
- --tw-text-opacity: 1;
- color: rgb(255 255 255 / var(--tw-text-opacity));
- background-color: red;
- }
- `)
- })
- test('does not prefix arbitrary group/peer classes', async () => {
- let config = {
- prefix: 'tw-',
- content: [
- {
- raw: html`
- <div class="tw-group tw-peer foo">
- <div class="group-[&.foo]:tw-flex"></div>
- </div>
- <div class="peer-[&.foo]:tw-flex"></div>
- `,
- },
- ],
- corePlugins: { preflight: false },
- }
- let input = css`
- @tailwind utilities;
- `
- const result = await run(input, config)
- expect(result.css).toMatchFormattedCss(css`
- .tw-group.foo .group-\[\&\.foo\]\:tw-flex,
- .tw-peer.foo ~ .peer-\[\&\.foo\]\:tw-flex {
- display: flex;
- }
- `)
- })
|