normalize-data-types.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { css, run } from './util/run'
  2. import { normalize } from '../src/util/dataTypes'
  3. let table = [
  4. ['foo', 'foo'],
  5. ['foo-bar', 'foo-bar'],
  6. ['16/9', '16/9'],
  7. ['16_/_9', '16 / 9'],
  8. // '_'s are converted to spaces except when escaped
  9. ['foo_bar', 'foo bar'],
  10. ['foo__bar', 'foo bar'],
  11. ['foo\\_bar', 'foo_bar'],
  12. // Urls are preserved as-is
  13. [
  14. 'url("https://example.com/abc+def/some-path/2022-01-01-abc/some_underscoered_path")',
  15. 'url("https://example.com/abc+def/some-path/2022-01-01-abc/some_underscoered_path")',
  16. ],
  17. // file-path-like-things without quotes are preserved as-is
  18. ['/foo/bar/baz.png', '/foo/bar/baz.png'],
  19. ['/foo/bar-2/baz.png', '/foo/bar-2/baz.png'],
  20. // var(…) is preserved as is
  21. ['var(--foo)', 'var(--foo)'],
  22. ['var(--headings-h1-size)', 'var(--headings-h1-size)'],
  23. // math functions like calc(…) get spaces around operators
  24. ['calc(1+2)', 'calc(1 + 2)'],
  25. ['calc(100%+1rem)', 'calc(100% + 1rem)'],
  26. ['calc(1+calc(100%-20px))', 'calc(1 + calc(100% - 20px))'],
  27. ['calc(var(--headings-h1-size)*100)', 'calc(var(--headings-h1-size) * 100)'],
  28. [
  29. 'calc(var(--headings-h1-size)*calc(100%+50%))',
  30. 'calc(var(--headings-h1-size) * calc(100% + 50%))',
  31. ],
  32. ['min(1+2)', 'min(1 + 2)'],
  33. ['max(1+2)', 'max(1 + 2)'],
  34. ['clamp(1+2,1+3,1+4)', 'clamp(1 + 2,1 + 3,1 + 4)'],
  35. ['var(--heading-h1-font-size)', 'var(--heading-h1-font-size)'],
  36. ['var(--my-var-with-more-than-3-words)', 'var(--my-var-with-more-than-3-words)'],
  37. ['var(--width, calc(100%+1rem))', 'var(--width, calc(100% + 1rem))'],
  38. ['calc(1px*(7--12/24))', 'calc(1px * (7 - -12 / 24))'],
  39. ['calc((7-32)/(1400-782))', 'calc((7 - 32) / (1400 - 782))'],
  40. ['calc((7-3)/(1400-782))', 'calc((7 - 3) / (1400 - 782))'],
  41. ['calc((7-32)/(1400-782))', 'calc((7 - 32) / (1400 - 782))'],
  42. ['calc((70-3)/(1400-782))', 'calc((70 - 3) / (1400 - 782))'],
  43. ['calc((70-32)/(1400-782))', 'calc((70 - 32) / (1400 - 782))'],
  44. ['calc((704-3)/(1400-782))', 'calc((704 - 3) / (1400 - 782))'],
  45. ['calc((704-320))', 'calc((704 - 320))'],
  46. ['calc((704-320)/1)', 'calc((704 - 320) / 1)'],
  47. ['calc((704-320)/(1400-782))', 'calc((704 - 320) / (1400 - 782))'],
  48. ['calc(24px+-1rem)', 'calc(24px + -1rem)'],
  49. ['calc(24px+(-1rem))', 'calc(24px + (-1rem))'],
  50. ['calc(24px_+_-1rem)', 'calc(24px + -1rem)'],
  51. ['calc(24px+(-1rem))', 'calc(24px + (-1rem))'],
  52. ['calc(24px_+_(-1rem))', 'calc(24px + (-1rem))'],
  53. [
  54. 'calc(var(--10-10px,calc(-20px-(-30px--40px)-50px)',
  55. 'calc(var(--10-10px,calc(-20px - (-30px - -40px) - 50px)',
  56. ],
  57. ['calc(theme(spacing.1-bar))', 'calc(theme(spacing.1-bar))'],
  58. ['theme(spacing.1-bar)', 'theme(spacing.1-bar)'],
  59. ['calc(theme(spacing.1-bar))', 'calc(theme(spacing.1-bar))'],
  60. ['calc(1rem-theme(spacing.1-bar))', 'calc(1rem - theme(spacing.1-bar))'],
  61. ['calc(theme(spacing.foo-2))', 'calc(theme(spacing.foo-2))'],
  62. ['calc(theme(spacing.foo-bar))', 'calc(theme(spacing.foo-bar))'],
  63. // A negative number immediately after a `,` should not have spaces inserted
  64. ['clamp(-3px+4px,-3px+4px,-3px+4px)', 'clamp(-3px + 4px,-3px + 4px,-3px + 4px)'],
  65. // Prevent formatting inside `var()` functions
  66. ['calc(var(--foo-bar-bar)*2)', 'calc(var(--foo-bar-bar) * 2)'],
  67. // Prevent formatting inside `env()` functions
  68. ['calc(env(safe-area-inset-bottom)*2)', 'calc(env(safe-area-inset-bottom) * 2)'],
  69. // Should format inside `calc()` nested in `env()`
  70. ['env(safe-area-inset-bottom,calc(10px+20px))', 'env(safe-area-inset-bottom,calc(10px + 20px))'],
  71. [
  72. 'calc(env(safe-area-inset-bottom,calc(10px+20px))+5px)',
  73. 'calc(env(safe-area-inset-bottom,calc(10px + 20px)) + 5px)',
  74. ],
  75. // Prevent formatting keywords
  76. ['minmax(min-content,25%)', 'minmax(min-content,25%)'],
  77. // Misc
  78. ['color(0_0_0/1.0)', 'color(0 0 0/1.0)'],
  79. ['color(0_0_0_/_1.0)', 'color(0 0 0 / 1.0)'],
  80. ]
  81. it.each(table)('normalize data: %s', (input, output) => {
  82. expect(normalize(input)).toBe(output)
  83. })
  84. it('should not automatically inject the `var()` for properties that accept `<dashed-ident>` as the value', () => {
  85. let config = {
  86. content: [
  87. // Automatic var injection
  88. { raw: '[color:--foo]' },
  89. // Automatic var injection is skipped
  90. { raw: '[timeline-scope:--foo]' },
  91. ],
  92. }
  93. let input = css`
  94. @tailwind utilities;
  95. `
  96. return run(input, config).then((result) => {
  97. expect(result.css).toMatchFormattedCss(css`
  98. .\[color\:--foo\] {
  99. color: var(--foo);
  100. }
  101. .\[timeline-scope\:--foo\] {
  102. timeline-scope: --foo;
  103. }
  104. `)
  105. })
  106. })