blocklist.test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { run, html, css } from './util/run'
  2. it('can block classes matched literally', () => {
  3. let config = {
  4. content: [
  5. {
  6. raw: html`<div
  7. class="font-bold uppercase sm:hover:text-sm hover:text-sm bg-red-500/50 my-custom-class"
  8. ></div>`,
  9. },
  10. ],
  11. blocklist: ['font', 'uppercase', 'hover:text-sm', 'bg-red-500/50', 'my-custom-class'],
  12. }
  13. let input = css`
  14. @tailwind utilities;
  15. .my-custom-class {
  16. color: red;
  17. }
  18. `
  19. return run(input, config).then((result) => {
  20. return expect(result.css).toMatchFormattedCss(css`
  21. .font-bold {
  22. font-weight: 700;
  23. }
  24. .my-custom-class {
  25. color: red;
  26. }
  27. @media (min-width: 640px) {
  28. .sm\:hover\:text-sm:hover {
  29. font-size: 0.875rem;
  30. line-height: 1.25rem;
  31. }
  32. }
  33. `)
  34. })
  35. })
  36. it('can block classes inside @layer', () => {
  37. let config = {
  38. content: [
  39. {
  40. raw: html`<div class="font-bold my-custom-class"></div>`,
  41. },
  42. ],
  43. blocklist: ['my-custom-class'],
  44. }
  45. let input = css`
  46. @tailwind utilities;
  47. @layer utilities {
  48. .my-custom-class {
  49. color: red;
  50. }
  51. }
  52. `
  53. return run(input, config).then((result) => {
  54. return expect(result.css).toMatchFormattedCss(css`
  55. .font-bold {
  56. font-weight: 700;
  57. }
  58. `)
  59. })
  60. })
  61. it('blocklists do NOT support regexes', async () => {
  62. let config = {
  63. content: [{ raw: html`<div class="font-bold bg-[#f00d1e]"></div>` }],
  64. blocklist: [/^bg-\[[^]+\]$/],
  65. }
  66. let result = await run('@tailwind utilities', config)
  67. expect(result.css).toMatchFormattedCss(css`
  68. .bg-\[\#f00d1e\] {
  69. --tw-bg-opacity: 1;
  70. background-color: rgb(240 13 30 / var(--tw-bg-opacity));
  71. }
  72. .font-bold {
  73. font-weight: 700;
  74. }
  75. `)
  76. expect().toHaveBeenWarnedWith(['blocklist-invalid'])
  77. })
  78. it('can block classes generated by the safelist', () => {
  79. let config = {
  80. content: [{ raw: html`<div class="font-bold"></div>` }],
  81. safelist: [{ pattern: /^bg-red-(400|500)$/ }],
  82. blocklist: ['bg-red-500'],
  83. }
  84. return run('@tailwind utilities', config).then((result) => {
  85. expect(result.css).toMatchFormattedCss(css`
  86. .bg-red-400 {
  87. --tw-bg-opacity: 1;
  88. background-color: rgb(248 113 113 / var(--tw-bg-opacity));
  89. }
  90. .font-bold {
  91. font-weight: 700;
  92. }
  93. `)
  94. })
  95. })