Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
properly detect theme value in arbitrary properties
  • Loading branch information
RobinMalfait committed Jan 3, 2022
commit 7534cbde679de01f7c4820a8f10a1da746d831de
1 change: 1 addition & 0 deletions src/lib/defaultExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const PATTERNS = [
/([^<>"'`\s]*\[\w*\("[^'`\s]*"\)\])/.source, // bg-[url("..."),url("...")]
/([^<>"'`\s]*\['[^"'`\s]*'\])/.source, // `content-['hello']` but not `content-['hello']']`
/([^<>"'`\s]*\["[^"'`\s]*"\])/.source, // `content-["hello"]` but not `content-["hello"]"]`
/([^<>"'`\s]*\[[^<>"'`\s]*:[^\]\s]*\])/.source, // `[attr:value]`
/([^<>"'`\s]*\[[^<>"'`\s]*:'[^"'`\s]*'\])/.source, // `[content:'hello']` but not `[content:"hello"]`
/([^<>"'`\s]*\[[^<>"'`\s]*:"[^"'`\s]*"\])/.source, // `[content:"hello"]` but not `[content:'hello']`
/([^<>"'`\s]*\[[^"'`\s]+\][^<>"'`\s]*)/.source, // `fill-[#bada55]`, `fill-[#bada55]/50`
Expand Down
48 changes: 48 additions & 0 deletions tests/arbitrary-properties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,51 @@ test('invalid arbitrary property 2', () => {
expect(result.css).toMatchFormattedCss(css``)
})
})

it('should be possible to read theme values in arbitrary properties (without quotes)', () => {
let config = {
content: [{ raw: html`<div class="[--a:theme(colors.blue.500)] [color:var(--a)]"></div>` }],
corePlugins: { preflight: false },
}

let input = css`
@tailwind base;
@tailwind components;
@tailwind utilities;
`

return run(input, config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.\[--a\:theme\(colors\.blue\.500\)\] {
--a: #3b82f6;
}
.\[color\:var\(--a\)\] {
color: var(--a);
}
`)
})
})

it('should be possible to read theme values in arbitrary properties (with quotes)', () => {
let config = {
content: [{ raw: html`<div class="[--a:theme('colors.blue.500')] [color:var(--a)]"></div>` }],
corePlugins: { preflight: false },
}

let input = css`
@tailwind base;
@tailwind components;
@tailwind utilities;
`

return run(input, config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
.\[--a\:theme\(\'colors\.blue\.500\'\)\] {
--a: #3b82f6;
}
.\[color\:var\(--a\)\] {
color: var(--a);
}
`)
})
})
9 changes: 9 additions & 0 deletions tests/default-extractor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,15 @@ test('arbitrary values with angle brackets in double quotes', async () => {
expect(extractions).toContain(`hover:focus:content-[">"]`)
})

test('arbitrary values with theme lookup using quotes', () => {
const extractions = defaultExtractor(`
<p class="[--y:theme('colors.blue.500')] [color:var(--y)]"></p>
`)

expect(extractions).toContain(`[--y:theme('colors.blue.500')]`)
expect(extractions).toContain(`[color:var(--y)]`)
})

test('special characters', async () => {
const extractions = defaultExtractor(`
<div class="<sm:underline md>:font-bold"></div>
Expand Down