arbitrary-properties.test.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import { run, html, css, defaults } from './util/run'
  2. test('basic arbitrary properties', () => {
  3. let config = {
  4. content: [
  5. {
  6. raw: html`<div class="[paint-order:markers]"></div>`,
  7. },
  8. ],
  9. corePlugins: { preflight: false },
  10. }
  11. let input = css`
  12. @tailwind base;
  13. @tailwind components;
  14. @tailwind utilities;
  15. `
  16. return run(input, config).then((result) => {
  17. expect(result.css).toMatchFormattedCss(css`
  18. ${defaults}
  19. .\[paint-order\:markers\] {
  20. paint-order: markers;
  21. }
  22. `)
  23. })
  24. })
  25. test('arbitrary properties with modifiers', () => {
  26. let config = {
  27. content: [
  28. {
  29. raw: html`<div class="dark:lg:hover:[paint-order:markers]"></div>`,
  30. },
  31. ],
  32. corePlugins: { preflight: false },
  33. }
  34. let input = css`
  35. @tailwind base;
  36. @tailwind components;
  37. @tailwind utilities;
  38. `
  39. return run(input, config).then((result) => {
  40. expect(result.css).toMatchFormattedCss(css`
  41. ${defaults}
  42. @media (prefers-color-scheme: dark) {
  43. @media (min-width: 1024px) {
  44. .dark\:lg\:hover\:\[paint-order\:markers\]:hover {
  45. paint-order: markers;
  46. }
  47. }
  48. }
  49. `)
  50. })
  51. })
  52. test('arbitrary properties are sorted after utilities', () => {
  53. let config = {
  54. content: [
  55. {
  56. raw: html`<div class="content-none [paint-order:markers] hover:pointer-events-none"></div>`,
  57. },
  58. ],
  59. corePlugins: { preflight: false },
  60. }
  61. let input = css`
  62. @tailwind base;
  63. @tailwind components;
  64. @tailwind utilities;
  65. `
  66. return run(input, config).then((result) => {
  67. expect(result.css).toMatchFormattedCss(css`
  68. ${defaults}
  69. .content-none {
  70. --tw-content: none;
  71. content: var(--tw-content);
  72. }
  73. .\[paint-order\:markers\] {
  74. paint-order: markers;
  75. }
  76. .hover\:pointer-events-none:hover {
  77. pointer-events: none;
  78. }
  79. `)
  80. })
  81. })
  82. test('using CSS variables', () => {
  83. let config = {
  84. content: [
  85. {
  86. raw: html`<div class="[--my-var:auto]"></div>`,
  87. },
  88. ],
  89. corePlugins: { preflight: false },
  90. }
  91. let input = css`
  92. @tailwind base;
  93. @tailwind components;
  94. @tailwind utilities;
  95. `
  96. return run(input, config).then((result) => {
  97. expect(result.css).toMatchFormattedCss(css`
  98. ${defaults}
  99. .\[--my-var\:auto\] {
  100. --my-var: auto;
  101. }
  102. `)
  103. })
  104. })
  105. test('using underscores as spaces', () => {
  106. let config = {
  107. content: [
  108. {
  109. raw: html`<div class="[--my-var:2px_4px]"></div>`,
  110. },
  111. ],
  112. corePlugins: { preflight: false },
  113. }
  114. let input = css`
  115. @tailwind base;
  116. @tailwind components;
  117. @tailwind utilities;
  118. `
  119. return run(input, config).then((result) => {
  120. expect(result.css).toMatchFormattedCss(css`
  121. ${defaults}
  122. .\[--my-var\:2px_4px\] {
  123. --my-var: 2px 4px;
  124. }
  125. `)
  126. })
  127. })
  128. test('using the important modifier', () => {
  129. let config = {
  130. content: [
  131. {
  132. raw: html`<div class="![--my-var:2px_4px]"></div>`,
  133. },
  134. ],
  135. corePlugins: { preflight: false },
  136. }
  137. let input = css`
  138. @tailwind base;
  139. @tailwind components;
  140. @tailwind utilities;
  141. `
  142. return run(input, config).then((result) => {
  143. expect(result.css).toMatchFormattedCss(css`
  144. ${defaults}
  145. .\!\[--my-var\:2px_4px\] {
  146. --my-var: 2px 4px !important;
  147. }
  148. `)
  149. })
  150. })
  151. test('colons are allowed in quotes', () => {
  152. let config = {
  153. content: [
  154. {
  155. raw: html`<div class="[content:'foo:bar']"></div>`,
  156. },
  157. ],
  158. corePlugins: { preflight: false },
  159. }
  160. let input = css`
  161. @tailwind base;
  162. @tailwind components;
  163. @tailwind utilities;
  164. `
  165. return run(input, config).then((result) => {
  166. expect(result.css).toMatchFormattedCss(css`
  167. ${defaults}
  168. .\[content\:\'foo\:bar\'\] {
  169. content: 'foo:bar';
  170. }
  171. `)
  172. })
  173. })
  174. test('colons are allowed in braces', () => {
  175. let config = {
  176. content: [
  177. {
  178. raw: html`<div class="[background-image:url(http://example.com/picture.jpg)]"></div>`,
  179. },
  180. ],
  181. corePlugins: { preflight: false },
  182. }
  183. let input = css`
  184. @tailwind base;
  185. @tailwind components;
  186. @tailwind utilities;
  187. `
  188. return run(input, config).then((result) => {
  189. expect(result.css).toMatchFormattedCss(css`
  190. ${defaults}
  191. .\[background-image\:url\(http\:\/\/example\.com\/picture\.jpg\)\] {
  192. background-image: url(http://example.com/picture.jpg);
  193. }
  194. `)
  195. })
  196. })
  197. test('invalid class', () => {
  198. let config = {
  199. content: [
  200. {
  201. raw: html`<div class="[a:b:c:d]"></div>`,
  202. },
  203. ],
  204. corePlugins: { preflight: false },
  205. }
  206. let input = css`
  207. @tailwind base;
  208. @tailwind components;
  209. @tailwind utilities;
  210. `
  211. return run(input, config).then((result) => {
  212. expect(result.css).toMatchFormattedCss(css`
  213. ${defaults}
  214. `)
  215. })
  216. })
  217. test('invalid arbitrary property', () => {
  218. let config = {
  219. content: [
  220. {
  221. raw: html`<div class="[autoplay:\${autoplay}]"></div>`,
  222. },
  223. ],
  224. corePlugins: { preflight: false },
  225. }
  226. let input = css`
  227. @tailwind base;
  228. @tailwind components;
  229. @tailwind utilities;
  230. `
  231. return run(input, config).then((result) => {
  232. expect(result.css).toMatchFormattedCss(css`
  233. ${defaults}
  234. `)
  235. })
  236. })
  237. test('invalid arbitrary property 2', () => {
  238. let config = {
  239. content: [
  240. {
  241. raw: html`[0:02]`,
  242. },
  243. ],
  244. corePlugins: { preflight: false },
  245. }
  246. let input = css`
  247. @tailwind base;
  248. @tailwind components;
  249. @tailwind utilities;
  250. `
  251. return run(input, config).then((result) => {
  252. expect(result.css).toMatchFormattedCss(css`
  253. ${defaults}
  254. `)
  255. })
  256. })
  257. it('should be possible to read theme values in arbitrary properties (without quotes)', () => {
  258. let config = {
  259. content: [{ raw: html`<div class="[--a:theme(colors.blue.500)] [color:var(--a)]"></div>` }],
  260. corePlugins: { preflight: false },
  261. }
  262. let input = css`
  263. @tailwind base;
  264. @tailwind components;
  265. @tailwind utilities;
  266. `
  267. return run(input, config).then((result) => {
  268. return expect(result.css).toMatchFormattedCss(css`
  269. ${defaults}
  270. .\[--a\:theme\(colors\.blue\.500\)\] {
  271. --a: #3b82f6;
  272. }
  273. .\[color\:var\(--a\)\] {
  274. color: var(--a);
  275. }
  276. `)
  277. })
  278. })
  279. it('should be possible to read theme values in arbitrary properties (with quotes)', () => {
  280. let config = {
  281. content: [{ raw: html`<div class="[--a:theme('colors.blue.500')] [color:var(--a)]"></div>` }],
  282. corePlugins: { preflight: false },
  283. }
  284. let input = css`
  285. @tailwind base;
  286. @tailwind components;
  287. @tailwind utilities;
  288. `
  289. return run(input, config).then((result) => {
  290. return expect(result.css).toMatchFormattedCss(css`
  291. ${defaults}
  292. .\[--a\:theme\(\'colors\.blue\.500\'\)\] {
  293. --a: #3b82f6;
  294. }
  295. .\[color\:var\(--a\)\] {
  296. color: var(--a);
  297. }
  298. `)
  299. })
  300. })
  301. it('should not generate invalid CSS', () => {
  302. let config = {
  303. content: [
  304. {
  305. raw: html`
  306. <div class="[https://en.wikipedia.org/wiki]"></div>
  307. <div class="[http://example.org]"></div>
  308. <div class="[http://example]"></div>
  309. <div class="[ftp://example]"></div>
  310. <div class="[stillworks:/example]"></div>
  311. `,
  312. // NOTE: In this case `stillworks:/example` being generated is not ideal
  313. // but it at least doesn't produce invalid CSS when run through prettier
  314. // So we can let it through since it is technically valid
  315. },
  316. ],
  317. corePlugins: { preflight: false },
  318. }
  319. return run('@tailwind utilities', config).then((result) => {
  320. return expect(result.css).toMatchFormattedCss(css`
  321. .\[stillworks\:\/example\] {
  322. stillworks: /example;
  323. }
  324. `)
  325. })
  326. })