|
| 1 | +import { test, expect, describe } from 'vitest' |
| 2 | +import dedent from 'dedent' |
| 3 | +import { createClient } from './client' |
| 4 | + |
| 5 | +const css = dedent |
| 6 | + |
| 7 | +const rgb = (red: number, green: number, blue: number, alpha: number = 1) => ({ |
| 8 | + red, |
| 9 | + green, |
| 10 | + blue, |
| 11 | + alpha, |
| 12 | +}) |
| 13 | + |
| 14 | +const range = (startLine: number, startCol: number, endLine: number, endCol: number) => ({ |
| 15 | + start: { line: startLine, character: startCol }, |
| 16 | + end: { line: endLine, character: endCol }, |
| 17 | +}) |
| 18 | + |
| 19 | +describe('v4', async () => { |
| 20 | + let client = await createClient({ |
| 21 | + config: { |
| 22 | + kind: 'css', |
| 23 | + content: css` |
| 24 | + @theme { |
| 25 | + --color-black: #000; |
| 26 | + --color-primary: #f00; |
| 27 | + --color-light-dark: light-dark(#ff0000, #0000ff); |
| 28 | + } |
| 29 | + `, |
| 30 | + }, |
| 31 | + }) |
| 32 | + |
| 33 | + test('named', async () => { |
| 34 | + let doc = await client.open({ |
| 35 | + lang: 'html', |
| 36 | + text: '<div class="bg-primary">', |
| 37 | + }) |
| 38 | + |
| 39 | + expect(await doc.documentColors()).toEqual([ |
| 40 | + // |
| 41 | + { range: range(0, 12, 0, 22), color: rgb(1, 0, 0) }, |
| 42 | + ]) |
| 43 | + }) |
| 44 | + |
| 45 | + test('named + opacity modifier', async () => { |
| 46 | + let doc = await client.open({ |
| 47 | + lang: 'html', |
| 48 | + text: '<div class="bg-primary/20">', |
| 49 | + }) |
| 50 | + |
| 51 | + expect(await doc.documentColors()).toEqual([ |
| 52 | + // |
| 53 | + { range: range(0, 12, 0, 25), color: rgb(1, 0, 0, 0.2) }, |
| 54 | + ]) |
| 55 | + }) |
| 56 | + |
| 57 | + test('named + arbitrary opacity modifier', async () => { |
| 58 | + let doc = await client.open({ |
| 59 | + lang: 'html', |
| 60 | + text: '<div class="bg-primary/[0.125]">', |
| 61 | + }) |
| 62 | + |
| 63 | + expect(await doc.documentColors()).toEqual([ |
| 64 | + // |
| 65 | + { range: range(0, 12, 0, 30), color: rgb(1, 0, 0, 0.13) }, |
| 66 | + ]) |
| 67 | + }) |
| 68 | + |
| 69 | + test('arbitrary value', async () => { |
| 70 | + let doc = await client.open({ |
| 71 | + lang: 'html', |
| 72 | + text: '<div class="bg-[red]">', |
| 73 | + }) |
| 74 | + |
| 75 | + expect(await doc.documentColors()).toEqual([ |
| 76 | + // |
| 77 | + { range: range(0, 12, 0, 20), color: rgb(1, 0, 0) }, |
| 78 | + ]) |
| 79 | + }) |
| 80 | + |
| 81 | + test('arbitrary value + opacity modifier', async () => { |
| 82 | + let doc = await client.open({ |
| 83 | + lang: 'html', |
| 84 | + text: '<div class="bg-[red]/20">', |
| 85 | + }) |
| 86 | + |
| 87 | + expect(await doc.documentColors()).toEqual([ |
| 88 | + // |
| 89 | + { range: range(0, 12, 0, 23), color: rgb(1, 0, 0, 0.2) }, |
| 90 | + ]) |
| 91 | + }) |
| 92 | + |
| 93 | + test('arbitrary value + arbitrary opacity modifier', async () => { |
| 94 | + let doc = await client.open({ |
| 95 | + lang: 'html', |
| 96 | + text: '<div class="bg-[red]/[0.125]">', |
| 97 | + }) |
| 98 | + |
| 99 | + expect(await doc.documentColors()).toEqual([ |
| 100 | + // |
| 101 | + { range: range(0, 12, 0, 28), color: rgb(1, 0, 0, 0.13) }, |
| 102 | + ]) |
| 103 | + }) |
| 104 | + |
| 105 | + test('an opacity modifier of zero is ignored', async () => { |
| 106 | + let doc = await client.open({ |
| 107 | + lang: 'html', |
| 108 | + text: '<div class="bg-primary/0 bg-[red]/0 bg-primary/[0] bg-[red]/[0]">', |
| 109 | + }) |
| 110 | + |
| 111 | + expect(await doc.documentColors()).toEqual([]) |
| 112 | + }) |
| 113 | + |
| 114 | + test('oklch colors are supported', async () => { |
| 115 | + let doc = await client.open({ |
| 116 | + lang: 'html', |
| 117 | + text: '<div class="bg-[oklch(60%_0.25_25)]', |
| 118 | + }) |
| 119 | + |
| 120 | + expect(await doc.documentColors()).toEqual([ |
| 121 | + // |
| 122 | + { range: range(0, 12, 0, 35), color: rgb(0.9475942429386454, 0, 0.14005415620741646) }, |
| 123 | + ]) |
| 124 | + }) |
| 125 | + |
| 126 | + test('gradient colors are supported', async () => { |
| 127 | + let doc = await client.open({ |
| 128 | + lang: 'html', |
| 129 | + text: '<div class="from-black from-black/50 via-black via-black/50 to-black to-black/50">', |
| 130 | + }) |
| 131 | + |
| 132 | + expect(await doc.documentColors()).toEqual([ |
| 133 | + // from-black from-black/50 |
| 134 | + { range: range(0, 12, 0, 22), color: rgb(0, 0, 0) }, |
| 135 | + { range: range(0, 23, 0, 36), color: rgb(0, 0, 0, 0.5) }, |
| 136 | + |
| 137 | + // via-black via-black/50 |
| 138 | + { range: range(0, 37, 0, 46), color: rgb(0, 0, 0) }, |
| 139 | + { range: range(0, 47, 0, 59), color: rgb(0, 0, 0, 0.5) }, |
| 140 | + |
| 141 | + // to-black to-black/50 |
| 142 | + { range: range(0, 60, 0, 68), color: rgb(0, 0, 0) }, |
| 143 | + { range: range(0, 69, 0, 80), color: rgb(0, 0, 0, 0.5) }, |
| 144 | + ]) |
| 145 | + }) |
| 146 | + |
| 147 | + test('light-dark() resolves to the light color', async () => { |
| 148 | + let doc = await client.open({ |
| 149 | + lang: 'html', |
| 150 | + text: '<div class="bg-light-dark">', |
| 151 | + }) |
| 152 | + |
| 153 | + let colors = await doc.documentColors() |
| 154 | + |
| 155 | + expect(colors).toEqual([ |
| 156 | + // |
| 157 | + { range: range(0, 12, 0, 25), color: rgb(1, 0, 0, 1) }, |
| 158 | + ]) |
| 159 | + }) |
| 160 | + |
| 161 | + test('colors are recursively resolved from the theme', async () => { |
| 162 | + let client = await createClient({ |
| 163 | + config: { |
| 164 | + kind: 'css', |
| 165 | + content: css` |
| 166 | + @theme { |
| 167 | + --color-primary: #ff0000; |
| 168 | + --color-level-1: var(--color-primary); |
| 169 | + --color-level-2: var(--color-level-1); |
| 170 | + --color-level-3: var(--color-level-2); |
| 171 | + --color-level-4: var(--color-level-3); |
| 172 | + --color-level-5: var(--color-level-4); |
| 173 | + } |
| 174 | + `, |
| 175 | + }, |
| 176 | + }) |
| 177 | + |
| 178 | + let doc = await client.open({ |
| 179 | + lang: 'html', |
| 180 | + text: '<div class="bg-primary bg-level-1 bg-level-2 bg-level-3 bg-level-4 bg-level-5">', |
| 181 | + }) |
| 182 | + |
| 183 | + let colors = await doc.documentColors() |
| 184 | + |
| 185 | + expect(colors).toEqual([ |
| 186 | + { range: range(0, 12, 0, 22), color: rgb(1, 0, 0, 1) }, |
| 187 | + { range: range(0, 23, 0, 33), color: rgb(1, 0, 0, 1) }, |
| 188 | + { range: range(0, 34, 0, 44), color: rgb(1, 0, 0, 1) }, |
| 189 | + { range: range(0, 45, 0, 55), color: rgb(1, 0, 0, 1) }, |
| 190 | + { range: range(0, 56, 0, 66), color: rgb(1, 0, 0, 1) }, |
| 191 | + { range: range(0, 67, 0, 77), color: rgb(1, 0, 0, 1) }, |
| 192 | + ]) |
| 193 | + }) |
| 194 | +}) |
0 commit comments